| author | ymh <ymh.work@gmail.com> |
| Fri, 05 Sep 2025 18:40:08 +0200 | |
| changeset 21 | 48c4eec2b7e6 |
| parent 19 | 3d72ae0968f4 |
| child 22 | 8c2e4d02f4ef |
| permissions | -rw-r--r-- |
| 0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress API for media display. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Media |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
10 |
* Retrieves additional image sizes. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
* @since 4.7.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @global array $_wp_additional_image_sizes |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* @return array Additional images size data. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
function wp_get_additional_image_sizes() {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
global $_wp_additional_image_sizes; |
| 16 | 20 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
if ( ! $_wp_additional_image_sizes ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
$_wp_additional_image_sizes = array(); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
} |
| 16 | 24 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
return $_wp_additional_image_sizes; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
* Scales down the default size of an image. |
| 0 | 30 |
* |
31 |
* This is so that the image is a better fit for the editor and theme. |
|
32 |
* |
|
| 5 | 33 |
* The `$size` parameter accepts either an array or a string. The supported string |
| 0 | 34 |
* values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at |
35 |
* 128 width and 96 height in pixels. Also supported for the string value is |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* 'medium', 'medium_large' and 'full'. The 'full' isn't actually supported, but any value other |
| 0 | 37 |
* than the supported will result in the content_width size or 500 if that is |
38 |
* not set. |
|
39 |
* |
|
| 5 | 40 |
* Finally, there is a filter named {@see 'editor_max_image_size'}, that will be
|
| 16 | 41 |
* called on the calculated array for width and height, respectively. |
| 0 | 42 |
* |
43 |
* @since 2.5.0 |
|
44 |
* |
|
| 18 | 45 |
* @global int $content_width |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* |
| 5 | 47 |
* @param int $width Width of the image in pixels. |
48 |
* @param int $height Height of the image in pixels. |
|
| 18 | 49 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
50 |
* of width and height values in pixels (in that order). Default 'medium'. |
|
| 5 | 51 |
* @param string $context Optional. Could be 'display' (like in a theme) or 'edit' |
52 |
* (like inserting into an editor). Default null. |
|
| 16 | 53 |
* @return int[] {
|
54 |
* An array of width and height values. |
|
55 |
* |
|
56 |
* @type int $0 The maximum width in pixels. |
|
57 |
* @type int $1 The maximum height in pixels. |
|
58 |
* } |
|
| 0 | 59 |
*/ |
| 5 | 60 |
function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
global $content_width; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
$_wp_additional_image_sizes = wp_get_additional_image_sizes(); |
| 0 | 64 |
|
| 9 | 65 |
if ( ! $context ) {
|
| 0 | 66 |
$context = is_admin() ? 'edit' : 'display'; |
67 |
} |
|
| 9 | 68 |
|
69 |
if ( is_array( $size ) ) {
|
|
70 |
$max_width = $size[0]; |
|
71 |
$max_height = $size[1]; |
|
| 16 | 72 |
} elseif ( 'thumb' === $size || 'thumbnail' === $size ) {
|
| 18 | 73 |
$max_width = (int) get_option( 'thumbnail_size_w' ); |
74 |
$max_height = (int) get_option( 'thumbnail_size_h' ); |
|
| 16 | 75 |
// Last chance thumbnail size defaults. |
| 9 | 76 |
if ( ! $max_width && ! $max_height ) {
|
77 |
$max_width = 128; |
|
| 0 | 78 |
$max_height = 96; |
79 |
} |
|
| 16 | 80 |
} elseif ( 'medium' === $size ) {
|
| 18 | 81 |
$max_width = (int) get_option( 'medium_size_w' ); |
82 |
$max_height = (int) get_option( 'medium_size_h' ); |
|
| 9 | 83 |
|
| 16 | 84 |
} elseif ( 'medium_large' === $size ) {
|
| 18 | 85 |
$max_width = (int) get_option( 'medium_large_size_w' ); |
86 |
$max_height = (int) get_option( 'medium_large_size_h' ); |
|
87 |
||
88 |
if ( (int) $content_width > 0 ) {
|
|
89 |
$max_width = min( (int) $content_width, $max_width ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
} |
| 16 | 91 |
} elseif ( 'large' === $size ) {
|
| 5 | 92 |
/* |
93 |
* We're inserting a large size image into the editor. If it's a really |
|
94 |
* big image we'll scale it down to fit reasonably within the editor |
|
95 |
* itself, and within the theme's content width if it's known. The user |
|
96 |
* can resize it in the editor if they wish. |
|
97 |
*/ |
|
| 18 | 98 |
$max_width = (int) get_option( 'large_size_w' ); |
99 |
$max_height = (int) get_option( 'large_size_h' ); |
|
100 |
||
101 |
if ( (int) $content_width > 0 ) {
|
|
102 |
$max_width = min( (int) $content_width, $max_width ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
} |
| 16 | 104 |
} elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) {
|
| 18 | 105 |
$max_width = (int) $_wp_additional_image_sizes[ $size ]['width']; |
106 |
$max_height = (int) $_wp_additional_image_sizes[ $size ]['height']; |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
// Only in admin. Assume that theme authors know what they're doing. |
| 18 | 108 |
if ( (int) $content_width > 0 && 'edit' === $context ) {
|
109 |
$max_width = min( (int) $content_width, $max_width ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
} |
| 16 | 111 |
} else { // $size === 'full' has no constraint.
|
| 9 | 112 |
$max_width = $width; |
| 0 | 113 |
$max_height = $height; |
114 |
} |
|
115 |
||
| 5 | 116 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
* Filters the maximum image size dimensions for the editor. |
| 5 | 118 |
* |
119 |
* @since 2.5.0 |
|
120 |
* |
|
| 16 | 121 |
* @param int[] $max_image_size {
|
122 |
* An array of width and height values. |
|
123 |
* |
|
124 |
* @type int $0 The maximum width in pixels. |
|
125 |
* @type int $1 The maximum height in pixels. |
|
126 |
* } |
|
| 18 | 127 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
128 |
* an array of width and height values in pixels (in that order). |
|
129 |
* @param string $context The context the image is being resized for. |
|
130 |
* Possible values are 'display' (like in a theme) |
|
131 |
* or 'edit' (like inserting into an editor). |
|
| 5 | 132 |
*/ |
| 0 | 133 |
list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); |
134 |
||
135 |
return wp_constrain_dimensions( $width, $height, $max_width, $max_height ); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
139 |
* Retrieves width and height attributes using given width and height values. |
| 0 | 140 |
* |
141 |
* Both attributes are required in the sense that both parameters must have a |
|
142 |
* value, but are optional in that if you set them to false or null, then they |
|
143 |
* will not be added to the returned string. |
|
144 |
* |
|
145 |
* You can set the value using a string, but it will only take numeric values. |
|
146 |
* If you wish to put 'px' after the numbers, then it will be stripped out of |
|
147 |
* the return. |
|
148 |
* |
|
149 |
* @since 2.5.0 |
|
150 |
* |
|
| 5 | 151 |
* @param int|string $width Image width in pixels. |
152 |
* @param int|string $height Image height in pixels. |
|
| 0 | 153 |
* @return string HTML attributes for width and, or height. |
154 |
*/ |
|
| 5 | 155 |
function image_hwstring( $width, $height ) {
|
| 0 | 156 |
$out = ''; |
| 9 | 157 |
if ( $width ) {
|
| 18 | 158 |
$out .= 'width="' . (int) $width . '" '; |
| 9 | 159 |
} |
160 |
if ( $height ) {
|
|
| 18 | 161 |
$out .= 'height="' . (int) $height . '" '; |
| 9 | 162 |
} |
| 0 | 163 |
return $out; |
164 |
} |
|
165 |
||
166 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
* Scales an image to fit a particular size (such as 'thumb' or 'medium'). |
| 0 | 168 |
* |
169 |
* The URL might be the original image, or it might be a resized version. This |
|
170 |
* function won't create a new resized copy, it will just return an already |
|
171 |
* resized one if it exists. |
|
172 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
* A plugin may use the {@see 'image_downsize'} filter to hook into and offer image
|
| 0 | 174 |
* resizing services for images. The hook must return an array with the same |
| 16 | 175 |
* elements that are normally returned from the function. |
| 0 | 176 |
* |
177 |
* @since 2.5.0 |
|
178 |
* |
|
| 5 | 179 |
* @param int $id Attachment ID for image. |
| 18 | 180 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
181 |
* of width and height values in pixels (in that order). Default 'medium'. |
|
| 16 | 182 |
* @return array|false {
|
183 |
* Array of image data, or boolean false if no image is available. |
|
184 |
* |
|
185 |
* @type string $0 Image source URL. |
|
186 |
* @type int $1 Image width in pixels. |
|
187 |
* @type int $2 Image height in pixels. |
|
188 |
* @type bool $3 Whether the image is a resized image. |
|
189 |
* } |
|
| 0 | 190 |
*/ |
| 5 | 191 |
function image_downsize( $id, $size = 'medium' ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
$is_image = wp_attachment_is_image( $id ); |
| 0 | 193 |
|
| 5 | 194 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* Filters whether to preempt the output of image_downsize(). |
| 5 | 196 |
* |
| 16 | 197 |
* Returning a truthy value from the filter will effectively short-circuit |
198 |
* down-sizing the image, returning that value instead. |
|
| 5 | 199 |
* |
200 |
* @since 2.5.0 |
|
201 |
* |
|
| 16 | 202 |
* @param bool|array $downsize Whether to short-circuit the image downsize. |
| 5 | 203 |
* @param int $id Attachment ID for image. |
| 18 | 204 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
205 |
* an array of width and height values in pixels (in that order). |
|
| 5 | 206 |
*/ |
| 16 | 207 |
$out = apply_filters( 'image_downsize', false, $id, $size ); |
208 |
||
209 |
if ( $out ) {
|
|
| 0 | 210 |
return $out; |
| 5 | 211 |
} |
| 0 | 212 |
|
| 9 | 213 |
$img_url = wp_get_attachment_url( $id ); |
214 |
$meta = wp_get_attachment_metadata( $id ); |
|
| 16 | 215 |
$width = 0; |
216 |
$height = 0; |
|
| 9 | 217 |
$is_intermediate = false; |
218 |
$img_url_basename = wp_basename( $img_url ); |
|
| 0 | 219 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
220 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
221 |
* If the file isn't an image, attempt to replace its URL with a rendered image from its meta. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
222 |
* Otherwise, a non-image type could be returned. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
223 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
if ( ! $is_image ) {
|
| 16 | 225 |
if ( ! empty( $meta['sizes']['full'] ) ) {
|
| 9 | 226 |
$img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
$img_url_basename = $meta['sizes']['full']['file']; |
| 9 | 228 |
$width = $meta['sizes']['full']['width']; |
229 |
$height = $meta['sizes']['full']['height']; |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
|
| 16 | 235 |
// Try for a new style intermediate size. |
236 |
$intermediate = image_get_intermediate_size( $id, $size ); |
|
237 |
||
238 |
if ( $intermediate ) {
|
|
| 9 | 239 |
$img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); |
240 |
$width = $intermediate['width']; |
|
241 |
$height = $intermediate['height']; |
|
| 0 | 242 |
$is_intermediate = true; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
243 |
} elseif ( 'thumbnail' === $size && ! empty( $meta['thumb'] ) && is_string( $meta['thumb'] ) ) {
|
| 16 | 244 |
// Fall back to the old thumbnail. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
245 |
$imagefile = get_attached_file( $id ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
246 |
$thumbfile = str_replace( wp_basename( $imagefile ), wp_basename( $meta['thumb'] ), $imagefile ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
247 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
248 |
if ( file_exists( $thumbfile ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
249 |
$info = wp_getimagesize( $thumbfile ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
250 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
251 |
if ( $info ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
252 |
$img_url = str_replace( $img_url_basename, wp_basename( $thumbfile ), $img_url ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
253 |
$width = $info[0]; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
254 |
$height = $info[1]; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
255 |
$is_intermediate = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
256 |
} |
| 0 | 257 |
} |
258 |
} |
|
| 16 | 259 |
|
| 9 | 260 |
if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) {
|
| 16 | 261 |
// Any other type: use the real image. |
| 9 | 262 |
$width = $meta['width']; |
| 0 | 263 |
$height = $meta['height']; |
264 |
} |
|
265 |
||
| 9 | 266 |
if ( $img_url ) {
|
| 16 | 267 |
// We have the actual image size, but might need to further constrain it if content_width is narrower. |
| 0 | 268 |
list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); |
269 |
||
270 |
return array( $img_url, $width, $height, $is_intermediate ); |
|
271 |
} |
|
| 16 | 272 |
|
| 0 | 273 |
return false; |
274 |
} |
|
275 |
||
276 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
277 |
* Registers a new image size. |
| 5 | 278 |
* |
| 0 | 279 |
* @since 2.9.0 |
| 5 | 280 |
* |
281 |
* @global array $_wp_additional_image_sizes Associative array of additional image sizes. |
|
282 |
* |
|
283 |
* @param string $name Image size identifier. |
|
| 9 | 284 |
* @param int $width Optional. Image width in pixels. Default 0. |
285 |
* @param int $height Optional. Image height in pixels. Default 0. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
286 |
* @param bool|array $crop {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
287 |
* Optional. Image cropping behavior. If false, the image will be scaled (default). |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
288 |
* If true, image will be cropped to the specified dimensions using center positions. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
289 |
* If an array, the image will be cropped using the array to specify the crop location: |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
290 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
291 |
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
292 |
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
293 |
* } |
| 0 | 294 |
*/ |
295 |
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
|
|
296 |
global $_wp_additional_image_sizes; |
|
| 5 | 297 |
|
298 |
$_wp_additional_image_sizes[ $name ] = array( |
|
299 |
'width' => absint( $width ), |
|
300 |
'height' => absint( $height ), |
|
301 |
'crop' => $crop, |
|
302 |
); |
|
303 |
} |
|
304 |
||
305 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
306 |
* Checks if an image size exists. |
| 5 | 307 |
* |
308 |
* @since 3.9.0 |
|
309 |
* |
|
310 |
* @param string $name The image size to check. |
|
311 |
* @return bool True if the image size exists, false if not. |
|
312 |
*/ |
|
313 |
function has_image_size( $name ) {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
$sizes = wp_get_additional_image_sizes(); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
return isset( $sizes[ $name ] ); |
| 0 | 316 |
} |
317 |
||
318 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
319 |
* Removes a new image size. |
| 5 | 320 |
* |
321 |
* @since 3.9.0 |
|
322 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* @global array $_wp_additional_image_sizes |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* |
| 5 | 325 |
* @param string $name The image size to remove. |
326 |
* @return bool True if the image size was successfully removed, false on failure. |
|
327 |
*/ |
|
328 |
function remove_image_size( $name ) {
|
|
329 |
global $_wp_additional_image_sizes; |
|
330 |
||
331 |
if ( isset( $_wp_additional_image_sizes[ $name ] ) ) {
|
|
332 |
unset( $_wp_additional_image_sizes[ $name ] ); |
|
333 |
return true; |
|
334 |
} |
|
335 |
||
336 |
return false; |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Registers an image size for the post thumbnail. |
|
| 0 | 341 |
* |
342 |
* @since 2.9.0 |
|
| 5 | 343 |
* |
344 |
* @see add_image_size() for details on cropping behavior. |
|
345 |
* |
|
346 |
* @param int $width Image width in pixels. |
|
347 |
* @param int $height Image height in pixels. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
348 |
* @param bool|array $crop {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
349 |
* Optional. Image cropping behavior. If false, the image will be scaled (default). |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
350 |
* If true, image will be cropped to the specified dimensions using center positions. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
351 |
* If an array, the image will be cropped using the array to specify the crop location: |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
352 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
353 |
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
354 |
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
355 |
* } |
| 0 | 356 |
*/ |
357 |
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
|
|
358 |
add_image_size( 'post-thumbnail', $width, $height, $crop ); |
|
359 |
} |
|
360 |
||
361 |
/** |
|
| 5 | 362 |
* Gets an img tag for an image attachment, scaling it down if requested. |
| 0 | 363 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
* The {@see 'get_image_tag_class'} filter allows for changing the class name for the
|
| 0 | 365 |
* image without having to use regular expressions on the HTML content. The |
366 |
* parameters are: what WordPress will use for the class, the Attachment ID, |
|
367 |
* image align value, and the size the image should be. |
|
368 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
* The second filter, {@see 'get_image_tag'}, has the HTML content, which can then be
|
| 0 | 370 |
* further manipulated by a plugin to change all attribute values and even HTML |
371 |
* content. |
|
372 |
* |
|
373 |
* @since 2.5.0 |
|
374 |
* |
|
| 5 | 375 |
* @param int $id Attachment ID. |
| 16 | 376 |
* @param string $alt Image description for the alt attribute. |
377 |
* @param string $title Image description for the title attribute. |
|
| 5 | 378 |
* @param string $align Part of the class name for aligning the image. |
| 18 | 379 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
380 |
* width and height values in pixels (in that order). Default 'medium'. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
381 |
* @return string HTML IMG element for given image attachment. |
| 0 | 382 |
*/ |
| 5 | 383 |
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
|
| 0 | 384 |
|
| 9 | 385 |
list( $img_src, $width, $height ) = image_downsize( $id, $size ); |
386 |
$hwstring = image_hwstring( $width, $height ); |
|
| 0 | 387 |
|
388 |
$title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; |
|
389 |
||
| 18 | 390 |
$size_class = is_array( $size ) ? implode( 'x', $size ) : $size; |
391 |
$class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id; |
|
| 5 | 392 |
|
393 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
* Filters the value of the attachment's image tag class attribute. |
| 5 | 395 |
* |
396 |
* @since 2.6.0 |
|
397 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* @param string $class CSS class name or space-separated list of classes. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* @param int $id Attachment ID. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
* @param string $align Part of the class name for aligning the image. |
| 18 | 401 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
402 |
* an array of width and height values in pixels (in that order). |
|
| 5 | 403 |
*/ |
404 |
$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); |
|
| 0 | 405 |
|
| 19 | 406 |
$html = '<img src="' . esc_url( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; |
| 0 | 407 |
|
| 5 | 408 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
* Filters the HTML content for the image tag. |
| 5 | 410 |
* |
411 |
* @since 2.6.0 |
|
412 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
* @param string $html HTML content for the image. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
* @param int $id Attachment ID. |
| 16 | 415 |
* @param string $alt Image description for the alt attribute. |
416 |
* @param string $title Image description for the title attribute. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
* @param string $align Part of the class name for aligning the image. |
| 18 | 418 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
419 |
* an array of width and height values in pixels (in that order). |
|
| 5 | 420 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); |
| 0 | 422 |
} |
423 |
||
424 |
/** |
|
| 5 | 425 |
* Calculates the new dimensions for a down-sampled image. |
| 0 | 426 |
* |
427 |
* If either width or height are empty, no constraint is applied on |
|
428 |
* that dimension. |
|
429 |
* |
|
430 |
* @since 2.5.0 |
|
431 |
* |
|
| 5 | 432 |
* @param int $current_width Current width of the image. |
| 0 | 433 |
* @param int $current_height Current height of the image. |
| 5 | 434 |
* @param int $max_width Optional. Max width in pixels to constrain to. Default 0. |
435 |
* @param int $max_height Optional. Max height in pixels to constrain to. Default 0. |
|
| 16 | 436 |
* @return int[] {
|
437 |
* An array of width and height values. |
|
438 |
* |
|
439 |
* @type int $0 The width in pixels. |
|
440 |
* @type int $1 The height in pixels. |
|
441 |
* } |
|
| 0 | 442 |
*/ |
| 5 | 443 |
function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) {
|
| 9 | 444 |
if ( ! $max_width && ! $max_height ) {
|
| 0 | 445 |
return array( $current_width, $current_height ); |
| 9 | 446 |
} |
| 0 | 447 |
|
| 16 | 448 |
$width_ratio = 1.0; |
449 |
$height_ratio = 1.0; |
|
450 |
$did_width = false; |
|
451 |
$did_height = false; |
|
| 0 | 452 |
|
453 |
if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
|
|
454 |
$width_ratio = $max_width / $current_width; |
|
| 9 | 455 |
$did_width = true; |
| 0 | 456 |
} |
457 |
||
458 |
if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
|
|
459 |
$height_ratio = $max_height / $current_height; |
|
| 9 | 460 |
$did_height = true; |
| 0 | 461 |
} |
462 |
||
| 16 | 463 |
// Calculate the larger/smaller ratios. |
| 0 | 464 |
$smaller_ratio = min( $width_ratio, $height_ratio ); |
465 |
$larger_ratio = max( $width_ratio, $height_ratio ); |
|
466 |
||
| 5 | 467 |
if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
|
| 9 | 468 |
// The larger ratio is too big. It would result in an overflow. |
| 0 | 469 |
$ratio = $smaller_ratio; |
| 5 | 470 |
} else {
|
| 0 | 471 |
// The larger ratio fits, and is likely to be a more "snug" fit. |
472 |
$ratio = $larger_ratio; |
|
| 5 | 473 |
} |
| 0 | 474 |
|
475 |
// Very small dimensions may result in 0, 1 should be the minimum. |
|
| 9 | 476 |
$w = max( 1, (int) round( $current_width * $ratio ) ); |
477 |
$h = max( 1, (int) round( $current_height * $ratio ) ); |
|
| 0 | 478 |
|
| 16 | 479 |
/* |
480 |
* Sometimes, due to rounding, we'll end up with a result like this: |
|
481 |
* 465x700 in a 177x177 box is 117x176... a pixel short. |
|
482 |
* We also have issues with recursive calls resulting in an ever-changing result. |
|
483 |
* Constraining to the result of a constraint should yield the original result. |
|
484 |
* Thus we look for dimensions that are one pixel shy of the max value and bump them up. |
|
485 |
*/ |
|
| 5 | 486 |
|
487 |
// Note: $did_width means it is possible $smaller_ratio == $width_ratio. |
|
| 16 | 488 |
if ( $did_width && $w === $max_width - 1 ) {
|
489 |
$w = $max_width; // Round it up. |
|
| 5 | 490 |
} |
491 |
||
492 |
// Note: $did_height means it is possible $smaller_ratio == $height_ratio. |
|
| 16 | 493 |
if ( $did_height && $h === $max_height - 1 ) {
|
494 |
$h = $max_height; // Round it up. |
|
| 5 | 495 |
} |
496 |
||
497 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
498 |
* Filters dimensions to constrain down-sampled images to. |
| 5 | 499 |
* |
500 |
* @since 4.1.0 |
|
501 |
* |
|
| 16 | 502 |
* @param int[] $dimensions {
|
503 |
* An array of width and height values. |
|
504 |
* |
|
505 |
* @type int $0 The width in pixels. |
|
506 |
* @type int $1 The height in pixels. |
|
507 |
* } |
|
| 9 | 508 |
* @param int $current_width The current width of the image. |
509 |
* @param int $current_height The current height of the image. |
|
510 |
* @param int $max_width The maximum width permitted. |
|
511 |
* @param int $max_height The maximum height permitted. |
|
| 5 | 512 |
*/ |
513 |
return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height ); |
|
| 0 | 514 |
} |
515 |
||
516 |
/** |
|
| 5 | 517 |
* Retrieves calculated resize dimensions for use in WP_Image_Editor. |
518 |
* |
|
519 |
* Calculates dimensions and coordinates for a resized image that fits |
|
520 |
* within a specified width and height. |
|
| 0 | 521 |
* |
522 |
* @since 2.5.0 |
|
523 |
* |
|
| 5 | 524 |
* @param int $orig_w Original width in pixels. |
525 |
* @param int $orig_h Original height in pixels. |
|
526 |
* @param int $dest_w New width in pixels. |
|
527 |
* @param int $dest_h New height in pixels. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
528 |
* @param bool|array $crop {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
529 |
* Optional. Image cropping behavior. If false, the image will be scaled (default). |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
530 |
* If true, image will be cropped to the specified dimensions using center positions. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
531 |
* If an array, the image will be cropped using the array to specify the crop location: |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
532 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
533 |
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
534 |
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
535 |
* } |
| 16 | 536 |
* @return array|false Returned array matches parameters for `imagecopyresampled()`. False on failure. |
| 0 | 537 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) {
|
| 0 | 539 |
|
| 9 | 540 |
if ( $orig_w <= 0 || $orig_h <= 0 ) {
|
| 0 | 541 |
return false; |
| 9 | 542 |
} |
| 16 | 543 |
// At least one of $dest_w or $dest_h must be specific. |
| 9 | 544 |
if ( $dest_w <= 0 && $dest_h <= 0 ) {
|
| 0 | 545 |
return false; |
| 9 | 546 |
} |
| 0 | 547 |
|
| 5 | 548 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
549 |
* Filters whether to preempt calculating the image resize dimensions. |
| 5 | 550 |
* |
| 16 | 551 |
* Returning a non-null value from the filter will effectively short-circuit |
| 5 | 552 |
* image_resize_dimensions(), returning that value instead. |
553 |
* |
|
554 |
* @since 3.4.0 |
|
555 |
* |
|
556 |
* @param null|mixed $null Whether to preempt output of the resize dimensions. |
|
557 |
* @param int $orig_w Original width in pixels. |
|
558 |
* @param int $orig_h Original height in pixels. |
|
559 |
* @param int $dest_w New width in pixels. |
|
560 |
* @param int $dest_h New height in pixels. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
* @param bool|array $crop Whether to crop image to specified width and height or resize. |
| 5 | 562 |
* An array can specify positioning of the crop area. Default false. |
563 |
*/ |
|
| 0 | 564 |
$output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); |
| 16 | 565 |
|
| 9 | 566 |
if ( null !== $output ) {
|
| 0 | 567 |
return $output; |
| 9 | 568 |
} |
| 0 | 569 |
|
| 16 | 570 |
// Stop if the destination size is larger than the original image dimensions. |
571 |
if ( empty( $dest_h ) ) {
|
|
572 |
if ( $orig_w < $dest_w ) {
|
|
573 |
return false; |
|
574 |
} |
|
575 |
} elseif ( empty( $dest_w ) ) {
|
|
576 |
if ( $orig_h < $dest_h ) {
|
|
577 |
return false; |
|
578 |
} |
|
579 |
} else {
|
|
580 |
if ( $orig_w < $dest_w && $orig_h < $dest_h ) {
|
|
581 |
return false; |
|
582 |
} |
|
583 |
} |
|
584 |
||
| 0 | 585 |
if ( $crop ) {
|
| 16 | 586 |
/* |
587 |
* Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h. |
|
588 |
* Note that the requested crop dimensions are used as a maximum bounding box for the original image. |
|
589 |
* If the original image's width or height is less than the requested width or height |
|
590 |
* only the greater one will be cropped. |
|
591 |
* For example when the original image is 600x300, and the requested crop dimensions are 400x400, |
|
592 |
* the resulting image will be 400x300. |
|
593 |
*/ |
|
| 0 | 594 |
$aspect_ratio = $orig_w / $orig_h; |
| 9 | 595 |
$new_w = min( $dest_w, $orig_w ); |
596 |
$new_h = min( $dest_h, $orig_h ); |
|
| 0 | 597 |
|
| 5 | 598 |
if ( ! $new_w ) {
|
599 |
$new_w = (int) round( $new_h * $aspect_ratio ); |
|
| 0 | 600 |
} |
601 |
||
| 5 | 602 |
if ( ! $new_h ) {
|
603 |
$new_h = (int) round( $new_w / $aspect_ratio ); |
|
| 0 | 604 |
} |
605 |
||
| 9 | 606 |
$size_ratio = max( $new_w / $orig_w, $new_h / $orig_h ); |
607 |
||
608 |
$crop_w = round( $new_w / $size_ratio ); |
|
609 |
$crop_h = round( $new_h / $size_ratio ); |
|
| 0 | 610 |
|
| 5 | 611 |
if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
|
612 |
$crop = array( 'center', 'center' ); |
|
613 |
} |
|
614 |
||
615 |
list( $x, $y ) = $crop; |
|
616 |
||
617 |
if ( 'left' === $x ) {
|
|
618 |
$s_x = 0; |
|
619 |
} elseif ( 'right' === $x ) {
|
|
620 |
$s_x = $orig_w - $crop_w; |
|
621 |
} else {
|
|
622 |
$s_x = floor( ( $orig_w - $crop_w ) / 2 ); |
|
623 |
} |
|
624 |
||
625 |
if ( 'top' === $y ) {
|
|
626 |
$s_y = 0; |
|
627 |
} elseif ( 'bottom' === $y ) {
|
|
628 |
$s_y = $orig_h - $crop_h; |
|
629 |
} else {
|
|
630 |
$s_y = floor( ( $orig_h - $crop_h ) / 2 ); |
|
631 |
} |
|
| 0 | 632 |
} else {
|
| 16 | 633 |
// Resize using $dest_w x $dest_h as a maximum bounding box. |
| 0 | 634 |
$crop_w = $orig_w; |
635 |
$crop_h = $orig_h; |
|
636 |
||
637 |
$s_x = 0; |
|
638 |
$s_y = 0; |
|
639 |
||
640 |
list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h ); |
|
641 |
} |
|
642 |
||
| 16 | 643 |
if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) {
|
644 |
// The new size has virtually the same dimensions as the original image. |
|
645 |
||
646 |
/** |
|
647 |
* Filters whether to proceed with making an image sub-size with identical dimensions |
|
648 |
* with the original/source image. Differences of 1px may be due to rounding and are ignored. |
|
649 |
* |
|
650 |
* @since 5.3.0 |
|
651 |
* |
|
652 |
* @param bool $proceed The filtered value. |
|
653 |
* @param int $orig_w Original image width. |
|
654 |
* @param int $orig_h Original image height. |
|
655 |
*/ |
|
656 |
$proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h ); |
|
657 |
||
658 |
if ( ! $proceed ) {
|
|
659 |
return false; |
|
660 |
} |
|
| 5 | 661 |
} |
| 0 | 662 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
663 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
664 |
* The return array matches the parameters to imagecopyresampled(). |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
665 |
* int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
666 |
*/ |
| 0 | 667 |
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); |
668 |
} |
|
669 |
||
670 |
/** |
|
| 5 | 671 |
* Resizes an image to make a thumbnail or intermediate size. |
| 0 | 672 |
* |
673 |
* The returned array has the file size, the image width, and image height. The |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
* {@see 'image_make_intermediate_size'} filter can be used to hook in and change the
|
| 0 | 675 |
* values of the returned array. The only parameter is the resized file path. |
676 |
* |
|
677 |
* @since 2.5.0 |
|
678 |
* |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
679 |
* @param string $file File path. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
680 |
* @param int $width Image width. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
681 |
* @param int $height Image height. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
682 |
* @param bool|array $crop {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
683 |
* Optional. Image cropping behavior. If false, the image will be scaled (default). |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
684 |
* If true, image will be cropped to the specified dimensions using center positions. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
685 |
* If an array, the image will be cropped using the array to specify the crop location: |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
686 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
687 |
* @type string $0 The x crop position. Accepts 'left' 'center', or 'right'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
688 |
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
689 |
* } |
| 16 | 690 |
* @return array|false Metadata array on success. False if no image was created. |
| 0 | 691 |
*/ |
692 |
function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
|
|
693 |
if ( $width || $height ) {
|
|
694 |
$editor = wp_get_image_editor( $file ); |
|
695 |
||
| 9 | 696 |
if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) {
|
| 0 | 697 |
return false; |
| 9 | 698 |
} |
| 0 | 699 |
|
700 |
$resized_file = $editor->save(); |
|
701 |
||
702 |
if ( ! is_wp_error( $resized_file ) && $resized_file ) {
|
|
703 |
unset( $resized_file['path'] ); |
|
704 |
return $resized_file; |
|
705 |
} |
|
706 |
} |
|
707 |
return false; |
|
708 |
} |
|
709 |
||
710 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
711 |
* Helper function to test if aspect ratios for two images match. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
712 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
713 |
* @since 4.6.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
* @param int $source_width Width of the first image in pixels. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
* @param int $source_height Height of the first image in pixels. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
717 |
* @param int $target_width Width of the second image in pixels. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
* @param int $target_height Height of the second image in pixels. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
719 |
* @return bool True if aspect ratios match within 1px. False if not. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
720 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
function wp_image_matches_ratio( $source_width, $source_height, $target_width, $target_height ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
722 |
/* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
723 |
* To test for varying crops, we constrain the dimensions of the larger image |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
724 |
* to the dimensions of the smaller image and see if they match. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
if ( $source_width > $target_width ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
$constrained_size = wp_constrain_dimensions( $source_width, $source_height, $target_width ); |
| 9 | 728 |
$expected_size = array( $target_width, $target_height ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
730 |
$constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width ); |
| 9 | 731 |
$expected_size = array( $source_width, $source_height ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
733 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
// If the image dimensions are within 1px of the expected size, we consider it a match. |
| 16 | 735 |
$matched = ( wp_fuzzy_number_match( $constrained_size[0], $expected_size[0] ) && wp_fuzzy_number_match( $constrained_size[1], $expected_size[1] ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
return $matched; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
740 |
/** |
| 5 | 741 |
* Retrieves the image's intermediate size (resized) path, width, and height. |
| 0 | 742 |
* |
743 |
* The $size parameter can be an array with the width and height respectively. |
|
744 |
* If the size matches the 'sizes' metadata array for width and height, then it |
|
745 |
* will be used. If there is no direct match, then the nearest image size larger |
|
746 |
* than the specified size will be used. If nothing is found, then the function |
|
747 |
* will break out and return false. |
|
748 |
* |
|
749 |
* The metadata 'sizes' is used for compatible sizes that can be used for the |
|
750 |
* parameter $size value. |
|
751 |
* |
|
752 |
* The url path will be given, when the $size parameter is a string. |
|
753 |
* |
|
754 |
* If you are passing an array for the $size, you should consider using |
|
755 |
* add_image_size() so that a cropped version is generated. It's much more |
|
756 |
* efficient than having to find the closest-sized image and then having the |
|
757 |
* browser scale down the image. |
|
758 |
* |
|
759 |
* @since 2.5.0 |
|
760 |
* |
|
| 5 | 761 |
* @param int $post_id Attachment ID. |
| 18 | 762 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
763 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
| 16 | 764 |
* @return array|false {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
* Array of file relative path, width, and height on success. Additionally includes absolute |
| 18 | 766 |
* path and URL if registered size is passed to `$size` parameter. False on failure. |
767 |
* |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
768 |
* @type string $file Filename of image. |
| 18 | 769 |
* @type int $width Width of image in pixels. |
770 |
* @type int $height Height of image in pixels. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
771 |
* @type string $path Path of image relative to uploads directory. |
| 18 | 772 |
* @type string $url URL of image. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
773 |
* } |
| 0 | 774 |
*/ |
| 5 | 775 |
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
|
| 16 | 776 |
$imagedata = wp_get_attachment_metadata( $post_id ); |
777 |
||
778 |
if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
|
|
| 0 | 779 |
return false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
780 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
781 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
$data = array(); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
// Find the best match when '$size' is an array. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
785 |
if ( is_array( $size ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
786 |
$candidates = array(); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
$imagedata['height'] = $imagedata['sizes']['full']['height']; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
$imagedata['width'] = $imagedata['sizes']['full']['width']; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
} |
| 5 | 792 |
|
| 0 | 793 |
foreach ( $imagedata['sizes'] as $_size => $data ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
// If there's an exact match to an existing image size, short circuit. |
| 18 | 795 |
if ( (int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $size[1] ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
796 |
$candidates[ $data['width'] * $data['height'] ] = $data; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
break; |
| 0 | 798 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
799 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
// If it's not an exact match, consider larger sizes with the same aspect ratio. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
802 |
// If '0' is passed to either size, we test ratios against the original file. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
if ( 0 === $size[0] || 0 === $size[1] ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
806 |
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
807 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
808 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
809 |
if ( $same_ratio ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
810 |
$candidates[ $data['width'] * $data['height'] ] = $data; |
| 0 | 811 |
} |
812 |
} |
|
813 |
} |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
if ( ! empty( $candidates ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
// Sort the array by size if we have more than one candidate. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
if ( 1 < count( $candidates ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
ksort( $candidates ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
$data = array_shift( $candidates ); |
| 9 | 822 |
/* |
823 |
* When the size requested is smaller than the thumbnail dimensions, we |
|
824 |
* fall back to the thumbnail size to maintain backward compatibility with |
|
825 |
* pre 4.6 versions of WordPress. |
|
826 |
*/ |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
} elseif ( ! empty( $imagedata['sizes']['thumbnail'] ) && $imagedata['sizes']['thumbnail']['width'] >= $size[0] && $imagedata['sizes']['thumbnail']['width'] >= $size[1] ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
$data = $imagedata['sizes']['thumbnail']; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
// Constrain the width and height attributes to the requested values. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
834 |
list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
835 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
836 |
} elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
837 |
$data = $imagedata['sizes'][ $size ]; |
| 0 | 838 |
} |
839 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
840 |
// If we still don't have a match at this point, return false. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
841 |
if ( empty( $data ) ) {
|
| 0 | 842 |
return false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
844 |
|
| 16 | 845 |
// Include the full filesystem path of the intermediate file. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
846 |
if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
|
| 9 | 847 |
$file_url = wp_get_attachment_url( $post_id ); |
848 |
$data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] ); |
|
849 |
$data['url'] = path_join( dirname( $file_url ), $data['file'] ); |
|
| 0 | 850 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
851 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
852 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
* Filters the output of image_get_intermediate_size() |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
854 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
855 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
* @see image_get_intermediate_size() |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
* @param array $data Array of file relative path, width, and height on success. May also include |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
860 |
* file absolute path and URL. |
| 18 | 861 |
* @param int $post_id The ID of the image attachment. |
862 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
863 |
* an array of width and height values in pixels (in that order). |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
865 |
return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); |
| 0 | 866 |
} |
867 |
||
868 |
/** |
|
| 16 | 869 |
* Gets the available intermediate image size names. |
| 5 | 870 |
* |
| 0 | 871 |
* @since 3.0.0 |
| 5 | 872 |
* |
| 16 | 873 |
* @return string[] An array of image size names. |
| 0 | 874 |
*/ |
875 |
function get_intermediate_image_sizes() {
|
|
| 16 | 876 |
$default_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' ); |
877 |
$additional_sizes = wp_get_additional_image_sizes(); |
|
878 |
||
879 |
if ( ! empty( $additional_sizes ) ) {
|
|
880 |
$default_sizes = array_merge( $default_sizes, array_keys( $additional_sizes ) ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
} |
| 0 | 882 |
|
| 5 | 883 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
884 |
* Filters the list of intermediate image sizes. |
| 5 | 885 |
* |
886 |
* @since 2.5.0 |
|
887 |
* |
|
| 16 | 888 |
* @param string[] $default_sizes An array of intermediate image size names. Defaults |
889 |
* are 'thumbnail', 'medium', 'medium_large', 'large'. |
|
| 5 | 890 |
*/ |
| 16 | 891 |
return apply_filters( 'intermediate_image_sizes', $default_sizes ); |
| 0 | 892 |
} |
893 |
||
894 |
/** |
|
| 16 | 895 |
* Returns a normalized list of all currently registered image sub-sizes. |
896 |
* |
|
897 |
* @since 5.3.0 |
|
898 |
* @uses wp_get_additional_image_sizes() |
|
899 |
* @uses get_intermediate_image_sizes() |
|
900 |
* |
|
| 19 | 901 |
* @return array[] Associative array of arrays of image sub-size information, |
902 |
* keyed by image size name. |
|
| 16 | 903 |
*/ |
904 |
function wp_get_registered_image_subsizes() {
|
|
905 |
$additional_sizes = wp_get_additional_image_sizes(); |
|
906 |
$all_sizes = array(); |
|
907 |
||
908 |
foreach ( get_intermediate_image_sizes() as $size_name ) {
|
|
909 |
$size_data = array( |
|
910 |
'width' => 0, |
|
911 |
'height' => 0, |
|
912 |
'crop' => false, |
|
913 |
); |
|
914 |
||
915 |
if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
|
|
916 |
// For sizes added by plugins and themes. |
|
| 18 | 917 |
$size_data['width'] = (int) $additional_sizes[ $size_name ]['width']; |
| 16 | 918 |
} else {
|
919 |
// For default sizes set in options. |
|
| 18 | 920 |
$size_data['width'] = (int) get_option( "{$size_name}_size_w" );
|
| 16 | 921 |
} |
922 |
||
923 |
if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
|
|
| 18 | 924 |
$size_data['height'] = (int) $additional_sizes[ $size_name ]['height']; |
| 16 | 925 |
} else {
|
| 18 | 926 |
$size_data['height'] = (int) get_option( "{$size_name}_size_h" );
|
| 16 | 927 |
} |
928 |
||
929 |
if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
|
|
930 |
// This size isn't set. |
|
931 |
continue; |
|
932 |
} |
|
933 |
||
934 |
if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
|
|
935 |
$size_data['crop'] = $additional_sizes[ $size_name ]['crop']; |
|
936 |
} else {
|
|
937 |
$size_data['crop'] = get_option( "{$size_name}_crop" );
|
|
938 |
} |
|
939 |
||
940 |
if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) {
|
|
941 |
$size_data['crop'] = (bool) $size_data['crop']; |
|
942 |
} |
|
943 |
||
944 |
$all_sizes[ $size_name ] = $size_data; |
|
945 |
} |
|
946 |
||
947 |
return $all_sizes; |
|
948 |
} |
|
949 |
||
950 |
/** |
|
951 |
* Retrieves an image to represent an attachment. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
952 |
* |
| 0 | 953 |
* @since 2.5.0 |
954 |
* |
|
| 5 | 955 |
* @param int $attachment_id Image attachment ID. |
| 18 | 956 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
957 |
* width and height values in pixels (in that order). Default 'thumbnail'. |
|
| 16 | 958 |
* @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. |
959 |
* @return array|false {
|
|
960 |
* Array of image data, or boolean false if no image is available. |
|
961 |
* |
|
962 |
* @type string $0 Image source URL. |
|
963 |
* @type int $1 Image width in pixels. |
|
964 |
* @type int $2 Image height in pixels. |
|
965 |
* @type bool $3 Whether the image is a resized image. |
|
966 |
* } |
|
| 0 | 967 |
*/ |
| 5 | 968 |
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
|
| 16 | 969 |
// Get a thumbnail or intermediate image if there is one. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
$image = image_downsize( $attachment_id, $size ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
if ( ! $image ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
$src = false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
|
| 16 | 974 |
if ( $icon ) {
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
975 |
$src = wp_mime_type_icon( $attachment_id, '.svg' ); |
| 16 | 976 |
|
977 |
if ( $src ) {
|
|
978 |
/** This filter is documented in wp-includes/post.php */ |
|
979 |
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); |
|
980 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
981 |
$src_file = $icon_dir . '/' . wp_basename( $src ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
982 |
|
| 18 | 983 |
list( $width, $height ) = wp_getimagesize( $src_file ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
984 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
985 |
$ext = strtolower( substr( $src_file, -4 ) ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
986 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
987 |
if ( '.svg' === $ext ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
988 |
// SVG does not have true dimensions, so this assigns width and height directly. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
989 |
$width = 48; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
990 |
$height = 64; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
991 |
} else {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
992 |
list( $width, $height ) = wp_getimagesize( $src_file ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
993 |
} |
| 16 | 994 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
997 |
if ( $src && $width && $height ) {
|
| 16 | 998 |
$image = array( $src, $width, $height, false ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
999 |
} |
| 0 | 1000 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1001 |
/** |
| 16 | 1002 |
* Filters the attachment image source result. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1003 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
* @since 4.3.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1005 |
* |
| 16 | 1006 |
* @param array|false $image {
|
1007 |
* Array of image data, or boolean false if no image is available. |
|
1008 |
* |
|
1009 |
* @type string $0 Image source URL. |
|
1010 |
* @type int $1 Image width in pixels. |
|
1011 |
* @type int $2 Image height in pixels. |
|
1012 |
* @type bool $3 Whether the image is a resized image. |
|
1013 |
* } |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1014 |
* @param int $attachment_id Image attachment ID. |
| 18 | 1015 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
1016 |
* an array of width and height values in pixels (in that order). |
|
| 16 | 1017 |
* @param bool $icon Whether the image should be treated as an icon. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1018 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); |
| 0 | 1020 |
} |
1021 |
||
1022 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1023 |
* Gets an HTML img element representing an image attachment. |
| 0 | 1024 |
* |
| 5 | 1025 |
* While `$size` will accept an array, it is better to register a size with |
| 0 | 1026 |
* add_image_size() so that a cropped version is generated. It's much more |
1027 |
* efficient than having to find the closest-sized image and then having the |
|
1028 |
* browser scale down the image. |
|
1029 |
* |
|
1030 |
* @since 2.5.0 |
|
| 16 | 1031 |
* @since 4.4.0 The `$srcset` and `$sizes` attributes were added. |
1032 |
* @since 5.5.0 The `$loading` attribute was added. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1033 |
* @since 6.1.0 The `$decoding` attribute was added. |
| 0 | 1034 |
* |
| 5 | 1035 |
* @param int $attachment_id Image attachment ID. |
| 18 | 1036 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
1037 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
| 5 | 1038 |
* @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
| 16 | 1039 |
* @param string|array $attr {
|
1040 |
* Optional. Attributes for the image markup. |
|
1041 |
* |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1042 |
* @type string $src Image attachment URL. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1043 |
* @type string $class CSS class name or space-separated list of classes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1044 |
* Default `attachment-$size_class size-$size_class`, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1045 |
* where `$size_class` is the image size being requested. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1046 |
* @type string $alt Image description for the alt attribute. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1047 |
* @type string $srcset The 'srcset' attribute value. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1048 |
* @type string $sizes The 'sizes' attribute value. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1049 |
* @type string|false $loading The 'loading' attribute value. Passing a value of false |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1050 |
* will result in the attribute being omitted for the image. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1051 |
* Default determined by {@see wp_get_loading_optimization_attributes()}.
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1052 |
* @type string $decoding The 'decoding' attribute value. Possible values are |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1053 |
* 'async' (default), 'sync', or 'auto'. Passing false or an empty |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1054 |
* string will result in the attribute being omitted. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1055 |
* @type string $fetchpriority The 'fetchpriority' attribute value, whether `high`, `low`, or `auto`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1056 |
* Default determined by {@see wp_get_loading_optimization_attributes()}.
|
| 16 | 1057 |
* } |
| 0 | 1058 |
* @return string HTML img element or empty string on failure. |
1059 |
*/ |
|
| 9 | 1060 |
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
|
1061 |
$html = ''; |
|
1062 |
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
|
| 16 | 1063 |
|
| 0 | 1064 |
if ( $image ) {
|
| 16 | 1065 |
list( $src, $width, $height ) = $image; |
1066 |
||
1067 |
$attachment = get_post( $attachment_id ); |
|
1068 |
$hwstring = image_hwstring( $width, $height ); |
|
1069 |
$size_class = $size; |
|
1070 |
||
| 5 | 1071 |
if ( is_array( $size_class ) ) {
|
| 18 | 1072 |
$size_class = implode( 'x', $size_class ); |
| 5 | 1073 |
} |
| 16 | 1074 |
|
| 0 | 1075 |
$default_attr = array( |
| 9 | 1076 |
'src' => $src, |
1077 |
'class' => "attachment-$size_class size-$size_class", |
|
1078 |
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), |
|
| 0 | 1079 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1080 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1081 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1082 |
* Filters the context in which wp_get_attachment_image() is used. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1083 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1084 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1085 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1086 |
* @param string $context The context. Default 'wp_get_attachment_image'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1087 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1088 |
$context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1089 |
$attr = wp_parse_args( $attr, $default_attr ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1090 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1091 |
$loading_attr = $attr; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1092 |
$loading_attr['width'] = $width; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1093 |
$loading_attr['height'] = $height; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1094 |
$loading_optimization_attr = wp_get_loading_optimization_attributes( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1095 |
'img', |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1096 |
$loading_attr, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1097 |
$context |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1098 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1099 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1100 |
// Add loading optimization attributes if not available. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1101 |
$attr = array_merge( $attr, $loading_optimization_attr ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1102 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1103 |
// Omit the `decoding` attribute if the value is invalid according to the spec. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1104 |
if ( empty( $attr['decoding'] ) || ! in_array( $attr['decoding'], array( 'async', 'sync', 'auto' ), true ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1105 |
unset( $attr['decoding'] ); |
| 16 | 1106 |
} |
1107 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1108 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1109 |
* If the default value of `lazy` for the `loading` attribute is overridden |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1110 |
* to omit the attribute for this image, ensure it is not included. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1111 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1112 |
if ( isset( $attr['loading'] ) && ! $attr['loading'] ) {
|
| 16 | 1113 |
unset( $attr['loading'] ); |
1114 |
} |
|
1115 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1116 |
// If the `fetchpriority` attribute is overridden and set to false or an empty string. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1117 |
if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1118 |
unset( $attr['fetchpriority'] ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1119 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1120 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
// Generate 'srcset' and 'sizes' if not already present. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1122 |
if ( empty( $attr['srcset'] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1123 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1124 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1125 |
if ( is_array( $image_meta ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
$size_array = array( absint( $width ), absint( $height ) ); |
| 9 | 1127 |
$srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); |
1128 |
$sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1130 |
if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
$attr['srcset'] = $srcset; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
if ( empty( $attr['sizes'] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
$attr['sizes'] = $sizes; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
} |
| 5 | 1139 |
|
1140 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1141 |
* Filters the list of attachment image attributes. |
| 5 | 1142 |
* |
1143 |
* @since 2.8.0 |
|
1144 |
* |
|
| 18 | 1145 |
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. |
| 16 | 1146 |
* See wp_get_attachment_image(). |
| 5 | 1147 |
* @param WP_Post $attachment Image attachment post. |
| 18 | 1148 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
1149 |
* an array of width and height values in pixels (in that order). |
|
| 5 | 1150 |
*/ |
1151 |
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); |
|
| 16 | 1152 |
|
| 0 | 1153 |
$attr = array_map( 'esc_attr', $attr ); |
| 9 | 1154 |
$html = rtrim( "<img $hwstring" ); |
| 16 | 1155 |
|
| 0 | 1156 |
foreach ( $attr as $name => $value ) {
|
1157 |
$html .= " $name=" . '"' . $value . '"'; |
|
1158 |
} |
|
| 16 | 1159 |
|
| 0 | 1160 |
$html .= ' />'; |
1161 |
} |
|
1162 |
||
| 18 | 1163 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1164 |
* Filters the HTML img element representing an image attachment. |
| 18 | 1165 |
* |
1166 |
* @since 5.6.0 |
|
1167 |
* |
|
1168 |
* @param string $html HTML img element or empty string on failure. |
|
1169 |
* @param int $attachment_id Image attachment ID. |
|
1170 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
1171 |
* an array of width and height values in pixels (in that order). |
|
1172 |
* @param bool $icon Whether the image should be treated as an icon. |
|
1173 |
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. |
|
1174 |
* See wp_get_attachment_image(). |
|
1175 |
*/ |
|
1176 |
return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr ); |
|
| 0 | 1177 |
} |
1178 |
||
1179 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1180 |
* Gets the URL of an image attachment. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
* @param int $attachment_id Image attachment ID. |
| 18 | 1185 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
1186 |
* width and height values in pixels (in that order). Default 'thumbnail'. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1187 |
* @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
| 18 | 1188 |
* @return string|false Attachment URL or false if no image is available. If `$size` does not match |
1189 |
* any registered image size, the original image URL will be returned. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1191 |
function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
| 19 | 1193 |
return isset( $image[0] ) ? $image[0] : false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1195 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1197 |
* Gets the attachment path relative to the upload directory. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1198 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
* @since 4.4.1 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
* @access private |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
* @param string $file Attachment file name. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
* @return string Attachment path relative to the upload directory. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
function _wp_get_attachment_relative_path( $file ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
$dirname = dirname( $file ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
if ( '.' === $dirname ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1209 |
return ''; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1212 |
if ( str_contains( $dirname, 'wp-content/uploads' ) ) {
|
| 16 | 1213 |
// Get the directory name relative to the upload directory (back compat for pre-2.7 uploads). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
$dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
$dirname = ltrim( $dirname, '/' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
return $dirname; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1221 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1222 |
* Gets the image size as array from its meta data. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1224 |
* Used for responsive images. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
* @access private |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
* |
| 18 | 1229 |
* @param string $size_name Image size. Accepts any registered image size name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
* @param array $image_meta The image meta data. |
| 18 | 1231 |
* @return array|false {
|
1232 |
* Array of width and height or false if the size isn't present in the meta data. |
|
1233 |
* |
|
1234 |
* @type int $0 Image width. |
|
1235 |
* @type int $1 Image height. |
|
1236 |
* } |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
function _wp_get_image_size_from_meta( $size_name, $image_meta ) {
|
| 16 | 1239 |
if ( 'full' === $size_name ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
return array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1241 |
absint( $image_meta['width'] ), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1242 |
absint( $image_meta['height'] ), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
); |
| 9 | 1244 |
} elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
return array( |
| 9 | 1246 |
absint( $image_meta['sizes'][ $size_name ]['width'] ), |
1247 |
absint( $image_meta['sizes'][ $size_name ]['height'] ), |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1249 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1250 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1252 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1253 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1254 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1255 |
* Retrieves the value for an image attachment's 'srcset' attribute. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1256 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1257 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1259 |
* @see wp_calculate_image_srcset() |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1260 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1261 |
* @param int $attachment_id Image attachment ID. |
| 18 | 1262 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
* width and height values in pixels (in that order). Default 'medium'. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1264 |
* @param array|null $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1265 |
* Default null. |
| 18 | 1266 |
* @return string|false A 'srcset' value string or false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
|
| 16 | 1269 |
$image = wp_get_attachment_image_src( $attachment_id, $size ); |
1270 |
||
1271 |
if ( ! $image ) {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1275 |
if ( ! is_array( $image_meta ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
|
| 9 | 1279 |
$image_src = $image[0]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
$size_array = array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
absint( $image[1] ), |
| 9 | 1282 |
absint( $image[2] ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1284 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1285 |
return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1286 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1288 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1289 |
* A helper function to calculate the image sources to include in a 'srcset' attribute. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
* |
| 16 | 1293 |
* @param int[] $size_array {
|
1294 |
* An array of width and height values. |
|
1295 |
* |
|
1296 |
* @type int $0 The width in pixels. |
|
1297 |
* @type int $1 The height in pixels. |
|
1298 |
* } |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
* @param string $image_src The 'src' of the image. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 16 | 1301 |
* @param int $attachment_id Optional. The image attachment ID. Default 0. |
| 18 | 1302 |
* @return string|false The 'srcset' attribute value. False on error or when only one source exists. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1304 |
function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1306 |
* Pre-filters the image meta to be able to fix inconsistencies in the stored data. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1307 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1308 |
* @since 4.5.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1310 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 16 | 1311 |
* @param int[] $size_array {
|
1312 |
* An array of requested width and height values. |
|
1313 |
* |
|
1314 |
* @type int $0 The width in pixels. |
|
1315 |
* @type int $1 The height in pixels. |
|
1316 |
* } |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
* @param string $image_src The 'src' of the image. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
* @param int $attachment_id The image attachment ID or 0 if not supplied. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1319 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1320 |
$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1321 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1322 |
if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) || strlen( $image_meta['file'] ) < 4 ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1323 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1325 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1326 |
$image_sizes = $image_meta['sizes']; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1327 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1328 |
// Get the width and height of the image. |
| 9 | 1329 |
$image_width = (int) $size_array[0]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1330 |
$image_height = (int) $size_array[1]; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1331 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1332 |
// Bail early if error/no width. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1333 |
if ( $image_width < 1 ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1334 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1336 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
$image_basename = wp_basename( $image_meta['file'] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
/* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
* WordPress flattens animated GIFs into one frame when generating intermediate sizes. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
* To avoid hiding animation in user content, if src is a full size GIF, a srcset attribute is not generated. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1342 |
* If src is an intermediate size GIF, the full size is excluded from srcset to keep a flattened GIF from becoming animated. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1343 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1344 |
if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1345 |
$image_sizes[] = array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1346 |
'width' => $image_meta['width'], |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1347 |
'height' => $image_meta['height'], |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
'file' => $image_basename, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1349 |
); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1350 |
} elseif ( str_contains( $image_src, $image_meta['file'] ) ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1351 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1352 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
// Retrieve the uploads sub-directory from the full size image. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1355 |
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1357 |
if ( $dirname ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
$dirname = trailingslashit( $dirname ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1359 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
|
| 9 | 1361 |
$upload_dir = wp_get_upload_dir(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
$image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
/* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
* If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
* (which is to say, when they share the domain name of the current request). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1368 |
if ( is_ssl() && ! str_starts_with( $image_baseurl, 'https' ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1369 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1370 |
* Since the `Host:` header might contain a port, it should |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1371 |
* be compared against the image URL using the same port. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1372 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1373 |
$parsed = parse_url( $image_baseurl ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1374 |
$domain = isset( $parsed['host'] ) ? $parsed['host'] : ''; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1375 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1376 |
if ( isset( $parsed['port'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1377 |
$domain .= ':' . $parsed['port']; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1378 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1379 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1380 |
if ( $_SERVER['HTTP_HOST'] === $domain ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1381 |
$image_baseurl = set_url_scheme( $image_baseurl, 'https' ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1382 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
/* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
* Images that have been edited in WordPress after being uploaded will |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
* contain a unique hash. Look for that hash and use it later to filter |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
* out images that are leftovers from previous versions. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
$image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash );
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
* Filters the maximum image width to be included in a 'srcset' attribute. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
* |
| 16 | 1397 |
* @param int $max_width The maximum image width to be included in the 'srcset'. Default '2048'. |
1398 |
* @param int[] $size_array {
|
|
1399 |
* An array of requested width and height values. |
|
1400 |
* |
|
1401 |
* @type int $0 The width in pixels. |
|
1402 |
* @type int $1 The height in pixels. |
|
1403 |
* } |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
*/ |
| 16 | 1405 |
$max_srcset_image_width = apply_filters( 'max_srcset_image_width', 2048, $size_array ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
// Array to hold URL candidates. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
$sources = array(); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* To make sure the ID matches our image src, we will check to see if any sizes in our attachment |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
* meta match our $image_src. If no matches are found we don't return a srcset to avoid serving |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
* an incorrect image. See #35045. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
$src_matched = false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
/* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
* Loop through available images. Only use images that are resized |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
* versions of the same edit. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
foreach ( $image_sizes as $image ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
$is_src = false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
// Check if image meta isn't corrupted. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
if ( ! is_array( $image ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
continue; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
// If the file name is part of the `src`, we've confirmed a match. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1430 |
if ( ! $src_matched && str_contains( $image_src, $dirname . $image['file'] ) ) {
|
| 16 | 1431 |
$src_matched = true; |
1432 |
$is_src = true; |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1433 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1434 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
// Filter out images that are from previous edits. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
continue; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1439 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1440 |
/* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1441 |
* Filters out images that are wider than '$max_srcset_image_width' unless |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1442 |
* that file is in the 'src' attribute. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1443 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1444 |
if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && ! $is_src ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1445 |
continue; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1446 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1447 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1448 |
// If the image dimensions are within 1px of the expected size, use it. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1449 |
if ( wp_image_matches_ratio( $image_width, $image_height, $image['width'], $image['height'] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1450 |
// Add the URL, descriptor, and value to the sources array to be returned. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1451 |
$source = array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
'url' => $image_baseurl . $image['file'], |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
'descriptor' => 'w', |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
'value' => $image['width'], |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1455 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1456 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1457 |
// The 'src' image has to be the first in the 'srcset', because of a bug in iOS8. See #35030. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
if ( $is_src ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
$sources = array( $image['width'] => $source ) + $sources; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1460 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1461 |
$sources[ $image['width'] ] = $source; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1462 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1463 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1464 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1465 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1466 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1467 |
* Filters an image's 'srcset' sources. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1468 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1469 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1470 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1471 |
* @param array $sources {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1472 |
* One or more arrays of source data to include in the 'srcset'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1473 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1474 |
* @type array $width {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1475 |
* @type string $url The URL of an image source. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1476 |
* @type string $descriptor The descriptor type used in the image candidate string, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1477 |
* either 'w' or 'x'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1478 |
* @type int $value The source width if paired with a 'w' descriptor, or a |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1479 |
* pixel density value if paired with an 'x' descriptor. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1480 |
* } |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1481 |
* } |
| 16 | 1482 |
* @param array $size_array {
|
1483 |
* An array of requested width and height values. |
|
1484 |
* |
|
1485 |
* @type int $0 The width in pixels. |
|
1486 |
* @type int $1 The height in pixels. |
|
1487 |
* } |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
* @param string $image_src The 'src' of the image. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1489 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
* @param int $attachment_id Image attachment ID or 0. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1491 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1493 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1494 |
// Only return a 'srcset' value if there is more than one source. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1495 |
if ( ! $src_matched || ! is_array( $sources ) || count( $sources ) < 2 ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1496 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1497 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1498 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1499 |
$srcset = ''; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1500 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1501 |
foreach ( $sources as $source ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1502 |
$srcset .= str_replace( ' ', '%20', $source['url'] ) . ' ' . $source['value'] . $source['descriptor'] . ', '; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1503 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1504 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1505 |
return rtrim( $srcset, ', ' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1506 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1507 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1508 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1509 |
* Retrieves the value for an image attachment's 'sizes' attribute. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1510 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1511 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1512 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1513 |
* @see wp_calculate_image_sizes() |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1514 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1515 |
* @param int $attachment_id Image attachment ID. |
| 18 | 1516 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
1517 |
* width and height values in pixels (in that order). Default 'medium'. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1518 |
* @param array|null $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1519 |
* Default null. |
| 18 | 1520 |
* @return string|false A valid source size value for use in a 'sizes' attribute or false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1521 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1522 |
function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
|
| 16 | 1523 |
$image = wp_get_attachment_image_src( $attachment_id, $size ); |
1524 |
||
1525 |
if ( ! $image ) {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1526 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1527 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1528 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1529 |
if ( ! is_array( $image_meta ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1530 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1531 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1532 |
|
| 9 | 1533 |
$image_src = $image[0]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1534 |
$size_array = array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1535 |
absint( $image[1] ), |
| 9 | 1536 |
absint( $image[2] ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1537 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1539 |
return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1541 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1542 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1543 |
* Creates a 'sizes' attribute value for an image. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1544 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1545 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1546 |
* |
| 18 | 1547 |
* @param string|int[] $size Image size. Accepts any registered image size name, or an array of |
1548 |
* width and height values in pixels (in that order). |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1549 |
* @param string|null $image_src Optional. The URL to the image file. Default null. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1550 |
* @param array|null $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1551 |
* Default null. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1552 |
* @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1553 |
* is needed when using the image size name as argument for `$size`. Default 0. |
| 18 | 1554 |
* @return string|false A valid source size value for use in a 'sizes' attribute or false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1555 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1556 |
function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1557 |
$width = 0; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1558 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1559 |
if ( is_array( $size ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1560 |
$width = absint( $size[0] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
} elseif ( is_string( $size ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1562 |
if ( ! $image_meta && $attachment_id ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1563 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1564 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1565 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1566 |
if ( is_array( $image_meta ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1567 |
$size_array = _wp_get_image_size_from_meta( $size, $image_meta ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1568 |
if ( $size_array ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1569 |
$width = absint( $size_array[0] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1570 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1571 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1572 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1573 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1574 |
if ( ! $width ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1575 |
return false; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1576 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1577 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1578 |
// Setup the default 'sizes' attribute. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1579 |
$sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1581 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1582 |
* Filters the output of 'wp_calculate_image_sizes()'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1583 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1584 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1585 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1586 |
* @param string $sizes A source size value for use in a 'sizes' attribute. |
| 18 | 1587 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
1588 |
* an array of width and height values in pixels (in that order). |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1589 |
* @param string|null $image_src The URL to the image file or null. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1590 |
* @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1591 |
* @param int $attachment_id Image attachment ID of the original image or 0. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1592 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1593 |
return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1594 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1595 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1596 |
/** |
| 16 | 1597 |
* Determines if the image meta data is for the image source file. |
1598 |
* |
|
1599 |
* The image meta data is retrieved by attachment post ID. In some cases the post IDs may change. |
|
1600 |
* For example when the website is exported and imported at another website. Then the |
|
1601 |
* attachment post IDs that are in post_content for the exported website may not match |
|
1602 |
* the same attachments at the new website. |
|
1603 |
* |
|
1604 |
* @since 5.5.0 |
|
1605 |
* |
|
1606 |
* @param string $image_location The full path or URI to the image file. |
|
1607 |
* @param array $image_meta The attachment meta data as returned by 'wp_get_attachment_metadata()'. |
|
1608 |
* @param int $attachment_id Optional. The image attachment ID. Default 0. |
|
1609 |
* @return bool Whether the image meta is for this image file. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1610 |
*/ |
| 16 | 1611 |
function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) {
|
1612 |
$match = false; |
|
1613 |
||
1614 |
// Ensure the $image_meta is valid. |
|
1615 |
if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1616 |
// Remove query args in image URI. |
| 16 | 1617 |
list( $image_location ) = explode( '?', $image_location ); |
1618 |
||
1619 |
// Check if the relative image path from the image meta is at the end of $image_location. |
|
1620 |
if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
|
|
1621 |
$match = true; |
|
1622 |
} else {
|
|
1623 |
// Retrieve the uploads sub-directory from the full size image. |
|
1624 |
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); |
|
1625 |
||
1626 |
if ( $dirname ) {
|
|
1627 |
$dirname = trailingslashit( $dirname ); |
|
1628 |
} |
|
1629 |
||
1630 |
if ( ! empty( $image_meta['original_image'] ) ) {
|
|
1631 |
$relative_path = $dirname . $image_meta['original_image']; |
|
1632 |
||
1633 |
if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
|
|
1634 |
$match = true; |
|
1635 |
} |
|
1636 |
} |
|
1637 |
||
1638 |
if ( ! $match && ! empty( $image_meta['sizes'] ) ) {
|
|
1639 |
foreach ( $image_meta['sizes'] as $image_size_data ) {
|
|
1640 |
$relative_path = $dirname . $image_size_data['file']; |
|
1641 |
||
1642 |
if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
|
|
1643 |
$match = true; |
|
1644 |
break; |
|
1645 |
} |
|
1646 |
} |
|
1647 |
} |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1648 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1649 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1650 |
|
| 16 | 1651 |
/** |
| 18 | 1652 |
* Filters whether an image path or URI matches image meta. |
| 16 | 1653 |
* |
1654 |
* @since 5.5.0 |
|
1655 |
* |
|
1656 |
* @param bool $match Whether the image relative path from the image meta |
|
1657 |
* matches the end of the URI or path to the image file. |
|
1658 |
* @param string $image_location Full path or URI to the tested image file. |
|
1659 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
1660 |
* @param int $attachment_id The image attachment ID or 0 if not supplied. |
|
1661 |
*/ |
|
1662 |
return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id ); |
|
1663 |
} |
|
1664 |
||
1665 |
/** |
|
1666 |
* Determines an image's width and height dimensions based on the source file. |
|
1667 |
* |
|
1668 |
* @since 5.5.0 |
|
1669 |
* |
|
1670 |
* @param string $image_src The image source file. |
|
1671 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
1672 |
* @param int $attachment_id Optional. The image attachment ID. Default 0. |
|
1673 |
* @return array|false Array with first element being the width and second element being the height, |
|
1674 |
* or false if dimensions cannot be determined. |
|
1675 |
*/ |
|
1676 |
function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {
|
|
| 18 | 1677 |
$dimensions = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1678 |
|
| 16 | 1679 |
// Is it a full size image? |
| 18 | 1680 |
if ( |
1681 |
isset( $image_meta['file'] ) && |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1682 |
str_contains( $image_src, wp_basename( $image_meta['file'] ) ) |
| 18 | 1683 |
) {
|
1684 |
$dimensions = array( |
|
| 16 | 1685 |
(int) $image_meta['width'], |
1686 |
(int) $image_meta['height'], |
|
1687 |
); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1688 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
|
| 18 | 1690 |
if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) {
|
| 16 | 1691 |
$src_filename = wp_basename( $image_src ); |
1692 |
||
1693 |
foreach ( $image_meta['sizes'] as $image_size_data ) {
|
|
1694 |
if ( $src_filename === $image_size_data['file'] ) {
|
|
| 18 | 1695 |
$dimensions = array( |
| 16 | 1696 |
(int) $image_size_data['width'], |
1697 |
(int) $image_size_data['height'], |
|
1698 |
); |
|
| 18 | 1699 |
|
1700 |
break; |
|
| 16 | 1701 |
} |
1702 |
} |
|
1703 |
} |
|
1704 |
||
| 18 | 1705 |
/** |
1706 |
* Filters the 'wp_image_src_get_dimensions' value. |
|
1707 |
* |
|
1708 |
* @since 5.7.0 |
|
1709 |
* |
|
1710 |
* @param array|false $dimensions Array with first element being the width |
|
1711 |
* and second element being the height, or |
|
1712 |
* false if dimensions could not be determined. |
|
1713 |
* @param string $image_src The image source file. |
|
1714 |
* @param array $image_meta The image meta data as returned by |
|
1715 |
* 'wp_get_attachment_metadata()'. |
|
1716 |
* @param int $attachment_id The image attachment ID. Default 0. |
|
1717 |
*/ |
|
1718 |
return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1719 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1720 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1721 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1722 |
* Adds 'srcset' and 'sizes' attributes to an existing 'img' element. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1723 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1724 |
* @since 4.4.0 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1725 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1726 |
* @see wp_calculate_image_srcset() |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1727 |
* @see wp_calculate_image_sizes() |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1728 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1729 |
* @param string $image An HTML 'img' element to be filtered. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1730 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1731 |
* @param int $attachment_id Image attachment ID. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1732 |
* @return string Converted 'img' element with 'srcset' and 'sizes' attributes added. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1733 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1734 |
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1735 |
// Ensure the image meta exists. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1736 |
if ( empty( $image_meta['sizes'] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1737 |
return $image; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1738 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1739 |
|
| 9 | 1740 |
$image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1741 |
list( $image_src ) = explode( '?', $image_src ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1742 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1743 |
// Return early if we couldn't get the image source. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1744 |
if ( ! $image_src ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1745 |
return $image; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1746 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1747 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1748 |
// Bail early if an image has been inserted and later edited. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1749 |
if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash )
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1750 |
&& ! str_contains( wp_basename( $image_src ), $img_edit_hash[0] ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1751 |
) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1752 |
return $image; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1753 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1754 |
|
| 9 | 1755 |
$width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1756 |
$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1757 |
|
| 16 | 1758 |
if ( $width && $height ) {
|
1759 |
$size_array = array( $width, $height ); |
|
1760 |
} else {
|
|
1761 |
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); |
|
1762 |
if ( ! $size_array ) {
|
|
1763 |
return $image; |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1764 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1766 |
|
| 16 | 1767 |
$srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1768 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1769 |
if ( $srcset ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1770 |
// Check if there is already a 'sizes' attribute. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1771 |
$sizes = strpos( $image, ' sizes=' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1772 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1773 |
if ( ! $sizes ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1774 |
$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1775 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1776 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1777 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1778 |
if ( $srcset && $sizes ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1779 |
// Format the 'srcset' and 'sizes' string and escape attributes. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1780 |
$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1781 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1782 |
if ( is_string( $sizes ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1783 |
$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1784 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1785 |
|
| 16 | 1786 |
// Add the srcset and sizes attributes to the image markup. |
1787 |
return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image ); |
|
1788 |
} |
|
1789 |
||
1790 |
return $image; |
|
1791 |
} |
|
1792 |
||
1793 |
/** |
|
| 18 | 1794 |
* Determines whether to add the `loading` attribute to the specified tag in the specified context. |
| 16 | 1795 |
* |
1796 |
* @since 5.5.0 |
|
| 18 | 1797 |
* @since 5.7.0 Now returns `true` by default for `iframe` tags. |
| 16 | 1798 |
* |
1799 |
* @param string $tag_name The tag name. |
|
| 18 | 1800 |
* @param string $context Additional context, like the current filter name |
1801 |
* or the function name from where this was called. |
|
| 16 | 1802 |
* @return bool Whether to add the attribute. |
1803 |
*/ |
|
1804 |
function wp_lazy_loading_enabled( $tag_name, $context ) {
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1805 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1806 |
* By default add to all 'img' and 'iframe' tags. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1807 |
* See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1808 |
* See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1809 |
*/ |
| 18 | 1810 |
$default = ( 'img' === $tag_name || 'iframe' === $tag_name ); |
| 16 | 1811 |
|
1812 |
/** |
|
1813 |
* Filters whether to add the `loading` attribute to the specified tag in the specified context. |
|
1814 |
* |
|
1815 |
* @since 5.5.0 |
|
1816 |
* |
|
1817 |
* @param bool $default Default value. |
|
1818 |
* @param string $tag_name The tag name. |
|
| 18 | 1819 |
* @param string $context Additional context, like the current filter name |
1820 |
* or the function name from where this was called. |
|
| 16 | 1821 |
*/ |
1822 |
return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context ); |
|
1823 |
} |
|
1824 |
||
1825 |
/** |
|
1826 |
* Filters specific tags in post content and modifies their markup. |
|
1827 |
* |
|
1828 |
* Modifies HTML tags in post content to include new browser and HTML technologies |
|
1829 |
* that may not have existed at the time of post creation. These modifications currently |
|
| 18 | 1830 |
* include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags, as well |
1831 |
* as adding `loading` attributes to `iframe` HTML tags. |
|
| 16 | 1832 |
* Future similar optimizations should be added/expected here. |
1833 |
* |
|
1834 |
* @since 5.5.0 |
|
| 18 | 1835 |
* @since 5.7.0 Now supports adding `loading` attributes to `iframe` tags. |
| 16 | 1836 |
* |
1837 |
* @see wp_img_tag_add_width_and_height_attr() |
|
1838 |
* @see wp_img_tag_add_srcset_and_sizes_attr() |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1839 |
* @see wp_img_tag_add_loading_optimization_attrs() |
| 18 | 1840 |
* @see wp_iframe_tag_add_loading_attr() |
| 16 | 1841 |
* |
1842 |
* @param string $content The HTML content to be filtered. |
|
1843 |
* @param string $context Optional. Additional context to pass to the filters. |
|
1844 |
* Defaults to `current_filter()` when not set. |
|
1845 |
* @return string Converted content with images modified. |
|
1846 |
*/ |
|
1847 |
function wp_filter_content_tags( $content, $context = null ) {
|
|
1848 |
if ( null === $context ) {
|
|
1849 |
$context = current_filter(); |
|
1850 |
} |
|
1851 |
||
| 18 | 1852 |
$add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context ); |
1853 |
||
1854 |
if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) {
|
|
| 16 | 1855 |
return $content; |
1856 |
} |
|
1857 |
||
1858 |
// List of the unique `img` tags found in $content. |
|
1859 |
$images = array(); |
|
1860 |
||
| 18 | 1861 |
// List of the unique `iframe` tags found in $content. |
1862 |
$iframes = array(); |
|
1863 |
||
1864 |
foreach ( $matches as $match ) {
|
|
1865 |
list( $tag, $tag_name ) = $match; |
|
1866 |
||
1867 |
switch ( $tag_name ) {
|
|
1868 |
case 'img': |
|
1869 |
if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $class_id ) ) {
|
|
1870 |
$attachment_id = absint( $class_id[1] ); |
|
1871 |
||
1872 |
if ( $attachment_id ) {
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1873 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1874 |
* If exactly the same image tag is used more than once, overwrite it. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1875 |
* All identical tags will be replaced later with 'str_replace()'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1876 |
*/ |
| 18 | 1877 |
$images[ $tag ] = $attachment_id; |
1878 |
break; |
|
1879 |
} |
|
1880 |
} |
|
1881 |
$images[ $tag ] = 0; |
|
1882 |
break; |
|
1883 |
case 'iframe': |
|
1884 |
$iframes[ $tag ] = 0; |
|
1885 |
break; |
|
| 16 | 1886 |
} |
1887 |
} |
|
1888 |
||
1889 |
// Reduce the array to unique attachment IDs. |
|
1890 |
$attachment_ids = array_unique( array_filter( array_values( $images ) ) ); |
|
1891 |
||
1892 |
if ( count( $attachment_ids ) > 1 ) {
|
|
1893 |
/* |
|
1894 |
* Warm the object cache with post and meta information for all found |
|
1895 |
* images to avoid making individual database calls. |
|
1896 |
*/ |
|
1897 |
_prime_post_caches( $attachment_ids, false, true ); |
|
1898 |
} |
|
1899 |
||
| 19 | 1900 |
// Iterate through the matches in order of occurrence as it is relevant for whether or not to lazy-load. |
1901 |
foreach ( $matches as $match ) {
|
|
1902 |
// Filter an image match. |
|
1903 |
if ( isset( $images[ $match[0] ] ) ) {
|
|
1904 |
$filtered_image = $match[0]; |
|
1905 |
$attachment_id = $images[ $match[0] ]; |
|
1906 |
||
1907 |
// Add 'width' and 'height' attributes if applicable. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1908 |
if ( $attachment_id > 0 && ! str_contains( $filtered_image, ' width=' ) && ! str_contains( $filtered_image, ' height=' ) ) {
|
| 19 | 1909 |
$filtered_image = wp_img_tag_add_width_and_height_attr( $filtered_image, $context, $attachment_id ); |
1910 |
} |
|
1911 |
||
1912 |
// Add 'srcset' and 'sizes' attributes if applicable. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1913 |
if ( $attachment_id > 0 && ! str_contains( $filtered_image, ' srcset=' ) ) {
|
| 19 | 1914 |
$filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id ); |
1915 |
} |
|
1916 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1917 |
// Add loading optimization attributes if applicable. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1918 |
$filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context ); |
| 19 | 1919 |
|
1920 |
/** |
|
1921 |
* Filters an img tag within the content for a given context. |
|
1922 |
* |
|
1923 |
* @since 6.0.0 |
|
1924 |
* |
|
1925 |
* @param string $filtered_image Full img tag with attributes that will replace the source img tag. |
|
1926 |
* @param string $context Additional context, like the current filter name or the function name from where this was called. |
|
1927 |
* @param int $attachment_id The image attachment ID. May be 0 in case the image is not an attachment. |
|
1928 |
*/ |
|
1929 |
$filtered_image = apply_filters( 'wp_content_img_tag', $filtered_image, $context, $attachment_id ); |
|
1930 |
||
1931 |
if ( $filtered_image !== $match[0] ) {
|
|
1932 |
$content = str_replace( $match[0], $filtered_image, $content ); |
|
1933 |
} |
|
1934 |
||
1935 |
/* |
|
1936 |
* Unset image lookup to not run the same logic again unnecessarily if the same image tag is used more than |
|
1937 |
* once in the same blob of content. |
|
1938 |
*/ |
|
1939 |
unset( $images[ $match[0] ] ); |
|
| 16 | 1940 |
} |
1941 |
||
| 19 | 1942 |
// Filter an iframe match. |
1943 |
if ( isset( $iframes[ $match[0] ] ) ) {
|
|
1944 |
$filtered_iframe = $match[0]; |
|
1945 |
||
1946 |
// Add 'loading' attribute if applicable. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1947 |
if ( $add_iframe_loading_attr && ! str_contains( $filtered_iframe, ' loading=' ) ) {
|
| 19 | 1948 |
$filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context ); |
1949 |
} |
|
1950 |
||
1951 |
if ( $filtered_iframe !== $match[0] ) {
|
|
1952 |
$content = str_replace( $match[0], $filtered_iframe, $content ); |
|
1953 |
} |
|
1954 |
||
1955 |
/* |
|
1956 |
* Unset iframe lookup to not run the same logic again unnecessarily if the same iframe tag is used more |
|
1957 |
* than once in the same blob of content. |
|
1958 |
*/ |
|
1959 |
unset( $iframes[ $match[0] ] ); |
|
| 18 | 1960 |
} |
1961 |
} |
|
1962 |
||
| 16 | 1963 |
return $content; |
1964 |
} |
|
1965 |
||
1966 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1967 |
* Adds optimization attributes to an `img` HTML tag. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1968 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1969 |
* @since 6.3.0 |
| 16 | 1970 |
* |
1971 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
1972 |
* @param string $context Additional context to pass to the filters. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1973 |
* @return string Converted `img` tag with optimization attributes added. |
| 16 | 1974 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1975 |
function wp_img_tag_add_loading_optimization_attrs( $image, $context ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1976 |
$width = preg_match( '/ width=["\']([0-9]+)["\']/', $image, $match_width ) ? (int) $match_width[1] : null; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1977 |
$height = preg_match( '/ height=["\']([0-9]+)["\']/', $image, $match_height ) ? (int) $match_height[1] : null; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1978 |
$loading_val = preg_match( '/ loading=["\']([A-Za-z]+)["\']/', $image, $match_loading ) ? $match_loading[1] : null; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1979 |
$fetchpriority_val = preg_match( '/ fetchpriority=["\']([A-Za-z]+)["\']/', $image, $match_fetchpriority ) ? $match_fetchpriority[1] : null; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1980 |
$decoding_val = preg_match( '/ decoding=["\']([A-Za-z]+)["\']/', $image, $match_decoding ) ? $match_decoding[1] : null; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1981 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1982 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1983 |
* Get loading optimization attributes to use. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1984 |
* This must occur before the conditional check below so that even images |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1985 |
* that are ineligible for being lazy-loaded are considered. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1986 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1987 |
$optimization_attrs = wp_get_loading_optimization_attributes( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1988 |
'img', |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1989 |
array( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1990 |
'width' => $width, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1991 |
'height' => $height, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1992 |
'loading' => $loading_val, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1993 |
'fetchpriority' => $fetchpriority_val, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1994 |
'decoding' => $decoding_val, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1995 |
), |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1996 |
$context |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1997 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1998 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1999 |
// Images should have source for the loading optimization attributes to be added. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2000 |
if ( ! str_contains( $image, ' src="' ) ) {
|
| 18 | 2001 |
return $image; |
2002 |
} |
|
2003 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2004 |
if ( empty( $decoding_val ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2005 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2006 |
* Filters the `decoding` attribute value to add to an image. Default `async`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2007 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2008 |
* Returning a falsey value will omit the attribute. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2009 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2010 |
* @since 6.1.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2011 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2012 |
* @param string|false|null $value The `decoding` attribute value. Returning a falsey value |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2013 |
* will result in the attribute being omitted for the image. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2014 |
* Otherwise, it may be: 'async', 'sync', or 'auto'. Defaults to false. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2015 |
* @param string $image The HTML `img` tag to be filtered. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2016 |
* @param string $context Additional context about how the function was called |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2017 |
* or where the img tag is. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2018 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2019 |
$filtered_decoding_attr = apply_filters( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2020 |
'wp_img_tag_add_decoding_attr', |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2021 |
isset( $optimization_attrs['decoding'] ) ? $optimization_attrs['decoding'] : false, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2022 |
$image, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2023 |
$context |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2024 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2025 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2026 |
// Validate the values after filtering. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2027 |
if ( isset( $optimization_attrs['decoding'] ) && ! $filtered_decoding_attr ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2028 |
// Unset `decoding` attribute if `$filtered_decoding_attr` is set to `false`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2029 |
unset( $optimization_attrs['decoding'] ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2030 |
} elseif ( in_array( $filtered_decoding_attr, array( 'async', 'sync', 'auto' ), true ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2031 |
$optimization_attrs['decoding'] = $filtered_decoding_attr; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2032 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2033 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2034 |
if ( ! empty( $optimization_attrs['decoding'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2035 |
$image = str_replace( '<img', '<img decoding="' . esc_attr( $optimization_attrs['decoding'] ) . '"', $image ); |
| 16 | 2036 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2037 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2038 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2039 |
// Images should have dimension attributes for the 'loading' and 'fetchpriority' attributes to be added. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2040 |
if ( ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2041 |
return $image; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2042 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2043 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2044 |
// Retained for backward compatibility. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2045 |
$loading_attrs_enabled = wp_lazy_loading_enabled( 'img', $context ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2046 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2047 |
if ( empty( $loading_val ) && $loading_attrs_enabled ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2048 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2049 |
* Filters the `loading` attribute value to add to an image. Default `lazy`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2050 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2051 |
* Returning `false` or an empty string will not add the attribute. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2052 |
* Returning `true` will add the default value. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2053 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2054 |
* @since 5.5.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2055 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2056 |
* @param string|bool $value The `loading` attribute value. Returning a falsey value will result in |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2057 |
* the attribute being omitted for the image. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2058 |
* @param string $image The HTML `img` tag to be filtered. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2059 |
* @param string $context Additional context about how the function was called or where the img tag is. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2060 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2061 |
$filtered_loading_attr = apply_filters( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2062 |
'wp_img_tag_add_loading_attr', |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2063 |
isset( $optimization_attrs['loading'] ) ? $optimization_attrs['loading'] : false, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2064 |
$image, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2065 |
$context |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2066 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2067 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2068 |
// Validate the values after filtering. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2069 |
if ( isset( $optimization_attrs['loading'] ) && ! $filtered_loading_attr ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2070 |
// Unset `loading` attributes if `$filtered_loading_attr` is set to `false`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2071 |
unset( $optimization_attrs['loading'] ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2072 |
} elseif ( in_array( $filtered_loading_attr, array( 'lazy', 'eager' ), true ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2073 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2074 |
* If the filter changed the loading attribute to "lazy" when a fetchpriority attribute |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2075 |
* with value "high" is already present, trigger a warning since those two attribute |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2076 |
* values should be mutually exclusive. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2077 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2078 |
* The same warning is present in `wp_get_loading_optimization_attributes()`, and here it |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2079 |
* is only intended for the specific scenario where the above filtered caused the problem. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2080 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2081 |
if ( isset( $optimization_attrs['fetchpriority'] ) && 'high' === $optimization_attrs['fetchpriority'] && |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2082 |
( isset( $optimization_attrs['loading'] ) ? $optimization_attrs['loading'] : false ) !== $filtered_loading_attr && |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2083 |
'lazy' === $filtered_loading_attr |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2084 |
) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2085 |
_doing_it_wrong( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2086 |
__FUNCTION__, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2087 |
__( 'An image should not be lazy-loaded and marked as high priority at the same time.' ), |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2088 |
'6.3.0' |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2089 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2090 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2091 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2092 |
// The filtered value will still be respected. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2093 |
$optimization_attrs['loading'] = $filtered_loading_attr; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2094 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2095 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2096 |
if ( ! empty( $optimization_attrs['loading'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2097 |
$image = str_replace( '<img', '<img loading="' . esc_attr( $optimization_attrs['loading'] ) . '"', $image ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2098 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2099 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2100 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2101 |
if ( empty( $fetchpriority_val ) && ! empty( $optimization_attrs['fetchpriority'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2102 |
$image = str_replace( '<img', '<img fetchpriority="' . esc_attr( $optimization_attrs['fetchpriority'] ) . '"', $image ); |
| 16 | 2103 |
} |
2104 |
||
2105 |
return $image; |
|
2106 |
} |
|
2107 |
||
2108 |
/** |
|
2109 |
* Adds `width` and `height` attributes to an `img` HTML tag. |
|
2110 |
* |
|
2111 |
* @since 5.5.0 |
|
2112 |
* |
|
2113 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
2114 |
* @param string $context Additional context to pass to the filters. |
|
2115 |
* @param int $attachment_id Image attachment ID. |
|
2116 |
* @return string Converted 'img' element with 'width' and 'height' attributes added. |
|
2117 |
*/ |
|
2118 |
function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id ) {
|
|
2119 |
$image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; |
|
2120 |
list( $image_src ) = explode( '?', $image_src ); |
|
2121 |
||
2122 |
// Return early if we couldn't get the image source. |
|
2123 |
if ( ! $image_src ) {
|
|
2124 |
return $image; |
|
2125 |
} |
|
2126 |
||
2127 |
/** |
|
2128 |
* Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`. |
|
2129 |
* |
|
2130 |
* Returning anything else than `true` will not add the attributes. |
|
2131 |
* |
|
2132 |
* @since 5.5.0 |
|
2133 |
* |
|
2134 |
* @param bool $value The filtered value, defaults to `true`. |
|
2135 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
2136 |
* @param string $context Additional context about how the function was called or where the img tag is. |
|
2137 |
* @param int $attachment_id The image attachment ID. |
|
2138 |
*/ |
|
2139 |
$add = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id ); |
|
2140 |
||
2141 |
if ( true === $add ) {
|
|
2142 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
2143 |
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); |
|
2144 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2145 |
if ( $size_array && $size_array[0] && $size_array[1] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2146 |
// If the width is enforced through style (e.g. in an inline image), calculate the dimension attributes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2147 |
$style_width = preg_match( '/style="width:\s*(\d+)px;"/', $image, $match_width ) ? (int) $match_width[1] : 0; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2148 |
if ( $style_width ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2149 |
$size_array[1] = (int) round( $size_array[1] * $style_width / $size_array[0] ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2150 |
$size_array[0] = $style_width; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2151 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2152 |
|
| 16 | 2153 |
$hw = trim( image_hwstring( $size_array[0], $size_array[1] ) ); |
2154 |
return str_replace( '<img', "<img {$hw}", $image );
|
|
2155 |
} |
|
2156 |
} |
|
2157 |
||
2158 |
return $image; |
|
2159 |
} |
|
2160 |
||
2161 |
/** |
|
2162 |
* Adds `srcset` and `sizes` attributes to an existing `img` HTML tag. |
|
2163 |
* |
|
2164 |
* @since 5.5.0 |
|
2165 |
* |
|
2166 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
2167 |
* @param string $context Additional context to pass to the filters. |
|
2168 |
* @param int $attachment_id Image attachment ID. |
|
2169 |
* @return string Converted 'img' element with 'loading' attribute added. |
|
2170 |
*/ |
|
2171 |
function wp_img_tag_add_srcset_and_sizes_attr( $image, $context, $attachment_id ) {
|
|
2172 |
/** |
|
2173 |
* Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`. |
|
2174 |
* |
|
2175 |
* Returning anything else than `true` will not add the attributes. |
|
2176 |
* |
|
2177 |
* @since 5.5.0 |
|
2178 |
* |
|
2179 |
* @param bool $value The filtered value, defaults to `true`. |
|
2180 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
2181 |
* @param string $context Additional context about how the function was called or where the img tag is. |
|
2182 |
* @param int $attachment_id The image attachment ID. |
|
2183 |
*/ |
|
2184 |
$add = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id ); |
|
2185 |
||
2186 |
if ( true === $add ) {
|
|
2187 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
2188 |
return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2189 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2190 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2191 |
return $image; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2192 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2193 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2194 |
/** |
| 18 | 2195 |
* Adds `loading` attribute to an `iframe` HTML tag. |
2196 |
* |
|
2197 |
* @since 5.7.0 |
|
2198 |
* |
|
2199 |
* @param string $iframe The HTML `iframe` tag where the attribute should be added. |
|
2200 |
* @param string $context Additional context to pass to the filters. |
|
2201 |
* @return string Converted `iframe` tag with `loading` attribute added. |
|
2202 |
*/ |
|
2203 |
function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2204 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2205 |
* Get loading attribute value to use. This must occur before the conditional check below so that even iframes that |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2206 |
* are ineligible for being lazy-loaded are considered. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2207 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2208 |
$optimization_attrs = wp_get_loading_optimization_attributes( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2209 |
'iframe', |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2210 |
array( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2211 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2212 |
* The concrete values for width and height are not important here for now |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2213 |
* since fetchpriority is not yet supported for iframes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2214 |
* TODO: Use WP_HTML_Tag_Processor to extract actual values once support is |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2215 |
* added. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2216 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2217 |
'width' => str_contains( $iframe, ' width="' ) ? 100 : null, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2218 |
'height' => str_contains( $iframe, ' height="' ) ? 100 : null, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2219 |
// This function is never called when a 'loading' attribute is already present. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2220 |
'loading' => null, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2221 |
), |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2222 |
$context |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2223 |
); |
| 19 | 2224 |
|
| 18 | 2225 |
// Iframes should have source and dimension attributes for the `loading` attribute to be added. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2226 |
if ( ! str_contains( $iframe, ' src="' ) || ! str_contains( $iframe, ' width="' ) || ! str_contains( $iframe, ' height="' ) ) {
|
| 18 | 2227 |
return $iframe; |
2228 |
} |
|
2229 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2230 |
$value = isset( $optimization_attrs['loading'] ) ? $optimization_attrs['loading'] : false; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2231 |
|
| 18 | 2232 |
/** |
2233 |
* Filters the `loading` attribute value to add to an iframe. Default `lazy`. |
|
2234 |
* |
|
2235 |
* Returning `false` or an empty string will not add the attribute. |
|
2236 |
* Returning `true` will add the default value. |
|
2237 |
* |
|
2238 |
* @since 5.7.0 |
|
2239 |
* |
|
2240 |
* @param string|bool $value The `loading` attribute value. Returning a falsey value will result in |
|
| 19 | 2241 |
* the attribute being omitted for the iframe. |
| 18 | 2242 |
* @param string $iframe The HTML `iframe` tag to be filtered. |
2243 |
* @param string $context Additional context about how the function was called or where the iframe tag is. |
|
2244 |
*/ |
|
| 19 | 2245 |
$value = apply_filters( 'wp_iframe_tag_add_loading_attr', $value, $iframe, $context ); |
| 18 | 2246 |
|
2247 |
if ( $value ) {
|
|
2248 |
if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
|
|
2249 |
$value = 'lazy'; |
|
2250 |
} |
|
2251 |
||
2252 |
return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe ); |
|
2253 |
} |
|
2254 |
||
2255 |
return $iframe; |
|
2256 |
} |
|
2257 |
||
2258 |
/** |
|
| 5 | 2259 |
* Adds a 'wp-post-image' class to post thumbnails. Internal use only. |
2260 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2261 |
* Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'}
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2262 |
* action hooks to dynamically add/remove itself so as to only filter post thumbnails. |
| 0 | 2263 |
* |
| 5 | 2264 |
* @ignore |
| 0 | 2265 |
* @since 2.9.0 |
| 5 | 2266 |
* |
| 16 | 2267 |
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name. |
2268 |
* @return string[] Modified array of attributes including the new 'wp-post-image' class. |
|
| 0 | 2269 |
*/ |
2270 |
function _wp_post_thumbnail_class_filter( $attr ) {
|
|
2271 |
$attr['class'] .= ' wp-post-image'; |
|
2272 |
return $attr; |
|
2273 |
} |
|
2274 |
||
2275 |
/** |
|
| 5 | 2276 |
* Adds '_wp_post_thumbnail_class_filter' callback to the 'wp_get_attachment_image_attributes' |
2277 |
* filter hook. Internal use only. |
|
| 0 | 2278 |
* |
| 5 | 2279 |
* @ignore |
| 0 | 2280 |
* @since 2.9.0 |
| 5 | 2281 |
* |
| 16 | 2282 |
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name. |
| 0 | 2283 |
*/ |
2284 |
function _wp_post_thumbnail_class_filter_add( $attr ) {
|
|
2285 |
add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
|
2286 |
} |
|
2287 |
||
2288 |
/** |
|
| 5 | 2289 |
* Removes the '_wp_post_thumbnail_class_filter' callback from the 'wp_get_attachment_image_attributes' |
2290 |
* filter hook. Internal use only. |
|
| 0 | 2291 |
* |
| 5 | 2292 |
* @ignore |
| 0 | 2293 |
* @since 2.9.0 |
| 5 | 2294 |
* |
| 16 | 2295 |
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name. |
| 0 | 2296 |
*/ |
2297 |
function _wp_post_thumbnail_class_filter_remove( $attr ) {
|
|
2298 |
remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
|
2299 |
} |
|
2300 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2301 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2302 |
* Overrides the context used in {@see wp_get_attachment_image()}. Internal use only.
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2303 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2304 |
* Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'}
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2305 |
* action hooks to dynamically add/remove itself so as to only filter post thumbnails. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2306 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2307 |
* @ignore |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2308 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2309 |
* @access private |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2310 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2311 |
* @param string $context The context for rendering an attachment image. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2312 |
* @return string Modified context set to 'the_post_thumbnail'. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2313 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2314 |
function _wp_post_thumbnail_context_filter( $context ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2315 |
return 'the_post_thumbnail'; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2316 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2317 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2318 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2319 |
* Adds the '_wp_post_thumbnail_context_filter' callback to the 'wp_get_attachment_image_context' |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2320 |
* filter hook. Internal use only. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2321 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2322 |
* @ignore |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2323 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2324 |
* @access private |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2325 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2326 |
function _wp_post_thumbnail_context_filter_add() {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2327 |
add_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2328 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2329 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2330 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2331 |
* Removes the '_wp_post_thumbnail_context_filter' callback from the 'wp_get_attachment_image_context' |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2332 |
* filter hook. Internal use only. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2333 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2334 |
* @ignore |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2335 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2336 |
* @access private |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2337 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2338 |
function _wp_post_thumbnail_context_filter_remove() {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2339 |
remove_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2340 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2341 |
|
| 9 | 2342 |
add_shortcode( 'wp_caption', 'img_caption_shortcode' ); |
2343 |
add_shortcode( 'caption', 'img_caption_shortcode' ); |
|
| 0 | 2344 |
|
2345 |
/** |
|
| 5 | 2346 |
* Builds the Caption shortcode output. |
| 0 | 2347 |
* |
2348 |
* Allows a plugin to replace the content that would otherwise be returned. The |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2349 |
* filter is {@see 'img_caption_shortcode'} and passes an empty string, the attr
|
| 0 | 2350 |
* parameter and the content parameter values. |
2351 |
* |
|
| 9 | 2352 |
* The supported attributes for the shortcode are 'id', 'caption_id', 'align', |
2353 |
* 'width', 'caption', and 'class'. |
|
| 0 | 2354 |
* |
2355 |
* @since 2.6.0 |
|
| 9 | 2356 |
* @since 3.9.0 The `class` attribute was added. |
2357 |
* @since 5.1.0 The `caption_id` attribute was added. |
|
| 19 | 2358 |
* @since 5.9.0 The `$content` parameter default value changed from `null` to `''`. |
| 0 | 2359 |
* |
| 5 | 2360 |
* @param array $attr {
|
2361 |
* Attributes of the caption shortcode. |
|
2362 |
* |
|
| 9 | 2363 |
* @type string $id ID of the image and caption container element, i.e. `<figure>` or `<div>`. |
2364 |
* @type string $caption_id ID of the caption element, i.e. `<figcaption>` or `<p>`. |
|
2365 |
* @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft', |
|
2366 |
* 'aligncenter', alignright', 'alignnone'. |
|
2367 |
* @type int $width The width of the caption, in pixels. |
|
2368 |
* @type string $caption The caption text. |
|
2369 |
* @type string $class Additional class name(s) added to the caption container. |
|
| 5 | 2370 |
* } |
| 19 | 2371 |
* @param string $content Optional. Shortcode content. Default empty string. |
| 5 | 2372 |
* @return string HTML content to display the caption. |
| 0 | 2373 |
*/ |
| 19 | 2374 |
function img_caption_shortcode( $attr, $content = '' ) {
|
| 0 | 2375 |
// New-style shortcode with the caption inside the shortcode with the link and image tags. |
2376 |
if ( ! isset( $attr['caption'] ) ) {
|
|
2377 |
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
|
|
| 9 | 2378 |
$content = $matches[1]; |
| 0 | 2379 |
$attr['caption'] = trim( $matches[2] ); |
2380 |
} |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2381 |
} elseif ( str_contains( $attr['caption'], '<' ) ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2382 |
$attr['caption'] = wp_kses( $attr['caption'], 'post' ); |
| 0 | 2383 |
} |
2384 |
||
| 5 | 2385 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2386 |
* Filters the default caption shortcode output. |
| 5 | 2387 |
* |
2388 |
* If the filtered output isn't empty, it will be used instead of generating |
|
2389 |
* the default caption template. |
|
2390 |
* |
|
2391 |
* @since 2.6.0 |
|
2392 |
* |
|
2393 |
* @see img_caption_shortcode() |
|
2394 |
* |
|
2395 |
* @param string $output The caption output. Default empty. |
|
2396 |
* @param array $attr Attributes of the caption shortcode. |
|
2397 |
* @param string $content The image element, possibly wrapped in a hyperlink. |
|
2398 |
*/ |
|
2399 |
$output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); |
|
| 16 | 2400 |
|
2401 |
if ( ! empty( $output ) ) {
|
|
| 0 | 2402 |
return $output; |
| 9 | 2403 |
} |
2404 |
||
2405 |
$atts = shortcode_atts( |
|
2406 |
array( |
|
2407 |
'id' => '', |
|
2408 |
'caption_id' => '', |
|
2409 |
'align' => 'alignnone', |
|
2410 |
'width' => '', |
|
2411 |
'caption' => '', |
|
2412 |
'class' => '', |
|
2413 |
), |
|
2414 |
$attr, |
|
2415 |
'caption' |
|
2416 |
); |
|
| 0 | 2417 |
|
2418 |
$atts['width'] = (int) $atts['width']; |
|
| 16 | 2419 |
|
| 9 | 2420 |
if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) {
|
| 0 | 2421 |
return $content; |
| 9 | 2422 |
} |
2423 |
||
| 16 | 2424 |
$id = ''; |
2425 |
$caption_id = ''; |
|
2426 |
$describedby = ''; |
|
| 9 | 2427 |
|
2428 |
if ( $atts['id'] ) {
|
|
2429 |
$atts['id'] = sanitize_html_class( $atts['id'] ); |
|
2430 |
$id = 'id="' . esc_attr( $atts['id'] ) . '" '; |
|
2431 |
} |
|
2432 |
||
2433 |
if ( $atts['caption_id'] ) {
|
|
2434 |
$atts['caption_id'] = sanitize_html_class( $atts['caption_id'] ); |
|
2435 |
} elseif ( $atts['id'] ) {
|
|
2436 |
$atts['caption_id'] = 'caption-' . str_replace( '_', '-', $atts['id'] ); |
|
2437 |
} |
|
2438 |
||
2439 |
if ( $atts['caption_id'] ) {
|
|
2440 |
$caption_id = 'id="' . esc_attr( $atts['caption_id'] ) . '" '; |
|
2441 |
$describedby = 'aria-describedby="' . esc_attr( $atts['caption_id'] ) . '" '; |
|
2442 |
} |
|
| 0 | 2443 |
|
| 5 | 2444 |
$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] ); |
2445 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2446 |
$html5 = current_theme_supports( 'html5', 'caption' ); |
| 16 | 2447 |
// HTML5 captions never added the extra 10px to the image width. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2448 |
$width = $html5 ? $atts['width'] : ( 10 + $atts['width'] ); |
| 0 | 2449 |
|
2450 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2451 |
* Filters the width of an image's caption. |
| 0 | 2452 |
* |
2453 |
* By default, the caption is 10 pixels greater than the width of the image, |
|
2454 |
* to prevent post content from running up against a floated image. |
|
2455 |
* |
|
2456 |
* @since 3.7.0 |
|
2457 |
* |
|
| 5 | 2458 |
* @see img_caption_shortcode() |
| 0 | 2459 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2460 |
* @param int $width Width of the caption in pixels. To remove this inline style, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2461 |
* return zero. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2462 |
* @param array $atts Attributes of the caption shortcode. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2463 |
* @param string $content The image element, possibly wrapped in a hyperlink. |
| 0 | 2464 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2465 |
$caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content ); |
| 0 | 2466 |
|
2467 |
$style = ''; |
|
| 16 | 2468 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2469 |
if ( $caption_width ) {
|
| 0 | 2470 |
$style = 'style="width: ' . (int) $caption_width . 'px" '; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2471 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2472 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2473 |
if ( $html5 ) {
|
| 9 | 2474 |
$html = sprintf( |
2475 |
'<figure %s%s%sclass="%s">%s%s</figure>', |
|
2476 |
$id, |
|
2477 |
$describedby, |
|
2478 |
$style, |
|
2479 |
esc_attr( $class ), |
|
2480 |
do_shortcode( $content ), |
|
2481 |
sprintf( |
|
2482 |
'<figcaption %sclass="wp-caption-text">%s</figcaption>', |
|
2483 |
$caption_id, |
|
2484 |
$atts['caption'] |
|
2485 |
) |
|
2486 |
); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2487 |
} else {
|
| 9 | 2488 |
$html = sprintf( |
2489 |
'<div %s%sclass="%s">%s%s</div>', |
|
2490 |
$id, |
|
2491 |
$style, |
|
2492 |
esc_attr( $class ), |
|
2493 |
str_replace( '<img ', '<img ' . $describedby, do_shortcode( $content ) ), |
|
2494 |
sprintf( |
|
2495 |
'<p %sclass="wp-caption-text">%s</p>', |
|
2496 |
$caption_id, |
|
2497 |
$atts['caption'] |
|
2498 |
) |
|
2499 |
); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2500 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2501 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2502 |
return $html; |
| 0 | 2503 |
} |
2504 |
||
| 9 | 2505 |
add_shortcode( 'gallery', 'gallery_shortcode' ); |
| 0 | 2506 |
|
2507 |
/** |
|
| 5 | 2508 |
* Builds the Gallery shortcode output. |
| 0 | 2509 |
* |
2510 |
* This implements the functionality of the Gallery Shortcode for displaying |
|
2511 |
* WordPress images on a post. |
|
2512 |
* |
|
2513 |
* @since 2.5.0 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2514 |
* @since 2.8.0 Added the `$attr` parameter to set the shortcode output. New attributes included |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2515 |
* such as `size`, `itemtag`, `icontag`, `captiontag`, and columns. Changed markup from |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2516 |
* `div` tags to `dl`, `dt` and `dd` tags. Support more than one gallery on the |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2517 |
* same page. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2518 |
* @since 2.9.0 Added support for `include` and `exclude` to shortcode. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2519 |
* @since 3.5.0 Use get_post() instead of global `$post`. Handle mapping of `ids` to `include` |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2520 |
* and `orderby`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2521 |
* @since 3.6.0 Added validation for tags used in gallery shortcode. Add orientation information to items. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2522 |
* @since 3.7.0 Introduced the `link` attribute. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2523 |
* @since 3.9.0 `html5` gallery support, accepting 'itemtag', 'icontag', and 'captiontag' attributes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2524 |
* @since 4.0.0 Removed use of `extract()`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2525 |
* @since 4.1.0 Added attribute to `wp_get_attachment_link()` to output `aria-describedby`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2526 |
* @since 4.2.0 Passed the shortcode instance ID to `post_gallery` and `post_playlist` filters. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2527 |
* @since 4.6.0 Standardized filter docs to match documentation standards for PHP. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2528 |
* @since 5.1.0 Code cleanup for WPCS 1.0.0 coding standards. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2529 |
* @since 5.3.0 Saved progress of intermediate image creation after upload. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2530 |
* @since 5.5.0 Ensured that galleries can be output as a list of links in feeds. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2531 |
* @since 5.6.0 Replaced order-style PHP type conversion functions with typecasts. Fix logic for |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2532 |
* an array of image dimensions. |
| 0 | 2533 |
* |
| 5 | 2534 |
* @param array $attr {
|
2535 |
* Attributes of the gallery shortcode. |
|
2536 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2537 |
* @type string $order Order of the images in the gallery. Default 'ASC'. Accepts 'ASC', 'DESC'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2538 |
* @type string $orderby The field to use when ordering the images. Default 'menu_order ID'. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2539 |
* Accepts any valid SQL ORDERBY statement. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2540 |
* @type int $id Post ID. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2541 |
* @type string $itemtag HTML tag to use for each image in the gallery. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2542 |
* Default 'dl', or 'figure' when the theme registers HTML5 gallery support. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2543 |
* @type string $icontag HTML tag to use for each image's icon. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2544 |
* Default 'dt', or 'div' when the theme registers HTML5 gallery support. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2545 |
* @type string $captiontag HTML tag to use for each image's caption. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2546 |
* Default 'dd', or 'figcaption' when the theme registers HTML5 gallery support. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2547 |
* @type int $columns Number of columns of images to display. Default 3. |
| 18 | 2548 |
* @type string|int[] $size Size of the images to display. Accepts any registered image size name, or an array |
2549 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2550 |
* @type string $ids A comma-separated list of IDs of attachments to display. Default empty. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2551 |
* @type string $include A comma-separated list of IDs of attachments to include. Default empty. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2552 |
* @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2553 |
* @type string $link What to link each image to. Default empty (links to the attachment page). |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2554 |
* Accepts 'file', 'none'. |
| 5 | 2555 |
* } |
| 0 | 2556 |
* @return string HTML content to display gallery. |
2557 |
*/ |
|
| 5 | 2558 |
function gallery_shortcode( $attr ) {
|
| 0 | 2559 |
$post = get_post(); |
2560 |
||
2561 |
static $instance = 0; |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2562 |
++$instance; |
| 0 | 2563 |
|
2564 |
if ( ! empty( $attr['ids'] ) ) {
|
|
2565 |
// 'ids' is explicitly ordered, unless you specify otherwise. |
|
| 5 | 2566 |
if ( empty( $attr['orderby'] ) ) {
|
| 0 | 2567 |
$attr['orderby'] = 'post__in'; |
| 5 | 2568 |
} |
| 0 | 2569 |
$attr['include'] = $attr['ids']; |
2570 |
} |
|
2571 |
||
| 5 | 2572 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2573 |
* Filters the default gallery shortcode output. |
| 5 | 2574 |
* |
2575 |
* If the filtered output isn't empty, it will be used instead of generating |
|
2576 |
* the default gallery template. |
|
2577 |
* |
|
2578 |
* @since 2.5.0 |
|
2579 |
* @since 4.2.0 The `$instance` parameter was added. |
|
2580 |
* |
|
2581 |
* @see gallery_shortcode() |
|
2582 |
* |
|
2583 |
* @param string $output The gallery output. Default empty. |
|
2584 |
* @param array $attr Attributes of the gallery shortcode. |
|
2585 |
* @param int $instance Unique numeric ID of this gallery shortcode instance. |
|
2586 |
*/ |
|
2587 |
$output = apply_filters( 'post_gallery', '', $attr, $instance ); |
|
| 16 | 2588 |
|
2589 |
if ( ! empty( $output ) ) {
|
|
| 0 | 2590 |
return $output; |
2591 |
} |
|
2592 |
||
| 5 | 2593 |
$html5 = current_theme_supports( 'html5', 'gallery' ); |
| 9 | 2594 |
$atts = shortcode_atts( |
2595 |
array( |
|
2596 |
'order' => 'ASC', |
|
2597 |
'orderby' => 'menu_order ID', |
|
2598 |
'id' => $post ? $post->ID : 0, |
|
2599 |
'itemtag' => $html5 ? 'figure' : 'dl', |
|
2600 |
'icontag' => $html5 ? 'div' : 'dt', |
|
2601 |
'captiontag' => $html5 ? 'figcaption' : 'dd', |
|
2602 |
'columns' => 3, |
|
2603 |
'size' => 'thumbnail', |
|
2604 |
'include' => '', |
|
2605 |
'exclude' => '', |
|
2606 |
'link' => '', |
|
2607 |
), |
|
2608 |
$attr, |
|
2609 |
'gallery' |
|
2610 |
); |
|
| 5 | 2611 |
|
| 18 | 2612 |
$id = (int) $atts['id']; |
| 5 | 2613 |
|
2614 |
if ( ! empty( $atts['include'] ) ) {
|
|
| 9 | 2615 |
$_attachments = get_posts( |
2616 |
array( |
|
2617 |
'include' => $atts['include'], |
|
2618 |
'post_status' => 'inherit', |
|
2619 |
'post_type' => 'attachment', |
|
2620 |
'post_mime_type' => 'image', |
|
2621 |
'order' => $atts['order'], |
|
2622 |
'orderby' => $atts['orderby'], |
|
2623 |
) |
|
2624 |
); |
|
| 0 | 2625 |
|
2626 |
$attachments = array(); |
|
2627 |
foreach ( $_attachments as $key => $val ) {
|
|
| 9 | 2628 |
$attachments[ $val->ID ] = $_attachments[ $key ]; |
| 0 | 2629 |
} |
| 5 | 2630 |
} elseif ( ! empty( $atts['exclude'] ) ) {
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2631 |
$post_parent_id = $id; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2632 |
$attachments = get_children( |
| 9 | 2633 |
array( |
2634 |
'post_parent' => $id, |
|
2635 |
'exclude' => $atts['exclude'], |
|
2636 |
'post_status' => 'inherit', |
|
2637 |
'post_type' => 'attachment', |
|
2638 |
'post_mime_type' => 'image', |
|
2639 |
'order' => $atts['order'], |
|
2640 |
'orderby' => $atts['orderby'], |
|
2641 |
) |
|
2642 |
); |
|
| 0 | 2643 |
} else {
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2644 |
$post_parent_id = $id; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2645 |
$attachments = get_children( |
| 9 | 2646 |
array( |
2647 |
'post_parent' => $id, |
|
2648 |
'post_status' => 'inherit', |
|
2649 |
'post_type' => 'attachment', |
|
2650 |
'post_mime_type' => 'image', |
|
2651 |
'order' => $atts['order'], |
|
2652 |
'orderby' => $atts['orderby'], |
|
2653 |
) |
|
2654 |
); |
|
| 0 | 2655 |
} |
2656 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2657 |
if ( ! empty( $post_parent_id ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2658 |
$post_parent = get_post( $post_parent_id ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2659 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2660 |
// Terminate the shortcode execution if the user cannot read the post or it is password-protected. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2661 |
if ( ! is_post_publicly_viewable( $post_parent->ID ) && ! current_user_can( 'read_post', $post_parent->ID ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2662 |
|| post_password_required( $post_parent ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2663 |
) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2664 |
return ''; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2665 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2666 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2667 |
|
| 5 | 2668 |
if ( empty( $attachments ) ) {
|
| 0 | 2669 |
return ''; |
| 5 | 2670 |
} |
| 0 | 2671 |
|
2672 |
if ( is_feed() ) {
|
|
2673 |
$output = "\n"; |
|
| 5 | 2674 |
foreach ( $attachments as $att_id => $attachment ) {
|
| 16 | 2675 |
if ( ! empty( $atts['link'] ) ) {
|
2676 |
if ( 'none' === $atts['link'] ) {
|
|
2677 |
$output .= wp_get_attachment_image( $att_id, $atts['size'], false, $attr ); |
|
2678 |
} else {
|
|
2679 |
$output .= wp_get_attachment_link( $att_id, $atts['size'], false ); |
|
2680 |
} |
|
2681 |
} else {
|
|
2682 |
$output .= wp_get_attachment_link( $att_id, $atts['size'], true ); |
|
2683 |
} |
|
2684 |
$output .= "\n"; |
|
| 5 | 2685 |
} |
| 0 | 2686 |
return $output; |
2687 |
} |
|
2688 |
||
| 9 | 2689 |
$itemtag = tag_escape( $atts['itemtag'] ); |
| 5 | 2690 |
$captiontag = tag_escape( $atts['captiontag'] ); |
| 9 | 2691 |
$icontag = tag_escape( $atts['icontag'] ); |
| 0 | 2692 |
$valid_tags = wp_kses_allowed_html( 'post' ); |
| 5 | 2693 |
if ( ! isset( $valid_tags[ $itemtag ] ) ) {
|
| 0 | 2694 |
$itemtag = 'dl'; |
| 5 | 2695 |
} |
2696 |
if ( ! isset( $valid_tags[ $captiontag ] ) ) {
|
|
| 0 | 2697 |
$captiontag = 'dd'; |
| 5 | 2698 |
} |
2699 |
if ( ! isset( $valid_tags[ $icontag ] ) ) {
|
|
| 0 | 2700 |
$icontag = 'dt'; |
| 5 | 2701 |
} |
2702 |
||
| 18 | 2703 |
$columns = (int) $atts['columns']; |
| 9 | 2704 |
$itemwidth = $columns > 0 ? floor( 100 / $columns ) : 100; |
2705 |
$float = is_rtl() ? 'right' : 'left'; |
|
| 0 | 2706 |
|
2707 |
$selector = "gallery-{$instance}";
|
|
2708 |
||
| 5 | 2709 |
$gallery_style = ''; |
2710 |
||
2711 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2712 |
* Filters whether to print default gallery styles. |
| 5 | 2713 |
* |
2714 |
* @since 3.1.0 |
|
2715 |
* |
|
2716 |
* @param bool $print Whether to print default gallery styles. |
|
2717 |
* Defaults to false if the theme supports HTML5 galleries. |
|
2718 |
* Otherwise, defaults to true. |
|
2719 |
*/ |
|
2720 |
if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
|
|
| 16 | 2721 |
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; |
2722 |
||
| 0 | 2723 |
$gallery_style = " |
| 16 | 2724 |
<style{$type_attr}>
|
| 0 | 2725 |
#{$selector} {
|
2726 |
margin: auto; |
|
2727 |
} |
|
2728 |
#{$selector} .gallery-item {
|
|
2729 |
float: {$float};
|
|
2730 |
margin-top: 10px; |
|
2731 |
text-align: center; |
|
2732 |
width: {$itemwidth}%;
|
|
2733 |
} |
|
2734 |
#{$selector} img {
|
|
2735 |
border: 2px solid #cfcfcf; |
|
2736 |
} |
|
2737 |
#{$selector} .gallery-caption {
|
|
2738 |
margin-left: 0; |
|
2739 |
} |
|
2740 |
/* see gallery_shortcode() in wp-includes/media.php */ |
|
| 5 | 2741 |
</style>\n\t\t"; |
2742 |
} |
|
2743 |
||
| 18 | 2744 |
$size_class = sanitize_html_class( is_array( $atts['size'] ) ? implode( 'x', $atts['size'] ) : $atts['size'] ); |
| 0 | 2745 |
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
|
| 5 | 2746 |
|
2747 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2748 |
* Filters the default gallery shortcode CSS styles. |
| 5 | 2749 |
* |
2750 |
* @since 2.5.0 |
|
2751 |
* |
|
2752 |
* @param string $gallery_style Default CSS styles and opening HTML div container |
|
2753 |
* for the gallery shortcode output. |
|
2754 |
*/ |
|
2755 |
$output = apply_filters( 'gallery_style', $gallery_style . $gallery_div ); |
|
| 0 | 2756 |
|
2757 |
$i = 0; |
|
| 16 | 2758 |
|
| 0 | 2759 |
foreach ( $attachments as $id => $attachment ) {
|
| 5 | 2760 |
|
2761 |
$attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : ''; |
|
| 16 | 2762 |
|
| 5 | 2763 |
if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) {
|
2764 |
$image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); |
|
2765 |
} elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) {
|
|
2766 |
$image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr ); |
|
2767 |
} else {
|
|
2768 |
$image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); |
|
2769 |
} |
|
| 16 | 2770 |
|
| 9 | 2771 |
$image_meta = wp_get_attachment_metadata( $id ); |
| 0 | 2772 |
|
2773 |
$orientation = ''; |
|
| 16 | 2774 |
|
| 5 | 2775 |
if ( isset( $image_meta['height'], $image_meta['width'] ) ) {
|
| 0 | 2776 |
$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; |
| 5 | 2777 |
} |
| 16 | 2778 |
|
| 0 | 2779 |
$output .= "<{$itemtag} class='gallery-item'>";
|
2780 |
$output .= " |
|
2781 |
<{$icontag} class='gallery-icon {$orientation}'>
|
|
2782 |
$image_output |
|
2783 |
</{$icontag}>";
|
|
| 16 | 2784 |
|
| 9 | 2785 |
if ( $captiontag && trim( $attachment->post_excerpt ) ) {
|
| 0 | 2786 |
$output .= " |
| 5 | 2787 |
<{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'>
|
| 9 | 2788 |
" . wptexturize( $attachment->post_excerpt ) . " |
| 0 | 2789 |
</{$captiontag}>";
|
2790 |
} |
|
| 16 | 2791 |
|
| 0 | 2792 |
$output .= "</{$itemtag}>";
|
| 16 | 2793 |
|
2794 |
if ( ! $html5 && $columns > 0 && 0 === ++$i % $columns ) {
|
|
| 0 | 2795 |
$output .= '<br style="clear: both" />'; |
| 5 | 2796 |
} |
2797 |
} |
|
2798 |
||
| 16 | 2799 |
if ( ! $html5 && $columns > 0 && 0 !== $i % $columns ) {
|
| 5 | 2800 |
$output .= " |
2801 |
<br style='clear: both' />"; |
|
| 0 | 2802 |
} |
2803 |
||
2804 |
$output .= " |
|
2805 |
</div>\n"; |
|
2806 |
||
2807 |
return $output; |
|
2808 |
} |
|
2809 |
||
2810 |
/** |
|
| 5 | 2811 |
* Outputs the templates used by playlists. |
2812 |
* |
|
2813 |
* @since 3.9.0 |
|
2814 |
*/ |
|
2815 |
function wp_underscore_playlist_templates() {
|
|
| 9 | 2816 |
?> |
| 5 | 2817 |
<script type="text/html" id="tmpl-wp-playlist-current-item"> |
| 18 | 2818 |
<# if ( data.thumb && data.thumb.src ) { #>
|
2819 |
<img src="{{ data.thumb.src }}" alt="" />
|
|
| 5 | 2820 |
<# } #> |
2821 |
<div class="wp-playlist-caption"> |
|
| 9 | 2822 |
<span class="wp-playlist-item-meta wp-playlist-item-title"> |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2823 |
<# if ( data.meta.album || data.meta.artist ) { #>
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2824 |
<?php |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2825 |
/* translators: %s: Playlist item title. */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2826 |
printf( _x( '“%s”', 'playlist item title' ), '{{ data.title }}' );
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2827 |
?> |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2828 |
<# } else { #>
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2829 |
{{ data.title }}
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2830 |
<# } #> |
| 9 | 2831 |
</span> |
| 5 | 2832 |
<# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #>
|
2833 |
<# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #>
|
|
2834 |
</div> |
|
2835 |
</script> |
|
2836 |
<script type="text/html" id="tmpl-wp-playlist-item"> |
|
2837 |
<div class="wp-playlist-item"> |
|
2838 |
<a class="wp-playlist-caption" href="{{ data.src }}">
|
|
2839 |
{{ data.index ? ( data.index + '. ' ) : '' }}
|
|
2840 |
<# if ( data.caption ) { #>
|
|
2841 |
{{ data.caption }}
|
|
2842 |
<# } else { #>
|
|
2843 |
<# if ( data.artists && data.meta.artist ) { #>
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2844 |
<span class="wp-playlist-item-title"> |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2845 |
<?php |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2846 |
/* translators: %s: Playlist item title. */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2847 |
printf( _x( '“%s”', 'playlist item title' ), '{{{ data.title }}}' );
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2848 |
?> |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2849 |
</span> |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2850 |
<span class="wp-playlist-item-artist"> — {{ data.meta.artist }}</span>
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2851 |
<# } else { #>
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2852 |
<span class="wp-playlist-item-title">{{{ data.title }}}</span>
|
| 5 | 2853 |
<# } #> |
2854 |
<# } #> |
|
2855 |
</a> |
|
2856 |
<# if ( data.meta.length_formatted ) { #>
|
|
2857 |
<div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div>
|
|
2858 |
<# } #> |
|
2859 |
</div> |
|
2860 |
</script> |
|
| 9 | 2861 |
<?php |
| 5 | 2862 |
} |
2863 |
||
2864 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2865 |
* Outputs and enqueues default scripts and styles for playlists. |
| 5 | 2866 |
* |
2867 |
* @since 3.9.0 |
|
2868 |
* |
|
2869 |
* @param string $type Type of playlist. Accepts 'audio' or 'video'. |
|
2870 |
*/ |
|
2871 |
function wp_playlist_scripts( $type ) {
|
|
2872 |
wp_enqueue_style( 'wp-mediaelement' ); |
|
2873 |
wp_enqueue_script( 'wp-playlist' ); |
|
| 9 | 2874 |
?> |
2875 |
<!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ); ?>');</script><![endif]-->
|
|
2876 |
<?php |
|
| 5 | 2877 |
add_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 ); |
2878 |
add_action( 'admin_footer', 'wp_underscore_playlist_templates', 0 ); |
|
2879 |
} |
|
2880 |
||
2881 |
/** |
|
2882 |
* Builds the Playlist shortcode output. |
|
2883 |
* |
|
2884 |
* This implements the functionality of the playlist shortcode for displaying |
|
2885 |
* a collection of WordPress audio or video files in a post. |
|
2886 |
* |
|
2887 |
* @since 3.9.0 |
|
2888 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2889 |
* @global int $content_width |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2890 |
* |
| 5 | 2891 |
* @param array $attr {
|
2892 |
* Array of default playlist attributes. |
|
2893 |
* |
|
2894 |
* @type string $type Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'. |
|
2895 |
* @type string $order Designates ascending or descending order of items in the playlist. |
|
2896 |
* Accepts 'ASC', 'DESC'. Default 'ASC'. |
|
2897 |
* @type string $orderby Any column, or columns, to sort the playlist. If $ids are |
|
2898 |
* passed, this defaults to the order of the $ids array ('post__in').
|
|
2899 |
* Otherwise default is 'menu_order ID'. |
|
2900 |
* @type int $id If an explicit $ids array is not present, this parameter |
|
2901 |
* will determine which attachments are used for the playlist. |
|
2902 |
* Default is the current post ID. |
|
2903 |
* @type array $ids Create a playlist out of these explicit attachment IDs. If empty, |
|
2904 |
* a playlist will be created from all $type attachments of $id. |
|
2905 |
* Default empty. |
|
2906 |
* @type array $exclude List of specific attachment IDs to exclude from the playlist. Default empty. |
|
2907 |
* @type string $style Playlist style to use. Accepts 'light' or 'dark'. Default 'light'. |
|
2908 |
* @type bool $tracklist Whether to show or hide the playlist. Default true. |
|
2909 |
* @type bool $tracknumbers Whether to show or hide the numbers next to entries in the playlist. Default true. |
|
2910 |
* @type bool $images Show or hide the video or audio thumbnail (Featured Image/post |
|
2911 |
* thumbnail). Default true. |
|
2912 |
* @type bool $artists Whether to show or hide artist name in the playlist. Default true. |
|
2913 |
* } |
|
2914 |
* |
|
2915 |
* @return string Playlist output. Empty string if the passed type is unsupported. |
|
2916 |
*/ |
|
2917 |
function wp_playlist_shortcode( $attr ) {
|
|
2918 |
global $content_width; |
|
2919 |
$post = get_post(); |
|
2920 |
||
2921 |
static $instance = 0; |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2922 |
++$instance; |
| 5 | 2923 |
|
2924 |
if ( ! empty( $attr['ids'] ) ) {
|
|
2925 |
// 'ids' is explicitly ordered, unless you specify otherwise. |
|
2926 |
if ( empty( $attr['orderby'] ) ) {
|
|
2927 |
$attr['orderby'] = 'post__in'; |
|
2928 |
} |
|
2929 |
$attr['include'] = $attr['ids']; |
|
2930 |
} |
|
2931 |
||
2932 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2933 |
* Filters the playlist output. |
| 5 | 2934 |
* |
| 16 | 2935 |
* Returning a non-empty value from the filter will short-circuit generation |
| 5 | 2936 |
* of the default playlist output, returning the passed value instead. |
2937 |
* |
|
2938 |
* @since 3.9.0 |
|
2939 |
* @since 4.2.0 The `$instance` parameter was added. |
|
2940 |
* |
|
2941 |
* @param string $output Playlist output. Default empty. |
|
2942 |
* @param array $attr An array of shortcode attributes. |
|
2943 |
* @param int $instance Unique numeric ID of this playlist shortcode instance. |
|
2944 |
*/ |
|
2945 |
$output = apply_filters( 'post_playlist', '', $attr, $instance ); |
|
| 16 | 2946 |
|
2947 |
if ( ! empty( $output ) ) {
|
|
| 5 | 2948 |
return $output; |
2949 |
} |
|
2950 |
||
| 9 | 2951 |
$atts = shortcode_atts( |
2952 |
array( |
|
2953 |
'type' => 'audio', |
|
2954 |
'order' => 'ASC', |
|
2955 |
'orderby' => 'menu_order ID', |
|
2956 |
'id' => $post ? $post->ID : 0, |
|
2957 |
'include' => '', |
|
2958 |
'exclude' => '', |
|
2959 |
'style' => 'light', |
|
2960 |
'tracklist' => true, |
|
2961 |
'tracknumbers' => true, |
|
2962 |
'images' => true, |
|
2963 |
'artists' => true, |
|
2964 |
), |
|
2965 |
$attr, |
|
2966 |
'playlist' |
|
2967 |
); |
|
| 5 | 2968 |
|
| 18 | 2969 |
$id = (int) $atts['id']; |
| 5 | 2970 |
|
| 16 | 2971 |
if ( 'audio' !== $atts['type'] ) {
|
| 5 | 2972 |
$atts['type'] = 'video'; |
2973 |
} |
|
2974 |
||
2975 |
$args = array( |
|
| 9 | 2976 |
'post_status' => 'inherit', |
2977 |
'post_type' => 'attachment', |
|
| 5 | 2978 |
'post_mime_type' => $atts['type'], |
| 9 | 2979 |
'order' => $atts['order'], |
2980 |
'orderby' => $atts['orderby'], |
|
| 5 | 2981 |
); |
2982 |
||
2983 |
if ( ! empty( $atts['include'] ) ) {
|
|
2984 |
$args['include'] = $atts['include']; |
|
| 9 | 2985 |
$_attachments = get_posts( $args ); |
| 5 | 2986 |
|
2987 |
$attachments = array(); |
|
2988 |
foreach ( $_attachments as $key => $val ) {
|
|
| 9 | 2989 |
$attachments[ $val->ID ] = $_attachments[ $key ]; |
| 5 | 2990 |
} |
2991 |
} elseif ( ! empty( $atts['exclude'] ) ) {
|
|
2992 |
$args['post_parent'] = $id; |
|
| 9 | 2993 |
$args['exclude'] = $atts['exclude']; |
2994 |
$attachments = get_children( $args ); |
|
| 5 | 2995 |
} else {
|
2996 |
$args['post_parent'] = $id; |
|
| 9 | 2997 |
$attachments = get_children( $args ); |
| 5 | 2998 |
} |
2999 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3000 |
if ( ! empty( $args['post_parent'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3001 |
$post_parent = get_post( $id ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3002 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3003 |
// Terminate the shortcode execution if the user cannot read the post or it is password-protected. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3004 |
if ( ! current_user_can( 'read_post', $post_parent->ID ) || post_password_required( $post_parent ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3005 |
return ''; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3006 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3007 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3008 |
|
| 5 | 3009 |
if ( empty( $attachments ) ) {
|
3010 |
return ''; |
|
3011 |
} |
|
3012 |
||
3013 |
if ( is_feed() ) {
|
|
3014 |
$output = "\n"; |
|
3015 |
foreach ( $attachments as $att_id => $attachment ) {
|
|
3016 |
$output .= wp_get_attachment_link( $att_id ) . "\n"; |
|
3017 |
} |
|
3018 |
return $output; |
|
3019 |
} |
|
3020 |
||
| 16 | 3021 |
$outer = 22; // Default padding and border of wrapper. |
| 5 | 3022 |
|
| 9 | 3023 |
$default_width = 640; |
| 5 | 3024 |
$default_height = 360; |
3025 |
||
| 9 | 3026 |
$theme_width = empty( $content_width ) ? $default_width : ( $content_width - $outer ); |
| 5 | 3027 |
$theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width ); |
3028 |
||
3029 |
$data = array( |
|
| 9 | 3030 |
'type' => $atts['type'], |
| 16 | 3031 |
// Don't pass strings to JSON, will be truthy in JS. |
| 9 | 3032 |
'tracklist' => wp_validate_boolean( $atts['tracklist'] ), |
| 5 | 3033 |
'tracknumbers' => wp_validate_boolean( $atts['tracknumbers'] ), |
| 9 | 3034 |
'images' => wp_validate_boolean( $atts['images'] ), |
3035 |
'artists' => wp_validate_boolean( $atts['artists'] ), |
|
| 5 | 3036 |
); |
3037 |
||
3038 |
$tracks = array(); |
|
3039 |
foreach ( $attachments as $attachment ) {
|
|
| 9 | 3040 |
$url = wp_get_attachment_url( $attachment->ID ); |
| 5 | 3041 |
$ftype = wp_check_filetype( $url, wp_get_mime_types() ); |
3042 |
$track = array( |
|
| 9 | 3043 |
'src' => $url, |
3044 |
'type' => $ftype['type'], |
|
3045 |
'title' => $attachment->post_title, |
|
3046 |
'caption' => $attachment->post_excerpt, |
|
3047 |
'description' => $attachment->post_content, |
|
| 5 | 3048 |
); |
3049 |
||
3050 |
$track['meta'] = array(); |
|
| 9 | 3051 |
$meta = wp_get_attachment_metadata( $attachment->ID ); |
| 5 | 3052 |
if ( ! empty( $meta ) ) {
|
3053 |
||
3054 |
foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
|
|
3055 |
if ( ! empty( $meta[ $key ] ) ) {
|
|
3056 |
$track['meta'][ $key ] = $meta[ $key ]; |
|
3057 |
} |
|
3058 |
} |
|
3059 |
||
3060 |
if ( 'video' === $atts['type'] ) {
|
|
3061 |
if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
|
|
| 9 | 3062 |
$width = $meta['width']; |
3063 |
$height = $meta['height']; |
|
| 5 | 3064 |
$theme_height = round( ( $height * $theme_width ) / $width ); |
3065 |
} else {
|
|
| 9 | 3066 |
$width = $default_width; |
| 5 | 3067 |
$height = $default_height; |
3068 |
} |
|
3069 |
||
3070 |
$track['dimensions'] = array( |
|
3071 |
'original' => compact( 'width', 'height' ), |
|
| 9 | 3072 |
'resized' => array( |
3073 |
'width' => $theme_width, |
|
3074 |
'height' => $theme_height, |
|
3075 |
), |
|
| 5 | 3076 |
); |
3077 |
} |
|
3078 |
} |
|
3079 |
||
3080 |
if ( $atts['images'] ) {
|
|
3081 |
$thumb_id = get_post_thumbnail_id( $attachment->ID ); |
|
3082 |
if ( ! empty( $thumb_id ) ) {
|
|
3083 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'full' ); |
|
| 9 | 3084 |
$track['image'] = compact( 'src', 'width', 'height' ); |
| 5 | 3085 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); |
| 9 | 3086 |
$track['thumb'] = compact( 'src', 'width', 'height' ); |
| 5 | 3087 |
} else {
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3088 |
$src = wp_mime_type_icon( $attachment->ID, '.svg' ); |
| 9 | 3089 |
$width = 48; |
3090 |
$height = 64; |
|
| 5 | 3091 |
$track['image'] = compact( 'src', 'width', 'height' ); |
3092 |
$track['thumb'] = compact( 'src', 'width', 'height' ); |
|
3093 |
} |
|
3094 |
} |
|
3095 |
||
3096 |
$tracks[] = $track; |
|
3097 |
} |
|
3098 |
$data['tracks'] = $tracks; |
|
3099 |
||
| 9 | 3100 |
$safe_type = esc_attr( $atts['type'] ); |
| 5 | 3101 |
$safe_style = esc_attr( $atts['style'] ); |
3102 |
||
3103 |
ob_start(); |
|
3104 |
||
3105 |
if ( 1 === $instance ) {
|
|
3106 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3107 |
* Prints and enqueues playlist scripts, styles, and JavaScript templates. |
| 5 | 3108 |
* |
3109 |
* @since 3.9.0 |
|
3110 |
* |
|
3111 |
* @param string $type Type of playlist. Possible values are 'audio' or 'video'. |
|
3112 |
* @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'. |
|
3113 |
*/ |
|
3114 |
do_action( 'wp_playlist_scripts', $atts['type'], $atts['style'] ); |
|
| 9 | 3115 |
} |
3116 |
?> |
|
3117 |
<div class="wp-playlist wp-<?php echo $safe_type; ?>-playlist wp-playlist-<?php echo $safe_style; ?>"> |
|
3118 |
<?php if ( 'audio' === $atts['type'] ) : ?> |
|
| 18 | 3119 |
<div class="wp-playlist-current-item"></div> |
3120 |
<?php endif; ?> |
|
3121 |
<<?php echo $safe_type; ?> controls="controls" preload="none" width="<?php echo (int) $theme_width; ?>" |
|
3122 |
<?php |
|
3123 |
if ( 'video' === $safe_type ) {
|
|
3124 |
echo ' height="', (int) $theme_height, '"'; |
|
3125 |
} |
|
3126 |
?> |
|
| 9 | 3127 |
></<?php echo $safe_type; ?>> |
| 5 | 3128 |
<div class="wp-playlist-next"></div> |
3129 |
<div class="wp-playlist-prev"></div> |
|
3130 |
<noscript> |
|
| 9 | 3131 |
<ol> |
| 18 | 3132 |
<?php |
3133 |
foreach ( $attachments as $att_id => $attachment ) {
|
|
3134 |
printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) ); |
|
3135 |
} |
|
3136 |
?> |
|
| 9 | 3137 |
</ol> |
| 5 | 3138 |
</noscript> |
| 9 | 3139 |
<script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ); ?></script> |
| 5 | 3140 |
</div> |
3141 |
<?php |
|
3142 |
return ob_get_clean(); |
|
3143 |
} |
|
3144 |
add_shortcode( 'playlist', 'wp_playlist_shortcode' ); |
|
3145 |
||
3146 |
/** |
|
3147 |
* Provides a No-JS Flash fallback as a last resort for audio / video. |
|
| 0 | 3148 |
* |
3149 |
* @since 3.6.0 |
|
3150 |
* |
|
| 5 | 3151 |
* @param string $url The media element URL. |
3152 |
* @return string Fallback HTML. |
|
| 0 | 3153 |
*/ |
3154 |
function wp_mediaelement_fallback( $url ) {
|
|
| 5 | 3155 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3156 |
* Filters the MediaElement fallback output for no-JS. |
| 5 | 3157 |
* |
3158 |
* @since 3.6.0 |
|
3159 |
* |
|
3160 |
* @param string $output Fallback output for no-JS. |
|
3161 |
* @param string $url Media file URL. |
|
3162 |
*/ |
|
| 0 | 3163 |
return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url ); |
3164 |
} |
|
3165 |
||
3166 |
/** |
|
| 16 | 3167 |
* Returns a filtered list of supported audio formats. |
| 0 | 3168 |
* |
3169 |
* @since 3.6.0 |
|
| 5 | 3170 |
* |
| 16 | 3171 |
* @return string[] Supported audio formats. |
| 0 | 3172 |
*/ |
3173 |
function wp_get_audio_extensions() {
|
|
| 5 | 3174 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3175 |
* Filters the list of supported audio formats. |
| 5 | 3176 |
* |
3177 |
* @since 3.6.0 |
|
3178 |
* |
|
| 16 | 3179 |
* @param string[] $extensions An array of supported audio formats. Defaults are |
3180 |
* 'mp3', 'ogg', 'flac', 'm4a', 'wav'. |
|
| 5 | 3181 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3182 |
return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'flac', 'm4a', 'wav' ) ); |
| 0 | 3183 |
} |
3184 |
||
3185 |
/** |
|
| 5 | 3186 |
* Returns useful keys to use to lookup data from an attachment's stored metadata. |
3187 |
* |
|
3188 |
* @since 3.9.0 |
|
3189 |
* |
|
3190 |
* @param WP_Post $attachment The current attachment, provided for context. |
|
3191 |
* @param string $context Optional. The context. Accepts 'edit', 'display'. Default 'display'. |
|
| 16 | 3192 |
* @return string[] Key/value pairs of field keys to labels. |
| 5 | 3193 |
*/ |
3194 |
function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) {
|
|
3195 |
$fields = array( |
|
3196 |
'artist' => __( 'Artist' ), |
|
| 9 | 3197 |
'album' => __( 'Album' ), |
| 5 | 3198 |
); |
3199 |
||
3200 |
if ( 'display' === $context ) {
|
|
3201 |
$fields['genre'] = __( 'Genre' ); |
|
3202 |
$fields['year'] = __( 'Year' ); |
|
3203 |
$fields['length_formatted'] = _x( 'Length', 'video or audio' ); |
|
3204 |
} elseif ( 'js' === $context ) {
|
|
| 9 | 3205 |
$fields['bitrate'] = __( 'Bitrate' ); |
3206 |
$fields['bitrate_mode'] = __( 'Bitrate Mode' ); |
|
| 5 | 3207 |
} |
3208 |
||
3209 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3210 |
* Filters the editable list of keys to look up data from an attachment's metadata. |
| 5 | 3211 |
* |
3212 |
* @since 3.9.0 |
|
3213 |
* |
|
3214 |
* @param array $fields Key/value pairs of field keys to labels. |
|
3215 |
* @param WP_Post $attachment Attachment object. |
|
3216 |
* @param string $context The context. Accepts 'edit', 'display'. Default 'display'. |
|
3217 |
*/ |
|
3218 |
return apply_filters( 'wp_get_attachment_id3_keys', $fields, $attachment, $context ); |
|
3219 |
} |
|
3220 |
/** |
|
3221 |
* Builds the Audio shortcode output. |
|
| 0 | 3222 |
* |
3223 |
* This implements the functionality of the Audio Shortcode for displaying |
|
3224 |
* WordPress mp3s in a post. |
|
3225 |
* |
|
3226 |
* @since 3.6.0 |
|
3227 |
* |
|
| 5 | 3228 |
* @param array $attr {
|
3229 |
* Attributes of the audio shortcode. |
|
3230 |
* |
|
3231 |
* @type string $src URL to the source of the audio file. Default empty. |
|
3232 |
* @type string $loop The 'loop' attribute for the `<audio>` element. Default empty. |
|
3233 |
* @type string $autoplay The 'autoplay' attribute for the `<audio>` element. Default empty. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3234 |
* @type string $preload The 'preload' attribute for the `<audio>` element. Default 'none'. |
| 5 | 3235 |
* @type string $class The 'class' attribute for the `<audio>` element. Default 'wp-audio-shortcode'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3236 |
* @type string $style The 'style' attribute for the `<audio>` element. Default 'width: 100%;'. |
| 5 | 3237 |
* } |
3238 |
* @param string $content Shortcode content. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3239 |
* @return string|void HTML content to display audio. |
| 0 | 3240 |
*/ |
3241 |
function wp_audio_shortcode( $attr, $content = '' ) {
|
|
3242 |
$post_id = get_post() ? get_the_ID() : 0; |
|
3243 |
||
| 5 | 3244 |
static $instance = 0; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3245 |
++$instance; |
| 0 | 3246 |
|
3247 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3248 |
* Filters the default audio shortcode output. |
| 0 | 3249 |
* |
| 5 | 3250 |
* If the filtered output isn't empty, it will be used instead of generating the default audio template. |
3251 |
* |
|
3252 |
* @since 3.6.0 |
|
| 0 | 3253 |
* |
| 5 | 3254 |
* @param string $html Empty variable to be replaced with shortcode markup. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3255 |
* @param array $attr Attributes of the shortcode. See {@see wp_audio_shortcode()}.
|
| 5 | 3256 |
* @param string $content Shortcode content. |
3257 |
* @param int $instance Unique numeric ID of this audio shortcode instance. |
|
| 0 | 3258 |
*/ |
| 5 | 3259 |
$override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance ); |
| 16 | 3260 |
|
| 5 | 3261 |
if ( '' !== $override ) {
|
3262 |
return $override; |
|
3263 |
} |
|
| 0 | 3264 |
|
3265 |
$audio = null; |
|
3266 |
||
3267 |
$default_types = wp_get_audio_extensions(); |
|
3268 |
$defaults_atts = array( |
|
3269 |
'src' => '', |
|
3270 |
'loop' => '', |
|
3271 |
'autoplay' => '', |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3272 |
'preload' => 'none', |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3273 |
'class' => 'wp-audio-shortcode', |
| 9 | 3274 |
'style' => 'width: 100%;', |
| 0 | 3275 |
); |
| 5 | 3276 |
foreach ( $default_types as $type ) {
|
| 9 | 3277 |
$defaults_atts[ $type ] = ''; |
| 5 | 3278 |
} |
| 0 | 3279 |
|
3280 |
$atts = shortcode_atts( $defaults_atts, $attr, 'audio' ); |
|
3281 |
||
3282 |
$primary = false; |
|
| 5 | 3283 |
if ( ! empty( $atts['src'] ) ) {
|
3284 |
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); |
|
| 16 | 3285 |
|
3286 |
if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
|
|
| 5 | 3287 |
return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); |
3288 |
} |
|
| 16 | 3289 |
|
| 0 | 3290 |
$primary = true; |
3291 |
array_unshift( $default_types, 'src' ); |
|
3292 |
} else {
|
|
3293 |
foreach ( $default_types as $ext ) {
|
|
| 5 | 3294 |
if ( ! empty( $atts[ $ext ] ) ) {
|
3295 |
$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); |
|
| 16 | 3296 |
|
| 5 | 3297 |
if ( strtolower( $type['ext'] ) === $ext ) {
|
| 0 | 3298 |
$primary = true; |
| 5 | 3299 |
} |
| 0 | 3300 |
} |
3301 |
} |
|
3302 |
} |
|
3303 |
||
3304 |
if ( ! $primary ) {
|
|
3305 |
$audios = get_attached_media( 'audio', $post_id ); |
|
| 16 | 3306 |
|
| 5 | 3307 |
if ( empty( $audios ) ) {
|
| 0 | 3308 |
return; |
| 5 | 3309 |
} |
| 0 | 3310 |
|
| 9 | 3311 |
$audio = reset( $audios ); |
| 5 | 3312 |
$atts['src'] = wp_get_attachment_url( $audio->ID ); |
| 16 | 3313 |
|
| 5 | 3314 |
if ( empty( $atts['src'] ) ) {
|
| 0 | 3315 |
return; |
| 5 | 3316 |
} |
| 0 | 3317 |
|
3318 |
array_unshift( $default_types, 'src' ); |
|
3319 |
} |
|
3320 |
||
| 5 | 3321 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3322 |
* Filters the media library used for the audio shortcode. |
| 5 | 3323 |
* |
3324 |
* @since 3.6.0 |
|
3325 |
* |
|
3326 |
* @param string $library Media library used for the audio shortcode. |
|
3327 |
*/ |
|
| 0 | 3328 |
$library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); |
| 16 | 3329 |
|
| 0 | 3330 |
if ( 'mediaelement' === $library && did_action( 'init' ) ) {
|
3331 |
wp_enqueue_style( 'wp-mediaelement' ); |
|
3332 |
wp_enqueue_script( 'wp-mediaelement' ); |
|
3333 |
} |
|
3334 |
||
| 5 | 3335 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3336 |
* Filters the class attribute for the audio shortcode output container. |
| 5 | 3337 |
* |
3338 |
* @since 3.6.0 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3339 |
* @since 4.9.0 The `$atts` parameter was added. |
| 5 | 3340 |
* |
3341 |
* @param string $class CSS class or list of space-separated classes. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3342 |
* @param array $atts Array of audio shortcode attributes. |
| 5 | 3343 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3344 |
$atts['class'] = apply_filters( 'wp_audio_shortcode_class', $atts['class'], $atts ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3345 |
|
| 5 | 3346 |
$html_atts = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3347 |
'class' => $atts['class'], |
| 5 | 3348 |
'id' => sprintf( 'audio-%d-%d', $post_id, $instance ), |
3349 |
'loop' => wp_validate_boolean( $atts['loop'] ), |
|
3350 |
'autoplay' => wp_validate_boolean( $atts['autoplay'] ), |
|
3351 |
'preload' => $atts['preload'], |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3352 |
'style' => $atts['style'], |
| 0 | 3353 |
); |
3354 |
||
| 16 | 3355 |
// These ones should just be omitted altogether if they are blank. |
| 0 | 3356 |
foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
|
| 9 | 3357 |
if ( empty( $html_atts[ $a ] ) ) {
|
3358 |
unset( $html_atts[ $a ] ); |
|
| 5 | 3359 |
} |
| 0 | 3360 |
} |
3361 |
||
3362 |
$attr_strings = array(); |
|
| 16 | 3363 |
|
| 5 | 3364 |
foreach ( $html_atts as $k => $v ) {
|
| 0 | 3365 |
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; |
3366 |
} |
|
3367 |
||
3368 |
$html = ''; |
|
| 16 | 3369 |
|
| 5 | 3370 |
if ( 'mediaelement' === $library && 1 === $instance ) {
|
| 0 | 3371 |
$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
|
| 5 | 3372 |
} |
| 16 | 3373 |
|
| 18 | 3374 |
$html .= sprintf( '<audio %s controls="controls">', implode( ' ', $attr_strings ) ); |
| 0 | 3375 |
|
3376 |
$fileurl = ''; |
|
| 9 | 3377 |
$source = '<source type="%s" src="%s" />'; |
| 16 | 3378 |
|
| 0 | 3379 |
foreach ( $default_types as $fallback ) {
|
| 5 | 3380 |
if ( ! empty( $atts[ $fallback ] ) ) {
|
3381 |
if ( empty( $fileurl ) ) {
|
|
3382 |
$fileurl = $atts[ $fallback ]; |
|
3383 |
} |
|
| 16 | 3384 |
|
| 9 | 3385 |
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); |
3386 |
$url = add_query_arg( '_', $instance, $atts[ $fallback ] ); |
|
| 5 | 3387 |
$html .= sprintf( $source, $type['type'], esc_url( $url ) ); |
| 0 | 3388 |
} |
3389 |
} |
|
3390 |
||
| 5 | 3391 |
if ( 'mediaelement' === $library ) {
|
| 0 | 3392 |
$html .= wp_mediaelement_fallback( $fileurl ); |
| 5 | 3393 |
} |
| 16 | 3394 |
|
| 0 | 3395 |
$html .= '</audio>'; |
3396 |
||
| 5 | 3397 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3398 |
* Filters the audio shortcode output. |
| 5 | 3399 |
* |
3400 |
* @since 3.6.0 |
|
3401 |
* |
|
3402 |
* @param string $html Audio shortcode HTML output. |
|
3403 |
* @param array $atts Array of audio shortcode attributes. |
|
3404 |
* @param string $audio Audio file. |
|
3405 |
* @param int $post_id Post ID. |
|
3406 |
* @param string $library Media library used for the audio shortcode. |
|
3407 |
*/ |
|
| 0 | 3408 |
return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library ); |
3409 |
} |
|
3410 |
add_shortcode( 'audio', 'wp_audio_shortcode' ); |
|
3411 |
||
3412 |
/** |
|
| 16 | 3413 |
* Returns a filtered list of supported video formats. |
| 0 | 3414 |
* |
3415 |
* @since 3.6.0 |
|
| 5 | 3416 |
* |
| 16 | 3417 |
* @return string[] List of supported video formats. |
| 0 | 3418 |
*/ |
3419 |
function wp_get_video_extensions() {
|
|
| 5 | 3420 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3421 |
* Filters the list of supported video formats. |
| 5 | 3422 |
* |
3423 |
* @since 3.6.0 |
|
3424 |
* |
|
| 16 | 3425 |
* @param string[] $extensions An array of supported video formats. Defaults are |
3426 |
* 'mp4', 'm4v', 'webm', 'ogv', 'flv'. |
|
| 5 | 3427 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3428 |
return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'flv' ) ); |
| 0 | 3429 |
} |
3430 |
||
3431 |
/** |
|
| 5 | 3432 |
* Builds the Video shortcode output. |
| 0 | 3433 |
* |
3434 |
* This implements the functionality of the Video Shortcode for displaying |
|
3435 |
* WordPress mp4s in a post. |
|
3436 |
* |
|
3437 |
* @since 3.6.0 |
|
3438 |
* |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3439 |
* @global int $content_width |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3440 |
* |
| 5 | 3441 |
* @param array $attr {
|
3442 |
* Attributes of the shortcode. |
|
3443 |
* |
|
3444 |
* @type string $src URL to the source of the video file. Default empty. |
|
3445 |
* @type int $height Height of the video embed in pixels. Default 360. |
|
3446 |
* @type int $width Width of the video embed in pixels. Default $content_width or 640. |
|
3447 |
* @type string $poster The 'poster' attribute for the `<video>` element. Default empty. |
|
3448 |
* @type string $loop The 'loop' attribute for the `<video>` element. Default empty. |
|
3449 |
* @type string $autoplay The 'autoplay' attribute for the `<video>` element. Default empty. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3450 |
* @type string $muted The 'muted' attribute for the `<video>` element. Default false. |
| 5 | 3451 |
* @type string $preload The 'preload' attribute for the `<video>` element. |
3452 |
* Default 'metadata'. |
|
3453 |
* @type string $class The 'class' attribute for the `<video>` element. |
|
3454 |
* Default 'wp-video-shortcode'. |
|
3455 |
* } |
|
3456 |
* @param string $content Shortcode content. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3457 |
* @return string|void HTML content to display video. |
| 0 | 3458 |
*/ |
3459 |
function wp_video_shortcode( $attr, $content = '' ) {
|
|
3460 |
global $content_width; |
|
3461 |
$post_id = get_post() ? get_the_ID() : 0; |
|
3462 |
||
| 5 | 3463 |
static $instance = 0; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3464 |
++$instance; |
| 0 | 3465 |
|
3466 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3467 |
* Filters the default video shortcode output. |
| 0 | 3468 |
* |
| 5 | 3469 |
* If the filtered output isn't empty, it will be used instead of generating |
3470 |
* the default video template. |
|
3471 |
* |
|
3472 |
* @since 3.6.0 |
|
3473 |
* |
|
3474 |
* @see wp_video_shortcode() |
|
| 0 | 3475 |
* |
| 5 | 3476 |
* @param string $html Empty variable to be replaced with shortcode markup. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3477 |
* @param array $attr Attributes of the shortcode. See {@see wp_video_shortcode()}.
|
| 5 | 3478 |
* @param string $content Video shortcode content. |
3479 |
* @param int $instance Unique numeric ID of this video shortcode instance. |
|
| 0 | 3480 |
*/ |
| 5 | 3481 |
$override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance ); |
| 16 | 3482 |
|
| 5 | 3483 |
if ( '' !== $override ) {
|
3484 |
return $override; |
|
3485 |
} |
|
| 0 | 3486 |
|
3487 |
$video = null; |
|
3488 |
||
3489 |
$default_types = wp_get_video_extensions(); |
|
3490 |
$defaults_atts = array( |
|
3491 |
'src' => '', |
|
3492 |
'poster' => '', |
|
3493 |
'loop' => '', |
|
3494 |
'autoplay' => '', |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3495 |
'muted' => 'false', |
| 0 | 3496 |
'preload' => 'metadata', |
| 5 | 3497 |
'width' => 640, |
| 0 | 3498 |
'height' => 360, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3499 |
'class' => 'wp-video-shortcode', |
| 0 | 3500 |
); |
3501 |
||
| 5 | 3502 |
foreach ( $default_types as $type ) {
|
| 9 | 3503 |
$defaults_atts[ $type ] = ''; |
| 5 | 3504 |
} |
| 0 | 3505 |
|
3506 |
$atts = shortcode_atts( $defaults_atts, $attr, 'video' ); |
|
| 5 | 3507 |
|
3508 |
if ( is_admin() ) {
|
|
| 16 | 3509 |
// Shrink the video so it isn't huge in the admin. |
| 5 | 3510 |
if ( $atts['width'] > $defaults_atts['width'] ) {
|
3511 |
$atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] ); |
|
| 9 | 3512 |
$atts['width'] = $defaults_atts['width']; |
| 5 | 3513 |
} |
3514 |
} else {
|
|
| 16 | 3515 |
// If the video is bigger than the theme. |
| 5 | 3516 |
if ( ! empty( $content_width ) && $atts['width'] > $content_width ) {
|
3517 |
$atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] ); |
|
| 9 | 3518 |
$atts['width'] = $content_width; |
| 5 | 3519 |
} |
3520 |
} |
|
3521 |
||
| 16 | 3522 |
$is_vimeo = false; |
3523 |
$is_youtube = false; |
|
| 9 | 3524 |
$yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#'; |
| 5 | 3525 |
$vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#'; |
| 0 | 3526 |
|
3527 |
$primary = false; |
|
| 5 | 3528 |
if ( ! empty( $atts['src'] ) ) {
|
| 9 | 3529 |
$is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) ); |
3530 |
$is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) ); |
|
| 16 | 3531 |
|
| 5 | 3532 |
if ( ! $is_youtube && ! $is_vimeo ) {
|
3533 |
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); |
|
| 16 | 3534 |
|
3535 |
if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) {
|
|
| 5 | 3536 |
return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); |
3537 |
} |
|
3538 |
} |
|
3539 |
||
3540 |
if ( $is_vimeo ) {
|
|
| 9 | 3541 |
wp_enqueue_script( 'mediaelement-vimeo' ); |
| 5 | 3542 |
} |
3543 |
||
| 0 | 3544 |
$primary = true; |
3545 |
array_unshift( $default_types, 'src' ); |
|
3546 |
} else {
|
|
3547 |
foreach ( $default_types as $ext ) {
|
|
| 5 | 3548 |
if ( ! empty( $atts[ $ext ] ) ) {
|
3549 |
$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); |
|
3550 |
if ( strtolower( $type['ext'] ) === $ext ) {
|
|
| 0 | 3551 |
$primary = true; |
| 5 | 3552 |
} |
| 0 | 3553 |
} |
3554 |
} |
|
3555 |
} |
|
3556 |
||
3557 |
if ( ! $primary ) {
|
|
3558 |
$videos = get_attached_media( 'video', $post_id ); |
|
| 5 | 3559 |
if ( empty( $videos ) ) {
|
| 0 | 3560 |
return; |
| 5 | 3561 |
} |
| 0 | 3562 |
|
| 9 | 3563 |
$video = reset( $videos ); |
| 5 | 3564 |
$atts['src'] = wp_get_attachment_url( $video->ID ); |
3565 |
if ( empty( $atts['src'] ) ) {
|
|
| 0 | 3566 |
return; |
| 5 | 3567 |
} |
| 0 | 3568 |
|
3569 |
array_unshift( $default_types, 'src' ); |
|
3570 |
} |
|
3571 |
||
| 5 | 3572 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3573 |
* Filters the media library used for the video shortcode. |
| 5 | 3574 |
* |
3575 |
* @since 3.6.0 |
|
3576 |
* |
|
3577 |
* @param string $library Media library used for the video shortcode. |
|
3578 |
*/ |
|
| 0 | 3579 |
$library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); |
3580 |
if ( 'mediaelement' === $library && did_action( 'init' ) ) {
|
|
3581 |
wp_enqueue_style( 'wp-mediaelement' ); |
|
3582 |
wp_enqueue_script( 'wp-mediaelement' ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3583 |
wp_enqueue_script( 'mediaelement-vimeo' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3584 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3585 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3586 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3587 |
* MediaElement.js has issues with some URL formats for Vimeo and YouTube, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3588 |
* so update the URL to prevent the ME.js player from breaking. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3589 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3590 |
if ( 'mediaelement' === $library ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3591 |
if ( $is_youtube ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3592 |
// Remove `feature` query arg and force SSL - see #40866. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3593 |
$atts['src'] = remove_query_arg( 'feature', $atts['src'] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3594 |
$atts['src'] = set_url_scheme( $atts['src'], 'https' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3595 |
} elseif ( $is_vimeo ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3596 |
// Remove all query arguments and force SSL - see #40866. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3597 |
$parsed_vimeo_url = wp_parse_url( $atts['src'] ); |
| 9 | 3598 |
$vimeo_src = 'https://' . $parsed_vimeo_url['host'] . $parsed_vimeo_url['path']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3599 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3600 |
// Add loop param for mejs bug - see #40977, not needed after #39686. |
| 9 | 3601 |
$loop = $atts['loop'] ? '1' : '0'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3602 |
$atts['src'] = add_query_arg( 'loop', $loop, $vimeo_src ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3603 |
} |
| 0 | 3604 |
} |
3605 |
||
| 5 | 3606 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3607 |
* Filters the class attribute for the video shortcode output container. |
| 5 | 3608 |
* |
3609 |
* @since 3.6.0 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3610 |
* @since 4.9.0 The `$atts` parameter was added. |
| 5 | 3611 |
* |
3612 |
* @param string $class CSS class or list of space-separated classes. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3613 |
* @param array $atts Array of video shortcode attributes. |
| 5 | 3614 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3615 |
$atts['class'] = apply_filters( 'wp_video_shortcode_class', $atts['class'], $atts ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3616 |
|
| 5 | 3617 |
$html_atts = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3618 |
'class' => $atts['class'], |
| 5 | 3619 |
'id' => sprintf( 'video-%d-%d', $post_id, $instance ), |
3620 |
'width' => absint( $atts['width'] ), |
|
3621 |
'height' => absint( $atts['height'] ), |
|
3622 |
'poster' => esc_url( $atts['poster'] ), |
|
3623 |
'loop' => wp_validate_boolean( $atts['loop'] ), |
|
3624 |
'autoplay' => wp_validate_boolean( $atts['autoplay'] ), |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3625 |
'muted' => wp_validate_boolean( $atts['muted'] ), |
| 5 | 3626 |
'preload' => $atts['preload'], |
| 0 | 3627 |
); |
3628 |
||
| 16 | 3629 |
// These ones should just be omitted altogether if they are blank. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3630 |
foreach ( array( 'poster', 'loop', 'autoplay', 'preload', 'muted' ) as $a ) {
|
| 9 | 3631 |
if ( empty( $html_atts[ $a ] ) ) {
|
3632 |
unset( $html_atts[ $a ] ); |
|
| 5 | 3633 |
} |
| 0 | 3634 |
} |
3635 |
||
3636 |
$attr_strings = array(); |
|
| 5 | 3637 |
foreach ( $html_atts as $k => $v ) {
|
| 0 | 3638 |
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; |
3639 |
} |
|
3640 |
||
3641 |
$html = ''; |
|
| 18 | 3642 |
|
| 5 | 3643 |
if ( 'mediaelement' === $library && 1 === $instance ) {
|
| 0 | 3644 |
$html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n";
|
| 5 | 3645 |
} |
| 18 | 3646 |
|
3647 |
$html .= sprintf( '<video %s controls="controls">', implode( ' ', $attr_strings ) ); |
|
| 0 | 3648 |
|
3649 |
$fileurl = ''; |
|
| 9 | 3650 |
$source = '<source type="%s" src="%s" />'; |
| 18 | 3651 |
|
| 0 | 3652 |
foreach ( $default_types as $fallback ) {
|
| 5 | 3653 |
if ( ! empty( $atts[ $fallback ] ) ) {
|
3654 |
if ( empty( $fileurl ) ) {
|
|
3655 |
$fileurl = $atts[ $fallback ]; |
|
3656 |
} |
|
3657 |
if ( 'src' === $fallback && $is_youtube ) {
|
|
3658 |
$type = array( 'type' => 'video/youtube' ); |
|
3659 |
} elseif ( 'src' === $fallback && $is_vimeo ) {
|
|
3660 |
$type = array( 'type' => 'video/vimeo' ); |
|
3661 |
} else {
|
|
3662 |
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); |
|
3663 |
} |
|
| 9 | 3664 |
$url = add_query_arg( '_', $instance, $atts[ $fallback ] ); |
| 5 | 3665 |
$html .= sprintf( $source, $type['type'], esc_url( $url ) ); |
| 0 | 3666 |
} |
3667 |
} |
|
| 5 | 3668 |
|
3669 |
if ( ! empty( $content ) ) {
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3670 |
if ( str_contains( $content, "\n" ) ) {
|
| 5 | 3671 |
$content = str_replace( array( "\r\n", "\n", "\t" ), '', $content ); |
3672 |
} |
|
3673 |
$html .= trim( $content ); |
|
3674 |
} |
|
3675 |
||
3676 |
if ( 'mediaelement' === $library ) {
|
|
| 0 | 3677 |
$html .= wp_mediaelement_fallback( $fileurl ); |
| 5 | 3678 |
} |
| 0 | 3679 |
$html .= '</video>'; |
3680 |
||
| 5 | 3681 |
$width_rule = ''; |
3682 |
if ( ! empty( $atts['width'] ) ) {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3683 |
$width_rule = sprintf( 'width: %dpx;', $atts['width'] ); |
| 5 | 3684 |
} |
3685 |
$output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html ); |
|
3686 |
||
3687 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3688 |
* Filters the output of the video shortcode. |
| 5 | 3689 |
* |
3690 |
* @since 3.6.0 |
|
3691 |
* |
|
3692 |
* @param string $output Video shortcode HTML output. |
|
3693 |
* @param array $atts Array of video shortcode attributes. |
|
3694 |
* @param string $video Video file. |
|
3695 |
* @param int $post_id Post ID. |
|
3696 |
* @param string $library Media library used for the video shortcode. |
|
3697 |
*/ |
|
3698 |
return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library ); |
|
| 0 | 3699 |
} |
3700 |
add_shortcode( 'video', 'wp_video_shortcode' ); |
|
3701 |
||
3702 |
/** |
|
| 18 | 3703 |
* Gets the previous image link that has the same post parent. |
3704 |
* |
|
3705 |
* @since 5.8.0 |
|
3706 |
* |
|
3707 |
* @see get_adjacent_image_link() |
|
3708 |
* |
|
3709 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3710 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3711 |
* @param string|false $text Optional. Link text. Default false. |
|
3712 |
* @return string Markup for previous image link. |
|
3713 |
*/ |
|
3714 |
function get_previous_image_link( $size = 'thumbnail', $text = false ) {
|
|
3715 |
return get_adjacent_image_link( true, $size, $text ); |
|
3716 |
} |
|
3717 |
||
3718 |
/** |
|
| 5 | 3719 |
* Displays previous image link that has the same post parent. |
| 0 | 3720 |
* |
3721 |
* @since 2.5.0 |
|
| 5 | 3722 |
* |
| 18 | 3723 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3724 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3725 |
* @param string|false $text Optional. Link text. Default false. |
|
| 0 | 3726 |
*/ |
| 5 | 3727 |
function previous_image_link( $size = 'thumbnail', $text = false ) {
|
| 18 | 3728 |
echo get_previous_image_link( $size, $text ); |
3729 |
} |
|
3730 |
||
3731 |
/** |
|
3732 |
* Gets the next image link that has the same post parent. |
|
3733 |
* |
|
3734 |
* @since 5.8.0 |
|
3735 |
* |
|
3736 |
* @see get_adjacent_image_link() |
|
3737 |
* |
|
3738 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3739 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3740 |
* @param string|false $text Optional. Link text. Default false. |
|
3741 |
* @return string Markup for next image link. |
|
3742 |
*/ |
|
3743 |
function get_next_image_link( $size = 'thumbnail', $text = false ) {
|
|
3744 |
return get_adjacent_image_link( false, $size, $text ); |
|
| 0 | 3745 |
} |
3746 |
||
3747 |
/** |
|
| 5 | 3748 |
* Displays next image link that has the same post parent. |
| 0 | 3749 |
* |
3750 |
* @since 2.5.0 |
|
| 5 | 3751 |
* |
| 18 | 3752 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3753 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3754 |
* @param string|false $text Optional. Link text. Default false. |
|
| 0 | 3755 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3756 |
function next_image_link( $size = 'thumbnail', $text = false ) {
|
| 18 | 3757 |
echo get_next_image_link( $size, $text ); |
| 0 | 3758 |
} |
3759 |
||
3760 |
/** |
|
| 18 | 3761 |
* Gets the next or previous image link that has the same post parent. |
| 0 | 3762 |
* |
3763 |
* Retrieves the current attachment object from the $post global. |
|
3764 |
* |
|
| 18 | 3765 |
* @since 5.8.0 |
| 0 | 3766 |
* |
| 5 | 3767 |
* @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. |
| 18 | 3768 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3769 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
| 5 | 3770 |
* @param bool $text Optional. Link text. Default false. |
| 18 | 3771 |
* @return string Markup for image link. |
| 0 | 3772 |
*/ |
| 18 | 3773 |
function get_adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
|
| 9 | 3774 |
$post = get_post(); |
3775 |
$attachments = array_values( |
|
3776 |
get_children( |
|
3777 |
array( |
|
3778 |
'post_parent' => $post->post_parent, |
|
3779 |
'post_status' => 'inherit', |
|
3780 |
'post_type' => 'attachment', |
|
3781 |
'post_mime_type' => 'image', |
|
3782 |
'order' => 'ASC', |
|
3783 |
'orderby' => 'menu_order ID', |
|
3784 |
) |
|
3785 |
) |
|
3786 |
); |
|
| 0 | 3787 |
|
| 5 | 3788 |
foreach ( $attachments as $k => $attachment ) {
|
| 18 | 3789 |
if ( (int) $attachment->ID === (int) $post->ID ) {
|
| 0 | 3790 |
break; |
| 5 | 3791 |
} |
3792 |
} |
|
3793 |
||
| 9 | 3794 |
$output = ''; |
| 5 | 3795 |
$attachment_id = 0; |
3796 |
||
3797 |
if ( $attachments ) {
|
|
3798 |
$k = $prev ? $k - 1 : $k + 1; |
|
3799 |
||
3800 |
if ( isset( $attachments[ $k ] ) ) {
|
|
3801 |
$attachment_id = $attachments[ $k ]->ID; |
|
| 18 | 3802 |
$attr = array( 'alt' => get_the_title( $attachment_id ) ); |
3803 |
$output = wp_get_attachment_link( $attachment_id, $size, true, false, $text, $attr ); |
|
| 5 | 3804 |
} |
| 0 | 3805 |
} |
3806 |
||
3807 |
$adjacent = $prev ? 'previous' : 'next'; |
|
| 5 | 3808 |
|
3809 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3810 |
* Filters the adjacent image link. |
| 5 | 3811 |
* |
3812 |
* The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency, |
|
3813 |
* either 'next', or 'previous'. |
|
3814 |
* |
|
| 18 | 3815 |
* Possible hook names include: |
3816 |
* |
|
3817 |
* - `next_image_link` |
|
3818 |
* - `previous_image_link` |
|
3819 |
* |
|
| 5 | 3820 |
* @since 3.5.0 |
3821 |
* |
|
3822 |
* @param string $output Adjacent image HTML markup. |
|
3823 |
* @param int $attachment_id Attachment ID |
|
| 18 | 3824 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
3825 |
* an array of width and height values in pixels (in that order). |
|
| 5 | 3826 |
* @param string $text Link text. |
3827 |
*/ |
|
| 18 | 3828 |
return apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
|
3829 |
} |
|
3830 |
||
3831 |
/** |
|
3832 |
* Displays next or previous image link that has the same post parent. |
|
3833 |
* |
|
3834 |
* Retrieves the current attachment object from the $post global. |
|
3835 |
* |
|
3836 |
* @since 2.5.0 |
|
3837 |
* |
|
3838 |
* @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. |
|
3839 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3840 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3841 |
* @param bool $text Optional. Link text. Default false. |
|
3842 |
*/ |
|
3843 |
function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) {
|
|
3844 |
echo get_adjacent_image_link( $prev, $size, $text ); |
|
| 0 | 3845 |
} |
3846 |
||
3847 |
/** |
|
| 5 | 3848 |
* Retrieves taxonomies attached to given the attachment. |
| 0 | 3849 |
* |
3850 |
* @since 2.5.0 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3851 |
* @since 4.7.0 Introduced the `$output` parameter. |
| 0 | 3852 |
* |
| 5 | 3853 |
* @param int|array|object $attachment Attachment ID, data array, or data object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3854 |
* @param string $output Output type. 'names' to return an array of taxonomy names, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3855 |
* or 'objects' to return an array of taxonomy objects. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3856 |
* Default is 'names'. |
| 16 | 3857 |
* @return string[]|WP_Taxonomy[] List of taxonomies or taxonomy names. Empty array on failure. |
| 0 | 3858 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3859 |
function get_attachment_taxonomies( $attachment, $output = 'names' ) {
|
| 5 | 3860 |
if ( is_int( $attachment ) ) {
|
3861 |
$attachment = get_post( $attachment ); |
|
3862 |
} elseif ( is_array( $attachment ) ) {
|
|
| 0 | 3863 |
$attachment = (object) $attachment; |
| 5 | 3864 |
} |
| 16 | 3865 |
|
| 9 | 3866 |
if ( ! is_object( $attachment ) ) {
|
| 0 | 3867 |
return array(); |
| 9 | 3868 |
} |
3869 |
||
3870 |
$file = get_attached_file( $attachment->ID ); |
|
3871 |
$filename = wp_basename( $file ); |
|
3872 |
||
3873 |
$objects = array( 'attachment' ); |
|
3874 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3875 |
if ( str_contains( $filename, '.' ) ) {
|
| 9 | 3876 |
$objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 ); |
3877 |
} |
|
| 16 | 3878 |
|
| 9 | 3879 |
if ( ! empty( $attachment->post_mime_type ) ) {
|
| 0 | 3880 |
$objects[] = 'attachment:' . $attachment->post_mime_type; |
| 16 | 3881 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3882 |
if ( str_contains( $attachment->post_mime_type, '/' ) ) {
|
| 9 | 3883 |
foreach ( explode( '/', $attachment->post_mime_type ) as $token ) {
|
3884 |
if ( ! empty( $token ) ) {
|
|
| 0 | 3885 |
$objects[] = "attachment:$token"; |
| 9 | 3886 |
} |
3887 |
} |
|
3888 |
} |
|
| 0 | 3889 |
} |
3890 |
||
3891 |
$taxonomies = array(); |
|
| 16 | 3892 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3893 |
foreach ( $objects as $object ) {
|
| 16 | 3894 |
$taxes = get_object_taxonomies( $object, $output ); |
3895 |
||
3896 |
if ( $taxes ) {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3897 |
$taxonomies = array_merge( $taxonomies, $taxes ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3898 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3899 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3900 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3901 |
if ( 'names' === $output ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3902 |
$taxonomies = array_unique( $taxonomies ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3903 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3904 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3905 |
return $taxonomies; |
| 0 | 3906 |
} |
3907 |
||
3908 |
/** |
|
| 9 | 3909 |
* Retrieves all of the taxonomies that are registered for attachments. |
| 0 | 3910 |
* |
3911 |
* Handles mime-type-specific taxonomies such as attachment:image and attachment:video. |
|
3912 |
* |
|
3913 |
* @since 3.5.0 |
|
| 16 | 3914 |
* |
| 5 | 3915 |
* @see get_taxonomies() |
| 0 | 3916 |
* |
| 5 | 3917 |
* @param string $output Optional. The type of taxonomy output to return. Accepts 'names' or 'objects'. |
3918 |
* Default 'names'. |
|
| 9 | 3919 |
* @return string[]|WP_Taxonomy[] Array of names or objects of registered taxonomies for attachments. |
| 0 | 3920 |
*/ |
3921 |
function get_taxonomies_for_attachments( $output = 'names' ) {
|
|
3922 |
$taxonomies = array(); |
|
| 16 | 3923 |
|
| 0 | 3924 |
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
|
3925 |
foreach ( $taxonomy->object_type as $object_type ) {
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3926 |
if ( 'attachment' === $object_type || str_starts_with( $object_type, 'attachment:' ) ) {
|
| 16 | 3927 |
if ( 'names' === $output ) {
|
| 0 | 3928 |
$taxonomies[] = $taxonomy->name; |
| 9 | 3929 |
} else {
|
| 0 | 3930 |
$taxonomies[ $taxonomy->name ] = $taxonomy; |
| 9 | 3931 |
} |
| 0 | 3932 |
break; |
3933 |
} |
|
3934 |
} |
|
3935 |
} |
|
3936 |
||
3937 |
return $taxonomies; |
|
3938 |
} |
|
3939 |
||
3940 |
/** |
|
| 18 | 3941 |
* Determines whether the value is an acceptable type for GD image functions. |
3942 |
* |
|
3943 |
* In PHP 8.0, the GD extension uses GdImage objects for its data structures. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3944 |
* This function checks if the passed value is either a GdImage object instance |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3945 |
* or a resource of type `gd`. Any other type will return false. |
| 18 | 3946 |
* |
3947 |
* @since 5.6.0 |
|
3948 |
* |
|
3949 |
* @param resource|GdImage|false $image A value to check the type for. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3950 |
* @return bool True if `$image` is either a GD image resource or a GdImage instance, |
| 18 | 3951 |
* false otherwise. |
3952 |
*/ |
|
3953 |
function is_gd_image( $image ) {
|
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3954 |
if ( $image instanceof GdImage |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3955 |
|| is_resource( $image ) && 'gd' === get_resource_type( $image ) |
| 18 | 3956 |
) {
|
3957 |
return true; |
|
3958 |
} |
|
3959 |
||
3960 |
return false; |
|
3961 |
} |
|
3962 |
||
3963 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3964 |
* Creates a new GD image resource with transparency support. |
| 5 | 3965 |
* |
| 16 | 3966 |
* @todo Deprecate if possible. |
| 0 | 3967 |
* |
3968 |
* @since 2.9.0 |
|
3969 |
* |
|
| 5 | 3970 |
* @param int $width Image width in pixels. |
| 18 | 3971 |
* @param int $height Image height in pixels. |
3972 |
* @return resource|GdImage|false The GD image resource or GdImage instance on success. |
|
3973 |
* False on failure. |
|
| 0 | 3974 |
*/ |
| 9 | 3975 |
function wp_imagecreatetruecolor( $width, $height ) {
|
3976 |
$img = imagecreatetruecolor( $width, $height ); |
|
| 18 | 3977 |
|
3978 |
if ( is_gd_image( $img ) |
|
3979 |
&& function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) |
|
3980 |
) {
|
|
| 9 | 3981 |
imagealphablending( $img, false ); |
3982 |
imagesavealpha( $img, true ); |
|
| 0 | 3983 |
} |
| 18 | 3984 |
|
| 0 | 3985 |
return $img; |
3986 |
} |
|
3987 |
||
3988 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
3989 |
* Based on a supplied width/height example, returns the biggest possible dimensions based on the max width/height. |
| 0 | 3990 |
* |
3991 |
* @since 2.9.0 |
|
| 5 | 3992 |
* |
3993 |
* @see wp_constrain_dimensions() |
|
| 0 | 3994 |
* |
| 5 | 3995 |
* @param int $example_width The width of an example embed. |
| 0 | 3996 |
* @param int $example_height The height of an example embed. |
| 5 | 3997 |
* @param int $max_width The maximum allowed width. |
3998 |
* @param int $max_height The maximum allowed height. |
|
| 16 | 3999 |
* @return int[] {
|
4000 |
* An array of maximum width and height values. |
|
4001 |
* |
|
4002 |
* @type int $0 The maximum width in pixels. |
|
4003 |
* @type int $1 The maximum height in pixels. |
|
4004 |
* } |
|
| 0 | 4005 |
*/ |
4006 |
function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
|
|
4007 |
$example_width = (int) $example_width; |
|
4008 |
$example_height = (int) $example_height; |
|
4009 |
$max_width = (int) $max_width; |
|
4010 |
$max_height = (int) $max_height; |
|
4011 |
||
4012 |
return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); |
|
4013 |
} |
|
4014 |
||
4015 |
/** |
|
| 5 | 4016 |
* Determines the maximum upload size allowed in php.ini. |
| 0 | 4017 |
* |
4018 |
* @since 2.5.0 |
|
4019 |
* |
|
4020 |
* @return int Allowed upload size. |
|
4021 |
*/ |
|
4022 |
function wp_max_upload_size() {
|
|
4023 |
$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) ); |
|
4024 |
$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) ); |
|
| 5 | 4025 |
|
4026 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4027 |
* Filters the maximum upload size allowed in php.ini. |
| 5 | 4028 |
* |
4029 |
* @since 2.5.0 |
|
4030 |
* |
|
4031 |
* @param int $size Max upload size limit in bytes. |
|
4032 |
* @param int $u_bytes Maximum upload filesize in bytes. |
|
4033 |
* @param int $p_bytes Maximum size of POST data in bytes. |
|
4034 |
*/ |
|
4035 |
return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes ); |
|
| 0 | 4036 |
} |
4037 |
||
4038 |
/** |
|
4039 |
* Returns a WP_Image_Editor instance and loads file into it. |
|
4040 |
* |
|
4041 |
* @since 3.5.0 |
|
4042 |
* |
|
| 5 | 4043 |
* @param string $path Path to the file to load. |
4044 |
* @param array $args Optional. Additional arguments for retrieving the image editor. |
|
4045 |
* Default empty array. |
|
| 18 | 4046 |
* @return WP_Image_Editor|WP_Error The WP_Image_Editor object on success, |
4047 |
* a WP_Error object otherwise. |
|
| 0 | 4048 |
*/ |
4049 |
function wp_get_image_editor( $path, $args = array() ) {
|
|
4050 |
$args['path'] = $path; |
|
4051 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4052 |
// If the mime type is not set in args, try to extract and set it from the file. |
| 0 | 4053 |
if ( ! isset( $args['mime_type'] ) ) {
|
4054 |
$file_info = wp_check_filetype( $args['path'] ); |
|
4055 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4056 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4057 |
* If $file_info['type'] is false, then we let the editor attempt to |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4058 |
* figure out the file type, rather than forcing a failure based on extension. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4059 |
*/ |
| 9 | 4060 |
if ( isset( $file_info ) && $file_info['type'] ) {
|
| 0 | 4061 |
$args['mime_type'] = $file_info['type']; |
| 9 | 4062 |
} |
| 0 | 4063 |
} |
4064 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4065 |
// Check and set the output mime type mapped to the input type. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4066 |
if ( isset( $args['mime_type'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4067 |
/** This filter is documented in wp-includes/class-wp-image-editor.php */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4068 |
$output_format = apply_filters( 'image_editor_output_format', array(), $path, $args['mime_type'] ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4069 |
if ( isset( $output_format[ $args['mime_type'] ] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4070 |
$args['output_mime_type'] = $output_format[ $args['mime_type'] ]; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4071 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4072 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4073 |
|
| 0 | 4074 |
$implementation = _wp_image_editor_choose( $args ); |
4075 |
||
4076 |
if ( $implementation ) {
|
|
4077 |
$editor = new $implementation( $path ); |
|
4078 |
$loaded = $editor->load(); |
|
4079 |
||
| 9 | 4080 |
if ( is_wp_error( $loaded ) ) {
|
| 0 | 4081 |
return $loaded; |
| 9 | 4082 |
} |
| 0 | 4083 |
|
4084 |
return $editor; |
|
4085 |
} |
|
4086 |
||
| 9 | 4087 |
return new WP_Error( 'image_no_editor', __( 'No editor could be selected.' ) ); |
| 0 | 4088 |
} |
4089 |
||
4090 |
/** |
|
4091 |
* Tests whether there is an editor that supports a given mime type or methods. |
|
4092 |
* |
|
4093 |
* @since 3.5.0 |
|
4094 |
* |
|
| 5 | 4095 |
* @param string|array $args Optional. Array of arguments to retrieve the image editor supports. |
4096 |
* Default empty array. |
|
4097 |
* @return bool True if an eligible editor is found; false otherwise. |
|
| 0 | 4098 |
*/ |
4099 |
function wp_image_editor_supports( $args = array() ) {
|
|
4100 |
return (bool) _wp_image_editor_choose( $args ); |
|
4101 |
} |
|
4102 |
||
4103 |
/** |
|
4104 |
* Tests which editors are capable of supporting the request. |
|
4105 |
* |
|
| 5 | 4106 |
* @ignore |
| 0 | 4107 |
* @since 3.5.0 |
4108 |
* |
|
| 5 | 4109 |
* @param array $args Optional. Array of arguments for choosing a capable editor. Default empty array. |
| 18 | 4110 |
* @return string|false Class name for the first editor that claims to support the request. |
4111 |
* False if no editor claims to support the request. |
|
| 0 | 4112 |
*/ |
4113 |
function _wp_image_editor_choose( $args = array() ) {
|
|
4114 |
require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; |
|
4115 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; |
|
4116 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4117 |
require_once ABSPATH . WPINC . '/class-avif-info.php'; |
| 5 | 4118 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4119 |
* Filters the list of image editing library classes. |
| 5 | 4120 |
* |
4121 |
* @since 3.5.0 |
|
4122 |
* |
|
| 16 | 4123 |
* @param string[] $image_editors Array of available image editor class names. Defaults are |
4124 |
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'. |
|
| 5 | 4125 |
*/ |
4126 |
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) ); |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4127 |
$supports_input = false; |
| 0 | 4128 |
|
4129 |
foreach ( $implementations as $implementation ) {
|
|
| 9 | 4130 |
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
|
| 0 | 4131 |
continue; |
| 9 | 4132 |
} |
| 0 | 4133 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4134 |
// Implementation should support the passed mime type. |
| 0 | 4135 |
if ( isset( $args['mime_type'] ) && |
4136 |
! call_user_func( |
|
4137 |
array( $implementation, 'supports_mime_type' ), |
|
| 9 | 4138 |
$args['mime_type'] |
4139 |
) ) {
|
|
| 0 | 4140 |
continue; |
4141 |
} |
|
4142 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4143 |
// Implementation should support requested methods. |
| 0 | 4144 |
if ( isset( $args['methods'] ) && |
| 9 | 4145 |
array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
|
4146 |
||
| 0 | 4147 |
continue; |
4148 |
} |
|
4149 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4150 |
// Implementation should ideally support the output mime type as well if set and different than the passed type. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4151 |
if ( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4152 |
isset( $args['mime_type'] ) && |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4153 |
isset( $args['output_mime_type'] ) && |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4154 |
$args['mime_type'] !== $args['output_mime_type'] && |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4155 |
! call_user_func( array( $implementation, 'supports_mime_type' ), $args['output_mime_type'] ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4156 |
) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4157 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4158 |
* This implementation supports the input type but not the output type. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4159 |
* Keep looking to see if we can find an implementation that supports both. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4160 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4161 |
$supports_input = $implementation; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4162 |
continue; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4163 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4164 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4165 |
// Favor the implementation that supports both input and output mime types. |
| 0 | 4166 |
return $implementation; |
4167 |
} |
|
4168 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4169 |
return $supports_input; |
| 0 | 4170 |
} |
4171 |
||
4172 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4173 |
* Prints default Plupload arguments. |
| 0 | 4174 |
* |
4175 |
* @since 3.4.0 |
|
4176 |
*/ |
|
4177 |
function wp_plupload_default_settings() {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4178 |
$wp_scripts = wp_scripts(); |
| 0 | 4179 |
|
4180 |
$data = $wp_scripts->get_data( 'wp-plupload', 'data' ); |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4181 |
if ( $data && str_contains( $data, '_wpPluploadSettings' ) ) {
|
| 0 | 4182 |
return; |
| 9 | 4183 |
} |
4184 |
||
4185 |
$max_upload_size = wp_max_upload_size(); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4186 |
$allowed_extensions = array_keys( get_allowed_mime_types() ); |
| 9 | 4187 |
$extensions = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4188 |
foreach ( $allowed_extensions as $extension ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4189 |
$extensions = array_merge( $extensions, explode( '|', $extension ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4190 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4191 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4192 |
/* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4193 |
* Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4194 |
* and the `flash_swf_url` and `silverlight_xap_url` are not used. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4195 |
*/ |
| 0 | 4196 |
$defaults = array( |
| 16 | 4197 |
'file_data_name' => 'async-upload', // Key passed to $_FILE. |
| 9 | 4198 |
'url' => admin_url( 'async-upload.php', 'relative' ), |
4199 |
'filters' => array( |
|
4200 |
'max_file_size' => $max_upload_size . 'b', |
|
4201 |
'mime_types' => array( array( 'extensions' => implode( ',', $extensions ) ) ), |
|
| 5 | 4202 |
), |
| 0 | 4203 |
); |
4204 |
||
| 16 | 4205 |
/* |
4206 |
* Currently only iOS Safari supports multiple files uploading, |
|
4207 |
* but iOS 7.x has a bug that prevents uploading of videos when enabled. |
|
4208 |
* See #29602. |
|
4209 |
*/ |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4210 |
if ( wp_is_mobile() |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4211 |
&& str_contains( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4212 |
&& str_contains( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4213 |
) {
|
| 0 | 4214 |
$defaults['multi_selection'] = false; |
| 5 | 4215 |
} |
4216 |
||
| 18 | 4217 |
// Check if WebP images can be edited. |
4218 |
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
|
|
4219 |
$defaults['webp_upload_error'] = true; |
|
4220 |
} |
|
4221 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4222 |
// Check if AVIF images can be edited. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4223 |
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4224 |
$defaults['avif_upload_error'] = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4225 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4226 |
|
| 5 | 4227 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4228 |
* Filters the Plupload default settings. |
| 5 | 4229 |
* |
4230 |
* @since 3.4.0 |
|
4231 |
* |
|
4232 |
* @param array $defaults Default Plupload settings array. |
|
4233 |
*/ |
|
| 0 | 4234 |
$defaults = apply_filters( 'plupload_default_settings', $defaults ); |
4235 |
||
4236 |
$params = array( |
|
4237 |
'action' => 'upload-attachment', |
|
4238 |
); |
|
4239 |
||
| 5 | 4240 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4241 |
* Filters the Plupload default parameters. |
| 5 | 4242 |
* |
4243 |
* @since 3.4.0 |
|
4244 |
* |
|
4245 |
* @param array $params Default Plupload parameters array. |
|
4246 |
*/ |
|
| 16 | 4247 |
$params = apply_filters( 'plupload_default_params', $params ); |
4248 |
||
4249 |
$params['_wpnonce'] = wp_create_nonce( 'media-form' ); |
|
4250 |
||
| 0 | 4251 |
$defaults['multipart_params'] = $params; |
4252 |
||
4253 |
$settings = array( |
|
| 9 | 4254 |
'defaults' => $defaults, |
4255 |
'browser' => array( |
|
| 0 | 4256 |
'mobile' => wp_is_mobile(), |
4257 |
'supported' => _device_can_upload(), |
|
4258 |
), |
|
| 9 | 4259 |
'limitExceeded' => is_multisite() && ! is_upload_space_available(), |
| 0 | 4260 |
); |
4261 |
||
| 5 | 4262 |
$script = 'var _wpPluploadSettings = ' . wp_json_encode( $settings ) . ';'; |
| 0 | 4263 |
|
| 9 | 4264 |
if ( $data ) {
|
| 0 | 4265 |
$script = "$data\n$script"; |
| 9 | 4266 |
} |
| 0 | 4267 |
|
4268 |
$wp_scripts->add_data( 'wp-plupload', 'data', $script ); |
|
4269 |
} |
|
4270 |
||
4271 |
/** |
|
4272 |
* Prepares an attachment post object for JS, where it is expected |
|
4273 |
* to be JSON-encoded and fit into an Attachment model. |
|
4274 |
* |
|
4275 |
* @since 3.5.0 |
|
4276 |
* |
|
| 9 | 4277 |
* @param int|WP_Post $attachment Attachment ID or object. |
| 18 | 4278 |
* @return array|void {
|
4279 |
* Array of attachment details, or void if the parameter does not correspond to an attachment. |
|
4280 |
* |
|
4281 |
* @type string $alt Alt text of the attachment. |
|
4282 |
* @type string $author ID of the attachment author, as a string. |
|
4283 |
* @type string $authorName Name of the attachment author. |
|
4284 |
* @type string $caption Caption for the attachment. |
|
4285 |
* @type array $compat Containing item and meta. |
|
4286 |
* @type string $context Context, whether it's used as the site icon for example. |
|
4287 |
* @type int $date Uploaded date, timestamp in milliseconds. |
|
4288 |
* @type string $dateFormatted Formatted date (e.g. June 29, 2018). |
|
4289 |
* @type string $description Description of the attachment. |
|
4290 |
* @type string $editLink URL to the edit page for the attachment. |
|
4291 |
* @type string $filename File name of the attachment. |
|
4292 |
* @type string $filesizeHumanReadable Filesize of the attachment in human readable format (e.g. 1 MB). |
|
4293 |
* @type int $filesizeInBytes Filesize of the attachment in bytes. |
|
4294 |
* @type int $height If the attachment is an image, represents the height of the image in pixels. |
|
4295 |
* @type string $icon Icon URL of the attachment (e.g. /wp-includes/images/media/archive.png). |
|
4296 |
* @type int $id ID of the attachment. |
|
4297 |
* @type string $link URL to the attachment. |
|
4298 |
* @type int $menuOrder Menu order of the attachment post. |
|
4299 |
* @type array $meta Meta data for the attachment. |
|
4300 |
* @type string $mime Mime type of the attachment (e.g. image/jpeg or application/zip). |
|
4301 |
* @type int $modified Last modified, timestamp in milliseconds. |
|
4302 |
* @type string $name Name, same as title of the attachment. |
|
4303 |
* @type array $nonces Nonces for update, delete and edit. |
|
4304 |
* @type string $orientation If the attachment is an image, represents the image orientation |
|
4305 |
* (landscape or portrait). |
|
4306 |
* @type array $sizes If the attachment is an image, contains an array of arrays |
|
4307 |
* for the images sizes: thumbnail, medium, large, and full. |
|
4308 |
* @type string $status Post status of the attachment (usually 'inherit'). |
|
4309 |
* @type string $subtype Mime subtype of the attachment (usually the last part, e.g. jpeg or zip). |
|
4310 |
* @type string $title Title of the attachment (usually slugified file name without the extension). |
|
4311 |
* @type string $type Type of the attachment (usually first part of the mime type, e.g. image). |
|
4312 |
* @type int $uploadedTo Parent post to which the attachment was uploaded. |
|
4313 |
* @type string $uploadedToLink URL to the edit page of the parent post of the attachment. |
|
4314 |
* @type string $uploadedToTitle Post title of the parent of the attachment. |
|
4315 |
* @type string $url Direct URL to the attachment file (from wp-content). |
|
4316 |
* @type int $width If the attachment is an image, represents the width of the image in pixels. |
|
4317 |
* } |
|
4318 |
* |
|
| 0 | 4319 |
*/ |
4320 |
function wp_prepare_attachment_for_js( $attachment ) {
|
|
| 16 | 4321 |
$attachment = get_post( $attachment ); |
4322 |
||
4323 |
if ( ! $attachment ) {
|
|
| 0 | 4324 |
return; |
| 9 | 4325 |
} |
4326 |
||
| 16 | 4327 |
if ( 'attachment' !== $attachment->post_type ) {
|
| 0 | 4328 |
return; |
| 9 | 4329 |
} |
| 0 | 4330 |
|
4331 |
$meta = wp_get_attachment_metadata( $attachment->ID ); |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4332 |
if ( str_contains( $attachment->post_mime_type, '/' ) ) {
|
| 0 | 4333 |
list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); |
| 9 | 4334 |
} else {
|
| 0 | 4335 |
list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); |
| 9 | 4336 |
} |
| 0 | 4337 |
|
4338 |
$attachment_url = wp_get_attachment_url( $attachment->ID ); |
|
| 9 | 4339 |
$base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url ); |
| 0 | 4340 |
|
4341 |
$response = array( |
|
| 9 | 4342 |
'id' => $attachment->ID, |
4343 |
'title' => $attachment->post_title, |
|
4344 |
'filename' => wp_basename( get_attached_file( $attachment->ID ) ), |
|
4345 |
'url' => $attachment_url, |
|
4346 |
'link' => get_attachment_link( $attachment->ID ), |
|
4347 |
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), |
|
4348 |
'author' => $attachment->post_author, |
|
4349 |
'description' => $attachment->post_content, |
|
4350 |
'caption' => $attachment->post_excerpt, |
|
4351 |
'name' => $attachment->post_name, |
|
4352 |
'status' => $attachment->post_status, |
|
4353 |
'uploadedTo' => $attachment->post_parent, |
|
4354 |
'date' => strtotime( $attachment->post_date_gmt ) * 1000, |
|
4355 |
'modified' => strtotime( $attachment->post_modified_gmt ) * 1000, |
|
4356 |
'menuOrder' => $attachment->menu_order, |
|
4357 |
'mime' => $attachment->post_mime_type, |
|
4358 |
'type' => $type, |
|
4359 |
'subtype' => $subtype, |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4360 |
'icon' => wp_mime_type_icon( $attachment->ID, '.svg' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4361 |
'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ), |
| 9 | 4362 |
'nonces' => array( |
| 0 | 4363 |
'update' => false, |
4364 |
'delete' => false, |
|
| 9 | 4365 |
'edit' => false, |
| 0 | 4366 |
), |
| 9 | 4367 |
'editLink' => false, |
4368 |
'meta' => false, |
|
| 0 | 4369 |
); |
4370 |
||
| 5 | 4371 |
$author = new WP_User( $attachment->post_author ); |
| 18 | 4372 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4373 |
if ( $author->exists() ) {
|
| 18 | 4374 |
$author_name = $author->display_name ? $author->display_name : $author->nickname; |
4375 |
$response['authorName'] = html_entity_decode( $author_name, ENT_QUOTES, get_bloginfo( 'charset' ) ); |
|
4376 |
$response['authorLink'] = get_edit_user_link( $author->ID ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4377 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4378 |
$response['authorName'] = __( '(no author)' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4379 |
} |
| 5 | 4380 |
|
4381 |
if ( $attachment->post_parent ) {
|
|
4382 |
$post_parent = get_post( $attachment->post_parent ); |
|
| 18 | 4383 |
if ( $post_parent ) {
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4384 |
$response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); |
| 18 | 4385 |
$response['uploadedToLink'] = get_edit_post_link( $attachment->post_parent, 'raw' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4386 |
} |
| 5 | 4387 |
} |
4388 |
||
4389 |
$attached_file = get_attached_file( $attachment->ID ); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4390 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4391 |
if ( isset( $meta['filesize'] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4392 |
$bytes = $meta['filesize']; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4393 |
} elseif ( file_exists( $attached_file ) ) {
|
| 19 | 4394 |
$bytes = wp_filesize( $attached_file ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4395 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4396 |
$bytes = ''; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4397 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4398 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4399 |
if ( $bytes ) {
|
| 9 | 4400 |
$response['filesizeInBytes'] = $bytes; |
| 5 | 4401 |
$response['filesizeHumanReadable'] = size_format( $bytes ); |
4402 |
} |
|
4403 |
||
| 9 | 4404 |
$context = get_post_meta( $attachment->ID, '_wp_attachment_context', true ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4405 |
$response['context'] = ( $context ) ? $context : ''; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4406 |
|
| 0 | 4407 |
if ( current_user_can( 'edit_post', $attachment->ID ) ) {
|
4408 |
$response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID ); |
|
| 9 | 4409 |
$response['nonces']['edit'] = wp_create_nonce( 'image_editor-' . $attachment->ID ); |
4410 |
$response['editLink'] = get_edit_post_link( $attachment->ID, 'raw' ); |
|
| 0 | 4411 |
} |
4412 |
||
| 9 | 4413 |
if ( current_user_can( 'delete_post', $attachment->ID ) ) {
|
| 0 | 4414 |
$response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID ); |
| 9 | 4415 |
} |
| 0 | 4416 |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4417 |
if ( $meta && ( 'image' === $type || ! empty( $meta['sizes'] ) ) ) {
|
| 0 | 4418 |
$sizes = array(); |
| 5 | 4419 |
|
| 0 | 4420 |
/** This filter is documented in wp-admin/includes/media.php */ |
| 9 | 4421 |
$possible_sizes = apply_filters( |
4422 |
'image_size_names_choose', |
|
4423 |
array( |
|
4424 |
'thumbnail' => __( 'Thumbnail' ), |
|
4425 |
'medium' => __( 'Medium' ), |
|
4426 |
'large' => __( 'Large' ), |
|
4427 |
'full' => __( 'Full Size' ), |
|
4428 |
) |
|
4429 |
); |
|
| 0 | 4430 |
unset( $possible_sizes['full'] ); |
4431 |
||
| 16 | 4432 |
/* |
4433 |
* Loop through all potential sizes that may be chosen. Try to do this with some efficiency. |
|
4434 |
* First: run the image_downsize filter. If it returns something, we can use its data. |
|
4435 |
* If the filter does not return something, then image_downsize() is just an expensive way |
|
4436 |
* to check the image metadata, which we do second. |
|
4437 |
*/ |
|
| 0 | 4438 |
foreach ( $possible_sizes as $size => $label ) {
|
| 5 | 4439 |
|
4440 |
/** This filter is documented in wp-includes/media.php */ |
|
| 16 | 4441 |
$downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ); |
4442 |
||
4443 |
if ( $downsize ) {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4444 |
if ( empty( $downsize[3] ) ) {
|
| 0 | 4445 |
continue; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4446 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4447 |
|
| 0 | 4448 |
$sizes[ $size ] = array( |
4449 |
'height' => $downsize[2], |
|
4450 |
'width' => $downsize[1], |
|
4451 |
'url' => $downsize[0], |
|
4452 |
'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape', |
|
4453 |
); |
|
4454 |
} elseif ( isset( $meta['sizes'][ $size ] ) ) {
|
|
4455 |
// Nothing from the filter, so consult image metadata if we have it. |
|
4456 |
$size_meta = $meta['sizes'][ $size ]; |
|
4457 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4458 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4459 |
* We have the actual image size, but might need to further constrain it if content_width is narrower. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4460 |
* Thumbnail, medium, and full sizes are also checked against the site's height/width options. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4461 |
*/ |
| 0 | 4462 |
list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' ); |
4463 |
||
4464 |
$sizes[ $size ] = array( |
|
4465 |
'height' => $height, |
|
4466 |
'width' => $width, |
|
4467 |
'url' => $base_url . $size_meta['file'], |
|
4468 |
'orientation' => $height > $width ? 'portrait' : 'landscape', |
|
4469 |
); |
|
4470 |
} |
|
4471 |
} |
|
4472 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4473 |
if ( 'image' === $type ) {
|
| 16 | 4474 |
if ( ! empty( $meta['original_image'] ) ) {
|
4475 |
$response['originalImageURL'] = wp_get_original_image_url( $attachment->ID ); |
|
4476 |
$response['originalImageName'] = wp_basename( wp_get_original_image_path( $attachment->ID ) ); |
|
4477 |
} |
|
4478 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4479 |
$sizes['full'] = array( 'url' => $attachment_url ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4480 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4481 |
if ( isset( $meta['height'], $meta['width'] ) ) {
|
| 9 | 4482 |
$sizes['full']['height'] = $meta['height']; |
4483 |
$sizes['full']['width'] = $meta['width']; |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4484 |
$sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape'; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4485 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4486 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4487 |
$response = array_merge( $response, $sizes['full'] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4488 |
} elseif ( $meta['sizes']['full']['file'] ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4489 |
$sizes['full'] = array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4490 |
'url' => $base_url . $meta['sizes']['full']['file'], |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4491 |
'height' => $meta['sizes']['full']['height'], |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4492 |
'width' => $meta['sizes']['full']['width'], |
| 9 | 4493 |
'orientation' => $meta['sizes']['full']['height'] > $meta['sizes']['full']['width'] ? 'portrait' : 'landscape', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4494 |
); |
| 0 | 4495 |
} |
4496 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4497 |
$response = array_merge( $response, array( 'sizes' => $sizes ) ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4498 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4499 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4500 |
if ( $meta && 'video' === $type ) {
|
| 9 | 4501 |
if ( isset( $meta['width'] ) ) {
|
| 0 | 4502 |
$response['width'] = (int) $meta['width']; |
| 9 | 4503 |
} |
4504 |
if ( isset( $meta['height'] ) ) {
|
|
| 0 | 4505 |
$response['height'] = (int) $meta['height']; |
| 9 | 4506 |
} |
| 0 | 4507 |
} |
4508 |
||
4509 |
if ( $meta && ( 'audio' === $type || 'video' === $type ) ) {
|
|
| 9 | 4510 |
if ( isset( $meta['length_formatted'] ) ) {
|
4511 |
$response['fileLength'] = $meta['length_formatted']; |
|
4512 |
$response['fileLengthHumanReadable'] = human_readable_duration( $meta['length_formatted'] ); |
|
4513 |
} |
|
| 5 | 4514 |
|
4515 |
$response['meta'] = array(); |
|
4516 |
foreach ( wp_get_attachment_id3_keys( $attachment, 'js' ) as $key => $label ) {
|
|
4517 |
$response['meta'][ $key ] = false; |
|
4518 |
||
4519 |
if ( ! empty( $meta[ $key ] ) ) {
|
|
4520 |
$response['meta'][ $key ] = $meta[ $key ]; |
|
4521 |
} |
|
4522 |
} |
|
4523 |
||
4524 |
$id = get_post_thumbnail_id( $attachment->ID ); |
|
4525 |
if ( ! empty( $id ) ) {
|
|
4526 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' ); |
|
| 9 | 4527 |
$response['image'] = compact( 'src', 'width', 'height' ); |
| 5 | 4528 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' ); |
| 9 | 4529 |
$response['thumb'] = compact( 'src', 'width', 'height' ); |
| 5 | 4530 |
} else {
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4531 |
$src = wp_mime_type_icon( $attachment->ID, '.svg' ); |
| 9 | 4532 |
$width = 48; |
4533 |
$height = 64; |
|
| 5 | 4534 |
$response['image'] = compact( 'src', 'width', 'height' ); |
4535 |
$response['thumb'] = compact( 'src', 'width', 'height' ); |
|
4536 |
} |
|
| 0 | 4537 |
} |
4538 |
||
| 9 | 4539 |
if ( function_exists( 'get_compat_media_markup' ) ) {
|
| 0 | 4540 |
$response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) ); |
| 9 | 4541 |
} |
| 0 | 4542 |
|
| 18 | 4543 |
if ( function_exists( 'get_media_states' ) ) {
|
4544 |
$media_states = get_media_states( $attachment ); |
|
4545 |
if ( ! empty( $media_states ) ) {
|
|
4546 |
$response['mediaStates'] = implode( ', ', $media_states ); |
|
4547 |
} |
|
4548 |
} |
|
4549 |
||
| 5 | 4550 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4551 |
* Filters the attachment data prepared for JavaScript. |
| 5 | 4552 |
* |
4553 |
* @since 3.5.0 |
|
4554 |
* |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4555 |
* @param array $response Array of prepared attachment data. See {@see wp_prepare_attachment_for_js()}.
|
| 9 | 4556 |
* @param WP_Post $attachment Attachment object. |
4557 |
* @param array|false $meta Array of attachment meta data, or false if there is none. |
|
| 5 | 4558 |
*/ |
| 0 | 4559 |
return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); |
4560 |
} |
|
4561 |
||
4562 |
/** |
|
4563 |
* Enqueues all scripts, styles, settings, and templates necessary to use |
|
4564 |
* all media JS APIs. |
|
4565 |
* |
|
4566 |
* @since 3.5.0 |
|
| 5 | 4567 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4568 |
* @global int $content_width |
| 16 | 4569 |
* @global wpdb $wpdb WordPress database abstraction object. |
4570 |
* @global WP_Locale $wp_locale WordPress date and time locale object. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4571 |
* |
| 5 | 4572 |
* @param array $args {
|
4573 |
* Arguments for enqueuing media scripts. |
|
4574 |
* |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4575 |
* @type int|WP_Post $post Post ID or post object. |
| 5 | 4576 |
* } |
| 0 | 4577 |
*/ |
4578 |
function wp_enqueue_media( $args = array() ) {
|
|
4579 |
// Enqueue me just once per page, please. |
|
| 9 | 4580 |
if ( did_action( 'wp_enqueue_media' ) ) {
|
| 0 | 4581 |
return; |
| 9 | 4582 |
} |
| 0 | 4583 |
|
| 5 | 4584 |
global $content_width, $wpdb, $wp_locale; |
4585 |
||
| 0 | 4586 |
$defaults = array( |
4587 |
'post' => null, |
|
4588 |
); |
|
| 9 | 4589 |
$args = wp_parse_args( $args, $defaults ); |
| 0 | 4590 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4591 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4592 |
* We're going to pass the old thickbox media tabs to `media_upload_tabs` |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4593 |
* to ensure plugins will work. We will then unset those tabs. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4594 |
*/ |
| 0 | 4595 |
$tabs = array( |
4596 |
// handler action suffix => tab label |
|
4597 |
'type' => '', |
|
4598 |
'type_url' => '', |
|
4599 |
'gallery' => '', |
|
4600 |
'library' => '', |
|
4601 |
); |
|
4602 |
||
| 5 | 4603 |
/** This filter is documented in wp-admin/includes/media.php */ |
| 0 | 4604 |
$tabs = apply_filters( 'media_upload_tabs', $tabs ); |
4605 |
unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] ); |
|
4606 |
||
4607 |
$props = array( |
|
| 16 | 4608 |
'link' => get_option( 'image_default_link_type' ), // DB default is 'file'. |
4609 |
'align' => get_option( 'image_default_align' ), // Empty default. |
|
4610 |
'size' => get_option( 'image_default_size' ), // Empty default. |
|
| 0 | 4611 |
); |
4612 |
||
| 9 | 4613 |
$exts = array_merge( wp_get_audio_extensions(), wp_get_video_extensions() ); |
4614 |
$mimes = get_allowed_mime_types(); |
|
| 5 | 4615 |
$ext_mimes = array(); |
4616 |
foreach ( $exts as $ext ) {
|
|
4617 |
foreach ( $mimes as $ext_preg => $mime_match ) {
|
|
4618 |
if ( preg_match( '#' . $ext . '#i', $ext_preg ) ) {
|
|
4619 |
$ext_mimes[ $ext ] = $mime_match; |
|
4620 |
break; |
|
4621 |
} |
|
4622 |
} |
|
4623 |
} |
|
4624 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4625 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4626 |
* Allows showing or hiding the "Create Audio Playlist" button in the media library. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4627 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4628 |
* By default, the "Create Audio Playlist" button will always be shown in |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4629 |
* the media library. If this filter returns `null`, a query will be run |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4630 |
* to determine whether the media library contains any audio items. This |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4631 |
* was the default behavior prior to version 4.8.0, but this query is |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4632 |
* expensive for large media libraries. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4633 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4634 |
* @since 4.7.4 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4635 |
* @since 4.8.0 The filter's default value is `true` rather than `null`. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4636 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4637 |
* @link https://core.trac.wordpress.org/ticket/31071 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4638 |
* |
| 16 | 4639 |
* @param bool|null $show Whether to show the button, or `null` to decide based |
4640 |
* on whether any audio files exist in the media library. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4641 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4642 |
$show_audio_playlist = apply_filters( 'media_library_show_audio_playlist', true ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4643 |
if ( null === $show_audio_playlist ) {
|
| 9 | 4644 |
$show_audio_playlist = $wpdb->get_var( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4645 |
"SELECT ID |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4646 |
FROM $wpdb->posts |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4647 |
WHERE post_type = 'attachment' |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4648 |
AND post_mime_type LIKE 'audio%' |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4649 |
LIMIT 1" |
| 9 | 4650 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4651 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4652 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4653 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4654 |
* Allows showing or hiding the "Create Video Playlist" button in the media library. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4655 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4656 |
* By default, the "Create Video Playlist" button will always be shown in |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4657 |
* the media library. If this filter returns `null`, a query will be run |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4658 |
* to determine whether the media library contains any video items. This |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4659 |
* was the default behavior prior to version 4.8.0, but this query is |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4660 |
* expensive for large media libraries. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4661 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4662 |
* @since 4.7.4 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4663 |
* @since 4.8.0 The filter's default value is `true` rather than `null`. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4664 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4665 |
* @link https://core.trac.wordpress.org/ticket/31071 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4666 |
* |
| 16 | 4667 |
* @param bool|null $show Whether to show the button, or `null` to decide based |
4668 |
* on whether any video files exist in the media library. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4669 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4670 |
$show_video_playlist = apply_filters( 'media_library_show_video_playlist', true ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4671 |
if ( null === $show_video_playlist ) {
|
| 9 | 4672 |
$show_video_playlist = $wpdb->get_var( |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4673 |
"SELECT ID |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4674 |
FROM $wpdb->posts |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4675 |
WHERE post_type = 'attachment' |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4676 |
AND post_mime_type LIKE 'video%' |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4677 |
LIMIT 1" |
| 9 | 4678 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4679 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4680 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4681 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4682 |
* Allows overriding the list of months displayed in the media library. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4683 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4684 |
* By default (if this filter does not return an array), a query will be |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4685 |
* run to determine the months that have media items. This query can be |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4686 |
* expensive for large media libraries, so it may be desirable for sites to |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4687 |
* override this behavior. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4688 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4689 |
* @since 4.7.4 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4690 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4691 |
* @link https://core.trac.wordpress.org/ticket/31071 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4692 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4693 |
* @param stdClass[]|null $months An array of objects with `month` and `year` |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4694 |
* properties, or `null` for default behavior. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4695 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4696 |
$months = apply_filters( 'media_library_months_with_files', null ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4697 |
if ( ! is_array( $months ) ) {
|
| 9 | 4698 |
$months = $wpdb->get_results( |
4699 |
$wpdb->prepare( |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4700 |
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4701 |
FROM $wpdb->posts |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4702 |
WHERE post_type = %s |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4703 |
ORDER BY post_date DESC", |
| 9 | 4704 |
'attachment' |
4705 |
) |
|
4706 |
); |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4707 |
} |
| 5 | 4708 |
foreach ( $months as $month_year ) {
|
| 16 | 4709 |
$month_year->text = sprintf( |
4710 |
/* translators: 1: Month, 2: Year. */ |
|
4711 |
__( '%1$s %2$d' ), |
|
4712 |
$wp_locale->get_month( $month_year->month ), |
|
4713 |
$month_year->year |
|
4714 |
); |
|
| 5 | 4715 |
} |
4716 |
||
| 18 | 4717 |
/** |
4718 |
* Filters whether the Media Library grid has infinite scrolling. Default `false`. |
|
4719 |
* |
|
4720 |
* @since 5.8.0 |
|
4721 |
* |
|
4722 |
* @param bool $infinite Whether the Media Library grid has infinite scrolling. |
|
4723 |
*/ |
|
4724 |
$infinite_scrolling = apply_filters( 'media_library_infinite_scrolling', false ); |
|
4725 |
||
| 0 | 4726 |
$settings = array( |
| 18 | 4727 |
'tabs' => $tabs, |
4728 |
'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ), |
|
4729 |
'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ), |
|
| 5 | 4730 |
/** This filter is documented in wp-admin/includes/media.php */ |
| 18 | 4731 |
'captions' => ! apply_filters( 'disable_captions', '' ), |
4732 |
'nonce' => array( |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4733 |
'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ), |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4734 |
'setAttachmentThumbnail' => wp_create_nonce( 'set-attachment-thumbnail' ), |
| 0 | 4735 |
), |
| 18 | 4736 |
'post' => array( |
| 0 | 4737 |
'id' => 0, |
4738 |
), |
|
| 18 | 4739 |
'defaultProps' => $props, |
4740 |
'attachmentCounts' => array( |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4741 |
'audio' => ( $show_audio_playlist ) ? 1 : 0, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4742 |
'video' => ( $show_video_playlist ) ? 1 : 0, |
| 5 | 4743 |
), |
| 18 | 4744 |
'oEmbedProxyUrl' => rest_url( 'oembed/1.0/proxy' ), |
4745 |
'embedExts' => $exts, |
|
4746 |
'embedMimes' => $ext_mimes, |
|
4747 |
'contentWidth' => $content_width, |
|
4748 |
'months' => $months, |
|
4749 |
'mediaTrash' => MEDIA_TRASH ? 1 : 0, |
|
4750 |
'infiniteScrolling' => ( $infinite_scrolling ) ? 1 : 0, |
|
| 0 | 4751 |
); |
4752 |
||
4753 |
$post = null; |
|
4754 |
if ( isset( $args['post'] ) ) {
|
|
| 9 | 4755 |
$post = get_post( $args['post'] ); |
| 0 | 4756 |
$settings['post'] = array( |
| 9 | 4757 |
'id' => $post->ID, |
| 0 | 4758 |
'nonce' => wp_create_nonce( 'update-post_' . $post->ID ), |
4759 |
); |
|
4760 |
||
| 5 | 4761 |
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' ); |
4762 |
if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) {
|
|
4763 |
if ( wp_attachment_is( 'audio', $post ) ) {
|
|
4764 |
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); |
|
4765 |
} elseif ( wp_attachment_is( 'video', $post ) ) {
|
|
4766 |
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); |
|
4767 |
} |
|
4768 |
} |
|
4769 |
||
4770 |
if ( $thumbnail_support ) {
|
|
| 9 | 4771 |
$featured_image_id = get_post_meta( $post->ID, '_thumbnail_id', true ); |
| 0 | 4772 |
$settings['post']['featuredImageId'] = $featured_image_id ? $featured_image_id : -1; |
4773 |
} |
|
4774 |
} |
|
4775 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4776 |
if ( $post ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4777 |
$post_type_object = get_post_type_object( $post->post_type ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4778 |
} else {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4779 |
$post_type_object = get_post_type_object( 'post' ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4780 |
} |
| 0 | 4781 |
|
4782 |
$strings = array( |
|
| 16 | 4783 |
// Generic. |
4784 |
'mediaFrameDefaultTitle' => __( 'Media' ), |
|
| 9 | 4785 |
'url' => __( 'URL' ), |
| 16 | 4786 |
'addMedia' => __( 'Add media' ), |
| 9 | 4787 |
'search' => __( 'Search' ), |
4788 |
'select' => __( 'Select' ), |
|
4789 |
'cancel' => __( 'Cancel' ), |
|
4790 |
'update' => __( 'Update' ), |
|
4791 |
'replace' => __( 'Replace' ), |
|
4792 |
'remove' => __( 'Remove' ), |
|
4793 |
'back' => __( 'Back' ), |
|
4794 |
/* |
|
4795 |
* translators: This is a would-be plural string used in the media manager. |
|
4796 |
* If there is not a word you can use in your language to avoid issues with the |
|
4797 |
* lack of plural support here, turn it into "selected: %d" then translate it. |
|
| 0 | 4798 |
*/ |
| 9 | 4799 |
'selected' => __( '%d selected' ), |
4800 |
'dragInfo' => __( 'Drag and drop to reorder media files.' ), |
|
| 0 | 4801 |
|
| 16 | 4802 |
// Upload. |
4803 |
'uploadFilesTitle' => __( 'Upload files' ), |
|
4804 |
'uploadImagesTitle' => __( 'Upload images' ), |
|
4805 |
||
4806 |
// Library. |
|
| 9 | 4807 |
'mediaLibraryTitle' => __( 'Media Library' ), |
| 16 | 4808 |
'insertMediaTitle' => __( 'Add media' ), |
| 9 | 4809 |
'createNewGallery' => __( 'Create a new gallery' ), |
4810 |
'createNewPlaylist' => __( 'Create a new playlist' ), |
|
4811 |
'createNewVideoPlaylist' => __( 'Create a new video playlist' ), |
|
| 18 | 4812 |
'returnToLibrary' => __( '← Go to library' ), |
| 9 | 4813 |
'allMediaItems' => __( 'All media items' ), |
4814 |
'allDates' => __( 'All dates' ), |
|
4815 |
'noItemsFound' => __( 'No items found.' ), |
|
4816 |
'insertIntoPost' => $post_type_object->labels->insert_into_item, |
|
| 19 | 4817 |
'unattached' => _x( 'Unattached', 'media items' ), |
| 9 | 4818 |
'mine' => _x( 'Mine', 'media items' ), |
4819 |
'trash' => _x( 'Trash', 'noun' ), |
|
4820 |
'uploadedToThisPost' => $post_type_object->labels->uploaded_to_this_item, |
|
4821 |
'warnDelete' => __( "You are about to permanently delete this item from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ), |
|
4822 |
'warnBulkDelete' => __( "You are about to permanently delete these items from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ), |
|
4823 |
'warnBulkTrash' => __( "You are about to trash these items.\n 'Cancel' to stop, 'OK' to delete." ), |
|
| 16 | 4824 |
'bulkSelect' => __( 'Bulk select' ), |
| 9 | 4825 |
'trashSelected' => __( 'Move to Trash' ), |
4826 |
'restoreSelected' => __( 'Restore from Trash' ), |
|
| 16 | 4827 |
'deletePermanently' => __( 'Delete permanently' ), |
| 19 | 4828 |
'errorDeleting' => __( 'Error in deleting the attachment.' ), |
| 9 | 4829 |
'apply' => __( 'Apply' ), |
4830 |
'filterByDate' => __( 'Filter by date' ), |
|
4831 |
'filterByType' => __( 'Filter by type' ), |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4832 |
'searchLabel' => __( 'Search media' ), |
| 16 | 4833 |
'searchMediaLabel' => __( 'Search media' ), // Backward compatibility pre-5.3. |
4834 |
'searchMediaPlaceholder' => __( 'Search media items...' ), // Placeholder (no ellipsis), backward compatibility pre-5.3. |
|
| 18 | 4835 |
/* translators: %d: Number of attachments found in a search. */ |
| 16 | 4836 |
'mediaFound' => __( 'Number of media items found: %d' ), |
4837 |
'noMedia' => __( 'No media items found.' ), |
|
4838 |
'noMediaTryNewSearch' => __( 'No media items found. Try a different search.' ), |
|
4839 |
||
4840 |
// Library Details. |
|
4841 |
'attachmentDetails' => __( 'Attachment details' ), |
|
4842 |
||
4843 |
// From URL. |
|
| 9 | 4844 |
'insertFromUrlTitle' => __( 'Insert from URL' ), |
| 0 | 4845 |
|
| 16 | 4846 |
// Featured Images. |
| 9 | 4847 |
'setFeaturedImageTitle' => $post_type_object->labels->featured_image, |
4848 |
'setFeaturedImage' => $post_type_object->labels->set_featured_image, |
|
| 0 | 4849 |
|
| 16 | 4850 |
// Gallery. |
4851 |
'createGalleryTitle' => __( 'Create gallery' ), |
|
4852 |
'editGalleryTitle' => __( 'Edit gallery' ), |
|
4853 |
'cancelGalleryTitle' => __( '← Cancel gallery' ), |
|
| 9 | 4854 |
'insertGallery' => __( 'Insert gallery' ), |
4855 |
'updateGallery' => __( 'Update gallery' ), |
|
4856 |
'addToGallery' => __( 'Add to gallery' ), |
|
| 16 | 4857 |
'addToGalleryTitle' => __( 'Add to gallery' ), |
| 9 | 4858 |
'reverseOrder' => __( 'Reverse order' ), |
| 5 | 4859 |
|
| 16 | 4860 |
// Edit Image. |
4861 |
'imageDetailsTitle' => __( 'Image details' ), |
|
4862 |
'imageReplaceTitle' => __( 'Replace image' ), |
|
4863 |
'imageDetailsCancel' => __( 'Cancel edit' ), |
|
4864 |
'editImage' => __( 'Edit image' ), |
|
4865 |
||
4866 |
// Crop Image. |
|
4867 |
'chooseImage' => __( 'Choose image' ), |
|
4868 |
'selectAndCrop' => __( 'Select and crop' ), |
|
4869 |
'skipCropping' => __( 'Skip cropping' ), |
|
4870 |
'cropImage' => __( 'Crop image' ), |
|
| 9 | 4871 |
'cropYourImage' => __( 'Crop your image' ), |
4872 |
'cropping' => __( 'Cropping…' ), |
|
| 16 | 4873 |
/* translators: 1: Suggested width number, 2: Suggested height number. */ |
| 9 | 4874 |
'suggestedDimensions' => __( 'Suggested image dimensions: %1$s by %2$s pixels.' ), |
4875 |
'cropError' => __( 'There has been an error cropping your image.' ), |
|
| 5 | 4876 |
|
| 16 | 4877 |
// Edit Audio. |
4878 |
'audioDetailsTitle' => __( 'Audio details' ), |
|
4879 |
'audioReplaceTitle' => __( 'Replace audio' ), |
|
4880 |
'audioAddSourceTitle' => __( 'Add audio source' ), |
|
4881 |
'audioDetailsCancel' => __( 'Cancel edit' ), |
|
4882 |
||
4883 |
// Edit Video. |
|
4884 |
'videoDetailsTitle' => __( 'Video details' ), |
|
4885 |
'videoReplaceTitle' => __( 'Replace video' ), |
|
4886 |
'videoAddSourceTitle' => __( 'Add video source' ), |
|
4887 |
'videoDetailsCancel' => __( 'Cancel edit' ), |
|
4888 |
'videoSelectPosterImageTitle' => __( 'Select poster image' ), |
|
4889 |
'videoAddTrackTitle' => __( 'Add subtitles' ), |
|
4890 |
||
4891 |
// Playlist. |
|
| 9 | 4892 |
'playlistDragInfo' => __( 'Drag and drop to reorder tracks.' ), |
| 16 | 4893 |
'createPlaylistTitle' => __( 'Create audio playlist' ), |
4894 |
'editPlaylistTitle' => __( 'Edit audio playlist' ), |
|
4895 |
'cancelPlaylistTitle' => __( '← Cancel audio playlist' ), |
|
| 9 | 4896 |
'insertPlaylist' => __( 'Insert audio playlist' ), |
4897 |
'updatePlaylist' => __( 'Update audio playlist' ), |
|
4898 |
'addToPlaylist' => __( 'Add to audio playlist' ), |
|
4899 |
'addToPlaylistTitle' => __( 'Add to Audio Playlist' ), |
|
4900 |
||
| 16 | 4901 |
// Video Playlist. |
| 9 | 4902 |
'videoPlaylistDragInfo' => __( 'Drag and drop to reorder videos.' ), |
| 16 | 4903 |
'createVideoPlaylistTitle' => __( 'Create video playlist' ), |
4904 |
'editVideoPlaylistTitle' => __( 'Edit video playlist' ), |
|
4905 |
'cancelVideoPlaylistTitle' => __( '← Cancel video playlist' ), |
|
| 9 | 4906 |
'insertVideoPlaylist' => __( 'Insert video playlist' ), |
4907 |
'updateVideoPlaylist' => __( 'Update video playlist' ), |
|
4908 |
'addToVideoPlaylist' => __( 'Add to video playlist' ), |
|
| 16 | 4909 |
'addToVideoPlaylistTitle' => __( 'Add to video Playlist' ), |
4910 |
||
4911 |
// Headings. |
|
4912 |
'filterAttachments' => __( 'Filter media' ), |
|
4913 |
'attachmentsList' => __( 'Media list' ), |
|
| 0 | 4914 |
); |
4915 |
||
| 5 | 4916 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4917 |
* Filters the media view settings. |
| 5 | 4918 |
* |
4919 |
* @since 3.5.0 |
|
4920 |
* |
|
4921 |
* @param array $settings List of media view settings. |
|
4922 |
* @param WP_Post $post Post object. |
|
4923 |
*/ |
|
| 0 | 4924 |
$settings = apply_filters( 'media_view_settings', $settings, $post ); |
| 5 | 4925 |
|
4926 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4927 |
* Filters the media view strings. |
| 5 | 4928 |
* |
4929 |
* @since 3.5.0 |
|
4930 |
* |
|
| 16 | 4931 |
* @param string[] $strings Array of media view strings keyed by the name they'll be referenced by in JavaScript. |
4932 |
* @param WP_Post $post Post object. |
|
| 5 | 4933 |
*/ |
| 9 | 4934 |
$strings = apply_filters( 'media_view_strings', $strings, $post ); |
| 0 | 4935 |
|
4936 |
$strings['settings'] = $settings; |
|
4937 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4938 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4939 |
* Ensure we enqueue media-editor first, that way media-views |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4940 |
* is registered internally before we try to localize it. See #24724. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
4941 |
*/ |
| 5 | 4942 |
wp_enqueue_script( 'media-editor' ); |
| 0 | 4943 |
wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings ); |
4944 |
||
| 5 | 4945 |
wp_enqueue_script( 'media-audiovideo' ); |
| 0 | 4946 |
wp_enqueue_style( 'media-views' ); |
| 5 | 4947 |
if ( is_admin() ) {
|
4948 |
wp_enqueue_script( 'mce-view' ); |
|
4949 |
wp_enqueue_script( 'image-edit' ); |
|
4950 |
} |
|
4951 |
wp_enqueue_style( 'imgareaselect' ); |
|
| 0 | 4952 |
wp_plupload_default_settings(); |
4953 |
||
4954 |
require_once ABSPATH . WPINC . '/media-template.php'; |
|
4955 |
add_action( 'admin_footer', 'wp_print_media_templates' ); |
|
4956 |
add_action( 'wp_footer', 'wp_print_media_templates' ); |
|
| 5 | 4957 |
add_action( 'customize_controls_print_footer_scripts', 'wp_print_media_templates' ); |
4958 |
||
4959 |
/** |
|
4960 |
* Fires at the conclusion of wp_enqueue_media(). |
|
4961 |
* |
|
4962 |
* @since 3.5.0 |
|
4963 |
*/ |
|
| 0 | 4964 |
do_action( 'wp_enqueue_media' ); |
4965 |
} |
|
4966 |
||
4967 |
/** |
|
| 5 | 4968 |
* Retrieves media attached to the passed post. |
| 0 | 4969 |
* |
4970 |
* @since 3.6.0 |
|
4971 |
* |
|
| 5 | 4972 |
* @param string $type Mime type. |
4973 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
|
| 16 | 4974 |
* @return WP_Post[] Array of media attached to the given post. |
| 0 | 4975 |
*/ |
4976 |
function get_attached_media( $type, $post = 0 ) {
|
|
| 16 | 4977 |
$post = get_post( $post ); |
4978 |
||
4979 |
if ( ! $post ) {
|
|
| 0 | 4980 |
return array(); |
| 9 | 4981 |
} |
| 0 | 4982 |
|
4983 |
$args = array( |
|
| 9 | 4984 |
'post_parent' => $post->ID, |
4985 |
'post_type' => 'attachment', |
|
| 0 | 4986 |
'post_mime_type' => $type, |
4987 |
'posts_per_page' => -1, |
|
| 9 | 4988 |
'orderby' => 'menu_order', |
4989 |
'order' => 'ASC', |
|
| 0 | 4990 |
); |
4991 |
||
| 5 | 4992 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4993 |
* Filters arguments used to retrieve media attached to the given post. |
| 5 | 4994 |
* |
4995 |
* @since 3.6.0 |
|
4996 |
* |
|
| 16 | 4997 |
* @param array $args Post query arguments. |
4998 |
* @param string $type Mime type of the desired media. |
|
4999 |
* @param WP_Post $post Post object. |
|
| 5 | 5000 |
*/ |
| 0 | 5001 |
$args = apply_filters( 'get_attached_media_args', $args, $type, $post ); |
5002 |
||
5003 |
$children = get_children( $args ); |
|
5004 |
||
| 5 | 5005 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5006 |
* Filters the list of media attached to the given post. |
| 5 | 5007 |
* |
5008 |
* @since 3.6.0 |
|
5009 |
* |
|
| 16 | 5010 |
* @param WP_Post[] $children Array of media attached to the given post. |
5011 |
* @param string $type Mime type of the media desired. |
|
5012 |
* @param WP_Post $post Post object. |
|
| 5 | 5013 |
*/ |
| 0 | 5014 |
return (array) apply_filters( 'get_attached_media', $children, $type, $post ); |
5015 |
} |
|
5016 |
||
5017 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5018 |
* Checks the HTML content for an audio, video, object, embed, or iframe tags. |
| 0 | 5019 |
* |
5020 |
* @since 3.6.0 |
|
5021 |
* |
|
| 16 | 5022 |
* @param string $content A string of HTML which might contain media elements. |
5023 |
* @param string[] $types An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'. |
|
5024 |
* @return string[] Array of found HTML media elements. |
|
| 0 | 5025 |
*/ |
5026 |
function get_media_embedded_in_content( $content, $types = null ) {
|
|
5027 |
$html = array(); |
|
| 5 | 5028 |
|
5029 |
/** |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5030 |
* Filters the embedded media types that are allowed to be returned from the content blob. |
| 5 | 5031 |
* |
5032 |
* @since 4.2.0 |
|
5033 |
* |
|
| 16 | 5034 |
* @param string[] $allowed_media_types An array of allowed media types. Default media types are |
5035 |
* 'audio', 'video', 'object', 'embed', and 'iframe'. |
|
| 5 | 5036 |
*/ |
5037 |
$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) ); |
|
5038 |
||
| 0 | 5039 |
if ( ! empty( $types ) ) {
|
| 5 | 5040 |
if ( ! is_array( $types ) ) {
|
| 0 | 5041 |
$types = array( $types ); |
| 5 | 5042 |
} |
5043 |
||
| 0 | 5044 |
$allowed_media_types = array_intersect( $allowed_media_types, $types ); |
5045 |
} |
|
5046 |
||
| 5 | 5047 |
$tags = implode( '|', $allowed_media_types ); |
5048 |
||
5049 |
if ( preg_match_all( '#<(?P<tag>' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches ) ) {
|
|
5050 |
foreach ( $matches[0] as $match ) {
|
|
5051 |
$html[] = $match; |
|
| 0 | 5052 |
} |
5053 |
} |
|
5054 |
||
5055 |
return $html; |
|
5056 |
} |
|
5057 |
||
5058 |
/** |
|
| 5 | 5059 |
* Retrieves galleries from the passed post's content. |
| 0 | 5060 |
* |
5061 |
* @since 3.6.0 |
|
5062 |
* |
|
| 5 | 5063 |
* @param int|WP_Post $post Post ID or object. |
5064 |
* @param bool $html Optional. Whether to return HTML or data in the array. Default true. |
|
| 0 | 5065 |
* @return array A list of arrays, each containing gallery data and srcs parsed |
| 5 | 5066 |
* from the expanded shortcode. |
| 0 | 5067 |
*/ |
5068 |
function get_post_galleries( $post, $html = true ) {
|
|
| 16 | 5069 |
$post = get_post( $post ); |
5070 |
||
5071 |
if ( ! $post ) {
|
|
| 0 | 5072 |
return array(); |
| 9 | 5073 |
} |
5074 |
||
| 19 | 5075 |
if ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) {
|
| 0 | 5076 |
return array(); |
| 9 | 5077 |
} |
| 0 | 5078 |
|
5079 |
$galleries = array(); |
|
5080 |
if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
|
|
5081 |
foreach ( $matches as $shortcode ) {
|
|
5082 |
if ( 'gallery' === $shortcode[2] ) {
|
|
5083 |
$srcs = array(); |
|
5084 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5085 |
$shortcode_attrs = shortcode_parse_atts( $shortcode[3] ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5086 |
|
| 16 | 5087 |
// Specify the post ID of the gallery we're viewing if the shortcode doesn't reference another post already. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5088 |
if ( ! isset( $shortcode_attrs['id'] ) ) {
|
| 18 | 5089 |
$shortcode[3] .= ' id="' . (int) $post->ID . '"'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5090 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5091 |
|
| 0 | 5092 |
$gallery = do_shortcode_tag( $shortcode ); |
5093 |
if ( $html ) {
|
|
5094 |
$galleries[] = $gallery; |
|
5095 |
} else {
|
|
5096 |
preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); |
|
5097 |
if ( ! empty( $src ) ) {
|
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5098 |
foreach ( $src as $s ) {
|
| 0 | 5099 |
$srcs[] = $s[2]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5100 |
} |
| 0 | 5101 |
} |
5102 |
||
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5103 |
$galleries[] = array_merge( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5104 |
$shortcode_attrs, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5105 |
array( |
| 9 | 5106 |
'src' => array_values( array_unique( $srcs ) ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5107 |
) |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5108 |
); |
| 0 | 5109 |
} |
5110 |
} |
|
5111 |
} |
|
5112 |
} |
|
5113 |
||
| 19 | 5114 |
if ( has_block( 'gallery', $post->post_content ) ) {
|
5115 |
$post_blocks = parse_blocks( $post->post_content ); |
|
5116 |
||
5117 |
while ( $block = array_shift( $post_blocks ) ) {
|
|
5118 |
$has_inner_blocks = ! empty( $block['innerBlocks'] ); |
|
5119 |
||
5120 |
// Skip blocks with no blockName and no innerHTML. |
|
5121 |
if ( ! $block['blockName'] ) {
|
|
5122 |
continue; |
|
5123 |
} |
|
5124 |
||
5125 |
// Skip non-Gallery blocks. |
|
5126 |
if ( 'core/gallery' !== $block['blockName'] ) {
|
|
5127 |
// Move inner blocks into the root array before skipping. |
|
5128 |
if ( $has_inner_blocks ) {
|
|
5129 |
array_push( $post_blocks, ...$block['innerBlocks'] ); |
|
5130 |
} |
|
5131 |
continue; |
|
5132 |
} |
|
5133 |
||
5134 |
// New Gallery block format as HTML. |
|
5135 |
if ( $has_inner_blocks && $html ) {
|
|
5136 |
$block_html = wp_list_pluck( $block['innerBlocks'], 'innerHTML' ); |
|
5137 |
$galleries[] = '<figure>' . implode( ' ', $block_html ) . '</figure>'; |
|
5138 |
continue; |
|
5139 |
} |
|
5140 |
||
5141 |
$srcs = array(); |
|
5142 |
||
5143 |
// New Gallery block format as an array. |
|
5144 |
if ( $has_inner_blocks ) {
|
|
5145 |
$attrs = wp_list_pluck( $block['innerBlocks'], 'attrs' ); |
|
5146 |
$ids = wp_list_pluck( $attrs, 'id' ); |
|
5147 |
||
5148 |
foreach ( $ids as $id ) {
|
|
5149 |
$url = wp_get_attachment_url( $id ); |
|
5150 |
||
5151 |
if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
|
|
5152 |
$srcs[] = $url; |
|
5153 |
} |
|
5154 |
} |
|
5155 |
||
5156 |
$galleries[] = array( |
|
5157 |
'ids' => implode( ',', $ids ), |
|
5158 |
'src' => $srcs, |
|
5159 |
); |
|
5160 |
||
5161 |
continue; |
|
5162 |
} |
|
5163 |
||
5164 |
// Old Gallery block format as HTML. |
|
5165 |
if ( $html ) {
|
|
5166 |
$galleries[] = $block['innerHTML']; |
|
5167 |
continue; |
|
5168 |
} |
|
5169 |
||
5170 |
// Old Gallery block format as an array. |
|
5171 |
$ids = ! empty( $block['attrs']['ids'] ) ? $block['attrs']['ids'] : array(); |
|
5172 |
||
5173 |
// If present, use the image IDs from the JSON blob as canonical. |
|
5174 |
if ( ! empty( $ids ) ) {
|
|
5175 |
foreach ( $ids as $id ) {
|
|
5176 |
$url = wp_get_attachment_url( $id ); |
|
5177 |
||
5178 |
if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
|
|
5179 |
$srcs[] = $url; |
|
5180 |
} |
|
5181 |
} |
|
5182 |
||
5183 |
$galleries[] = array( |
|
5184 |
'ids' => implode( ',', $ids ), |
|
5185 |
'src' => $srcs, |
|
5186 |
); |
|
5187 |
||
5188 |
continue; |
|
5189 |
} |
|
5190 |
||
5191 |
// Otherwise, extract srcs from the innerHTML. |
|
5192 |
preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $found_srcs, PREG_SET_ORDER ); |
|
5193 |
||
5194 |
if ( ! empty( $found_srcs[0] ) ) {
|
|
5195 |
foreach ( $found_srcs as $src ) {
|
|
5196 |
if ( isset( $src[2] ) && ! in_array( $src[2], $srcs, true ) ) {
|
|
5197 |
$srcs[] = $src[2]; |
|
5198 |
} |
|
5199 |
} |
|
5200 |
} |
|
5201 |
||
5202 |
$galleries[] = array( 'src' => $srcs ); |
|
5203 |
} |
|
5204 |
} |
|
5205 |
||
| 5 | 5206 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5207 |
* Filters the list of all found galleries in the given post. |
| 5 | 5208 |
* |
5209 |
* @since 3.6.0 |
|
5210 |
* |
|
5211 |
* @param array $galleries Associative array of all found post galleries. |
|
5212 |
* @param WP_Post $post Post object. |
|
5213 |
*/ |
|
| 0 | 5214 |
return apply_filters( 'get_post_galleries', $galleries, $post ); |
5215 |
} |
|
5216 |
||
5217 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5218 |
* Checks a specified post's content for gallery and, if present, return the first |
| 0 | 5219 |
* |
5220 |
* @since 3.6.0 |
|
5221 |
* |
|
| 5 | 5222 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
5223 |
* @param bool $html Optional. Whether to return HTML or data. Default is true. |
|
5224 |
* @return string|array Gallery data and srcs parsed from the expanded shortcode. |
|
| 0 | 5225 |
*/ |
5226 |
function get_post_gallery( $post = 0, $html = true ) {
|
|
5227 |
$galleries = get_post_galleries( $post, $html ); |
|
| 9 | 5228 |
$gallery = reset( $galleries ); |
| 0 | 5229 |
|
| 5 | 5230 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5231 |
* Filters the first-found post gallery. |
| 5 | 5232 |
* |
5233 |
* @since 3.6.0 |
|
5234 |
* |
|
5235 |
* @param array $gallery The first-found post gallery. |
|
5236 |
* @param int|WP_Post $post Post ID or object. |
|
5237 |
* @param array $galleries Associative array of all found post galleries. |
|
5238 |
*/ |
|
| 0 | 5239 |
return apply_filters( 'get_post_gallery', $gallery, $post, $galleries ); |
5240 |
} |
|
5241 |
||
5242 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5243 |
* Retrieves the image srcs from galleries from a post's content, if present. |
| 0 | 5244 |
* |
5245 |
* @since 3.6.0 |
|
5246 |
* |
|
| 5 | 5247 |
* @see get_post_galleries() |
5248 |
* |
|
5249 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
|
5250 |
* @return array A list of lists, each containing image srcs parsed. |
|
5251 |
* from an expanded shortcode |
|
| 0 | 5252 |
*/ |
5253 |
function get_post_galleries_images( $post = 0 ) {
|
|
5254 |
$galleries = get_post_galleries( $post, false ); |
|
5255 |
return wp_list_pluck( $galleries, 'src' ); |
|
5256 |
} |
|
5257 |
||
5258 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5259 |
* Checks a post's content for galleries and return the image srcs for the first found gallery. |
| 0 | 5260 |
* |
5261 |
* @since 3.6.0 |
|
5262 |
* |
|
| 5 | 5263 |
* @see get_post_gallery() |
5264 |
* |
|
5265 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
|
| 16 | 5266 |
* @return string[] A list of a gallery's image srcs in order. |
| 0 | 5267 |
*/ |
5268 |
function get_post_gallery_images( $post = 0 ) {
|
|
5269 |
$gallery = get_post_gallery( $post, false ); |
|
5270 |
return empty( $gallery['src'] ) ? array() : $gallery['src']; |
|
5271 |
} |
|
| 5 | 5272 |
|
5273 |
/** |
|
5274 |
* Maybe attempts to generate attachment metadata, if missing. |
|
5275 |
* |
|
5276 |
* @since 3.9.0 |
|
5277 |
* |
|
5278 |
* @param WP_Post $attachment Attachment object. |
|
5279 |
*/ |
|
5280 |
function wp_maybe_generate_attachment_metadata( $attachment ) {
|
|
| 16 | 5281 |
if ( empty( $attachment ) || empty( $attachment->ID ) ) {
|
| 5 | 5282 |
return; |
5283 |
} |
|
5284 |
||
| 16 | 5285 |
$attachment_id = (int) $attachment->ID; |
5286 |
$file = get_attached_file( $attachment_id ); |
|
5287 |
$meta = wp_get_attachment_metadata( $attachment_id ); |
|
5288 |
||
| 5 | 5289 |
if ( empty( $meta ) && file_exists( $file ) ) {
|
| 16 | 5290 |
$_meta = get_post_meta( $attachment_id ); |
5291 |
$_lock = 'wp_generating_att_' . $attachment_id; |
|
5292 |
||
5293 |
if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) {
|
|
5294 |
set_transient( $_lock, $file ); |
|
| 5 | 5295 |
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); |
| 16 | 5296 |
delete_transient( $_lock ); |
| 5 | 5297 |
} |
5298 |
} |
|
5299 |
} |
|
5300 |
||
5301 |
/** |
|
5302 |
* Tries to convert an attachment URL into a post ID. |
|
5303 |
* |
|
5304 |
* @since 4.0.0 |
|
5305 |
* |
|
5306 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5307 |
* |
|
5308 |
* @param string $url The URL to resolve. |
|
5309 |
* @return int The found post ID, or 0 on failure. |
|
5310 |
*/ |
|
5311 |
function attachment_url_to_postid( $url ) {
|
|
5312 |
global $wpdb; |
|
5313 |
||
| 9 | 5314 |
$dir = wp_get_upload_dir(); |
| 5 | 5315 |
$path = $url; |
5316 |
||
| 9 | 5317 |
$site_url = parse_url( $dir['url'] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5318 |
$image_path = parse_url( $path ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5319 |
|
| 16 | 5320 |
// Force the protocols to match if needed. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5321 |
if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5322 |
$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5323 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5324 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5325 |
if ( str_starts_with( $path, $dir['baseurl'] . '/' ) ) {
|
| 5 | 5326 |
$path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); |
5327 |
} |
|
5328 |
||
| 16 | 5329 |
$sql = $wpdb->prepare( |
5330 |
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", |
|
| 5 | 5331 |
$path |
5332 |
); |
|
| 16 | 5333 |
|
5334 |
$results = $wpdb->get_results( $sql ); |
|
5335 |
$post_id = null; |
|
5336 |
||
5337 |
if ( $results ) {
|
|
5338 |
// Use the first available result, but prefer a case-sensitive match, if exists. |
|
5339 |
$post_id = reset( $results )->post_id; |
|
5340 |
||
5341 |
if ( count( $results ) > 1 ) {
|
|
5342 |
foreach ( $results as $result ) {
|
|
5343 |
if ( $path === $result->meta_value ) {
|
|
5344 |
$post_id = $result->post_id; |
|
5345 |
break; |
|
5346 |
} |
|
5347 |
} |
|
5348 |
} |
|
5349 |
} |
|
| 5 | 5350 |
|
5351 |
/** |
|
| 16 | 5352 |
* Filters an attachment ID found by URL. |
| 5 | 5353 |
* |
5354 |
* @since 4.2.0 |
|
5355 |
* |
|
5356 |
* @param int|null $post_id The post_id (if any) found by the function. |
|
5357 |
* @param string $url The URL being looked up. |
|
5358 |
*/ |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5359 |
return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url ); |
| 5 | 5360 |
} |
5361 |
||
5362 |
/** |
|
5363 |
* Returns the URLs for CSS files used in an iframe-sandbox'd TinyMCE media view. |
|
5364 |
* |
|
5365 |
* @since 4.0.0 |
|
5366 |
* |
|
| 16 | 5367 |
* @return string[] The relevant CSS file URLs. |
| 5 | 5368 |
*/ |
5369 |
function wpview_media_sandbox_styles() {
|
|
| 9 | 5370 |
$version = 'ver=' . get_bloginfo( 'version' ); |
5371 |
$mediaelement = includes_url( "js/mediaelement/mediaelementplayer-legacy.min.css?$version" ); |
|
5372 |
$wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" ); |
|
| 5 | 5373 |
|
5374 |
return array( $mediaelement, $wpmediaelement ); |
|
5375 |
} |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5376 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5377 |
/** |
| 16 | 5378 |
* Registers the personal data exporter for media. |
5379 |
* |
|
5380 |
* @param array[] $exporters An array of personal data exporters, keyed by their ID. |
|
5381 |
* @return array[] Updated array of personal data exporters. |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5382 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5383 |
function wp_register_media_personal_data_exporter( $exporters ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5384 |
$exporters['wordpress-media'] = array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5385 |
'exporter_friendly_name' => __( 'WordPress Media' ), |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5386 |
'callback' => 'wp_media_personal_data_exporter', |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5387 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5388 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5389 |
return $exporters; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5390 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5391 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5392 |
/** |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5393 |
* Finds and exports attachments associated with an email address. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5394 |
* |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5395 |
* @since 4.9.6 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5396 |
* |
| 16 | 5397 |
* @param string $email_address The attachment owner email address. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5398 |
* @param int $page Attachment page number. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5399 |
* @return array {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5400 |
* An array of personal data. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5401 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5402 |
* @type array[] $data An array of personal data arrays. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5403 |
* @type bool $done Whether the exporter is finished. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5404 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5405 |
*/ |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5406 |
function wp_media_personal_data_exporter( $email_address, $page = 1 ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5407 |
// Limit us to 50 attachments at a time to avoid timing out. |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5408 |
$number = 50; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5409 |
$page = (int) $page; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5410 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5411 |
$data_to_export = array(); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5412 |
|
| 9 | 5413 |
$user = get_user_by( 'email', $email_address ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5414 |
if ( false === $user ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5415 |
return array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5416 |
'data' => $data_to_export, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5417 |
'done' => true, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5418 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5419 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5420 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5421 |
$post_query = new WP_Query( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5422 |
array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5423 |
'author' => $user->ID, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5424 |
'posts_per_page' => $number, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5425 |
'paged' => $page, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5426 |
'post_type' => 'attachment', |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5427 |
'post_status' => 'any', |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5428 |
'orderby' => 'ID', |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5429 |
'order' => 'ASC', |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5430 |
) |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5431 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5432 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5433 |
foreach ( (array) $post_query->posts as $post ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5434 |
$attachment_url = wp_get_attachment_url( $post->ID ); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5435 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5436 |
if ( $attachment_url ) {
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5437 |
$post_data_to_export = array( |
| 9 | 5438 |
array( |
5439 |
'name' => __( 'URL' ), |
|
5440 |
'value' => $attachment_url, |
|
5441 |
), |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5442 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5443 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5444 |
$data_to_export[] = array( |
| 16 | 5445 |
'group_id' => 'media', |
5446 |
'group_label' => __( 'Media' ), |
|
5447 |
'group_description' => __( 'User’s media data.' ), |
|
5448 |
'item_id' => "post-{$post->ID}",
|
|
5449 |
'data' => $post_data_to_export, |
|
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5450 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5451 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5452 |
} |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5453 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5454 |
$done = $post_query->max_num_pages <= $page; |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5455 |
|
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5456 |
return array( |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5457 |
'data' => $data_to_export, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5458 |
'done' => $done, |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5459 |
); |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5460 |
} |
| 16 | 5461 |
|
5462 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5463 |
* Adds additional default image sub-sizes. |
| 16 | 5464 |
* |
5465 |
* These sizes are meant to enhance the way WordPress displays images on the front-end on larger, |
|
5466 |
* high-density devices. They make it possible to generate more suitable `srcset` and `sizes` attributes |
|
5467 |
* when the users upload large images. |
|
5468 |
* |
|
5469 |
* The sizes can be changed or removed by themes and plugins but that is not recommended. |
|
5470 |
* The size "names" reflect the image dimensions, so changing the sizes would be quite misleading. |
|
5471 |
* |
|
5472 |
* @since 5.3.0 |
|
5473 |
* @access private |
|
5474 |
*/ |
|
5475 |
function _wp_add_additional_image_sizes() {
|
|
5476 |
// 2x medium_large size. |
|
5477 |
add_image_size( '1536x1536', 1536, 1536 ); |
|
5478 |
// 2x large size. |
|
5479 |
add_image_size( '2048x2048', 2048, 2048 ); |
|
5480 |
} |
|
5481 |
||
5482 |
/** |
|
5483 |
* Callback to enable showing of the user error when uploading .heic images. |
|
5484 |
* |
|
5485 |
* @since 5.5.0 |
|
5486 |
* |
|
5487 |
* @param array[] $plupload_settings The settings for Plupload.js. |
|
5488 |
* @return array[] Modified settings for Plupload.js. |
|
5489 |
*/ |
|
5490 |
function wp_show_heic_upload_error( $plupload_settings ) {
|
|
5491 |
$plupload_settings['heic_upload_error'] = true; |
|
5492 |
return $plupload_settings; |
|
5493 |
} |
|
| 18 | 5494 |
|
5495 |
/** |
|
5496 |
* Allows PHP's getimagesize() to be debuggable when necessary. |
|
5497 |
* |
|
5498 |
* @since 5.7.0 |
|
5499 |
* @since 5.8.0 Added support for WebP images. |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5500 |
* @since 6.5.0 Added support for AVIF images. |
| 18 | 5501 |
* |
5502 |
* @param string $filename The file path. |
|
5503 |
* @param array $image_info Optional. Extended image information (passed by reference). |
|
5504 |
* @return array|false Array of image information or false on failure. |
|
5505 |
*/ |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5506 |
function wp_getimagesize( $filename, ?array &$image_info = null ) {
|
| 18 | 5507 |
// Don't silence errors when in debug mode, unless running unit tests. |
5508 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG |
|
5509 |
&& ! defined( 'WP_RUN_CORE_TESTS' ) |
|
5510 |
) {
|
|
5511 |
if ( 2 === func_num_args() ) {
|
|
5512 |
$info = getimagesize( $filename, $image_info ); |
|
5513 |
} else {
|
|
5514 |
$info = getimagesize( $filename ); |
|
5515 |
} |
|
5516 |
} else {
|
|
5517 |
/* |
|
5518 |
* Silencing notice and warning is intentional. |
|
5519 |
* |
|
5520 |
* getimagesize() has a tendency to generate errors, such as |
|
5521 |
* "corrupt JPEG data: 7191 extraneous bytes before marker", |
|
5522 |
* even when it's able to provide image size information. |
|
5523 |
* |
|
5524 |
* See https://core.trac.wordpress.org/ticket/42480 |
|
5525 |
*/ |
|
5526 |
if ( 2 === func_num_args() ) {
|
|
5527 |
$info = @getimagesize( $filename, $image_info ); |
|
5528 |
} else {
|
|
5529 |
$info = @getimagesize( $filename ); |
|
5530 |
} |
|
5531 |
} |
|
5532 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5533 |
if ( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5534 |
! empty( $info ) && |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5535 |
// Some PHP versions return 0x0 sizes from `getimagesize` for unrecognized image formats, including AVIFs. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5536 |
! ( empty( $info[0] ) && empty( $info[1] ) ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5537 |
) {
|
| 18 | 5538 |
return $info; |
5539 |
} |
|
5540 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5541 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5542 |
* For PHP versions that don't support WebP images, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5543 |
* extract the image size info from the file headers. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5544 |
*/ |
| 18 | 5545 |
if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
|
5546 |
$webp_info = wp_get_webp_info( $filename ); |
|
5547 |
$width = $webp_info['width']; |
|
5548 |
$height = $webp_info['height']; |
|
5549 |
||
5550 |
// Mimic the native return format. |
|
5551 |
if ( $width && $height ) {
|
|
5552 |
return array( |
|
5553 |
$width, |
|
5554 |
$height, |
|
| 19 | 5555 |
IMAGETYPE_WEBP, |
| 18 | 5556 |
sprintf( |
5557 |
'width="%d" height="%d"', |
|
5558 |
$width, |
|
5559 |
$height |
|
5560 |
), |
|
5561 |
'mime' => 'image/webp', |
|
5562 |
); |
|
5563 |
} |
|
5564 |
} |
|
5565 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5566 |
// For PHP versions that don't support AVIF images, extract the image size info from the file headers. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5567 |
if ( 'image/avif' === wp_get_image_mime( $filename ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5568 |
$avif_info = wp_get_avif_info( $filename ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5569 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5570 |
$width = $avif_info['width']; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5571 |
$height = $avif_info['height']; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5572 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5573 |
// Mimic the native return format. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5574 |
if ( $width && $height ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5575 |
return array( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5576 |
$width, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5577 |
$height, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5578 |
IMAGETYPE_AVIF, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5579 |
sprintf( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5580 |
'width="%d" height="%d"', |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5581 |
$width, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5582 |
$height |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5583 |
), |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5584 |
'mime' => 'image/avif', |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5585 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5586 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5587 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5588 |
|
| 18 | 5589 |
// The image could not be parsed. |
5590 |
return false; |
|
5591 |
} |
|
5592 |
||
5593 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5594 |
* Extracts meta information about an AVIF file: width, height, bit depth, and number of channels. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5595 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5596 |
* @since 6.5.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5597 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5598 |
* @param string $filename Path to an AVIF file. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5599 |
* @return array {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5600 |
* An array of AVIF image information. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5601 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5602 |
* @type int|false $width Image width on success, false on failure. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5603 |
* @type int|false $height Image height on success, false on failure. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5604 |
* @type int|false $bit_depth Image bit depth on success, false on failure. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5605 |
* @type int|false $num_channels Image number of channels on success, false on failure. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5606 |
* } |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5607 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5608 |
function wp_get_avif_info( $filename ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5609 |
$results = array( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5610 |
'width' => false, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5611 |
'height' => false, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5612 |
'bit_depth' => false, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5613 |
'num_channels' => false, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5614 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5615 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5616 |
if ( 'image/avif' !== wp_get_image_mime( $filename ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5617 |
return $results; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5618 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5619 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5620 |
// Parse the file using libavifinfo's PHP implementation. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5621 |
require_once ABSPATH . WPINC . '/class-avif-info.php'; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5622 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5623 |
$handle = fopen( $filename, 'rb' ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5624 |
if ( $handle ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5625 |
$parser = new Avifinfo\Parser( $handle ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5626 |
$success = $parser->parse_ftyp() && $parser->parse_file(); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5627 |
fclose( $handle ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5628 |
if ( $success ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5629 |
$results = $parser->features->primary_item_features; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5630 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5631 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5632 |
return $results; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5633 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5634 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5635 |
/** |
| 19 | 5636 |
* Extracts meta information about a WebP file: width, height, and type. |
| 18 | 5637 |
* |
5638 |
* @since 5.8.0 |
|
5639 |
* |
|
5640 |
* @param string $filename Path to a WebP file. |
|
| 19 | 5641 |
* @return array {
|
| 18 | 5642 |
* An array of WebP image information. |
5643 |
* |
|
| 19 | 5644 |
* @type int|false $width Image width on success, false on failure. |
5645 |
* @type int|false $height Image height on success, false on failure. |
|
5646 |
* @type string|false $type The WebP type: one of 'lossy', 'lossless' or 'animated-alpha'. |
|
5647 |
* False on failure. |
|
5648 |
* } |
|
| 18 | 5649 |
*/ |
5650 |
function wp_get_webp_info( $filename ) {
|
|
5651 |
$width = false; |
|
5652 |
$height = false; |
|
5653 |
$type = false; |
|
5654 |
||
5655 |
if ( 'image/webp' !== wp_get_image_mime( $filename ) ) {
|
|
5656 |
return compact( 'width', 'height', 'type' ); |
|
5657 |
} |
|
5658 |
||
| 19 | 5659 |
$magic = file_get_contents( $filename, false, null, 0, 40 ); |
5660 |
||
5661 |
if ( false === $magic ) {
|
|
5662 |
return compact( 'width', 'height', 'type' ); |
|
5663 |
} |
|
5664 |
||
5665 |
// Make sure we got enough bytes. |
|
5666 |
if ( strlen( $magic ) < 40 ) {
|
|
5667 |
return compact( 'width', 'height', 'type' ); |
|
5668 |
} |
|
5669 |
||
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5670 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5671 |
* The headers are a little different for each of the three formats. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5672 |
* Header values based on WebP docs, see https://developers.google.com/speed/webp/docs/riff_container. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5673 |
*/ |
| 19 | 5674 |
switch ( substr( $magic, 12, 4 ) ) {
|
5675 |
// Lossy WebP. |
|
5676 |
case 'VP8 ': |
|
5677 |
$parts = unpack( 'v2', substr( $magic, 26, 4 ) ); |
|
5678 |
$width = (int) ( $parts[1] & 0x3FFF ); |
|
5679 |
$height = (int) ( $parts[2] & 0x3FFF ); |
|
5680 |
$type = 'lossy'; |
|
5681 |
break; |
|
5682 |
// Lossless WebP. |
|
5683 |
case 'VP8L': |
|
5684 |
$parts = unpack( 'C4', substr( $magic, 21, 4 ) ); |
|
5685 |
$width = (int) ( $parts[1] | ( ( $parts[2] & 0x3F ) << 8 ) ) + 1; |
|
5686 |
$height = (int) ( ( ( $parts[2] & 0xC0 ) >> 6 ) | ( $parts[3] << 2 ) | ( ( $parts[4] & 0x03 ) << 10 ) ) + 1; |
|
5687 |
$type = 'lossless'; |
|
5688 |
break; |
|
5689 |
// Animated/alpha WebP. |
|
5690 |
case 'VP8X': |
|
5691 |
// Pad 24-bit int. |
|
5692 |
$width = unpack( 'V', substr( $magic, 24, 3 ) . "\x00" ); |
|
5693 |
$width = (int) ( $width[1] & 0xFFFFFF ) + 1; |
|
5694 |
// Pad 24-bit int. |
|
5695 |
$height = unpack( 'V', substr( $magic, 27, 3 ) . "\x00" ); |
|
5696 |
$height = (int) ( $height[1] & 0xFFFFFF ) + 1; |
|
5697 |
$type = 'animated-alpha'; |
|
5698 |
break; |
|
| 18 | 5699 |
} |
5700 |
||
5701 |
return compact( 'width', 'height', 'type' ); |
|
5702 |
} |
|
| 19 | 5703 |
|
5704 |
/** |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5705 |
* Gets loading optimization attributes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5706 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5707 |
* This function returns an array of attributes that should be merged into the given attributes array to optimize |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5708 |
* loading performance. Potential attributes returned by this function are: |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5709 |
* - `loading` attribute with a value of "lazy" |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5710 |
* - `fetchpriority` attribute with a value of "high" |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5711 |
* - `decoding` attribute with a value of "async" |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5712 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5713 |
* If any of these attributes are already present in the given attributes, they will not be modified. Note that no |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5714 |
* element should have both `loading="lazy"` and `fetchpriority="high"`, so the function will trigger a warning in case |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5715 |
* both attributes are present with those values. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5716 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5717 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5718 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5719 |
* @global WP_Query $wp_query WordPress Query object. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5720 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5721 |
* @param string $tag_name The tag name. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5722 |
* @param array $attr Array of the attributes for the tag. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5723 |
* @param string $context Context for the element for which the loading optimization attribute is requested. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5724 |
* @return array Loading optimization attributes. |
| 19 | 5725 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5726 |
function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5727 |
global $wp_query; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5728 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5729 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5730 |
* Filters whether to short-circuit loading optimization attributes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5731 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5732 |
* Returning an array from the filter will effectively short-circuit the loading of optimization attributes, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5733 |
* returning that value instead. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5734 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5735 |
* @since 6.4.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5736 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5737 |
* @param array|false $loading_attrs False by default, or array of loading optimization attributes to short-circuit. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5738 |
* @param string $tag_name The tag name. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5739 |
* @param array $attr Array of the attributes for the tag. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5740 |
* @param string $context Context for the element for which the loading optimization attribute is requested. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5741 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5742 |
$loading_attrs = apply_filters( 'pre_wp_get_loading_optimization_attributes', false, $tag_name, $attr, $context ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5743 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5744 |
if ( is_array( $loading_attrs ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5745 |
return $loading_attrs; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5746 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5747 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5748 |
$loading_attrs = array(); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5749 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5750 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5751 |
* Skip lazy-loading for the overall block template, as it is handled more granularly. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5752 |
* The skip is also applicable for `fetchpriority`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5753 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5754 |
if ( 'template' === $context ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5755 |
/** This filter is documented in wp-includes/media.php */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5756 |
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5757 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5758 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5759 |
// For now this function only supports images and iframes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5760 |
if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5761 |
/** This filter is documented in wp-includes/media.php */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5762 |
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5763 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5764 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5765 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5766 |
* Skip programmatically created images within content blobs as they need to be handled together with the other |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5767 |
* images within the post content or widget content. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5768 |
* Without this clause, they would already be considered within their own context which skews the image count and |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5769 |
* can result in the first post content image being lazy-loaded or an image further down the page being marked as a |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5770 |
* high priority. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5771 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5772 |
if ( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5773 |
'the_content' !== $context && doing_filter( 'the_content' ) || |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5774 |
'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) || |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5775 |
'widget_block_content' !== $context && doing_filter( 'widget_block_content' ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5776 |
) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5777 |
/** This filter is documented in wp-includes/media.php */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5778 |
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5779 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5780 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5781 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5782 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5783 |
* Add `decoding` with a value of "async" for every image unless it has a |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5784 |
* conflicting `decoding` attribute already present. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5785 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5786 |
if ( 'img' === $tag_name ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5787 |
if ( isset( $attr['decoding'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5788 |
$loading_attrs['decoding'] = $attr['decoding']; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5789 |
} else {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5790 |
$loading_attrs['decoding'] = 'async'; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5791 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5792 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5793 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5794 |
// For any resources, width and height must be provided, to avoid layout shifts. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5795 |
if ( ! isset( $attr['width'], $attr['height'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5796 |
/** This filter is documented in wp-includes/media.php */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5797 |
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5798 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5799 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5800 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5801 |
* The key function logic starts here. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5802 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5803 |
$maybe_in_viewport = null; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5804 |
$increase_count = false; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5805 |
$maybe_increase_count = false; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5806 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5807 |
// Logic to handle a `loading` attribute that is already provided. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5808 |
if ( isset( $attr['loading'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5809 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5810 |
* Interpret "lazy" as not in viewport. Any other value can be |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5811 |
* interpreted as in viewport (realistically only "eager" or `false` |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5812 |
* to force-omit the attribute are other potential values). |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5813 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5814 |
if ( 'lazy' === $attr['loading'] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5815 |
$maybe_in_viewport = false; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5816 |
} else {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5817 |
$maybe_in_viewport = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5818 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5819 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5820 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5821 |
// Logic to handle a `fetchpriority` attribute that is already provided. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5822 |
if ( isset( $attr['fetchpriority'] ) && 'high' === $attr['fetchpriority'] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5823 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5824 |
* If the image was already determined to not be in the viewport (e.g. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5825 |
* from an already provided `loading` attribute), trigger a warning. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5826 |
* Otherwise, the value can be interpreted as in viewport, since only |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5827 |
* the most important in-viewport image should have `fetchpriority` set |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5828 |
* to "high". |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5829 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5830 |
if ( false === $maybe_in_viewport ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5831 |
_doing_it_wrong( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5832 |
__FUNCTION__, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5833 |
__( 'An image should not be lazy-loaded and marked as high priority at the same time.' ), |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5834 |
'6.3.0' |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5835 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5836 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5837 |
* Set `fetchpriority` here for backward-compatibility as we should |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5838 |
* not override what a developer decided, even though it seems |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5839 |
* incorrect. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5840 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5841 |
$loading_attrs['fetchpriority'] = 'high'; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5842 |
} else {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5843 |
$maybe_in_viewport = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5844 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5845 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5846 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5847 |
if ( null === $maybe_in_viewport ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5848 |
$header_enforced_contexts = array( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5849 |
'template_part_' . WP_TEMPLATE_PART_AREA_HEADER => true, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5850 |
'get_header_image_tag' => true, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5851 |
); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5852 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5853 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5854 |
* Filters the header-specific contexts. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5855 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5856 |
* @since 6.4.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5857 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5858 |
* @param array $default_header_enforced_contexts Map of contexts for which elements should be considered |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5859 |
* in the header of the page, as $context => $enabled |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5860 |
* pairs. The $enabled should always be true. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5861 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5862 |
$header_enforced_contexts = apply_filters( 'wp_loading_optimization_force_header_contexts', $header_enforced_contexts ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5863 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5864 |
// Consider elements with these header-specific contexts to be in viewport. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5865 |
if ( isset( $header_enforced_contexts[ $context ] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5866 |
$maybe_in_viewport = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5867 |
$maybe_increase_count = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5868 |
} elseif ( ! is_admin() && in_the_loop() && is_main_query() ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5869 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5870 |
* Get the content media count, since this is a main query |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5871 |
* content element. This is accomplished by "increasing" |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5872 |
* the count by zero, as the only way to get the count is |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5873 |
* to call this function. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5874 |
* The actual count increase happens further below, based |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5875 |
* on the `$increase_count` flag set here. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5876 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5877 |
$content_media_count = wp_increase_content_media_count( 0 ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5878 |
$increase_count = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5879 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5880 |
// If the count so far is below the threshold, `loading` attribute is omitted. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5881 |
if ( $content_media_count < wp_omit_loading_attr_threshold() ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5882 |
$maybe_in_viewport = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5883 |
} else {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5884 |
$maybe_in_viewport = false; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5885 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5886 |
} elseif ( |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5887 |
// Only apply for main query but before the loop. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5888 |
$wp_query->before_loop && $wp_query->is_main_query() |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5889 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5890 |
* Any image before the loop, but after the header has started should not be lazy-loaded, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5891 |
* except when the footer has already started which can happen when the current template |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5892 |
* does not include any loop. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5893 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5894 |
&& did_action( 'get_header' ) && ! did_action( 'get_footer' ) |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5895 |
) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5896 |
$maybe_in_viewport = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5897 |
$maybe_increase_count = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5898 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5899 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5900 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5901 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5902 |
* If the element is in the viewport (`true`), potentially add |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5903 |
* `fetchpriority` with a value of "high". Otherwise, i.e. if the element |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5904 |
* is not not in the viewport (`false`) or it is unknown (`null`), add |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5905 |
* `loading` with a value of "lazy". |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5906 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5907 |
if ( $maybe_in_viewport ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5908 |
$loading_attrs = wp_maybe_add_fetchpriority_high_attr( $loading_attrs, $tag_name, $attr ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5909 |
} else {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5910 |
// Only add `loading="lazy"` if the feature is enabled. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5911 |
if ( wp_lazy_loading_enabled( $tag_name, $context ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5912 |
$loading_attrs['loading'] = 'lazy'; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5913 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5914 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5915 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5916 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5917 |
* If flag was set based on contextual logic above, increase the content |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5918 |
* media count, either unconditionally, or based on whether the image size |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5919 |
* is larger than the threshold. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5920 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5921 |
if ( $increase_count ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5922 |
wp_increase_content_media_count(); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5923 |
} elseif ( $maybe_increase_count ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5924 |
/** This filter is documented in wp-includes/media.php */ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5925 |
$wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5926 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5927 |
if ( $wp_min_priority_img_pixels <= $attr['width'] * $attr['height'] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5928 |
wp_increase_content_media_count(); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5929 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5930 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5931 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5932 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5933 |
* Filters the loading optimization attributes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5934 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5935 |
* @since 6.4.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5936 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5937 |
* @param array $loading_attrs The loading optimization attributes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5938 |
* @param string $tag_name The tag name. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5939 |
* @param array $attr Array of the attributes for the tag. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5940 |
* @param string $context Context for the element for which the loading optimization attribute is requested. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5941 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5942 |
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context ); |
| 19 | 5943 |
} |
5944 |
||
5945 |
/** |
|
5946 |
* Gets the threshold for how many of the first content media elements to not lazy-load. |
|
5947 |
* |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5948 |
* This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3.
|
| 19 | 5949 |
* The filter is only run once per page load, unless the `$force` parameter is used. |
5950 |
* |
|
5951 |
* @since 5.9.0 |
|
5952 |
* |
|
5953 |
* @param bool $force Optional. If set to true, the filter will be (re-)applied even if it already has been before. |
|
5954 |
* Default false. |
|
5955 |
* @return int The number of content media elements to not lazy-load. |
|
5956 |
*/ |
|
5957 |
function wp_omit_loading_attr_threshold( $force = false ) {
|
|
5958 |
static $omit_threshold; |
|
5959 |
||
5960 |
// This function may be called multiple times. Run the filter only once per page load. |
|
5961 |
if ( ! isset( $omit_threshold ) || $force ) {
|
|
5962 |
/** |
|
5963 |
* Filters the threshold for how many of the first content media elements to not lazy-load. |
|
5964 |
* |
|
5965 |
* For these first content media elements, the `loading` attribute will be omitted. By default, this is the case |
|
5966 |
* for only the very first content media element. |
|
5967 |
* |
|
5968 |
* @since 5.9.0 |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5969 |
* @since 6.3.0 The default threshold was changed from 1 to 3. |
| 19 | 5970 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5971 |
* @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 3. |
| 19 | 5972 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5973 |
$omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 3 ); |
| 19 | 5974 |
} |
5975 |
||
5976 |
return $omit_threshold; |
|
5977 |
} |
|
5978 |
||
5979 |
/** |
|
5980 |
* Increases an internal content media count variable. |
|
5981 |
* |
|
5982 |
* @since 5.9.0 |
|
5983 |
* @access private |
|
5984 |
* |
|
5985 |
* @param int $amount Optional. Amount to increase by. Default 1. |
|
5986 |
* @return int The latest content media count, after the increase. |
|
5987 |
*/ |
|
5988 |
function wp_increase_content_media_count( $amount = 1 ) {
|
|
5989 |
static $content_media_count = 0; |
|
5990 |
||
5991 |
$content_media_count += $amount; |
|
5992 |
||
5993 |
return $content_media_count; |
|
5994 |
} |
|
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5995 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5996 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5997 |
* Determines whether to add `fetchpriority='high'` to loading attributes. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5998 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
5999 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6000 |
* @access private |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6001 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6002 |
* @param array $loading_attrs Array of the loading optimization attributes for the element. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6003 |
* @param string $tag_name The tag name. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6004 |
* @param array $attr Array of the attributes for the element. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6005 |
* @return array Updated loading optimization attributes for the element. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6006 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6007 |
function wp_maybe_add_fetchpriority_high_attr( $loading_attrs, $tag_name, $attr ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6008 |
// For now, adding `fetchpriority="high"` is only supported for images. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6009 |
if ( 'img' !== $tag_name ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6010 |
return $loading_attrs; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6011 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6012 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6013 |
if ( isset( $attr['fetchpriority'] ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6014 |
/* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6015 |
* While any `fetchpriority` value could be set in `$loading_attrs`, |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6016 |
* for consistency we only do it for `fetchpriority="high"` since that |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6017 |
* is the only possible value that WordPress core would apply on its |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6018 |
* own. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6019 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6020 |
if ( 'high' === $attr['fetchpriority'] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6021 |
$loading_attrs['fetchpriority'] = 'high'; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6022 |
wp_high_priority_element_flag( false ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6023 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6024 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6025 |
return $loading_attrs; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6026 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6027 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6028 |
// Lazy-loading and `fetchpriority="high"` are mutually exclusive. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6029 |
if ( isset( $loading_attrs['loading'] ) && 'lazy' === $loading_attrs['loading'] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6030 |
return $loading_attrs; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6031 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6032 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6033 |
if ( ! wp_high_priority_element_flag() ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6034 |
return $loading_attrs; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6035 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6036 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6037 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6038 |
* Filters the minimum square-pixels threshold for an image to be eligible as the high-priority image. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6039 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6040 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6041 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6042 |
* @param int $threshold Minimum square-pixels threshold. Default 50000. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6043 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6044 |
$wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6045 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6046 |
if ( $wp_min_priority_img_pixels <= $attr['width'] * $attr['height'] ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6047 |
$loading_attrs['fetchpriority'] = 'high'; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6048 |
wp_high_priority_element_flag( false ); |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6049 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6050 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6051 |
return $loading_attrs; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6052 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6053 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6054 |
/** |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6055 |
* Accesses a flag that indicates if an element is a possible candidate for `fetchpriority='high'`. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6056 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6057 |
* @since 6.3.0 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6058 |
* @access private |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6059 |
* |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6060 |
* @param bool $value Optional. Used to change the static variable. Default null. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6061 |
* @return bool Returns true if high-priority element was marked already, otherwise false. |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6062 |
*/ |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6063 |
function wp_high_priority_element_flag( $value = null ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6064 |
static $high_priority_element = true; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6065 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6066 |
if ( is_bool( $value ) ) {
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6067 |
$high_priority_element = $value; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6068 |
} |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6069 |
|
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6070 |
return $high_priority_element; |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
6071 |
} |