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