Ticket #37255: 37255.5.diff
| File 37255.5.diff, 20.8 KB (added by , 5 years ago) |
|---|
-
media.php
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 { … … 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 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 ); 201 194 202 /** 195 203 * Filters whether to preempt the output of image_downsize(). 196 204 * … … 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->ID ); 215 225 $width = 0; 216 226 $height = 0; 217 227 $is_intermediate = false; … … 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 ); … … 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 ) { … … 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. 369 380 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 370 * width and height values in pixels (in that order). Default 'medium'.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' ) { 374 385 386 $post = get_post( $attachment ); 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 … … 384 402 * Filters the value of the attachment's image tag class attribute. 385 403 * 386 404 * @since 2.6.0 405 * @since 5.6.0 add $post parameter. 387 406 * 388 407 * @param string $class CSS class name or space-separated list of classes. 389 408 * @param int $id Attachment ID. … … 390 409 * @param string $align Part of the class name for aligning the image. 391 410 * @param string|int[] $size Requested image size. Can be any registered image size name, or 392 411 * an array of width and height values in pixels (in that order). 412 * @param WP_Post $post Attachment post object. 393 413 */ 394 $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );414 $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size, $post ); 395 415 396 416 $html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; 397 417 … … 399 419 * Filters the HTML content for the image tag. 400 420 * 401 421 * @since 2.6.0 422 * @since 5.6.0 add $post parameter. 402 423 * 403 424 * @param string $html HTML content for the image. 404 425 * @param int $id Attachment ID. … … 407 428 * @param string $align Part of the class name for aligning the image. 408 429 * @param string|int[] $size Requested image size. Can be any registered image size name, or 409 430 * an array of width and height values in pixels (in that order). 431 * @param WP_Post $post Attachment post object. 410 432 */ 411 return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );433 return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size, $post ); 412 434 } 413 435 414 436 /** … … 741 763 * browser scale down the image. 742 764 * 743 765 * @since 2.5.0 766 * @since 5.6.0 allow WP_Post object to be passed. 744 767 * 745 * @param int $post_id Attachment ID.746 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array747 * of width and height values in pixels (in that order). Default 'thumbnail'.768 * @param int|WP_Post $attachment Attachment post or post ID. 769 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 770 * of width and height values in pixels (in that order). Default 'thumbnail'. 748 771 * @return array|false { 749 772 * Array of file relative path, width, and height on success. Additionally includes absolute 750 773 * path and URL if registered size is passed to `$size` parameter. False on failure. … … 756 779 * @type string $url URL of image. 757 780 * } 758 781 */ 759 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 760 $imagedata = wp_get_attachment_metadata( $post_id ); 782 function image_get_intermediate_size( $attachment, $size = 'thumbnail' ) { 761 783 762 if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { 784 $data = array(); 785 $post = get_post( $attachment ); 786 787 if ( ! $size || ! $post || 'attachment' !== $post->post_type ) { 763 788 return false; 764 789 } 765 790 766 $ data = array();791 $imagedata = wp_get_attachment_metadata( $post ); 767 792 793 if ( ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { 794 return false; 795 } 796 768 797 // Find the best match when '$size' is an array. 769 798 if ( is_array( $size ) ) { 770 799 $candidates = array(); … … 828 857 829 858 // Include the full filesystem path of the intermediate file. 830 859 if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) { 831 $file_url = wp_get_attachment_url( $post _id);860 $file_url = wp_get_attachment_url( $post ); 832 861 $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] ); 833 862 $data['url'] = path_join( dirname( $file_url ), $data['file'] ); 834 863 } … … 837 866 * Filters the output of image_get_intermediate_size() 838 867 * 839 868 * @since 4.4.0 869 * @since 5.6.0 add $post parameter. 840 870 * 841 871 * @see image_get_intermediate_size() 842 872 * 843 873 * @param array $data Array of file relative path, width, and height on success. May also include 844 874 * file absolute path and URL. 845 * @param int $post_idThe ID of the image attachment.875 * @param int The ID of the image attachment. 846 876 * @param string|int[] $size Requested image size. Can be any registered image size name, or 847 877 * an array of width and height values in pixels (in that order). 878 * @param WP_Post $post Attachment post object. 848 879 */ 849 return apply_filters( 'image_get_intermediate_size', $data, $post _id, $size);880 return apply_filters( 'image_get_intermediate_size', $data, $post->ID, $size, $post ); 850 881 } 851 882 852 883 /** … … 934 965 * Retrieves an image to represent an attachment. 935 966 * 936 967 * @since 2.5.0 968 * @since 5.6.0 allow WP_Post object to be passed. 937 969 * 938 * @param int $attachment_id Image attachment ID.970 * @param int|WP_Post $attachment Attachment post or post ID. 939 971 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 940 972 * width and height values in pixels (in that order). Default 'thumbnail'. 941 973 * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. … … 948 980 * @type bool $3 Whether the image is a resized image. 949 981 * } 950 982 */ 951 function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) { 983 function wp_get_attachment_image_src( $attachment, $size = 'thumbnail', $icon = false ) { 984 985 $post = get_post( $attachment ); 986 987 if ( ! $post || 'attachment' !== $post->post_type ) { 988 return false; 989 } 990 952 991 // Get a thumbnail or intermediate image if there is one. 992 $attachment_id = $post->ID; 953 993 $image = image_downsize( $attachment_id, $size ); 954 994 if ( ! $image ) { 955 995 $src = false; … … 974 1014 * Filters the attachment image source result. 975 1015 * 976 1016 * @since 4.3.0 1017 * @since 5.6.0 add $post parameter. 977 1018 * 978 1019 * @param array|false $image { 979 1020 * Array of image data, or boolean false if no image is available. … … 987 1028 * @param string|int[] $size Requested image size. Can be any registered image size name, or 988 1029 * an array of width and height values in pixels (in that order). 989 1030 * @param bool $icon Whether the image should be treated as an icon. 1031 * @param WP_Post $post Attachment post object. 990 1032 */ 991 return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );1033 return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon, $post ); 992 1034 } 993 1035 994 1036 /** … … 1002 1044 * @since 2.5.0 1003 1045 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added. 1004 1046 * @since 5.5.0 The `$loading` attribute was added. 1047 * @since 5.6.0 allow WP_Post object to be passed. 1005 1048 * 1006 * @param int $attachment_id Image attachment ID.1049 * @param int|WP_Post $attachment Attachment post or post ID. 1007 1050 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 1008 1051 * of width and height values in pixels (in that order). Default 'thumbnail'. 1009 1052 * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. … … 1023 1066 * } 1024 1067 * @return string HTML img element or empty string on failure. 1025 1068 */ 1026 function wp_get_attachment_image( $attachment _id, $size = 'thumbnail', $icon = false, $attr = '' ) {1069 function wp_get_attachment_image( $attachment, $size = 'thumbnail', $icon = false, $attr = '' ) { 1027 1070 $html = ''; 1028 $image = wp_get_attachment_image_src( $attachment _id, $size, $icon );1071 $image = wp_get_attachment_image_src( $attachment, $size, $icon ); 1029 1072 1030 1073 if ( $image ) { 1031 1074 list( $src, $width, $height ) = $image; 1032 1075 1033 $ attachment = get_post( $attachment_id);1076 $post = get_post( $attachment ); 1034 1077 $hwstring = image_hwstring( $width, $height ); 1035 1078 $size_class = $size; 1036 1079 … … 1041 1084 $default_attr = array( 1042 1085 'src' => $src, 1043 1086 'class' => "attachment-$size_class size-$size_class", 1044 'alt' => trim( strip_tags( get_post_meta( $ attachment_id, '_wp_attachment_image_alt', true ) ) ),1087 'alt' => trim( strip_tags( get_post_meta( $post->ID, '_wp_attachment_image_alt', true ) ) ), 1045 1088 ); 1046 1089 1047 1090 // Add `loading` attribute. … … 1059 1102 1060 1103 // Generate 'srcset' and 'sizes' if not already present. 1061 1104 if ( empty( $attr['srcset'] ) ) { 1062 $image_meta = wp_get_attachment_metadata( $ attachment_id);1105 $image_meta = wp_get_attachment_metadata( $post->ID ); 1063 1106 1064 1107 if ( is_array( $image_meta ) ) { 1065 1108 $size_array = array( absint( $width ), absint( $height ) ); 1066 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $ attachment_id);1067 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $ attachment_id);1109 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $post->ID ); 1110 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $post->ID ); 1068 1111 1069 1112 if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { 1070 1113 $attr['srcset'] = $srcset; … … 1083 1126 * 1084 1127 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. 1085 1128 * See wp_get_attachment_image(). 1086 * @param WP_Post $ attachmentImage attachment post.1129 * @param WP_Post $post Image attachment post. 1087 1130 * @param string|int[] $size Requested image size. Can be any registered image size name, or 1088 1131 * an array of width and height values in pixels (in that order). 1089 1132 */ 1090 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $ attachment, $size );1133 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $post, $size ); 1091 1134 1092 1135 $attr = array_map( 'esc_attr', $attr ); 1093 1136 $html = rtrim( "<img $hwstring" ); … … 1112 1155 * @param string[] $attr Array of attribute values for the image markup, keyed by attribute name. 1113 1156 * See wp_get_attachment_image(). 1114 1157 */ 1115 return apply_filters( 'wp_get_attachment_image', $html, $attachment _id, $size, $icon, $attr );1158 return apply_filters( 'wp_get_attachment_image', $html, $attachment, $size, $icon, $attr ); 1116 1159 } 1117 1160 1118 1161 /** … … 1197 1240 * 1198 1241 * @see wp_calculate_image_srcset() 1199 1242 * 1200 * @param int $attachment _id Image attachment ID.1243 * @param int $attachment Attachment post or post ID. 1201 1244 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 1202 1245 * width and height values in pixels (in that order). Default 'medium'. 1203 1246 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. … … 1204 1247 * Default null. 1205 1248 * @return string|false A 'srcset' value string or false. 1206 1249 */ 1207 function wp_get_attachment_image_srcset( $attachment _id, $size = 'medium', $image_meta = null ) {1208 $image = wp_get_attachment_image_src( $attachment _id, $size );1250 function wp_get_attachment_image_srcset( $attachment, $size = 'medium', $image_meta = null ) { 1251 $image = wp_get_attachment_image_src( $attachment, $size ); 1209 1252 1210 1253 if ( ! $image ) { 1211 1254 return false; 1212 1255 } 1213 1256 1257 $post = get_post( $attachment ); 1258 1214 1259 if ( ! is_array( $image_meta ) ) { 1215 $image_meta = wp_get_attachment_metadata( $ attachment_id);1260 $image_meta = wp_get_attachment_metadata( $post->ID ); 1216 1261 } 1217 1262 1218 1263 $image_src = $image[0]; … … 1221 1266 absint( $image[2] ), 1222 1267 ); 1223 1268 1224 return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $ attachment_id);1269 return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $post->ID ); 1225 1270 } 1226 1271 1227 1272 /** … … 1438 1483 * 1439 1484 * @see wp_calculate_image_sizes() 1440 1485 * 1441 * @param int $attachment _id Image attachment ID.1486 * @param int $attachment Attachment post or post ID. 1442 1487 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 1443 1488 * width and height values in pixels (in that order). Default 'medium'. 1444 1489 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. … … 1445 1490 * Default null. 1446 1491 * @return string|false A valid source size value for use in a 'sizes' attribute or false. 1447 1492 */ 1448 function wp_get_attachment_image_sizes( $attachment _id, $size = 'medium', $image_meta = null ) {1449 $image = wp_get_attachment_image_src( $attachment _id, $size );1493 function wp_get_attachment_image_sizes( $attachment, $size = 'medium', $image_meta = null ) { 1494 $image = wp_get_attachment_image_src( $attachment, $size ); 1450 1495 1451 1496 if ( ! $image ) { 1452 1497 return false; 1453 1498 } 1454 1499 1500 $post = get_post( $attachment ); 1501 1455 1502 if ( ! is_array( $image_meta ) ) { 1456 $image_meta = wp_get_attachment_metadata( $ attachment_id);1503 $image_meta = wp_get_attachment_metadata( $post->ID ); 1457 1504 } 1458 1505 1459 1506 $image_src = $image[0]; … … 1462 1509 absint( $image[2] ), 1463 1510 ); 1464 1511 1465 return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $ attachment_id);1512 return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $post->ID ); 1466 1513 } 1467 1514 1468 1515 /** -
post.php
6289 6289 * Retrieves attachment metadata for attachment ID. 6290 6290 * 6291 6291 * @since 2.1.0 6292 * @since 5.6.0 allow WP_Post object to be passed. 6292 6293 * 6293 * @param int $attachment_id Attachment post ID. Defaults to global $post. 6294 * @param bool $unfiltered Optional. If true, filters are not run. Default false. 6294 * @param int|WP_Post $attachment Attachment post or post ID. 6295 * @param bool $unfiltered Optional. If true, filters are not run. Default false. 6296 * 6295 6297 * @return array|false { 6296 6298 * Attachment metadata. False on failure. 6297 6299 * … … 6378 6380 * 6379 6381 * @global string $pagenow 6380 6382 * 6381 * @param int $attachment_id Optional. Attachmentpost ID. Defaults to global $post.6383 * @param int|WP_Post $attachment Attachment post or post ID. Defaults to global $post. 6382 6384 * @return string|false Attachment URL, otherwise false. 6383 6385 */ 6384 function wp_get_attachment_url( $attachment _id= 0 ) {6386 function wp_get_attachment_url( $attachment = 0 ) { 6385 6387 global $pagenow; 6386 6388 6387 $ attachment_id = (int) $attachment_id;6389 $post = get_post( $attachment ); 6388 6390 6389 $post = get_post( $attachment_id ); 6390 6391 if ( ! $post ) { 6391 if( ! $post || 'attachment' !== $post->post_type ) { 6392 6392 return false; 6393 6393 } 6394 6394 6395 if ( 'attachment' !== $post->post_type ) {6396 return false;6397 }6398 6399 6395 $url = ''; 6400 6396 // Get attached file. 6401 6397 $file = get_post_meta( $post->ID, '_wp_attached_file', true ); … … 6434 6430 * Filters the attachment URL. 6435 6431 * 6436 6432 * @since 2.1.0 6433 * @since 5.6.0 add $post parameter. 6437 6434 * 6438 * @param string $url URL for the given attachment. 6439 * @param int $attachment_id Attachment post ID. 6435 * @param string $url URL for the given attachment. 6436 * @param int $attachment_id Attachment post ID. 6437 * @param WP_Post $post Attachment WP_Post object. 6440 6438 */ 6441 $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );6439 $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID, $post ); 6442 6440 6443 6441 if ( ! $url ) { 6444 6442 return false; … … 6510 6508 * Filters the attachment thumbnail file path. 6511 6509 * 6512 6510 * @since 2.1.0 6511 * @since 5.6.0 add $post parameter. 6513 6512 * 6514 * @param string $thumbfile File path to the attachment thumbnail. 6515 * @param int $post_id Attachment ID. 6513 * @param string $thumbfile File path to the attachment thumbnail. 6514 * @param int $attachment_id Attachment ID. 6515 * @param WP_Post $post Attachment post object. 6516 6516 */ 6517 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );6517 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID, $post ); 6518 6518 } 6519 6519 } 6520 6520 return false; … … 6541 6541 return false; 6542 6542 } 6543 6543 6544 $sized = image_downsize( $post _id, 'thumbnail' );6544 $sized = image_downsize( $post->ID, 'thumbnail' ); 6545 6545 if ( $sized ) { 6546 6546 return $sized[0]; 6547 6547 } … … 6557 6557 * Filters the attachment thumbnail URL. 6558 6558 * 6559 6559 * @since 2.1.0 6560 * @since 5.6.0 add $post parameter. 6560 6561 * 6561 * @param string $url URL for the attachment thumbnail. 6562 * @param int $post_id Attachment ID. 6562 * @param string $url URL for the attachment thumbnail. 6563 * @param int $attachment_id Attachment ID. 6564 * @param WP_Post $post Attachment post object. 6563 6565 */ 6564 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID );6566 return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID, $post ); 6565 6567 } 6566 6568 6567 6569 /**