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