40 * Finally, there is a filter named {@see 'editor_max_image_size'}, that will be |
40 * Finally, there is a filter named {@see 'editor_max_image_size'}, that will be |
41 * called on the calculated array for width and height, respectively. |
41 * called on the calculated array for width and height, respectively. |
42 * |
42 * |
43 * @since 2.5.0 |
43 * @since 2.5.0 |
44 * |
44 * |
45 * @global int $content_width |
45 * @global int $content_width |
46 * |
46 * |
47 * @param int $width Width of the image in pixels. |
47 * @param int $width Width of the image in pixels. |
48 * @param int $height Height of the image in pixels. |
48 * @param int $height Height of the image in pixels. |
49 * @param string|array $size Optional. Image size. Accepts any valid image size, or an array |
49 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
50 * of width and height values in pixels (in that order). |
50 * of width and height values in pixels (in that order). Default 'medium'. |
51 * Default 'medium'. |
|
52 * @param string $context Optional. Could be 'display' (like in a theme) or 'edit' |
51 * @param string $context Optional. Could be 'display' (like in a theme) or 'edit' |
53 * (like inserting into an editor). Default null. |
52 * (like inserting into an editor). Default null. |
54 * @return int[] { |
53 * @return int[] { |
55 * An array of width and height values. |
54 * An array of width and height values. |
56 * |
55 * |
69 |
68 |
70 if ( is_array( $size ) ) { |
69 if ( is_array( $size ) ) { |
71 $max_width = $size[0]; |
70 $max_width = $size[0]; |
72 $max_height = $size[1]; |
71 $max_height = $size[1]; |
73 } elseif ( 'thumb' === $size || 'thumbnail' === $size ) { |
72 } elseif ( 'thumb' === $size || 'thumbnail' === $size ) { |
74 $max_width = intval( get_option( 'thumbnail_size_w' ) ); |
73 $max_width = (int) get_option( 'thumbnail_size_w' ); |
75 $max_height = intval( get_option( 'thumbnail_size_h' ) ); |
74 $max_height = (int) get_option( 'thumbnail_size_h' ); |
76 // Last chance thumbnail size defaults. |
75 // Last chance thumbnail size defaults. |
77 if ( ! $max_width && ! $max_height ) { |
76 if ( ! $max_width && ! $max_height ) { |
78 $max_width = 128; |
77 $max_width = 128; |
79 $max_height = 96; |
78 $max_height = 96; |
80 } |
79 } |
81 } elseif ( 'medium' === $size ) { |
80 } elseif ( 'medium' === $size ) { |
82 $max_width = intval( get_option( 'medium_size_w' ) ); |
81 $max_width = (int) get_option( 'medium_size_w' ); |
83 $max_height = intval( get_option( 'medium_size_h' ) ); |
82 $max_height = (int) get_option( 'medium_size_h' ); |
84 |
83 |
85 } elseif ( 'medium_large' === $size ) { |
84 } elseif ( 'medium_large' === $size ) { |
86 $max_width = intval( get_option( 'medium_large_size_w' ) ); |
85 $max_width = (int) get_option( 'medium_large_size_w' ); |
87 $max_height = intval( get_option( 'medium_large_size_h' ) ); |
86 $max_height = (int) get_option( 'medium_large_size_h' ); |
88 |
87 |
89 if ( intval( $content_width ) > 0 ) { |
88 if ( (int) $content_width > 0 ) { |
90 $max_width = min( intval( $content_width ), $max_width ); |
89 $max_width = min( (int) $content_width, $max_width ); |
91 } |
90 } |
92 } elseif ( 'large' === $size ) { |
91 } elseif ( 'large' === $size ) { |
93 /* |
92 /* |
94 * We're inserting a large size image into the editor. If it's a really |
93 * We're inserting a large size image into the editor. If it's a really |
95 * big image we'll scale it down to fit reasonably within the editor |
94 * big image we'll scale it down to fit reasonably within the editor |
96 * itself, and within the theme's content width if it's known. The user |
95 * itself, and within the theme's content width if it's known. The user |
97 * can resize it in the editor if they wish. |
96 * can resize it in the editor if they wish. |
98 */ |
97 */ |
99 $max_width = intval( get_option( 'large_size_w' ) ); |
98 $max_width = (int) get_option( 'large_size_w' ); |
100 $max_height = intval( get_option( 'large_size_h' ) ); |
99 $max_height = (int) get_option( 'large_size_h' ); |
101 |
100 |
102 if ( intval( $content_width ) > 0 ) { |
101 if ( (int) $content_width > 0 ) { |
103 $max_width = min( intval( $content_width ), $max_width ); |
102 $max_width = min( (int) $content_width, $max_width ); |
104 } |
103 } |
105 } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) { |
104 } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) { |
106 $max_width = intval( $_wp_additional_image_sizes[ $size ]['width'] ); |
105 $max_width = (int) $_wp_additional_image_sizes[ $size ]['width']; |
107 $max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] ); |
106 $max_height = (int) $_wp_additional_image_sizes[ $size ]['height']; |
108 // Only in admin. Assume that theme authors know what they're doing. |
107 // Only in admin. Assume that theme authors know what they're doing. |
109 if ( intval( $content_width ) > 0 && 'edit' === $context ) { |
108 if ( (int) $content_width > 0 && 'edit' === $context ) { |
110 $max_width = min( intval( $content_width ), $max_width ); |
109 $max_width = min( (int) $content_width, $max_width ); |
111 } |
110 } |
112 } else { // $size === 'full' has no constraint. |
111 } else { // $size === 'full' has no constraint. |
113 $max_width = $width; |
112 $max_width = $width; |
114 $max_height = $height; |
113 $max_height = $height; |
115 } |
114 } |
123 * An array of width and height values. |
122 * An array of width and height values. |
124 * |
123 * |
125 * @type int $0 The maximum width in pixels. |
124 * @type int $0 The maximum width in pixels. |
126 * @type int $1 The maximum height in pixels. |
125 * @type int $1 The maximum height in pixels. |
127 * } |
126 * } |
128 * @param string|array $size Size of what the result image should be. |
127 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
129 * @param string $context The context the image is being resized for. |
128 * an array of width and height values in pixels (in that order). |
130 * Possible values are 'display' (like in a theme) |
129 * @param string $context The context the image is being resized for. |
131 * or 'edit' (like inserting into an editor). |
130 * Possible values are 'display' (like in a theme) |
|
131 * or 'edit' (like inserting into an editor). |
132 */ |
132 */ |
133 list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); |
133 list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); |
134 |
134 |
135 return wp_constrain_dimensions( $width, $height, $max_width, $max_height ); |
135 return wp_constrain_dimensions( $width, $height, $max_width, $max_height ); |
136 } |
136 } |
175 * elements that are normally returned from the function. |
175 * elements that are normally returned from the function. |
176 * |
176 * |
177 * @since 2.5.0 |
177 * @since 2.5.0 |
178 * |
178 * |
179 * @param int $id Attachment ID for image. |
179 * @param int $id Attachment ID for image. |
180 * @param string|int[] $size Optional. Image size to scale to. Accepts any valid image size name, |
180 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
181 * or an array of width and height values in pixels (in that order). |
181 * of width and height values in pixels (in that order). Default 'medium'. |
182 * Default 'medium'. |
|
183 * @return array|false { |
182 * @return array|false { |
184 * Array of image data, or boolean false if no image is available. |
183 * Array of image data, or boolean false if no image is available. |
185 * |
184 * |
186 * @type string $0 Image source URL. |
185 * @type string $0 Image source URL. |
187 * @type int $1 Image width in pixels. |
186 * @type int $1 Image width in pixels. |
365 * |
364 * |
366 * @param int $id Attachment ID. |
365 * @param int $id Attachment ID. |
367 * @param string $alt Image description for the alt attribute. |
366 * @param string $alt Image description for the alt attribute. |
368 * @param string $title Image description for the title attribute. |
367 * @param string $title Image description for the title attribute. |
369 * @param string $align Part of the class name for aligning the image. |
368 * @param string $align Part of the class name for aligning the image. |
370 * @param string|array $size Optional. Registered image size to retrieve a tag for. Accepts any |
369 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
371 * valid image size, or an array of width and height values in pixels |
370 * width and height values in pixels (in that order). Default 'medium'. |
372 * (in that order). Default 'medium'. |
|
373 * @return string HTML IMG element for given image attachment |
371 * @return string HTML IMG element for given image attachment |
374 */ |
372 */ |
375 function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { |
373 function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { |
376 |
374 |
377 list( $img_src, $width, $height ) = image_downsize( $id, $size ); |
375 list( $img_src, $width, $height ) = image_downsize( $id, $size ); |
378 $hwstring = image_hwstring( $width, $height ); |
376 $hwstring = image_hwstring( $width, $height ); |
379 |
377 |
380 $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; |
378 $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; |
381 |
379 |
382 $class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size ) . ' wp-image-' . $id; |
380 $size_class = is_array( $size ) ? implode( 'x', $size ) : $size; |
|
381 $class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id; |
383 |
382 |
384 /** |
383 /** |
385 * Filters the value of the attachment's image tag class attribute. |
384 * Filters the value of the attachment's image tag class attribute. |
386 * |
385 * |
387 * @since 2.6.0 |
386 * @since 2.6.0 |
388 * |
387 * |
389 * @param string $class CSS class name or space-separated list of classes. |
388 * @param string $class CSS class name or space-separated list of classes. |
390 * @param int $id Attachment ID. |
389 * @param int $id Attachment ID. |
391 * @param string $align Part of the class name for aligning the image. |
390 * @param string $align Part of the class name for aligning the image. |
392 * @param string|array $size Size of image. Image size or array of width and height values (in that order). |
391 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
393 * Default 'medium'. |
392 * an array of width and height values in pixels (in that order). |
394 */ |
393 */ |
395 $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); |
394 $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); |
396 |
395 |
397 $html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; |
396 $html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; |
398 |
397 |
404 * @param string $html HTML content for the image. |
403 * @param string $html HTML content for the image. |
405 * @param int $id Attachment ID. |
404 * @param int $id Attachment ID. |
406 * @param string $alt Image description for the alt attribute. |
405 * @param string $alt Image description for the alt attribute. |
407 * @param string $title Image description for the title attribute. |
406 * @param string $title Image description for the title attribute. |
408 * @param string $align Part of the class name for aligning the image. |
407 * @param string $align Part of the class name for aligning the image. |
409 * @param string|array $size Size of image. Image size or array of width and height values (in that order). |
408 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
410 * Default 'medium'. |
409 * an array of width and height values in pixels (in that order). |
411 */ |
410 */ |
412 return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); |
411 return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); |
413 } |
412 } |
414 |
413 |
415 /** |
414 /** |
742 * browser scale down the image. |
741 * browser scale down the image. |
743 * |
742 * |
744 * @since 2.5.0 |
743 * @since 2.5.0 |
745 * |
744 * |
746 * @param int $post_id Attachment ID. |
745 * @param int $post_id Attachment ID. |
747 * @param array|string $size Optional. Image size. Accepts any valid image size, or an array |
746 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
748 * of width and height values in pixels (in that order). |
747 * of width and height values in pixels (in that order). Default 'thumbnail'. |
749 * Default 'thumbnail'. |
|
750 * @return array|false { |
748 * @return array|false { |
751 * Array of file relative path, width, and height on success. Additionally includes absolute |
749 * Array of file relative path, width, and height on success. Additionally includes absolute |
752 * path and URL if registered size is passed to $size parameter. False on failure. |
750 * path and URL if registered size is passed to `$size` parameter. False on failure. |
753 * |
751 * |
754 * @type string $file Image's path relative to uploads directory |
752 * @type string $file Path of image relative to uploads directory. |
755 * @type int $width Width of image |
753 * @type int $width Width of image in pixels. |
756 * @type int $height Height of image |
754 * @type int $height Height of image in pixels. |
757 * @type string $path Image's absolute filesystem path. |
755 * @type string $path Absolute filesystem path of image. |
758 * @type string $url Image's URL. |
756 * @type string $url URL of image. |
759 * } |
757 * } |
760 */ |
758 */ |
761 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { |
759 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { |
762 $imagedata = wp_get_attachment_metadata( $post_id ); |
760 $imagedata = wp_get_attachment_metadata( $post_id ); |
763 |
761 |
842 * |
840 * |
843 * @see image_get_intermediate_size() |
841 * @see image_get_intermediate_size() |
844 * |
842 * |
845 * @param array $data Array of file relative path, width, and height on success. May also include |
843 * @param array $data Array of file relative path, width, and height on success. May also include |
846 * file absolute path and URL. |
844 * file absolute path and URL. |
847 * @param int $post_id The post_id of the image attachment |
845 * @param int $post_id The ID of the image attachment. |
848 * @param string|array $size Registered image size or flat array of initially-requested height and width |
846 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
849 * dimensions (in that order). |
847 * an array of width and height values in pixels (in that order). |
850 */ |
848 */ |
851 return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); |
849 return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); |
852 } |
850 } |
853 |
851 |
854 /** |
852 /** |
897 'crop' => false, |
895 'crop' => false, |
898 ); |
896 ); |
899 |
897 |
900 if ( isset( $additional_sizes[ $size_name ]['width'] ) ) { |
898 if ( isset( $additional_sizes[ $size_name ]['width'] ) ) { |
901 // For sizes added by plugins and themes. |
899 // For sizes added by plugins and themes. |
902 $size_data['width'] = intval( $additional_sizes[ $size_name ]['width'] ); |
900 $size_data['width'] = (int) $additional_sizes[ $size_name ]['width']; |
903 } else { |
901 } else { |
904 // For default sizes set in options. |
902 // For default sizes set in options. |
905 $size_data['width'] = intval( get_option( "{$size_name}_size_w" ) ); |
903 $size_data['width'] = (int) get_option( "{$size_name}_size_w" ); |
906 } |
904 } |
907 |
905 |
908 if ( isset( $additional_sizes[ $size_name ]['height'] ) ) { |
906 if ( isset( $additional_sizes[ $size_name ]['height'] ) ) { |
909 $size_data['height'] = intval( $additional_sizes[ $size_name ]['height'] ); |
907 $size_data['height'] = (int) $additional_sizes[ $size_name ]['height']; |
910 } else { |
908 } else { |
911 $size_data['height'] = intval( get_option( "{$size_name}_size_h" ) ); |
909 $size_data['height'] = (int) get_option( "{$size_name}_size_h" ); |
912 } |
910 } |
913 |
911 |
914 if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) { |
912 if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) { |
915 // This size isn't set. |
913 // This size isn't set. |
916 continue; |
914 continue; |
936 * Retrieves an image to represent an attachment. |
934 * Retrieves an image to represent an attachment. |
937 * |
935 * |
938 * @since 2.5.0 |
936 * @since 2.5.0 |
939 * |
937 * |
940 * @param int $attachment_id Image attachment ID. |
938 * @param int $attachment_id Image attachment ID. |
941 * @param string|int[] $size Optional. Image size. Accepts any valid image size name, or an array of width |
939 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
942 * and height values in pixels (in that order). Default 'thumbnail'. |
940 * width and height values in pixels (in that order). Default 'thumbnail'. |
943 * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. |
941 * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. |
944 * @return array|false { |
942 * @return array|false { |
945 * Array of image data, or boolean false if no image is available. |
943 * Array of image data, or boolean false if no image is available. |
946 * |
944 * |
947 * @type string $0 Image source URL. |
945 * @type string $0 Image source URL. |
984 * @type int $1 Image width in pixels. |
982 * @type int $1 Image width in pixels. |
985 * @type int $2 Image height in pixels. |
983 * @type int $2 Image height in pixels. |
986 * @type bool $3 Whether the image is a resized image. |
984 * @type bool $3 Whether the image is a resized image. |
987 * } |
985 * } |
988 * @param int $attachment_id Image attachment ID. |
986 * @param int $attachment_id Image attachment ID. |
989 * @param string|int[] $size Requested size of image. Image size name, or array of width |
987 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
990 * and height values (in that order). |
988 * an array of width and height values in pixels (in that order). |
991 * @param bool $icon Whether the image should be treated as an icon. |
989 * @param bool $icon Whether the image should be treated as an icon. |
992 */ |
990 */ |
993 return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); |
991 return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); |
994 } |
992 } |
995 |
993 |
996 /** |
994 /** |
997 * Get an HTML img element representing an image attachment |
995 * Get an HTML img element representing an image attachment. |
998 * |
996 * |
999 * While `$size` will accept an array, it is better to register a size with |
997 * While `$size` will accept an array, it is better to register a size with |
1000 * add_image_size() so that a cropped version is generated. It's much more |
998 * add_image_size() so that a cropped version is generated. It's much more |
1001 * efficient than having to find the closest-sized image and then having the |
999 * efficient than having to find the closest-sized image and then having the |
1002 * browser scale down the image. |
1000 * browser scale down the image. |
1004 * @since 2.5.0 |
1002 * @since 2.5.0 |
1005 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added. |
1003 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added. |
1006 * @since 5.5.0 The `$loading` attribute was added. |
1004 * @since 5.5.0 The `$loading` attribute was added. |
1007 * |
1005 * |
1008 * @param int $attachment_id Image attachment ID. |
1006 * @param int $attachment_id Image attachment ID. |
1009 * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width |
1007 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
1010 * and height values in pixels (in that order). Default 'thumbnail'. |
1008 * of width and height values in pixels (in that order). Default 'thumbnail'. |
1011 * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
1009 * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
1012 * @param string|array $attr { |
1010 * @param string|array $attr { |
1013 * Optional. Attributes for the image markup. |
1011 * Optional. Attributes for the image markup. |
1014 * |
1012 * |
1015 * @type string $src Image attachment URL. |
1013 * @type string $src Image attachment URL. |
1081 /** |
1079 /** |
1082 * Filters the list of attachment image attributes. |
1080 * Filters the list of attachment image attributes. |
1083 * |
1081 * |
1084 * @since 2.8.0 |
1082 * @since 2.8.0 |
1085 * |
1083 * |
1086 * @param array $attr Array of attribute values for the image markup, keyed by attribute name. |
1084 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. |
1087 * See wp_get_attachment_image(). |
1085 * See wp_get_attachment_image(). |
1088 * @param WP_Post $attachment Image attachment post. |
1086 * @param WP_Post $attachment Image attachment post. |
1089 * @param string|array $size Requested size. Image size or array of width and height values |
1087 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
1090 * (in that order). Default 'thumbnail'. |
1088 * an array of width and height values in pixels (in that order). |
1091 */ |
1089 */ |
1092 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); |
1090 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); |
1093 |
1091 |
1094 $attr = array_map( 'esc_attr', $attr ); |
1092 $attr = array_map( 'esc_attr', $attr ); |
1095 $html = rtrim( "<img $hwstring" ); |
1093 $html = rtrim( "<img $hwstring" ); |
1099 } |
1097 } |
1100 |
1098 |
1101 $html .= ' />'; |
1099 $html .= ' />'; |
1102 } |
1100 } |
1103 |
1101 |
1104 return $html; |
1102 /** |
|
1103 * HTML img element representing an image attachment. |
|
1104 * |
|
1105 * @since 5.6.0 |
|
1106 * |
|
1107 * @param string $html HTML img element or empty string on failure. |
|
1108 * @param int $attachment_id Image attachment ID. |
|
1109 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
1110 * an array of width and height values in pixels (in that order). |
|
1111 * @param bool $icon Whether the image should be treated as an icon. |
|
1112 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. |
|
1113 * See wp_get_attachment_image(). |
|
1114 */ |
|
1115 return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr ); |
1105 } |
1116 } |
1106 |
1117 |
1107 /** |
1118 /** |
1108 * Get the URL of an image attachment. |
1119 * Get the URL of an image attachment. |
1109 * |
1120 * |
1110 * @since 4.4.0 |
1121 * @since 4.4.0 |
1111 * |
1122 * |
1112 * @param int $attachment_id Image attachment ID. |
1123 * @param int $attachment_id Image attachment ID. |
1113 * @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array |
1124 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
1114 * of width and height values in pixels (in that order). Default 'thumbnail'. |
1125 * width and height values in pixels (in that order). Default 'thumbnail'. |
1115 * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
1126 * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
1116 * @return string|false Attachment URL or false if no image is available. |
1127 * @return string|false Attachment URL or false if no image is available. If `$size` does not match |
|
1128 * any registered image size, the original image URL will be returned. |
1117 */ |
1129 */ |
1118 function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { |
1130 function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { |
1119 $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
1131 $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
1120 return isset( $image['0'] ) ? $image['0'] : false; |
1132 return isset( $image['0'] ) ? $image['0'] : false; |
1121 } |
1133 } |
1151 * Used for responsive images. |
1163 * Used for responsive images. |
1152 * |
1164 * |
1153 * @since 4.4.0 |
1165 * @since 4.4.0 |
1154 * @access private |
1166 * @access private |
1155 * |
1167 * |
1156 * @param string $size_name Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.). |
1168 * @param string $size_name Image size. Accepts any registered image size name. |
1157 * @param array $image_meta The image meta data. |
1169 * @param array $image_meta The image meta data. |
1158 * @return array|bool The image meta data as returned by `wp_get_attachment_metadata()`. |
1170 * @return array|false { |
|
1171 * Array of width and height or false if the size isn't present in the meta data. |
|
1172 * |
|
1173 * @type int $0 Image width. |
|
1174 * @type int $1 Image height. |
|
1175 * } |
1159 */ |
1176 */ |
1160 function _wp_get_image_size_from_meta( $size_name, $image_meta ) { |
1177 function _wp_get_image_size_from_meta( $size_name, $image_meta ) { |
1161 if ( 'full' === $size_name ) { |
1178 if ( 'full' === $size_name ) { |
1162 return array( |
1179 return array( |
1163 absint( $image_meta['width'] ), |
1180 absint( $image_meta['width'] ), |
1179 * @since 4.4.0 |
1196 * @since 4.4.0 |
1180 * |
1197 * |
1181 * @see wp_calculate_image_srcset() |
1198 * @see wp_calculate_image_srcset() |
1182 * |
1199 * |
1183 * @param int $attachment_id Image attachment ID. |
1200 * @param int $attachment_id Image attachment ID. |
1184 * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of |
1201 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
1185 * width and height values in pixels (in that order). Default 'medium'. |
1202 * width and height values in pixels (in that order). Default 'medium'. |
1186 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
1203 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
1187 * Default null. |
1204 * Default null. |
1188 * @return string|bool A 'srcset' value string or false. |
1205 * @return string|false A 'srcset' value string or false. |
1189 */ |
1206 */ |
1190 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { |
1207 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { |
1191 $image = wp_get_attachment_image_src( $attachment_id, $size ); |
1208 $image = wp_get_attachment_image_src( $attachment_id, $size ); |
1192 |
1209 |
1193 if ( ! $image ) { |
1210 if ( ! $image ) { |
1219 * @type int $1 The height in pixels. |
1236 * @type int $1 The height in pixels. |
1220 * } |
1237 * } |
1221 * @param string $image_src The 'src' of the image. |
1238 * @param string $image_src The 'src' of the image. |
1222 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
1239 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
1223 * @param int $attachment_id Optional. The image attachment ID. Default 0. |
1240 * @param int $attachment_id Optional. The image attachment ID. Default 0. |
1224 * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. |
1241 * @return string|false The 'srcset' attribute value. False on error or when only one source exists. |
1225 */ |
1242 */ |
1226 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) { |
1243 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) { |
1227 /** |
1244 /** |
1228 * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data. |
1245 * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data. |
1229 * |
1246 * |
1420 * @since 4.4.0 |
1437 * @since 4.4.0 |
1421 * |
1438 * |
1422 * @see wp_calculate_image_sizes() |
1439 * @see wp_calculate_image_sizes() |
1423 * |
1440 * |
1424 * @param int $attachment_id Image attachment ID. |
1441 * @param int $attachment_id Image attachment ID. |
1425 * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of width |
1442 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
1426 * and height values in pixels (in that order). Default 'medium'. |
1443 * width and height values in pixels (in that order). Default 'medium'. |
1427 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
1444 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
1428 * Default null. |
1445 * Default null. |
1429 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. |
1446 * @return string|false A valid source size value for use in a 'sizes' attribute or false. |
1430 */ |
1447 */ |
1431 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { |
1448 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { |
1432 $image = wp_get_attachment_image_src( $attachment_id, $size ); |
1449 $image = wp_get_attachment_image_src( $attachment_id, $size ); |
1433 |
1450 |
1434 if ( ! $image ) { |
1451 if ( ! $image ) { |
1451 /** |
1468 /** |
1452 * Creates a 'sizes' attribute value for an image. |
1469 * Creates a 'sizes' attribute value for an image. |
1453 * |
1470 * |
1454 * @since 4.4.0 |
1471 * @since 4.4.0 |
1455 * |
1472 * |
1456 * @param array|string $size Image size to retrieve. Accepts any valid image size, or an array |
1473 * @param string|int[] $size Image size. Accepts any registered image size name, or an array of |
1457 * of width and height values in pixels (in that order). Default 'medium'. |
1474 * width and height values in pixels (in that order). |
1458 * @param string $image_src Optional. The URL to the image file. Default null. |
1475 * @param string $image_src Optional. The URL to the image file. Default null. |
1459 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
1476 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
1460 * Default null. |
1477 * Default null. |
1461 * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` |
1478 * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` |
1462 * is needed when using the image size name as argument for `$size`. Default 0. |
1479 * is needed when using the image size name as argument for `$size`. Default 0. |
1463 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. |
1480 * @return string|false A valid source size value for use in a 'sizes' attribute or false. |
1464 */ |
1481 */ |
1465 function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) { |
1482 function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) { |
1466 $width = 0; |
1483 $width = 0; |
1467 |
1484 |
1468 if ( is_array( $size ) ) { |
1485 if ( is_array( $size ) ) { |
1491 * Filters the output of 'wp_calculate_image_sizes()'. |
1508 * Filters the output of 'wp_calculate_image_sizes()'. |
1492 * |
1509 * |
1493 * @since 4.4.0 |
1510 * @since 4.4.0 |
1494 * |
1511 * |
1495 * @param string $sizes A source size value for use in a 'sizes' attribute. |
1512 * @param string $sizes A source size value for use in a 'sizes' attribute. |
1496 * @param array|string $size Requested size. Image size or array of width and height values |
1513 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
1497 * in pixels (in that order). |
1514 * an array of width and height values in pixels (in that order). |
1498 * @param string|null $image_src The URL to the image file or null. |
1515 * @param string|null $image_src The URL to the image file or null. |
1499 * @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. |
1516 * @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. |
1500 * @param int $attachment_id Image attachment ID of the original image or 0. |
1517 * @param int $attachment_id Image attachment ID of the original image or 0. |
1501 */ |
1518 */ |
1502 return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); |
1519 return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); |
1581 * @param int $attachment_id Optional. The image attachment ID. Default 0. |
1598 * @param int $attachment_id Optional. The image attachment ID. Default 0. |
1582 * @return array|false Array with first element being the width and second element being the height, |
1599 * @return array|false Array with first element being the width and second element being the height, |
1583 * or false if dimensions cannot be determined. |
1600 * or false if dimensions cannot be determined. |
1584 */ |
1601 */ |
1585 function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) { |
1602 function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) { |
1586 if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta, $attachment_id ) ) { |
1603 $dimensions = false; |
1587 return false; |
|
1588 } |
|
1589 |
1604 |
1590 // Is it a full size image? |
1605 // Is it a full size image? |
1591 if ( strpos( $image_src, $image_meta['file'] ) !== false ) { |
1606 if ( |
1592 return array( |
1607 isset( $image_meta['file'] ) && |
|
1608 strpos( $image_src, wp_basename( $image_meta['file'] ) ) !== false |
|
1609 ) { |
|
1610 $dimensions = array( |
1593 (int) $image_meta['width'], |
1611 (int) $image_meta['width'], |
1594 (int) $image_meta['height'], |
1612 (int) $image_meta['height'], |
1595 ); |
1613 ); |
1596 } |
1614 } |
1597 |
1615 |
1598 if ( ! empty( $image_meta['sizes'] ) ) { |
1616 if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) { |
1599 $src_filename = wp_basename( $image_src ); |
1617 $src_filename = wp_basename( $image_src ); |
1600 |
1618 |
1601 foreach ( $image_meta['sizes'] as $image_size_data ) { |
1619 foreach ( $image_meta['sizes'] as $image_size_data ) { |
1602 if ( $src_filename === $image_size_data['file'] ) { |
1620 if ( $src_filename === $image_size_data['file'] ) { |
1603 return array( |
1621 $dimensions = array( |
1604 (int) $image_size_data['width'], |
1622 (int) $image_size_data['width'], |
1605 (int) $image_size_data['height'], |
1623 (int) $image_size_data['height'], |
1606 ); |
1624 ); |
|
1625 |
|
1626 break; |
1607 } |
1627 } |
1608 } |
1628 } |
1609 } |
1629 } |
1610 |
1630 |
1611 return false; |
1631 /** |
|
1632 * Filters the 'wp_image_src_get_dimensions' value. |
|
1633 * |
|
1634 * @since 5.7.0 |
|
1635 * |
|
1636 * @param array|false $dimensions Array with first element being the width |
|
1637 * and second element being the height, or |
|
1638 * false if dimensions could not be determined. |
|
1639 * @param string $image_src The image source file. |
|
1640 * @param array $image_meta The image meta data as returned by |
|
1641 * 'wp_get_attachment_metadata()'. |
|
1642 * @param int $attachment_id The image attachment ID. Default 0. |
|
1643 */ |
|
1644 return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id ); |
1612 } |
1645 } |
1613 |
1646 |
1614 /** |
1647 /** |
1615 * Adds 'srcset' and 'sizes' attributes to an existing 'img' element. |
1648 * Adds 'srcset' and 'sizes' attributes to an existing 'img' element. |
1616 * |
1649 * |
1682 |
1715 |
1683 return $image; |
1716 return $image; |
1684 } |
1717 } |
1685 |
1718 |
1686 /** |
1719 /** |
1687 * Determine whether to add the `loading` attribute to the specified tag in the specified context. |
1720 * Determines whether to add the `loading` attribute to the specified tag in the specified context. |
1688 * |
1721 * |
1689 * @since 5.5.0 |
1722 * @since 5.5.0 |
|
1723 * @since 5.7.0 Now returns `true` by default for `iframe` tags. |
1690 * |
1724 * |
1691 * @param string $tag_name The tag name. |
1725 * @param string $tag_name The tag name. |
1692 * @param string $context Additional context, like the current filter name or the function name from where this was called. |
1726 * @param string $context Additional context, like the current filter name |
|
1727 * or the function name from where this was called. |
1693 * @return bool Whether to add the attribute. |
1728 * @return bool Whether to add the attribute. |
1694 */ |
1729 */ |
1695 function wp_lazy_loading_enabled( $tag_name, $context ) { |
1730 function wp_lazy_loading_enabled( $tag_name, $context ) { |
1696 // By default add to all 'img' tags. |
1731 // By default add to all 'img' and 'iframe' tags. |
1697 // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading |
1732 // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading |
1698 $default = ( 'img' === $tag_name ); |
1733 // See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading |
|
1734 $default = ( 'img' === $tag_name || 'iframe' === $tag_name ); |
1699 |
1735 |
1700 /** |
1736 /** |
1701 * Filters whether to add the `loading` attribute to the specified tag in the specified context. |
1737 * Filters whether to add the `loading` attribute to the specified tag in the specified context. |
1702 * |
1738 * |
1703 * @since 5.5.0 |
1739 * @since 5.5.0 |
1704 * |
1740 * |
1705 * @param bool $default Default value. |
1741 * @param bool $default Default value. |
1706 * @param string $tag_name The tag name. |
1742 * @param string $tag_name The tag name. |
1707 * @param string $context Additional context, like the current filter name or the function name from where this was called. |
1743 * @param string $context Additional context, like the current filter name |
|
1744 * or the function name from where this was called. |
1708 */ |
1745 */ |
1709 return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context ); |
1746 return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context ); |
1710 } |
1747 } |
1711 |
1748 |
1712 /** |
1749 /** |
1713 * Filters specific tags in post content and modifies their markup. |
1750 * Filters specific tags in post content and modifies their markup. |
1714 * |
1751 * |
1715 * Modifies HTML tags in post content to include new browser and HTML technologies |
1752 * Modifies HTML tags in post content to include new browser and HTML technologies |
1716 * that may not have existed at the time of post creation. These modifications currently |
1753 * that may not have existed at the time of post creation. These modifications currently |
1717 * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags. |
1754 * include adding `srcset`, `sizes`, and `loading` attributes to `img` HTML tags, as well |
|
1755 * as adding `loading` attributes to `iframe` HTML tags. |
1718 * Future similar optimizations should be added/expected here. |
1756 * Future similar optimizations should be added/expected here. |
1719 * |
1757 * |
1720 * @since 5.5.0 |
1758 * @since 5.5.0 |
|
1759 * @since 5.7.0 Now supports adding `loading` attributes to `iframe` tags. |
1721 * |
1760 * |
1722 * @see wp_img_tag_add_width_and_height_attr() |
1761 * @see wp_img_tag_add_width_and_height_attr() |
1723 * @see wp_img_tag_add_srcset_and_sizes_attr() |
1762 * @see wp_img_tag_add_srcset_and_sizes_attr() |
1724 * @see wp_img_tag_add_loading_attr() |
1763 * @see wp_img_tag_add_loading_attr() |
|
1764 * @see wp_iframe_tag_add_loading_attr() |
1725 * |
1765 * |
1726 * @param string $content The HTML content to be filtered. |
1766 * @param string $content The HTML content to be filtered. |
1727 * @param string $context Optional. Additional context to pass to the filters. |
1767 * @param string $context Optional. Additional context to pass to the filters. |
1728 * Defaults to `current_filter()` when not set. |
1768 * Defaults to `current_filter()` when not set. |
1729 * @return string Converted content with images modified. |
1769 * @return string Converted content with images modified. |
1731 function wp_filter_content_tags( $content, $context = null ) { |
1771 function wp_filter_content_tags( $content, $context = null ) { |
1732 if ( null === $context ) { |
1772 if ( null === $context ) { |
1733 $context = current_filter(); |
1773 $context = current_filter(); |
1734 } |
1774 } |
1735 |
1775 |
1736 $add_loading_attr = wp_lazy_loading_enabled( 'img', $context ); |
1776 $add_img_loading_attr = wp_lazy_loading_enabled( 'img', $context ); |
1737 |
1777 $add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context ); |
1738 if ( false === strpos( $content, '<img' ) ) { |
1778 |
1739 return $content; |
1779 if ( ! preg_match_all( '/<(img|iframe)\s[^>]+>/', $content, $matches, PREG_SET_ORDER ) ) { |
1740 } |
|
1741 |
|
1742 if ( ! preg_match_all( '/<img\s[^>]+>/', $content, $matches ) ) { |
|
1743 return $content; |
1780 return $content; |
1744 } |
1781 } |
1745 |
1782 |
1746 // List of the unique `img` tags found in $content. |
1783 // List of the unique `img` tags found in $content. |
1747 $images = array(); |
1784 $images = array(); |
1748 |
1785 |
1749 foreach ( $matches[0] as $image ) { |
1786 // List of the unique `iframe` tags found in $content. |
1750 if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { |
1787 $iframes = array(); |
1751 $attachment_id = absint( $class_id[1] ); |
1788 |
1752 |
1789 foreach ( $matches as $match ) { |
1753 if ( $attachment_id ) { |
1790 list( $tag, $tag_name ) = $match; |
1754 // If exactly the same image tag is used more than once, overwrite it. |
1791 |
1755 // All identical tags will be replaced later with 'str_replace()'. |
1792 switch ( $tag_name ) { |
1756 $images[ $image ] = $attachment_id; |
1793 case 'img': |
1757 continue; |
1794 if ( preg_match( '/wp-image-([0-9]+)/i', $tag, $class_id ) ) { |
1758 } |
1795 $attachment_id = absint( $class_id[1] ); |
1759 } |
1796 |
1760 |
1797 if ( $attachment_id ) { |
1761 $images[ $image ] = 0; |
1798 // If exactly the same image tag is used more than once, overwrite it. |
|
1799 // All identical tags will be replaced later with 'str_replace()'. |
|
1800 $images[ $tag ] = $attachment_id; |
|
1801 break; |
|
1802 } |
|
1803 } |
|
1804 $images[ $tag ] = 0; |
|
1805 break; |
|
1806 case 'iframe': |
|
1807 $iframes[ $tag ] = 0; |
|
1808 break; |
|
1809 } |
1762 } |
1810 } |
1763 |
1811 |
1764 // Reduce the array to unique attachment IDs. |
1812 // Reduce the array to unique attachment IDs. |
1765 $attachment_ids = array_unique( array_filter( array_values( $images ) ) ); |
1813 $attachment_ids = array_unique( array_filter( array_values( $images ) ) ); |
1766 |
1814 |
1784 if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) { |
1832 if ( $attachment_id > 0 && false === strpos( $filtered_image, ' srcset=' ) ) { |
1785 $filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id ); |
1833 $filtered_image = wp_img_tag_add_srcset_and_sizes_attr( $filtered_image, $context, $attachment_id ); |
1786 } |
1834 } |
1787 |
1835 |
1788 // Add 'loading' attribute if applicable. |
1836 // Add 'loading' attribute if applicable. |
1789 if ( $add_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { |
1837 if ( $add_img_loading_attr && false === strpos( $filtered_image, ' loading=' ) ) { |
1790 $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); |
1838 $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); |
1791 } |
1839 } |
1792 |
1840 |
1793 if ( $filtered_image !== $image ) { |
1841 if ( $filtered_image !== $image ) { |
1794 $content = str_replace( $image, $filtered_image, $content ); |
1842 $content = str_replace( $image, $filtered_image, $content ); |
|
1843 } |
|
1844 } |
|
1845 |
|
1846 foreach ( $iframes as $iframe => $attachment_id ) { |
|
1847 $filtered_iframe = $iframe; |
|
1848 |
|
1849 // Add 'loading' attribute if applicable. |
|
1850 if ( $add_iframe_loading_attr && false === strpos( $filtered_iframe, ' loading=' ) ) { |
|
1851 $filtered_iframe = wp_iframe_tag_add_loading_attr( $filtered_iframe, $context ); |
|
1852 } |
|
1853 |
|
1854 if ( $filtered_iframe !== $iframe ) { |
|
1855 $content = str_replace( $iframe, $filtered_iframe, $content ); |
1795 } |
1856 } |
1796 } |
1857 } |
1797 |
1858 |
1798 return $content; |
1859 return $content; |
1799 } |
1860 } |
1806 * @param string $image The HTML `img` tag where the attribute should be added. |
1867 * @param string $image The HTML `img` tag where the attribute should be added. |
1807 * @param string $context Additional context to pass to the filters. |
1868 * @param string $context Additional context to pass to the filters. |
1808 * @return string Converted `img` tag with `loading` attribute added. |
1869 * @return string Converted `img` tag with `loading` attribute added. |
1809 */ |
1870 */ |
1810 function wp_img_tag_add_loading_attr( $image, $context ) { |
1871 function wp_img_tag_add_loading_attr( $image, $context ) { |
1811 /** |
1872 // Images should have source and dimension attributes for the `loading` attribute to be added. |
1812 * Filters the `loading` attribute value. Default `lazy`. |
1873 if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) { |
|
1874 return $image; |
|
1875 } |
|
1876 |
|
1877 /** |
|
1878 * Filters the `loading` attribute value to add to an image. Default `lazy`. |
1813 * |
1879 * |
1814 * Returning `false` or an empty string will not add the attribute. |
1880 * Returning `false` or an empty string will not add the attribute. |
1815 * Returning `true` will add the default value. |
1881 * Returning `true` will add the default value. |
1816 * |
1882 * |
1817 * @since 5.5.0 |
1883 * @since 5.5.0 |
1818 * |
1884 * |
1819 * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in |
1885 * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in |
1820 * the attribute being omitted for the image. Default is `lazy`. |
1886 * the attribute being omitted for the image. Default 'lazy'. |
1821 * @param string $image The HTML `img` tag to be filtered. |
1887 * @param string $image The HTML `img` tag to be filtered. |
1822 * @param string $context Additional context about how the function was called or where the img tag is. |
1888 * @param string $context Additional context about how the function was called or where the img tag is. |
1823 */ |
1889 */ |
1824 $value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context ); |
1890 $value = apply_filters( 'wp_img_tag_add_loading_attr', 'lazy', $image, $context ); |
1825 |
1891 |
1826 if ( $value ) { |
1892 if ( $value ) { |
1827 if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { |
1893 if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { |
1828 $value = 'lazy'; |
1894 $value = 'lazy'; |
1829 } |
|
1830 |
|
1831 // Images should have source and dimension attributes for the `loading` attribute to be added. |
|
1832 if ( false === strpos( $image, ' src="' ) || false === strpos( $image, ' width="' ) || false === strpos( $image, ' height="' ) ) { |
|
1833 return $image; |
|
1834 } |
1895 } |
1835 |
1896 |
1836 return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image ); |
1897 return str_replace( '<img', '<img loading="' . esc_attr( $value ) . '"', $image ); |
1837 } |
1898 } |
1838 |
1899 |
1914 $image_meta = wp_get_attachment_metadata( $attachment_id ); |
1975 $image_meta = wp_get_attachment_metadata( $attachment_id ); |
1915 return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ); |
1976 return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ); |
1916 } |
1977 } |
1917 |
1978 |
1918 return $image; |
1979 return $image; |
|
1980 } |
|
1981 |
|
1982 /** |
|
1983 * Adds `loading` attribute to an `iframe` HTML tag. |
|
1984 * |
|
1985 * @since 5.7.0 |
|
1986 * |
|
1987 * @param string $iframe The HTML `iframe` tag where the attribute should be added. |
|
1988 * @param string $context Additional context to pass to the filters. |
|
1989 * @return string Converted `iframe` tag with `loading` attribute added. |
|
1990 */ |
|
1991 function wp_iframe_tag_add_loading_attr( $iframe, $context ) { |
|
1992 // Iframes with fallback content (see `wp_filter_oembed_result()`) should not be lazy-loaded because they are |
|
1993 // visually hidden initially. |
|
1994 if ( false !== strpos( $iframe, ' data-secret="' ) ) { |
|
1995 return $iframe; |
|
1996 } |
|
1997 |
|
1998 // Iframes should have source and dimension attributes for the `loading` attribute to be added. |
|
1999 if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) { |
|
2000 return $iframe; |
|
2001 } |
|
2002 |
|
2003 /** |
|
2004 * Filters the `loading` attribute value to add to an iframe. Default `lazy`. |
|
2005 * |
|
2006 * Returning `false` or an empty string will not add the attribute. |
|
2007 * Returning `true` will add the default value. |
|
2008 * |
|
2009 * @since 5.7.0 |
|
2010 * |
|
2011 * @param string|bool $value The `loading` attribute value. Returning a falsey value will result in |
|
2012 * the attribute being omitted for the iframe. Default 'lazy'. |
|
2013 * @param string $iframe The HTML `iframe` tag to be filtered. |
|
2014 * @param string $context Additional context about how the function was called or where the iframe tag is. |
|
2015 */ |
|
2016 $value = apply_filters( 'wp_iframe_tag_add_loading_attr', 'lazy', $iframe, $context ); |
|
2017 |
|
2018 if ( $value ) { |
|
2019 if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) { |
|
2020 $value = 'lazy'; |
|
2021 } |
|
2022 |
|
2023 return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe ); |
|
2024 } |
|
2025 |
|
2026 return $iframe; |
1919 } |
2027 } |
1920 |
2028 |
1921 /** |
2029 /** |
1922 * Adds a 'wp-post-image' class to post thumbnails. Internal use only. |
2030 * Adds a 'wp-post-image' class to post thumbnails. Internal use only. |
1923 * |
2031 * |
2145 * @type string $icontag HTML tag to use for each image's icon. |
2253 * @type string $icontag HTML tag to use for each image's icon. |
2146 * Default 'dt', or 'div' when the theme registers HTML5 gallery support. |
2254 * Default 'dt', or 'div' when the theme registers HTML5 gallery support. |
2147 * @type string $captiontag HTML tag to use for each image's caption. |
2255 * @type string $captiontag HTML tag to use for each image's caption. |
2148 * Default 'dd', or 'figcaption' when the theme registers HTML5 gallery support. |
2256 * Default 'dd', or 'figcaption' when the theme registers HTML5 gallery support. |
2149 * @type int $columns Number of columns of images to display. Default 3. |
2257 * @type int $columns Number of columns of images to display. Default 3. |
2150 * @type string|array $size Size of the images to display. Accepts any valid image size, or an array of width |
2258 * @type string|int[] $size Size of the images to display. Accepts any registered image size name, or an array |
2151 * and height values in pixels (in that order). Default 'thumbnail'. |
2259 * of width and height values in pixels (in that order). Default 'thumbnail'. |
2152 * @type string $ids A comma-separated list of IDs of attachments to display. Default empty. |
2260 * @type string $ids A comma-separated list of IDs of attachments to display. Default empty. |
2153 * @type string $include A comma-separated list of IDs of attachments to include. Default empty. |
2261 * @type string $include A comma-separated list of IDs of attachments to include. Default empty. |
2154 * @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty. |
2262 * @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty. |
2155 * @type string $link What to link each image to. Default empty (links to the attachment page). |
2263 * @type string $link What to link each image to. Default empty (links to the attachment page). |
2156 * Accepts 'file', 'none'. |
2264 * Accepts 'file', 'none'. |
2688 do_action( 'wp_playlist_scripts', $atts['type'], $atts['style'] ); |
2796 do_action( 'wp_playlist_scripts', $atts['type'], $atts['style'] ); |
2689 } |
2797 } |
2690 ?> |
2798 ?> |
2691 <div class="wp-playlist wp-<?php echo $safe_type; ?>-playlist wp-playlist-<?php echo $safe_style; ?>"> |
2799 <div class="wp-playlist wp-<?php echo $safe_type; ?>-playlist wp-playlist-<?php echo $safe_style; ?>"> |
2692 <?php if ( 'audio' === $atts['type'] ) : ?> |
2800 <?php if ( 'audio' === $atts['type'] ) : ?> |
2693 <div class="wp-playlist-current-item"></div> |
2801 <div class="wp-playlist-current-item"></div> |
2694 <?php endif ?> |
2802 <?php endif; ?> |
2695 <<?php echo $safe_type; ?> controls="controls" preload="none" width=" |
2803 <<?php echo $safe_type; ?> controls="controls" preload="none" width="<?php echo (int) $theme_width; ?>" |
2696 <?php |
2804 <?php |
2697 echo (int) $theme_width; |
2805 if ( 'video' === $safe_type ) { |
2698 ?> |
2806 echo ' height="', (int) $theme_height, '"'; |
2699 " |
2807 } |
2700 <?php |
2808 ?> |
2701 if ( 'video' === $safe_type ) : |
|
2702 echo ' height="', (int) $theme_height, '"'; |
|
2703 endif; |
|
2704 ?> |
|
2705 ></<?php echo $safe_type; ?>> |
2809 ></<?php echo $safe_type; ?>> |
2706 <div class="wp-playlist-next"></div> |
2810 <div class="wp-playlist-next"></div> |
2707 <div class="wp-playlist-prev"></div> |
2811 <div class="wp-playlist-prev"></div> |
2708 <noscript> |
2812 <noscript> |
2709 <ol> |
2813 <ol> |
2710 <?php |
2814 <?php |
2711 foreach ( $attachments as $att_id => $attachment ) { |
2815 foreach ( $attachments as $att_id => $attachment ) { |
2712 printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) ); |
2816 printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) ); |
2713 } |
2817 } |
2714 ?> |
2818 ?> |
2715 </ol> |
2819 </ol> |
2716 </noscript> |
2820 </noscript> |
2717 <script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ); ?></script> |
2821 <script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ); ?></script> |
2718 </div> |
2822 </div> |
2719 <?php |
2823 <?php |
3210 foreach ( $html_atts as $k => $v ) { |
3314 foreach ( $html_atts as $k => $v ) { |
3211 $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; |
3315 $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; |
3212 } |
3316 } |
3213 |
3317 |
3214 $html = ''; |
3318 $html = ''; |
|
3319 |
3215 if ( 'mediaelement' === $library && 1 === $instance ) { |
3320 if ( 'mediaelement' === $library && 1 === $instance ) { |
3216 $html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n"; |
3321 $html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n"; |
3217 } |
3322 } |
3218 $html .= sprintf( '<video %s controls="controls">', join( ' ', $attr_strings ) ); |
3323 |
|
3324 $html .= sprintf( '<video %s controls="controls">', implode( ' ', $attr_strings ) ); |
3219 |
3325 |
3220 $fileurl = ''; |
3326 $fileurl = ''; |
3221 $source = '<source type="%s" src="%s" />'; |
3327 $source = '<source type="%s" src="%s" />'; |
|
3328 |
3222 foreach ( $default_types as $fallback ) { |
3329 foreach ( $default_types as $fallback ) { |
3223 if ( ! empty( $atts[ $fallback ] ) ) { |
3330 if ( ! empty( $atts[ $fallback ] ) ) { |
3224 if ( empty( $fileurl ) ) { |
3331 if ( empty( $fileurl ) ) { |
3225 $fileurl = $atts[ $fallback ]; |
3332 $fileurl = $atts[ $fallback ]; |
3226 } |
3333 } |
3268 return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library ); |
3375 return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library ); |
3269 } |
3376 } |
3270 add_shortcode( 'video', 'wp_video_shortcode' ); |
3377 add_shortcode( 'video', 'wp_video_shortcode' ); |
3271 |
3378 |
3272 /** |
3379 /** |
|
3380 * Gets the previous image link that has the same post parent. |
|
3381 * |
|
3382 * @since 5.8.0 |
|
3383 * |
|
3384 * @see get_adjacent_image_link() |
|
3385 * |
|
3386 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3387 * of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3388 * @param string|false $text Optional. Link text. Default false. |
|
3389 * @return string Markup for previous image link. |
|
3390 */ |
|
3391 function get_previous_image_link( $size = 'thumbnail', $text = false ) { |
|
3392 return get_adjacent_image_link( true, $size, $text ); |
|
3393 } |
|
3394 |
|
3395 /** |
3273 * Displays previous image link that has the same post parent. |
3396 * Displays previous image link that has the same post parent. |
3274 * |
3397 * |
3275 * @since 2.5.0 |
3398 * @since 2.5.0 |
3276 * |
3399 * |
3277 * @see adjacent_image_link() |
3400 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3278 * |
3401 * of width and height values in pixels (in that order). Default 'thumbnail'. |
3279 * @param string|array $size Optional. Image size. Accepts any valid image size, an array of width and |
3402 * @param string|false $text Optional. Link text. Default false. |
3280 * height values in pixels (in that order), 0, or 'none'. 0 or 'none' will |
|
3281 * default to 'post_title' or `$text`. Default 'thumbnail'. |
|
3282 * @param string $text Optional. Link text. Default false. |
|
3283 */ |
3403 */ |
3284 function previous_image_link( $size = 'thumbnail', $text = false ) { |
3404 function previous_image_link( $size = 'thumbnail', $text = false ) { |
3285 adjacent_image_link( true, $size, $text ); |
3405 echo get_previous_image_link( $size, $text ); |
|
3406 } |
|
3407 |
|
3408 /** |
|
3409 * Gets the next image link that has the same post parent. |
|
3410 * |
|
3411 * @since 5.8.0 |
|
3412 * |
|
3413 * @see get_adjacent_image_link() |
|
3414 * |
|
3415 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3416 * of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3417 * @param string|false $text Optional. Link text. Default false. |
|
3418 * @return string Markup for next image link. |
|
3419 */ |
|
3420 function get_next_image_link( $size = 'thumbnail', $text = false ) { |
|
3421 return get_adjacent_image_link( false, $size, $text ); |
3286 } |
3422 } |
3287 |
3423 |
3288 /** |
3424 /** |
3289 * Displays next image link that has the same post parent. |
3425 * Displays next image link that has the same post parent. |
3290 * |
3426 * |
3291 * @since 2.5.0 |
3427 * @since 2.5.0 |
3292 * |
3428 * |
3293 * @see adjacent_image_link() |
3429 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3294 * |
3430 * of width and height values in pixels (in that order). Default 'thumbnail'. |
3295 * @param string|array $size Optional. Image size. Accepts any valid image size, an array of width and |
3431 * @param string|false $text Optional. Link text. Default false. |
3296 * height values in pixels (in that order), 0, or 'none'. 0 or 'none' will |
|
3297 * default to 'post_title' or `$text`. Default 'thumbnail'. |
|
3298 * @param string $text Optional. Link text. Default false. |
|
3299 */ |
3432 */ |
3300 function next_image_link( $size = 'thumbnail', $text = false ) { |
3433 function next_image_link( $size = 'thumbnail', $text = false ) { |
3301 adjacent_image_link( false, $size, $text ); |
3434 echo get_next_image_link( $size, $text ); |
3302 } |
3435 } |
3303 |
3436 |
3304 /** |
3437 /** |
3305 * Displays next or previous image link that has the same post parent. |
3438 * Gets the next or previous image link that has the same post parent. |
3306 * |
3439 * |
3307 * Retrieves the current attachment object from the $post global. |
3440 * Retrieves the current attachment object from the $post global. |
3308 * |
3441 * |
3309 * @since 2.5.0 |
3442 * @since 5.8.0 |
3310 * |
3443 * |
3311 * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. |
3444 * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. |
3312 * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width and height |
3445 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
3313 * values in pixels (in that order). Default 'thumbnail'. |
3446 * of width and height values in pixels (in that order). Default 'thumbnail'. |
3314 * @param bool $text Optional. Link text. Default false. |
3447 * @param bool $text Optional. Link text. Default false. |
3315 */ |
3448 * @return string Markup for image link. |
3316 function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { |
3449 */ |
|
3450 function get_adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { |
3317 $post = get_post(); |
3451 $post = get_post(); |
3318 $attachments = array_values( |
3452 $attachments = array_values( |
3319 get_children( |
3453 get_children( |
3320 array( |
3454 array( |
3321 'post_parent' => $post->post_parent, |
3455 'post_parent' => $post->post_parent, |
3352 * Filters the adjacent image link. |
3487 * Filters the adjacent image link. |
3353 * |
3488 * |
3354 * The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency, |
3489 * The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency, |
3355 * either 'next', or 'previous'. |
3490 * either 'next', or 'previous'. |
3356 * |
3491 * |
|
3492 * Possible hook names include: |
|
3493 * |
|
3494 * - `next_image_link` |
|
3495 * - `previous_image_link` |
|
3496 * |
3357 * @since 3.5.0 |
3497 * @since 3.5.0 |
3358 * |
3498 * |
3359 * @param string $output Adjacent image HTML markup. |
3499 * @param string $output Adjacent image HTML markup. |
3360 * @param int $attachment_id Attachment ID |
3500 * @param int $attachment_id Attachment ID |
3361 * @param string $size Image size. |
3501 * @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
3502 * an array of width and height values in pixels (in that order). |
3362 * @param string $text Link text. |
3503 * @param string $text Link text. |
3363 */ |
3504 */ |
3364 echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); |
3505 return apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); |
|
3506 } |
|
3507 |
|
3508 /** |
|
3509 * Displays next or previous image link that has the same post parent. |
|
3510 * |
|
3511 * Retrieves the current attachment object from the $post global. |
|
3512 * |
|
3513 * @since 2.5.0 |
|
3514 * |
|
3515 * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. |
|
3516 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
3517 * of width and height values in pixels (in that order). Default 'thumbnail'. |
|
3518 * @param bool $text Optional. Link text. Default false. |
|
3519 */ |
|
3520 function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { |
|
3521 echo get_adjacent_image_link( $prev, $size, $text ); |
3365 } |
3522 } |
3366 |
3523 |
3367 /** |
3524 /** |
3368 * Retrieves taxonomies attached to given the attachment. |
3525 * Retrieves taxonomies attached to given the attachment. |
3369 * |
3526 * |
3456 |
3613 |
3457 return $taxonomies; |
3614 return $taxonomies; |
3458 } |
3615 } |
3459 |
3616 |
3460 /** |
3617 /** |
|
3618 * Determines whether the value is an acceptable type for GD image functions. |
|
3619 * |
|
3620 * In PHP 8.0, the GD extension uses GdImage objects for its data structures. |
|
3621 * This function checks if the passed value is either a resource of type `gd` |
|
3622 * or a GdImage object instance. Any other type will return false. |
|
3623 * |
|
3624 * @since 5.6.0 |
|
3625 * |
|
3626 * @param resource|GdImage|false $image A value to check the type for. |
|
3627 * @return bool True if $image is either a GD image resource or GdImage instance, |
|
3628 * false otherwise. |
|
3629 */ |
|
3630 function is_gd_image( $image ) { |
|
3631 if ( is_resource( $image ) && 'gd' === get_resource_type( $image ) |
|
3632 || is_object( $image ) && $image instanceof GdImage |
|
3633 ) { |
|
3634 return true; |
|
3635 } |
|
3636 |
|
3637 return false; |
|
3638 } |
|
3639 |
|
3640 /** |
3461 * Create new GD image resource with transparency support |
3641 * Create new GD image resource with transparency support |
3462 * |
3642 * |
3463 * @todo Deprecate if possible. |
3643 * @todo Deprecate if possible. |
3464 * |
3644 * |
3465 * @since 2.9.0 |
3645 * @since 2.9.0 |
3466 * |
3646 * |
3467 * @param int $width Image width in pixels. |
3647 * @param int $width Image width in pixels. |
3468 * @param int $height Image height in pixels.. |
3648 * @param int $height Image height in pixels. |
3469 * @return resource The GD image resource. |
3649 * @return resource|GdImage|false The GD image resource or GdImage instance on success. |
|
3650 * False on failure. |
3470 */ |
3651 */ |
3471 function wp_imagecreatetruecolor( $width, $height ) { |
3652 function wp_imagecreatetruecolor( $width, $height ) { |
3472 $img = imagecreatetruecolor( $width, $height ); |
3653 $img = imagecreatetruecolor( $width, $height ); |
3473 if ( is_resource( $img ) && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
3654 |
|
3655 if ( is_gd_image( $img ) |
|
3656 && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) |
|
3657 ) { |
3474 imagealphablending( $img, false ); |
3658 imagealphablending( $img, false ); |
3475 imagesavealpha( $img, true ); |
3659 imagesavealpha( $img, true ); |
3476 } |
3660 } |
|
3661 |
3477 return $img; |
3662 return $img; |
3478 } |
3663 } |
3479 |
3664 |
3480 /** |
3665 /** |
3481 * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. |
3666 * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. |
3722 * to be JSON-encoded and fit into an Attachment model. |
3912 * to be JSON-encoded and fit into an Attachment model. |
3723 * |
3913 * |
3724 * @since 3.5.0 |
3914 * @since 3.5.0 |
3725 * |
3915 * |
3726 * @param int|WP_Post $attachment Attachment ID or object. |
3916 * @param int|WP_Post $attachment Attachment ID or object. |
3727 * @return array|void Array of attachment details. |
3917 * @return array|void { |
|
3918 * Array of attachment details, or void if the parameter does not correspond to an attachment. |
|
3919 * |
|
3920 * @type string $alt Alt text of the attachment. |
|
3921 * @type string $author ID of the attachment author, as a string. |
|
3922 * @type string $authorName Name of the attachment author. |
|
3923 * @type string $caption Caption for the attachment. |
|
3924 * @type array $compat Containing item and meta. |
|
3925 * @type string $context Context, whether it's used as the site icon for example. |
|
3926 * @type int $date Uploaded date, timestamp in milliseconds. |
|
3927 * @type string $dateFormatted Formatted date (e.g. June 29, 2018). |
|
3928 * @type string $description Description of the attachment. |
|
3929 * @type string $editLink URL to the edit page for the attachment. |
|
3930 * @type string $filename File name of the attachment. |
|
3931 * @type string $filesizeHumanReadable Filesize of the attachment in human readable format (e.g. 1 MB). |
|
3932 * @type int $filesizeInBytes Filesize of the attachment in bytes. |
|
3933 * @type int $height If the attachment is an image, represents the height of the image in pixels. |
|
3934 * @type string $icon Icon URL of the attachment (e.g. /wp-includes/images/media/archive.png). |
|
3935 * @type int $id ID of the attachment. |
|
3936 * @type string $link URL to the attachment. |
|
3937 * @type int $menuOrder Menu order of the attachment post. |
|
3938 * @type array $meta Meta data for the attachment. |
|
3939 * @type string $mime Mime type of the attachment (e.g. image/jpeg or application/zip). |
|
3940 * @type int $modified Last modified, timestamp in milliseconds. |
|
3941 * @type string $name Name, same as title of the attachment. |
|
3942 * @type array $nonces Nonces for update, delete and edit. |
|
3943 * @type string $orientation If the attachment is an image, represents the image orientation |
|
3944 * (landscape or portrait). |
|
3945 * @type array $sizes If the attachment is an image, contains an array of arrays |
|
3946 * for the images sizes: thumbnail, medium, large, and full. |
|
3947 * @type string $status Post status of the attachment (usually 'inherit'). |
|
3948 * @type string $subtype Mime subtype of the attachment (usually the last part, e.g. jpeg or zip). |
|
3949 * @type string $title Title of the attachment (usually slugified file name without the extension). |
|
3950 * @type string $type Type of the attachment (usually first part of the mime type, e.g. image). |
|
3951 * @type int $uploadedTo Parent post to which the attachment was uploaded. |
|
3952 * @type string $uploadedToLink URL to the edit page of the parent post of the attachment. |
|
3953 * @type string $uploadedToTitle Post title of the parent of the attachment. |
|
3954 * @type string $url Direct URL to the attachment file (from wp-content). |
|
3955 * @type int $width If the attachment is an image, represents the width of the image in pixels. |
|
3956 * } |
|
3957 * |
3728 */ |
3958 */ |
3729 function wp_prepare_attachment_for_js( $attachment ) { |
3959 function wp_prepare_attachment_for_js( $attachment ) { |
3730 $attachment = get_post( $attachment ); |
3960 $attachment = get_post( $attachment ); |
3731 |
3961 |
3732 if ( ! $attachment ) { |
3962 if ( ! $attachment ) { |
3776 'editLink' => false, |
4006 'editLink' => false, |
3777 'meta' => false, |
4007 'meta' => false, |
3778 ); |
4008 ); |
3779 |
4009 |
3780 $author = new WP_User( $attachment->post_author ); |
4010 $author = new WP_User( $attachment->post_author ); |
|
4011 |
3781 if ( $author->exists() ) { |
4012 if ( $author->exists() ) { |
3782 $response['authorName'] = html_entity_decode( $author->display_name, ENT_QUOTES, get_bloginfo( 'charset' ) ); |
4013 $author_name = $author->display_name ? $author->display_name : $author->nickname; |
|
4014 $response['authorName'] = html_entity_decode( $author_name, ENT_QUOTES, get_bloginfo( 'charset' ) ); |
|
4015 $response['authorLink'] = get_edit_user_link( $author->ID ); |
3783 } else { |
4016 } else { |
3784 $response['authorName'] = __( '(no author)' ); |
4017 $response['authorName'] = __( '(no author)' ); |
3785 } |
4018 } |
3786 |
4019 |
3787 if ( $attachment->post_parent ) { |
4020 if ( $attachment->post_parent ) { |
3788 $post_parent = get_post( $attachment->post_parent ); |
4021 $post_parent = get_post( $attachment->post_parent ); |
3789 } else { |
4022 if ( $post_parent ) { |
3790 $post_parent = false; |
|
3791 } |
|
3792 |
|
3793 if ( $post_parent ) { |
|
3794 $parent_type = get_post_type_object( $post_parent->post_type ); |
|
3795 |
|
3796 if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $attachment->post_parent ) ) { |
|
3797 $response['uploadedToLink'] = get_edit_post_link( $attachment->post_parent, 'raw' ); |
|
3798 } |
|
3799 |
|
3800 if ( $parent_type && current_user_can( 'read_post', $attachment->post_parent ) ) { |
|
3801 $response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); |
4023 $response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); |
|
4024 $response['uploadedToLink'] = get_edit_post_link( $attachment->post_parent, 'raw' ); |
3802 } |
4025 } |
3803 } |
4026 } |
3804 |
4027 |
3805 $attached_file = get_attached_file( $attachment->ID ); |
4028 $attached_file = get_attached_file( $attachment->ID ); |
3806 |
4029 |
3952 |
4175 |
3953 if ( function_exists( 'get_compat_media_markup' ) ) { |
4176 if ( function_exists( 'get_compat_media_markup' ) ) { |
3954 $response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) ); |
4177 $response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) ); |
3955 } |
4178 } |
3956 |
4179 |
|
4180 if ( function_exists( 'get_media_states' ) ) { |
|
4181 $media_states = get_media_states( $attachment ); |
|
4182 if ( ! empty( $media_states ) ) { |
|
4183 $response['mediaStates'] = implode( ', ', $media_states ); |
|
4184 } |
|
4185 } |
|
4186 |
3957 /** |
4187 /** |
3958 * Filters the attachment data prepared for JavaScript. |
4188 * Filters the attachment data prepared for JavaScript. |
3959 * |
4189 * |
3960 * @since 3.5.0 |
4190 * @since 3.5.0 |
3961 * |
4191 * |
3962 * @param array $response Array of prepared attachment data. |
4192 * @param array $response Array of prepared attachment data. @see wp_prepare_attachment_for_js(). |
3963 * @param WP_Post $attachment Attachment object. |
4193 * @param WP_Post $attachment Attachment object. |
3964 * @param array|false $meta Array of attachment meta data, or false if there is none. |
4194 * @param array|false $meta Array of attachment meta data, or false if there is none. |
3965 */ |
4195 */ |
3966 return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); |
4196 return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); |
3967 } |
4197 } |
4124 $wp_locale->get_month( $month_year->month ), |
4354 $wp_locale->get_month( $month_year->month ), |
4125 $month_year->year |
4355 $month_year->year |
4126 ); |
4356 ); |
4127 } |
4357 } |
4128 |
4358 |
|
4359 /** |
|
4360 * Filters whether the Media Library grid has infinite scrolling. Default `false`. |
|
4361 * |
|
4362 * @since 5.8.0 |
|
4363 * |
|
4364 * @param bool $infinite Whether the Media Library grid has infinite scrolling. |
|
4365 */ |
|
4366 $infinite_scrolling = apply_filters( 'media_library_infinite_scrolling', false ); |
|
4367 |
4129 $settings = array( |
4368 $settings = array( |
4130 'tabs' => $tabs, |
4369 'tabs' => $tabs, |
4131 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ), |
4370 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url( 'media-upload.php' ) ), |
4132 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ), |
4371 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ), |
4133 /** This filter is documented in wp-admin/includes/media.php */ |
4372 /** This filter is documented in wp-admin/includes/media.php */ |
4134 'captions' => ! apply_filters( 'disable_captions', '' ), |
4373 'captions' => ! apply_filters( 'disable_captions', '' ), |
4135 'nonce' => array( |
4374 'nonce' => array( |
4136 'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ), |
4375 'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ), |
4137 ), |
4376 ), |
4138 'post' => array( |
4377 'post' => array( |
4139 'id' => 0, |
4378 'id' => 0, |
4140 ), |
4379 ), |
4141 'defaultProps' => $props, |
4380 'defaultProps' => $props, |
4142 'attachmentCounts' => array( |
4381 'attachmentCounts' => array( |
4143 'audio' => ( $show_audio_playlist ) ? 1 : 0, |
4382 'audio' => ( $show_audio_playlist ) ? 1 : 0, |
4144 'video' => ( $show_video_playlist ) ? 1 : 0, |
4383 'video' => ( $show_video_playlist ) ? 1 : 0, |
4145 ), |
4384 ), |
4146 'oEmbedProxyUrl' => rest_url( 'oembed/1.0/proxy' ), |
4385 'oEmbedProxyUrl' => rest_url( 'oembed/1.0/proxy' ), |
4147 'embedExts' => $exts, |
4386 'embedExts' => $exts, |
4148 'embedMimes' => $ext_mimes, |
4387 'embedMimes' => $ext_mimes, |
4149 'contentWidth' => $content_width, |
4388 'contentWidth' => $content_width, |
4150 'months' => $months, |
4389 'months' => $months, |
4151 'mediaTrash' => MEDIA_TRASH ? 1 : 0, |
4390 'mediaTrash' => MEDIA_TRASH ? 1 : 0, |
|
4391 'infiniteScrolling' => ( $infinite_scrolling ) ? 1 : 0, |
4152 ); |
4392 ); |
4153 |
4393 |
4154 $post = null; |
4394 $post = null; |
4155 if ( isset( $args['post'] ) ) { |
4395 if ( isset( $args['post'] ) ) { |
4156 $post = get_post( $args['post'] ); |
4396 $post = get_post( $args['post'] ); |
4208 'mediaLibraryTitle' => __( 'Media Library' ), |
4448 'mediaLibraryTitle' => __( 'Media Library' ), |
4209 'insertMediaTitle' => __( 'Add media' ), |
4449 'insertMediaTitle' => __( 'Add media' ), |
4210 'createNewGallery' => __( 'Create a new gallery' ), |
4450 'createNewGallery' => __( 'Create a new gallery' ), |
4211 'createNewPlaylist' => __( 'Create a new playlist' ), |
4451 'createNewPlaylist' => __( 'Create a new playlist' ), |
4212 'createNewVideoPlaylist' => __( 'Create a new video playlist' ), |
4452 'createNewVideoPlaylist' => __( 'Create a new video playlist' ), |
4213 'returnToLibrary' => __( '← Return to library' ), |
4453 'returnToLibrary' => __( '← Go to library' ), |
4214 'allMediaItems' => __( 'All media items' ), |
4454 'allMediaItems' => __( 'All media items' ), |
4215 'allDates' => __( 'All dates' ), |
4455 'allDates' => __( 'All dates' ), |
4216 'noItemsFound' => __( 'No items found.' ), |
4456 'noItemsFound' => __( 'No items found.' ), |
4217 'insertIntoPost' => $post_type_object->labels->insert_into_item, |
4457 'insertIntoPost' => $post_type_object->labels->insert_into_item, |
4218 'unattached' => __( 'Unattached' ), |
4458 'unattached' => __( 'Unattached' ), |
4230 'filterByDate' => __( 'Filter by date' ), |
4470 'filterByDate' => __( 'Filter by date' ), |
4231 'filterByType' => __( 'Filter by type' ), |
4471 'filterByType' => __( 'Filter by type' ), |
4232 'searchLabel' => __( 'Search' ), |
4472 'searchLabel' => __( 'Search' ), |
4233 'searchMediaLabel' => __( 'Search media' ), // Backward compatibility pre-5.3. |
4473 'searchMediaLabel' => __( 'Search media' ), // Backward compatibility pre-5.3. |
4234 'searchMediaPlaceholder' => __( 'Search media items...' ), // Placeholder (no ellipsis), backward compatibility pre-5.3. |
4474 'searchMediaPlaceholder' => __( 'Search media items...' ), // Placeholder (no ellipsis), backward compatibility pre-5.3. |
|
4475 /* translators: %d: Number of attachments found in a search. */ |
4235 'mediaFound' => __( 'Number of media items found: %d' ), |
4476 'mediaFound' => __( 'Number of media items found: %d' ), |
4236 'mediaFoundHasMoreResults' => __( 'Number of media items displayed: %d. Scroll the page for more results.' ), |
|
4237 'noMedia' => __( 'No media items found.' ), |
4477 'noMedia' => __( 'No media items found.' ), |
4238 'noMediaTryNewSearch' => __( 'No media items found. Try a different search.' ), |
4478 'noMediaTryNewSearch' => __( 'No media items found. Try a different search.' ), |
4239 |
4479 |
4240 // Library Details. |
4480 // Library Details. |
4241 'attachmentDetails' => __( 'Attachment details' ), |
4481 'attachmentDetails' => __( 'Attachment details' ), |
4793 */ |
5033 */ |
4794 function wp_show_heic_upload_error( $plupload_settings ) { |
5034 function wp_show_heic_upload_error( $plupload_settings ) { |
4795 $plupload_settings['heic_upload_error'] = true; |
5035 $plupload_settings['heic_upload_error'] = true; |
4796 return $plupload_settings; |
5036 return $plupload_settings; |
4797 } |
5037 } |
|
5038 |
|
5039 /** |
|
5040 * Allows PHP's getimagesize() to be debuggable when necessary. |
|
5041 * |
|
5042 * @since 5.7.0 |
|
5043 * @since 5.8.0 Added support for WebP images. |
|
5044 * |
|
5045 * @param string $filename The file path. |
|
5046 * @param array $image_info Optional. Extended image information (passed by reference). |
|
5047 * @return array|false Array of image information or false on failure. |
|
5048 */ |
|
5049 function wp_getimagesize( $filename, array &$image_info = null ) { |
|
5050 // Don't silence errors when in debug mode, unless running unit tests. |
|
5051 if ( defined( 'WP_DEBUG' ) && WP_DEBUG |
|
5052 && ! defined( 'WP_RUN_CORE_TESTS' ) |
|
5053 ) { |
|
5054 if ( 2 === func_num_args() ) { |
|
5055 $info = getimagesize( $filename, $image_info ); |
|
5056 } else { |
|
5057 $info = getimagesize( $filename ); |
|
5058 } |
|
5059 } else { |
|
5060 /* |
|
5061 * Silencing notice and warning is intentional. |
|
5062 * |
|
5063 * getimagesize() has a tendency to generate errors, such as |
|
5064 * "corrupt JPEG data: 7191 extraneous bytes before marker", |
|
5065 * even when it's able to provide image size information. |
|
5066 * |
|
5067 * See https://core.trac.wordpress.org/ticket/42480 |
|
5068 */ |
|
5069 if ( 2 === func_num_args() ) { |
|
5070 // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
5071 $info = @getimagesize( $filename, $image_info ); |
|
5072 } else { |
|
5073 // phpcs:ignore WordPress.PHP.NoSilencedErrors |
|
5074 $info = @getimagesize( $filename ); |
|
5075 } |
|
5076 } |
|
5077 |
|
5078 if ( false !== $info ) { |
|
5079 return $info; |
|
5080 } |
|
5081 |
|
5082 // For PHP versions that don't support WebP images, |
|
5083 // extract the image size info from the file headers. |
|
5084 if ( 'image/webp' === wp_get_image_mime( $filename ) ) { |
|
5085 $webp_info = wp_get_webp_info( $filename ); |
|
5086 $width = $webp_info['width']; |
|
5087 $height = $webp_info['height']; |
|
5088 |
|
5089 // Mimic the native return format. |
|
5090 if ( $width && $height ) { |
|
5091 return array( |
|
5092 $width, |
|
5093 $height, |
|
5094 IMAGETYPE_WEBP, // phpcs:ignore PHPCompatibility.Constants.NewConstants.imagetype_webpFound |
|
5095 sprintf( |
|
5096 'width="%d" height="%d"', |
|
5097 $width, |
|
5098 $height |
|
5099 ), |
|
5100 'mime' => 'image/webp', |
|
5101 ); |
|
5102 } |
|
5103 } |
|
5104 |
|
5105 // The image could not be parsed. |
|
5106 return false; |
|
5107 } |
|
5108 |
|
5109 /** |
|
5110 * Extracts meta information about a webp file: width, height and type. |
|
5111 * |
|
5112 * @since 5.8.0 |
|
5113 * |
|
5114 * @param string $filename Path to a WebP file. |
|
5115 * @return array $webp_info { |
|
5116 * An array of WebP image information. |
|
5117 * |
|
5118 * @type array $size { |
|
5119 * @type int|false $width Image width on success, false on failure. |
|
5120 * @type int|false $height Image height on success, false on failure. |
|
5121 * @type string|false $type The WebP type: one of 'lossy', 'lossless' or 'animated-alpha'. |
|
5122 * False on failure. |
|
5123 * } |
|
5124 */ |
|
5125 function wp_get_webp_info( $filename ) { |
|
5126 $width = false; |
|
5127 $height = false; |
|
5128 $type = false; |
|
5129 |
|
5130 if ( 'image/webp' !== wp_get_image_mime( $filename ) ) { |
|
5131 return compact( 'width', 'height', 'type' ); |
|
5132 } |
|
5133 |
|
5134 try { |
|
5135 $handle = fopen( $filename, 'rb' ); |
|
5136 if ( $handle ) { |
|
5137 $magic = fread( $handle, 40 ); |
|
5138 fclose( $handle ); |
|
5139 |
|
5140 // Make sure we got enough bytes. |
|
5141 if ( strlen( $magic ) < 40 ) { |
|
5142 return compact( 'width', 'height', 'type' ); |
|
5143 } |
|
5144 |
|
5145 // The headers are a little different for each of the three formats. |
|
5146 // Header values based on WebP docs, see https://developers.google.com/speed/webp/docs/riff_container. |
|
5147 switch ( substr( $magic, 12, 4 ) ) { |
|
5148 // Lossy WebP. |
|
5149 case 'VP8 ': |
|
5150 $parts = unpack( 'v2', substr( $magic, 26, 4 ) ); |
|
5151 $width = (int) ( $parts[1] & 0x3FFF ); |
|
5152 $height = (int) ( $parts[2] & 0x3FFF ); |
|
5153 $type = 'lossy'; |
|
5154 break; |
|
5155 // Lossless WebP. |
|
5156 case 'VP8L': |
|
5157 $parts = unpack( 'C4', substr( $magic, 21, 4 ) ); |
|
5158 $width = (int) ( $parts[1] | ( ( $parts[2] & 0x3F ) << 8 ) ) + 1; |
|
5159 $height = (int) ( ( ( $parts[2] & 0xC0 ) >> 6 ) | ( $parts[3] << 2 ) | ( ( $parts[4] & 0x03 ) << 10 ) ) + 1; |
|
5160 $type = 'lossless'; |
|
5161 break; |
|
5162 // Animated/alpha WebP. |
|
5163 case 'VP8X': |
|
5164 // Pad 24-bit int. |
|
5165 $width = unpack( 'V', substr( $magic, 24, 3 ) . "\x00" ); |
|
5166 $width = (int) ( $width[1] & 0xFFFFFF ) + 1; |
|
5167 // Pad 24-bit int. |
|
5168 $height = unpack( 'V', substr( $magic, 27, 3 ) . "\x00" ); |
|
5169 $height = (int) ( $height[1] & 0xFFFFFF ) + 1; |
|
5170 $type = 'animated-alpha'; |
|
5171 break; |
|
5172 } |
|
5173 } |
|
5174 } catch ( Exception $e ) { |
|
5175 } |
|
5176 |
|
5177 return compact( 'width', 'height', 'type' ); |
|
5178 } |