136
|
1 |
<?php |
|
2 |
/** |
|
3 |
* WordPress API for media display. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Scale down the default size of an image. |
|
10 |
* |
|
11 |
* This is so that the image is a better fit for the editor and theme. |
|
12 |
* |
|
13 |
* The $size parameter accepts either an array or a string. The supported string |
|
14 |
* values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at |
|
15 |
* 128 width and 96 height in pixels. Also supported for the string value is |
|
16 |
* 'medium' and 'full'. The 'full' isn't actually supported, but any value other |
|
17 |
* than the supported will result in the content_width size or 500 if that is |
|
18 |
* not set. |
|
19 |
* |
|
20 |
* Finally, there is a filter named, 'editor_max_image_size' that will be called |
|
21 |
* on the calculated array for width and height, respectively. The second |
|
22 |
* parameter will be the value that was in the $size parameter. The returned |
|
23 |
* type for the hook is an array with the width as the first element and the |
|
24 |
* height as the second element. |
|
25 |
* |
|
26 |
* @since 2.5.0 |
|
27 |
* @uses wp_constrain_dimensions() This function passes the widths and the heights. |
|
28 |
* |
|
29 |
* @param int $width Width of the image |
|
30 |
* @param int $height Height of the image |
|
31 |
* @param string|array $size Size of what the result image should be. |
|
32 |
* @return array Width and height of what the result image should resize to. |
|
33 |
*/ |
|
34 |
function image_constrain_size_for_editor($width, $height, $size = 'medium') { |
|
35 |
global $content_width, $_wp_additional_image_sizes; |
|
36 |
|
|
37 |
if ( is_array($size) ) { |
|
38 |
$max_width = $size[0]; |
|
39 |
$max_height = $size[1]; |
|
40 |
} |
|
41 |
elseif ( $size == 'thumb' || $size == 'thumbnail' ) { |
|
42 |
$max_width = intval(get_option('thumbnail_size_w')); |
|
43 |
$max_height = intval(get_option('thumbnail_size_h')); |
|
44 |
// last chance thumbnail size defaults |
|
45 |
if ( !$max_width && !$max_height ) { |
|
46 |
$max_width = 128; |
|
47 |
$max_height = 96; |
|
48 |
} |
|
49 |
} |
|
50 |
elseif ( $size == 'medium' ) { |
|
51 |
$max_width = intval(get_option('medium_size_w')); |
|
52 |
$max_height = intval(get_option('medium_size_h')); |
|
53 |
// if no width is set, default to the theme content width if available |
|
54 |
} |
|
55 |
elseif ( $size == 'large' ) { |
|
56 |
// we're inserting a large size image into the editor. if it's a really |
|
57 |
// big image we'll scale it down to fit reasonably within the editor |
|
58 |
// itself, and within the theme's content width if it's known. the user |
|
59 |
// can resize it in the editor if they wish. |
|
60 |
$max_width = intval(get_option('large_size_w')); |
|
61 |
$max_height = intval(get_option('large_size_h')); |
|
62 |
if ( intval($content_width) > 0 ) |
|
63 |
$max_width = min( intval($content_width), $max_width ); |
|
64 |
} elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) { |
|
65 |
$max_width = intval( $_wp_additional_image_sizes[$size]['width'] ); |
|
66 |
$max_height = intval( $_wp_additional_image_sizes[$size]['height'] ); |
|
67 |
if ( intval($content_width) > 0 ) |
|
68 |
$max_width = min( intval($content_width), $max_width ); |
|
69 |
} |
|
70 |
// $size == 'full' has no constraint |
|
71 |
else { |
|
72 |
$max_width = $width; |
|
73 |
$max_height = $height; |
|
74 |
} |
|
75 |
|
|
76 |
list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size ); |
|
77 |
|
|
78 |
return wp_constrain_dimensions( $width, $height, $max_width, $max_height ); |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* Retrieve width and height attributes using given width and height values. |
|
83 |
* |
|
84 |
* Both attributes are required in the sense that both parameters must have a |
|
85 |
* value, but are optional in that if you set them to false or null, then they |
|
86 |
* will not be added to the returned string. |
|
87 |
* |
|
88 |
* You can set the value using a string, but it will only take numeric values. |
|
89 |
* If you wish to put 'px' after the numbers, then it will be stripped out of |
|
90 |
* the return. |
|
91 |
* |
|
92 |
* @since 2.5.0 |
|
93 |
* |
|
94 |
* @param int|string $width Optional. Width attribute value. |
|
95 |
* @param int|string $height Optional. Height attribute value. |
|
96 |
* @return string HTML attributes for width and, or height. |
|
97 |
*/ |
|
98 |
function image_hwstring($width, $height) { |
|
99 |
$out = ''; |
|
100 |
if ($width) |
|
101 |
$out .= 'width="'.intval($width).'" '; |
|
102 |
if ($height) |
|
103 |
$out .= 'height="'.intval($height).'" '; |
|
104 |
return $out; |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* Scale an image to fit a particular size (such as 'thumb' or 'medium'). |
|
109 |
* |
|
110 |
* Array with image url, width, height, and whether is intermediate size, in |
|
111 |
* that order is returned on success is returned. $is_intermediate is true if |
|
112 |
* $url is a resized image, false if it is the original. |
|
113 |
* |
|
114 |
* The URL might be the original image, or it might be a resized version. This |
|
115 |
* function won't create a new resized copy, it will just return an already |
|
116 |
* resized one if it exists. |
|
117 |
* |
|
118 |
* A plugin may use the 'image_downsize' filter to hook into and offer image |
|
119 |
* resizing services for images. The hook must return an array with the same |
|
120 |
* elements that are returned in the function. The first element being the URL |
|
121 |
* to the new image that was resized. |
|
122 |
* |
|
123 |
* @since 2.5.0 |
|
124 |
* @uses apply_filters() Calls 'image_downsize' on $id and $size to provide |
|
125 |
* resize services. |
|
126 |
* |
|
127 |
* @param int $id Attachment ID for image. |
|
128 |
* @param string $size Optional, default is 'medium'. Size of image, can be 'thumbnail'. |
|
129 |
* @return bool|array False on failure, array on success. |
|
130 |
*/ |
|
131 |
function image_downsize($id, $size = 'medium') { |
|
132 |
|
|
133 |
if ( !wp_attachment_is_image($id) ) |
|
134 |
return false; |
|
135 |
|
|
136 |
$img_url = wp_get_attachment_url($id); |
|
137 |
$meta = wp_get_attachment_metadata($id); |
|
138 |
$width = $height = 0; |
|
139 |
$is_intermediate = false; |
|
140 |
|
|
141 |
// plugins can use this to provide resize services |
|
142 |
if ( $out = apply_filters('image_downsize', false, $id, $size) ) |
|
143 |
return $out; |
|
144 |
|
|
145 |
// try for a new style intermediate size |
|
146 |
if ( $intermediate = image_get_intermediate_size($id, $size) ) { |
|
147 |
$img_url = str_replace(basename($img_url), $intermediate['file'], $img_url); |
|
148 |
$width = $intermediate['width']; |
|
149 |
$height = $intermediate['height']; |
|
150 |
$is_intermediate = true; |
|
151 |
} |
|
152 |
elseif ( $size == 'thumbnail' ) { |
|
153 |
// fall back to the old thumbnail |
|
154 |
if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) { |
|
155 |
$img_url = str_replace(basename($img_url), basename($thumb_file), $img_url); |
|
156 |
$width = $info[0]; |
|
157 |
$height = $info[1]; |
|
158 |
$is_intermediate = true; |
|
159 |
} |
|
160 |
} |
|
161 |
if ( !$width && !$height && isset($meta['width'], $meta['height']) ) { |
|
162 |
// any other type: use the real image |
|
163 |
$width = $meta['width']; |
|
164 |
$height = $meta['height']; |
|
165 |
} |
|
166 |
|
|
167 |
if ( $img_url) { |
|
168 |
// we have the actual image size, but might need to further constrain it if content_width is narrower |
|
169 |
list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); |
|
170 |
|
|
171 |
return array( $img_url, $width, $height, $is_intermediate ); |
|
172 |
} |
|
173 |
return false; |
|
174 |
|
|
175 |
} |
|
176 |
|
|
177 |
/** |
|
178 |
* Registers a new image size |
|
179 |
*/ |
|
180 |
function add_image_size( $name, $width = 0, $height = 0, $crop = FALSE ) { |
|
181 |
global $_wp_additional_image_sizes; |
|
182 |
$_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => !!$crop ); |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* Registers an image size for the post thumbnail |
|
187 |
*/ |
|
188 |
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE ) { |
|
189 |
add_image_size( 'post-thumbnail', $width, $height, $crop ); |
|
190 |
} |
|
191 |
|
|
192 |
/** |
|
193 |
* An <img src /> tag for an image attachment, scaling it down if requested. |
|
194 |
* |
|
195 |
* The filter 'get_image_tag_class' allows for changing the class name for the |
|
196 |
* image without having to use regular expressions on the HTML content. The |
|
197 |
* parameters are: what WordPress will use for the class, the Attachment ID, |
|
198 |
* image align value, and the size the image should be. |
|
199 |
* |
|
200 |
* The second filter 'get_image_tag' has the HTML content, which can then be |
|
201 |
* further manipulated by a plugin to change all attribute values and even HTML |
|
202 |
* content. |
|
203 |
* |
|
204 |
* @since 2.5.0 |
|
205 |
* |
|
206 |
* @uses apply_filters() The 'get_image_tag_class' filter is the IMG element |
|
207 |
* class attribute. |
|
208 |
* @uses apply_filters() The 'get_image_tag' filter is the full IMG element with |
|
209 |
* all attributes. |
|
210 |
* |
|
211 |
* @param int $id Attachment ID. |
|
212 |
* @param string $alt Image Description for the alt attribute. |
|
213 |
* @param string $title Image Description for the title attribute. |
|
214 |
* @param string $align Part of the class name for aligning the image. |
|
215 |
* @param string $size Optional. Default is 'medium'. |
|
216 |
* @return string HTML IMG element for given image attachment |
|
217 |
*/ |
|
218 |
function get_image_tag($id, $alt, $title, $align, $size='medium') { |
|
219 |
|
|
220 |
list( $img_src, $width, $height ) = image_downsize($id, $size); |
|
221 |
$hwstring = image_hwstring($width, $height); |
|
222 |
|
|
223 |
$class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id; |
|
224 |
$class = apply_filters('get_image_tag_class', $class, $id, $align, $size); |
|
225 |
|
|
226 |
$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />'; |
|
227 |
|
|
228 |
$html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); |
|
229 |
|
|
230 |
return $html; |
|
231 |
} |
|
232 |
|
|
233 |
/** |
|
234 |
* Calculates the new dimentions for a downsampled image. |
|
235 |
* |
|
236 |
* Same as {@link wp_shrink_dimensions()}, except the max parameters are |
|
237 |
* optional. If either width or height are empty, no constraint is applied on |
|
238 |
* that dimension. |
|
239 |
* |
|
240 |
* @since 2.5.0 |
|
241 |
* |
|
242 |
* @param int $current_width Current width of the image. |
|
243 |
* @param int $current_height Current height of the image. |
|
244 |
* @param int $max_width Optional. Maximum wanted width. |
|
245 |
* @param int $max_height Optional. Maximum wanted height. |
|
246 |
* @return array First item is the width, the second item is the height. |
|
247 |
*/ |
|
248 |
function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) { |
|
249 |
if ( !$max_width and !$max_height ) |
|
250 |
return array( $current_width, $current_height ); |
|
251 |
|
|
252 |
$width_ratio = $height_ratio = 1.0; |
|
253 |
|
|
254 |
if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) |
|
255 |
$width_ratio = $max_width / $current_width; |
|
256 |
|
|
257 |
if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) |
|
258 |
$height_ratio = $max_height / $current_height; |
|
259 |
|
|
260 |
// the smaller ratio is the one we need to fit it to the constraining box |
|
261 |
$ratio = min( $width_ratio, $height_ratio ); |
|
262 |
|
|
263 |
return array( intval($current_width * $ratio), intval($current_height * $ratio) ); |
|
264 |
} |
|
265 |
|
|
266 |
/** |
|
267 |
* Retrieve calculated resized dimensions for use in imagecopyresampled(). |
|
268 |
* |
|
269 |
* Calculate dimensions and coordinates for a resized image that fits within a |
|
270 |
* specified width and height. If $crop is true, the largest matching central |
|
271 |
* portion of the image will be cropped out and resized to the required size. |
|
272 |
* |
|
273 |
* @since 2.5.0 |
|
274 |
* |
|
275 |
* @param int $orig_w Original width. |
|
276 |
* @param int $orig_h Original height. |
|
277 |
* @param int $dest_w New width. |
|
278 |
* @param int $dest_h New height. |
|
279 |
* @param bool $crop Optional, default is false. Whether to crop image or resize. |
|
280 |
* @return bool|array False, on failure. Returned array matches parameters for imagecopyresampled() PHP function. |
|
281 |
*/ |
|
282 |
function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) { |
|
283 |
|
|
284 |
if ($orig_w <= 0 || $orig_h <= 0) |
|
285 |
return false; |
|
286 |
// at least one of dest_w or dest_h must be specific |
|
287 |
if ($dest_w <= 0 && $dest_h <= 0) |
|
288 |
return false; |
|
289 |
|
|
290 |
if ( $crop ) { |
|
291 |
// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h |
|
292 |
$aspect_ratio = $orig_w / $orig_h; |
|
293 |
$new_w = min($dest_w, $orig_w); |
|
294 |
$new_h = min($dest_h, $orig_h); |
|
295 |
|
|
296 |
if ( !$new_w ) { |
|
297 |
$new_w = intval($new_h * $aspect_ratio); |
|
298 |
} |
|
299 |
|
|
300 |
if ( !$new_h ) { |
|
301 |
$new_h = intval($new_w / $aspect_ratio); |
|
302 |
} |
|
303 |
|
|
304 |
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h); |
|
305 |
|
|
306 |
$crop_w = round($new_w / $size_ratio); |
|
307 |
$crop_h = round($new_h / $size_ratio); |
|
308 |
|
|
309 |
$s_x = floor( ($orig_w - $crop_w) / 2 ); |
|
310 |
$s_y = floor( ($orig_h - $crop_h) / 2 ); |
|
311 |
} else { |
|
312 |
// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box |
|
313 |
$crop_w = $orig_w; |
|
314 |
$crop_h = $orig_h; |
|
315 |
|
|
316 |
$s_x = 0; |
|
317 |
$s_y = 0; |
|
318 |
|
|
319 |
list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h ); |
|
320 |
} |
|
321 |
|
|
322 |
// if the resulting image would be the same size or larger we don't want to resize it |
|
323 |
if ( $new_w >= $orig_w && $new_h >= $orig_h ) |
|
324 |
return false; |
|
325 |
|
|
326 |
// the return array matches the parameters to imagecopyresampled() |
|
327 |
// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h |
|
328 |
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); |
|
329 |
|
|
330 |
} |
|
331 |
|
|
332 |
/** |
|
333 |
* Scale down an image to fit a particular size and save a new copy of the image. |
|
334 |
* |
|
335 |
* The PNG transparency will be preserved using the function, as well as the |
|
336 |
* image type. If the file going in is PNG, then the resized image is going to |
|
337 |
* be PNG. The only supported image types are PNG, GIF, and JPEG. |
|
338 |
* |
|
339 |
* Some functionality requires API to exist, so some PHP version may lose out |
|
340 |
* support. This is not the fault of WordPress (where functionality is |
|
341 |
* downgraded, not actual defects), but of your PHP version. |
|
342 |
* |
|
343 |
* @since 2.5.0 |
|
344 |
* |
|
345 |
* @param string $file Image file path. |
|
346 |
* @param int $max_w Maximum width to resize to. |
|
347 |
* @param int $max_h Maximum height to resize to. |
|
348 |
* @param bool $crop Optional. Whether to crop image or resize. |
|
349 |
* @param string $suffix Optional. File Suffix. |
|
350 |
* @param string $dest_path Optional. New image file path. |
|
351 |
* @param int $jpeg_quality Optional, default is 90. Image quality percentage. |
|
352 |
* @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()} |
|
353 |
*/ |
|
354 |
function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) { |
|
355 |
|
|
356 |
$image = wp_load_image( $file ); |
|
357 |
if ( !is_resource( $image ) ) |
|
358 |
return new WP_Error('error_loading_image', $image); |
|
359 |
|
|
360 |
$size = @getimagesize( $file ); |
|
361 |
if ( !$size ) |
|
362 |
return new WP_Error('invalid_image', __('Could not read image size'), $file); |
|
363 |
list($orig_w, $orig_h, $orig_type) = $size; |
|
364 |
|
|
365 |
$dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); |
|
366 |
if ( !$dims ) |
|
367 |
return $dims; |
|
368 |
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; |
|
369 |
|
|
370 |
$newimage = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|
371 |
|
|
372 |
imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
|
373 |
|
|
374 |
// convert from full colors to index colors, like original PNG. |
|
375 |
if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $image ) ) |
|
376 |
imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) ); |
|
377 |
|
|
378 |
// we don't need the original in memory anymore |
|
379 |
imagedestroy( $image ); |
|
380 |
|
|
381 |
// $suffix will be appended to the destination filename, just before the extension |
|
382 |
if ( !$suffix ) |
|
383 |
$suffix = "{$dst_w}x{$dst_h}"; |
|
384 |
|
|
385 |
$info = pathinfo($file); |
|
386 |
$dir = $info['dirname']; |
|
387 |
$ext = $info['extension']; |
|
388 |
$name = basename($file, ".{$ext}"); |
|
389 |
if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) ) |
|
390 |
$dir = $_dest_path; |
|
391 |
$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; |
|
392 |
|
|
393 |
if ( IMAGETYPE_GIF == $orig_type ) { |
|
394 |
if ( !imagegif( $newimage, $destfilename ) ) |
|
395 |
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); |
|
396 |
} elseif ( IMAGETYPE_PNG == $orig_type ) { |
|
397 |
if ( !imagepng( $newimage, $destfilename ) ) |
|
398 |
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); |
|
399 |
} else { |
|
400 |
// all other formats are converted to jpg |
|
401 |
$destfilename = "{$dir}/{$name}-{$suffix}.jpg"; |
|
402 |
if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) |
|
403 |
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); |
|
404 |
} |
|
405 |
|
|
406 |
imagedestroy( $newimage ); |
|
407 |
|
|
408 |
// Set correct file permissions |
|
409 |
$stat = stat( dirname( $destfilename )); |
|
410 |
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits |
|
411 |
@ chmod( $destfilename, $perms ); |
|
412 |
|
|
413 |
return $destfilename; |
|
414 |
} |
|
415 |
|
|
416 |
/** |
|
417 |
* Resize an image to make a thumbnail or intermediate size. |
|
418 |
* |
|
419 |
* The returned array has the file size, the image width, and image height. The |
|
420 |
* filter 'image_make_intermediate_size' can be used to hook in and change the |
|
421 |
* values of the returned array. The only parameter is the resized file path. |
|
422 |
* |
|
423 |
* @since 2.5.0 |
|
424 |
* |
|
425 |
* @param string $file File path. |
|
426 |
* @param int $width Image width. |
|
427 |
* @param int $height Image height. |
|
428 |
* @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize. |
|
429 |
* @return bool|array False, if no image was created. Metadata array on success. |
|
430 |
*/ |
|
431 |
function image_make_intermediate_size($file, $width, $height, $crop=false) { |
|
432 |
if ( $width || $height ) { |
|
433 |
$resized_file = image_resize($file, $width, $height, $crop); |
|
434 |
if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) { |
|
435 |
$resized_file = apply_filters('image_make_intermediate_size', $resized_file); |
|
436 |
return array( |
|
437 |
'file' => basename( $resized_file ), |
|
438 |
'width' => $info[0], |
|
439 |
'height' => $info[1], |
|
440 |
); |
|
441 |
} |
|
442 |
} |
|
443 |
return false; |
|
444 |
} |
|
445 |
|
|
446 |
/** |
|
447 |
* Retrieve the image's intermediate size (resized) path, width, and height. |
|
448 |
* |
|
449 |
* The $size parameter can be an array with the width and height respectively. |
|
450 |
* If the size matches the 'sizes' metadata array for width and height, then it |
|
451 |
* will be used. If there is no direct match, then the nearest image size larger |
|
452 |
* than the specified size will be used. If nothing is found, then the function |
|
453 |
* will break out and return false. |
|
454 |
* |
|
455 |
* The metadata 'sizes' is used for compatible sizes that can be used for the |
|
456 |
* parameter $size value. |
|
457 |
* |
|
458 |
* The url path will be given, when the $size parameter is a string. |
|
459 |
* |
|
460 |
* @since 2.5.0 |
|
461 |
* |
|
462 |
* @param int $post_id Attachment ID for image. |
|
463 |
* @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string. |
|
464 |
* @return bool|array False on failure or array of file path, width, and height on success. |
|
465 |
*/ |
|
466 |
function image_get_intermediate_size($post_id, $size='thumbnail') { |
|
467 |
if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) ) |
|
468 |
return false; |
|
469 |
|
|
470 |
// get the best one for a specified set of dimensions |
|
471 |
if ( is_array($size) && !empty($imagedata['sizes']) ) { |
|
472 |
foreach ( $imagedata['sizes'] as $_size => $data ) { |
|
473 |
// already cropped to width or height; so use this size |
|
474 |
if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) { |
|
475 |
$file = $data['file']; |
|
476 |
list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); |
|
477 |
return compact( 'file', 'width', 'height' ); |
|
478 |
} |
|
479 |
// add to lookup table: area => size |
|
480 |
$areas[$data['width'] * $data['height']] = $_size; |
|
481 |
} |
|
482 |
if ( !$size || !empty($areas) ) { |
|
483 |
// find for the smallest image not smaller than the desired size |
|
484 |
ksort($areas); |
|
485 |
foreach ( $areas as $_size ) { |
|
486 |
$data = $imagedata['sizes'][$_size]; |
|
487 |
if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) { |
|
488 |
$file = $data['file']; |
|
489 |
list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); |
|
490 |
return compact( 'file', 'width', 'height' ); |
|
491 |
} |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
|
|
496 |
if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) ) |
|
497 |
return false; |
|
498 |
|
|
499 |
$data = $imagedata['sizes'][$size]; |
|
500 |
// include the full filesystem path of the intermediate file |
|
501 |
if ( empty($data['path']) && !empty($data['file']) ) { |
|
502 |
$file_url = wp_get_attachment_url($post_id); |
|
503 |
$data['path'] = path_join( dirname($imagedata['file']), $data['file'] ); |
|
504 |
$data['url'] = path_join( dirname($file_url), $data['file'] ); |
|
505 |
} |
|
506 |
return $data; |
|
507 |
} |
|
508 |
|
|
509 |
/** |
|
510 |
* Retrieve an image to represent an attachment. |
|
511 |
* |
|
512 |
* A mime icon for files, thumbnail or intermediate size for images. |
|
513 |
* |
|
514 |
* @since 2.5.0 |
|
515 |
* |
|
516 |
* @param int $attachment_id Image attachment ID. |
|
517 |
* @param string $size Optional, default is 'thumbnail'. |
|
518 |
* @param bool $icon Optional, default is false. Whether it is an icon. |
|
519 |
* @return bool|array Returns an array (url, width, height), or false, if no image is available. |
|
520 |
*/ |
|
521 |
function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) { |
|
522 |
|
|
523 |
// get a thumbnail or intermediate image if there is one |
|
524 |
if ( $image = image_downsize($attachment_id, $size) ) |
|
525 |
return $image; |
|
526 |
|
|
527 |
$src = false; |
|
528 |
|
|
529 |
if ( $icon && $src = wp_mime_type_icon($attachment_id) ) { |
|
530 |
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' ); |
|
531 |
$src_file = $icon_dir . '/' . basename($src); |
|
532 |
@list($width, $height) = getimagesize($src_file); |
|
533 |
} |
|
534 |
if ( $src && $width && $height ) |
|
535 |
return array( $src, $width, $height ); |
|
536 |
return false; |
|
537 |
} |
|
538 |
|
|
539 |
/** |
|
540 |
* Get an HTML img element representing an image attachment |
|
541 |
* |
|
542 |
* @uses apply_filters() Calls 'wp_get_attachment_image_attributes' hook on attributes array |
|
543 |
* @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions |
|
544 |
* @since 2.5.0 |
|
545 |
* |
|
546 |
* @param int $attachment_id Image attachment ID. |
|
547 |
* @param string $size Optional, default is 'thumbnail'. |
|
548 |
* @param bool $icon Optional, default is false. Whether it is an icon. |
|
549 |
* @return string HTML img element or empty string on failure. |
|
550 |
*/ |
|
551 |
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { |
|
552 |
|
|
553 |
$html = ''; |
|
554 |
$image = wp_get_attachment_image_src($attachment_id, $size, $icon); |
|
555 |
if ( $image ) { |
|
556 |
list($src, $width, $height) = $image; |
|
557 |
$hwstring = image_hwstring($width, $height); |
|
558 |
if ( is_array($size) ) |
|
559 |
$size = join('x', $size); |
|
560 |
$attachment =& get_post($attachment_id); |
|
561 |
$default_attr = array( |
|
562 |
'src' => $src, |
|
563 |
'class' => "attachment-$size", |
|
564 |
'alt' => trim(strip_tags( $attachment->post_excerpt )), |
|
565 |
'title' => trim(strip_tags( $attachment->post_title )), |
|
566 |
); |
|
567 |
$attr = wp_parse_args($attr, $default_attr); |
|
568 |
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); |
|
569 |
$attr = array_map( 'esc_attr', $attr ); |
|
570 |
$html = rtrim("<img $hwstring"); |
|
571 |
foreach ( $attr as $name => $value ) { |
|
572 |
$html .= " $name=" . '"' . $value . '"'; |
|
573 |
} |
|
574 |
$html .= ' />'; |
|
575 |
} |
|
576 |
|
|
577 |
return $html; |
|
578 |
} |
|
579 |
|
|
580 |
/** |
|
581 |
* Adds a 'wp-post-image' class to post thumbnail thumbnails |
|
582 |
* Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to |
|
583 |
* dynamically add/remove itself so as to only filter post thumbnail thumbnails |
|
584 |
* |
|
585 |
* @author Mark Jaquith |
|
586 |
* @since 2.9.0 |
|
587 |
* @param array $attr Attributes including src, class, alt, title |
|
588 |
* @return array |
|
589 |
*/ |
|
590 |
function _wp_post_thumbnail_class_filter( $attr ) { |
|
591 |
$attr['class'] .= ' wp-post-image'; |
|
592 |
return $attr; |
|
593 |
} |
|
594 |
|
|
595 |
/** |
|
596 |
* Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter |
|
597 |
* |
|
598 |
* @author Mark Jaquith |
|
599 |
* @since 2.9.0 |
|
600 |
*/ |
|
601 |
function _wp_post_thumbnail_class_filter_add( $attr ) { |
|
602 |
add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
|
603 |
} |
|
604 |
|
|
605 |
/** |
|
606 |
* Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter |
|
607 |
* |
|
608 |
* @author Mark Jaquith |
|
609 |
* @since 2.9.0 |
|
610 |
*/ |
|
611 |
function _wp_post_thumbnail_class_filter_remove( $attr ) { |
|
612 |
remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); |
|
613 |
} |
|
614 |
|
|
615 |
add_shortcode('wp_caption', 'img_caption_shortcode'); |
|
616 |
add_shortcode('caption', 'img_caption_shortcode'); |
|
617 |
|
|
618 |
/** |
|
619 |
* The Caption shortcode. |
|
620 |
* |
|
621 |
* Allows a plugin to replace the content that would otherwise be returned. The |
|
622 |
* filter is 'img_caption_shortcode' and passes an empty string, the attr |
|
623 |
* parameter and the content parameter values. |
|
624 |
* |
|
625 |
* The supported attributes for the shortcode are 'id', 'align', 'width', and |
|
626 |
* 'caption'. |
|
627 |
* |
|
628 |
* @since 2.6.0 |
|
629 |
* |
|
630 |
* @param array $attr Attributes attributed to the shortcode. |
|
631 |
* @param string $content Optional. Shortcode content. |
|
632 |
* @return string |
|
633 |
*/ |
|
634 |
function img_caption_shortcode($attr, $content = null) { |
|
635 |
|
|
636 |
// Allow plugins/themes to override the default caption template. |
|
637 |
$output = apply_filters('img_caption_shortcode', '', $attr, $content); |
|
638 |
if ( $output != '' ) |
|
639 |
return $output; |
|
640 |
|
|
641 |
extract(shortcode_atts(array( |
|
642 |
'id' => '', |
|
643 |
'align' => 'alignnone', |
|
644 |
'width' => '', |
|
645 |
'caption' => '' |
|
646 |
), $attr)); |
|
647 |
|
|
648 |
if ( 1 > (int) $width || empty($caption) ) |
|
649 |
return $content; |
|
650 |
|
|
651 |
if ( $id ) $id = 'id="' . esc_attr($id) . '" '; |
|
652 |
|
|
653 |
return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">' |
|
654 |
. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>'; |
|
655 |
} |
|
656 |
|
|
657 |
add_shortcode('gallery', 'gallery_shortcode'); |
|
658 |
|
|
659 |
/** |
|
660 |
* The Gallery shortcode. |
|
661 |
* |
|
662 |
* This implements the functionality of the Gallery Shortcode for displaying |
|
663 |
* WordPress images on a post. |
|
664 |
* |
|
665 |
* @since 2.5.0 |
|
666 |
* |
|
667 |
* @param array $attr Attributes attributed to the shortcode. |
|
668 |
* @return string HTML content to display gallery. |
|
669 |
*/ |
|
670 |
function gallery_shortcode($attr) { |
|
671 |
global $post, $wp_locale; |
|
672 |
|
|
673 |
static $instance = 0; |
|
674 |
$instance++; |
|
675 |
|
|
676 |
// Allow plugins/themes to override the default gallery template. |
|
677 |
$output = apply_filters('post_gallery', '', $attr); |
|
678 |
if ( $output != '' ) |
|
679 |
return $output; |
|
680 |
|
|
681 |
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement |
|
682 |
if ( isset( $attr['orderby'] ) ) { |
|
683 |
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); |
|
684 |
if ( !$attr['orderby'] ) |
|
685 |
unset( $attr['orderby'] ); |
|
686 |
} |
|
687 |
|
|
688 |
extract(shortcode_atts(array( |
|
689 |
'order' => 'ASC', |
|
690 |
'orderby' => 'menu_order ID', |
|
691 |
'id' => $post->ID, |
|
692 |
'itemtag' => 'dl', |
|
693 |
'icontag' => 'dt', |
|
694 |
'captiontag' => 'dd', |
|
695 |
'columns' => 3, |
|
696 |
'size' => 'thumbnail', |
|
697 |
'include' => '', |
|
698 |
'exclude' => '' |
|
699 |
), $attr)); |
|
700 |
|
|
701 |
$id = intval($id); |
|
702 |
if ( 'RAND' == $order ) |
|
703 |
$orderby = 'none'; |
|
704 |
|
|
705 |
if ( !empty($include) ) { |
|
706 |
$include = preg_replace( '/[^0-9,]+/', '', $include ); |
|
707 |
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); |
|
708 |
|
|
709 |
$attachments = array(); |
|
710 |
foreach ( $_attachments as $key => $val ) { |
|
711 |
$attachments[$val->ID] = $_attachments[$key]; |
|
712 |
} |
|
713 |
} elseif ( !empty($exclude) ) { |
|
714 |
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude ); |
|
715 |
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); |
|
716 |
} else { |
|
717 |
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) ); |
|
718 |
} |
|
719 |
|
|
720 |
if ( empty($attachments) ) |
|
721 |
return ''; |
|
722 |
|
|
723 |
if ( is_feed() ) { |
|
724 |
$output = "\n"; |
|
725 |
foreach ( $attachments as $att_id => $attachment ) |
|
726 |
$output .= wp_get_attachment_link($att_id, $size, true) . "\n"; |
|
727 |
return $output; |
|
728 |
} |
|
729 |
|
|
730 |
$itemtag = tag_escape($itemtag); |
|
731 |
$captiontag = tag_escape($captiontag); |
|
732 |
$columns = intval($columns); |
|
733 |
$itemwidth = $columns > 0 ? floor(100/$columns) : 100; |
|
734 |
$float = $wp_locale->text_direction == 'rtl' ? 'right' : 'left'; |
|
735 |
|
|
736 |
$selector = "gallery-{$instance}"; |
|
737 |
|
|
738 |
$output = apply_filters('gallery_style', " |
|
739 |
<style type='text/css'> |
|
740 |
#{$selector} { |
|
741 |
margin: auto; |
|
742 |
} |
|
743 |
#{$selector} .gallery-item { |
|
744 |
float: {$float}; |
|
745 |
margin-top: 10px; |
|
746 |
text-align: center; |
|
747 |
width: {$itemwidth}%; } |
|
748 |
#{$selector} img { |
|
749 |
border: 2px solid #cfcfcf; |
|
750 |
} |
|
751 |
#{$selector} .gallery-caption { |
|
752 |
margin-left: 0; |
|
753 |
} |
|
754 |
</style> |
|
755 |
<!-- see gallery_shortcode() in wp-includes/media.php --> |
|
756 |
<div id='$selector' class='gallery galleryid-{$id}'>"); |
|
757 |
|
|
758 |
$i = 0; |
|
759 |
foreach ( $attachments as $id => $attachment ) { |
|
760 |
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false); |
|
761 |
|
|
762 |
$output .= "<{$itemtag} class='gallery-item'>"; |
|
763 |
$output .= " |
|
764 |
<{$icontag} class='gallery-icon'> |
|
765 |
$link |
|
766 |
</{$icontag}>"; |
|
767 |
if ( $captiontag && trim($attachment->post_excerpt) ) { |
|
768 |
$output .= " |
|
769 |
<{$captiontag} class='gallery-caption'> |
|
770 |
" . wptexturize($attachment->post_excerpt) . " |
|
771 |
</{$captiontag}>"; |
|
772 |
} |
|
773 |
$output .= "</{$itemtag}>"; |
|
774 |
if ( $columns > 0 && ++$i % $columns == 0 ) |
|
775 |
$output .= '<br style="clear: both" />'; |
|
776 |
} |
|
777 |
|
|
778 |
$output .= " |
|
779 |
<br style='clear: both;' /> |
|
780 |
</div>\n"; |
|
781 |
|
|
782 |
return $output; |
|
783 |
} |
|
784 |
|
|
785 |
/** |
|
786 |
* Display previous image link that has the same post parent. |
|
787 |
* |
|
788 |
* @since 2.5.0 |
|
789 |
* @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text; |
|
790 |
* @param string $text Optional, default is false. If included, link will reflect $text variable. |
|
791 |
* @return string HTML content. |
|
792 |
*/ |
|
793 |
function previous_image_link($size = 'thumbnail', $text = false) { |
|
794 |
adjacent_image_link(true, $size, $text); |
|
795 |
} |
|
796 |
|
|
797 |
/** |
|
798 |
* Display next image link that has the same post parent. |
|
799 |
* |
|
800 |
* @since 2.5.0 |
|
801 |
* @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text; |
|
802 |
* @param string $text Optional, default is false. If included, link will reflect $text variable. |
|
803 |
* @return string HTML content. |
|
804 |
*/ |
|
805 |
function next_image_link($size = 'thumbnail', $text = false) { |
|
806 |
adjacent_image_link(false, $size, $text); |
|
807 |
} |
|
808 |
|
|
809 |
/** |
|
810 |
* Display next or previous image link that has the same post parent. |
|
811 |
* |
|
812 |
* Retrieves the current attachment object from the $post global. |
|
813 |
* |
|
814 |
* @since 2.5.0 |
|
815 |
* |
|
816 |
* @param bool $prev Optional. Default is true to display previous link, true for next. |
|
817 |
*/ |
|
818 |
function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) { |
|
819 |
global $post; |
|
820 |
$post = get_post($post); |
|
821 |
$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') )); |
|
822 |
|
|
823 |
foreach ( $attachments as $k => $attachment ) |
|
824 |
if ( $attachment->ID == $post->ID ) |
|
825 |
break; |
|
826 |
|
|
827 |
$k = $prev ? $k - 1 : $k + 1; |
|
828 |
|
|
829 |
if ( isset($attachments[$k]) ) |
|
830 |
echo wp_get_attachment_link($attachments[$k]->ID, $size, true, false, $text); |
|
831 |
} |
|
832 |
|
|
833 |
/** |
|
834 |
* Retrieve taxonomies attached to the attachment. |
|
835 |
* |
|
836 |
* @since 2.5.0 |
|
837 |
* |
|
838 |
* @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object. |
|
839 |
* @return array Empty array on failure. List of taxonomies on success. |
|
840 |
*/ |
|
841 |
function get_attachment_taxonomies($attachment) { |
|
842 |
if ( is_int( $attachment ) ) |
|
843 |
$attachment = get_post($attachment); |
|
844 |
else if ( is_array($attachment) ) |
|
845 |
$attachment = (object) $attachment; |
|
846 |
|
|
847 |
if ( ! is_object($attachment) ) |
|
848 |
return array(); |
|
849 |
|
|
850 |
$filename = basename($attachment->guid); |
|
851 |
|
|
852 |
$objects = array('attachment'); |
|
853 |
|
|
854 |
if ( false !== strpos($filename, '.') ) |
|
855 |
$objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1); |
|
856 |
if ( !empty($attachment->post_mime_type) ) { |
|
857 |
$objects[] = 'attachment:' . $attachment->post_mime_type; |
|
858 |
if ( false !== strpos($attachment->post_mime_type, '/') ) |
|
859 |
foreach ( explode('/', $attachment->post_mime_type) as $token ) |
|
860 |
if ( !empty($token) ) |
|
861 |
$objects[] = "attachment:$token"; |
|
862 |
} |
|
863 |
|
|
864 |
$taxonomies = array(); |
|
865 |
foreach ( $objects as $object ) |
|
866 |
if ( $taxes = get_object_taxonomies($object) ) |
|
867 |
$taxonomies = array_merge($taxonomies, $taxes); |
|
868 |
|
|
869 |
return array_unique($taxonomies); |
|
870 |
} |
|
871 |
|
|
872 |
/** |
|
873 |
* Check if the installed version of GD supports particular image type |
|
874 |
* |
|
875 |
* @since 2.9.0 |
|
876 |
* |
|
877 |
* @param $mime_type string |
|
878 |
* @return bool |
|
879 |
*/ |
|
880 |
function gd_edit_image_support($mime_type) { |
|
881 |
if ( function_exists('imagetypes') ) { |
|
882 |
switch( $mime_type ) { |
|
883 |
case 'image/jpeg': |
|
884 |
return (imagetypes() & IMG_JPG) != 0; |
|
885 |
case 'image/png': |
|
886 |
return (imagetypes() & IMG_PNG) != 0; |
|
887 |
case 'image/gif': |
|
888 |
return (imagetypes() & IMG_GIF) != 0; |
|
889 |
} |
|
890 |
} else { |
|
891 |
switch( $mime_type ) { |
|
892 |
case 'image/jpeg': |
|
893 |
return function_exists('imagecreatefromjpeg'); |
|
894 |
case 'image/png': |
|
895 |
return function_exists('imagecreatefrompng'); |
|
896 |
case 'image/gif': |
|
897 |
return function_exists('imagecreatefromgif'); |
|
898 |
} |
|
899 |
} |
|
900 |
return false; |
|
901 |
} |
|
902 |
|
|
903 |
/** |
|
904 |
* Create new GD image resource with transparency support |
|
905 |
* |
|
906 |
* @since 2.9.0 |
|
907 |
* |
|
908 |
* @param $width |
|
909 |
* @param $height |
|
910 |
* @return image resource |
|
911 |
*/ |
|
912 |
function wp_imagecreatetruecolor($width, $height) { |
|
913 |
$img = imagecreatetruecolor($width, $height); |
|
914 |
if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) { |
|
915 |
imagealphablending($img, false); |
|
916 |
imagesavealpha($img, true); |
|
917 |
} |
|
918 |
return $img; |
|
919 |
} |
|
920 |
|
|
921 |
/** |
|
922 |
* API for easily embedding rich media such as videos and images into content. |
|
923 |
* |
|
924 |
* @package WordPress |
|
925 |
* @subpackage Embed |
|
926 |
* @since 2.9.0 |
|
927 |
*/ |
|
928 |
class WP_Embed { |
|
929 |
var $handlers = array(); |
|
930 |
var $post_ID; |
|
931 |
var $usecache = true; |
|
932 |
var $linkifunknown = true; |
|
933 |
|
|
934 |
/** |
|
935 |
* PHP4 constructor |
|
936 |
*/ |
|
937 |
function WP_Embed() { |
|
938 |
return $this->__construct(); |
|
939 |
} |
|
940 |
|
|
941 |
/** |
|
942 |
* PHP5 constructor |
|
943 |
*/ |
|
944 |
function __construct() { |
|
945 |
// Hack to get the [embed] shortcode to run before wpautop() |
|
946 |
add_filter( 'the_content', array(&$this, 'run_shortcode'), 8 ); |
|
947 |
|
|
948 |
// Attempts to embed all URLs in a post |
|
949 |
if ( get_option('embed_autourls') ) |
|
950 |
add_filter( 'the_content', array(&$this, 'autoembed'), 8 ); |
|
951 |
|
|
952 |
// After a post is saved, invalidate the oEmbed cache |
|
953 |
add_action( 'save_post', array(&$this, 'delete_oembed_caches') ); |
|
954 |
|
|
955 |
// After a post is saved, cache oEmbed items via AJAX |
|
956 |
add_action( 'edit_form_advanced', array(&$this, 'maybe_run_ajax_cache') ); |
|
957 |
} |
|
958 |
|
|
959 |
/** |
|
960 |
* Process the [embed] shortcode. |
|
961 |
* |
|
962 |
* Since the [embed] shortcode needs to be run earlier than other shortcodes, |
|
963 |
* this function removes all existing shortcodes, registers the [embed] shortcode, |
|
964 |
* calls {@link do_shortcode()}, and then re-registers the old shortcodes. |
|
965 |
* |
|
966 |
* @uses $shortcode_tags |
|
967 |
* @uses remove_all_shortcodes() |
|
968 |
* @uses add_shortcode() |
|
969 |
* @uses do_shortcode() |
|
970 |
* |
|
971 |
* @param string $content Content to parse |
|
972 |
* @return string Content with shortcode parsed |
|
973 |
*/ |
|
974 |
function run_shortcode( $content ) { |
|
975 |
global $shortcode_tags; |
|
976 |
|
|
977 |
// Backup current registered shortcodes and clear them all out |
|
978 |
$orig_shortcode_tags = $shortcode_tags; |
|
979 |
remove_all_shortcodes(); |
|
980 |
|
|
981 |
add_shortcode( 'embed', array(&$this, 'shortcode') ); |
|
982 |
|
|
983 |
// Do the shortcode (only the [embed] one is registered) |
|
984 |
$content = do_shortcode( $content ); |
|
985 |
|
|
986 |
// Put the original shortcodes back |
|
987 |
$shortcode_tags = $orig_shortcode_tags; |
|
988 |
|
|
989 |
return $content; |
|
990 |
} |
|
991 |
|
|
992 |
/** |
|
993 |
* If a post/page was saved, then output Javascript to make |
|
994 |
* an AJAX request that will call WP_Embed::cache_oembed(). |
|
995 |
*/ |
|
996 |
function maybe_run_ajax_cache() { |
|
997 |
global $post_ID; |
|
998 |
|
|
999 |
if ( empty($post_ID) || empty($_GET['message']) || 1 != $_GET['message'] ) |
|
1000 |
return; |
|
1001 |
|
|
1002 |
?> |
|
1003 |
<script type="text/javascript"> |
|
1004 |
/* <![CDATA[ */ |
|
1005 |
jQuery(document).ready(function($){ |
|
1006 |
$.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post_ID ); ?>"); |
|
1007 |
}); |
|
1008 |
/* ]]> */ |
|
1009 |
</script> |
|
1010 |
<?php |
|
1011 |
} |
|
1012 |
|
|
1013 |
/** |
|
1014 |
* Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead. |
|
1015 |
* This function should probably also only be used for sites that do not support oEmbed. |
|
1016 |
* |
|
1017 |
* @param string $id An internal ID/name for the handler. Needs to be unique. |
|
1018 |
* @param string $regex The regex that will be used to see if this handler should be used for a URL. |
|
1019 |
* @param callback $callback The callback function that will be called if the regex is matched. |
|
1020 |
* @param int $priority Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action. |
|
1021 |
*/ |
|
1022 |
function register_handler( $id, $regex, $callback, $priority = 10 ) { |
|
1023 |
$this->handlers[$priority][$id] = array( |
|
1024 |
'regex' => $regex, |
|
1025 |
'callback' => $callback, |
|
1026 |
); |
|
1027 |
} |
|
1028 |
|
|
1029 |
/** |
|
1030 |
* Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead. |
|
1031 |
* |
|
1032 |
* @param string $id The handler ID that should be removed. |
|
1033 |
* @param int $priority Optional. The priority of the handler to be removed (default: 10). |
|
1034 |
*/ |
|
1035 |
function unregister_handler( $id, $priority = 10 ) { |
|
1036 |
if ( isset($this->handlers[$priority][$id]) ) |
|
1037 |
unset($this->handlers[$priority][$id]); |
|
1038 |
} |
|
1039 |
|
|
1040 |
/** |
|
1041 |
* The {@link do_shortcode()} callback function. |
|
1042 |
* |
|
1043 |
* Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers. |
|
1044 |
* If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class. |
|
1045 |
* |
|
1046 |
* @uses wp_oembed_get() |
|
1047 |
* @uses wp_parse_args() |
|
1048 |
* @uses wp_embed_defaults() |
|
1049 |
* @uses WP_Embed::maybe_make_link() |
|
1050 |
* @uses get_option() |
|
1051 |
* @uses current_user_can() |
|
1052 |
* @uses wp_cache_get() |
|
1053 |
* @uses wp_cache_set() |
|
1054 |
* @uses get_post_meta() |
|
1055 |
* @uses update_post_meta() |
|
1056 |
* |
|
1057 |
* @param array $attr Shortcode attributes. |
|
1058 |
* @param string $url The URL attempting to be embeded. |
|
1059 |
* @return string The embed HTML on success, otherwise the original URL. |
|
1060 |
*/ |
|
1061 |
function shortcode( $attr, $url = '' ) { |
|
1062 |
global $post; |
|
1063 |
|
|
1064 |
if ( empty($url) ) |
|
1065 |
return ''; |
|
1066 |
|
|
1067 |
$rawattr = $attr; |
|
1068 |
$attr = wp_parse_args( $attr, wp_embed_defaults() ); |
|
1069 |
|
|
1070 |
// Look for known internal handlers |
|
1071 |
ksort( $this->handlers ); |
|
1072 |
foreach ( $this->handlers as $priority => $handlers ) { |
|
1073 |
foreach ( $handlers as $id => $handler ) { |
|
1074 |
if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) { |
|
1075 |
if ( false !== $return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr ) ) |
|
1076 |
return apply_filters( 'embed_handler_html', $return, $url, $attr ); |
|
1077 |
} |
|
1078 |
} |
|
1079 |
} |
|
1080 |
|
|
1081 |
$post_ID = ( !empty($post->ID) ) ? $post->ID : null; |
|
1082 |
if ( !empty($this->post_ID) ) // Potentially set by WP_Embed::cache_oembed() |
|
1083 |
$post_ID = $this->post_ID; |
|
1084 |
|
|
1085 |
// Unknown URL format. Let oEmbed have a go. |
|
1086 |
if ( $post_ID ) { |
|
1087 |
|
|
1088 |
// Check for a cached result (stored in the post meta) |
|
1089 |
$cachekey = '_oembed_' . md5( $url . serialize( $attr ) ); |
|
1090 |
if ( $this->usecache ) { |
|
1091 |
$cache = get_post_meta( $post_ID, $cachekey, true ); |
|
1092 |
|
|
1093 |
// Failures are cached |
|
1094 |
if ( '{{unknown}}' === $cache ) |
|
1095 |
return $this->maybe_make_link( $url ); |
|
1096 |
|
|
1097 |
if ( !empty($cache) ) |
|
1098 |
return apply_filters( 'embed_oembed_html', $cache, $url, $attr ); |
|
1099 |
} |
|
1100 |
|
|
1101 |
// Use oEmbed to get the HTML |
|
1102 |
$attr['discover'] = ( apply_filters('embed_oembed_discover', false) && author_can( $post_ID, 'unfiltered_html' ) ) ? true : false; |
|
1103 |
$html = wp_oembed_get( $url, $attr ); |
|
1104 |
|
|
1105 |
// Cache the result |
|
1106 |
$cache = ( $html ) ? $html : '{{unknown}}'; |
|
1107 |
update_post_meta( $post_ID, $cachekey, $cache ); |
|
1108 |
|
|
1109 |
// If there was a result, return it |
|
1110 |
if ( $html ) |
|
1111 |
return apply_filters( 'embed_oembed_html', $html, $url, $attr ); |
|
1112 |
} |
|
1113 |
|
|
1114 |
// Still unknown |
|
1115 |
return $this->maybe_make_link( $url ); |
|
1116 |
} |
|
1117 |
|
|
1118 |
/** |
|
1119 |
* Delete all oEmbed caches. |
|
1120 |
* |
|
1121 |
* @param int $post_ID Post ID to delete the caches for. |
|
1122 |
*/ |
|
1123 |
function delete_oembed_caches( $post_ID ) { |
|
1124 |
$post_metas = get_post_custom_keys( $post_ID ); |
|
1125 |
if ( empty($post_metas) ) |
|
1126 |
return; |
|
1127 |
|
|
1128 |
foreach( $post_metas as $post_meta_key ) { |
|
1129 |
if ( '_oembed_' == substr( $post_meta_key, 0, 8 ) ) |
|
1130 |
delete_post_meta( $post_ID, $post_meta_key ); |
|
1131 |
} |
|
1132 |
} |
|
1133 |
|
|
1134 |
/** |
|
1135 |
* Triggers a caching of all oEmbed results. |
|
1136 |
* |
|
1137 |
* @param int $post_ID Post ID to do the caching for. |
|
1138 |
*/ |
|
1139 |
function cache_oembed( $post_ID ) { |
|
1140 |
$post = get_post( $post_ID ); |
|
1141 |
|
|
1142 |
if ( empty($post->ID) || !in_array( $post->post_type, apply_filters( 'embed_cache_oembed_types', array( 'post', 'page' ) ) ) ) |
|
1143 |
return; |
|
1144 |
|
|
1145 |
// Trigger a caching |
|
1146 |
if ( !empty($post->post_content) ) { |
|
1147 |
$this->post_ID = $post->ID; |
|
1148 |
$this->usecache = false; |
|
1149 |
|
|
1150 |
$content = $this->run_shortcode( $post->post_content ); |
|
1151 |
if ( get_option('embed_autourls') ) |
|
1152 |
$this->autoembed( $content ); |
|
1153 |
|
|
1154 |
$this->usecache = true; |
|
1155 |
} |
|
1156 |
} |
|
1157 |
|
|
1158 |
/** |
|
1159 |
* Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding. |
|
1160 |
* |
|
1161 |
* @uses WP_Embed::autoembed_callback() |
|
1162 |
* |
|
1163 |
* @param string $content The content to be searched. |
|
1164 |
* @return string Potentially modified $content. |
|
1165 |
*/ |
|
1166 |
function autoembed( $content ) { |
|
1167 |
return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array(&$this, 'autoembed_callback'), $content ); |
|
1168 |
} |
|
1169 |
|
|
1170 |
/** |
|
1171 |
* Callback function for {@link WP_Embed::autoembed()}. |
|
1172 |
* |
|
1173 |
* @uses WP_Embed::shortcode() |
|
1174 |
* |
|
1175 |
* @param array $match A regex match array. |
|
1176 |
* @return string The embed HTML on success, otherwise the original URL. |
|
1177 |
*/ |
|
1178 |
function autoembed_callback( $match ) { |
|
1179 |
$oldval = $this->linkifunknown; |
|
1180 |
$this->linkifunknown = false; |
|
1181 |
$return = $this->shortcode( array(), $match[1] ); |
|
1182 |
$this->linkifunknown = $oldval; |
|
1183 |
|
|
1184 |
return "\n$return\n"; |
|
1185 |
} |
|
1186 |
|
|
1187 |
/** |
|
1188 |
* Conditionally makes a hyperlink based on an internal class variable. |
|
1189 |
* |
|
1190 |
* @param string $url URL to potentially be linked. |
|
1191 |
* @return string Linked URL or the original URL. |
|
1192 |
*/ |
|
1193 |
function maybe_make_link( $url ) { |
|
1194 |
$output = ( $this->linkifunknown ) ? '<a href="' . esc_attr($url) . '">' . esc_html($url) . '</a>' : $url; |
|
1195 |
return apply_filters( 'embed_maybe_make_link', $output, $url ); |
|
1196 |
} |
|
1197 |
} |
|
1198 |
$wp_embed = new WP_Embed(); |
|
1199 |
|
|
1200 |
/** |
|
1201 |
* Register an embed handler. This function should probably only be used for sites that do not support oEmbed. |
|
1202 |
* |
|
1203 |
* @since 2.9.0 |
|
1204 |
* @see WP_Embed::register_handler() |
|
1205 |
*/ |
|
1206 |
function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) { |
|
1207 |
global $wp_embed; |
|
1208 |
$wp_embed->register_handler( $id, $regex, $callback, $priority ); |
|
1209 |
} |
|
1210 |
|
|
1211 |
/** |
|
1212 |
* Unregister a previously registered embed handler. |
|
1213 |
* |
|
1214 |
* @since 2.9.0 |
|
1215 |
* @see WP_Embed::unregister_handler() |
|
1216 |
*/ |
|
1217 |
function wp_embed_unregister_handler( $id, $priority = 10 ) { |
|
1218 |
global $wp_embed; |
|
1219 |
$wp_embed->unregister_handler( $id, $priority ); |
|
1220 |
} |
|
1221 |
|
|
1222 |
/** |
|
1223 |
* Create default array of embed parameters. |
|
1224 |
* |
|
1225 |
* @since 2.9.0 |
|
1226 |
* |
|
1227 |
* @return array Default embed parameters. |
|
1228 |
*/ |
|
1229 |
function wp_embed_defaults() { |
|
1230 |
if ( !empty($GLOBALS['content_width']) ) |
|
1231 |
$theme_width = (int) $GLOBALS['content_width']; |
|
1232 |
|
|
1233 |
$width = get_option('embed_size_w'); |
|
1234 |
|
|
1235 |
if ( !$width && !empty($theme_width) ) |
|
1236 |
$width = $theme_width; |
|
1237 |
|
|
1238 |
if ( !$width ) |
|
1239 |
$width = 500; |
|
1240 |
|
|
1241 |
return apply_filters( 'embed_defaults', array( |
|
1242 |
'width' => $width, |
|
1243 |
'height' => 700, |
|
1244 |
) ); |
|
1245 |
} |
|
1246 |
|
|
1247 |
/** |
|
1248 |
* Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. |
|
1249 |
* |
|
1250 |
* @since 2.9.0 |
|
1251 |
* @uses wp_constrain_dimensions() This function passes the widths and the heights. |
|
1252 |
* |
|
1253 |
* @param int $example_width The width of an example embed. |
|
1254 |
* @param int $example_height The height of an example embed. |
|
1255 |
* @param int $max_width The maximum allowed width. |
|
1256 |
* @param int $max_height The maximum allowed height. |
|
1257 |
* @return array The maximum possible width and height based on the example ratio. |
|
1258 |
*/ |
|
1259 |
function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) { |
|
1260 |
$example_width = (int) $example_width; |
|
1261 |
$example_height = (int) $example_height; |
|
1262 |
$max_width = (int) $max_width; |
|
1263 |
$max_height = (int) $max_height; |
|
1264 |
|
|
1265 |
return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); |
|
1266 |
} |
|
1267 |
|
|
1268 |
/** |
|
1269 |
* Attempts to fetch the embed HTML for a provided URL using oEmbed. |
|
1270 |
* |
|
1271 |
* @since 2.9.0 |
|
1272 |
* @see WP_oEmbed |
|
1273 |
* |
|
1274 |
* @uses _wp_oembed_get_object() |
|
1275 |
* @uses WP_oEmbed::get_html() |
|
1276 |
* |
|
1277 |
* @param string $url The URL that should be embeded. |
|
1278 |
* @param array $args Addtional arguments and parameters. |
|
1279 |
* @return string The original URL on failure or the embed HTML on success. |
|
1280 |
*/ |
|
1281 |
function wp_oembed_get( $url, $args = '' ) { |
|
1282 |
require_once( 'class-oembed.php' ); |
|
1283 |
$oembed = _wp_oembed_get_object(); |
|
1284 |
return $oembed->get_html( $url, $args ); |
|
1285 |
} |
|
1286 |
|
|
1287 |
/** |
|
1288 |
* Adds a URL format and oEmbed provider URL pair. |
|
1289 |
* |
|
1290 |
* @since 2.9.0 |
|
1291 |
* @see WP_oEmbed |
|
1292 |
* |
|
1293 |
* @uses _wp_oembed_get_object() |
|
1294 |
* |
|
1295 |
* @param string $format The format of URL that this provider can handle. You can use asterisks as wildcards. |
|
1296 |
* @param string $provider The URL to the oEmbed provider. |
|
1297 |
* @param boolean $regex Whether the $format parameter is in a regex format or not. |
|
1298 |
*/ |
|
1299 |
function wp_oembed_add_provider( $format, $provider, $regex = false ) { |
|
1300 |
require_once( 'class-oembed.php' ); |
|
1301 |
$oembed = _wp_oembed_get_object(); |
|
1302 |
$oembed->providers[$format] = array( $provider, $regex ); |
|
1303 |
} |