-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathImage.php
More file actions
123 lines (111 loc) · 4.5 KB
/
Image.php
File metadata and controls
123 lines (111 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace BEA\Theme\Framework\Helpers\Formatting\Image;
/**
* @usage BEA\Theme\Framework\Helpers\Formatting\Image\get_the_image( 1, [ 'data-location' => 'image-size' ] );
*
* @param int $image_id Attachment post ID
*
* @param array $attributes {
* Attributes for the image markup.
*
* @type string $src Image attachment URL.
* @type string $data-location Location of image.
* @type string $class CSS class name or space-separated list of classes.
* Default `attachment-$size_class size-$size_class`,
* where `$size_class` is the image size being requested.
* @type string $alt Image description for the alt attribute.
* @type string $srcset The 'srcset' attribute value.
* @type string $sizes The 'sizes' attribute value.
* @type string|false $loading The 'loading' attribute value. Passing a value of false
* will result in the attribute being omitted for the image.
* Defaults to 'lazy', depending on wp_lazy_loading_enabled().
* }
*
* @param array $settings {
* Optional. Settings for the image markup.
*
* @type string $size Optional. The 'sizes' attribute value.
* @type string $before Optional. Markup to prepend to the image. Default empty.
* @type string $after Optional. Markup to append to the image. Default empty.
* @type boolean $default Optional. If WP image does not exists, display a default image.
*
* }
*
* @return string Return the markup of the image
*/
function get_the_image( int $image_id, array $attributes, array $settings = [] ): string {
// When the caller passes 'alt' => '', it was overridden by filters/wp_get_attachment_image with the attachment alt from BO.
// We remember the intent here and force empty alt in the final markup below so decorative images get alt="" as intended.
$force_empty_alt = array_key_exists( 'alt', $attributes ) && '' === $attributes['alt'];
$attributes = wp_parse_args(
$attributes,
[
'data-location' => '',
'class' => '',
]
);
$attributes = apply_filters( 'bea_theme_framework_the_image_attributes', $attributes, $image_id, $settings );
$settings = wp_parse_args(
$settings,
[
'size' => 'thumbnail',
'before' => '',
'after' => '',
'default' => false,
]
);
$settings = apply_filters( 'bea_theme_framework_the_image_settings', $settings, $image_id, $attributes );
if ( 0 === $image_id && ! $settings['default'] ) {
return '';
}
$image_markup = \wp_get_attachment_image(
$image_id,
$settings['size'],
false,
$attributes
);
if ( empty( $image_markup ) ) {
return '';
}
$image_markup = apply_filters( 'bea_theme_framework_the_image_markup', $image_markup, $image_id, $attributes, $settings );
// Force alt="" in markup when empty alt was requested (see $force_empty_alt above).
if ( $force_empty_alt ) {
$image_markup = preg_replace( '/\salt="[^"]*"/', ' alt=""', $image_markup );
}
return $settings['before'] . $image_markup . $settings['after'];
}
/**
* @usage BEA\Theme\Framework\Helpers\Formatting\Image\the_image( 1, [ 'data-location' => 'image-size' ], ['before' => '');
*
* @param int $image_id Attachment post ID
*
* @param array $attributes {
* Attributes for the image markup.
*
* @type string $src Image attachment URL.
* @type string $data-location Location of image.
* @type string $class CSS class name or space-separated list of classes.
* Default `attachment-$size_class size-$size_class`,
* where `$size_class` is the image size being requested.
* @type string $alt Image description for the alt attribute.
* @type string $srcset The 'srcset' attribute value.
* @type string $sizes The 'sizes' attribute value.
* @type string|false $loading The 'loading' attribute value. Passing a value of false
* will result in the attribute being omitted for the image.
* Defaults to 'lazy', depending on wp_lazy_loading_enabled().
* }
*
* @param array $settings {
* Optional. Settings for the image markup.
*
* @type string $size Optional. The 'sizes' attribute value.
* @type string $before Optional. Markup to prepend to the image. Default empty.
* @type string $after Optional. Markup to append to the image. Default empty.
*
* }
*
* @return void Echo of the image markup
*/
function the_image( int $image_id, array $attributes, array $settings = [] ): void {
echo get_the_image( $image_id, $attributes, $settings );
}