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