Skip to content

Commit 2d2ffc8

Browse files
authored
Merge pull request #172 from BeAPI/issue/165
Issue/165
2 parents 6f351c4 + 3082c1f commit 2d2ffc8

File tree

9 files changed

+715
-33
lines changed

9 files changed

+715
-33
lines changed

functions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ function () {
1111
}
1212
);
1313
require_once __DIR__ . '/inc/Helpers/Svg.php';
14+
require_once __DIR__ . '/inc/Helpers/Formatting/Escape.php';
15+
require_once __DIR__ . '/inc/Helpers/Formatting/Image.php';
16+
require_once __DIR__ . '/inc/Helpers/Formatting/Link.php';
17+
require_once __DIR__ . '/inc/Helpers/Formatting/Share.php';
18+
require_once __DIR__ . '/inc/Helpers/Formatting/Term.php';
19+
require_once __DIR__ . '/inc/Helpers/Formatting/Text.php';

inc/Helpers/Accessible_Menu_Walker.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

inc/Helpers/Formatting/Escape.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace BEA\Theme\Framework\Helpers\Formatting\Escape;
3+
4+
/**
5+
* Method for escaping attributes
6+
*
7+
* @param string $value Value to be escaped
8+
* @param string $escape We can pass an anonymous function or method in native escape form as a string (example: 'esc_url'). By default the method escapes the attributes
9+
*
10+
* @return mixed Return value escape
11+
*/
12+
function escape_attribute_value( string $value, string $escape ) {
13+
if ( ! function_exists( $escape ) ) {
14+
return $value;
15+
}
16+
17+
return $escape( $value );
18+
}
19+
20+
/**
21+
* Method for escaping html or content
22+
*
23+
* @param string $value Value to be escaped
24+
* @param string $escape We can pass an anonymous function or method in native escape form as a string (example: 'wp_kses'). By default the method escapes the html
25+
*
26+
* @return mixed Return value escape
27+
*/
28+
function escape_content_value( string $value, string $escape ) {
29+
if ( ! function_exists( $escape ) ) {
30+
return $value;
31+
}
32+
33+
return $escape( $value );
34+
}

inc/Helpers/Formatting/Image.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
namespace BEA\Theme\Framework\Helpers\Formatting\Image;
3+
4+
/**
5+
* @usage BEA\Theme\Framework\Helpers\Formatting\Image\get_the_image( 1, [ 'data-location' => 'image-size' ] );
6+
*
7+
* @param int $image_id Attachment post ID
8+
*
9+
* @param array $attributes {
10+
* Attributes for the image markup.
11+
*
12+
* @type string $src Image attachment URL.
13+
* @type string $data-location Location of image.
14+
* @type string $class CSS class name or space-separated list of classes.
15+
* Default `attachment-$size_class size-$size_class`,
16+
* where `$size_class` is the image size being requested.
17+
* @type string $alt Image description for the alt attribute.
18+
* @type string $srcset The 'srcset' attribute value.
19+
* @type string $sizes The 'sizes' attribute value.
20+
* @type string|false $loading The 'loading' attribute value. Passing a value of false
21+
* will result in the attribute being omitted for the image.
22+
* Defaults to 'lazy', depending on wp_lazy_loading_enabled().
23+
* }
24+
*
25+
* @param array $settings {
26+
* Optional. Settings for the image markup.
27+
*
28+
* @type string $size Optional. The 'sizes' attribute value.
29+
* @type string $before Optional. Markup to prepend to the image. Default empty.
30+
* @type string $after Optional. Markup to append to the image. Default empty.
31+
*
32+
* }
33+
*
34+
* @return string Return the markup of the image
35+
*/
36+
function get_the_image( int $image_id, array $attributes, array $settings = [] ): string {
37+
if ( $image_id <= 0 ) {
38+
return '';
39+
}
40+
41+
$attributes = wp_parse_args(
42+
$attributes,
43+
[
44+
'data-location' => '',
45+
'class' => '',
46+
]
47+
);
48+
49+
$attributes = apply_filters( 'bea_theme_framework_the_image_attributes', $attributes, $image_id, $settings );
50+
51+
$settings = wp_parse_args(
52+
$settings,
53+
[
54+
'size' => 'thumbnail',
55+
'before' => '',
56+
'after' => '',
57+
]
58+
);
59+
60+
$settings = apply_filters( 'bea_theme_framework_the_image_settings', $settings, $image_id, $attributes );
61+
$image_markup = \wp_get_attachment_image(
62+
$image_id,
63+
$settings['size'],
64+
false,
65+
$attributes
66+
);
67+
68+
if ( empty( $image_markup ) ) {
69+
return '';
70+
}
71+
72+
$image_markup = apply_filters( 'bea_theme_framework_the_image_markup', $image_markup, $image_id, $attributes, $settings );
73+
74+
return $settings['before'] . $image_markup . $settings['after'];
75+
}
76+
77+
/**
78+
* @usage BEA\Theme\Framework\Helpers\Formatting\Image\the_image( 1, [ 'data-location' => 'image-size' ], ['before' => '');
79+
*
80+
* @param int $image_id Attachment post ID
81+
*
82+
* @param array $attributes {
83+
* Attributes for the image markup.
84+
*
85+
* @type string $src Image attachment URL.
86+
* @type string $data-location Location of image.
87+
* @type string $class CSS class name or space-separated list of classes.
88+
* Default `attachment-$size_class size-$size_class`,
89+
* where `$size_class` is the image size being requested.
90+
* @type string $alt Image description for the alt attribute.
91+
* @type string $srcset The 'srcset' attribute value.
92+
* @type string $sizes The 'sizes' attribute value.
93+
* @type string|false $loading The 'loading' attribute value. Passing a value of false
94+
* will result in the attribute being omitted for the image.
95+
* Defaults to 'lazy', depending on wp_lazy_loading_enabled().
96+
* }
97+
*
98+
* @param array $settings {
99+
* Optional. Settings for the image markup.
100+
*
101+
* @type string $size Optional. The 'sizes' attribute value.
102+
* @type string $before Optional. Markup to prepend to the image. Default empty.
103+
* @type string $after Optional. Markup to append to the image. Default empty.
104+
*
105+
* }
106+
*
107+
* @return void Echo of the image markup
108+
*/
109+
function the_image( int $image_id, array $attributes, array $settings = [] ): void {
110+
echo get_the_image( $image_id, $attributes, $settings );
111+
}

0 commit comments

Comments
 (0)