Ticket #37255: 37255.6.diff
| File 37255.6.diff, 21.5 KB (added by , 3 years ago) |
|---|
-
src/wp-includes/media.php
175 175 * elements that are normally returned from the function. 176 176 * 177 177 * @since 2.5.0 178 * @since 6.3.0 Allow WP_Post object to be passed. 178 179 * 179 * @param int $id Attachment ID for image.180 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array181 * of width and height values in pixels (in that order). Default 'medium'.180 * @param int|WP_Post $attachment Attachment post object or attachment ID for image. 181 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 182 * of width and height values in pixels (in that order). Default 'medium'. 182 183 * @return array|false { 183 184 * Array of image data, or boolean false if no image is available. 184 185 * … … 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 $is_image = wp_attachment_is_image( $attachment ); 193 194 195 // Ensure backward compatibility whenever a WP_Post object is passed. 196 $id = $attachment; 197 if ( is_a( $attachment, 'WP_Post' ) ) { 198 $id = $attachment->ID; 199 } 200 194 201 /** 195 202 * Filters whether to preempt the output of image_downsize(). 196 203 * … … 361 368 * content. 362 369 * 363 370 * @since 2.5.0 371 * @since 6.3.0 Allow WP_Post object to be passed. 364 372 * 365 * @param int $id Attachment ID.366 * @param string $alt Image description for the alt attribute.367 * @param string $title Image description for the title attribute.368 * @param string $align Part of the class name for aligning the image.369 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of370 * width and height values in pixels (in that order). Default 'medium'.373 * @param int|WP_Post $attachment Attachment WP_Post object or attachment ID. 374 * @param string $alt Image description for the alt attribute. 375 * @param string $title Image description for the title attribute. 376 * @param string $align Part of the class name for aligning the image. 377 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 378 * width and height values in pixels (in that order). Default 'medium'. 371 379 * @return string HTML IMG element for given image attachment? 372 380 */ 373 function get_image_tag( $ id, $alt, $title, $align, $size = 'medium' ) {381 function get_image_tag( $attachment, $alt, $title, $align, $size = 'medium' ) { 374 382 375 list( $img_src, $width, $height ) = image_downsize( $ id, $size );383 list( $img_src, $width, $height ) = image_downsize( $attachment, $size ); 376 384 $hwstring = image_hwstring( $width, $height ); 377 385 386 // Ensure backward compatibility whenever a WP_Post object is passed. 387 $id = $attachment; 388 if ( is_a( $attachment, 'WP_Post' ) ) { 389 $id = $attachment->ID; 390 } 391 378 392 $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; 379 393 380 394 $size_class = is_array( $size ) ? implode( 'x', $size ) : $size; … … 741 755 * browser scale down the image. 742 756 * 743 757 * @since 2.5.0 758 * @since 6.3.0 Allow WP_Post object to be passed. 744 759 * 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'.760 * @param int|WP_Post $attachment Attachment post object or attachment ID. 761 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 762 * of width and height values in pixels (in that order). Default 'thumbnail'. 748 763 * @return array|false { 749 764 * Array of file relative path, width, and height on success. Additionally includes absolute 750 765 * path and URL if registered size is passed to `$size` parameter. False on failure. … … 756 771 * @type string $url URL of image. 757 772 * } 758 773 */ 759 function image_get_intermediate_size( $ post_id, $size = 'thumbnail' ) {760 $imagedata = wp_get_attachment_metadata( $ post_id);774 function image_get_intermediate_size( $attachment, $size = 'thumbnail' ) { 775 $imagedata = wp_get_attachment_metadata( $attachment ); 761 776 762 777 if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { 763 778 return false; … … 828 843 829 844 // Include the full filesystem path of the intermediate file. 830 845 if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) { 831 $file_url = wp_get_attachment_url( $ post_id);846 $file_url = wp_get_attachment_url( $attachment ); 832 847 $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] ); 833 848 $data['url'] = path_join( dirname( $file_url ), $data['file'] ); 834 849 } 835 850 851 // Ensure backward compatibility whenever a WP_Post object is passed. 852 $attachment_id = $attachment; 853 if ( is_a( $attachment, 'WP_Post' ) ) { 854 $attachment_id = $attachment->ID; 855 } 856 836 857 /** 837 858 * Filters the output of image_get_intermediate_size() 838 859 * … … 840 861 * 841 862 * @see image_get_intermediate_size() 842 863 * 843 * @param array $data Array of file relative path, width, and height on success. May also include844 * file absolute path and URL.845 * @param int $ post_id The ID of the image attachment.846 * @param string|int[] $size Requested image size. Can be any registered image size name, or847 * an array of width and height values in pixels (in that order).864 * @param array $data Array of file relative path, width, and height on success. May also include 865 * file absolute path and URL. 866 * @param int $attachment_id The ID of the image attachment. 867 * @param string|int[] $size Requested image size. Can be any registered image size name, or 868 * an array of width and height values in pixels (in that order). 848 869 */ 849 return apply_filters( 'image_get_intermediate_size', $data, $ post_id, $size );870 return apply_filters( 'image_get_intermediate_size', $data, $attachment_id, $size ); 850 871 } 851 872 852 873 /** … … 935 956 * Retrieves an image to represent an attachment. 936 957 * 937 958 * @since 2.5.0 959 * @since 6.3.0 Allow WP_Post object to be passed. 938 960 * 939 * @param int $attachment_id Imageattachment ID.940 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of941 * width and height values in pixels (in that order). Default 'thumbnail'.942 * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false.961 * @param int|WP_Post $attachment Image attachment WP_Post object or attachment ID. 962 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 963 * width and height values in pixels (in that order). Default 'thumbnail'. 964 * @param bool $icon Optional. Whether the image should fall back to a mime type icon. Default false. 943 965 * @return array|false { 944 966 * Array of image data, or boolean false if no image is available. 945 967 * … … 949 971 * @type bool $3 Whether the image is a resized image. 950 972 * } 951 973 */ 952 function wp_get_attachment_image_src( $attachment _id, $size = 'thumbnail', $icon = false ) {974 function wp_get_attachment_image_src( $attachment, $size = 'thumbnail', $icon = false ) { 953 975 // Get a thumbnail or intermediate image if there is one. 954 $image = image_downsize( $attachment_id, $size ); 976 $image = image_downsize( $attachment, $size ); 977 978 // Ensure backward compatibility whenever a WP_Post object is passed. 979 $attachment_id = $attachment; 980 if ( is_a( $attachment, 'WP_Post' ) ) { 981 $attachment_id = $attachment->ID; 982 } 983 955 984 if ( ! $image ) { 956 985 $src = false; 957 986 … … 1004 1033 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added. 1005 1034 * @since 5.5.0 The `$loading` attribute was added. 1006 1035 * @since 6.1.0 The `$decoding` attribute was added. 1036 * @since 6.3.0 Allow WP_Post object to be passed. 1007 1037 * 1008 * @param int $attachment_id Imageattachment ID.1009 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array1010 * 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.1038 * @param int|WP_Post $attachment Image attachment WP_Post object or attachment ID. 1039 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array 1040 * of width and height values in pixels (in that order). Default 'thumbnail'. 1041 * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. 1012 1042 * @param string|array $attr { 1013 1043 * Optional. Attributes for the image markup. 1014 1044 * … … 1028 1058 * } 1029 1059 * @return string HTML img element or empty string on failure. 1030 1060 */ 1031 function wp_get_attachment_image( $attachment _id, $size = 'thumbnail', $icon = false, $attr = '' ) {1061 function wp_get_attachment_image( $attachment, $size = 'thumbnail', $icon = false, $attr = '' ) { 1032 1062 $html = ''; 1033 $image = wp_get_attachment_image_src( $attachment _id, $size, $icon );1063 $image = wp_get_attachment_image_src( $attachment, $size, $icon ); 1034 1064 1065 // Ensure backward compatibility whenever a WP_Post object is passed. 1066 $attachment_id = $attachment; 1067 if ( is_a( $attachment, 'WP_Post' ) ) { 1068 $attachment_id = $attachment->ID; 1069 } 1070 1035 1071 if ( $image ) { 1036 1072 list( $src, $width, $height ) = $image; 1037 1073 1038 $attachment = get_post( $attachment_id ); 1039 $hwstring = image_hwstring( $width, $height ); 1040 $size_class = $size; 1074 $attachment = get_post( $attachment ); 1075 $attachment_id = ( $attachment ) ? $attachment->ID : 0; 1076 $hwstring = image_hwstring( $width, $height ); 1077 $size_class = $size; 1041 1078 1042 1079 if ( is_array( $size_class ) ) { 1043 1080 $size_class = implode( 'x', $size_class ); … … 1070 1107 1071 1108 // Generate 'srcset' and 'sizes' if not already present. 1072 1109 if ( empty( $attr['srcset'] ) ) { 1073 $image_meta = wp_get_attachment_metadata( $attachment _id);1110 $image_meta = wp_get_attachment_metadata( $attachment ); 1074 1111 1075 1112 if ( is_array( $image_meta ) ) { 1076 1113 $size_array = array( absint( $width ), absint( $height ) ); 1077 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment _id);1078 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment _id);1114 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment ); 1115 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment ); 1079 1116 1080 1117 if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { 1081 1118 $attr['srcset'] = $srcset; … … 1205 1242 * Retrieves the value for an image attachment's 'srcset' attribute. 1206 1243 * 1207 1244 * @since 4.4.0 1245 * @since 6.3.0 Allow WP_Post object to be passed. 1208 1246 * 1209 1247 * @see wp_calculate_image_srcset() 1210 1248 * 1211 * @param int $attachment_idImage attachment ID.1212 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of1213 * width and height values in pixels (in that order). Default 'medium'.1214 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.1215 * Default null.1249 * @param int|WP_Post $attachment Image attachment ID. 1250 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 1251 * width and height values in pixels (in that order). Default 'medium'. 1252 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 1253 * Default null. 1216 1254 * @return string|false A 'srcset' value string or false. 1217 1255 */ 1218 function wp_get_attachment_image_srcset( $attachment _id, $size = 'medium', $image_meta = null ) {1219 $image = wp_get_attachment_image_src( $attachment _id, $size );1256 function wp_get_attachment_image_srcset( $attachment, $size = 'medium', $image_meta = null ) { 1257 $image = wp_get_attachment_image_src( $attachment, $size ); 1220 1258 1221 1259 if ( ! $image ) { 1222 1260 return false; … … 1223 1261 } 1224 1262 1225 1263 if ( ! is_array( $image_meta ) ) { 1226 $image_meta = wp_get_attachment_metadata( $attachment _id);1264 $image_meta = wp_get_attachment_metadata( $attachment ); 1227 1265 } 1228 1266 1229 1267 $image_src = $image[0]; … … 1232 1270 absint( $image[2] ), 1233 1271 ); 1234 1272 1273 // Ensure backward compatibility whenever a WP_Post object is passed. 1274 $attachment_id = $attachment; 1275 if ( is_a( $attachment, 'WP_Post' ) ) { 1276 $attachment_id = $attachment->ID; 1277 } 1278 1235 1279 return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); 1236 1280 } 1237 1281 … … 1446 1490 * Retrieves the value for an image attachment's 'sizes' attribute. 1447 1491 * 1448 1492 * @since 4.4.0 1493 * @since 6.3.0 Allow WP_Post object to be passed. 1449 1494 * 1450 1495 * @see wp_calculate_image_sizes() 1451 1496 * 1452 * @param int $attachment_id Imageattachment ID.1453 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of1454 * width and height values in pixels (in that order). Default 'medium'.1455 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.1456 * Default null.1497 * @param int|WP_Post $attachment Image attachment post object or attachment ID. 1498 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of 1499 * width and height values in pixels (in that order). Default 'medium'. 1500 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 1501 * Default null. 1457 1502 * @return string|false A valid source size value for use in a 'sizes' attribute or false. 1458 1503 */ 1459 function wp_get_attachment_image_sizes( $attachment _id, $size = 'medium', $image_meta = null ) {1460 $image = wp_get_attachment_image_src( $attachment _id, $size );1504 function wp_get_attachment_image_sizes( $attachment, $size = 'medium', $image_meta = null ) { 1505 $image = wp_get_attachment_image_src( $attachment, $size ); 1461 1506 1462 1507 if ( ! $image ) { 1463 1508 return false; … … 1464 1509 } 1465 1510 1466 1511 if ( ! is_array( $image_meta ) ) { 1467 $image_meta = wp_get_attachment_metadata( $attachment _id);1512 $image_meta = wp_get_attachment_metadata( $attachment ); 1468 1513 } 1469 1514 1470 1515 $image_src = $image[0]; … … 1473 1518 absint( $image[2] ), 1474 1519 ); 1475 1520 1521 // Ensure backward compatibility whenever a WP_Post object is passed. 1522 $attachment_id = $attachment; 1523 if ( is_a( $attachment, 'WP_Post' ) ) { 1524 $attachment_id = $attachment->ID; 1525 } 1526 1476 1527 return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); 1477 1528 } 1478 1529 -
src/wp-includes/post.php
6510 6510 * 6511 6511 * @since 2.1.0 6512 6512 * @since 6.0.0 The `$filesize` value was added to the returned array. 6513 * @since 6.3.0 allow WP_Post object to be passed. 6513 6514 * 6514 * @param int $attachment_id Attachment post ID. Defaults to global $post.6515 * @param bool $unfilteredOptional. If true, filters are not run. Default false.6515 * @param int|WP_Post $attachment Attachment post object or attachment ID. Defaults to global $post. 6516 * @param bool $unfiltered Optional. If true, filters are not run. Default false. 6516 6517 * @return array|false { 6517 6518 * Attachment metadata. False on failure. 6518 6519 * … … 6525 6526 * @type int $filesize File size of the attachment. 6526 6527 * } 6527 6528 */ 6528 function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { 6529 $attachment_id = (int) $attachment_id; 6529 function wp_get_attachment_metadata( $attachment = 0, $unfiltered = false ) { 6530 6530 6531 if ( is_a( $attachment, 'WP_Post' ) ) { 6532 $attachment_id = $attachment->ID; 6533 } else { 6534 $attachment_id = (int) $attachment; 6535 } 6536 6531 6537 if ( ! $attachment_id ) { 6532 6538 $post = get_post(); 6533 6539 … … 6597 6603 * Retrieves the URL for an attachment. 6598 6604 * 6599 6605 * @since 2.1.0 6606 * @since 6.3.0 Allow WP_Post object to be passed. 6600 6607 * 6601 6608 * @global string $pagenow The filename of the current screen. 6602 6609 * 6603 * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.6610 * @param int|WP_Post $post Optional. Attachment post object or attachment ID. Defaults to global $post. 6604 6611 * @return string|false Attachment URL, otherwise false. 6605 6612 */ 6606 function wp_get_attachment_url( $ attachment_id= 0 ) {6613 function wp_get_attachment_url( $post = 0 ) { 6607 6614 global $pagenow; 6608 6615 6609 $attachment_id = (int) $attachment_id; 6616 if ( ! is_a( $post, 'WP_Post' ) ) { 6617 $attachment_id = (int) $post; 6618 $post = get_post( $attachment_id ); 6619 } 6610 6620 6611 $post = get_post( $attachment_id );6612 6613 6621 if ( ! $post ) { 6614 6622 return false; 6615 6623 } -
tests/phpunit/tests/attachment/passedPost.php
1 <?php 2 3 /** 4 * @group attachment 5 * @ticket 37255 6 */ 7 class Tests_Attachment_PassedPost extends WP_UnitTestCase { 8 protected static $a; 9 protected static $a_id; 10 11 public static function wpSetUpBeforeClass() { 12 self::$a_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/a2-small.jpg' ); 13 self::$a = get_post( self::$a_id ); 14 } 15 16 public function test_wp_get_attachment_url_should_accept_post_id() { 17 $str = wp_get_attachment_url( self::$a_id ); 18 $this->assertNotEmpty( $str ); 19 } 20 21 public function test_wp_get_attachment_url_should_accept_post_object() { 22 $str = wp_get_attachment_url( self::$a ); 23 $this->assertNotEmpty( $str ); 24 } 25 26 public function test_wp_get_attachment_metadata_should_accept_post_id() { 27 $str = wp_get_attachment_metadata( self::$a_id ); 28 $this->assertNotEmpty( $str ); 29 } 30 31 public function test_wp_get_attachment_metadata_should_accept_post_object() { 32 $str = wp_get_attachment_metadata( self::$a ); 33 $this->assertNotEmpty( $str ); 34 } 35 36 public function test_image_downsize_should_accept_post_id() { 37 $arr = image_downsize( self::$a_id ); 38 $this->assertNotEmpty( $arr ); 39 } 40 41 public function test_image_downsize_should_accept_post_object() { 42 $arr = image_downsize( self::$a ); 43 $this->assertNotEmpty( $arr ); 44 } 45 46 public function test_get_image_tag_should_accept_post_id() { 47 $html = get_image_tag( self::$a_id, 'foo', 'bar', 'center' ); 48 $this->assertNotEmpty( $html ); 49 } 50 51 public function test_get_image_tag_should_accept_post_object() { 52 $html = get_image_tag( self::$a, 'foo', 'bar', 'center' ); 53 $this->assertNotEmpty( $html ); 54 } 55 56 public function test_image_get_intermediate_size_should_accept_post_id() { 57 $arr = image_get_intermediate_size( self::$a_id ); 58 $this->assertNotEmpty( $arr ); 59 } 60 61 public function test_image_get_intermediate_size_should_accept_post_object() { 62 $arr = image_get_intermediate_size( self::$a ); 63 $this->assertNotEmpty( $arr ); 64 } 65 66 public function test_wp_get_attachment_image_src_should_accept_post_id() { 67 $arr = wp_get_attachment_image_src( self::$a_id ); 68 $this->assertNotEmpty( $arr ); 69 } 70 71 public function test_wp_get_attachment_image_src_should_accept_post_object() { 72 $arr = wp_get_attachment_image_src( self::$a ); 73 $this->assertNotEmpty( $arr ); 74 } 75 76 public function test_wp_get_attachment_image_should_accept_post_id() { 77 $html = wp_get_attachment_image( self::$a_id ); 78 $this->assertNotEmpty( $html ); 79 } 80 81 public function test_wp_get_attachment_image_should_accept_post_object() { 82 $html = wp_get_attachment_image( self::$a ); 83 $this->assertNotEmpty( $html ); 84 } 85 86 public function test_wp_get_attachment_image_url_should_accept_post_id() { 87 $str = wp_get_attachment_image_url( self::$a_id ); 88 $this->assertNotEmpty( $str ); 89 } 90 91 public function test_wp_get_attachment_image_url_should_accept_post_object() { 92 $str = wp_get_attachment_image_url( self::$a ); 93 $this->assertNotEmpty( $str ); 94 } 95 96 public function test_wp_get_attachment_image_srcset_should_accept_post_id() { 97 $str = wp_get_attachment_image_srcset( self::$a_id ); 98 $this->assertNotEmpty( $str ); 99 } 100 101 public function test_wp_get_attachment_image_srcset_should_accept_post_object() { 102 $str = wp_get_attachment_image_srcset( self::$a ); 103 $this->assertNotEmpty( $str ); 104 } 105 106 public function test_wp_get_attachment_image_sizes_should_accept_post_id() { 107 $str = wp_get_attachment_image_sizes( self::$a_id ); 108 $this->assertNotEmpty( $str ); 109 } 110 111 public function test_wp_get_attachment_image_sizes_should_accept_post_object() { 112 $str = wp_get_attachment_image_sizes( self::$a ); 113 $this->assertNotEmpty( $str ); 114 } 115 116 } 117 No newline at end of file