Make WordPress Core

Ticket #37255: 37255.6.diff

File 37255.6.diff, 21.5 KB (added by Howdy_McGee, 3 years ago)

Patch refreshed with tests.

  • src/wp-includes/media.php

     
    175175 * elements that are normally returned from the function.
    176176 *
    177177 * @since 2.5.0
     178 * @since 6.3.0 Allow WP_Post object to be passed.
    178179 *
    179  * @param int          $id   Attachment ID for image.
    180  * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
    181  *                           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'.
    182183 * @return array|false {
    183184 *     Array of image data, or boolean false if no image is available.
    184185 *
     
    188189 *     @type bool   $3 Whether the image is a resized image.
    189190 * }
    190191 */
    191 function image_downsize( $id, $size = 'medium' ) {
    192         $is_image = wp_attachment_is_image( $id );
     192function image_downsize( $attachment, $size = 'medium' ) {
     193        $is_image = wp_attachment_is_image( $attachment );
    193194
     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
    194201        /**
    195202         * Filters whether to preempt the output of image_downsize().
    196203         *
     
    361368 * content.
    362369 *
    363370 * @since 2.5.0
     371 * @since 6.3.0 Allow WP_Post object to be passed.
    364372 *
    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 of
    370  *                            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'.
    371379 * @return string HTML IMG element for given image attachment?
    372380 */
    373 function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
     381function get_image_tag( $attachment, $alt, $title, $align, $size = 'medium' ) {
    374382
    375         list( $img_src, $width, $height ) = image_downsize( $id, $size );
     383        list( $img_src, $width, $height ) = image_downsize( $attachment, $size );
    376384        $hwstring                         = image_hwstring( $width, $height );
    377385
     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
    378392        $title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';
    379393
    380394        $size_class = is_array( $size ) ? implode( 'x', $size ) : $size;
     
    741755 * browser scale down the image.
    742756 *
    743757 * @since 2.5.0
     758 * @since 6.3.0 Allow WP_Post object to be passed.
    744759 *
    745  * @param int          $post_id Attachment ID.
    746  * @param string|int[] $size    Optional. Image size. Accepts any registered image size name, or an array
    747  *                              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'.
    748763 * @return array|false {
    749764 *     Array of file relative path, width, and height on success. Additionally includes absolute
    750765 *     path and URL if registered size is passed to `$size` parameter. False on failure.
     
    756771 *     @type string $url    URL of image.
    757772 * }
    758773 */
    759 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
    760         $imagedata = wp_get_attachment_metadata( $post_id );
     774function image_get_intermediate_size( $attachment, $size = 'thumbnail' ) {
     775        $imagedata = wp_get_attachment_metadata( $attachment );
    761776
    762777        if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
    763778                return false;
     
    828843
    829844        // Include the full filesystem path of the intermediate file.
    830845        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 );
    832847                $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] );
    833848                $data['url']  = path_join( dirname( $file_url ), $data['file'] );
    834849        }
    835850
     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
    836857        /**
    837858         * Filters the output of image_get_intermediate_size()
    838859         *
     
    840861         *
    841862         * @see image_get_intermediate_size()
    842863         *
    843          * @param array        $data    Array of file relative path, width, and height on success. May also include
    844          *                              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, or
    847          *                              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).
    848869         */
    849         return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
     870        return apply_filters( 'image_get_intermediate_size', $data, $attachment_id, $size );
    850871}
    851872
    852873/**
     
    935956 * Retrieves an image to represent an attachment.
    936957 *
    937958 * @since 2.5.0
     959 * @since 6.3.0 Allow WP_Post object to be passed.
    938960 *
    939  * @param int          $attachment_id Image attachment ID.
    940  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
    941  *                                    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.
    943965 * @return array|false {
    944966 *     Array of image data, or boolean false if no image is available.
    945967 *
     
    949971 *     @type bool   $3 Whether the image is a resized image.
    950972 * }
    951973 */
    952 function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
     974function wp_get_attachment_image_src( $attachment, $size = 'thumbnail', $icon = false ) {
    953975        // 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
    955984        if ( ! $image ) {
    956985                $src = false;
    957986
     
    10041033 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added.
    10051034 * @since 5.5.0 The `$loading` attribute was added.
    10061035 * @since 6.1.0 The `$decoding` attribute was added.
     1036 * @since 6.3.0 Allow WP_Post object to be passed.
    10071037 *
    1008  * @param int          $attachment_id Image attachment ID.
    1009  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array
    1010  *                                    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.
    10121042 * @param string|array $attr {
    10131043 *     Optional. Attributes for the image markup.
    10141044 *
     
    10281058 * }
    10291059 * @return string HTML img element or empty string on failure.
    10301060 */
    1031 function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
     1061function wp_get_attachment_image( $attachment, $size = 'thumbnail', $icon = false, $attr = '' ) {
    10321062        $html  = '';
    1033         $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
     1063        $image = wp_get_attachment_image_src( $attachment, $size, $icon );
    10341064
     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
    10351071        if ( $image ) {
    10361072                list( $src, $width, $height ) = $image;
    10371073
    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;
    10411078
    10421079                if ( is_array( $size_class ) ) {
    10431080                        $size_class = implode( 'x', $size_class );
     
    10701107
    10711108                // Generate 'srcset' and 'sizes' if not already present.
    10721109                if ( empty( $attr['srcset'] ) ) {
    1073                         $image_meta = wp_get_attachment_metadata( $attachment_id );
     1110                        $image_meta = wp_get_attachment_metadata( $attachment );
    10741111
    10751112                        if ( is_array( $image_meta ) ) {
    10761113                                $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 );
    10791116
    10801117                                if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
    10811118                                        $attr['srcset'] = $srcset;
     
    12051242 * Retrieves the value for an image attachment's 'srcset' attribute.
    12061243 *
    12071244 * @since 4.4.0
     1245 * @since 6.3.0 Allow WP_Post object to be passed.
    12081246 *
    12091247 * @see wp_calculate_image_srcset()
    12101248 *
    1211  * @param int          $attachment_id Image attachment ID.
    1212  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
    1213  *                                    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.
    12161254 * @return string|false A 'srcset' value string or false.
    12171255 */
    1218 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
    1219         $image = wp_get_attachment_image_src( $attachment_id, $size );
     1256function wp_get_attachment_image_srcset( $attachment, $size = 'medium', $image_meta = null ) {
     1257        $image = wp_get_attachment_image_src( $attachment, $size );
    12201258
    12211259        if ( ! $image ) {
    12221260                return false;
     
    12231261        }
    12241262
    12251263        if ( ! is_array( $image_meta ) ) {
    1226                 $image_meta = wp_get_attachment_metadata( $attachment_id );
     1264                $image_meta = wp_get_attachment_metadata( $attachment );
    12271265        }
    12281266
    12291267        $image_src  = $image[0];
     
    12321270                absint( $image[2] ),
    12331271        );
    12341272
     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
    12351279        return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
    12361280}
    12371281
     
    14461490 * Retrieves the value for an image attachment's 'sizes' attribute.
    14471491 *
    14481492 * @since 4.4.0
     1493 * @since 6.3.0 Allow WP_Post object to be passed.
    14491494 *
    14501495 * @see wp_calculate_image_sizes()
    14511496 *
    1452  * @param int          $attachment_id Image attachment ID.
    1453  * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
    1454  *                                    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.
    14571502 * @return string|false A valid source size value for use in a 'sizes' attribute or false.
    14581503 */
    1459 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
    1460         $image = wp_get_attachment_image_src( $attachment_id, $size );
     1504function wp_get_attachment_image_sizes( $attachment, $size = 'medium', $image_meta = null ) {
     1505        $image = wp_get_attachment_image_src( $attachment, $size );
    14611506
    14621507        if ( ! $image ) {
    14631508                return false;
     
    14641509        }
    14651510
    14661511        if ( ! is_array( $image_meta ) ) {
    1467                 $image_meta = wp_get_attachment_metadata( $attachment_id );
     1512                $image_meta = wp_get_attachment_metadata( $attachment );
    14681513        }
    14691514
    14701515        $image_src  = $image[0];
     
    14731518                absint( $image[2] ),
    14741519        );
    14751520
     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
    14761527        return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
    14771528}
    14781529
  • src/wp-includes/post.php

     
    65106510 *
    65116511 * @since 2.1.0
    65126512 * @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.
    65136514 *
    6514  * @param int  $attachment_id Attachment post ID. Defaults to global $post.
    6515  * @param bool $unfiltered    Optional. 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.
    65166517 * @return array|false {
    65176518 *     Attachment metadata. False on failure.
    65186519 *
     
    65256526 *     @type int    $filesize   File size of the attachment.
    65266527 * }
    65276528 */
    6528 function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
    6529         $attachment_id = (int) $attachment_id;
     6529function wp_get_attachment_metadata( $attachment = 0, $unfiltered = false ) {
    65306530
     6531        if ( is_a( $attachment, 'WP_Post' ) ) {
     6532                $attachment_id = $attachment->ID;
     6533        } else {
     6534                $attachment_id = (int) $attachment;
     6535        }
     6536
    65316537        if ( ! $attachment_id ) {
    65326538                $post = get_post();
    65336539
     
    65976603 * Retrieves the URL for an attachment.
    65986604 *
    65996605 * @since 2.1.0
     6606 * @since 6.3.0 Allow WP_Post object to be passed.
    66006607 *
    66016608 * @global string $pagenow The filename of the current screen.
    66026609 *
    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.
    66046611 * @return string|false Attachment URL, otherwise false.
    66056612 */
    6606 function wp_get_attachment_url( $attachment_id = 0 ) {
     6613function wp_get_attachment_url( $post = 0 ) {
    66076614        global $pagenow;
    66086615
    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        }
    66106620
    6611         $post = get_post( $attachment_id );
    6612 
    66136621        if ( ! $post ) {
    66146622                return false;
    66156623        }
  • tests/phpunit/tests/attachment/passedPost.php

     
     1<?php
     2
     3/**
     4 * @group attachment
     5 * @ticket 37255
     6 */
     7class 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