author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress API for media display. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Media |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
* Retrieve additional image sizes. |
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 |
/** |
0 | 29 |
* Scale down the default size of an image. |
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 |
/** |
|
139 |
* Retrieve width and height attributes using given width and height values. |
|
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 |
/** |
|
167 |
* Scale an image to fit a particular size (such as 'thumb' or 'medium'). |
|
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 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
// If the file isn't an image, attempt to replace its URL with a rendered image from its meta. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
// Otherwise, a non-image type could be returned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
if ( ! $is_image ) { |
16 | 223 |
if ( ! empty( $meta['sizes']['full'] ) ) { |
9 | 224 |
$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
|
225 |
$img_url_basename = $meta['sizes']['full']['file']; |
9 | 226 |
$width = $meta['sizes']['full']['width']; |
227 |
$height = $meta['sizes']['full']['height']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
|
16 | 233 |
// Try for a new style intermediate size. |
234 |
$intermediate = image_get_intermediate_size( $id, $size ); |
|
235 |
||
236 |
if ( $intermediate ) { |
|
9 | 237 |
$img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); |
238 |
$width = $intermediate['width']; |
|
239 |
$height = $intermediate['height']; |
|
0 | 240 |
$is_intermediate = true; |
16 | 241 |
} elseif ( 'thumbnail' === $size ) { |
242 |
// Fall back to the old thumbnail. |
|
243 |
$thumb_file = wp_get_attachment_thumb_file( $id ); |
|
244 |
$info = null; |
|
245 |
||
246 |
if ( $thumb_file ) { |
|
18 | 247 |
$info = wp_getimagesize( $thumb_file ); |
16 | 248 |
} |
249 |
||
250 |
if ( $thumb_file && $info ) { |
|
9 | 251 |
$img_url = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url ); |
252 |
$width = $info[0]; |
|
253 |
$height = $info[1]; |
|
0 | 254 |
$is_intermediate = true; |
255 |
} |
|
256 |
} |
|
16 | 257 |
|
9 | 258 |
if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) { |
16 | 259 |
// Any other type: use the real image. |
9 | 260 |
$width = $meta['width']; |
0 | 261 |
$height = $meta['height']; |
262 |
} |
|
263 |
||
9 | 264 |
if ( $img_url ) { |
16 | 265 |
// We have the actual image size, but might need to further constrain it if content_width is narrower. |
0 | 266 |
list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); |
267 |
||
268 |
return array( $img_url, $width, $height, $is_intermediate ); |
|
269 |
} |
|
16 | 270 |
|
0 | 271 |
return false; |
272 |
} |
|
273 |
||
274 |
/** |
|
5 | 275 |
* Register a new image size. |
276 |
* |
|
0 | 277 |
* @since 2.9.0 |
5 | 278 |
* |
279 |
* @global array $_wp_additional_image_sizes Associative array of additional image sizes. |
|
280 |
* |
|
281 |
* @param string $name Image size identifier. |
|
9 | 282 |
* @param int $width Optional. Image width in pixels. Default 0. |
283 |
* @param int $height Optional. Image height in pixels. Default 0. |
|
16 | 284 |
* @param bool|array $crop Optional. Image cropping behavior. If false, the image will be scaled (default), |
285 |
* If true, image will be cropped to the specified dimensions using center positions. |
|
286 |
* If an array, the image will be cropped using the array to specify the crop location. |
|
287 |
* Array values must be in the format: array( x_crop_position, y_crop_position ) where: |
|
288 |
* - x_crop_position accepts: 'left', 'center', or 'right'. |
|
289 |
* - y_crop_position accepts: 'top', 'center', or 'bottom'. |
|
0 | 290 |
*/ |
291 |
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { |
|
292 |
global $_wp_additional_image_sizes; |
|
5 | 293 |
|
294 |
$_wp_additional_image_sizes[ $name ] = array( |
|
295 |
'width' => absint( $width ), |
|
296 |
'height' => absint( $height ), |
|
297 |
'crop' => $crop, |
|
298 |
); |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Check if an image size exists. |
|
303 |
* |
|
304 |
* @since 3.9.0 |
|
305 |
* |
|
306 |
* @param string $name The image size to check. |
|
307 |
* @return bool True if the image size exists, false if not. |
|
308 |
*/ |
|
309 |
function has_image_size( $name ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
$sizes = wp_get_additional_image_sizes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
return isset( $sizes[ $name ] ); |
0 | 312 |
} |
313 |
||
314 |
/** |
|
5 | 315 |
* Remove a new image size. |
316 |
* |
|
317 |
* @since 3.9.0 |
|
318 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
* @global array $_wp_additional_image_sizes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
* |
5 | 321 |
* @param string $name The image size to remove. |
322 |
* @return bool True if the image size was successfully removed, false on failure. |
|
323 |
*/ |
|
324 |
function remove_image_size( $name ) { |
|
325 |
global $_wp_additional_image_sizes; |
|
326 |
||
327 |
if ( isset( $_wp_additional_image_sizes[ $name ] ) ) { |
|
328 |
unset( $_wp_additional_image_sizes[ $name ] ); |
|
329 |
return true; |
|
330 |
} |
|
331 |
||
332 |
return false; |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Registers an image size for the post thumbnail. |
|
0 | 337 |
* |
338 |
* @since 2.9.0 |
|
5 | 339 |
* |
340 |
* @see add_image_size() for details on cropping behavior. |
|
341 |
* |
|
342 |
* @param int $width Image width in pixels. |
|
343 |
* @param int $height Image height in pixels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
* @param bool|array $crop Optional. Whether to crop images to specified width and height or resize. |
5 | 345 |
* An array can specify positioning of the crop area. Default false. |
0 | 346 |
*/ |
347 |
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { |
|
348 |
add_image_size( 'post-thumbnail', $width, $height, $crop ); |
|
349 |
} |
|
350 |
||
351 |
/** |
|
5 | 352 |
* Gets an img tag for an image attachment, scaling it down if requested. |
0 | 353 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
* The {@see 'get_image_tag_class'} filter allows for changing the class name for the |
0 | 355 |
* image without having to use regular expressions on the HTML content. The |
356 |
* parameters are: what WordPress will use for the class, the Attachment ID, |
|
357 |
* image align value, and the size the image should be. |
|
358 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
* The second filter, {@see 'get_image_tag'}, has the HTML content, which can then be |
0 | 360 |
* further manipulated by a plugin to change all attribute values and even HTML |
361 |
* content. |
|
362 |
* |
|
363 |
* @since 2.5.0 |
|
364 |
* |
|
5 | 365 |
* @param int $id Attachment ID. |
16 | 366 |
* @param string $alt Image description for the alt attribute. |
367 |
* @param string $title Image description for the title attribute. |
|
5 | 368 |
* @param string $align Part of the class name for aligning the image. |
18 | 369 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
370 |
* width and height values in pixels (in that order). Default 'medium'. |
|
0 | 371 |
* @return string HTML IMG element for given image attachment |
372 |
*/ |
|
5 | 373 |
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { |
0 | 374 |
|
9 | 375 |
list( $img_src, $width, $height ) = image_downsize( $id, $size ); |
376 |
$hwstring = image_hwstring( $width, $height ); |
|
0 | 377 |
|
378 |
$title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; |
|
379 |
||
18 | 380 |
$size_class = is_array( $size ) ? implode( 'x', $size ) : $size; |
381 |
$class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id; |
|
5 | 382 |
|
383 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
* Filters the value of the attachment's image tag class attribute. |
5 | 385 |
* |
386 |
* @since 2.6.0 |
|
387 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
* @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
|
389 |
* @param int $id Attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
* @param string $align Part of the class name for aligning the image. |
18 | 391 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
392 |
* an array of width and height values in pixels (in that order). |
|
5 | 393 |
*/ |
394 |
$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); |
|
0 | 395 |
|
19 | 396 |
$html = '<img src="' . esc_url( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; |
0 | 397 |
|
5 | 398 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* Filters the HTML content for the image tag. |
5 | 400 |
* |
401 |
* @since 2.6.0 |
|
402 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
* @param string $html HTML content for the image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
* @param int $id Attachment ID. |
16 | 405 |
* @param string $alt Image description for the alt attribute. |
406 |
* @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
|
407 |
* @param string $align Part of the class name for aligning the image. |
18 | 408 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
409 |
* an array of width and height values in pixels (in that order). |
|
5 | 410 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); |
0 | 412 |
} |
413 |
||
414 |
/** |
|
5 | 415 |
* Calculates the new dimensions for a down-sampled image. |
0 | 416 |
* |
417 |
* If either width or height are empty, no constraint is applied on |
|
418 |
* that dimension. |
|
419 |
* |
|
420 |
* @since 2.5.0 |
|
421 |
* |
|
5 | 422 |
* @param int $current_width Current width of the image. |
0 | 423 |
* @param int $current_height Current height of the image. |
5 | 424 |
* @param int $max_width Optional. Max width in pixels to constrain to. Default 0. |
425 |
* @param int $max_height Optional. Max height in pixels to constrain to. Default 0. |
|
16 | 426 |
* @return int[] { |
427 |
* An array of width and height values. |
|
428 |
* |
|
429 |
* @type int $0 The width in pixels. |
|
430 |
* @type int $1 The height in pixels. |
|
431 |
* } |
|
0 | 432 |
*/ |
5 | 433 |
function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) { |
9 | 434 |
if ( ! $max_width && ! $max_height ) { |
0 | 435 |
return array( $current_width, $current_height ); |
9 | 436 |
} |
0 | 437 |
|
16 | 438 |
$width_ratio = 1.0; |
439 |
$height_ratio = 1.0; |
|
440 |
$did_width = false; |
|
441 |
$did_height = false; |
|
0 | 442 |
|
443 |
if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) { |
|
444 |
$width_ratio = $max_width / $current_width; |
|
9 | 445 |
$did_width = true; |
0 | 446 |
} |
447 |
||
448 |
if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) { |
|
449 |
$height_ratio = $max_height / $current_height; |
|
9 | 450 |
$did_height = true; |
0 | 451 |
} |
452 |
||
16 | 453 |
// Calculate the larger/smaller ratios. |
0 | 454 |
$smaller_ratio = min( $width_ratio, $height_ratio ); |
455 |
$larger_ratio = max( $width_ratio, $height_ratio ); |
|
456 |
||
5 | 457 |
if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) { |
9 | 458 |
// The larger ratio is too big. It would result in an overflow. |
0 | 459 |
$ratio = $smaller_ratio; |
5 | 460 |
} else { |
0 | 461 |
// The larger ratio fits, and is likely to be a more "snug" fit. |
462 |
$ratio = $larger_ratio; |
|
5 | 463 |
} |
0 | 464 |
|
465 |
// Very small dimensions may result in 0, 1 should be the minimum. |
|
9 | 466 |
$w = max( 1, (int) round( $current_width * $ratio ) ); |
467 |
$h = max( 1, (int) round( $current_height * $ratio ) ); |
|
0 | 468 |
|
16 | 469 |
/* |
470 |
* Sometimes, due to rounding, we'll end up with a result like this: |
|
471 |
* 465x700 in a 177x177 box is 117x176... a pixel short. |
|
472 |
* We also have issues with recursive calls resulting in an ever-changing result. |
|
473 |
* Constraining to the result of a constraint should yield the original result. |
|
474 |
* Thus we look for dimensions that are one pixel shy of the max value and bump them up. |
|
475 |
*/ |
|
5 | 476 |
|
477 |
// Note: $did_width means it is possible $smaller_ratio == $width_ratio. |
|
16 | 478 |
if ( $did_width && $w === $max_width - 1 ) { |
479 |
$w = $max_width; // Round it up. |
|
5 | 480 |
} |
481 |
||
482 |
// Note: $did_height means it is possible $smaller_ratio == $height_ratio. |
|
16 | 483 |
if ( $did_height && $h === $max_height - 1 ) { |
484 |
$h = $max_height; // Round it up. |
|
5 | 485 |
} |
486 |
||
487 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
* Filters dimensions to constrain down-sampled images to. |
5 | 489 |
* |
490 |
* @since 4.1.0 |
|
491 |
* |
|
16 | 492 |
* @param int[] $dimensions { |
493 |
* An array of width and height values. |
|
494 |
* |
|
495 |
* @type int $0 The width in pixels. |
|
496 |
* @type int $1 The height in pixels. |
|
497 |
* } |
|
9 | 498 |
* @param int $current_width The current width of the image. |
499 |
* @param int $current_height The current height of the image. |
|
500 |
* @param int $max_width The maximum width permitted. |
|
501 |
* @param int $max_height The maximum height permitted. |
|
5 | 502 |
*/ |
503 |
return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height ); |
|
0 | 504 |
} |
505 |
||
506 |
/** |
|
5 | 507 |
* Retrieves calculated resize dimensions for use in WP_Image_Editor. |
508 |
* |
|
509 |
* Calculates dimensions and coordinates for a resized image that fits |
|
510 |
* within a specified width and height. |
|
0 | 511 |
* |
5 | 512 |
* Cropping behavior is dependent on the value of $crop: |
513 |
* 1. If false (default), images will not be cropped. |
|
514 |
* 2. If an array in the form of array( x_crop_position, y_crop_position ): |
|
515 |
* - x_crop_position accepts 'left' 'center', or 'right'. |
|
516 |
* - y_crop_position accepts 'top', 'center', or 'bottom'. |
|
517 |
* Images will be cropped to the specified dimensions within the defined crop area. |
|
518 |
* 3. If true, images will be cropped to the specified dimensions using center positions. |
|
0 | 519 |
* |
520 |
* @since 2.5.0 |
|
521 |
* |
|
5 | 522 |
* @param int $orig_w Original width in pixels. |
523 |
* @param int $orig_h Original height in pixels. |
|
524 |
* @param int $dest_w New width in pixels. |
|
525 |
* @param int $dest_h New height in pixels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
* @param bool|array $crop Optional. Whether to crop image to specified width and height or resize. |
5 | 527 |
* An array can specify positioning of the crop area. Default false. |
16 | 528 |
* @return array|false Returned array matches parameters for `imagecopyresampled()`. False on failure. |
0 | 529 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) { |
0 | 531 |
|
9 | 532 |
if ( $orig_w <= 0 || $orig_h <= 0 ) { |
0 | 533 |
return false; |
9 | 534 |
} |
16 | 535 |
// At least one of $dest_w or $dest_h must be specific. |
9 | 536 |
if ( $dest_w <= 0 && $dest_h <= 0 ) { |
0 | 537 |
return false; |
9 | 538 |
} |
0 | 539 |
|
5 | 540 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
* Filters whether to preempt calculating the image resize dimensions. |
5 | 542 |
* |
16 | 543 |
* Returning a non-null value from the filter will effectively short-circuit |
5 | 544 |
* image_resize_dimensions(), returning that value instead. |
545 |
* |
|
546 |
* @since 3.4.0 |
|
547 |
* |
|
548 |
* @param null|mixed $null Whether to preempt output of the resize dimensions. |
|
549 |
* @param int $orig_w Original width in pixels. |
|
550 |
* @param int $orig_h Original height in pixels. |
|
551 |
* @param int $dest_w New width in pixels. |
|
552 |
* @param int $dest_h New height in pixels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
* @param bool|array $crop Whether to crop image to specified width and height or resize. |
5 | 554 |
* An array can specify positioning of the crop area. Default false. |
555 |
*/ |
|
0 | 556 |
$output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); |
16 | 557 |
|
9 | 558 |
if ( null !== $output ) { |
0 | 559 |
return $output; |
9 | 560 |
} |
0 | 561 |
|
16 | 562 |
// Stop if the destination size is larger than the original image dimensions. |
563 |
if ( empty( $dest_h ) ) { |
|
564 |
if ( $orig_w < $dest_w ) { |
|
565 |
return false; |
|
566 |
} |
|
567 |
} elseif ( empty( $dest_w ) ) { |
|
568 |
if ( $orig_h < $dest_h ) { |
|
569 |
return false; |
|
570 |
} |
|
571 |
} else { |
|
572 |
if ( $orig_w < $dest_w && $orig_h < $dest_h ) { |
|
573 |
return false; |
|
574 |
} |
|
575 |
} |
|
576 |
||
0 | 577 |
if ( $crop ) { |
16 | 578 |
/* |
579 |
* Crop the largest possible portion of the original image that we can size to $dest_w x $dest_h. |
|
580 |
* Note that the requested crop dimensions are used as a maximum bounding box for the original image. |
|
581 |
* If the original image's width or height is less than the requested width or height |
|
582 |
* only the greater one will be cropped. |
|
583 |
* For example when the original image is 600x300, and the requested crop dimensions are 400x400, |
|
584 |
* the resulting image will be 400x300. |
|
585 |
*/ |
|
0 | 586 |
$aspect_ratio = $orig_w / $orig_h; |
9 | 587 |
$new_w = min( $dest_w, $orig_w ); |
588 |
$new_h = min( $dest_h, $orig_h ); |
|
0 | 589 |
|
5 | 590 |
if ( ! $new_w ) { |
591 |
$new_w = (int) round( $new_h * $aspect_ratio ); |
|
0 | 592 |
} |
593 |
||
5 | 594 |
if ( ! $new_h ) { |
595 |
$new_h = (int) round( $new_w / $aspect_ratio ); |
|
0 | 596 |
} |
597 |
||
9 | 598 |
$size_ratio = max( $new_w / $orig_w, $new_h / $orig_h ); |
599 |
||
600 |
$crop_w = round( $new_w / $size_ratio ); |
|
601 |
$crop_h = round( $new_h / $size_ratio ); |
|
0 | 602 |
|
5 | 603 |
if ( ! is_array( $crop ) || count( $crop ) !== 2 ) { |
604 |
$crop = array( 'center', 'center' ); |
|
605 |
} |
|
606 |
||
607 |
list( $x, $y ) = $crop; |
|
608 |
||
609 |
if ( 'left' === $x ) { |
|
610 |
$s_x = 0; |
|
611 |
} elseif ( 'right' === $x ) { |
|
612 |
$s_x = $orig_w - $crop_w; |
|
613 |
} else { |
|
614 |
$s_x = floor( ( $orig_w - $crop_w ) / 2 ); |
|
615 |
} |
|
616 |
||
617 |
if ( 'top' === $y ) { |
|
618 |
$s_y = 0; |
|
619 |
} elseif ( 'bottom' === $y ) { |
|
620 |
$s_y = $orig_h - $crop_h; |
|
621 |
} else { |
|
622 |
$s_y = floor( ( $orig_h - $crop_h ) / 2 ); |
|
623 |
} |
|
0 | 624 |
} else { |
16 | 625 |
// Resize using $dest_w x $dest_h as a maximum bounding box. |
0 | 626 |
$crop_w = $orig_w; |
627 |
$crop_h = $orig_h; |
|
628 |
||
629 |
$s_x = 0; |
|
630 |
$s_y = 0; |
|
631 |
||
632 |
list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h ); |
|
633 |
} |
|
634 |
||
16 | 635 |
if ( wp_fuzzy_number_match( $new_w, $orig_w ) && wp_fuzzy_number_match( $new_h, $orig_h ) ) { |
636 |
// The new size has virtually the same dimensions as the original image. |
|
637 |
||
638 |
/** |
|
639 |
* Filters whether to proceed with making an image sub-size with identical dimensions |
|
640 |
* with the original/source image. Differences of 1px may be due to rounding and are ignored. |
|
641 |
* |
|
642 |
* @since 5.3.0 |
|
643 |
* |
|
644 |
* @param bool $proceed The filtered value. |
|
645 |
* @param int $orig_w Original image width. |
|
646 |
* @param int $orig_h Original image height. |
|
647 |
*/ |
|
648 |
$proceed = (bool) apply_filters( 'wp_image_resize_identical_dimensions', false, $orig_w, $orig_h ); |
|
649 |
||
650 |
if ( ! $proceed ) { |
|
651 |
return false; |
|
652 |
} |
|
5 | 653 |
} |
0 | 654 |
|
16 | 655 |
// The return array matches the parameters to imagecopyresampled(). |
0 | 656 |
// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h |
657 |
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); |
|
658 |
} |
|
659 |
||
660 |
/** |
|
5 | 661 |
* Resizes an image to make a thumbnail or intermediate size. |
0 | 662 |
* |
663 |
* 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
|
664 |
* {@see 'image_make_intermediate_size'} filter can be used to hook in and change the |
0 | 665 |
* values of the returned array. The only parameter is the resized file path. |
666 |
* |
|
667 |
* @since 2.5.0 |
|
668 |
* |
|
5 | 669 |
* @param string $file File path. |
670 |
* @param int $width Image width. |
|
671 |
* @param int $height Image height. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
* @param bool $crop Optional. Whether to crop image to specified width and height or resize. |
5 | 673 |
* Default false. |
16 | 674 |
* @return array|false Metadata array on success. False if no image was created. |
0 | 675 |
*/ |
676 |
function image_make_intermediate_size( $file, $width, $height, $crop = false ) { |
|
677 |
if ( $width || $height ) { |
|
678 |
$editor = wp_get_image_editor( $file ); |
|
679 |
||
9 | 680 |
if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) { |
0 | 681 |
return false; |
9 | 682 |
} |
0 | 683 |
|
684 |
$resized_file = $editor->save(); |
|
685 |
||
686 |
if ( ! is_wp_error( $resized_file ) && $resized_file ) { |
|
687 |
unset( $resized_file['path'] ); |
|
688 |
return $resized_file; |
|
689 |
} |
|
690 |
} |
|
691 |
return false; |
|
692 |
} |
|
693 |
||
694 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
* 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
|
696 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
698 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
699 |
* @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
|
700 |
* @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
|
701 |
* @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
|
702 |
* @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
|
703 |
* @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
|
704 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
705 |
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
|
706 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
707 |
* 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
|
708 |
* 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
|
709 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
710 |
if ( $source_width > $target_width ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
711 |
$constrained_size = wp_constrain_dimensions( $source_width, $source_height, $target_width ); |
9 | 712 |
$expected_size = array( $target_width, $target_height ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
713 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
$constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width ); |
9 | 715 |
$expected_size = array( $source_width, $source_height ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
717 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
// If the image dimensions are within 1px of the expected size, we consider it a match. |
16 | 719 |
$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
|
720 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
return $matched; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
724 |
/** |
5 | 725 |
* Retrieves the image's intermediate size (resized) path, width, and height. |
0 | 726 |
* |
727 |
* The $size parameter can be an array with the width and height respectively. |
|
728 |
* If the size matches the 'sizes' metadata array for width and height, then it |
|
729 |
* will be used. If there is no direct match, then the nearest image size larger |
|
730 |
* than the specified size will be used. If nothing is found, then the function |
|
731 |
* will break out and return false. |
|
732 |
* |
|
733 |
* The metadata 'sizes' is used for compatible sizes that can be used for the |
|
734 |
* parameter $size value. |
|
735 |
* |
|
736 |
* The url path will be given, when the $size parameter is a string. |
|
737 |
* |
|
738 |
* If you are passing an array for the $size, you should consider using |
|
739 |
* add_image_size() so that a cropped version is generated. It's much more |
|
740 |
* efficient than having to find the closest-sized image and then having the |
|
741 |
* browser scale down the image. |
|
742 |
* |
|
743 |
* @since 2.5.0 |
|
744 |
* |
|
5 | 745 |
* @param int $post_id Attachment ID. |
18 | 746 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
747 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
16 | 748 |
* @return array|false { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
* Array of file relative path, width, and height on success. Additionally includes absolute |
18 | 750 |
* path and URL if registered size is passed to `$size` parameter. False on failure. |
751 |
* |
|
752 |
* @type string $file Path of image relative to uploads directory. |
|
753 |
* @type int $width Width of image in pixels. |
|
754 |
* @type int $height Height of image in pixels. |
|
755 |
* @type string $path Absolute filesystem path of image. |
|
756 |
* @type string $url URL of image. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
* } |
0 | 758 |
*/ |
5 | 759 |
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { |
16 | 760 |
$imagedata = wp_get_attachment_metadata( $post_id ); |
761 |
||
762 |
if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { |
|
0 | 763 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
764 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
766 |
$data = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
// Find the best match when '$size' is an array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
if ( is_array( $size ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
770 |
$candidates = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
771 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
772 |
if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
773 |
$imagedata['height'] = $imagedata['sizes']['full']['height']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
774 |
$imagedata['width'] = $imagedata['sizes']['full']['width']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
} |
5 | 776 |
|
0 | 777 |
foreach ( $imagedata['sizes'] as $_size => $data ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
778 |
// If there's an exact match to an existing image size, short circuit. |
18 | 779 |
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
|
780 |
$candidates[ $data['width'] * $data['height'] ] = $data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
781 |
break; |
0 | 782 |
} |
7
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 |
// 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
|
785 |
if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
786 |
// 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
|
787 |
if ( 0 === $size[0] || 0 === $size[1] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
$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
|
789 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
$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
|
791 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
if ( $same_ratio ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
$candidates[ $data['width'] * $data['height'] ] = $data; |
0 | 795 |
} |
796 |
} |
|
797 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
799 |
if ( ! empty( $candidates ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
// 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
|
801 |
if ( 1 < count( $candidates ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
802 |
ksort( $candidates ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
$data = array_shift( $candidates ); |
9 | 806 |
/* |
807 |
* When the size requested is smaller than the thumbnail dimensions, we |
|
808 |
* fall back to the thumbnail size to maintain backward compatibility with |
|
809 |
* pre 4.6 versions of WordPress. |
|
810 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
} 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
|
812 |
$data = $imagedata['sizes']['thumbnail']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
// 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
|
818 |
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
|
819 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
} elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
$data = $imagedata['sizes'][ $size ]; |
0 | 822 |
} |
823 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
// 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
|
825 |
if ( empty( $data ) ) { |
0 | 826 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
|
16 | 829 |
// 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
|
830 |
if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) { |
9 | 831 |
$file_url = wp_get_attachment_url( $post_id ); |
832 |
$data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] ); |
|
833 |
$data['url'] = path_join( dirname( $file_url ), $data['file'] ); |
|
0 | 834 |
} |
7
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
837 |
* Filters the output of image_get_intermediate_size() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
838 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
839 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
840 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
841 |
* @see image_get_intermediate_size() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
842 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
843 |
* @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
|
844 |
* file absolute path and URL. |
18 | 845 |
* @param int $post_id The ID of the image attachment. |
846 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
847 |
* 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
|
848 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
849 |
return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); |
0 | 850 |
} |
851 |
||
852 |
/** |
|
16 | 853 |
* Gets the available intermediate image size names. |
5 | 854 |
* |
0 | 855 |
* @since 3.0.0 |
5 | 856 |
* |
16 | 857 |
* @return string[] An array of image size names. |
0 | 858 |
*/ |
859 |
function get_intermediate_image_sizes() { |
|
16 | 860 |
$default_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' ); |
861 |
$additional_sizes = wp_get_additional_image_sizes(); |
|
862 |
||
863 |
if ( ! empty( $additional_sizes ) ) { |
|
864 |
$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
|
865 |
} |
0 | 866 |
|
5 | 867 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
* Filters the list of intermediate image sizes. |
5 | 869 |
* |
870 |
* @since 2.5.0 |
|
871 |
* |
|
16 | 872 |
* @param string[] $default_sizes An array of intermediate image size names. Defaults |
873 |
* are 'thumbnail', 'medium', 'medium_large', 'large'. |
|
5 | 874 |
*/ |
16 | 875 |
return apply_filters( 'intermediate_image_sizes', $default_sizes ); |
0 | 876 |
} |
877 |
||
878 |
/** |
|
16 | 879 |
* Returns a normalized list of all currently registered image sub-sizes. |
880 |
* |
|
881 |
* @since 5.3.0 |
|
882 |
* @uses wp_get_additional_image_sizes() |
|
883 |
* @uses get_intermediate_image_sizes() |
|
884 |
* |
|
19 | 885 |
* @return array[] Associative array of arrays of image sub-size information, |
886 |
* keyed by image size name. |
|
16 | 887 |
*/ |
888 |
function wp_get_registered_image_subsizes() { |
|
889 |
$additional_sizes = wp_get_additional_image_sizes(); |
|
890 |
$all_sizes = array(); |
|
891 |
||
892 |
foreach ( get_intermediate_image_sizes() as $size_name ) { |
|
893 |
$size_data = array( |
|
894 |
'width' => 0, |
|
895 |
'height' => 0, |
|
896 |
'crop' => false, |
|
897 |
); |
|
898 |
||
899 |
if ( isset( $additional_sizes[ $size_name ]['width'] ) ) { |
|
900 |
// For sizes added by plugins and themes. |
|
18 | 901 |
$size_data['width'] = (int) $additional_sizes[ $size_name ]['width']; |
16 | 902 |
} else { |
903 |
// For default sizes set in options. |
|
18 | 904 |
$size_data['width'] = (int) get_option( "{$size_name}_size_w" ); |
16 | 905 |
} |
906 |
||
907 |
if ( isset( $additional_sizes[ $size_name ]['height'] ) ) { |
|
18 | 908 |
$size_data['height'] = (int) $additional_sizes[ $size_name ]['height']; |
16 | 909 |
} else { |
18 | 910 |
$size_data['height'] = (int) get_option( "{$size_name}_size_h" ); |
16 | 911 |
} |
912 |
||
913 |
if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) { |
|
914 |
// This size isn't set. |
|
915 |
continue; |
|
916 |
} |
|
917 |
||
918 |
if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) { |
|
919 |
$size_data['crop'] = $additional_sizes[ $size_name ]['crop']; |
|
920 |
} else { |
|
921 |
$size_data['crop'] = get_option( "{$size_name}_crop" ); |
|
922 |
} |
|
923 |
||
924 |
if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) { |
|
925 |
$size_data['crop'] = (bool) $size_data['crop']; |
|
926 |
} |
|
927 |
||
928 |
$all_sizes[ $size_name ] = $size_data; |
|
929 |
} |
|
930 |
||
931 |
return $all_sizes; |
|
932 |
} |
|
933 |
||
934 |
/** |
|
935 |
* Retrieves an image to represent an attachment. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
936 |
* |
0 | 937 |
* @since 2.5.0 |
938 |
* |
|
5 | 939 |
* @param int $attachment_id Image attachment ID. |
18 | 940 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
941 |
* width and height values in pixels (in that order). Default 'thumbnail'. |
|
16 | 942 |
* @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. |
943 |
* @return array|false { |
|
944 |
* Array of image data, or boolean false if no image is available. |
|
945 |
* |
|
946 |
* @type string $0 Image source URL. |
|
947 |
* @type int $1 Image width in pixels. |
|
948 |
* @type int $2 Image height in pixels. |
|
949 |
* @type bool $3 Whether the image is a resized image. |
|
950 |
* } |
|
0 | 951 |
*/ |
5 | 952 |
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) { |
16 | 953 |
// 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
|
954 |
$image = image_downsize( $attachment_id, $size ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
if ( ! $image ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
$src = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
|
16 | 958 |
if ( $icon ) { |
959 |
$src = wp_mime_type_icon( $attachment_id ); |
|
960 |
||
961 |
if ( $src ) { |
|
962 |
/** This filter is documented in wp-includes/post.php */ |
|
963 |
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); |
|
964 |
||
965 |
$src_file = $icon_dir . '/' . wp_basename( $src ); |
|
18 | 966 |
list( $width, $height ) = wp_getimagesize( $src_file ); |
16 | 967 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
if ( $src && $width && $height ) { |
16 | 971 |
$image = array( $src, $width, $height, false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
} |
0 | 973 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
/** |
16 | 975 |
* Filters the attachment image source result. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
* |
16 | 979 |
* @param array|false $image { |
980 |
* Array of image data, or boolean false if no image is available. |
|
981 |
* |
|
982 |
* @type string $0 Image source URL. |
|
983 |
* @type int $1 Image width in pixels. |
|
984 |
* @type int $2 Image height in pixels. |
|
985 |
* @type bool $3 Whether the image is a resized image. |
|
986 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
* @param int $attachment_id Image attachment ID. |
18 | 988 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
989 |
* an array of width and height values in pixels (in that order). |
|
16 | 990 |
* @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
|
991 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); |
0 | 993 |
} |
994 |
||
995 |
/** |
|
18 | 996 |
* Get an HTML img element representing an image attachment. |
0 | 997 |
* |
5 | 998 |
* While `$size` will accept an array, it is better to register a size with |
0 | 999 |
* add_image_size() so that a cropped version is generated. It's much more |
1000 |
* efficient than having to find the closest-sized image and then having the |
|
1001 |
* browser scale down the image. |
|
1002 |
* |
|
1003 |
* @since 2.5.0 |
|
16 | 1004 |
* @since 4.4.0 The `$srcset` and `$sizes` attributes were added. |
1005 |
* @since 5.5.0 The `$loading` attribute was added. |
|
0 | 1006 |
* |
5 | 1007 |
* @param int $attachment_id Image attachment ID. |
18 | 1008 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
1009 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
5 | 1010 |
* @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
16 | 1011 |
* @param string|array $attr { |
1012 |
* Optional. Attributes for the image markup. |
|
1013 |
* |
|
1014 |
* @type string $src Image attachment URL. |
|
1015 |
* @type string $class CSS class name or space-separated list of classes. |
|
1016 |
* Default `attachment-$size_class size-$size_class`, |
|
1017 |
* where `$size_class` is the image size being requested. |
|
1018 |
* @type string $alt Image description for the alt attribute. |
|
1019 |
* @type string $srcset The 'srcset' attribute value. |
|
1020 |
* @type string $sizes The 'sizes' attribute value. |
|
1021 |
* @type string|false $loading The 'loading' attribute value. Passing a value of false |
|
1022 |
* will result in the attribute being omitted for the image. |
|
1023 |
* Defaults to 'lazy', depending on wp_lazy_loading_enabled(). |
|
1024 |
* } |
|
0 | 1025 |
* @return string HTML img element or empty string on failure. |
1026 |
*/ |
|
9 | 1027 |
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) { |
1028 |
$html = ''; |
|
1029 |
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
|
16 | 1030 |
|
0 | 1031 |
if ( $image ) { |
16 | 1032 |
list( $src, $width, $height ) = $image; |
1033 |
||
1034 |
$attachment = get_post( $attachment_id ); |
|
1035 |
$hwstring = image_hwstring( $width, $height ); |
|
1036 |
$size_class = $size; |
|
1037 |
||
5 | 1038 |
if ( is_array( $size_class ) ) { |
18 | 1039 |
$size_class = implode( 'x', $size_class ); |
5 | 1040 |
} |
16 | 1041 |
|
0 | 1042 |
$default_attr = array( |
9 | 1043 |
'src' => $src, |
1044 |
'class' => "attachment-$size_class size-$size_class", |
|
1045 |
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), |
|
0 | 1046 |
); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1047 |
|
16 | 1048 |
// Add `loading` attribute. |
1049 |
if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) { |
|
19 | 1050 |
$default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' ); |
16 | 1051 |
} |
1052 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
$attr = wp_parse_args( $attr, $default_attr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1054 |
|
16 | 1055 |
// If the default value of `lazy` for the `loading` attribute is overridden |
1056 |
// to omit the attribute for this image, ensure it is not included. |
|
1057 |
if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) { |
|
1058 |
unset( $attr['loading'] ); |
|
1059 |
} |
|
1060 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1061 |
// Generate 'srcset' and 'sizes' if not already present. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1062 |
if ( empty( $attr['srcset'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1063 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1064 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1065 |
if ( is_array( $image_meta ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1066 |
$size_array = array( absint( $width ), absint( $height ) ); |
9 | 1067 |
$srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); |
1068 |
$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
|
1069 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1070 |
if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1071 |
$attr['srcset'] = $srcset; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1072 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1073 |
if ( empty( $attr['sizes'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1074 |
$attr['sizes'] = $sizes; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1075 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1076 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1078 |
} |
5 | 1079 |
|
1080 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1081 |
* Filters the list of attachment image attributes. |
5 | 1082 |
* |
1083 |
* @since 2.8.0 |
|
1084 |
* |
|
18 | 1085 |
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. |
16 | 1086 |
* See wp_get_attachment_image(). |
5 | 1087 |
* @param WP_Post $attachment Image attachment post. |
18 | 1088 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
1089 |
* an array of width and height values in pixels (in that order). |
|
5 | 1090 |
*/ |
1091 |
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); |
|
16 | 1092 |
|
0 | 1093 |
$attr = array_map( 'esc_attr', $attr ); |
9 | 1094 |
$html = rtrim( "<img $hwstring" ); |
16 | 1095 |
|
0 | 1096 |
foreach ( $attr as $name => $value ) { |
1097 |
$html .= " $name=" . '"' . $value . '"'; |
|
1098 |
} |
|
16 | 1099 |
|
0 | 1100 |
$html .= ' />'; |
1101 |
} |
|
1102 |
||
18 | 1103 |
/** |
1104 |
* HTML img element representing an image attachment. |
|
1105 |
* |
|
1106 |
* @since 5.6.0 |
|
1107 |
* |
|
1108 |
* @param string $html HTML img element or empty string on failure. |
|
1109 |
* @param int $attachment_id Image attachment ID. |
|
1110 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
1111 |
* an array of width and height values in pixels (in that order). |
|
1112 |
* @param bool $icon Whether the image should be treated as an icon. |
|
1113 |
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. |
|
1114 |
* See wp_get_attachment_image(). |
|
1115 |
*/ |
|
1116 |
return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr ); |
|
0 | 1117 |
} |
1118 |
||
1119 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
* Get the URL of an image attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1122 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1123 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1124 |
* @param int $attachment_id Image attachment ID. |
18 | 1125 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
1126 |
* 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
|
1127 |
* @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
18 | 1128 |
* @return string|false Attachment URL or false if no image is available. If `$size` does not match |
1129 |
* 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
|
1130 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
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
|
1132 |
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
19 | 1133 |
return isset( $image[0] ) ? $image[0] : false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
} |
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 |
* Get the attachment path relative to the upload directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
* @since 4.4.1 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1140 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1141 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
* @param string $file Attachment file name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
* @return string Attachment path relative to the upload directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1144 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
function _wp_get_attachment_relative_path( $file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1146 |
$dirname = dirname( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1148 |
if ( '.' === $dirname ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1149 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1151 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1152 |
if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) { |
16 | 1153 |
// 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
|
1154 |
$dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
$dirname = ltrim( $dirname, '/' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
return $dirname; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1160 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1161 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1162 |
* Get the image size as array from its meta data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1163 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1164 |
* Used for responsive images. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1165 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1167 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1168 |
* |
18 | 1169 |
* @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
|
1170 |
* @param array $image_meta The image meta data. |
18 | 1171 |
* @return array|false { |
1172 |
* Array of width and height or false if the size isn't present in the meta data. |
|
1173 |
* |
|
1174 |
* @type int $0 Image width. |
|
1175 |
* @type int $1 Image height. |
|
1176 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
function _wp_get_image_size_from_meta( $size_name, $image_meta ) { |
16 | 1179 |
if ( 'full' === $size_name ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
return array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
absint( $image_meta['width'] ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
absint( $image_meta['height'] ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
); |
9 | 1184 |
} elseif ( ! empty( $image_meta['sizes'][ $size_name ] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
return array( |
9 | 1186 |
absint( $image_meta['sizes'][ $size_name ]['width'] ), |
1187 |
absint( $image_meta['sizes'][ $size_name ]['height'] ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
} |
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 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1193 |
|
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 |
* 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
|
1196 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
* @since 4.4.0 |
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 |
* @see wp_calculate_image_srcset() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
* @param int $attachment_id Image attachment ID. |
18 | 1202 |
* @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
|
1203 |
* width and height values in pixels (in that order). Default 'medium'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
* @param array $image_meta Optional. 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
|
1205 |
* Default null. |
18 | 1206 |
* @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
|
1207 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { |
16 | 1209 |
$image = wp_get_attachment_image_src( $attachment_id, $size ); |
1210 |
||
1211 |
if ( ! $image ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1213 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
if ( ! is_array( $image_meta ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
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 |
|
9 | 1219 |
$image_src = $image[0]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
$size_array = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1221 |
absint( $image[1] ), |
9 | 1222 |
absint( $image[2] ), |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
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
|
1226 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
* 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
|
1230 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
* |
16 | 1233 |
* @param int[] $size_array { |
1234 |
* An array of width and height values. |
|
1235 |
* |
|
1236 |
* @type int $0 The width in pixels. |
|
1237 |
* @type int $1 The height in pixels. |
|
1238 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
* @param string $image_src The 'src' of the image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
16 | 1241 |
* @param int $attachment_id Optional. The image attachment ID. Default 0. |
18 | 1242 |
* @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
|
1243 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1244 |
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
|
1245 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
* Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1247 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
* @since 4.5.0 |
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 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
16 | 1251 |
* @param int[] $size_array { |
1252 |
* An array of requested width and height values. |
|
1253 |
* |
|
1254 |
* @type int $0 The width in pixels. |
|
1255 |
* @type int $1 The height in pixels. |
|
1256 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1257 |
* @param string $image_src The 'src' of the image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
* @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
|
1259 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1260 |
$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
|
1261 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
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
|
1263 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1265 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1266 |
$image_sizes = $image_meta['sizes']; |
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 |
// Get the width and height of the image. |
9 | 1269 |
$image_width = (int) $size_array[0]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1270 |
$image_height = (int) $size_array[1]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1271 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
// Bail early if error/no width. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
if ( $image_width < 1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1275 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
$image_basename = wp_basename( $image_meta['file'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
* 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
|
1281 |
* 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
|
1282 |
* 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
|
1283 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1284 |
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
|
1285 |
$image_sizes[] = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1286 |
'width' => $image_meta['width'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
'height' => $image_meta['height'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1288 |
'file' => $image_basename, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1289 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
} elseif ( strpos( $image_src, $image_meta['file'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1293 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
// 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
|
1295 |
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1296 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
if ( $dirname ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
$dirname = trailingslashit( $dirname ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
|
9 | 1301 |
$upload_dir = wp_get_upload_dir(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
$image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname; |
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 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
* 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
|
1306 |
* (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
|
1307 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1308 |
if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
$image_baseurl = set_url_scheme( $image_baseurl, 'https' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1310 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1311 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1312 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1313 |
* 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
|
1314 |
* 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
|
1315 |
* out images that are leftovers from previous versions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1316 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
$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
|
1318 |
|
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 |
* 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
|
1321 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1322 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1323 |
* |
16 | 1324 |
* @param int $max_width The maximum image width to be included in the 'srcset'. Default '2048'. |
1325 |
* @param int[] $size_array { |
|
1326 |
* An array of requested width and height values. |
|
1327 |
* |
|
1328 |
* @type int $0 The width in pixels. |
|
1329 |
* @type int $1 The height in pixels. |
|
1330 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1331 |
*/ |
16 | 1332 |
$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
|
1333 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1334 |
// Array to hold URL candidates. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
$sources = array(); |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
* 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
|
1339 |
* 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
|
1340 |
* an incorrect image. See #35045. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1342 |
$src_matched = false; |
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 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1345 |
* 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
|
1346 |
* versions of the same edit. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1347 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
foreach ( $image_sizes as $image ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1349 |
$is_src = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1350 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1351 |
// Check if image meta isn't corrupted. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1352 |
if ( ! is_array( $image ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1355 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
// If the file name is part of the `src`, we've confirmed a match. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1357 |
if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) { |
16 | 1358 |
$src_matched = true; |
1359 |
$is_src = true; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
// Filter out images that are from previous edits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
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
|
1364 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
* 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
|
1369 |
* that file is in the 'src' attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
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
|
1372 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1373 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1374 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
// 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
|
1376 |
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
|
1377 |
// 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
|
1378 |
$source = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
'url' => $image_baseurl . $image['file'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
'descriptor' => 'w', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
'value' => $image['width'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
); |
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 |
// 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
|
1385 |
if ( $is_src ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
$sources = array( $image['width'] => $source ) + $sources; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
$sources[ $image['width'] ] = $source; |
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 |
} |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
* Filters an image's 'srcset' sources. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
* @param array $sources { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
* 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
|
1400 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
* @type array $width { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
* @type string $url The URL of an image source. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
* @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
|
1404 |
* either 'w' or 'x'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
* @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
|
1406 |
* pixel density value if paired with an 'x' descriptor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
* } |
16 | 1409 |
* @param array $size_array { |
1410 |
* An array of requested width and height values. |
|
1411 |
* |
|
1412 |
* @type int $0 The width in pixels. |
|
1413 |
* @type int $1 The height in pixels. |
|
1414 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
* @param string $image_src The 'src' of the image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
* @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
|
1417 |
* @param int $attachment_id Image attachment ID or 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
$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
|
1420 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
// 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
|
1422 |
if ( ! $src_matched || ! is_array( $sources ) || count( $sources ) < 2 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
$srcset = ''; |
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 |
foreach ( $sources as $source ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
$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
|
1430 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1431 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1432 |
return rtrim( $srcset, ', ' ); |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
* 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
|
1437 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
* @since 4.4.0 |
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 |
* @see wp_calculate_image_sizes() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1441 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1442 |
* @param int $attachment_id Image attachment ID. |
18 | 1443 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
1444 |
* width and height values in pixels (in that order). Default 'medium'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1445 |
* @param array $image_meta Optional. 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
|
1446 |
* Default null. |
18 | 1447 |
* @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
|
1448 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1449 |
function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { |
16 | 1450 |
$image = wp_get_attachment_image_src( $attachment_id, $size ); |
1451 |
||
1452 |
if ( ! $image ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
} |
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 |
if ( ! is_array( $image_meta ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1457 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
|
9 | 1460 |
$image_src = $image[0]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1461 |
$size_array = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1462 |
absint( $image[1] ), |
9 | 1463 |
absint( $image[2] ), |
7
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 |
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
|
1467 |
} |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1470 |
* Creates a 'sizes' attribute value for an image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1471 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1472 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1473 |
* |
18 | 1474 |
* @param string|int[] $size Image size. Accepts any registered image size name, or an array of |
1475 |
* 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
|
1476 |
* @param string $image_src Optional. The URL to the image file. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1477 |
* @param array $image_meta Optional. 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
|
1478 |
* Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1479 |
* @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
|
1480 |
* is needed when using the image size name as argument for `$size`. Default 0. |
18 | 1481 |
* @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
|
1482 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1483 |
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
|
1484 |
$width = 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1485 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1486 |
if ( is_array( $size ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1487 |
$width = absint( $size[0] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
} elseif ( is_string( $size ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1489 |
if ( ! $image_meta && $attachment_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1493 |
if ( is_array( $image_meta ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1494 |
$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
|
1495 |
if ( $size_array ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1496 |
$width = absint( $size_array[0] ); |
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 |
} |
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 |
if ( ! $width ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1502 |
return false; |
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 |
// Setup the default 'sizes' attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1506 |
$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
|
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 |
* Filters the output of 'wp_calculate_image_sizes()'. |
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 |
* @param string $sizes A source size value for use in a 'sizes' attribute. |
18 | 1514 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
1515 |
* 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
|
1516 |
* @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
|
1517 |
* @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
|
1518 |
* @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
|
1519 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1520 |
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
|
1521 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1522 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1523 |
/** |
16 | 1524 |
* Determines if the image meta data is for the image source file. |
1525 |
* |
|
1526 |
* The image meta data is retrieved by attachment post ID. In some cases the post IDs may change. |
|
1527 |
* For example when the website is exported and imported at another website. Then the |
|
1528 |
* attachment post IDs that are in post_content for the exported website may not match |
|
1529 |
* the same attachments at the new website. |
|
1530 |
* |
|
1531 |
* @since 5.5.0 |
|
1532 |
* |
|
1533 |
* @param string $image_location The full path or URI to the image file. |
|
1534 |
* @param array $image_meta The attachment meta data as returned by 'wp_get_attachment_metadata()'. |
|
1535 |
* @param int $attachment_id Optional. The image attachment ID. Default 0. |
|
1536 |
* @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
|
1537 |
*/ |
16 | 1538 |
function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) { |
1539 |
$match = false; |
|
1540 |
||
1541 |
// Ensure the $image_meta is valid. |
|
1542 |
if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) { |
|
1543 |
// Remove quiery args if image URI. |
|
1544 |
list( $image_location ) = explode( '?', $image_location ); |
|
1545 |
||
1546 |
// Check if the relative image path from the image meta is at the end of $image_location. |
|
1547 |
if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) { |
|
1548 |
$match = true; |
|
1549 |
} else { |
|
1550 |
// Retrieve the uploads sub-directory from the full size image. |
|
1551 |
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); |
|
1552 |
||
1553 |
if ( $dirname ) { |
|
1554 |
$dirname = trailingslashit( $dirname ); |
|
1555 |
} |
|
1556 |
||
1557 |
if ( ! empty( $image_meta['original_image'] ) ) { |
|
1558 |
$relative_path = $dirname . $image_meta['original_image']; |
|
1559 |
||
1560 |
if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) { |
|
1561 |
$match = true; |
|
1562 |
} |
|
1563 |
} |
|
1564 |
||
1565 |
if ( ! $match && ! empty( $image_meta['sizes'] ) ) { |
|
1566 |
foreach ( $image_meta['sizes'] as $image_size_data ) { |
|
1567 |
$relative_path = $dirname . $image_size_data['file']; |
|
1568 |
||
1569 |
if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) { |
|
1570 |
$match = true; |
|
1571 |
break; |
|
1572 |
} |
|
1573 |
} |
|
1574 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1575 |
} |
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 |
|
16 | 1578 |
/** |
18 | 1579 |
* Filters whether an image path or URI matches image meta. |
16 | 1580 |
* |
1581 |
* @since 5.5.0 |
|
1582 |
* |
|
1583 |
* @param bool $match Whether the image relative path from the image meta |
|
1584 |
* matches the end of the URI or path to the image file. |
|
1585 |
* @param string $image_location Full path or URI to the tested image file. |
|
1586 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
1587 |
* @param int $attachment_id The image attachment ID or 0 if not supplied. |
|
1588 |
*/ |
|
1589 |
return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id ); |
|
1590 |
} |
|
1591 |
||
1592 |
/** |
|
1593 |
* Determines an image's width and height dimensions based on the source file. |
|
1594 |
* |
|
1595 |
* @since 5.5.0 |
|
1596 |
* |
|
1597 |
* @param string $image_src The image source file. |
|
1598 |
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
|
1599 |
* @param int $attachment_id Optional. The image attachment ID. Default 0. |
|
1600 |
* @return array|false Array with first element being the width and second element being the height, |
|
1601 |
* or false if dimensions cannot be determined. |
|
1602 |
*/ |
|
1603 |
function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) { |
|
18 | 1604 |
$dimensions = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1605 |
|
16 | 1606 |
// Is it a full size image? |
18 | 1607 |
if ( |
1608 |
isset( $image_meta['file'] ) && |
|
1609 |
strpos( $image_src, wp_basename( $image_meta['file'] ) ) !== false |
|
1610 |
) { |
|
1611 |
$dimensions = array( |
|
16 | 1612 |
(int) $image_meta['width'], |
1613 |
(int) $image_meta['height'], |
|
1614 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1615 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1616 |
|
18 | 1617 |
if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) { |
16 | 1618 |
$src_filename = wp_basename( $image_src ); |
1619 |
||
1620 |
foreach ( $image_meta['sizes'] as $image_size_data ) { |
|
1621 |
if ( $src_filename === $image_size_data['file'] ) { |
|
18 | 1622 |
$dimensions = array( |
16 | 1623 |
(int) $image_size_data['width'], |
1624 |
(int) $image_size_data['height'], |
|
1625 |
); |
|
18 | 1626 |
|
1627 |
break; |
|
16 | 1628 |
} |
1629 |
} |
|
1630 |
} |
|
1631 |
||
18 | 1632 |
/** |
1633 |
* Filters the 'wp_image_src_get_dimensions' value. |
|
1634 |
* |
|
1635 |
* @since 5.7.0 |
|
1636 |
* |
|
1637 |
* @param array|false $dimensions Array with first element being the width |
|
1638 |
* and second element being the height, or |
|
1639 |
* false if dimensions could not be determined. |
|
1640 |
* @param string $image_src The image source file. |
|
1641 |
* @param array $image_meta The image meta data as returned by |
|
1642 |
* 'wp_get_attachment_metadata()'. |
|
1643 |
* @param int $attachment_id The image attachment ID. Default 0. |
|
1644 |
*/ |
|
1645 |
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
|
1646 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1647 |
|
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 |
* 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
|
1650 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1651 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1652 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1653 |
* @see wp_calculate_image_srcset() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1654 |
* @see wp_calculate_image_sizes() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1655 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1656 |
* @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
|
1657 |
* @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
|
1658 |
* @param int $attachment_id Image attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1659 |
* @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
|
1660 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1661 |
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
|
1662 |
// Ensure the image meta exists. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1663 |
if ( empty( $image_meta['sizes'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1664 |
return $image; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1665 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1666 |
|
9 | 1667 |
$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
|
1668 |
list( $image_src ) = explode( '?', $image_src ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1669 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1670 |
// 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
|
1671 |
if ( ! $image_src ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1672 |
return $image; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1673 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1674 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1675 |
// Bail early if an image has been inserted and later edited. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1676 |
if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1677 |
strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1678 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1679 |
return $image; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1680 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1681 |
|
9 | 1682 |
$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
|
1683 |
$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
|
1684 |
|
16 | 1685 |
if ( $width && $height ) { |
1686 |
$size_array = array( $width, $height ); |
|
1687 |
} else { |
|
1688 |
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); |
|
1689 |
if ( ! $size_array ) { |
|
1690 |
return $image; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1691 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1692 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1693 |
|
16 | 1694 |
$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
|
1695 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
if ( $srcset ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1697 |
// Check if there is already a 'sizes' attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
$sizes = strpos( $image, ' sizes=' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1699 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1700 |
if ( ! $sizes ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1701 |
$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
|
1702 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1703 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1704 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1705 |
if ( $srcset && $sizes ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1706 |
// Format the 'srcset' and 'sizes' string and escape attributes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1707 |
$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1708 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1709 |
if ( is_string( $sizes ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1710 |
$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1711 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1712 |
|
16 | 1713 |
// Add the srcset and sizes attributes to the image markup. |
1714 |
return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image ); |
|
1715 |
} |
|
1716 |
||
1717 |
return $image; |
|
1718 |
} |
|
1719 |
||
1720 |
/** |
|
18 | 1721 |
* Determines whether to add the `loading` attribute to the specified tag in the specified context. |
16 | 1722 |
* |
1723 |
* @since 5.5.0 |
|
18 | 1724 |
* @since 5.7.0 Now returns `true` by default for `iframe` tags. |
16 | 1725 |
* |
1726 |
* @param string $tag_name The tag name. |
|
18 | 1727 |
* @param string $context Additional context, like the current filter name |
1728 |
* or the function name from where this was called. |
|
16 | 1729 |
* @return bool Whether to add the attribute. |
1730 |
*/ |
|
1731 |
function wp_lazy_loading_enabled( $tag_name, $context ) { |
|
18 | 1732 |
// By default add to all 'img' and 'iframe' tags. |
16 | 1733 |
// See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading |
18 | 1734 |
// See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading |
1735 |
$default = ( 'img' === $tag_name || 'iframe' === $tag_name ); |
|
16 | 1736 |
|
1737 |
/** |
|
1738 |
* Filters whether to add the `loading` attribute to the specified tag in the specified context. |
|
1739 |
* |
|
1740 |
* @since 5.5.0 |
|
1741 |
* |
|
1742 |
* @param bool $default Default value. |
|
1743 |
* @param string $tag_name The tag name. |
|
18 | 1744 |
* @param string $context Additional context, like the current filter name |
1745 |
* or the function name from where this was called. |
|
16 | 1746 |
*/ |
1747 |
return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context ); |
|
1748 |
} |
|
1749 |
||
1750 |
/** |
|
1751 |
* Filters specific tags in post content and modifies their markup. |
|
1752 |
* |
|
1753 |
* Modifies HTML tags in post content to include new browser and HTML technologies |
|
1754 |
* that may not have existed at the time of post creation. These modifications currently |
|
18 | 1755 |
* include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags, as well |
1756 |
* as adding `loading` attributes to `iframe` HTML tags. |
|
16 | 1757 |
* Future similar optimizations should be added/expected here. |
1758 |
* |
|
1759 |
* @since 5.5.0 |
|
18 | 1760 |
* @since 5.7.0 Now supports adding `loading` attributes to `iframe` tags. |
16 | 1761 |
* |
1762 |
* @see wp_img_tag_add_width_and_height_attr() |
|
1763 |
* @see wp_img_tag_add_srcset_and_sizes_attr() |
|
1764 |
* @see wp_img_tag_add_loading_attr() |
|
18 | 1765 |
* @see wp_iframe_tag_add_loading_attr() |
16 | 1766 |
* |
1767 |
* @param string $content The HTML content to be filtered. |
|
1768 |
* @param string $context Optional. Additional context to pass to the filters. |
|
1769 |
* Defaults to `current_filter()` when not set. |
|
1770 |
* @return string Converted content with images modified. |
|
1771 |
*/ |
|
1772 |
function wp_filter_content_tags( $content, $context = null ) { |
|
1773 |
if ( null === $context ) { |
|
1774 |
$context = current_filter(); |
|
1775 |
} |
|
1776 |
||
18 | 1777 |
$add_img_loading_attr = wp_lazy_loading_enabled( 'img', $context ); |
1778 |
$add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context ); |
|
1779 |
||
1780 |
if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) { |
|
16 | 1781 |
return $content; |
1782 |
} |
|
1783 |
||
1784 |
// List of the unique `img` tags found in $content. |
|
1785 |
$images = array(); |
|
1786 |
||
18 | 1787 |
// List of the unique `iframe` tags found in $content. |
1788 |
$iframes = array(); |
|
1789 |
||
1790 |
foreach ( $matches as $match ) { |
|
1791 |
list( $tag, $tag_name ) = $match; |
|
1792 |
||
1793 |
switch ( $tag_name ) { |
|
1794 |
case 'img': |
|
1795 |
if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $class_id ) ) { |
|
1796 |
$attachment_id = absint( $class_id[1] ); |
|
1797 |
||
1798 |
if ( $attachment_id ) { |
|
1799 |
// If exactly the same image tag is used more than once, overwrite it. |
|
1800 |
// All identical tags will be replaced later with 'str_replace()'. |
|
1801 |
$images[ $tag ] = $attachment_id; |
|
1802 |
break; |
|
1803 |
} |
|
1804 |
} |
|
1805 |
$images[ $tag ] = 0; |
|
1806 |
break; |
|
1807 |
case 'iframe': |
|
1808 |
$iframes[ $tag ] = 0; |
|
1809 |
break; |
|
16 | 1810 |
} |
1811 |
} |
|
1812 |
||
1813 |
// Reduce the array to unique attachment IDs. |
|
1814 |
$attachment_ids = array_unique( array_filter( array_values( $images ) ) ); |
|
1815 |
||
1816 |
if ( count( $attachment_ids ) > 1 ) { |
|
1817 |
/* |
|
1818 |
* Warm the object cache with post and meta information for all found |
|
1819 |
* images to avoid making individual database calls. |
|
1820 |
*/ |
|
1821 |
_prime_post_caches( $attachment_ids, false, true ); |
|
1822 |
} |
|
1823 |
||
19 | 1824 |
// Iterate through the matches in order of occurrence as it is relevant for whether or not to lazy-load. |
1825 |
foreach ( $matches as $match ) { |
|
1826 |
// Filter an image match. |
|
1827 |
if ( isset( $images[ $match[0] ] ) ) { |
|
1828 |
$filtered_image = $match[0]; |
|
1829 |
$attachment_id = $images[ $match[0] ]; |
|
1830 |
||
1831 |
// Add 'width' and 'height' attributes if applicable. |
|
1832 |
if ( $attachment_id > 0 && false === strpos( $filtered_image, ' width=' ) && false === strpos( $filtered_image, ' height=' ) ) { |
|
1833 |
$filtered_image = wp_img_tag_add_width_and_height_attr( $filtered_image, $context, $attachment_id ); |
|
1834 |
} |
|
1835 |
||
1836 |
// Add 'srcset' and 'sizes' attributes if applicable. |
|
1837 |
if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) { |
|
1838 |
$filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id ); |
|
1839 |
} |
|
1840 |
||
1841 |
// Add 'loading' attribute if applicable. |
|
1842 |
if ( $add_img_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { |
|
1843 |
$filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); |
|
1844 |
} |
|
1845 |
||
1846 |
/** |
|
1847 |
* Filters an img tag within the content for a given context. |
|
1848 |
* |
|
1849 |
* @since 6.0.0 |
|
1850 |
* |
|
1851 |
* @param string $filtered_image Full img tag with attributes that will replace the source img tag. |
|
1852 |
* @param string $context Additional context, like the current filter name or the function name from where this was called. |
|
1853 |
* @param int $attachment_id The image attachment ID. May be 0 in case the image is not an attachment. |
|
1854 |
*/ |
|
1855 |
$filtered_image = apply_filters( 'wp_content_img_tag', $filtered_image, $context, $attachment_id ); |
|
1856 |
||
1857 |
if ( $filtered_image !== $match[0] ) { |
|
1858 |
$content = str_replace( $match[0], $filtered_image, $content ); |
|
1859 |
} |
|
1860 |
||
1861 |
/* |
|
1862 |
* Unset image lookup to not run the same logic again unnecessarily if the same image tag is used more than |
|
1863 |
* once in the same blob of content. |
|
1864 |
*/ |
|
1865 |
unset( $images[ $match[0] ] ); |
|
16 | 1866 |
} |
1867 |
||
19 | 1868 |
// Filter an iframe match. |
1869 |
if ( isset( $iframes[ $match[0] ] ) ) { |
|
1870 |
$filtered_iframe = $match[0]; |
|
1871 |
||
1872 |
// Add 'loading' attribute if applicable. |
|
1873 |
if ( $add_iframe_loading_attr && false === strpos( $filtered_iframe, ' loading=' ) ) { |
|
1874 |
$filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context ); |
|
1875 |
} |
|
1876 |
||
1877 |
if ( $filtered_iframe !== $match[0] ) { |
|
1878 |
$content = str_replace( $match[0], $filtered_iframe, $content ); |
|
1879 |
} |
|
1880 |
||
1881 |
/* |
|
1882 |
* Unset iframe lookup to not run the same logic again unnecessarily if the same iframe tag is used more |
|
1883 |
* than once in the same blob of content. |
|
1884 |
*/ |
|
1885 |
unset( $iframes[ $match[0] ] ); |
|
18 | 1886 |
} |
1887 |
} |
|
1888 |
||
16 | 1889 |
return $content; |
1890 |
} |
|
1891 |
||
1892 |
/** |
|
1893 |
* Adds `loading` attribute to an `img` HTML tag. |
|
1894 |
* |
|
1895 |
* @since 5.5.0 |
|
1896 |
* |
|
1897 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
1898 |
* @param string $context Additional context to pass to the filters. |
|
1899 |
* @return string Converted `img` tag with `loading` attribute added. |
|
1900 |
*/ |
|
1901 |
function wp_img_tag_add_loading_attr( $image, $context ) { |
|
19 | 1902 |
// Get loading attribute value to use. This must occur before the conditional check below so that even images that |
1903 |
// are ineligible for being lazy-loaded are considered. |
|
1904 |
$value = wp_get_loading_attr_default( $context ); |
|
1905 |
||
18 | 1906 |
// Images should have source and dimension attributes for the `loading` attribute to be added. |
1907 |
if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) { |
|
1908 |
return $image; |
|
1909 |
} |
|
1910 |
||
16 | 1911 |
/** |
18 | 1912 |
* Filters the `loading` attribute value to add to an image. Default `lazy`. |
16 | 1913 |
* |
1914 |
* Returning `false` or an empty string will not add the attribute. |
|
1915 |
* Returning `true` will add the default value. |
|
1916 |
* |
|
1917 |
* @since 5.5.0 |
|
1918 |
* |
|
1919 |
* @param string|bool $value The `loading` attribute value. Returning a falsey value will result in |
|
19 | 1920 |
* the attribute being omitted for the image. |
16 | 1921 |
* @param string $image The HTML `img` tag to be filtered. |
1922 |
* @param string $context Additional context about how the function was called or where the img tag is. |
|
1923 |
*/ |
|
19 | 1924 |
$value = apply_filters( 'wp_img_tag_add_loading_attr', $value, $image, $context ); |
16 | 1925 |
|
1926 |
if ( $value ) { |
|
1927 |
if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { |
|
1928 |
$value = 'lazy'; |
|
1929 |
} |
|
1930 |
||
1931 |
return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image ); |
|
1932 |
} |
|
1933 |
||
1934 |
return $image; |
|
1935 |
} |
|
1936 |
||
1937 |
/** |
|
1938 |
* Adds `width` and `height` attributes to an `img` HTML tag. |
|
1939 |
* |
|
1940 |
* @since 5.5.0 |
|
1941 |
* |
|
1942 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
1943 |
* @param string $context Additional context to pass to the filters. |
|
1944 |
* @param int $attachment_id Image attachment ID. |
|
1945 |
* @return string Converted 'img' element with 'width' and 'height' attributes added. |
|
1946 |
*/ |
|
1947 |
function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id ) { |
|
1948 |
$image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; |
|
1949 |
list( $image_src ) = explode( '?', $image_src ); |
|
1950 |
||
1951 |
// Return early if we couldn't get the image source. |
|
1952 |
if ( ! $image_src ) { |
|
1953 |
return $image; |
|
1954 |
} |
|
1955 |
||
1956 |
/** |
|
1957 |
* Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`. |
|
1958 |
* |
|
1959 |
* Returning anything else than `true` will not add the attributes. |
|
1960 |
* |
|
1961 |
* @since 5.5.0 |
|
1962 |
* |
|
1963 |
* @param bool $value The filtered value, defaults to `true`. |
|
1964 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
1965 |
* @param string $context Additional context about how the function was called or where the img tag is. |
|
1966 |
* @param int $attachment_id The image attachment ID. |
|
1967 |
*/ |
|
1968 |
$add = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id ); |
|
1969 |
||
1970 |
if ( true === $add ) { |
|
1971 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
1972 |
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); |
|
1973 |
||
1974 |
if ( $size_array ) { |
|
1975 |
$hw = trim( image_hwstring( $size_array[0], $size_array[1] ) ); |
|
1976 |
return str_replace( '<img', "<img {$hw}", $image ); |
|
1977 |
} |
|
1978 |
} |
|
1979 |
||
1980 |
return $image; |
|
1981 |
} |
|
1982 |
||
1983 |
/** |
|
1984 |
* Adds `srcset` and `sizes` attributes to an existing `img` HTML tag. |
|
1985 |
* |
|
1986 |
* @since 5.5.0 |
|
1987 |
* |
|
1988 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
1989 |
* @param string $context Additional context to pass to the filters. |
|
1990 |
* @param int $attachment_id Image attachment ID. |
|
1991 |
* @return string Converted 'img' element with 'loading' attribute added. |
|
1992 |
*/ |
|
1993 |
function wp_img_tag_add_srcset_and_sizes_attr( $image, $context, $attachment_id ) { |
|
1994 |
/** |
|
1995 |
* Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`. |
|
1996 |
* |
|
1997 |
* Returning anything else than `true` will not add the attributes. |
|
1998 |
* |
|
1999 |
* @since 5.5.0 |
|
2000 |
* |
|
2001 |
* @param bool $value The filtered value, defaults to `true`. |
|
2002 |
* @param string $image The HTML `img` tag where the attribute should be added. |
|
2003 |
* @param string $context Additional context about how the function was called or where the img tag is. |
|
2004 |
* @param int $attachment_id The image attachment ID. |
|
2005 |
*/ |
|
2006 |
$add = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id ); |
|
2007 |
||
2008 |
if ( true === $add ) { |
|
2009 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
2010 |
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
|
2011 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2012 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2013 |
return $image; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2014 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2015 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2016 |
/** |
18 | 2017 |
* Adds `loading` attribute to an `iframe` HTML tag. |
2018 |
* |
|
2019 |
* @since 5.7.0 |
|
2020 |
* |
|
2021 |
* @param string $iframe The HTML `iframe` tag where the attribute should be added. |
|
2022 |
* @param string $context Additional context to pass to the filters. |
|
2023 |
* @return string Converted `iframe` tag with `loading` attribute added. |
|
2024 |
*/ |
|
2025 |
function wp_iframe_tag_add_loading_attr( $iframe, $context ) { |
|
2026 |
// Iframes with fallback content (see `wp_filter_oembed_result()`) should not be lazy-loaded because they are |
|
2027 |
// visually hidden initially. |
|
2028 |
if ( false !== strpos( $iframe, ' data-secret="' ) ) { |
|
2029 |
return $iframe; |
|
2030 |
} |
|
2031 |
||
19 | 2032 |
// Get loading attribute value to use. This must occur before the conditional check below so that even iframes that |
2033 |
// are ineligible for being lazy-loaded are considered. |
|
2034 |
$value = wp_get_loading_attr_default( $context ); |
|
2035 |
||
18 | 2036 |
// Iframes should have source and dimension attributes for the `loading` attribute to be added. |
2037 |
if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) { |
|
2038 |
return $iframe; |
|
2039 |
} |
|
2040 |
||
2041 |
/** |
|
2042 |
* Filters the `loading` attribute value to add to an iframe. Default `lazy`. |
|
2043 |
* |
|
2044 |
* Returning `false` or an empty string will not add the attribute. |
|
2045 |
* Returning `true` will add the default value. |
|
2046 |
* |
|
2047 |
* @since 5.7.0 |
|
2048 |
* |
|
2049 |
* @param string|bool $value The `loading` attribute value. Returning a falsey value will result in |
|
19 | 2050 |
* the attribute being omitted for the iframe. |
18 | 2051 |
* @param string $iframe The HTML `iframe` tag to be filtered. |
2052 |
* @param string $context Additional context about how the function was called or where the iframe tag is. |
|
2053 |
*/ |
|
19 | 2054 |
$value = apply_filters( 'wp_iframe_tag_add_loading_attr', $value, $iframe, $context ); |
18 | 2055 |
|
2056 |
if ( $value ) { |
|
2057 |
if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { |
|
2058 |
$value = 'lazy'; |
|
2059 |
} |
|
2060 |
||
2061 |
return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe ); |
|
2062 |
} |
|
2063 |
||
2064 |
return $iframe; |
|
2065 |
} |
|
2066 |
||
2067 |
/** |
|
5 | 2068 |
* Adds a 'wp-post-image' class to post thumbnails. Internal use only. |
2069 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2070 |
* 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
|
2071 |
* action hooks to dynamically add/remove itself so as to only filter post thumbnails. |
0 | 2072 |
* |
5 | 2073 |
* @ignore |
0 | 2074 |
* @since 2.9.0 |
5 | 2075 |
* |
16 | 2076 |
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name. |
2077 |
* @return string[] Modified array of attributes including the new 'wp-post-image' class. |
|
0 | 2078 |
*/ |
2079 |
function _wp_post_thumbnail_class_filter( $attr ) { |
|
2080 |
$attr['class'] .= ' wp-post-image'; |
|
2081 |
return $attr; |
|
2082 |
} |
|
2083 |
||
2084 |
/** |
|
5 | 2085 |
* Adds '_wp_post_thumbnail_class_filter' callback to the 'wp_get_attachment_image_attributes' |
2086 |
* filter hook. Internal use only. |
|
0 | 2087 |
* |
5 | 2088 |
* @ignore |
0 | 2089 |
* @since 2.9.0 |
5 | 2090 |
* |
16 | 2091 |
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name. |
0 | 2092 |
*/ |
2093 |
function _wp_post_thumbnail_class_filter_add( $attr ) { |
|
2094 |
add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
|
2095 |
} |
|
2096 |
||
2097 |
/** |
|
5 | 2098 |
* Removes the '_wp_post_thumbnail_class_filter' callback from the 'wp_get_attachment_image_attributes' |
2099 |
* filter hook. Internal use only. |
|
0 | 2100 |
* |
5 | 2101 |
* @ignore |
0 | 2102 |
* @since 2.9.0 |
5 | 2103 |
* |
16 | 2104 |
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name. |
0 | 2105 |
*/ |
2106 |
function _wp_post_thumbnail_class_filter_remove( $attr ) { |
|
2107 |
remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
|
2108 |
} |
|
2109 |
||
9 | 2110 |
add_shortcode( 'wp_caption', 'img_caption_shortcode' ); |
2111 |
add_shortcode( 'caption', 'img_caption_shortcode' ); |
|
0 | 2112 |
|
2113 |
/** |
|
5 | 2114 |
* Builds the Caption shortcode output. |
0 | 2115 |
* |
2116 |
* 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
|
2117 |
* filter is {@see 'img_caption_shortcode'} and passes an empty string, the attr |
0 | 2118 |
* parameter and the content parameter values. |
2119 |
* |
|
9 | 2120 |
* The supported attributes for the shortcode are 'id', 'caption_id', 'align', |
2121 |
* 'width', 'caption', and 'class'. |
|
0 | 2122 |
* |
2123 |
* @since 2.6.0 |
|
9 | 2124 |
* @since 3.9.0 The `class` attribute was added. |
2125 |
* @since 5.1.0 The `caption_id` attribute was added. |
|
19 | 2126 |
* @since 5.9.0 The `$content` parameter default value changed from `null` to `''`. |
0 | 2127 |
* |
5 | 2128 |
* @param array $attr { |
2129 |
* Attributes of the caption shortcode. |
|
2130 |
* |
|
9 | 2131 |
* @type string $id ID of the image and caption container element, i.e. `<figure>` or `<div>`. |
2132 |
* @type string $caption_id ID of the caption element, i.e. `<figcaption>` or `<p>`. |
|
2133 |
* @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft', |
|
2134 |
* 'aligncenter', alignright', 'alignnone'. |
|
2135 |
* @type int $width The width of the caption, in pixels. |
|
2136 |
* @type string $caption The caption text. |
|
2137 |
* @type string $class Additional class name(s) added to the caption container. |
|
5 | 2138 |
* } |
19 | 2139 |
* @param string $content Optional. Shortcode content. Default empty string. |
5 | 2140 |
* @return string HTML content to display the caption. |
0 | 2141 |
*/ |
19 | 2142 |
function img_caption_shortcode( $attr, $content = '' ) { |
0 | 2143 |
// New-style shortcode with the caption inside the shortcode with the link and image tags. |
2144 |
if ( ! isset( $attr['caption'] ) ) { |
|
2145 |
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) { |
|
9 | 2146 |
$content = $matches[1]; |
0 | 2147 |
$attr['caption'] = trim( $matches[2] ); |
2148 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2149 |
} elseif ( strpos( $attr['caption'], '<' ) !== false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2150 |
$attr['caption'] = wp_kses( $attr['caption'], 'post' ); |
0 | 2151 |
} |
2152 |
||
5 | 2153 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2154 |
* Filters the default caption shortcode output. |
5 | 2155 |
* |
2156 |
* If the filtered output isn't empty, it will be used instead of generating |
|
2157 |
* the default caption template. |
|
2158 |
* |
|
2159 |
* @since 2.6.0 |
|
2160 |
* |
|
2161 |
* @see img_caption_shortcode() |
|
2162 |
* |
|
2163 |
* @param string $output The caption output. Default empty. |
|
2164 |
* @param array $attr Attributes of the caption shortcode. |
|
2165 |
* @param string $content The image element, possibly wrapped in a hyperlink. |
|
2166 |
*/ |
|
2167 |
$output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); |
|
16 | 2168 |
|
2169 |
if ( ! empty( $output ) ) { |
|
0 | 2170 |
return $output; |
9 | 2171 |
} |
2172 |
||
2173 |
$atts = shortcode_atts( |
|
2174 |
array( |
|
2175 |
'id' => '', |
|
2176 |
'caption_id' => '', |
|
2177 |
'align' => 'alignnone', |
|
2178 |
'width' => '', |
|
2179 |
'caption' => '', |
|
2180 |
'class' => '', |
|
2181 |
), |
|
2182 |
$attr, |
|
2183 |
'caption' |
|
2184 |
); |
|
0 | 2185 |
|
2186 |
$atts['width'] = (int) $atts['width']; |
|
16 | 2187 |
|
9 | 2188 |
if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) { |
0 | 2189 |
return $content; |
9 | 2190 |
} |
2191 |
||
16 | 2192 |
$id = ''; |
2193 |
$caption_id = ''; |
|
2194 |
$describedby = ''; |
|
9 | 2195 |
|
2196 |
if ( $atts['id'] ) { |
|
2197 |
$atts['id'] = sanitize_html_class( $atts['id'] ); |
|
2198 |
$id = 'id="' . esc_attr( $atts['id'] ) . '" '; |
|
2199 |
} |
|
2200 |
||
2201 |
if ( $atts['caption_id'] ) { |
|
2202 |
$atts['caption_id'] = sanitize_html_class( $atts['caption_id'] ); |
|
2203 |
} elseif ( $atts['id'] ) { |
|
2204 |
$atts['caption_id'] = 'caption-' . str_replace( '_', '-', $atts['id'] ); |
|
2205 |
} |
|
2206 |
||
2207 |
if ( $atts['caption_id'] ) { |
|
2208 |
$caption_id = 'id="' . esc_attr( $atts['caption_id'] ) . '" '; |
|
2209 |
$describedby = 'aria-describedby="' . esc_attr( $atts['caption_id'] ) . '" '; |
|
2210 |
} |
|
0 | 2211 |
|
5 | 2212 |
$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] ); |
2213 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2214 |
$html5 = current_theme_supports( 'html5', 'caption' ); |
16 | 2215 |
// 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
|
2216 |
$width = $html5 ? $atts['width'] : ( 10 + $atts['width'] ); |
0 | 2217 |
|
2218 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2219 |
* Filters the width of an image's caption. |
0 | 2220 |
* |
2221 |
* By default, the caption is 10 pixels greater than the width of the image, |
|
2222 |
* to prevent post content from running up against a floated image. |
|
2223 |
* |
|
2224 |
* @since 3.7.0 |
|
2225 |
* |
|
5 | 2226 |
* @see img_caption_shortcode() |
0 | 2227 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2228 |
* @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
|
2229 |
* return zero. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2230 |
* @param array $atts Attributes of the caption shortcode. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2231 |
* @param string $content The image element, possibly wrapped in a hyperlink. |
0 | 2232 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2233 |
$caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content ); |
0 | 2234 |
|
2235 |
$style = ''; |
|
16 | 2236 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2237 |
if ( $caption_width ) { |
0 | 2238 |
$style = 'style="width: ' . (int) $caption_width . 'px" '; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2239 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2240 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2241 |
if ( $html5 ) { |
9 | 2242 |
$html = sprintf( |
2243 |
'<figure %s%s%sclass="%s">%s%s</figure>', |
|
2244 |
$id, |
|
2245 |
$describedby, |
|
2246 |
$style, |
|
2247 |
esc_attr( $class ), |
|
2248 |
do_shortcode( $content ), |
|
2249 |
sprintf( |
|
2250 |
'<figcaption %sclass="wp-caption-text">%s</figcaption>', |
|
2251 |
$caption_id, |
|
2252 |
$atts['caption'] |
|
2253 |
) |
|
2254 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2255 |
} else { |
9 | 2256 |
$html = sprintf( |
2257 |
'<div %s%sclass="%s">%s%s</div>', |
|
2258 |
$id, |
|
2259 |
$style, |
|
2260 |
esc_attr( $class ), |
|
2261 |
str_replace( '<img ', '<img ' . $describedby, do_shortcode( $content ) ), |
|
2262 |
sprintf( |
|
2263 |
'<p %sclass="wp-caption-text">%s</p>', |
|
2264 |
$caption_id, |
|
2265 |
$atts['caption'] |
|
2266 |
) |
|
2267 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2268 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2269 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2270 |
return $html; |
0 | 2271 |
} |
2272 |
||
9 | 2273 |
add_shortcode( 'gallery', 'gallery_shortcode' ); |
0 | 2274 |
|
2275 |
/** |
|
5 | 2276 |
* Builds the Gallery shortcode output. |
0 | 2277 |
* |
2278 |
* This implements the functionality of the Gallery Shortcode for displaying |
|
2279 |
* WordPress images on a post. |
|
2280 |
* |
|
2281 |
* @since 2.5.0 |
|
2282 |
* |
|
5 | 2283 |
* @param array $attr { |
2284 |
* Attributes of the gallery shortcode. |
|
2285 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2286 |
* @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
|
2287 |
* @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
|
2288 |
* Accepts any valid SQL ORDERBY statement. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2289 |
* @type int $id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2290 |
* @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
|
2291 |
* 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
|
2292 |
* @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
|
2293 |
* 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
|
2294 |
* @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
|
2295 |
* 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
|
2296 |
* @type int $columns Number of columns of images to display. Default 3. |
18 | 2297 |
* @type string|int[] $size Size of the images to display. Accepts any registered image size name, or an array |
2298 |
* 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
|
2299 |
* @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
|
2300 |
* @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
|
2301 |
* @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
|
2302 |
* @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
|
2303 |
* Accepts 'file', 'none'. |
5 | 2304 |
* } |
0 | 2305 |
* @return string HTML content to display gallery. |
2306 |
*/ |
|
5 | 2307 |
function gallery_shortcode( $attr ) { |
0 | 2308 |
$post = get_post(); |
2309 |
||
2310 |
static $instance = 0; |
|
2311 |
$instance++; |
|
2312 |
||
2313 |
if ( ! empty( $attr['ids'] ) ) { |
|
2314 |
// 'ids' is explicitly ordered, unless you specify otherwise. |
|
5 | 2315 |
if ( empty( $attr['orderby'] ) ) { |
0 | 2316 |
$attr['orderby'] = 'post__in'; |
5 | 2317 |
} |
0 | 2318 |
$attr['include'] = $attr['ids']; |
2319 |
} |
|
2320 |
||
5 | 2321 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2322 |
* Filters the default gallery shortcode output. |
5 | 2323 |
* |
2324 |
* If the filtered output isn't empty, it will be used instead of generating |
|
2325 |
* the default gallery template. |
|
2326 |
* |
|
2327 |
* @since 2.5.0 |
|
2328 |
* @since 4.2.0 The `$instance` parameter was added. |
|
2329 |
* |
|
2330 |
* @see gallery_shortcode() |
|
2331 |
* |
|
2332 |
* @param string $output The gallery output. Default empty. |
|
2333 |
* @param array $attr Attributes of the gallery shortcode. |
|
2334 |
* @param int $instance Unique numeric ID of this gallery shortcode instance. |
|
2335 |
*/ |
|
2336 |
$output = apply_filters( 'post_gallery', '', $attr, $instance ); |
|
16 | 2337 |
|
2338 |
if ( ! empty( $output ) ) { |
|
0 | 2339 |
return $output; |
2340 |
} |
|
2341 |
||
5 | 2342 |
$html5 = current_theme_supports( 'html5', 'gallery' ); |
9 | 2343 |
$atts = shortcode_atts( |
2344 |
array( |
|
2345 |
'order' => 'ASC', |
|
2346 |
'orderby' => 'menu_order ID', |
|
2347 |
'id' => $post ? $post->ID : 0, |
|
2348 |
'itemtag' => $html5 ? 'figure' : 'dl', |
|
2349 |
'icontag' => $html5 ? 'div' : 'dt', |
|
2350 |
'captiontag' => $html5 ? 'figcaption' : 'dd', |
|
2351 |
'columns' => 3, |
|
2352 |
'size' => 'thumbnail', |
|
2353 |
'include' => '', |
|
2354 |
'exclude' => '', |
|
2355 |
'link' => '', |
|
2356 |
), |
|
2357 |
$attr, |
|
2358 |
'gallery' |
|
2359 |
); |
|
5 | 2360 |
|
18 | 2361 |
$id = (int) $atts['id']; |
5 | 2362 |
|
2363 |
if ( ! empty( $atts['include'] ) ) { |
|
9 | 2364 |
$_attachments = get_posts( |
2365 |
array( |
|
2366 |
'include' => $atts['include'], |
|
2367 |
'post_status' => 'inherit', |
|
2368 |
'post_type' => 'attachment', |
|
2369 |
'post_mime_type' => 'image', |
|
2370 |
'order' => $atts['order'], |
|
2371 |
'orderby' => $atts['orderby'], |
|
2372 |
) |
|
2373 |
); |
|
0 | 2374 |
|
2375 |
$attachments = array(); |
|
2376 |
foreach ( $_attachments as $key => $val ) { |
|
9 | 2377 |
$attachments[ $val->ID ] = $_attachments[ $key ]; |
0 | 2378 |
} |
5 | 2379 |
} elseif ( ! empty( $atts['exclude'] ) ) { |
9 | 2380 |
$attachments = get_children( |
2381 |
array( |
|
2382 |
'post_parent' => $id, |
|
2383 |
'exclude' => $atts['exclude'], |
|
2384 |
'post_status' => 'inherit', |
|
2385 |
'post_type' => 'attachment', |
|
2386 |
'post_mime_type' => 'image', |
|
2387 |
'order' => $atts['order'], |
|
2388 |
'orderby' => $atts['orderby'], |
|
2389 |
) |
|
2390 |
); |
|
0 | 2391 |
} else { |
9 | 2392 |
$attachments = get_children( |
2393 |
array( |
|
2394 |
'post_parent' => $id, |
|
2395 |
'post_status' => 'inherit', |
|
2396 |
'post_type' => 'attachment', |
|
2397 |
'post_mime_type' => 'image', |
|
2398 |
'order' => $atts['order'], |
|
2399 |
'orderby' => $atts['orderby'], |
|
2400 |
) |
|
2401 |
); |
|
0 | 2402 |
} |
2403 |
||
5 | 2404 |
if ( empty( $attachments ) ) { |
0 | 2405 |
return ''; |
5 | 2406 |
} |
0 | 2407 |
|
2408 |
if ( is_feed() ) { |
|
2409 |
$output = "\n"; |
|
5 | 2410 |
foreach ( $attachments as $att_id => $attachment ) { |
16 | 2411 |
if ( ! empty( $atts['link'] ) ) { |
2412 |
if ( 'none' === $atts['link'] ) { |
|
2413 |
$output .= wp_get_attachment_image( $att_id, $atts['size'], false, $attr ); |
|
2414 |
} else { |
|
2415 |
$output .= wp_get_attachment_link( $att_id, $atts['size'], false ); |
|
2416 |
} |
|
2417 |
} else { |
|
2418 |
$output .= wp_get_attachment_link( $att_id, $atts['size'], true ); |
|
2419 |
} |
|
2420 |
$output .= "\n"; |
|
5 | 2421 |
} |
0 | 2422 |
return $output; |
2423 |
} |
|
2424 |
||
9 | 2425 |
$itemtag = tag_escape( $atts['itemtag'] ); |
5 | 2426 |
$captiontag = tag_escape( $atts['captiontag'] ); |
9 | 2427 |
$icontag = tag_escape( $atts['icontag'] ); |
0 | 2428 |
$valid_tags = wp_kses_allowed_html( 'post' ); |
5 | 2429 |
if ( ! isset( $valid_tags[ $itemtag ] ) ) { |
0 | 2430 |
$itemtag = 'dl'; |
5 | 2431 |
} |
2432 |
if ( ! isset( $valid_tags[ $captiontag ] ) ) { |
|
0 | 2433 |
$captiontag = 'dd'; |
5 | 2434 |
} |
2435 |
if ( ! isset( $valid_tags[ $icontag ] ) ) { |
|
0 | 2436 |
$icontag = 'dt'; |
5 | 2437 |
} |
2438 |
||
18 | 2439 |
$columns = (int) $atts['columns']; |
9 | 2440 |
$itemwidth = $columns > 0 ? floor( 100 / $columns ) : 100; |
2441 |
$float = is_rtl() ? 'right' : 'left'; |
|
0 | 2442 |
|
2443 |
$selector = "gallery-{$instance}"; |
|
2444 |
||
5 | 2445 |
$gallery_style = ''; |
2446 |
||
2447 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2448 |
* Filters whether to print default gallery styles. |
5 | 2449 |
* |
2450 |
* @since 3.1.0 |
|
2451 |
* |
|
2452 |
* @param bool $print Whether to print default gallery styles. |
|
2453 |
* Defaults to false if the theme supports HTML5 galleries. |
|
2454 |
* Otherwise, defaults to true. |
|
2455 |
*/ |
|
2456 |
if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) { |
|
16 | 2457 |
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; |
2458 |
||
0 | 2459 |
$gallery_style = " |
16 | 2460 |
<style{$type_attr}> |
0 | 2461 |
#{$selector} { |
2462 |
margin: auto; |
|
2463 |
} |
|
2464 |
#{$selector} .gallery-item { |
|
2465 |
float: {$float}; |
|
2466 |
margin-top: 10px; |
|
2467 |
text-align: center; |
|
2468 |
width: {$itemwidth}%; |
|
2469 |
} |
|
2470 |
#{$selector} img { |
|
2471 |
border: 2px solid #cfcfcf; |
|
2472 |
} |
|
2473 |
#{$selector} .gallery-caption { |
|
2474 |
margin-left: 0; |
|
2475 |
} |
|
2476 |
/* see gallery_shortcode() in wp-includes/media.php */ |
|
5 | 2477 |
</style>\n\t\t"; |
2478 |
} |
|
2479 |
||
18 | 2480 |
$size_class = sanitize_html_class( is_array( $atts['size'] ) ? implode( 'x', $atts['size'] ) : $atts['size'] ); |
0 | 2481 |
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; |
5 | 2482 |
|
2483 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2484 |
* Filters the default gallery shortcode CSS styles. |
5 | 2485 |
* |
2486 |
* @since 2.5.0 |
|
2487 |
* |
|
2488 |
* @param string $gallery_style Default CSS styles and opening HTML div container |
|
2489 |
* for the gallery shortcode output. |
|
2490 |
*/ |
|
2491 |
$output = apply_filters( 'gallery_style', $gallery_style . $gallery_div ); |
|
0 | 2492 |
|
2493 |
$i = 0; |
|
16 | 2494 |
|
0 | 2495 |
foreach ( $attachments as $id => $attachment ) { |
5 | 2496 |
|
2497 |
$attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : ''; |
|
16 | 2498 |
|
5 | 2499 |
if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { |
2500 |
$image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); |
|
2501 |
} elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) { |
|
2502 |
$image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr ); |
|
2503 |
} else { |
|
2504 |
$image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); |
|
2505 |
} |
|
16 | 2506 |
|
9 | 2507 |
$image_meta = wp_get_attachment_metadata( $id ); |
0 | 2508 |
|
2509 |
$orientation = ''; |
|
16 | 2510 |
|
5 | 2511 |
if ( isset( $image_meta['height'], $image_meta['width'] ) ) { |
0 | 2512 |
$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; |
5 | 2513 |
} |
16 | 2514 |
|
0 | 2515 |
$output .= "<{$itemtag} class='gallery-item'>"; |
2516 |
$output .= " |
|
2517 |
<{$icontag} class='gallery-icon {$orientation}'> |
|
2518 |
$image_output |
|
2519 |
</{$icontag}>"; |
|
16 | 2520 |
|
9 | 2521 |
if ( $captiontag && trim( $attachment->post_excerpt ) ) { |
0 | 2522 |
$output .= " |
5 | 2523 |
<{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'> |
9 | 2524 |
" . wptexturize( $attachment->post_excerpt ) . " |
0 | 2525 |
</{$captiontag}>"; |
2526 |
} |
|
16 | 2527 |
|
0 | 2528 |
$output .= "</{$itemtag}>"; |
16 | 2529 |
|
2530 |
if ( ! $html5 && $columns > 0 && 0 === ++$i % $columns ) { |
|
0 | 2531 |
$output .= '<br style="clear: both" />'; |
5 | 2532 |
} |
2533 |
} |
|
2534 |
||
16 | 2535 |
if ( ! $html5 && $columns > 0 && 0 !== $i % $columns ) { |
5 | 2536 |
$output .= " |
2537 |
<br style='clear: both' />"; |
|
0 | 2538 |
} |
2539 |
||
2540 |
$output .= " |
|
2541 |
</div>\n"; |
|
2542 |
||
2543 |
return $output; |
|
2544 |
} |
|
2545 |
||
2546 |
/** |
|
5 | 2547 |
* Outputs the templates used by playlists. |
2548 |
* |
|
2549 |
* @since 3.9.0 |
|
2550 |
*/ |
|
2551 |
function wp_underscore_playlist_templates() { |
|
9 | 2552 |
?> |
5 | 2553 |
<script type="text/html" id="tmpl-wp-playlist-current-item"> |
18 | 2554 |
<# if ( data.thumb && data.thumb.src ) { #> |
2555 |
<img src="{{ data.thumb.src }}" alt="" /> |
|
5 | 2556 |
<# } #> |
2557 |
<div class="wp-playlist-caption"> |
|
9 | 2558 |
<span class="wp-playlist-item-meta wp-playlist-item-title"> |
2559 |
<?php |
|
16 | 2560 |
/* translators: %s: Playlist item title. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2561 |
printf( _x( '“%s”', 'playlist item title' ), '{{ data.title }}' ); |
9 | 2562 |
?> |
2563 |
</span> |
|
5 | 2564 |
<# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #> |
2565 |
<# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #> |
|
2566 |
</div> |
|
2567 |
</script> |
|
2568 |
<script type="text/html" id="tmpl-wp-playlist-item"> |
|
2569 |
<div class="wp-playlist-item"> |
|
2570 |
<a class="wp-playlist-caption" href="{{ data.src }}"> |
|
2571 |
{{ data.index ? ( data.index + '. ' ) : '' }} |
|
2572 |
<# if ( data.caption ) { #> |
|
2573 |
{{ data.caption }} |
|
2574 |
<# } else { #> |
|
9 | 2575 |
<span class="wp-playlist-item-title"> |
2576 |
<?php |
|
16 | 2577 |
/* translators: %s: Playlist item title. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2578 |
printf( _x( '“%s”', 'playlist item title' ), '{{{ data.title }}}' ); |
9 | 2579 |
?> |
2580 |
</span> |
|
5 | 2581 |
<# if ( data.artists && data.meta.artist ) { #> |
2582 |
<span class="wp-playlist-item-artist"> — {{ data.meta.artist }}</span> |
|
2583 |
<# } #> |
|
2584 |
<# } #> |
|
2585 |
</a> |
|
2586 |
<# if ( data.meta.length_formatted ) { #> |
|
2587 |
<div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div> |
|
2588 |
<# } #> |
|
2589 |
</div> |
|
2590 |
</script> |
|
9 | 2591 |
<?php |
5 | 2592 |
} |
2593 |
||
2594 |
/** |
|
2595 |
* Outputs and enqueue default scripts and styles for playlists. |
|
2596 |
* |
|
2597 |
* @since 3.9.0 |
|
2598 |
* |
|
2599 |
* @param string $type Type of playlist. Accepts 'audio' or 'video'. |
|
2600 |
*/ |
|
2601 |
function wp_playlist_scripts( $type ) { |
|
2602 |
wp_enqueue_style( 'wp-mediaelement' ); |
|
2603 |
wp_enqueue_script( 'wp-playlist' ); |
|
9 | 2604 |
?> |
2605 |
<!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ); ?>');</script><![endif]--> |
|
2606 |
<?php |
|
5 | 2607 |
add_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 ); |
2608 |
add_action( 'admin_footer', 'wp_underscore_playlist_templates', 0 ); |
|
2609 |
} |
|
2610 |
||
2611 |
/** |
|
2612 |
* Builds the Playlist shortcode output. |
|
2613 |
* |
|
2614 |
* This implements the functionality of the playlist shortcode for displaying |
|
2615 |
* a collection of WordPress audio or video files in a post. |
|
2616 |
* |
|
2617 |
* @since 3.9.0 |
|
2618 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2619 |
* @global int $content_width |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2620 |
* |
5 | 2621 |
* @param array $attr { |
2622 |
* Array of default playlist attributes. |
|
2623 |
* |
|
2624 |
* @type string $type Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'. |
|
2625 |
* @type string $order Designates ascending or descending order of items in the playlist. |
|
2626 |
* Accepts 'ASC', 'DESC'. Default 'ASC'. |
|
2627 |
* @type string $orderby Any column, or columns, to sort the playlist. If $ids are |
|
2628 |
* passed, this defaults to the order of the $ids array ('post__in'). |
|
2629 |
* Otherwise default is 'menu_order ID'. |
|
2630 |
* @type int $id If an explicit $ids array is not present, this parameter |
|
2631 |
* will determine which attachments are used for the playlist. |
|
2632 |
* Default is the current post ID. |
|
2633 |
* @type array $ids Create a playlist out of these explicit attachment IDs. If empty, |
|
2634 |
* a playlist will be created from all $type attachments of $id. |
|
2635 |
* Default empty. |
|
2636 |
* @type array $exclude List of specific attachment IDs to exclude from the playlist. Default empty. |
|
2637 |
* @type string $style Playlist style to use. Accepts 'light' or 'dark'. Default 'light'. |
|
2638 |
* @type bool $tracklist Whether to show or hide the playlist. Default true. |
|
2639 |
* @type bool $tracknumbers Whether to show or hide the numbers next to entries in the playlist. Default true. |
|
2640 |
* @type bool $images Show or hide the video or audio thumbnail (Featured Image/post |
|
2641 |
* thumbnail). Default true. |
|
2642 |
* @type bool $artists Whether to show or hide artist name in the playlist. Default true. |
|
2643 |
* } |
|
2644 |
* |
|
2645 |
* @return string Playlist output. Empty string if the passed type is unsupported. |
|
2646 |
*/ |
|
2647 |
function wp_playlist_shortcode( $attr ) { |
|
2648 |
global $content_width; |
|
2649 |
$post = get_post(); |
|
2650 |
||
2651 |
static $instance = 0; |
|
2652 |
$instance++; |
|
2653 |
||
2654 |
if ( ! empty( $attr['ids'] ) ) { |
|
2655 |
// 'ids' is explicitly ordered, unless you specify otherwise. |
|
2656 |
if ( empty( $attr['orderby'] ) ) { |
|
2657 |
$attr['orderby'] = 'post__in'; |
|
2658 |
} |
|
2659 |
$attr['include'] = $attr['ids']; |
|
2660 |
} |
|
2661 |
||
2662 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2663 |
* Filters the playlist output. |
5 | 2664 |
* |
16 | 2665 |
* Returning a non-empty value from the filter will short-circuit generation |
5 | 2666 |
* of the default playlist output, returning the passed value instead. |
2667 |
* |
|
2668 |
* @since 3.9.0 |
|
2669 |
* @since 4.2.0 The `$instance` parameter was added. |
|
2670 |
* |
|
2671 |
* @param string $output Playlist output. Default empty. |
|
2672 |
* @param array $attr An array of shortcode attributes. |
|
2673 |
* @param int $instance Unique numeric ID of this playlist shortcode instance. |
|
2674 |
*/ |
|
2675 |
$output = apply_filters( 'post_playlist', '', $attr, $instance ); |
|
16 | 2676 |
|
2677 |
if ( ! empty( $output ) ) { |
|
5 | 2678 |
return $output; |
2679 |
} |
|
2680 |
||
9 | 2681 |
$atts = shortcode_atts( |
2682 |
array( |
|
2683 |
'type' => 'audio', |
|
2684 |
'order' => 'ASC', |
|
2685 |
'orderby' => 'menu_order ID', |
|
2686 |
'id' => $post ? $post->ID : 0, |
|
2687 |
'include' => '', |
|
2688 |
'exclude' => '', |
|
2689 |
'style' => 'light', |
|
2690 |
'tracklist' => true, |
|
2691 |
'tracknumbers' => true, |
|
2692 |
'images' => true, |
|
2693 |
'artists' => true, |
|
2694 |
), |
|
2695 |
$attr, |
|
2696 |
'playlist' |
|
2697 |
); |
|
5 | 2698 |
|
18 | 2699 |
$id = (int) $atts['id']; |
5 | 2700 |
|
16 | 2701 |
if ( 'audio' !== $atts['type'] ) { |
5 | 2702 |
$atts['type'] = 'video'; |
2703 |
} |
|
2704 |
||
2705 |
$args = array( |
|
9 | 2706 |
'post_status' => 'inherit', |
2707 |
'post_type' => 'attachment', |
|
5 | 2708 |
'post_mime_type' => $atts['type'], |
9 | 2709 |
'order' => $atts['order'], |
2710 |
'orderby' => $atts['orderby'], |
|
5 | 2711 |
); |
2712 |
||
2713 |
if ( ! empty( $atts['include'] ) ) { |
|
2714 |
$args['include'] = $atts['include']; |
|
9 | 2715 |
$_attachments = get_posts( $args ); |
5 | 2716 |
|
2717 |
$attachments = array(); |
|
2718 |
foreach ( $_attachments as $key => $val ) { |
|
9 | 2719 |
$attachments[ $val->ID ] = $_attachments[ $key ]; |
5 | 2720 |
} |
2721 |
} elseif ( ! empty( $atts['exclude'] ) ) { |
|
2722 |
$args['post_parent'] = $id; |
|
9 | 2723 |
$args['exclude'] = $atts['exclude']; |
2724 |
$attachments = get_children( $args ); |
|
5 | 2725 |
} else { |
2726 |
$args['post_parent'] = $id; |
|
9 | 2727 |
$attachments = get_children( $args ); |
5 | 2728 |
} |
2729 |
||
2730 |
if ( empty( $attachments ) ) { |
|
2731 |
return ''; |
|
2732 |
} |
|
2733 |
||
2734 |
if ( is_feed() ) { |
|
2735 |
$output = "\n"; |
|
2736 |
foreach ( $attachments as $att_id => $attachment ) { |
|
2737 |
$output .= wp_get_attachment_link( $att_id ) . "\n"; |
|
2738 |
} |
|
2739 |
return $output; |
|
2740 |
} |
|
2741 |
||
16 | 2742 |
$outer = 22; // Default padding and border of wrapper. |
5 | 2743 |
|
9 | 2744 |
$default_width = 640; |
5 | 2745 |
$default_height = 360; |
2746 |
||
9 | 2747 |
$theme_width = empty( $content_width ) ? $default_width : ( $content_width - $outer ); |
5 | 2748 |
$theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width ); |
2749 |
||
2750 |
$data = array( |
|
9 | 2751 |
'type' => $atts['type'], |
16 | 2752 |
// Don't pass strings to JSON, will be truthy in JS. |
9 | 2753 |
'tracklist' => wp_validate_boolean( $atts['tracklist'] ), |
5 | 2754 |
'tracknumbers' => wp_validate_boolean( $atts['tracknumbers'] ), |
9 | 2755 |
'images' => wp_validate_boolean( $atts['images'] ), |
2756 |
'artists' => wp_validate_boolean( $atts['artists'] ), |
|
5 | 2757 |
); |
2758 |
||
2759 |
$tracks = array(); |
|
2760 |
foreach ( $attachments as $attachment ) { |
|
9 | 2761 |
$url = wp_get_attachment_url( $attachment->ID ); |
5 | 2762 |
$ftype = wp_check_filetype( $url, wp_get_mime_types() ); |
2763 |
$track = array( |
|
9 | 2764 |
'src' => $url, |
2765 |
'type' => $ftype['type'], |
|
2766 |
'title' => $attachment->post_title, |
|
2767 |
'caption' => $attachment->post_excerpt, |
|
2768 |
'description' => $attachment->post_content, |
|
5 | 2769 |
); |
2770 |
||
2771 |
$track['meta'] = array(); |
|
9 | 2772 |
$meta = wp_get_attachment_metadata( $attachment->ID ); |
5 | 2773 |
if ( ! empty( $meta ) ) { |
2774 |
||
2775 |
foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) { |
|
2776 |
if ( ! empty( $meta[ $key ] ) ) { |
|
2777 |
$track['meta'][ $key ] = $meta[ $key ]; |
|
2778 |
} |
|
2779 |
} |
|
2780 |
||
2781 |
if ( 'video' === $atts['type'] ) { |
|
2782 |
if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { |
|
9 | 2783 |
$width = $meta['width']; |
2784 |
$height = $meta['height']; |
|
5 | 2785 |
$theme_height = round( ( $height * $theme_width ) / $width ); |
2786 |
} else { |
|
9 | 2787 |
$width = $default_width; |
5 | 2788 |
$height = $default_height; |
2789 |
} |
|
2790 |
||
2791 |
$track['dimensions'] = array( |
|
2792 |
'original' => compact( 'width', 'height' ), |
|
9 | 2793 |
'resized' => array( |
2794 |
'width' => $theme_width, |
|
2795 |
'height' => $theme_height, |
|
2796 |
), |
|
5 | 2797 |
); |
2798 |
} |
|
2799 |
} |
|
2800 |
||
2801 |
if ( $atts['images'] ) { |
|
2802 |
$thumb_id = get_post_thumbnail_id( $attachment->ID ); |
|
2803 |
if ( ! empty( $thumb_id ) ) { |
|
2804 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'full' ); |
|
9 | 2805 |
$track['image'] = compact( 'src', 'width', 'height' ); |
5 | 2806 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); |
9 | 2807 |
$track['thumb'] = compact( 'src', 'width', 'height' ); |
5 | 2808 |
} else { |
9 | 2809 |
$src = wp_mime_type_icon( $attachment->ID ); |
2810 |
$width = 48; |
|
2811 |
$height = 64; |
|
5 | 2812 |
$track['image'] = compact( 'src', 'width', 'height' ); |
2813 |
$track['thumb'] = compact( 'src', 'width', 'height' ); |
|
2814 |
} |
|
2815 |
} |
|
2816 |
||
2817 |
$tracks[] = $track; |
|
2818 |
} |
|
2819 |
$data['tracks'] = $tracks; |
|
2820 |
||
9 | 2821 |
$safe_type = esc_attr( $atts['type'] ); |
5 | 2822 |
$safe_style = esc_attr( $atts['style'] ); |
2823 |
||
2824 |
ob_start(); |
|
2825 |
||
2826 |
if ( 1 === $instance ) { |
|
2827 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2828 |
* Prints and enqueues playlist scripts, styles, and JavaScript templates. |
5 | 2829 |
* |
2830 |
* @since 3.9.0 |
|
2831 |
* |
|
2832 |
* @param string $type Type of playlist. Possible values are 'audio' or 'video'. |
|
2833 |
* @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'. |
|
2834 |
*/ |
|
2835 |
do_action( 'wp_playlist_scripts', $atts['type'], $atts['style'] ); |
|
9 | 2836 |
} |
2837 |
?> |
|
2838 |
<div class="wp-playlist wp-<?php echo $safe_type; ?>-playlist wp-playlist-<?php echo $safe_style; ?>"> |
|
2839 |
<?php if ( 'audio' === $atts['type'] ) : ?> |
|
18 | 2840 |
<div class="wp-playlist-current-item"></div> |
2841 |
<?php endif; ?> |
|
2842 |
<<?php echo $safe_type; ?> controls="controls" preload="none" width="<?php echo (int) $theme_width; ?>" |
|
2843 |
<?php |
|
2844 |
if ( 'video' === $safe_type ) { |
|
2845 |
echo ' height="', (int) $theme_height, '"'; |
|
2846 |
} |
|
2847 |
?> |
|
9 | 2848 |
></<?php echo $safe_type; ?>> |
5 | 2849 |
<div class="wp-playlist-next"></div> |
2850 |
<div class="wp-playlist-prev"></div> |
|
2851 |
<noscript> |
|
9 | 2852 |
<ol> |
18 | 2853 |
<?php |
2854 |
foreach ( $attachments as $att_id => $attachment ) { |
|
2855 |
printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) ); |
|
2856 |
} |
|
2857 |
?> |
|
9 | 2858 |
</ol> |
5 | 2859 |
</noscript> |
9 | 2860 |
<script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ); ?></script> |
5 | 2861 |
</div> |
2862 |
<?php |
|
2863 |
return ob_get_clean(); |
|
2864 |
} |
|
2865 |
add_shortcode( 'playlist', 'wp_playlist_shortcode' ); |
|
2866 |
||
2867 |
/** |
|
2868 |
* Provides a No-JS Flash fallback as a last resort for audio / video. |
|
0 | 2869 |
* |
2870 |
* @since 3.6.0 |
|
2871 |
* |
|
5 | 2872 |
* @param string $url The media element URL. |
2873 |
* @return string Fallback HTML. |
|
0 | 2874 |
*/ |
2875 |
function wp_mediaelement_fallback( $url ) { |
|
5 | 2876 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2877 |
* Filters the Mediaelement fallback output for no-JS. |
5 | 2878 |
* |
2879 |
* @since 3.6.0 |
|
2880 |
* |
|
2881 |
* @param string $output Fallback output for no-JS. |
|
2882 |
* @param string $url Media file URL. |
|
2883 |
*/ |
|
0 | 2884 |
return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url ); |
2885 |
} |
|
2886 |
||
2887 |
/** |
|
16 | 2888 |
* Returns a filtered list of supported audio formats. |
0 | 2889 |
* |
2890 |
* @since 3.6.0 |
|
5 | 2891 |
* |
16 | 2892 |
* @return string[] Supported audio formats. |
0 | 2893 |
*/ |
2894 |
function wp_get_audio_extensions() { |
|
5 | 2895 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2896 |
* Filters the list of supported audio formats. |
5 | 2897 |
* |
2898 |
* @since 3.6.0 |
|
2899 |
* |
|
16 | 2900 |
* @param string[] $extensions An array of supported audio formats. Defaults are |
2901 |
* 'mp3', 'ogg', 'flac', 'm4a', 'wav'. |
|
5 | 2902 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2903 |
return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'flac', 'm4a', 'wav' ) ); |
0 | 2904 |
} |
2905 |
||
2906 |
/** |
|
5 | 2907 |
* Returns useful keys to use to lookup data from an attachment's stored metadata. |
2908 |
* |
|
2909 |
* @since 3.9.0 |
|
2910 |
* |
|
2911 |
* @param WP_Post $attachment The current attachment, provided for context. |
|
2912 |
* @param string $context Optional. The context. Accepts 'edit', 'display'. Default 'display'. |
|
16 | 2913 |
* @return string[] Key/value pairs of field keys to labels. |
5 | 2914 |
*/ |
2915 |
function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) { |
|
2916 |
$fields = array( |
|
2917 |
'artist' => __( 'Artist' ), |
|
9 | 2918 |
'album' => __( 'Album' ), |
5 | 2919 |
); |
2920 |
||
2921 |
if ( 'display' === $context ) { |
|
2922 |
$fields['genre'] = __( 'Genre' ); |
|
2923 |
$fields['year'] = __( 'Year' ); |
|
2924 |
$fields['length_formatted'] = _x( 'Length', 'video or audio' ); |
|
2925 |
} elseif ( 'js' === $context ) { |
|
9 | 2926 |
$fields['bitrate'] = __( 'Bitrate' ); |
2927 |
$fields['bitrate_mode'] = __( 'Bitrate Mode' ); |
|
5 | 2928 |
} |
2929 |
||
2930 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2931 |
* Filters the editable list of keys to look up data from an attachment's metadata. |
5 | 2932 |
* |
2933 |
* @since 3.9.0 |
|
2934 |
* |
|
2935 |
* @param array $fields Key/value pairs of field keys to labels. |
|
2936 |
* @param WP_Post $attachment Attachment object. |
|
2937 |
* @param string $context The context. Accepts 'edit', 'display'. Default 'display'. |
|
2938 |
*/ |
|
2939 |
return apply_filters( 'wp_get_attachment_id3_keys', $fields, $attachment, $context ); |
|
2940 |
} |
|
2941 |
/** |
|
2942 |
* Builds the Audio shortcode output. |
|
0 | 2943 |
* |
2944 |
* This implements the functionality of the Audio Shortcode for displaying |
|
2945 |
* WordPress mp3s in a post. |
|
2946 |
* |
|
2947 |
* @since 3.6.0 |
|
2948 |
* |
|
5 | 2949 |
* @param array $attr { |
2950 |
* Attributes of the audio shortcode. |
|
2951 |
* |
|
2952 |
* @type string $src URL to the source of the audio file. Default empty. |
|
2953 |
* @type string $loop The 'loop' attribute for the `<audio>` element. Default empty. |
|
2954 |
* @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
|
2955 |
* @type string $preload The 'preload' attribute for the `<audio>` element. Default 'none'. |
5 | 2956 |
* @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
|
2957 |
* @type string $style The 'style' attribute for the `<audio>` element. Default 'width: 100%;'. |
5 | 2958 |
* } |
2959 |
* @param string $content Shortcode content. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2960 |
* @return string|void HTML content to display audio. |
0 | 2961 |
*/ |
2962 |
function wp_audio_shortcode( $attr, $content = '' ) { |
|
2963 |
$post_id = get_post() ? get_the_ID() : 0; |
|
2964 |
||
5 | 2965 |
static $instance = 0; |
2966 |
$instance++; |
|
0 | 2967 |
|
2968 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2969 |
* Filters the default audio shortcode output. |
0 | 2970 |
* |
5 | 2971 |
* If the filtered output isn't empty, it will be used instead of generating the default audio template. |
2972 |
* |
|
2973 |
* @since 3.6.0 |
|
0 | 2974 |
* |
5 | 2975 |
* @param string $html Empty variable to be replaced with shortcode markup. |
2976 |
* @param array $attr Attributes of the shortcode. @see wp_audio_shortcode() |
|
2977 |
* @param string $content Shortcode content. |
|
2978 |
* @param int $instance Unique numeric ID of this audio shortcode instance. |
|
0 | 2979 |
*/ |
5 | 2980 |
$override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance ); |
16 | 2981 |
|
5 | 2982 |
if ( '' !== $override ) { |
2983 |
return $override; |
|
2984 |
} |
|
0 | 2985 |
|
2986 |
$audio = null; |
|
2987 |
||
2988 |
$default_types = wp_get_audio_extensions(); |
|
2989 |
$defaults_atts = array( |
|
2990 |
'src' => '', |
|
2991 |
'loop' => '', |
|
2992 |
'autoplay' => '', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2993 |
'preload' => 'none', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2994 |
'class' => 'wp-audio-shortcode', |
9 | 2995 |
'style' => 'width: 100%;', |
0 | 2996 |
); |
5 | 2997 |
foreach ( $default_types as $type ) { |
9 | 2998 |
$defaults_atts[ $type ] = ''; |
5 | 2999 |
} |
0 | 3000 |
|
3001 |
$atts = shortcode_atts( $defaults_atts, $attr, 'audio' ); |
|
3002 |
||
3003 |
$primary = false; |
|
5 | 3004 |
if ( ! empty( $atts['src'] ) ) { |
3005 |
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); |
|
16 | 3006 |
|
3007 |
if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) { |
|
5 | 3008 |
return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); |
3009 |
} |
|
16 | 3010 |
|
0 | 3011 |
$primary = true; |
3012 |
array_unshift( $default_types, 'src' ); |
|
3013 |
} else { |
|
3014 |
foreach ( $default_types as $ext ) { |
|
5 | 3015 |
if ( ! empty( $atts[ $ext ] ) ) { |
3016 |
$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); |
|
16 | 3017 |
|
5 | 3018 |
if ( strtolower( $type['ext'] ) === $ext ) { |
0 | 3019 |
$primary = true; |
5 | 3020 |
} |
0 | 3021 |
} |
3022 |
} |
|
3023 |
} |
|
3024 |
||
3025 |
if ( ! $primary ) { |
|
3026 |
$audios = get_attached_media( 'audio', $post_id ); |
|
16 | 3027 |
|
5 | 3028 |
if ( empty( $audios ) ) { |
0 | 3029 |
return; |
5 | 3030 |
} |
0 | 3031 |
|
9 | 3032 |
$audio = reset( $audios ); |
5 | 3033 |
$atts['src'] = wp_get_attachment_url( $audio->ID ); |
16 | 3034 |
|
5 | 3035 |
if ( empty( $atts['src'] ) ) { |
0 | 3036 |
return; |
5 | 3037 |
} |
0 | 3038 |
|
3039 |
array_unshift( $default_types, 'src' ); |
|
3040 |
} |
|
3041 |
||
5 | 3042 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3043 |
* Filters the media library used for the audio shortcode. |
5 | 3044 |
* |
3045 |
* @since 3.6.0 |
|
3046 |
* |
|
3047 |
* @param string $library Media library used for the audio shortcode. |
|
3048 |
*/ |
|
0 | 3049 |
$library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); |
16 | 3050 |
|
0 | 3051 |
if ( 'mediaelement' === $library && did_action( 'init' ) ) { |
3052 |
wp_enqueue_style( 'wp-mediaelement' ); |
|
3053 |
wp_enqueue_script( 'wp-mediaelement' ); |
|
3054 |
} |
|
3055 |
||
5 | 3056 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3057 |
* Filters the class attribute for the audio shortcode output container. |
5 | 3058 |
* |
3059 |
* @since 3.6.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3060 |
* @since 4.9.0 The `$atts` parameter was added. |
5 | 3061 |
* |
3062 |
* @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
|
3063 |
* @param array $atts Array of audio shortcode attributes. |
5 | 3064 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3065 |
$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
|
3066 |
|
5 | 3067 |
$html_atts = array( |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3068 |
'class' => $atts['class'], |
5 | 3069 |
'id' => sprintf( 'audio-%d-%d', $post_id, $instance ), |
3070 |
'loop' => wp_validate_boolean( $atts['loop'] ), |
|
3071 |
'autoplay' => wp_validate_boolean( $atts['autoplay'] ), |
|
3072 |
'preload' => $atts['preload'], |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3073 |
'style' => $atts['style'], |
0 | 3074 |
); |
3075 |
||
16 | 3076 |
// These ones should just be omitted altogether if they are blank. |
0 | 3077 |
foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) { |
9 | 3078 |
if ( empty( $html_atts[ $a ] ) ) { |
3079 |
unset( $html_atts[ $a ] ); |
|
5 | 3080 |
} |
0 | 3081 |
} |
3082 |
||
3083 |
$attr_strings = array(); |
|
16 | 3084 |
|
5 | 3085 |
foreach ( $html_atts as $k => $v ) { |
0 | 3086 |
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; |
3087 |
} |
|
3088 |
||
3089 |
$html = ''; |
|
16 | 3090 |
|
5 | 3091 |
if ( 'mediaelement' === $library && 1 === $instance ) { |
0 | 3092 |
$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n"; |
5 | 3093 |
} |
16 | 3094 |
|
18 | 3095 |
$html .= sprintf( '<audio %s controls="controls">', implode( ' ', $attr_strings ) ); |
0 | 3096 |
|
3097 |
$fileurl = ''; |
|
9 | 3098 |
$source = '<source type="%s" src="%s" />'; |
16 | 3099 |
|
0 | 3100 |
foreach ( $default_types as $fallback ) { |
5 | 3101 |
if ( ! empty( $atts[ $fallback ] ) ) { |
3102 |
if ( empty( $fileurl ) ) { |
|
3103 |
$fileurl = $atts[ $fallback ]; |
|
3104 |
} |
|
16 | 3105 |
|
9 | 3106 |
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); |
3107 |
$url = add_query_arg( '_', $instance, $atts[ $fallback ] ); |
|
5 | 3108 |
$html .= sprintf( $source, $type['type'], esc_url( $url ) ); |
0 | 3109 |
} |
3110 |
} |
|
3111 |
||
5 | 3112 |
if ( 'mediaelement' === $library ) { |
0 | 3113 |
$html .= wp_mediaelement_fallback( $fileurl ); |
5 | 3114 |
} |
16 | 3115 |
|
0 | 3116 |
$html .= '</audio>'; |
3117 |
||
5 | 3118 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3119 |
* Filters the audio shortcode output. |
5 | 3120 |
* |
3121 |
* @since 3.6.0 |
|
3122 |
* |
|
3123 |
* @param string $html Audio shortcode HTML output. |
|
3124 |
* @param array $atts Array of audio shortcode attributes. |
|
3125 |
* @param string $audio Audio file. |
|
3126 |
* @param int $post_id Post ID. |
|
3127 |
* @param string $library Media library used for the audio shortcode. |
|
3128 |
*/ |
|
0 | 3129 |
return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library ); |
3130 |
} |
|
3131 |
add_shortcode( 'audio', 'wp_audio_shortcode' ); |
|
3132 |
||
3133 |
/** |
|
16 | 3134 |
* Returns a filtered list of supported video formats. |
0 | 3135 |
* |
3136 |
* @since 3.6.0 |
|
5 | 3137 |
* |
16 | 3138 |
* @return string[] List of supported video formats. |
0 | 3139 |
*/ |
3140 |
function wp_get_video_extensions() { |
|
5 | 3141 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3142 |
* Filters the list of supported video formats. |
5 | 3143 |
* |
3144 |
* @since 3.6.0 |
|
3145 |
* |
|
16 | 3146 |
* @param string[] $extensions An array of supported video formats. Defaults are |
3147 |
* 'mp4', 'm4v', 'webm', 'ogv', 'flv'. |
|
5 | 3148 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3149 |
return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'flv' ) ); |
0 | 3150 |
} |
3151 |
||
3152 |
/** |
|
5 | 3153 |
* Builds the Video shortcode output. |
0 | 3154 |
* |
3155 |
* This implements the functionality of the Video Shortcode for displaying |
|
3156 |
* WordPress mp4s in a post. |
|
3157 |
* |
|
3158 |
* @since 3.6.0 |
|
3159 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3160 |
* @global int $content_width |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3161 |
* |
5 | 3162 |
* @param array $attr { |
3163 |
* Attributes of the shortcode. |
|
3164 |
* |
|
3165 |
* @type string $src URL to the source of the video file. Default empty. |
|
3166 |
* @type int $height Height of the video embed in pixels. Default 360. |
|
3167 |
* @type int $width Width of the video embed in pixels. Default $content_width or 640. |
|
3168 |
* @type string $poster The 'poster' attribute for the `<video>` element. Default empty. |
|
3169 |
* @type string $loop The 'loop' attribute for the `<video>` element. Default empty. |
|
3170 |
* @type string $autoplay The 'autoplay' attribute for the `<video>` element. Default empty. |
|
3171 |
* @type string $preload The 'preload' attribute for the `<video>` element. |
|
3172 |
* Default 'metadata'. |
|
3173 |
* @type string $class The 'class' attribute for the `<video>` element. |
|
3174 |
* Default 'wp-video-shortcode'. |
|
3175 |
* } |
|
3176 |
* @param string $content Shortcode content. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3177 |
* @return string|void HTML content to display video. |
0 | 3178 |
*/ |
3179 |
function wp_video_shortcode( $attr, $content = '' ) { |
|
3180 |
global $content_width; |
|
3181 |
$post_id = get_post() ? get_the_ID() : 0; |
|
3182 |
||
5 | 3183 |
static $instance = 0; |
3184 |
$instance++; |
|
0 | 3185 |
|
3186 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3187 |
* Filters the default video shortcode output. |
0 | 3188 |
* |
5 | 3189 |
* If the filtered output isn't empty, it will be used instead of generating |
3190 |
* the default video template. |
|
3191 |
* |
|
3192 |
* @since 3.6.0 |
|
3193 |
* |
|
3194 |
* @see wp_video_shortcode() |
|
0 | 3195 |
* |
5 | 3196 |
* @param string $html Empty variable to be replaced with shortcode markup. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3197 |
* @param array $attr Attributes of the shortcode. @see wp_video_shortcode() |
5 | 3198 |
* @param string $content Video shortcode content. |
3199 |
* @param int $instance Unique numeric ID of this video shortcode instance. |
|
0 | 3200 |
*/ |
5 | 3201 |
$override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance ); |
16 | 3202 |
|
5 | 3203 |
if ( '' !== $override ) { |
3204 |
return $override; |
|
3205 |
} |
|
0 | 3206 |
|
3207 |
$video = null; |
|
3208 |
||
3209 |
$default_types = wp_get_video_extensions(); |
|
3210 |
$defaults_atts = array( |
|
3211 |
'src' => '', |
|
3212 |
'poster' => '', |
|
3213 |
'loop' => '', |
|
3214 |
'autoplay' => '', |
|
3215 |
'preload' => 'metadata', |
|
5 | 3216 |
'width' => 640, |
0 | 3217 |
'height' => 360, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3218 |
'class' => 'wp-video-shortcode', |
0 | 3219 |
); |
3220 |
||
5 | 3221 |
foreach ( $default_types as $type ) { |
9 | 3222 |
$defaults_atts[ $type ] = ''; |
5 | 3223 |
} |
0 | 3224 |
|
3225 |
$atts = shortcode_atts( $defaults_atts, $attr, 'video' ); |
|
5 | 3226 |
|
3227 |
if ( is_admin() ) { |
|
16 | 3228 |
// Shrink the video so it isn't huge in the admin. |
5 | 3229 |
if ( $atts['width'] > $defaults_atts['width'] ) { |
3230 |
$atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] ); |
|
9 | 3231 |
$atts['width'] = $defaults_atts['width']; |
5 | 3232 |
} |
3233 |
} else { |
|
16 | 3234 |
// If the video is bigger than the theme. |
5 | 3235 |
if ( ! empty( $content_width ) && $atts['width'] > $content_width ) { |
3236 |
$atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] ); |
|
9 | 3237 |
$atts['width'] = $content_width; |
5 | 3238 |
} |
3239 |
} |
|
3240 |
||
16 | 3241 |
$is_vimeo = false; |
3242 |
$is_youtube = false; |
|
9 | 3243 |
$yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#'; |
5 | 3244 |
$vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#'; |
0 | 3245 |
|
3246 |
$primary = false; |
|
5 | 3247 |
if ( ! empty( $atts['src'] ) ) { |
9 | 3248 |
$is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) ); |
3249 |
$is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) ); |
|
16 | 3250 |
|
5 | 3251 |
if ( ! $is_youtube && ! $is_vimeo ) { |
3252 |
$type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); |
|
16 | 3253 |
|
3254 |
if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) { |
|
5 | 3255 |
return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); |
3256 |
} |
|
3257 |
} |
|
3258 |
||
3259 |
if ( $is_vimeo ) { |
|
9 | 3260 |
wp_enqueue_script( 'mediaelement-vimeo' ); |
5 | 3261 |
} |
3262 |
||
0 | 3263 |
$primary = true; |
3264 |
array_unshift( $default_types, 'src' ); |
|
3265 |
} else { |
|
3266 |
foreach ( $default_types as $ext ) { |
|
5 | 3267 |
if ( ! empty( $atts[ $ext ] ) ) { |
3268 |
$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); |
|
3269 |
if ( strtolower( $type['ext'] ) === $ext ) { |
|
0 | 3270 |
$primary = true; |
5 | 3271 |
} |
0 | 3272 |
} |
3273 |
} |
|
3274 |
} |
|
3275 |
||
3276 |
if ( ! $primary ) { |
|
3277 |
$videos = get_attached_media( 'video', $post_id ); |
|
5 | 3278 |
if ( empty( $videos ) ) { |
0 | 3279 |
return; |
5 | 3280 |
} |
0 | 3281 |
|
9 | 3282 |
$video = reset( $videos ); |
5 | 3283 |
$atts['src'] = wp_get_attachment_url( $video->ID ); |
3284 |
if ( empty( $atts['src'] ) ) { |
|
0 | 3285 |
return; |
5 | 3286 |
} |
0 | 3287 |
|
3288 |
array_unshift( $default_types, 'src' ); |
|
3289 |
} |
|
3290 |
||
5 | 3291 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3292 |
* Filters the media library used for the video shortcode. |
5 | 3293 |
* |
3294 |
* @since 3.6.0 |
|
3295 |
* |
|
3296 |
* @param string $library Media library used for the video shortcode. |
|
3297 |
*/ |
|
0 | 3298 |
$library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); |
3299 |
if ( 'mediaelement' === $library && did_action( 'init' ) ) { |
|
3300 |
wp_enqueue_style( 'wp-mediaelement' ); |
|
3301 |
wp_enqueue_script( 'wp-mediaelement' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3302 |
wp_enqueue_script( 'mediaelement-vimeo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3303 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3304 |
|
16 | 3305 |
// MediaElement.js has issues with some URL formats for Vimeo and YouTube, |
3306 |
// so update the URL to prevent the ME.js player from breaking. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3307 |
if ( 'mediaelement' === $library ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3308 |
if ( $is_youtube ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3309 |
// Remove `feature` query arg and force SSL - see #40866. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3310 |
$atts['src'] = remove_query_arg( 'feature', $atts['src'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3311 |
$atts['src'] = set_url_scheme( $atts['src'], 'https' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3312 |
} elseif ( $is_vimeo ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3313 |
// Remove all query arguments and force SSL - see #40866. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3314 |
$parsed_vimeo_url = wp_parse_url( $atts['src'] ); |
9 | 3315 |
$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
|
3316 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3317 |
// Add loop param for mejs bug - see #40977, not needed after #39686. |
9 | 3318 |
$loop = $atts['loop'] ? '1' : '0'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3319 |
$atts['src'] = add_query_arg( 'loop', $loop, $vimeo_src ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3320 |
} |
0 | 3321 |
} |
3322 |
||
5 | 3323 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3324 |
* Filters the class attribute for the video shortcode output container. |
5 | 3325 |
* |
3326 |
* @since 3.6.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3327 |
* @since 4.9.0 The `$atts` parameter was added. |
5 | 3328 |
* |
3329 |
* @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
|
3330 |
* @param array $atts Array of video shortcode attributes. |
5 | 3331 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3332 |
$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
|
3333 |
|
5 | 3334 |
$html_atts = array( |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3335 |
'class' => $atts['class'], |
5 | 3336 |
'id' => sprintf( 'video-%d-%d', $post_id, $instance ), |
3337 |
'width' => absint( $atts['width'] ), |
|
3338 |
'height' => absint( $atts['height'] ), |
|
3339 |
'poster' => esc_url( $atts['poster'] ), |
|
3340 |
'loop' => wp_validate_boolean( $atts['loop'] ), |
|
3341 |
'autoplay' => wp_validate_boolean( $atts['autoplay'] ), |
|
3342 |
'preload' => $atts['preload'], |
|
0 | 3343 |
); |
3344 |
||
16 | 3345 |
// These ones should just be omitted altogether if they are blank. |
0 | 3346 |
foreach ( array( 'poster', 'loop', 'autoplay', 'preload' ) as $a ) { |
9 | 3347 |
if ( empty( $html_atts[ $a ] ) ) { |
3348 |
unset( $html_atts[ $a ] ); |
|
5 | 3349 |
} |
0 | 3350 |
} |
3351 |
||
3352 |
$attr_strings = array(); |
|
5 | 3353 |
foreach ( $html_atts as $k => $v ) { |
0 | 3354 |
$attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; |
3355 |
} |
|
3356 |
||
3357 |
$html = ''; |
|
18 | 3358 |
|
5 | 3359 |
if ( 'mediaelement' === $library && 1 === $instance ) { |
0 | 3360 |
$html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n"; |
5 | 3361 |
} |
18 | 3362 |
|
3363 |
$html .= sprintf( '<video %s controls="controls">', implode( ' ', $attr_strings ) ); |
|
0 | 3364 |
|
3365 |
$fileurl = ''; |
|
9 | 3366 |
$source = '<source type="%s" src="%s" />'; |
18 | 3367 |
|
0 | 3368 |
foreach ( $default_types as $fallback ) { |
5 | 3369 |
if ( ! empty( $atts[ $fallback ] ) ) { |
3370 |
if ( empty( $fileurl ) ) { |
|
3371 |
$fileurl = $atts[ $fallback ]; |
|
3372 |
} |
|
3373 |
if ( 'src' === $fallback && $is_youtube ) { |
|
3374 |
$type = array( 'type' => 'video/youtube' ); |
|
3375 |
} elseif ( 'src' === $fallback && $is_vimeo ) { |
|
3376 |
$type = array( 'type' => 'video/vimeo' ); |
|
3377 |
} else { |
|
3378 |
$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); |
|
3379 |
} |
|
9 | 3380 |
$url = add_query_arg( '_', $instance, $atts[ $fallback ] ); |
5 | 3381 |
$html .= sprintf( $source, $type['type'], esc_url( $url ) ); |
0 | 3382 |
} |
3383 |
} |
|
5 | 3384 |
|
3385 |
if ( ! empty( $content ) ) { |
|
3386 |
if ( false !== strpos( $content, "\n" ) ) { |
|
3387 |
$content = str_replace( array( "\r\n", "\n", "\t" ), '', $content ); |
|
3388 |
} |
|
3389 |
$html .= trim( $content ); |
|
3390 |
} |
|
3391 |
||
3392 |
if ( 'mediaelement' === $library ) { |
|
0 | 3393 |
$html .= wp_mediaelement_fallback( $fileurl ); |
5 | 3394 |
} |
0 | 3395 |
$html .= '</video>'; |
3396 |
||
5 | 3397 |
$width_rule = ''; |
3398 |
if ( ! empty( $atts['width'] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3399 |
$width_rule = sprintf( 'width: %dpx;', $atts['width'] ); |
5 | 3400 |
} |
3401 |
$output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html ); |
|
3402 |
||
3403 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3404 |
* Filters the output of the video shortcode. |
5 | 3405 |
* |
3406 |
* @since 3.6.0 |
|
3407 |
* |
|
3408 |
* @param string $output Video shortcode HTML output. |
|
3409 |
* @param array $atts Array of video shortcode attributes. |
|
3410 |
* @param string $video Video file. |
|
3411 |
* @param int $post_id Post ID. |
|
3412 |
* @param string $library Media library used for the video shortcode. |
|
3413 |
*/ |
|
3414 |
return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library ); |
|
0 | 3415 |
} |
3416 |
add_shortcode( 'video', 'wp_video_shortcode' ); |
|
3417 |
||
3418 |
/** |
|
18 | 3419 |
* Gets the previous image link that has the same post parent. |
3420 |
* |
|
3421 |
* @since 5.8.0 |
|
3422 |
* |
|
3423 |
* @see get_adjacent_image_link() |
|
3424 |
* |
|
3425 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3426 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3427 |
* @param string|false $text Optional. Link text. Default false. |
|
3428 |
* @return string Markup for previous image link. |
|
3429 |
*/ |
|
3430 |
function get_previous_image_link( $size = 'thumbnail', $text = false ) { |
|
3431 |
return get_adjacent_image_link( true, $size, $text ); |
|
3432 |
} |
|
3433 |
||
3434 |
/** |
|
5 | 3435 |
* Displays previous image link that has the same post parent. |
0 | 3436 |
* |
3437 |
* @since 2.5.0 |
|
5 | 3438 |
* |
18 | 3439 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3440 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3441 |
* @param string|false $text Optional. Link text. Default false. |
|
0 | 3442 |
*/ |
5 | 3443 |
function previous_image_link( $size = 'thumbnail', $text = false ) { |
18 | 3444 |
echo get_previous_image_link( $size, $text ); |
3445 |
} |
|
3446 |
||
3447 |
/** |
|
3448 |
* Gets the next image link that has the same post parent. |
|
3449 |
* |
|
3450 |
* @since 5.8.0 |
|
3451 |
* |
|
3452 |
* @see get_adjacent_image_link() |
|
3453 |
* |
|
3454 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3455 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3456 |
* @param string|false $text Optional. Link text. Default false. |
|
3457 |
* @return string Markup for next image link. |
|
3458 |
*/ |
|
3459 |
function get_next_image_link( $size = 'thumbnail', $text = false ) { |
|
3460 |
return get_adjacent_image_link( false, $size, $text ); |
|
0 | 3461 |
} |
3462 |
||
3463 |
/** |
|
5 | 3464 |
* Displays next image link that has the same post parent. |
0 | 3465 |
* |
3466 |
* @since 2.5.0 |
|
5 | 3467 |
* |
18 | 3468 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3469 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3470 |
* @param string|false $text Optional. Link text. Default false. |
|
0 | 3471 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3472 |
function next_image_link( $size = 'thumbnail', $text = false ) { |
18 | 3473 |
echo get_next_image_link( $size, $text ); |
0 | 3474 |
} |
3475 |
||
3476 |
/** |
|
18 | 3477 |
* Gets the next or previous image link that has the same post parent. |
0 | 3478 |
* |
3479 |
* Retrieves the current attachment object from the $post global. |
|
3480 |
* |
|
18 | 3481 |
* @since 5.8.0 |
0 | 3482 |
* |
5 | 3483 |
* @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. |
18 | 3484 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3485 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
5 | 3486 |
* @param bool $text Optional. Link text. Default false. |
18 | 3487 |
* @return string Markup for image link. |
0 | 3488 |
*/ |
18 | 3489 |
function get_adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { |
9 | 3490 |
$post = get_post(); |
3491 |
$attachments = array_values( |
|
3492 |
get_children( |
|
3493 |
array( |
|
3494 |
'post_parent' => $post->post_parent, |
|
3495 |
'post_status' => 'inherit', |
|
3496 |
'post_type' => 'attachment', |
|
3497 |
'post_mime_type' => 'image', |
|
3498 |
'order' => 'ASC', |
|
3499 |
'orderby' => 'menu_order ID', |
|
3500 |
) |
|
3501 |
) |
|
3502 |
); |
|
0 | 3503 |
|
5 | 3504 |
foreach ( $attachments as $k => $attachment ) { |
18 | 3505 |
if ( (int) $attachment->ID === (int) $post->ID ) { |
0 | 3506 |
break; |
5 | 3507 |
} |
3508 |
} |
|
3509 |
||
9 | 3510 |
$output = ''; |
5 | 3511 |
$attachment_id = 0; |
3512 |
||
3513 |
if ( $attachments ) { |
|
3514 |
$k = $prev ? $k - 1 : $k + 1; |
|
3515 |
||
3516 |
if ( isset( $attachments[ $k ] ) ) { |
|
3517 |
$attachment_id = $attachments[ $k ]->ID; |
|
18 | 3518 |
$attr = array( 'alt' => get_the_title( $attachment_id ) ); |
3519 |
$output = wp_get_attachment_link( $attachment_id, $size, true, false, $text, $attr ); |
|
5 | 3520 |
} |
0 | 3521 |
} |
3522 |
||
3523 |
$adjacent = $prev ? 'previous' : 'next'; |
|
5 | 3524 |
|
3525 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3526 |
* Filters the adjacent image link. |
5 | 3527 |
* |
3528 |
* The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency, |
|
3529 |
* either 'next', or 'previous'. |
|
3530 |
* |
|
18 | 3531 |
* Possible hook names include: |
3532 |
* |
|
3533 |
* - `next_image_link` |
|
3534 |
* - `previous_image_link` |
|
3535 |
* |
|
5 | 3536 |
* @since 3.5.0 |
3537 |
* |
|
3538 |
* @param string $output Adjacent image HTML markup. |
|
3539 |
* @param int $attachment_id Attachment ID |
|
18 | 3540 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
3541 |
* an array of width and height values in pixels (in that order). |
|
5 | 3542 |
* @param string $text Link text. |
3543 |
*/ |
|
18 | 3544 |
return apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); |
3545 |
} |
|
3546 |
||
3547 |
/** |
|
3548 |
* Displays next or previous image link that has the same post parent. |
|
3549 |
* |
|
3550 |
* Retrieves the current attachment object from the $post global. |
|
3551 |
* |
|
3552 |
* @since 2.5.0 |
|
3553 |
* |
|
3554 |
* @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. |
|
3555 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3556 |
* of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3557 |
* @param bool $text Optional. Link text. Default false. |
|
3558 |
*/ |
|
3559 |
function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { |
|
3560 |
echo get_adjacent_image_link( $prev, $size, $text ); |
|
0 | 3561 |
} |
3562 |
||
3563 |
/** |
|
5 | 3564 |
* Retrieves taxonomies attached to given the attachment. |
0 | 3565 |
* |
3566 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3567 |
* @since 4.7.0 Introduced the `$output` parameter. |
0 | 3568 |
* |
5 | 3569 |
* @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
|
3570 |
* @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
|
3571 |
* or 'objects' to return an array of taxonomy objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3572 |
* Default is 'names'. |
16 | 3573 |
* @return string[]|WP_Taxonomy[] List of taxonomies or taxonomy names. Empty array on failure. |
0 | 3574 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3575 |
function get_attachment_taxonomies( $attachment, $output = 'names' ) { |
5 | 3576 |
if ( is_int( $attachment ) ) { |
3577 |
$attachment = get_post( $attachment ); |
|
3578 |
} elseif ( is_array( $attachment ) ) { |
|
0 | 3579 |
$attachment = (object) $attachment; |
5 | 3580 |
} |
16 | 3581 |
|
9 | 3582 |
if ( ! is_object( $attachment ) ) { |
0 | 3583 |
return array(); |
9 | 3584 |
} |
3585 |
||
3586 |
$file = get_attached_file( $attachment->ID ); |
|
3587 |
$filename = wp_basename( $file ); |
|
3588 |
||
3589 |
$objects = array( 'attachment' ); |
|
3590 |
||
3591 |
if ( false !== strpos( $filename, '.' ) ) { |
|
3592 |
$objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 ); |
|
3593 |
} |
|
16 | 3594 |
|
9 | 3595 |
if ( ! empty( $attachment->post_mime_type ) ) { |
0 | 3596 |
$objects[] = 'attachment:' . $attachment->post_mime_type; |
16 | 3597 |
|
9 | 3598 |
if ( false !== strpos( $attachment->post_mime_type, '/' ) ) { |
3599 |
foreach ( explode( '/', $attachment->post_mime_type ) as $token ) { |
|
3600 |
if ( ! empty( $token ) ) { |
|
0 | 3601 |
$objects[] = "attachment:$token"; |
9 | 3602 |
} |
3603 |
} |
|
3604 |
} |
|
0 | 3605 |
} |
3606 |
||
3607 |
$taxonomies = array(); |
|
16 | 3608 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3609 |
foreach ( $objects as $object ) { |
16 | 3610 |
$taxes = get_object_taxonomies( $object, $output ); |
3611 |
||
3612 |
if ( $taxes ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3613 |
$taxonomies = array_merge( $taxonomies, $taxes ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3614 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3615 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3616 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3617 |
if ( 'names' === $output ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3618 |
$taxonomies = array_unique( $taxonomies ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3619 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3620 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3621 |
return $taxonomies; |
0 | 3622 |
} |
3623 |
||
3624 |
/** |
|
9 | 3625 |
* Retrieves all of the taxonomies that are registered for attachments. |
0 | 3626 |
* |
3627 |
* Handles mime-type-specific taxonomies such as attachment:image and attachment:video. |
|
3628 |
* |
|
3629 |
* @since 3.5.0 |
|
16 | 3630 |
* |
5 | 3631 |
* @see get_taxonomies() |
0 | 3632 |
* |
5 | 3633 |
* @param string $output Optional. The type of taxonomy output to return. Accepts 'names' or 'objects'. |
3634 |
* Default 'names'. |
|
9 | 3635 |
* @return string[]|WP_Taxonomy[] Array of names or objects of registered taxonomies for attachments. |
0 | 3636 |
*/ |
3637 |
function get_taxonomies_for_attachments( $output = 'names' ) { |
|
3638 |
$taxonomies = array(); |
|
16 | 3639 |
|
0 | 3640 |
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) { |
3641 |
foreach ( $taxonomy->object_type as $object_type ) { |
|
16 | 3642 |
if ( 'attachment' === $object_type || 0 === strpos( $object_type, 'attachment:' ) ) { |
3643 |
if ( 'names' === $output ) { |
|
0 | 3644 |
$taxonomies[] = $taxonomy->name; |
9 | 3645 |
} else { |
0 | 3646 |
$taxonomies[ $taxonomy->name ] = $taxonomy; |
9 | 3647 |
} |
0 | 3648 |
break; |
3649 |
} |
|
3650 |
} |
|
3651 |
} |
|
3652 |
||
3653 |
return $taxonomies; |
|
3654 |
} |
|
3655 |
||
3656 |
/** |
|
18 | 3657 |
* Determines whether the value is an acceptable type for GD image functions. |
3658 |
* |
|
3659 |
* In PHP 8.0, the GD extension uses GdImage objects for its data structures. |
|
3660 |
* This function checks if the passed value is either a resource of type `gd` |
|
3661 |
* or a GdImage object instance. Any other type will return false. |
|
3662 |
* |
|
3663 |
* @since 5.6.0 |
|
3664 |
* |
|
3665 |
* @param resource|GdImage|false $image A value to check the type for. |
|
3666 |
* @return bool True if $image is either a GD image resource or GdImage instance, |
|
3667 |
* false otherwise. |
|
3668 |
*/ |
|
3669 |
function is_gd_image( $image ) { |
|
3670 |
if ( is_resource( $image ) && 'gd' === get_resource_type( $image ) |
|
3671 |
|| is_object( $image ) && $image instanceof GdImage |
|
3672 |
) { |
|
3673 |
return true; |
|
3674 |
} |
|
3675 |
||
3676 |
return false; |
|
3677 |
} |
|
3678 |
||
3679 |
/** |
|
0 | 3680 |
* Create new GD image resource with transparency support |
5 | 3681 |
* |
16 | 3682 |
* @todo Deprecate if possible. |
0 | 3683 |
* |
3684 |
* @since 2.9.0 |
|
3685 |
* |
|
5 | 3686 |
* @param int $width Image width in pixels. |
18 | 3687 |
* @param int $height Image height in pixels. |
3688 |
* @return resource|GdImage|false The GD image resource or GdImage instance on success. |
|
3689 |
* False on failure. |
|
0 | 3690 |
*/ |
9 | 3691 |
function wp_imagecreatetruecolor( $width, $height ) { |
3692 |
$img = imagecreatetruecolor( $width, $height ); |
|
18 | 3693 |
|
3694 |
if ( is_gd_image( $img ) |
|
3695 |
&& function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) |
|
3696 |
) { |
|
9 | 3697 |
imagealphablending( $img, false ); |
3698 |
imagesavealpha( $img, true ); |
|
0 | 3699 |
} |
18 | 3700 |
|
0 | 3701 |
return $img; |
3702 |
} |
|
3703 |
||
3704 |
/** |
|
3705 |
* Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. |
|
3706 |
* |
|
3707 |
* @since 2.9.0 |
|
5 | 3708 |
* |
3709 |
* @see wp_constrain_dimensions() |
|
0 | 3710 |
* |
5 | 3711 |
* @param int $example_width The width of an example embed. |
0 | 3712 |
* @param int $example_height The height of an example embed. |
5 | 3713 |
* @param int $max_width The maximum allowed width. |
3714 |
* @param int $max_height The maximum allowed height. |
|
16 | 3715 |
* @return int[] { |
3716 |
* An array of maximum width and height values. |
|
3717 |
* |
|
3718 |
* @type int $0 The maximum width in pixels. |
|
3719 |
* @type int $1 The maximum height in pixels. |
|
3720 |
* } |
|
0 | 3721 |
*/ |
3722 |
function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) { |
|
3723 |
$example_width = (int) $example_width; |
|
3724 |
$example_height = (int) $example_height; |
|
3725 |
$max_width = (int) $max_width; |
|
3726 |
$max_height = (int) $max_height; |
|
3727 |
||
3728 |
return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); |
|
3729 |
} |
|
3730 |
||
3731 |
/** |
|
5 | 3732 |
* Determines the maximum upload size allowed in php.ini. |
0 | 3733 |
* |
3734 |
* @since 2.5.0 |
|
3735 |
* |
|
3736 |
* @return int Allowed upload size. |
|
3737 |
*/ |
|
3738 |
function wp_max_upload_size() { |
|
3739 |
$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) ); |
|
3740 |
$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) ); |
|
5 | 3741 |
|
3742 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3743 |
* Filters the maximum upload size allowed in php.ini. |
5 | 3744 |
* |
3745 |
* @since 2.5.0 |
|
3746 |
* |
|
3747 |
* @param int $size Max upload size limit in bytes. |
|
3748 |
* @param int $u_bytes Maximum upload filesize in bytes. |
|
3749 |
* @param int $p_bytes Maximum size of POST data in bytes. |
|
3750 |
*/ |
|
3751 |
return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes ); |
|
0 | 3752 |
} |
3753 |
||
3754 |
/** |
|
3755 |
* Returns a WP_Image_Editor instance and loads file into it. |
|
3756 |
* |
|
3757 |
* @since 3.5.0 |
|
3758 |
* |
|
5 | 3759 |
* @param string $path Path to the file to load. |
3760 |
* @param array $args Optional. Additional arguments for retrieving the image editor. |
|
3761 |
* Default empty array. |
|
18 | 3762 |
* @return WP_Image_Editor|WP_Error The WP_Image_Editor object on success, |
3763 |
* a WP_Error object otherwise. |
|
0 | 3764 |
*/ |
3765 |
function wp_get_image_editor( $path, $args = array() ) { |
|
3766 |
$args['path'] = $path; |
|
3767 |
||
3768 |
if ( ! isset( $args['mime_type'] ) ) { |
|
3769 |
$file_info = wp_check_filetype( $args['path'] ); |
|
3770 |
||
3771 |
// If $file_info['type'] is false, then we let the editor attempt to |
|
3772 |
// figure out the file type, rather than forcing a failure based on extension. |
|
9 | 3773 |
if ( isset( $file_info ) && $file_info['type'] ) { |
0 | 3774 |
$args['mime_type'] = $file_info['type']; |
9 | 3775 |
} |
0 | 3776 |
} |
3777 |
||
3778 |
$implementation = _wp_image_editor_choose( $args ); |
|
3779 |
||
3780 |
if ( $implementation ) { |
|
3781 |
$editor = new $implementation( $path ); |
|
3782 |
$loaded = $editor->load(); |
|
3783 |
||
9 | 3784 |
if ( is_wp_error( $loaded ) ) { |
0 | 3785 |
return $loaded; |
9 | 3786 |
} |
0 | 3787 |
|
3788 |
return $editor; |
|
3789 |
} |
|
3790 |
||
9 | 3791 |
return new WP_Error( 'image_no_editor', __( 'No editor could be selected.' ) ); |
0 | 3792 |
} |
3793 |
||
3794 |
/** |
|
3795 |
* Tests whether there is an editor that supports a given mime type or methods. |
|
3796 |
* |
|
3797 |
* @since 3.5.0 |
|
3798 |
* |
|
5 | 3799 |
* @param string|array $args Optional. Array of arguments to retrieve the image editor supports. |
3800 |
* Default empty array. |
|
3801 |
* @return bool True if an eligible editor is found; false otherwise. |
|
0 | 3802 |
*/ |
3803 |
function wp_image_editor_supports( $args = array() ) { |
|
3804 |
return (bool) _wp_image_editor_choose( $args ); |
|
3805 |
} |
|
3806 |
||
3807 |
/** |
|
3808 |
* Tests which editors are capable of supporting the request. |
|
3809 |
* |
|
5 | 3810 |
* @ignore |
0 | 3811 |
* @since 3.5.0 |
3812 |
* |
|
5 | 3813 |
* @param array $args Optional. Array of arguments for choosing a capable editor. Default empty array. |
18 | 3814 |
* @return string|false Class name for the first editor that claims to support the request. |
3815 |
* False if no editor claims to support the request. |
|
0 | 3816 |
*/ |
3817 |
function _wp_image_editor_choose( $args = array() ) { |
|
3818 |
require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; |
|
3819 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; |
|
3820 |
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; |
|
5 | 3821 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3822 |
* Filters the list of image editing library classes. |
5 | 3823 |
* |
3824 |
* @since 3.5.0 |
|
3825 |
* |
|
16 | 3826 |
* @param string[] $image_editors Array of available image editor class names. Defaults are |
3827 |
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'. |
|
5 | 3828 |
*/ |
3829 |
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) ); |
|
0 | 3830 |
|
3831 |
foreach ( $implementations as $implementation ) { |
|
9 | 3832 |
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) { |
0 | 3833 |
continue; |
9 | 3834 |
} |
0 | 3835 |
|
3836 |
if ( isset( $args['mime_type'] ) && |
|
3837 |
! call_user_func( |
|
3838 |
array( $implementation, 'supports_mime_type' ), |
|
9 | 3839 |
$args['mime_type'] |
3840 |
) ) { |
|
0 | 3841 |
continue; |
3842 |
} |
|
3843 |
||
3844 |
if ( isset( $args['methods'] ) && |
|
9 | 3845 |
array_diff( $args['methods'], get_class_methods( $implementation ) ) ) { |
3846 |
||
0 | 3847 |
continue; |
3848 |
} |
|
3849 |
||
3850 |
return $implementation; |
|
3851 |
} |
|
3852 |
||
3853 |
return false; |
|
3854 |
} |
|
3855 |
||
3856 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3857 |
* Prints default Plupload arguments. |
0 | 3858 |
* |
3859 |
* @since 3.4.0 |
|
3860 |
*/ |
|
3861 |
function wp_plupload_default_settings() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3862 |
$wp_scripts = wp_scripts(); |
0 | 3863 |
|
3864 |
$data = $wp_scripts->get_data( 'wp-plupload', 'data' ); |
|
9 | 3865 |
if ( $data && false !== strpos( $data, '_wpPluploadSettings' ) ) { |
0 | 3866 |
return; |
9 | 3867 |
} |
3868 |
||
3869 |
$max_upload_size = wp_max_upload_size(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3870 |
$allowed_extensions = array_keys( get_allowed_mime_types() ); |
9 | 3871 |
$extensions = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3872 |
foreach ( $allowed_extensions as $extension ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3873 |
$extensions = array_merge( $extensions, explode( '|', $extension ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3874 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3875 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3876 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3877 |
* 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
|
3878 |
* 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
|
3879 |
*/ |
0 | 3880 |
$defaults = array( |
16 | 3881 |
'file_data_name' => 'async-upload', // Key passed to $_FILE. |
9 | 3882 |
'url' => admin_url( 'async-upload.php', 'relative' ), |
3883 |
'filters' => array( |
|
3884 |
'max_file_size' => $max_upload_size . 'b', |
|
3885 |
'mime_types' => array( array( 'extensions' => implode( ',', $extensions ) ) ), |
|
5 | 3886 |
), |
0 | 3887 |
); |
3888 |
||
16 | 3889 |
/* |
3890 |
* Currently only iOS Safari supports multiple files uploading, |
|
3891 |
* but iOS 7.x has a bug that prevents uploading of videos when enabled. |
|
3892 |
* See #29602. |
|
3893 |
*/ |
|
5 | 3894 |
if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false && |
3895 |
strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) { |
|
3896 |
||
0 | 3897 |
$defaults['multi_selection'] = false; |
5 | 3898 |
} |
3899 |
||
18 | 3900 |
// Check if WebP images can be edited. |
3901 |
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { |
|
3902 |
$defaults['webp_upload_error'] = true; |
|
3903 |
} |
|
3904 |
||
5 | 3905 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3906 |
* Filters the Plupload default settings. |
5 | 3907 |
* |
3908 |
* @since 3.4.0 |
|
3909 |
* |
|
3910 |
* @param array $defaults Default Plupload settings array. |
|
3911 |
*/ |
|
0 | 3912 |
$defaults = apply_filters( 'plupload_default_settings', $defaults ); |
3913 |
||
3914 |
$params = array( |
|
3915 |
'action' => 'upload-attachment', |
|
3916 |
); |
|
3917 |
||
5 | 3918 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3919 |
* Filters the Plupload default parameters. |
5 | 3920 |
* |
3921 |
* @since 3.4.0 |
|
3922 |
* |
|
3923 |
* @param array $params Default Plupload parameters array. |
|
3924 |
*/ |
|
16 | 3925 |
$params = apply_filters( 'plupload_default_params', $params ); |
3926 |
||
3927 |
$params['_wpnonce'] = wp_create_nonce( 'media-form' ); |
|
3928 |
||
0 | 3929 |
$defaults['multipart_params'] = $params; |
3930 |
||
3931 |
$settings = array( |
|
9 | 3932 |
'defaults' => $defaults, |
3933 |
'browser' => array( |
|
0 | 3934 |
'mobile' => wp_is_mobile(), |
3935 |
'supported' => _device_can_upload(), |
|
3936 |
), |
|
9 | 3937 |
'limitExceeded' => is_multisite() && ! is_upload_space_available(), |
0 | 3938 |
); |
3939 |
||
5 | 3940 |
$script = 'var _wpPluploadSettings = ' . wp_json_encode( $settings ) . ';'; |
0 | 3941 |
|
9 | 3942 |
if ( $data ) { |
0 | 3943 |
$script = "$data\n$script"; |
9 | 3944 |
} |
0 | 3945 |
|
3946 |
$wp_scripts->add_data( 'wp-plupload', 'data', $script ); |
|
3947 |
} |
|
3948 |
||
3949 |
/** |
|
3950 |
* Prepares an attachment post object for JS, where it is expected |
|
3951 |
* to be JSON-encoded and fit into an Attachment model. |
|
3952 |
* |
|
3953 |
* @since 3.5.0 |
|
3954 |
* |
|
9 | 3955 |
* @param int|WP_Post $attachment Attachment ID or object. |
18 | 3956 |
* @return array|void { |
3957 |
* Array of attachment details, or void if the parameter does not correspond to an attachment. |
|
3958 |
* |
|
3959 |
* @type string $alt Alt text of the attachment. |
|
3960 |
* @type string $author ID of the attachment author, as a string. |
|
3961 |
* @type string $authorName Name of the attachment author. |
|
3962 |
* @type string $caption Caption for the attachment. |
|
3963 |
* @type array $compat Containing item and meta. |
|
3964 |
* @type string $context Context, whether it's used as the site icon for example. |
|
3965 |
* @type int $date Uploaded date, timestamp in milliseconds. |
|
3966 |
* @type string $dateFormatted Formatted date (e.g. June 29, 2018). |
|
3967 |
* @type string $description Description of the attachment. |
|
3968 |
* @type string $editLink URL to the edit page for the attachment. |
|
3969 |
* @type string $filename File name of the attachment. |
|
3970 |
* @type string $filesizeHumanReadable Filesize of the attachment in human readable format (e.g. 1 MB). |
|
3971 |
* @type int $filesizeInBytes Filesize of the attachment in bytes. |
|
3972 |
* @type int $height If the attachment is an image, represents the height of the image in pixels. |
|
3973 |
* @type string $icon Icon URL of the attachment (e.g. /wp-includes/images/media/archive.png). |
|
3974 |
* @type int $id ID of the attachment. |
|
3975 |
* @type string $link URL to the attachment. |
|
3976 |
* @type int $menuOrder Menu order of the attachment post. |
|
3977 |
* @type array $meta Meta data for the attachment. |
|
3978 |
* @type string $mime Mime type of the attachment (e.g. image/jpeg or application/zip). |
|
3979 |
* @type int $modified Last modified, timestamp in milliseconds. |
|
3980 |
* @type string $name Name, same as title of the attachment. |
|
3981 |
* @type array $nonces Nonces for update, delete and edit. |
|
3982 |
* @type string $orientation If the attachment is an image, represents the image orientation |
|
3983 |
* (landscape or portrait). |
|
3984 |
* @type array $sizes If the attachment is an image, contains an array of arrays |
|
3985 |
* for the images sizes: thumbnail, medium, large, and full. |
|
3986 |
* @type string $status Post status of the attachment (usually 'inherit'). |
|
3987 |
* @type string $subtype Mime subtype of the attachment (usually the last part, e.g. jpeg or zip). |
|
3988 |
* @type string $title Title of the attachment (usually slugified file name without the extension). |
|
3989 |
* @type string $type Type of the attachment (usually first part of the mime type, e.g. image). |
|
3990 |
* @type int $uploadedTo Parent post to which the attachment was uploaded. |
|
3991 |
* @type string $uploadedToLink URL to the edit page of the parent post of the attachment. |
|
3992 |
* @type string $uploadedToTitle Post title of the parent of the attachment. |
|
3993 |
* @type string $url Direct URL to the attachment file (from wp-content). |
|
3994 |
* @type int $width If the attachment is an image, represents the width of the image in pixels. |
|
3995 |
* } |
|
3996 |
* |
|
0 | 3997 |
*/ |
3998 |
function wp_prepare_attachment_for_js( $attachment ) { |
|
16 | 3999 |
$attachment = get_post( $attachment ); |
4000 |
||
4001 |
if ( ! $attachment ) { |
|
0 | 4002 |
return; |
9 | 4003 |
} |
4004 |
||
16 | 4005 |
if ( 'attachment' !== $attachment->post_type ) { |
0 | 4006 |
return; |
9 | 4007 |
} |
0 | 4008 |
|
4009 |
$meta = wp_get_attachment_metadata( $attachment->ID ); |
|
9 | 4010 |
if ( false !== strpos( $attachment->post_mime_type, '/' ) ) { |
0 | 4011 |
list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); |
9 | 4012 |
} else { |
0 | 4013 |
list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); |
9 | 4014 |
} |
0 | 4015 |
|
4016 |
$attachment_url = wp_get_attachment_url( $attachment->ID ); |
|
9 | 4017 |
$base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url ); |
0 | 4018 |
|
4019 |
$response = array( |
|
9 | 4020 |
'id' => $attachment->ID, |
4021 |
'title' => $attachment->post_title, |
|
4022 |
'filename' => wp_basename( get_attached_file( $attachment->ID ) ), |
|
4023 |
'url' => $attachment_url, |
|
4024 |
'link' => get_attachment_link( $attachment->ID ), |
|
4025 |
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), |
|
4026 |
'author' => $attachment->post_author, |
|
4027 |
'description' => $attachment->post_content, |
|
4028 |
'caption' => $attachment->post_excerpt, |
|
4029 |
'name' => $attachment->post_name, |
|
4030 |
'status' => $attachment->post_status, |
|
4031 |
'uploadedTo' => $attachment->post_parent, |
|
4032 |
'date' => strtotime( $attachment->post_date_gmt ) * 1000, |
|
4033 |
'modified' => strtotime( $attachment->post_modified_gmt ) * 1000, |
|
4034 |
'menuOrder' => $attachment->menu_order, |
|
4035 |
'mime' => $attachment->post_mime_type, |
|
4036 |
'type' => $type, |
|
4037 |
'subtype' => $subtype, |
|
4038 |
'icon' => wp_mime_type_icon( $attachment->ID ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4039 |
'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ), |
9 | 4040 |
'nonces' => array( |
0 | 4041 |
'update' => false, |
4042 |
'delete' => false, |
|
9 | 4043 |
'edit' => false, |
0 | 4044 |
), |
9 | 4045 |
'editLink' => false, |
4046 |
'meta' => false, |
|
0 | 4047 |
); |
4048 |
||
5 | 4049 |
$author = new WP_User( $attachment->post_author ); |
18 | 4050 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4051 |
if ( $author->exists() ) { |
18 | 4052 |
$author_name = $author->display_name ? $author->display_name : $author->nickname; |
4053 |
$response['authorName'] = html_entity_decode( $author_name, ENT_QUOTES, get_bloginfo( 'charset' ) ); |
|
4054 |
$response['authorLink'] = get_edit_user_link( $author->ID ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4055 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4056 |
$response['authorName'] = __( '(no author)' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4057 |
} |
5 | 4058 |
|
4059 |
if ( $attachment->post_parent ) { |
|
4060 |
$post_parent = get_post( $attachment->post_parent ); |
|
18 | 4061 |
if ( $post_parent ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4062 |
$response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); |
18 | 4063 |
$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
|
4064 |
} |
5 | 4065 |
} |
4066 |
||
4067 |
$attached_file = get_attached_file( $attachment->ID ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4068 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4069 |
if ( isset( $meta['filesize'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4070 |
$bytes = $meta['filesize']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4071 |
} elseif ( file_exists( $attached_file ) ) { |
19 | 4072 |
$bytes = wp_filesize( $attached_file ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4073 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4074 |
$bytes = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4075 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4076 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4077 |
if ( $bytes ) { |
9 | 4078 |
$response['filesizeInBytes'] = $bytes; |
5 | 4079 |
$response['filesizeHumanReadable'] = size_format( $bytes ); |
4080 |
} |
|
4081 |
||
9 | 4082 |
$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
|
4083 |
$response['context'] = ( $context ) ? $context : ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4084 |
|
0 | 4085 |
if ( current_user_can( 'edit_post', $attachment->ID ) ) { |
4086 |
$response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID ); |
|
9 | 4087 |
$response['nonces']['edit'] = wp_create_nonce( 'image_editor-' . $attachment->ID ); |
4088 |
$response['editLink'] = get_edit_post_link( $attachment->ID, 'raw' ); |
|
0 | 4089 |
} |
4090 |
||
9 | 4091 |
if ( current_user_can( 'delete_post', $attachment->ID ) ) { |
0 | 4092 |
$response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID ); |
9 | 4093 |
} |
0 | 4094 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4095 |
if ( $meta && ( 'image' === $type || ! empty( $meta['sizes'] ) ) ) { |
0 | 4096 |
$sizes = array(); |
5 | 4097 |
|
0 | 4098 |
/** This filter is documented in wp-admin/includes/media.php */ |
9 | 4099 |
$possible_sizes = apply_filters( |
4100 |
'image_size_names_choose', |
|
4101 |
array( |
|
4102 |
'thumbnail' => __( 'Thumbnail' ), |
|
4103 |
'medium' => __( 'Medium' ), |
|
4104 |
'large' => __( 'Large' ), |
|
4105 |
'full' => __( 'Full Size' ), |
|
4106 |
) |
|
4107 |
); |
|
0 | 4108 |
unset( $possible_sizes['full'] ); |
4109 |
||
16 | 4110 |
/* |
4111 |
* Loop through all potential sizes that may be chosen. Try to do this with some efficiency. |
|
4112 |
* First: run the image_downsize filter. If it returns something, we can use its data. |
|
4113 |
* If the filter does not return something, then image_downsize() is just an expensive way |
|
4114 |
* to check the image metadata, which we do second. |
|
4115 |
*/ |
|
0 | 4116 |
foreach ( $possible_sizes as $size => $label ) { |
5 | 4117 |
|
4118 |
/** This filter is documented in wp-includes/media.php */ |
|
16 | 4119 |
$downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ); |
4120 |
||
4121 |
if ( $downsize ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4122 |
if ( empty( $downsize[3] ) ) { |
0 | 4123 |
continue; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4124 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4125 |
|
0 | 4126 |
$sizes[ $size ] = array( |
4127 |
'height' => $downsize[2], |
|
4128 |
'width' => $downsize[1], |
|
4129 |
'url' => $downsize[0], |
|
4130 |
'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape', |
|
4131 |
); |
|
4132 |
} elseif ( isset( $meta['sizes'][ $size ] ) ) { |
|
4133 |
// Nothing from the filter, so consult image metadata if we have it. |
|
4134 |
$size_meta = $meta['sizes'][ $size ]; |
|
4135 |
||
4136 |
// We have the actual image size, but might need to further constrain it if content_width is narrower. |
|
4137 |
// Thumbnail, medium, and full sizes are also checked against the site's height/width options. |
|
4138 |
list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' ); |
|
4139 |
||
4140 |
$sizes[ $size ] = array( |
|
4141 |
'height' => $height, |
|
4142 |
'width' => $width, |
|
4143 |
'url' => $base_url . $size_meta['file'], |
|
4144 |
'orientation' => $height > $width ? 'portrait' : 'landscape', |
|
4145 |
); |
|
4146 |
} |
|
4147 |
} |
|
4148 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4149 |
if ( 'image' === $type ) { |
16 | 4150 |
if ( ! empty( $meta['original_image'] ) ) { |
4151 |
$response['originalImageURL'] = wp_get_original_image_url( $attachment->ID ); |
|
4152 |
$response['originalImageName'] = wp_basename( wp_get_original_image_path( $attachment->ID ) ); |
|
4153 |
} |
|
4154 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4155 |
$sizes['full'] = array( 'url' => $attachment_url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4156 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4157 |
if ( isset( $meta['height'], $meta['width'] ) ) { |
9 | 4158 |
$sizes['full']['height'] = $meta['height']; |
4159 |
$sizes['full']['width'] = $meta['width']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4160 |
$sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4161 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4162 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4163 |
$response = array_merge( $response, $sizes['full'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4164 |
} elseif ( $meta['sizes']['full']['file'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4165 |
$sizes['full'] = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4166 |
'url' => $base_url . $meta['sizes']['full']['file'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4167 |
'height' => $meta['sizes']['full']['height'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4168 |
'width' => $meta['sizes']['full']['width'], |
9 | 4169 |
'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
|
4170 |
); |
0 | 4171 |
} |
4172 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4173 |
$response = array_merge( $response, array( 'sizes' => $sizes ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4174 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4175 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4176 |
if ( $meta && 'video' === $type ) { |
9 | 4177 |
if ( isset( $meta['width'] ) ) { |
0 | 4178 |
$response['width'] = (int) $meta['width']; |
9 | 4179 |
} |
4180 |
if ( isset( $meta['height'] ) ) { |
|
0 | 4181 |
$response['height'] = (int) $meta['height']; |
9 | 4182 |
} |
0 | 4183 |
} |
4184 |
||
4185 |
if ( $meta && ( 'audio' === $type || 'video' === $type ) ) { |
|
9 | 4186 |
if ( isset( $meta['length_formatted'] ) ) { |
4187 |
$response['fileLength'] = $meta['length_formatted']; |
|
4188 |
$response['fileLengthHumanReadable'] = human_readable_duration( $meta['length_formatted'] ); |
|
4189 |
} |
|
5 | 4190 |
|
4191 |
$response['meta'] = array(); |
|
4192 |
foreach ( wp_get_attachment_id3_keys( $attachment, 'js' ) as $key => $label ) { |
|
4193 |
$response['meta'][ $key ] = false; |
|
4194 |
||
4195 |
if ( ! empty( $meta[ $key ] ) ) { |
|
4196 |
$response['meta'][ $key ] = $meta[ $key ]; |
|
4197 |
} |
|
4198 |
} |
|
4199 |
||
4200 |
$id = get_post_thumbnail_id( $attachment->ID ); |
|
4201 |
if ( ! empty( $id ) ) { |
|
4202 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' ); |
|
9 | 4203 |
$response['image'] = compact( 'src', 'width', 'height' ); |
5 | 4204 |
list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' ); |
9 | 4205 |
$response['thumb'] = compact( 'src', 'width', 'height' ); |
5 | 4206 |
} else { |
9 | 4207 |
$src = wp_mime_type_icon( $attachment->ID ); |
4208 |
$width = 48; |
|
4209 |
$height = 64; |
|
5 | 4210 |
$response['image'] = compact( 'src', 'width', 'height' ); |
4211 |
$response['thumb'] = compact( 'src', 'width', 'height' ); |
|
4212 |
} |
|
0 | 4213 |
} |
4214 |
||
9 | 4215 |
if ( function_exists( 'get_compat_media_markup' ) ) { |
0 | 4216 |
$response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) ); |
9 | 4217 |
} |
0 | 4218 |
|
18 | 4219 |
if ( function_exists( 'get_media_states' ) ) { |
4220 |
$media_states = get_media_states( $attachment ); |
|
4221 |
if ( ! empty( $media_states ) ) { |
|
4222 |
$response['mediaStates'] = implode( ', ', $media_states ); |
|
4223 |
} |
|
4224 |
} |
|
4225 |
||
5 | 4226 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4227 |
* Filters the attachment data prepared for JavaScript. |
5 | 4228 |
* |
4229 |
* @since 3.5.0 |
|
4230 |
* |
|
18 | 4231 |
* @param array $response Array of prepared attachment data. @see wp_prepare_attachment_for_js(). |
9 | 4232 |
* @param WP_Post $attachment Attachment object. |
4233 |
* @param array|false $meta Array of attachment meta data, or false if there is none. |
|
5 | 4234 |
*/ |
0 | 4235 |
return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); |
4236 |
} |
|
4237 |
||
4238 |
/** |
|
4239 |
* Enqueues all scripts, styles, settings, and templates necessary to use |
|
4240 |
* all media JS APIs. |
|
4241 |
* |
|
4242 |
* @since 3.5.0 |
|
5 | 4243 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4244 |
* @global int $content_width |
16 | 4245 |
* @global wpdb $wpdb WordPress database abstraction object. |
4246 |
* @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
|
4247 |
* |
5 | 4248 |
* @param array $args { |
4249 |
* Arguments for enqueuing media scripts. |
|
4250 |
* |
|
19 | 4251 |
* @type int|WP_Post $post A post object or ID. |
5 | 4252 |
* } |
0 | 4253 |
*/ |
4254 |
function wp_enqueue_media( $args = array() ) { |
|
4255 |
// Enqueue me just once per page, please. |
|
9 | 4256 |
if ( did_action( 'wp_enqueue_media' ) ) { |
0 | 4257 |
return; |
9 | 4258 |
} |
0 | 4259 |
|
5 | 4260 |
global $content_width, $wpdb, $wp_locale; |
4261 |
||
0 | 4262 |
$defaults = array( |
4263 |
'post' => null, |
|
4264 |
); |
|
9 | 4265 |
$args = wp_parse_args( $args, $defaults ); |
0 | 4266 |
|
4267 |
// We're going to pass the old thickbox media tabs to `media_upload_tabs` |
|
4268 |
// to ensure plugins will work. We will then unset those tabs. |
|
4269 |
$tabs = array( |
|
4270 |
// handler action suffix => tab label |
|
4271 |
'type' => '', |
|
4272 |
'type_url' => '', |
|
4273 |
'gallery' => '', |
|
4274 |
'library' => '', |
|
4275 |
); |
|
4276 |
||
5 | 4277 |
/** This filter is documented in wp-admin/includes/media.php */ |
0 | 4278 |
$tabs = apply_filters( 'media_upload_tabs', $tabs ); |
4279 |
unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] ); |
|
4280 |
||
4281 |
$props = array( |
|
16 | 4282 |
'link' => get_option( 'image_default_link_type' ), // DB default is 'file'. |
4283 |
'align' => get_option( 'image_default_align' ), // Empty default. |
|
4284 |
'size' => get_option( 'image_default_size' ), // Empty default. |
|
0 | 4285 |
); |
4286 |
||
9 | 4287 |
$exts = array_merge( wp_get_audio_extensions(), wp_get_video_extensions() ); |
4288 |
$mimes = get_allowed_mime_types(); |
|
5 | 4289 |
$ext_mimes = array(); |
4290 |
foreach ( $exts as $ext ) { |
|
4291 |
foreach ( $mimes as $ext_preg => $mime_match ) { |
|
4292 |
if ( preg_match( '#' . $ext . '#i', $ext_preg ) ) { |
|
4293 |
$ext_mimes[ $ext ] = $mime_match; |
|
4294 |
break; |
|
4295 |
} |
|
4296 |
} |
|
4297 |
} |
|
4298 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4299 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4300 |
* 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
|
4301 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4302 |
* 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
|
4303 |
* 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
|
4304 |
* 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
|
4305 |
* 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
|
4306 |
* expensive for large media libraries. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4307 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4308 |
* @since 4.7.4 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4309 |
* @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
|
4310 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4311 |
* @link https://core.trac.wordpress.org/ticket/31071 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4312 |
* |
16 | 4313 |
* @param bool|null $show Whether to show the button, or `null` to decide based |
4314 |
* 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
|
4315 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4316 |
$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
|
4317 |
if ( null === $show_audio_playlist ) { |
9 | 4318 |
$show_audio_playlist = $wpdb->get_var( |
4319 |
" |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4320 |
SELECT ID |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4321 |
FROM $wpdb->posts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4322 |
WHERE post_type = 'attachment' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4323 |
AND post_mime_type LIKE 'audio%' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4324 |
LIMIT 1 |
9 | 4325 |
" |
4326 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4327 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4328 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4329 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4330 |
* 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
|
4331 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4332 |
* 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
|
4333 |
* 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
|
4334 |
* 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
|
4335 |
* 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
|
4336 |
* expensive for large media libraries. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4337 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4338 |
* @since 4.7.4 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4339 |
* @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
|
4340 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4341 |
* @link https://core.trac.wordpress.org/ticket/31071 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4342 |
* |
16 | 4343 |
* @param bool|null $show Whether to show the button, or `null` to decide based |
4344 |
* 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
|
4345 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4346 |
$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
|
4347 |
if ( null === $show_video_playlist ) { |
9 | 4348 |
$show_video_playlist = $wpdb->get_var( |
4349 |
" |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4350 |
SELECT ID |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4351 |
FROM $wpdb->posts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4352 |
WHERE post_type = 'attachment' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4353 |
AND post_mime_type LIKE 'video%' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4354 |
LIMIT 1 |
9 | 4355 |
" |
4356 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4357 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4358 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4359 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4360 |
* 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
|
4361 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4362 |
* 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
|
4363 |
* 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
|
4364 |
* 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
|
4365 |
* override this behavior. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4366 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4367 |
* @since 4.7.4 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4368 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4369 |
* @link https://core.trac.wordpress.org/ticket/31071 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4370 |
* |
16 | 4371 |
* @param array|null $months An array of objects with `month` and `year` |
4372 |
* properties, or `null` (or any other non-array value) |
|
4373 |
* for default behavior. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4374 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4375 |
$months = apply_filters( 'media_library_months_with_files', null ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4376 |
if ( ! is_array( $months ) ) { |
9 | 4377 |
$months = $wpdb->get_results( |
4378 |
$wpdb->prepare( |
|
4379 |
" |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4380 |
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4381 |
FROM $wpdb->posts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4382 |
WHERE post_type = %s |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4383 |
ORDER BY post_date DESC |
9 | 4384 |
", |
4385 |
'attachment' |
|
4386 |
) |
|
4387 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4388 |
} |
5 | 4389 |
foreach ( $months as $month_year ) { |
16 | 4390 |
$month_year->text = sprintf( |
4391 |
/* translators: 1: Month, 2: Year. */ |
|
4392 |
__( '%1$s %2$d' ), |
|
4393 |
$wp_locale->get_month( $month_year->month ), |
|
4394 |
$month_year->year |
|
4395 |
); |
|
5 | 4396 |
} |
4397 |
||
18 | 4398 |
/** |
4399 |
* Filters whether the Media Library grid has infinite scrolling. Default `false`. |
|
4400 |
* |
|
4401 |
* @since 5.8.0 |
|
4402 |
* |
|
4403 |
* @param bool $infinite Whether the Media Library grid has infinite scrolling. |
|
4404 |
*/ |
|
4405 |
$infinite_scrolling = apply_filters( 'media_library_infinite_scrolling', false ); |
|
4406 |
||
0 | 4407 |
$settings = array( |
18 | 4408 |
'tabs' => $tabs, |
4409 |
'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ), |
|
4410 |
'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ), |
|
5 | 4411 |
/** This filter is documented in wp-admin/includes/media.php */ |
18 | 4412 |
'captions' => ! apply_filters( 'disable_captions', '' ), |
4413 |
'nonce' => array( |
|
0 | 4414 |
'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ), |
4415 |
), |
|
18 | 4416 |
'post' => array( |
0 | 4417 |
'id' => 0, |
4418 |
), |
|
18 | 4419 |
'defaultProps' => $props, |
4420 |
'attachmentCounts' => array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4421 |
'audio' => ( $show_audio_playlist ) ? 1 : 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4422 |
'video' => ( $show_video_playlist ) ? 1 : 0, |
5 | 4423 |
), |
18 | 4424 |
'oEmbedProxyUrl' => rest_url( 'oembed/1.0/proxy' ), |
4425 |
'embedExts' => $exts, |
|
4426 |
'embedMimes' => $ext_mimes, |
|
4427 |
'contentWidth' => $content_width, |
|
4428 |
'months' => $months, |
|
4429 |
'mediaTrash' => MEDIA_TRASH ? 1 : 0, |
|
4430 |
'infiniteScrolling' => ( $infinite_scrolling ) ? 1 : 0, |
|
0 | 4431 |
); |
4432 |
||
4433 |
$post = null; |
|
4434 |
if ( isset( $args['post'] ) ) { |
|
9 | 4435 |
$post = get_post( $args['post'] ); |
0 | 4436 |
$settings['post'] = array( |
9 | 4437 |
'id' => $post->ID, |
0 | 4438 |
'nonce' => wp_create_nonce( 'update-post_' . $post->ID ), |
4439 |
); |
|
4440 |
||
5 | 4441 |
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' ); |
4442 |
if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) { |
|
4443 |
if ( wp_attachment_is( 'audio', $post ) ) { |
|
4444 |
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); |
|
4445 |
} elseif ( wp_attachment_is( 'video', $post ) ) { |
|
4446 |
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); |
|
4447 |
} |
|
4448 |
} |
|
4449 |
||
4450 |
if ( $thumbnail_support ) { |
|
9 | 4451 |
$featured_image_id = get_post_meta( $post->ID, '_thumbnail_id', true ); |
0 | 4452 |
$settings['post']['featuredImageId'] = $featured_image_id ? $featured_image_id : -1; |
4453 |
} |
|
4454 |
} |
|
4455 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4456 |
if ( $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4457 |
$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
|
4458 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4459 |
$post_type_object = get_post_type_object( 'post' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4460 |
} |
0 | 4461 |
|
4462 |
$strings = array( |
|
16 | 4463 |
// Generic. |
4464 |
'mediaFrameDefaultTitle' => __( 'Media' ), |
|
9 | 4465 |
'url' => __( 'URL' ), |
16 | 4466 |
'addMedia' => __( 'Add media' ), |
9 | 4467 |
'search' => __( 'Search' ), |
4468 |
'select' => __( 'Select' ), |
|
4469 |
'cancel' => __( 'Cancel' ), |
|
4470 |
'update' => __( 'Update' ), |
|
4471 |
'replace' => __( 'Replace' ), |
|
4472 |
'remove' => __( 'Remove' ), |
|
4473 |
'back' => __( 'Back' ), |
|
4474 |
/* |
|
4475 |
* translators: This is a would-be plural string used in the media manager. |
|
4476 |
* If there is not a word you can use in your language to avoid issues with the |
|
4477 |
* lack of plural support here, turn it into "selected: %d" then translate it. |
|
0 | 4478 |
*/ |
9 | 4479 |
'selected' => __( '%d selected' ), |
4480 |
'dragInfo' => __( 'Drag and drop to reorder media files.' ), |
|
0 | 4481 |
|
16 | 4482 |
// Upload. |
4483 |
'uploadFilesTitle' => __( 'Upload files' ), |
|
4484 |
'uploadImagesTitle' => __( 'Upload images' ), |
|
4485 |
||
4486 |
// Library. |
|
9 | 4487 |
'mediaLibraryTitle' => __( 'Media Library' ), |
16 | 4488 |
'insertMediaTitle' => __( 'Add media' ), |
9 | 4489 |
'createNewGallery' => __( 'Create a new gallery' ), |
4490 |
'createNewPlaylist' => __( 'Create a new playlist' ), |
|
4491 |
'createNewVideoPlaylist' => __( 'Create a new video playlist' ), |
|
18 | 4492 |
'returnToLibrary' => __( '← Go to library' ), |
9 | 4493 |
'allMediaItems' => __( 'All media items' ), |
4494 |
'allDates' => __( 'All dates' ), |
|
4495 |
'noItemsFound' => __( 'No items found.' ), |
|
4496 |
'insertIntoPost' => $post_type_object->labels->insert_into_item, |
|
19 | 4497 |
'unattached' => _x( 'Unattached', 'media items' ), |
9 | 4498 |
'mine' => _x( 'Mine', 'media items' ), |
4499 |
'trash' => _x( 'Trash', 'noun' ), |
|
4500 |
'uploadedToThisPost' => $post_type_object->labels->uploaded_to_this_item, |
|
4501 |
'warnDelete' => __( "You are about to permanently delete this item from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ), |
|
4502 |
'warnBulkDelete' => __( "You are about to permanently delete these items from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ), |
|
4503 |
'warnBulkTrash' => __( "You are about to trash these items.\n 'Cancel' to stop, 'OK' to delete." ), |
|
16 | 4504 |
'bulkSelect' => __( 'Bulk select' ), |
9 | 4505 |
'trashSelected' => __( 'Move to Trash' ), |
4506 |
'restoreSelected' => __( 'Restore from Trash' ), |
|
16 | 4507 |
'deletePermanently' => __( 'Delete permanently' ), |
19 | 4508 |
'errorDeleting' => __( 'Error in deleting the attachment.' ), |
9 | 4509 |
'apply' => __( 'Apply' ), |
4510 |
'filterByDate' => __( 'Filter by date' ), |
|
4511 |
'filterByType' => __( 'Filter by type' ), |
|
16 | 4512 |
'searchLabel' => __( 'Search' ), |
4513 |
'searchMediaLabel' => __( 'Search media' ), // Backward compatibility pre-5.3. |
|
4514 |
'searchMediaPlaceholder' => __( 'Search media items...' ), // Placeholder (no ellipsis), backward compatibility pre-5.3. |
|
18 | 4515 |
/* translators: %d: Number of attachments found in a search. */ |
16 | 4516 |
'mediaFound' => __( 'Number of media items found: %d' ), |
4517 |
'noMedia' => __( 'No media items found.' ), |
|
4518 |
'noMediaTryNewSearch' => __( 'No media items found. Try a different search.' ), |
|
4519 |
||
4520 |
// Library Details. |
|
4521 |
'attachmentDetails' => __( 'Attachment details' ), |
|
4522 |
||
4523 |
// From URL. |
|
9 | 4524 |
'insertFromUrlTitle' => __( 'Insert from URL' ), |
0 | 4525 |
|
16 | 4526 |
// Featured Images. |
9 | 4527 |
'setFeaturedImageTitle' => $post_type_object->labels->featured_image, |
4528 |
'setFeaturedImage' => $post_type_object->labels->set_featured_image, |
|
0 | 4529 |
|
16 | 4530 |
// Gallery. |
4531 |
'createGalleryTitle' => __( 'Create gallery' ), |
|
4532 |
'editGalleryTitle' => __( 'Edit gallery' ), |
|
4533 |
'cancelGalleryTitle' => __( '← Cancel gallery' ), |
|
9 | 4534 |
'insertGallery' => __( 'Insert gallery' ), |
4535 |
'updateGallery' => __( 'Update gallery' ), |
|
4536 |
'addToGallery' => __( 'Add to gallery' ), |
|
16 | 4537 |
'addToGalleryTitle' => __( 'Add to gallery' ), |
9 | 4538 |
'reverseOrder' => __( 'Reverse order' ), |
5 | 4539 |
|
16 | 4540 |
// Edit Image. |
4541 |
'imageDetailsTitle' => __( 'Image details' ), |
|
4542 |
'imageReplaceTitle' => __( 'Replace image' ), |
|
4543 |
'imageDetailsCancel' => __( 'Cancel edit' ), |
|
4544 |
'editImage' => __( 'Edit image' ), |
|
4545 |
||
4546 |
// Crop Image. |
|
4547 |
'chooseImage' => __( 'Choose image' ), |
|
4548 |
'selectAndCrop' => __( 'Select and crop' ), |
|
4549 |
'skipCropping' => __( 'Skip cropping' ), |
|
4550 |
'cropImage' => __( 'Crop image' ), |
|
9 | 4551 |
'cropYourImage' => __( 'Crop your image' ), |
4552 |
'cropping' => __( 'Cropping…' ), |
|
16 | 4553 |
/* translators: 1: Suggested width number, 2: Suggested height number. */ |
9 | 4554 |
'suggestedDimensions' => __( 'Suggested image dimensions: %1$s by %2$s pixels.' ), |
4555 |
'cropError' => __( 'There has been an error cropping your image.' ), |
|
5 | 4556 |
|
16 | 4557 |
// Edit Audio. |
4558 |
'audioDetailsTitle' => __( 'Audio details' ), |
|
4559 |
'audioReplaceTitle' => __( 'Replace audio' ), |
|
4560 |
'audioAddSourceTitle' => __( 'Add audio source' ), |
|
4561 |
'audioDetailsCancel' => __( 'Cancel edit' ), |
|
4562 |
||
4563 |
// Edit Video. |
|
4564 |
'videoDetailsTitle' => __( 'Video details' ), |
|
4565 |
'videoReplaceTitle' => __( 'Replace video' ), |
|
4566 |
'videoAddSourceTitle' => __( 'Add video source' ), |
|
4567 |
'videoDetailsCancel' => __( 'Cancel edit' ), |
|
4568 |
'videoSelectPosterImageTitle' => __( 'Select poster image' ), |
|
4569 |
'videoAddTrackTitle' => __( 'Add subtitles' ), |
|
4570 |
||
4571 |
// Playlist. |
|
9 | 4572 |
'playlistDragInfo' => __( 'Drag and drop to reorder tracks.' ), |
16 | 4573 |
'createPlaylistTitle' => __( 'Create audio playlist' ), |
4574 |
'editPlaylistTitle' => __( 'Edit audio playlist' ), |
|
4575 |
'cancelPlaylistTitle' => __( '← Cancel audio playlist' ), |
|
9 | 4576 |
'insertPlaylist' => __( 'Insert audio playlist' ), |
4577 |
'updatePlaylist' => __( 'Update audio playlist' ), |
|
4578 |
'addToPlaylist' => __( 'Add to audio playlist' ), |
|
4579 |
'addToPlaylistTitle' => __( 'Add to Audio Playlist' ), |
|
4580 |
||
16 | 4581 |
// Video Playlist. |
9 | 4582 |
'videoPlaylistDragInfo' => __( 'Drag and drop to reorder videos.' ), |
16 | 4583 |
'createVideoPlaylistTitle' => __( 'Create video playlist' ), |
4584 |
'editVideoPlaylistTitle' => __( 'Edit video playlist' ), |
|
4585 |
'cancelVideoPlaylistTitle' => __( '← Cancel video playlist' ), |
|
9 | 4586 |
'insertVideoPlaylist' => __( 'Insert video playlist' ), |
4587 |
'updateVideoPlaylist' => __( 'Update video playlist' ), |
|
4588 |
'addToVideoPlaylist' => __( 'Add to video playlist' ), |
|
16 | 4589 |
'addToVideoPlaylistTitle' => __( 'Add to video Playlist' ), |
4590 |
||
4591 |
// Headings. |
|
4592 |
'filterAttachments' => __( 'Filter media' ), |
|
4593 |
'attachmentsList' => __( 'Media list' ), |
|
0 | 4594 |
); |
4595 |
||
5 | 4596 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4597 |
* Filters the media view settings. |
5 | 4598 |
* |
4599 |
* @since 3.5.0 |
|
4600 |
* |
|
4601 |
* @param array $settings List of media view settings. |
|
4602 |
* @param WP_Post $post Post object. |
|
4603 |
*/ |
|
0 | 4604 |
$settings = apply_filters( 'media_view_settings', $settings, $post ); |
5 | 4605 |
|
4606 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4607 |
* Filters the media view strings. |
5 | 4608 |
* |
4609 |
* @since 3.5.0 |
|
4610 |
* |
|
16 | 4611 |
* @param string[] $strings Array of media view strings keyed by the name they'll be referenced by in JavaScript. |
4612 |
* @param WP_Post $post Post object. |
|
5 | 4613 |
*/ |
9 | 4614 |
$strings = apply_filters( 'media_view_strings', $strings, $post ); |
0 | 4615 |
|
4616 |
$strings['settings'] = $settings; |
|
4617 |
||
16 | 4618 |
// Ensure we enqueue media-editor first, that way media-views |
4619 |
// is registered internally before we try to localize it. See #24724. |
|
5 | 4620 |
wp_enqueue_script( 'media-editor' ); |
0 | 4621 |
wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings ); |
4622 |
||
5 | 4623 |
wp_enqueue_script( 'media-audiovideo' ); |
0 | 4624 |
wp_enqueue_style( 'media-views' ); |
5 | 4625 |
if ( is_admin() ) { |
4626 |
wp_enqueue_script( 'mce-view' ); |
|
4627 |
wp_enqueue_script( 'image-edit' ); |
|
4628 |
} |
|
4629 |
wp_enqueue_style( 'imgareaselect' ); |
|
0 | 4630 |
wp_plupload_default_settings(); |
4631 |
||
4632 |
require_once ABSPATH . WPINC . '/media-template.php'; |
|
4633 |
add_action( 'admin_footer', 'wp_print_media_templates' ); |
|
4634 |
add_action( 'wp_footer', 'wp_print_media_templates' ); |
|
5 | 4635 |
add_action( 'customize_controls_print_footer_scripts', 'wp_print_media_templates' ); |
4636 |
||
4637 |
/** |
|
4638 |
* Fires at the conclusion of wp_enqueue_media(). |
|
4639 |
* |
|
4640 |
* @since 3.5.0 |
|
4641 |
*/ |
|
0 | 4642 |
do_action( 'wp_enqueue_media' ); |
4643 |
} |
|
4644 |
||
4645 |
/** |
|
5 | 4646 |
* Retrieves media attached to the passed post. |
0 | 4647 |
* |
4648 |
* @since 3.6.0 |
|
4649 |
* |
|
5 | 4650 |
* @param string $type Mime type. |
4651 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
|
16 | 4652 |
* @return WP_Post[] Array of media attached to the given post. |
0 | 4653 |
*/ |
4654 |
function get_attached_media( $type, $post = 0 ) { |
|
16 | 4655 |
$post = get_post( $post ); |
4656 |
||
4657 |
if ( ! $post ) { |
|
0 | 4658 |
return array(); |
9 | 4659 |
} |
0 | 4660 |
|
4661 |
$args = array( |
|
9 | 4662 |
'post_parent' => $post->ID, |
4663 |
'post_type' => 'attachment', |
|
0 | 4664 |
'post_mime_type' => $type, |
4665 |
'posts_per_page' => -1, |
|
9 | 4666 |
'orderby' => 'menu_order', |
4667 |
'order' => 'ASC', |
|
0 | 4668 |
); |
4669 |
||
5 | 4670 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4671 |
* Filters arguments used to retrieve media attached to the given post. |
5 | 4672 |
* |
4673 |
* @since 3.6.0 |
|
4674 |
* |
|
16 | 4675 |
* @param array $args Post query arguments. |
4676 |
* @param string $type Mime type of the desired media. |
|
4677 |
* @param WP_Post $post Post object. |
|
5 | 4678 |
*/ |
0 | 4679 |
$args = apply_filters( 'get_attached_media_args', $args, $type, $post ); |
4680 |
||
4681 |
$children = get_children( $args ); |
|
4682 |
||
5 | 4683 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4684 |
* Filters the list of media attached to the given post. |
5 | 4685 |
* |
4686 |
* @since 3.6.0 |
|
4687 |
* |
|
16 | 4688 |
* @param WP_Post[] $children Array of media attached to the given post. |
4689 |
* @param string $type Mime type of the media desired. |
|
4690 |
* @param WP_Post $post Post object. |
|
5 | 4691 |
*/ |
0 | 4692 |
return (array) apply_filters( 'get_attached_media', $children, $type, $post ); |
4693 |
} |
|
4694 |
||
4695 |
/** |
|
16 | 4696 |
* Check the content HTML for a audio, video, object, embed, or iframe tags. |
0 | 4697 |
* |
4698 |
* @since 3.6.0 |
|
4699 |
* |
|
16 | 4700 |
* @param string $content A string of HTML which might contain media elements. |
4701 |
* @param string[] $types An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'. |
|
4702 |
* @return string[] Array of found HTML media elements. |
|
0 | 4703 |
*/ |
4704 |
function get_media_embedded_in_content( $content, $types = null ) { |
|
4705 |
$html = array(); |
|
5 | 4706 |
|
4707 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4708 |
* Filters the embedded media types that are allowed to be returned from the content blob. |
5 | 4709 |
* |
4710 |
* @since 4.2.0 |
|
4711 |
* |
|
16 | 4712 |
* @param string[] $allowed_media_types An array of allowed media types. Default media types are |
4713 |
* 'audio', 'video', 'object', 'embed', and 'iframe'. |
|
5 | 4714 |
*/ |
4715 |
$allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) ); |
|
4716 |
||
0 | 4717 |
if ( ! empty( $types ) ) { |
5 | 4718 |
if ( ! is_array( $types ) ) { |
0 | 4719 |
$types = array( $types ); |
5 | 4720 |
} |
4721 |
||
0 | 4722 |
$allowed_media_types = array_intersect( $allowed_media_types, $types ); |
4723 |
} |
|
4724 |
||
5 | 4725 |
$tags = implode( '|', $allowed_media_types ); |
4726 |
||
4727 |
if ( preg_match_all( '#<(?P<tag>' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches ) ) { |
|
4728 |
foreach ( $matches[0] as $match ) { |
|
4729 |
$html[] = $match; |
|
0 | 4730 |
} |
4731 |
} |
|
4732 |
||
4733 |
return $html; |
|
4734 |
} |
|
4735 |
||
4736 |
/** |
|
5 | 4737 |
* Retrieves galleries from the passed post's content. |
0 | 4738 |
* |
4739 |
* @since 3.6.0 |
|
4740 |
* |
|
5 | 4741 |
* @param int|WP_Post $post Post ID or object. |
4742 |
* @param bool $html Optional. Whether to return HTML or data in the array. Default true. |
|
0 | 4743 |
* @return array A list of arrays, each containing gallery data and srcs parsed |
5 | 4744 |
* from the expanded shortcode. |
0 | 4745 |
*/ |
4746 |
function get_post_galleries( $post, $html = true ) { |
|
16 | 4747 |
$post = get_post( $post ); |
4748 |
||
4749 |
if ( ! $post ) { |
|
0 | 4750 |
return array(); |
9 | 4751 |
} |
4752 |
||
19 | 4753 |
if ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) { |
0 | 4754 |
return array(); |
9 | 4755 |
} |
0 | 4756 |
|
4757 |
$galleries = array(); |
|
4758 |
if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) { |
|
4759 |
foreach ( $matches as $shortcode ) { |
|
4760 |
if ( 'gallery' === $shortcode[2] ) { |
|
4761 |
$srcs = array(); |
|
4762 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4763 |
$shortcode_attrs = shortcode_parse_atts( $shortcode[3] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4764 |
if ( ! is_array( $shortcode_attrs ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4765 |
$shortcode_attrs = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4766 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4767 |
|
16 | 4768 |
// 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
|
4769 |
if ( ! isset( $shortcode_attrs['id'] ) ) { |
18 | 4770 |
$shortcode[3] .= ' id="' . (int) $post->ID . '"'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4771 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4772 |
|
0 | 4773 |
$gallery = do_shortcode_tag( $shortcode ); |
4774 |
if ( $html ) { |
|
4775 |
$galleries[] = $gallery; |
|
4776 |
} else { |
|
4777 |
preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); |
|
4778 |
if ( ! empty( $src ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4779 |
foreach ( $src as $s ) { |
0 | 4780 |
$srcs[] = $s[2]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4781 |
} |
0 | 4782 |
} |
4783 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4784 |
$galleries[] = array_merge( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4785 |
$shortcode_attrs, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4786 |
array( |
9 | 4787 |
'src' => array_values( array_unique( $srcs ) ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4788 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4789 |
); |
0 | 4790 |
} |
4791 |
} |
|
4792 |
} |
|
4793 |
} |
|
4794 |
||
19 | 4795 |
if ( has_block( 'gallery', $post->post_content ) ) { |
4796 |
$post_blocks = parse_blocks( $post->post_content ); |
|
4797 |
||
4798 |
while ( $block = array_shift( $post_blocks ) ) { |
|
4799 |
$has_inner_blocks = ! empty( $block['innerBlocks'] ); |
|
4800 |
||
4801 |
// Skip blocks with no blockName and no innerHTML. |
|
4802 |
if ( ! $block['blockName'] ) { |
|
4803 |
continue; |
|
4804 |
} |
|
4805 |
||
4806 |
// Skip non-Gallery blocks. |
|
4807 |
if ( 'core/gallery' !== $block['blockName'] ) { |
|
4808 |
// Move inner blocks into the root array before skipping. |
|
4809 |
if ( $has_inner_blocks ) { |
|
4810 |
array_push( $post_blocks, ...$block['innerBlocks'] ); |
|
4811 |
} |
|
4812 |
continue; |
|
4813 |
} |
|
4814 |
||
4815 |
// New Gallery block format as HTML. |
|
4816 |
if ( $has_inner_blocks && $html ) { |
|
4817 |
$block_html = wp_list_pluck( $block['innerBlocks'], 'innerHTML' ); |
|
4818 |
$galleries[] = '<figure>' . implode( ' ', $block_html ) . '</figure>'; |
|
4819 |
continue; |
|
4820 |
} |
|
4821 |
||
4822 |
$srcs = array(); |
|
4823 |
||
4824 |
// New Gallery block format as an array. |
|
4825 |
if ( $has_inner_blocks ) { |
|
4826 |
$attrs = wp_list_pluck( $block['innerBlocks'], 'attrs' ); |
|
4827 |
$ids = wp_list_pluck( $attrs, 'id' ); |
|
4828 |
||
4829 |
foreach ( $ids as $id ) { |
|
4830 |
$url = wp_get_attachment_url( $id ); |
|
4831 |
||
4832 |
if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) { |
|
4833 |
$srcs[] = $url; |
|
4834 |
} |
|
4835 |
} |
|
4836 |
||
4837 |
$galleries[] = array( |
|
4838 |
'ids' => implode( ',', $ids ), |
|
4839 |
'src' => $srcs, |
|
4840 |
); |
|
4841 |
||
4842 |
continue; |
|
4843 |
} |
|
4844 |
||
4845 |
// Old Gallery block format as HTML. |
|
4846 |
if ( $html ) { |
|
4847 |
$galleries[] = $block['innerHTML']; |
|
4848 |
continue; |
|
4849 |
} |
|
4850 |
||
4851 |
// Old Gallery block format as an array. |
|
4852 |
$ids = ! empty( $block['attrs']['ids'] ) ? $block['attrs']['ids'] : array(); |
|
4853 |
||
4854 |
// If present, use the image IDs from the JSON blob as canonical. |
|
4855 |
if ( ! empty( $ids ) ) { |
|
4856 |
foreach ( $ids as $id ) { |
|
4857 |
$url = wp_get_attachment_url( $id ); |
|
4858 |
||
4859 |
if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) { |
|
4860 |
$srcs[] = $url; |
|
4861 |
} |
|
4862 |
} |
|
4863 |
||
4864 |
$galleries[] = array( |
|
4865 |
'ids' => implode( ',', $ids ), |
|
4866 |
'src' => $srcs, |
|
4867 |
); |
|
4868 |
||
4869 |
continue; |
|
4870 |
} |
|
4871 |
||
4872 |
// Otherwise, extract srcs from the innerHTML. |
|
4873 |
preg_match_all( '#src=([\'"])(.+?)\1#is', $block['innerHTML'], $found_srcs, PREG_SET_ORDER ); |
|
4874 |
||
4875 |
if ( ! empty( $found_srcs[0] ) ) { |
|
4876 |
foreach ( $found_srcs as $src ) { |
|
4877 |
if ( isset( $src[2] ) && ! in_array( $src[2], $srcs, true ) ) { |
|
4878 |
$srcs[] = $src[2]; |
|
4879 |
} |
|
4880 |
} |
|
4881 |
} |
|
4882 |
||
4883 |
$galleries[] = array( 'src' => $srcs ); |
|
4884 |
} |
|
4885 |
} |
|
4886 |
||
5 | 4887 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4888 |
* Filters the list of all found galleries in the given post. |
5 | 4889 |
* |
4890 |
* @since 3.6.0 |
|
4891 |
* |
|
4892 |
* @param array $galleries Associative array of all found post galleries. |
|
4893 |
* @param WP_Post $post Post object. |
|
4894 |
*/ |
|
0 | 4895 |
return apply_filters( 'get_post_galleries', $galleries, $post ); |
4896 |
} |
|
4897 |
||
4898 |
/** |
|
4899 |
* Check a specified post's content for gallery and, if present, return the first |
|
4900 |
* |
|
4901 |
* @since 3.6.0 |
|
4902 |
* |
|
5 | 4903 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
4904 |
* @param bool $html Optional. Whether to return HTML or data. Default is true. |
|
4905 |
* @return string|array Gallery data and srcs parsed from the expanded shortcode. |
|
0 | 4906 |
*/ |
4907 |
function get_post_gallery( $post = 0, $html = true ) { |
|
4908 |
$galleries = get_post_galleries( $post, $html ); |
|
9 | 4909 |
$gallery = reset( $galleries ); |
0 | 4910 |
|
5 | 4911 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4912 |
* Filters the first-found post gallery. |
5 | 4913 |
* |
4914 |
* @since 3.6.0 |
|
4915 |
* |
|
4916 |
* @param array $gallery The first-found post gallery. |
|
4917 |
* @param int|WP_Post $post Post ID or object. |
|
4918 |
* @param array $galleries Associative array of all found post galleries. |
|
4919 |
*/ |
|
0 | 4920 |
return apply_filters( 'get_post_gallery', $gallery, $post, $galleries ); |
4921 |
} |
|
4922 |
||
4923 |
/** |
|
4924 |
* Retrieve the image srcs from galleries from a post's content, if present |
|
4925 |
* |
|
4926 |
* @since 3.6.0 |
|
4927 |
* |
|
5 | 4928 |
* @see get_post_galleries() |
4929 |
* |
|
4930 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
|
4931 |
* @return array A list of lists, each containing image srcs parsed. |
|
4932 |
* from an expanded shortcode |
|
0 | 4933 |
*/ |
4934 |
function get_post_galleries_images( $post = 0 ) { |
|
4935 |
$galleries = get_post_galleries( $post, false ); |
|
4936 |
return wp_list_pluck( $galleries, 'src' ); |
|
4937 |
} |
|
4938 |
||
4939 |
/** |
|
5 | 4940 |
* Checks a post's content for galleries and return the image srcs for the first found gallery |
0 | 4941 |
* |
4942 |
* @since 3.6.0 |
|
4943 |
* |
|
5 | 4944 |
* @see get_post_gallery() |
4945 |
* |
|
4946 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
|
16 | 4947 |
* @return string[] A list of a gallery's image srcs in order. |
0 | 4948 |
*/ |
4949 |
function get_post_gallery_images( $post = 0 ) { |
|
4950 |
$gallery = get_post_gallery( $post, false ); |
|
4951 |
return empty( $gallery['src'] ) ? array() : $gallery['src']; |
|
4952 |
} |
|
5 | 4953 |
|
4954 |
/** |
|
4955 |
* Maybe attempts to generate attachment metadata, if missing. |
|
4956 |
* |
|
4957 |
* @since 3.9.0 |
|
4958 |
* |
|
4959 |
* @param WP_Post $attachment Attachment object. |
|
4960 |
*/ |
|
4961 |
function wp_maybe_generate_attachment_metadata( $attachment ) { |
|
16 | 4962 |
if ( empty( $attachment ) || empty( $attachment->ID ) ) { |
5 | 4963 |
return; |
4964 |
} |
|
4965 |
||
16 | 4966 |
$attachment_id = (int) $attachment->ID; |
4967 |
$file = get_attached_file( $attachment_id ); |
|
4968 |
$meta = wp_get_attachment_metadata( $attachment_id ); |
|
4969 |
||
5 | 4970 |
if ( empty( $meta ) && file_exists( $file ) ) { |
16 | 4971 |
$_meta = get_post_meta( $attachment_id ); |
4972 |
$_lock = 'wp_generating_att_' . $attachment_id; |
|
4973 |
||
4974 |
if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) { |
|
4975 |
set_transient( $_lock, $file ); |
|
5 | 4976 |
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); |
16 | 4977 |
delete_transient( $_lock ); |
5 | 4978 |
} |
4979 |
} |
|
4980 |
} |
|
4981 |
||
4982 |
/** |
|
4983 |
* Tries to convert an attachment URL into a post ID. |
|
4984 |
* |
|
4985 |
* @since 4.0.0 |
|
4986 |
* |
|
4987 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4988 |
* |
|
4989 |
* @param string $url The URL to resolve. |
|
4990 |
* @return int The found post ID, or 0 on failure. |
|
4991 |
*/ |
|
4992 |
function attachment_url_to_postid( $url ) { |
|
4993 |
global $wpdb; |
|
4994 |
||
9 | 4995 |
$dir = wp_get_upload_dir(); |
5 | 4996 |
$path = $url; |
4997 |
||
9 | 4998 |
$site_url = parse_url( $dir['url'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4999 |
$image_path = parse_url( $path ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5000 |
|
16 | 5001 |
// Force the protocols to match if needed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5002 |
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
|
5003 |
$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
|
5004 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5005 |
|
5 | 5006 |
if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) { |
5007 |
$path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); |
|
5008 |
} |
|
5009 |
||
16 | 5010 |
$sql = $wpdb->prepare( |
5011 |
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", |
|
5 | 5012 |
$path |
5013 |
); |
|
16 | 5014 |
|
5015 |
$results = $wpdb->get_results( $sql ); |
|
5016 |
$post_id = null; |
|
5017 |
||
5018 |
if ( $results ) { |
|
5019 |
// Use the first available result, but prefer a case-sensitive match, if exists. |
|
5020 |
$post_id = reset( $results )->post_id; |
|
5021 |
||
5022 |
if ( count( $results ) > 1 ) { |
|
5023 |
foreach ( $results as $result ) { |
|
5024 |
if ( $path === $result->meta_value ) { |
|
5025 |
$post_id = $result->post_id; |
|
5026 |
break; |
|
5027 |
} |
|
5028 |
} |
|
5029 |
} |
|
5030 |
} |
|
5 | 5031 |
|
5032 |
/** |
|
16 | 5033 |
* Filters an attachment ID found by URL. |
5 | 5034 |
* |
5035 |
* @since 4.2.0 |
|
5036 |
* |
|
5037 |
* @param int|null $post_id The post_id (if any) found by the function. |
|
5038 |
* @param string $url The URL being looked up. |
|
5039 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5040 |
return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url ); |
5 | 5041 |
} |
5042 |
||
5043 |
/** |
|
5044 |
* Returns the URLs for CSS files used in an iframe-sandbox'd TinyMCE media view. |
|
5045 |
* |
|
5046 |
* @since 4.0.0 |
|
5047 |
* |
|
16 | 5048 |
* @return string[] The relevant CSS file URLs. |
5 | 5049 |
*/ |
5050 |
function wpview_media_sandbox_styles() { |
|
9 | 5051 |
$version = 'ver=' . get_bloginfo( 'version' ); |
5052 |
$mediaelement = includes_url( "js/mediaelement/mediaelementplayer-legacy.min.css?$version" ); |
|
5053 |
$wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" ); |
|
5 | 5054 |
|
5055 |
return array( $mediaelement, $wpmediaelement ); |
|
5056 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5057 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5058 |
/** |
16 | 5059 |
* Registers the personal data exporter for media. |
5060 |
* |
|
5061 |
* @param array[] $exporters An array of personal data exporters, keyed by their ID. |
|
5062 |
* @return array[] Updated array of personal data exporters. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5063 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5064 |
function wp_register_media_personal_data_exporter( $exporters ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5065 |
$exporters['wordpress-media'] = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5066 |
'exporter_friendly_name' => __( 'WordPress Media' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5067 |
'callback' => 'wp_media_personal_data_exporter', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5068 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5069 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5070 |
return $exporters; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5071 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5072 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5073 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5074 |
* Finds and exports attachments associated with an email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5075 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5076 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5077 |
* |
16 | 5078 |
* @param string $email_address The attachment owner email address. |
5079 |
* @param int $page Attachment page. |
|
5080 |
* @return array An array of personal data. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5081 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5082 |
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
|
5083 |
// 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
|
5084 |
$number = 50; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5085 |
$page = (int) $page; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5086 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5087 |
$data_to_export = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5088 |
|
9 | 5089 |
$user = get_user_by( 'email', $email_address ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5090 |
if ( false === $user ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5091 |
return array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5092 |
'data' => $data_to_export, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5093 |
'done' => true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5094 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5095 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5096 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5097 |
$post_query = new WP_Query( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5098 |
array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5099 |
'author' => $user->ID, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5100 |
'posts_per_page' => $number, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5101 |
'paged' => $page, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5102 |
'post_type' => 'attachment', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5103 |
'post_status' => 'any', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5104 |
'orderby' => 'ID', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5105 |
'order' => 'ASC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5106 |
) |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5109 |
foreach ( (array) $post_query->posts as $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5110 |
$attachment_url = wp_get_attachment_url( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5111 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5112 |
if ( $attachment_url ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5113 |
$post_data_to_export = array( |
9 | 5114 |
array( |
5115 |
'name' => __( 'URL' ), |
|
5116 |
'value' => $attachment_url, |
|
5117 |
), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5118 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5119 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5120 |
$data_to_export[] = array( |
16 | 5121 |
'group_id' => 'media', |
5122 |
'group_label' => __( 'Media' ), |
|
5123 |
'group_description' => __( 'User’s media data.' ), |
|
5124 |
'item_id' => "post-{$post->ID}", |
|
5125 |
'data' => $post_data_to_export, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5126 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5127 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5128 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5129 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5130 |
$done = $post_query->max_num_pages <= $page; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5131 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5132 |
return array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5133 |
'data' => $data_to_export, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5134 |
'done' => $done, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5135 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5136 |
} |
16 | 5137 |
|
5138 |
/** |
|
5139 |
* Add additional default image sub-sizes. |
|
5140 |
* |
|
5141 |
* These sizes are meant to enhance the way WordPress displays images on the front-end on larger, |
|
5142 |
* high-density devices. They make it possible to generate more suitable `srcset` and `sizes` attributes |
|
5143 |
* when the users upload large images. |
|
5144 |
* |
|
5145 |
* The sizes can be changed or removed by themes and plugins but that is not recommended. |
|
5146 |
* The size "names" reflect the image dimensions, so changing the sizes would be quite misleading. |
|
5147 |
* |
|
5148 |
* @since 5.3.0 |
|
5149 |
* @access private |
|
5150 |
*/ |
|
5151 |
function _wp_add_additional_image_sizes() { |
|
5152 |
// 2x medium_large size. |
|
5153 |
add_image_size( '1536x1536', 1536, 1536 ); |
|
5154 |
// 2x large size. |
|
5155 |
add_image_size( '2048x2048', 2048, 2048 ); |
|
5156 |
} |
|
5157 |
||
5158 |
/** |
|
5159 |
* Callback to enable showing of the user error when uploading .heic images. |
|
5160 |
* |
|
5161 |
* @since 5.5.0 |
|
5162 |
* |
|
5163 |
* @param array[] $plupload_settings The settings for Plupload.js. |
|
5164 |
* @return array[] Modified settings for Plupload.js. |
|
5165 |
*/ |
|
5166 |
function wp_show_heic_upload_error( $plupload_settings ) { |
|
5167 |
$plupload_settings['heic_upload_error'] = true; |
|
5168 |
return $plupload_settings; |
|
5169 |
} |
|
18 | 5170 |
|
5171 |
/** |
|
5172 |
* Allows PHP's getimagesize() to be debuggable when necessary. |
|
5173 |
* |
|
5174 |
* @since 5.7.0 |
|
5175 |
* @since 5.8.0 Added support for WebP images. |
|
5176 |
* |
|
5177 |
* @param string $filename The file path. |
|
5178 |
* @param array $image_info Optional. Extended image information (passed by reference). |
|
5179 |
* @return array|false Array of image information or false on failure. |
|
5180 |
*/ |
|
5181 |
function wp_getimagesize( $filename, array &$image_info = null ) { |
|
5182 |
// Don't silence errors when in debug mode, unless running unit tests. |
|
5183 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG |
|
5184 |
&& ! defined( 'WP_RUN_CORE_TESTS' ) |
|
5185 |
) { |
|
5186 |
if ( 2 === func_num_args() ) { |
|
5187 |
$info = getimagesize( $filename, $image_info ); |
|
5188 |
} else { |
|
5189 |
$info = getimagesize( $filename ); |
|
5190 |
} |
|
5191 |
} else { |
|
5192 |
/* |
|
5193 |
* Silencing notice and warning is intentional. |
|
5194 |
* |
|
5195 |
* getimagesize() has a tendency to generate errors, such as |
|
5196 |
* "corrupt JPEG data: 7191 extraneous bytes before marker", |
|
5197 |
* even when it's able to provide image size information. |
|
5198 |
* |
|
5199 |
* See https://core.trac.wordpress.org/ticket/42480 |
|
5200 |
*/ |
|
5201 |
if ( 2 === func_num_args() ) { |
|
5202 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
5203 |
$info = @getimagesize( $filename, $image_info ); |
|
5204 |
} else { |
|
5205 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
5206 |
$info = @getimagesize( $filename ); |
|
5207 |
} |
|
5208 |
} |
|
5209 |
||
5210 |
if ( false !== $info ) { |
|
5211 |
return $info; |
|
5212 |
} |
|
5213 |
||
5214 |
// For PHP versions that don't support WebP images, |
|
5215 |
// extract the image size info from the file headers. |
|
5216 |
if ( 'image/webp' === wp_get_image_mime( $filename ) ) { |
|
5217 |
$webp_info = wp_get_webp_info( $filename ); |
|
5218 |
$width = $webp_info['width']; |
|
5219 |
$height = $webp_info['height']; |
|
5220 |
||
5221 |
// Mimic the native return format. |
|
5222 |
if ( $width && $height ) { |
|
5223 |
return array( |
|
5224 |
$width, |
|
5225 |
$height, |
|
19 | 5226 |
IMAGETYPE_WEBP, |
18 | 5227 |
sprintf( |
5228 |
'width="%d" height="%d"', |
|
5229 |
$width, |
|
5230 |
$height |
|
5231 |
), |
|
5232 |
'mime' => 'image/webp', |
|
5233 |
); |
|
5234 |
} |
|
5235 |
} |
|
5236 |
||
5237 |
// The image could not be parsed. |
|
5238 |
return false; |
|
5239 |
} |
|
5240 |
||
5241 |
/** |
|
19 | 5242 |
* Extracts meta information about a WebP file: width, height, and type. |
18 | 5243 |
* |
5244 |
* @since 5.8.0 |
|
5245 |
* |
|
5246 |
* @param string $filename Path to a WebP file. |
|
19 | 5247 |
* @return array { |
18 | 5248 |
* An array of WebP image information. |
5249 |
* |
|
19 | 5250 |
* @type int|false $width Image width on success, false on failure. |
5251 |
* @type int|false $height Image height on success, false on failure. |
|
5252 |
* @type string|false $type The WebP type: one of 'lossy', 'lossless' or 'animated-alpha'. |
|
5253 |
* False on failure. |
|
5254 |
* } |
|
18 | 5255 |
*/ |
5256 |
function wp_get_webp_info( $filename ) { |
|
5257 |
$width = false; |
|
5258 |
$height = false; |
|
5259 |
$type = false; |
|
5260 |
||
5261 |
if ( 'image/webp' !== wp_get_image_mime( $filename ) ) { |
|
5262 |
return compact( 'width', 'height', 'type' ); |
|
5263 |
} |
|
5264 |
||
19 | 5265 |
$magic = file_get_contents( $filename, false, null, 0, 40 ); |
5266 |
||
5267 |
if ( false === $magic ) { |
|
5268 |
return compact( 'width', 'height', 'type' ); |
|
5269 |
} |
|
5270 |
||
5271 |
// Make sure we got enough bytes. |
|
5272 |
if ( strlen( $magic ) < 40 ) { |
|
5273 |
return compact( 'width', 'height', 'type' ); |
|
5274 |
} |
|
5275 |
||
5276 |
// The headers are a little different for each of the three formats. |
|
5277 |
// Header values based on WebP docs, see https://developers.google.com/speed/webp/docs/riff_container. |
|
5278 |
switch ( substr( $magic, 12, 4 ) ) { |
|
5279 |
// Lossy WebP. |
|
5280 |
case 'VP8 ': |
|
5281 |
$parts = unpack( 'v2', substr( $magic, 26, 4 ) ); |
|
5282 |
$width = (int) ( $parts[1] & 0x3FFF ); |
|
5283 |
$height = (int) ( $parts[2] & 0x3FFF ); |
|
5284 |
$type = 'lossy'; |
|
5285 |
break; |
|
5286 |
// Lossless WebP. |
|
5287 |
case 'VP8L': |
|
5288 |
$parts = unpack( 'C4', substr( $magic, 21, 4 ) ); |
|
5289 |
$width = (int) ( $parts[1] | ( ( $parts[2] & 0x3F ) << 8 ) ) + 1; |
|
5290 |
$height = (int) ( ( ( $parts[2] & 0xC0 ) >> 6 ) | ( $parts[3] << 2 ) | ( ( $parts[4] & 0x03 ) << 10 ) ) + 1; |
|
5291 |
$type = 'lossless'; |
|
5292 |
break; |
|
5293 |
// Animated/alpha WebP. |
|
5294 |
case 'VP8X': |
|
5295 |
// Pad 24-bit int. |
|
5296 |
$width = unpack( 'V', substr( $magic, 24, 3 ) . "\x00" ); |
|
5297 |
$width = (int) ( $width[1] & 0xFFFFFF ) + 1; |
|
5298 |
// Pad 24-bit int. |
|
5299 |
$height = unpack( 'V', substr( $magic, 27, 3 ) . "\x00" ); |
|
5300 |
$height = (int) ( $height[1] & 0xFFFFFF ) + 1; |
|
5301 |
$type = 'animated-alpha'; |
|
5302 |
break; |
|
18 | 5303 |
} |
5304 |
||
5305 |
return compact( 'width', 'height', 'type' ); |
|
5306 |
} |
|
19 | 5307 |
|
5308 |
/** |
|
5309 |
* Gets the default value to use for a `loading` attribute on an element. |
|
5310 |
* |
|
5311 |
* This function should only be called for a tag and context if lazy-loading is generally enabled. |
|
5312 |
* |
|
5313 |
* The function usually returns 'lazy', but uses certain heuristics to guess whether the current element is likely to |
|
5314 |
* appear above the fold, in which case it returns a boolean `false`, which will lead to the `loading` attribute being |
|
5315 |
* omitted on the element. The purpose of this refinement is to avoid lazy-loading elements that are within the initial |
|
5316 |
* viewport, which can have a negative performance impact. |
|
5317 |
* |
|
5318 |
* Under the hood, the function uses {@see wp_increase_content_media_count()} every time it is called for an element |
|
5319 |
* within the main content. If the element is the very first content element, the `loading` attribute will be omitted. |
|
5320 |
* This default threshold of 1 content element to omit the `loading` attribute for can be customized using the |
|
5321 |
* {@see 'wp_omit_loading_attr_threshold'} filter. |
|
5322 |
* |
|
5323 |
* @since 5.9.0 |
|
5324 |
* |
|
5325 |
* @param string $context Context for the element for which the `loading` attribute value is requested. |
|
5326 |
* @return string|bool The default `loading` attribute value. Either 'lazy', 'eager', or a boolean `false`, to indicate |
|
5327 |
* that the `loading` attribute should be skipped. |
|
5328 |
*/ |
|
5329 |
function wp_get_loading_attr_default( $context ) { |
|
5330 |
// Only elements with 'the_content' or 'the_post_thumbnail' context have special handling. |
|
5331 |
if ( 'the_content' !== $context && 'the_post_thumbnail' !== $context ) { |
|
5332 |
return 'lazy'; |
|
5333 |
} |
|
5334 |
||
5335 |
// Only elements within the main query loop have special handling. |
|
5336 |
if ( is_admin() || ! in_the_loop() || ! is_main_query() ) { |
|
5337 |
return 'lazy'; |
|
5338 |
} |
|
5339 |
||
5340 |
// Increase the counter since this is a main query content element. |
|
5341 |
$content_media_count = wp_increase_content_media_count(); |
|
5342 |
||
5343 |
// If the count so far is below the threshold, return `false` so that the `loading` attribute is omitted. |
|
5344 |
if ( $content_media_count <= wp_omit_loading_attr_threshold() ) { |
|
5345 |
return false; |
|
5346 |
} |
|
5347 |
||
5348 |
// For elements after the threshold, lazy-load them as usual. |
|
5349 |
return 'lazy'; |
|
5350 |
} |
|
5351 |
||
5352 |
/** |
|
5353 |
* Gets the threshold for how many of the first content media elements to not lazy-load. |
|
5354 |
* |
|
5355 |
* This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 1. |
|
5356 |
* The filter is only run once per page load, unless the `$force` parameter is used. |
|
5357 |
* |
|
5358 |
* @since 5.9.0 |
|
5359 |
* |
|
5360 |
* @param bool $force Optional. If set to true, the filter will be (re-)applied even if it already has been before. |
|
5361 |
* Default false. |
|
5362 |
* @return int The number of content media elements to not lazy-load. |
|
5363 |
*/ |
|
5364 |
function wp_omit_loading_attr_threshold( $force = false ) { |
|
5365 |
static $omit_threshold; |
|
5366 |
||
5367 |
// This function may be called multiple times. Run the filter only once per page load. |
|
5368 |
if ( ! isset( $omit_threshold ) || $force ) { |
|
5369 |
/** |
|
5370 |
* Filters the threshold for how many of the first content media elements to not lazy-load. |
|
5371 |
* |
|
5372 |
* For these first content media elements, the `loading` attribute will be omitted. By default, this is the case |
|
5373 |
* for only the very first content media element. |
|
5374 |
* |
|
5375 |
* @since 5.9.0 |
|
5376 |
* |
|
5377 |
* @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 1. |
|
5378 |
*/ |
|
5379 |
$omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 1 ); |
|
5380 |
} |
|
5381 |
||
5382 |
return $omit_threshold; |
|
5383 |
} |
|
5384 |
||
5385 |
/** |
|
5386 |
* Increases an internal content media count variable. |
|
5387 |
* |
|
5388 |
* @since 5.9.0 |
|
5389 |
* @access private |
|
5390 |
* |
|
5391 |
* @param int $amount Optional. Amount to increase by. Default 1. |
|
5392 |
* @return int The latest content media count, after the increase. |
|
5393 |
*/ |
|
5394 |
function wp_increase_content_media_count( $amount = 1 ) { |
|
5395 |
static $content_media_count = 0; |
|
5396 |
||
5397 |
$content_media_count += $amount; |
|
5398 |
||
5399 |
return $content_media_count; |
|
5400 |
} |