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