diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
index ece84326f7..47ed3697bb 100644
|
a
|
b
|
function image_hwstring( $width, $height ) { |
| 175 | 175 | * elements that are normally returned from the function. |
| 176 | 176 | * |
| 177 | 177 | * @since 2.5.0 |
| | 178 | * @since 5.6.0 allow WP_Post object to be passed. |
| 178 | 179 | * |
| 179 | | * @param int $id Attachment ID for image. |
| | 180 | * @param int|WP_Post $attachment Attachment post or attachment ID for image. |
| 180 | 181 | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
| 181 | 182 | * of width and height values in pixels (in that order). Default 'medium'. |
| 182 | 183 | * @return array|false { |
| … |
… |
function image_hwstring( $width, $height ) { |
| 188 | 189 | * @type bool $3 Whether the image is a resized image. |
| 189 | 190 | * } |
| 190 | 191 | */ |
| 191 | | function image_downsize( $id, $size = 'medium' ) { |
| 192 | | $is_image = wp_attachment_is_image( $id ); |
| | 192 | function image_downsize( $attachment, $size = 'medium' ) { |
| | 193 | |
| | 194 | $post = get_post( $attachment ); |
| | 195 | |
| | 196 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| | 197 | return false; |
| | 198 | } |
| | 199 | |
| | 200 | $is_image = wp_attachment_is_image( $post ); |
| 193 | 201 | |
| 194 | 202 | /** |
| 195 | 203 | * Filters whether to preempt the output of image_downsize(). |
| … |
… |
function image_downsize( $id, $size = 'medium' ) { |
| 198 | 206 | * down-sizing the image, returning that value instead. |
| 199 | 207 | * |
| 200 | 208 | * @since 2.5.0 |
| | 209 | * @since 5.6.0 add $post parameter. |
| 201 | 210 | * |
| 202 | 211 | * @param bool|array $downsize Whether to short-circuit the image downsize. |
| 203 | 212 | * @param int $id Attachment ID for image. |
| 204 | 213 | * @param string|int[] $size Requested image size. Can be any registered image size name, or |
| 205 | 214 | * an array of width and height values in pixels (in that order). |
| | 215 | * @param WP_Post $post Attachment post object. |
| 206 | 216 | */ |
| 207 | | $out = apply_filters( 'image_downsize', false, $id, $size ); |
| | 217 | $out = apply_filters( 'image_downsize', false, $post->ID, $size, $post ); |
| 208 | 218 | |
| 209 | 219 | if ( $out ) { |
| 210 | 220 | return $out; |
| 211 | 221 | } |
| 212 | 222 | |
| 213 | | $img_url = wp_get_attachment_url( $id ); |
| 214 | | $meta = wp_get_attachment_metadata( $id ); |
| | 223 | $img_url = wp_get_attachment_url( $post ); |
| | 224 | $meta = wp_get_attachment_metadata( $post ); |
| 215 | 225 | $width = 0; |
| 216 | 226 | $height = 0; |
| 217 | 227 | $is_intermediate = false; |
| … |
… |
function image_downsize( $id, $size = 'medium' ) { |
| 231 | 241 | } |
| 232 | 242 | |
| 233 | 243 | // Try for a new style intermediate size. |
| 234 | | $intermediate = image_get_intermediate_size( $id, $size ); |
| | 244 | $intermediate = image_get_intermediate_size( $post, $size ); |
| 235 | 245 | |
| 236 | 246 | if ( $intermediate ) { |
| 237 | 247 | $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); |
| … |
… |
function image_downsize( $id, $size = 'medium' ) { |
| 240 | 250 | $is_intermediate = true; |
| 241 | 251 | } elseif ( 'thumbnail' === $size ) { |
| 242 | 252 | // Fall back to the old thumbnail. |
| 243 | | $thumb_file = wp_get_attachment_thumb_file( $id ); |
| | 253 | $thumb_file = wp_get_attachment_thumb_file( $post ); |
| 244 | 254 | $info = null; |
| 245 | 255 | |
| 246 | 256 | if ( $thumb_file ) { |
| … |
… |
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { |
| 361 | 371 | * content. |
| 362 | 372 | * |
| 363 | 373 | * @since 2.5.0 |
| | 374 | * @since 5.6.0 allow WP_Post object to be passed. |
| 364 | 375 | * |
| 365 | | * @param int $id Attachment ID. |
| | 376 | * @param int|WP_Post $attachment Attachment post or post ID. |
| 366 | 377 | * @param string $alt Image description for the alt attribute. |
| 367 | 378 | * @param string $title Image description for the title attribute. |
| 368 | 379 | * @param string $align Part of the class name for aligning the image. |
| … |
… |
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { |
| 370 | 381 | * width and height values in pixels (in that order). Default 'medium'. |
| 371 | 382 | * @return string HTML IMG element for given image attachment |
| 372 | 383 | */ |
| 373 | | function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { |
| | 384 | function get_image_tag( $attachment, $alt, $title, $align, $size = 'medium' ) { |
| | 385 | |
| | 386 | $post = get_post( $attachment ); |
| 374 | 387 | |
| | 388 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| | 389 | return ''; |
| | 390 | } |
| | 391 | |
| | 392 | $id = $post->ID; |
| 375 | 393 | list( $img_src, $width, $height ) = image_downsize( $id, $size ); |
| 376 | 394 | $hwstring = image_hwstring( $width, $height ); |
| 377 | 395 | |
| … |
… |
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { |
| 383 | 401 | * Filters the value of the attachment's image tag class attribute. |
| 384 | 402 | * |
| 385 | 403 | * @since 2.6.0 |
| | 404 | * @since 5.6.0 add $post parameter. |
| 386 | 405 | * |
| 387 | 406 | * @param string $class CSS class name or space-separated list of classes. |
| 388 | 407 | * @param int $id Attachment ID. |
| 389 | 408 | * @param string $align Part of the class name for aligning the image. |
| 390 | 409 | * @param string|int[] $size Requested image size. Can be any registered image size name, or |
| 391 | 410 | * an array of width and height values in pixels (in that order). |
| | 411 | * @param WP_Post $post Attachment post object. |
| 392 | 412 | */ |
| 393 | | $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); |
| | 413 | $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size, $post ); |
| 394 | 414 | |
| 395 | 415 | $html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; |
| 396 | 416 | |
| … |
… |
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { |
| 398 | 418 | * Filters the HTML content for the image tag. |
| 399 | 419 | * |
| 400 | 420 | * @since 2.6.0 |
| | 421 | * @since 5.6.0 add $post parameter. |
| 401 | 422 | * |
| 402 | 423 | * @param string $html HTML content for the image. |
| 403 | 424 | * @param int $id Attachment ID. |
| … |
… |
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { |
| 406 | 427 | * @param string $align Part of the class name for aligning the image. |
| 407 | 428 | * @param string|int[] $size Requested image size. Can be any registered image size name, or |
| 408 | 429 | * an array of width and height values in pixels (in that order). |
| | 430 | * @param WP_Post $post Attachment post object. |
| 409 | 431 | */ |
| 410 | | return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); |
| | 432 | return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size, $post ); |
| 411 | 433 | } |
| 412 | 434 | |
| 413 | 435 | /** |
| … |
… |
function wp_image_matches_ratio( $source_width, $source_height, $target_width, $ |
| 740 | 762 | * browser scale down the image. |
| 741 | 763 | * |
| 742 | 764 | * @since 2.5.0 |
| | 765 | * @since 5.6.0 allow WP_Post object to be passed. |
| 743 | 766 | * |
| 744 | | * @param int $post_id Attachment ID. |
| 745 | | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
| 746 | | * of width and height values in pixels (in that order). Default 'thumbnail'. |
| | 767 | * @param int|WP_Post $attachment Attachment post or post ID. |
| | 768 | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
| | 769 | * of width and height values in pixels (in that order). Default 'thumbnail'. |
| 747 | 770 | * @return array|false { |
| 748 | 771 | * Array of file relative path, width, and height on success. Additionally includes absolute |
| 749 | 772 | * path and URL if registered size is passed to `$size` parameter. False on failure. |
| … |
… |
function wp_image_matches_ratio( $source_width, $source_height, $target_width, $ |
| 755 | 778 | * @type string $url URL of image. |
| 756 | 779 | * } |
| 757 | 780 | */ |
| 758 | | function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { |
| 759 | | $imagedata = wp_get_attachment_metadata( $post_id ); |
| | 781 | function image_get_intermediate_size( $attachment, $size = 'thumbnail' ) { |
| 760 | 782 | |
| 761 | | if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { |
| | 783 | $post = get_post( $attachment ); |
| | 784 | $imagedata = wp_get_attachment_metadata( $post ); |
| | 785 | |
| | 786 | if ( ! $size || ! $post || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { |
| 762 | 787 | return false; |
| 763 | 788 | } |
| 764 | 789 | |
| … |
… |
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { |
| 827 | 852 | |
| 828 | 853 | // Include the full filesystem path of the intermediate file. |
| 829 | 854 | if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) { |
| 830 | | $file_url = wp_get_attachment_url( $post_id ); |
| | 855 | $file_url = wp_get_attachment_url( $post ); |
| 831 | 856 | $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] ); |
| 832 | 857 | $data['url'] = path_join( dirname( $file_url ), $data['file'] ); |
| 833 | 858 | } |
| … |
… |
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { |
| 836 | 861 | * Filters the output of image_get_intermediate_size() |
| 837 | 862 | * |
| 838 | 863 | * @since 4.4.0 |
| | 864 | * @since 5.6.0 add $post parameter. |
| 839 | 865 | * |
| 840 | 866 | * @see image_get_intermediate_size() |
| 841 | 867 | * |
| 842 | 868 | * @param array $data Array of file relative path, width, and height on success. May also include |
| 843 | 869 | * file absolute path and URL. |
| 844 | | * @param int $post_id The ID of the image attachment. |
| | 870 | * @param int The ID of the image attachment. |
| 845 | 871 | * @param string|int[] $size Requested image size. Can be any registered image size name, or |
| 846 | 872 | * an array of width and height values in pixels (in that order). |
| | 873 | * @param WP_Post $post Attachment post object. |
| 847 | 874 | */ |
| 848 | | return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); |
| | 875 | return apply_filters( 'image_get_intermediate_size', $data, $post->ID, $size, $post ); |
| 849 | 876 | } |
| 850 | 877 | |
| 851 | 878 | /** |
| … |
… |
function wp_get_registered_image_subsizes() { |
| 933 | 960 | * Retrieves an image to represent an attachment. |
| 934 | 961 | * |
| 935 | 962 | * @since 2.5.0 |
| | 963 | * @since 5.6.0 allow WP_Post object to be passed. |
| 936 | 964 | * |
| 937 | | * @param int $attachment_id Image attachment ID. |
| | 965 | * @param int|WP_Post $attachment Attachment post or post ID. |
| 938 | 966 | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
| 939 | 967 | * width and height values in pixels (in that order). Default 'thumbnail'. |
| 940 | 968 | * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. |
| … |
… |
function wp_get_registered_image_subsizes() { |
| 947 | 975 | * @type bool $3 Whether the image is a resized image. |
| 948 | 976 | * } |
| 949 | 977 | */ |
| 950 | | function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) { |
| | 978 | function wp_get_attachment_image_src( $attachment, $size = 'thumbnail', $icon = false ) { |
| | 979 | |
| | 980 | $post = get_post( $attachment ); |
| | 981 | |
| | 982 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| | 983 | return false; |
| | 984 | } |
| | 985 | |
| 951 | 986 | // Get a thumbnail or intermediate image if there is one. |
| | 987 | $attachment_id = $post->ID; |
| 952 | 988 | $image = image_downsize( $attachment_id, $size ); |
| 953 | 989 | if ( ! $image ) { |
| 954 | 990 | $src = false; |
| … |
… |
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon |
| 973 | 1009 | * Filters the attachment image source result. |
| 974 | 1010 | * |
| 975 | 1011 | * @since 4.3.0 |
| | 1012 | * @since 5.6.0 add $post parameter. |
| 976 | 1013 | * |
| 977 | 1014 | * @param array|false $image { |
| 978 | 1015 | * Array of image data, or boolean false if no image is available. |
| … |
… |
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon |
| 986 | 1023 | * @param string|int[] $size Requested image size. Can be any registered image size name, or |
| 987 | 1024 | * an array of width and height values in pixels (in that order). |
| 988 | 1025 | * @param bool $icon Whether the image should be treated as an icon. |
| | 1026 | * @param WP_Post $post Attachment post object. |
| 989 | 1027 | */ |
| 990 | | return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); |
| | 1028 | return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon, $post ); |
| 991 | 1029 | } |
| 992 | 1030 | |
| 993 | 1031 | /** |
| … |
… |
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon |
| 1001 | 1039 | * @since 2.5.0 |
| 1002 | 1040 | * @since 4.4.0 The `$srcset` and `$sizes` attributes were added. |
| 1003 | 1041 | * @since 5.5.0 The `$loading` attribute was added. |
| | 1042 | * @since 5.6.0 allow WP_Post object to be passed. |
| 1004 | 1043 | * |
| 1005 | | * @param int $attachment_id Image attachment ID. |
| | 1044 | * @param int|WP_Post $attachment Attachment post or post ID. |
| 1006 | 1045 | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
| 1007 | 1046 | * of width and height values in pixels (in that order). Default 'thumbnail'. |
| 1008 | 1047 | * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
| … |
… |
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon |
| 1022 | 1061 | * } |
| 1023 | 1062 | * @return string HTML img element or empty string on failure. |
| 1024 | 1063 | */ |
| 1025 | | function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) { |
| | 1064 | function wp_get_attachment_image( $attachment, $size = 'thumbnail', $icon = false, $attr = '' ) { |
| 1026 | 1065 | $html = ''; |
| 1027 | | $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
| | 1066 | $image = wp_get_attachment_image_src( $attachment, $size, $icon ); |
| 1028 | 1067 | |
| 1029 | 1068 | if ( $image ) { |
| 1030 | 1069 | list( $src, $width, $height ) = $image; |
| 1031 | 1070 | |
| 1032 | | $attachment = get_post( $attachment_id ); |
| | 1071 | $post = get_post( $attachment ); |
| 1033 | 1072 | $hwstring = image_hwstring( $width, $height ); |
| 1034 | 1073 | $size_class = $size; |
| 1035 | 1074 | |
| 1036 | 1075 | if ( is_array( $size_class ) ) { |
| 1037 | | $size_class = join( 'x', $size_class ); |
| | 1076 | $size_class = implode( 'x', $size_class ); |
| 1038 | 1077 | } |
| 1039 | 1078 | |
| 1040 | 1079 | $default_attr = array( |
| 1041 | 1080 | 'src' => $src, |
| 1042 | 1081 | 'class' => "attachment-$size_class size-$size_class", |
| 1043 | | 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), |
| | 1082 | 'alt' => trim( strip_tags( get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) ) ), |
| 1044 | 1083 | ); |
| 1045 | 1084 | |
| 1046 | 1085 | // Add `loading` attribute. |
| … |
… |
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f |
| 1058 | 1097 | |
| 1059 | 1098 | // Generate 'srcset' and 'sizes' if not already present. |
| 1060 | 1099 | if ( empty( $attr['srcset'] ) ) { |
| 1061 | | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
| | 1100 | $image_meta = wp_get_attachment_metadata( $post ); |
| 1062 | 1101 | |
| 1063 | 1102 | if ( is_array( $image_meta ) ) { |
| 1064 | 1103 | $size_array = array( absint( $width ), absint( $height ) ); |
| 1065 | | $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); |
| 1066 | | $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); |
| | 1104 | $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $post->ID ); |
| | 1105 | $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $post->ID ); |
| 1067 | 1106 | |
| 1068 | 1107 | if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { |
| 1069 | 1108 | $attr['srcset'] = $srcset; |
| … |
… |
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f |
| 1082 | 1121 | * |
| 1083 | 1122 | * @param array $attr Array of attribute values for the image markup, keyed by attribute name. |
| 1084 | 1123 | * See wp_get_attachment_image(). |
| 1085 | | * @param WP_Post $attachment Image attachment post. |
| | 1124 | * @param WP_Post $post Image attachment post. |
| 1086 | 1125 | * @param string|int[] $size Requested image size. Can be any registered image size name, or |
| 1087 | 1126 | * an array of width and height values in pixels (in that order). |
| 1088 | 1127 | */ |
| 1089 | | $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); |
| | 1128 | $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $post, $size ); |
| 1090 | 1129 | |
| 1091 | 1130 | $attr = array_map( 'esc_attr', $attr ); |
| 1092 | 1131 | $html = rtrim( "<img $hwstring" ); |
| … |
… |
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f |
| 1104 | 1143 | /** |
| 1105 | 1144 | * Get the URL of an image attachment. |
| 1106 | 1145 | * |
| 1107 | | * @since 4.4.0 |
| | 1146 | * @since 5.6.0 allow WP_Post object to be passed. |
| 1108 | 1147 | * |
| 1109 | | * @param int $attachment_id Image attachment ID. |
| | 1148 | * @param int|WP_Post $attachment Attachment post or post ID. |
| 1110 | 1149 | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
| 1111 | 1150 | * width and height values in pixels (in that order). Default 'thumbnail'. |
| 1112 | 1151 | * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. |
| 1113 | 1152 | * @return string|false Attachment URL or false if no image is available. |
| 1114 | 1153 | */ |
| 1115 | | function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { |
| 1116 | | $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); |
| | 1154 | function wp_get_attachment_image_url( $attachment, $size = 'thumbnail', $icon = false ) { |
| | 1155 | $image = wp_get_attachment_image_src( $attachment, $size, $icon ); |
| 1117 | 1156 | return isset( $image['0'] ) ? $image['0'] : false; |
| 1118 | 1157 | } |
| 1119 | 1158 | |
| … |
… |
function _wp_get_image_size_from_meta( $size_name, $image_meta ) { |
| 1179 | 1218 | * Retrieves the value for an image attachment's 'srcset' attribute. |
| 1180 | 1219 | * |
| 1181 | 1220 | * @since 4.4.0 |
| | 1221 | * @since 5.6.0 allow WP_Post object to be passed. |
| 1182 | 1222 | * |
| 1183 | 1223 | * @see wp_calculate_image_srcset() |
| 1184 | 1224 | * |
| 1185 | | * @param int $attachment_id Image attachment ID. |
| | 1225 | * @param int|WP_Post $attachment Attachment post or post ID. |
| 1186 | 1226 | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
| 1187 | 1227 | * width and height values in pixels (in that order). Default 'medium'. |
| 1188 | 1228 | * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 1189 | 1229 | * Default null. |
| 1190 | 1230 | * @return string|bool A 'srcset' value string or false. |
| 1191 | 1231 | */ |
| 1192 | | function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { |
| 1193 | | $image = wp_get_attachment_image_src( $attachment_id, $size ); |
| | 1232 | function wp_get_attachment_image_srcset( $attachment, $size = 'medium', $image_meta = null ) { |
| | 1233 | |
| | 1234 | $post = get_post( $attachment ); |
| | 1235 | |
| | 1236 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| | 1237 | return false; |
| | 1238 | } |
| | 1239 | |
| | 1240 | $image = wp_get_attachment_image_src( $post, $size ); |
| 1194 | 1241 | |
| 1195 | 1242 | if ( ! $image ) { |
| 1196 | 1243 | return false; |
| 1197 | 1244 | } |
| 1198 | 1245 | |
| 1199 | 1246 | if ( ! is_array( $image_meta ) ) { |
| 1200 | | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
| | 1247 | $image_meta = wp_get_attachment_metadata( $post ); |
| 1201 | 1248 | } |
| 1202 | 1249 | |
| 1203 | 1250 | $image_src = $image[0]; |
| … |
… |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag |
| 1206 | 1253 | absint( $image[2] ), |
| 1207 | 1254 | ); |
| 1208 | 1255 | |
| 1209 | | return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); |
| | 1256 | return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $post->ID ); |
| 1210 | 1257 | } |
| 1211 | 1258 | |
| 1212 | 1259 | /** |
| … |
… |
function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac |
| 1423 | 1470 | * |
| 1424 | 1471 | * @see wp_calculate_image_sizes() |
| 1425 | 1472 | * |
| 1426 | | * @param int $attachment_id Image attachment ID. |
| | 1473 | * @param int|WP_Post $attachment Attachment post or post ID. |
| 1427 | 1474 | * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of |
| 1428 | 1475 | * width and height values in pixels (in that order). Default 'medium'. |
| 1429 | 1476 | * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 1430 | 1477 | * Default null. |
| 1431 | 1478 | * @return string|bool A valid source size value for use in a 'sizes' attribute or false. |
| 1432 | 1479 | */ |
| 1433 | | function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { |
| 1434 | | $image = wp_get_attachment_image_src( $attachment_id, $size ); |
| | 1480 | function wp_get_attachment_image_sizes( $attachment, $size = 'medium', $image_meta = null ) { |
| | 1481 | |
| | 1482 | $post = get_post( $attachment ); |
| | 1483 | |
| | 1484 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| | 1485 | return false; |
| | 1486 | } |
| | 1487 | |
| | 1488 | $image = wp_get_attachment_image_src( $post, $size ); |
| 1435 | 1489 | |
| 1436 | 1490 | if ( ! $image ) { |
| 1437 | 1491 | return false; |
| 1438 | 1492 | } |
| 1439 | 1493 | |
| 1440 | 1494 | if ( ! is_array( $image_meta ) ) { |
| 1441 | | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
| | 1495 | $image_meta = wp_get_attachment_metadata( $post ); |
| 1442 | 1496 | } |
| 1443 | 1497 | |
| 1444 | 1498 | $image_src = $image[0]; |
| … |
… |
function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image |
| 1447 | 1501 | absint( $image[2] ), |
| 1448 | 1502 | ); |
| 1449 | 1503 | |
| 1450 | | return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); |
| | 1504 | return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $post->ID ); |
| 1451 | 1505 | } |
| 1452 | 1506 | |
| 1453 | 1507 | /** |
diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index 575a2fbaea..7cf9d10026 100644
|
a
|
b
|
function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { |
| 5975 | 5975 | * Retrieves attachment metadata for attachment ID. |
| 5976 | 5976 | * |
| 5977 | 5977 | * @since 2.1.0 |
| | 5978 | * @since 5.6.0 allow WP_Post object to be passed. |
| | 5979 | * |
| | 5980 | * @param int|WP_Post $attachment Attachment post or post ID. |
| | 5981 | * @param bool $unfiltered Optional. If true, filters are not run. Default false. |
| 5978 | 5982 | * |
| 5979 | | * @param int $attachment_id Attachment post ID. Defaults to global $post. |
| 5980 | | * @param bool $unfiltered Optional. If true, filters are not run. Default false. |
| 5981 | 5983 | * @return array|false { |
| 5982 | 5984 | * Attachment metadata. False on failure. |
| 5983 | 5985 | * |
| … |
… |
function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { |
| 5989 | 5991 | * @type array $image_meta Image metadata. |
| 5990 | 5992 | * } |
| 5991 | 5993 | */ |
| 5992 | | function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { |
| 5993 | | $attachment_id = (int) $attachment_id; |
| | 5994 | function wp_get_attachment_metadata( $attachment, $unfiltered = false ) { |
| | 5995 | $post = get_post( $attachment ); |
| 5994 | 5996 | |
| 5995 | | $data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); |
| | 5997 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| | 5998 | return false; |
| | 5999 | } |
| | 6000 | |
| | 6001 | $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); |
| 5996 | 6002 | |
| 5997 | 6003 | if ( empty( $data ) ) { |
| 5998 | 6004 | return false; |
| … |
… |
function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { |
| 6006 | 6012 | * Filters the attachment meta data. |
| 6007 | 6013 | * |
| 6008 | 6014 | * @since 2.1.0 |
| | 6015 | * @since 5.6.0 add $post parameter. |
| 6009 | 6016 | * |
| 6010 | | * @param array|bool $data Array of meta data for the given attachment, or false |
| | 6017 | * @param array|bool $data Array of meta data for the given attachment, or false |
| 6011 | 6018 | * if the object does not exist. |
| 6012 | | * @param int $attachment_id Attachment post ID. |
| | 6019 | * @param int Attachment post ID. |
| | 6020 | * @param WP_Post $post Attachment WP_Post object. |
| 6013 | 6021 | */ |
| 6014 | | return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id ); |
| | 6022 | return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID, $post ); |
| 6015 | 6023 | } |
| 6016 | 6024 | |
| 6017 | 6025 | /** |
| 6018 | 6026 | * Updates metadata for an attachment. |
| 6019 | 6027 | * |
| 6020 | 6028 | * @since 2.1.0 |
| | 6029 | * @since 5.6.0 allow WP_Post object to be passed. |
| 6021 | 6030 | * |
| 6022 | | * @param int $attachment_id Attachment post ID. |
| 6023 | | * @param array $data Attachment meta data. |
| | 6031 | * @param int|WP_Post $attachment Attachment post or post ID. |
| | 6032 | * @param array $data Attachment meta data. |
| 6024 | 6033 | * @return int|bool False if $post is invalid. |
| 6025 | 6034 | */ |
| 6026 | | function wp_update_attachment_metadata( $attachment_id, $data ) { |
| 6027 | | $attachment_id = (int) $attachment_id; |
| | 6035 | function wp_update_attachment_metadata( $attachment, $data ) { |
| 6028 | 6036 | |
| 6029 | | $post = get_post( $attachment_id ); |
| 6030 | | if ( ! $post ) { |
| | 6037 | $post = get_post( $attachment ); |
| | 6038 | |
| | 6039 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| 6031 | 6040 | return false; |
| 6032 | 6041 | } |
| 6033 | 6042 | |
| … |
… |
function wp_update_attachment_metadata( $attachment_id, $data ) { |
| 6035 | 6044 | * Filters the updated attachment meta data. |
| 6036 | 6045 | * |
| 6037 | 6046 | * @since 2.1.0 |
| | 6047 | * @since 5.6.0 add $post parameter. |
| 6038 | 6048 | * |
| 6039 | | * @param array $data Array of updated attachment meta data. |
| 6040 | | * @param int $attachment_id Attachment post ID. |
| | 6049 | * @param array $data Array of updated attachment meta data. |
| | 6050 | * @param int $attachment_id Attachment post ID. |
| | 6051 | * @param WP_Post $attachment Attachment WP_Post object. |
| 6041 | 6052 | */ |
| 6042 | | $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ); |
| | 6053 | $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID, $post ); |
| 6043 | 6054 | if ( $data ) { |
| 6044 | 6055 | return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); |
| 6045 | | } else { |
| 6046 | | return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
| 6047 | 6056 | } |
| | 6057 | |
| | 6058 | return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
| 6048 | 6059 | } |
| 6049 | 6060 | |
| 6050 | 6061 | /** |
| 6051 | 6062 | * Retrieve the URL for an attachment. |
| 6052 | 6063 | * |
| 6053 | 6064 | * @since 2.1.0 |
| | 6065 | * @since 5.6.0 allow WP_Post object to be passed. |
| 6054 | 6066 | * |
| 6055 | 6067 | * @global string $pagenow |
| 6056 | 6068 | * |
| 6057 | | * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post. |
| | 6069 | * @param int|WP_Post $attachment Attachment post or post ID. |
| | 6070 | * |
| 6058 | 6071 | * @return string|false Attachment URL, otherwise false. |
| 6059 | 6072 | */ |
| 6060 | | function wp_get_attachment_url( $attachment_id = 0 ) { |
| 6061 | | $attachment_id = (int) $attachment_id; |
| | 6073 | function wp_get_attachment_url( $attachment ) { |
| 6062 | 6074 | |
| 6063 | | $post = get_post( $attachment_id ); |
| 6064 | | if ( ! $post ) { |
| 6065 | | return false; |
| 6066 | | } |
| | 6075 | $post = get_post( $attachment ); |
| 6067 | 6076 | |
| 6068 | | if ( 'attachment' !== $post->post_type ) { |
| | 6077 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| 6069 | 6078 | return false; |
| 6070 | 6079 | } |
| 6071 | 6080 | |
| … |
… |
function wp_get_attachment_url( $attachment_id = 0 ) { |
| 6107 | 6116 | * Filters the attachment URL. |
| 6108 | 6117 | * |
| 6109 | 6118 | * @since 2.1.0 |
| | 6119 | * @since 5.6.0 add $post parameter. |
| 6110 | 6120 | * |
| 6111 | | * @param string $url URL for the given attachment. |
| 6112 | | * @param int $attachment_id Attachment post ID. |
| | 6121 | * @param string $url URL for the given attachment. |
| | 6122 | * @param int $attachment_id Attachment post ID. |
| | 6123 | * @param WP_Post $post Attachment WP_Post object. |
| 6113 | 6124 | */ |
| 6114 | | $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); |
| | 6125 | $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID, $post ); |
| 6115 | 6126 | |
| 6116 | 6127 | if ( empty( $url ) ) { |
| 6117 | 6128 | return false; |
| … |
… |
function wp_get_attachment_url( $attachment_id = 0 ) { |
| 6125 | 6136 | * |
| 6126 | 6137 | * @since 4.6.0 |
| 6127 | 6138 | * |
| 6128 | | * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
| | 6139 | * @param int|WP_Post $attachment Attachment post or post ID. |
| | 6140 | * |
| 6129 | 6141 | * @return string|false False on failure. Attachment caption on success. |
| 6130 | 6142 | */ |
| 6131 | | function wp_get_attachment_caption( $post_id = 0 ) { |
| 6132 | | $post_id = (int) $post_id; |
| 6133 | | $post = get_post( $post_id ); |
| 6134 | | if ( ! $post ) { |
| 6135 | | return false; |
| 6136 | | } |
| | 6143 | function wp_get_attachment_caption( $attachment ) { |
| 6137 | 6144 | |
| 6138 | | if ( 'attachment' !== $post->post_type ) { |
| | 6145 | $post = get_post( $attachment ); |
| | 6146 | |
| | 6147 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| 6139 | 6148 | return false; |
| 6140 | 6149 | } |
| 6141 | 6150 | |
| … |
… |
function wp_get_attachment_caption( $post_id = 0 ) { |
| 6145 | 6154 | * Filters the attachment caption. |
| 6146 | 6155 | * |
| 6147 | 6156 | * @since 4.6.0 |
| | 6157 | * @since 5.6.0 add $post parameter. |
| 6148 | 6158 | * |
| 6149 | 6159 | * @param string $caption Caption for the given attachment. |
| 6150 | | * @param int $post_id Attachment ID. |
| | 6160 | * @param int $attachment_id Attachment ID. |
| | 6161 | * @param WP_Post $attachment Attachment WP_Post object. |
| 6151 | 6162 | */ |
| 6152 | | return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); |
| | 6163 | return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID, $post ); |
| 6153 | 6164 | } |
| 6154 | 6165 | |
| 6155 | 6166 | /** |
| 6156 | 6167 | * Retrieve thumbnail for an attachment. |
| 6157 | 6168 | * |
| 6158 | 6169 | * @since 2.1.0 |
| | 6170 | * @since 5.6.0 allow WP_Post object to be passed. |
| | 6171 | * |
| | 6172 | * @param int|WP_Post $attachment Attachment post or post ID. Defaults to the global post. |
| 6159 | 6173 | * |
| 6160 | | * @param int $post_id Optional. Attachment ID. Default 0. |
| 6161 | 6174 | * @return string|false False on failure. Thumbnail file path on success. |
| 6162 | 6175 | */ |
| 6163 | | function wp_get_attachment_thumb_file( $post_id = 0 ) { |
| 6164 | | $post_id = (int) $post_id; |
| 6165 | | $post = get_post( $post_id ); |
| 6166 | | if ( ! $post ) { |
| | 6176 | function wp_get_attachment_thumb_file( $attachment ) { |
| | 6177 | |
| | 6178 | $post = get_post( $attachment ); |
| | 6179 | |
| | 6180 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| 6167 | 6181 | return false; |
| 6168 | 6182 | } |
| 6169 | 6183 | |
| … |
… |
function wp_get_attachment_thumb_file( $post_id = 0 ) { |
| 6181 | 6195 | * Filters the attachment thumbnail file path. |
| 6182 | 6196 | * |
| 6183 | 6197 | * @since 2.1.0 |
| | 6198 | * @since 5.6.0 add $post parameter. |
| 6184 | 6199 | * |
| 6185 | | * @param string $thumbfile File path to the attachment thumbnail. |
| 6186 | | * @param int $post_id Attachment ID. |
| | 6200 | * @param string $thumbfile File path to the attachment thumbnail. |
| | 6201 | * @param int $attachment_id Attachment ID. |
| | 6202 | * @param WP_Post $post Attachment post object. |
| 6187 | 6203 | */ |
| 6188 | | return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
| | 6204 | return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID, $post ); |
| 6189 | 6205 | } |
| 6190 | 6206 | } |
| 6191 | 6207 | return false; |
| … |
… |
function wp_get_attachment_thumb_file( $post_id = 0 ) { |
| 6195 | 6211 | * Retrieve URL for an attachment thumbnail. |
| 6196 | 6212 | * |
| 6197 | 6213 | * @since 2.1.0 |
| | 6214 | * @since 5.6.0 allow WP_Post object to be passed. |
| | 6215 | * |
| | 6216 | * @param int|WP_Post $attachment Attachment post or post ID. |
| 6198 | 6217 | * |
| 6199 | | * @param int $post_id Optional. Attachment ID. Default 0. |
| 6200 | 6218 | * @return string|false False on failure. Thumbnail URL on success. |
| 6201 | 6219 | */ |
| 6202 | | function wp_get_attachment_thumb_url( $post_id = 0 ) { |
| 6203 | | $post_id = (int) $post_id; |
| 6204 | | $post = get_post( $post_id ); |
| 6205 | | if ( ! $post ) { |
| | 6220 | function wp_get_attachment_thumb_url( $attachment ) { |
| | 6221 | |
| | 6222 | $post = get_post( $attachment ); |
| | 6223 | |
| | 6224 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| 6206 | 6225 | return false; |
| 6207 | 6226 | } |
| 6208 | 6227 | |
| … |
… |
function wp_get_attachment_thumb_url( $post_id = 0 ) { |
| 6211 | 6230 | return false; |
| 6212 | 6231 | } |
| 6213 | 6232 | |
| 6214 | | $sized = image_downsize( $post_id, 'thumbnail' ); |
| | 6233 | $sized = image_downsize( $post->ID, 'thumbnail' ); |
| 6215 | 6234 | if ( $sized ) { |
| 6216 | 6235 | return $sized[0]; |
| 6217 | 6236 | } |
| … |
… |
function wp_get_attachment_thumb_url( $post_id = 0 ) { |
| 6227 | 6246 | * Filters the attachment thumbnail URL. |
| 6228 | 6247 | * |
| 6229 | 6248 | * @since 2.1.0 |
| | 6249 | * @since 5.6.0 add $post parameter. |
| 6230 | 6250 | * |
| 6231 | | * @param string $url URL for the attachment thumbnail. |
| 6232 | | * @param int $post_id Attachment ID. |
| | 6251 | * @param string $url URL for the attachment thumbnail. |
| | 6252 | * @param int $attachment_id Attachment ID. |
| | 6253 | * @param WP_Post $post Attachment post object. |
| 6233 | 6254 | */ |
| 6234 | | return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
| | 6255 | return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID, $post ); |
| 6235 | 6256 | } |
| 6236 | 6257 | |
| 6237 | 6258 | /** |
| … |
… |
function wp_get_attachment_thumb_url( $post_id = 0 ) { |
| 6244 | 6265 | * @return bool True if one of the accepted types, false otherwise. |
| 6245 | 6266 | */ |
| 6246 | 6267 | function wp_attachment_is( $type, $post = null ) { |
| | 6268 | |
| 6247 | 6269 | $post = get_post( $post ); |
| 6248 | | if ( ! $post ) { |
| | 6270 | |
| | 6271 | if ( ! $post || 'attachment' !== $post->post_type ) { |
| 6249 | 6272 | return false; |
| 6250 | 6273 | } |
| 6251 | 6274 | |