Make WordPress Core

Ticket #37255: 37255.5.diff

File 37255.5.diff, 20.8 KB (added by Howdy_McGee, 5 years ago)

Patch Refreshed

  • media.php

     
    175175 * elements that are normally returned from the function.
    176176 *
    177177 * @since 2.5.0
     178 * @since 5.6.0 allow WP_Post object to be passed.
    178179 *
    179  * @param int          $id   Attachment ID for image.
     180 * @param int|WP_Post  $attachment Attachment post or attachment ID for image.
    180181 * @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
    181182 *                           of width and height values in pixels (in that order). Default 'medium'.
    182183 * @return array|false {
     
    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' ) {
    193193
     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
    194202        /**
    195203         * Filters whether to preempt the output of image_downsize().
    196204         *
     
    198206         * down-sizing the image, returning that value instead.
    199207         *
    200208         * @since 2.5.0
     209         * @since 5.6.0 add $post parameter.
    201210         *
    202211         * @param bool|array   $downsize Whether to short-circuit the image downsize.
    203212         * @param int          $id       Attachment ID for image.
    204213         * @param string|int[] $size     Requested image size. Can be any registered image size name, or
    205214         *                               an array of width and height values in pixels (in that order).
     215         * @param WP_Post      $post     Attachment post object.
    206216         */
    207         $out = apply_filters( 'image_downsize', false, $id, $size );
     217        $out = apply_filters( 'image_downsize', false, $post->ID, $size, $post );
    208218
    209219        if ( $out ) {
    210220                return $out;
    211221        }
    212222
    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 );
    215225        $width            = 0;
    216226        $height           = 0;
    217227        $is_intermediate  = false;
     
    231241        }
    232242
    233243        // Try for a new style intermediate size.
    234         $intermediate = image_get_intermediate_size( $id, $size );
     244        $intermediate = image_get_intermediate_size( $post, $size );
    235245
    236246        if ( $intermediate ) {
    237247                $img_url         = str_replace( $img_url_basename, $intermediate['file'], $img_url );
     
    240250                $is_intermediate = true;
    241251        } elseif ( 'thumbnail' === $size ) {
    242252                // Fall back to the old thumbnail.
    243                 $thumb_file = wp_get_attachment_thumb_file( $id );
     253                $thumb_file = wp_get_attachment_thumb_file( $post );
    244254                $info       = null;
    245255
    246256                if ( $thumb_file ) {
     
    361371 * content.
    362372 *
    363373 * @since 2.5.0
     374 * @since 5.6.0 allow WP_Post object to be passed.
    364375 *
    365  * @param int          $id    Attachment ID.
     376 * @param int|WP_Post  $attachment Attachment post or post ID.
    366377 * @param string       $alt   Image description for the alt attribute.
    367378 * @param string       $title Image description for the title attribute.
    368379 * @param string       $align Part of the class name for aligning the image.
    369380 * @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'.
    371382 * @return string HTML IMG element for given image attachment
    372383 */
    373 function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
     384function get_image_tag( $attachment, $alt, $title, $align, $size = 'medium' ) {
    374385
     386        $post = get_post( $attachment );
     387
     388        if ( ! $post || 'attachment' !== $post->post_type ) {
     389                return '';
     390        }
     391
     392        $id = $post->ID;
    375393        list( $img_src, $width, $height ) = image_downsize( $id, $size );
    376394        $hwstring                         = image_hwstring( $width, $height );
    377395
     
    384402         * Filters the value of the attachment's image tag class attribute.
    385403         *
    386404         * @since 2.6.0
     405         * @since 5.6.0 add $post parameter.
    387406         *
    388407         * @param string       $class CSS class name or space-separated list of classes.
    389408         * @param int          $id    Attachment ID.
     
    390409         * @param string       $align Part of the class name for aligning the image.
    391410         * @param string|int[] $size  Requested image size. Can be any registered image size name, or
    392411         *                            an array of width and height values in pixels (in that order).
     412         * @param WP_Post      $post  Attachment post object.
    393413         */
    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 );
    395415
    396416        $html = '<img src="' . esc_attr( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
    397417
     
    399419         * Filters the HTML content for the image tag.
    400420         *
    401421         * @since 2.6.0
     422         * @since 5.6.0 add $post parameter.
    402423         *
    403424         * @param string       $html  HTML content for the image.
    404425         * @param int          $id    Attachment ID.
     
    407428         * @param string       $align Part of the class name for aligning the image.
    408429         * @param string|int[] $size  Requested image size. Can be any registered image size name, or
    409430         *                            an array of width and height values in pixels (in that order).
     431         * @param WP_Post      $post  Attachment post object.
    410432         */
    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 );
    412434}
    413435
    414436/**
     
    741763 * browser scale down the image.
    742764 *
    743765 * @since 2.5.0
     766 * @since 5.6.0 allow WP_Post object to be passed.
    744767 *
    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'.
     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'.
    748771 * @return array|false {
    749772 *     Array of file relative path, width, and height on success. Additionally includes absolute
    750773 *     path and URL if registered size is passed to `$size` parameter. False on failure.
     
    756779 *     @type string $url    URL of image.
    757780 * }
    758781 */
    759 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
    760         $imagedata = wp_get_attachment_metadata( $post_id );
     782function image_get_intermediate_size( $attachment, $size = 'thumbnail' ) {
    761783
    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 ) {
    763788                return false;
    764789        }
    765790
    766         $data = array();
     791        $imagedata = wp_get_attachment_metadata( $post );
    767792
     793        if ( ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
     794                return false;
     795        }
     796
    768797        // Find the best match when '$size' is an array.
    769798        if ( is_array( $size ) ) {
    770799                $candidates = array();
     
    828857
    829858        // Include the full filesystem path of the intermediate file.
    830859        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 );
    832861                $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] );
    833862                $data['url']  = path_join( dirname( $file_url ), $data['file'] );
    834863        }
     
    837866         * Filters the output of image_get_intermediate_size()
    838867         *
    839868         * @since 4.4.0
     869         * @since 5.6.0 add $post parameter.
    840870         *
    841871         * @see image_get_intermediate_size()
    842872         *
    843873         * @param array        $data    Array of file relative path, width, and height on success. May also include
    844874         *                              file absolute path and URL.
    845          * @param int          $post_id The ID of the image attachment.
     875         * @param int                  The ID of the image attachment.
    846876         * @param string|int[] $size    Requested image size. Can be any registered image size name, or
    847877         *                              an array of width and height values in pixels (in that order).
     878         * @param WP_Post      $post    Attachment post object.
    848879         */
    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 );
    850881}
    851882
    852883/**
     
    934965 * Retrieves an image to represent an attachment.
    935966 *
    936967 * @since 2.5.0
     968 * @since 5.6.0 allow WP_Post object to be passed.
    937969 *
    938  * @param int          $attachment_id Image attachment ID.
     970 * @param int|WP_Post  $attachment    Attachment post or post ID.
    939971 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
    940972 *                                    width and height values in pixels (in that order). Default 'thumbnail'.
    941973 * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
     
    948980 *     @type bool   $3 Whether the image is a resized image.
    949981 * }
    950982 */
    951 function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
     983function 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
    952991        // Get a thumbnail or intermediate image if there is one.
     992        $attachment_id = $post->ID;
    953993        $image = image_downsize( $attachment_id, $size );
    954994        if ( ! $image ) {
    955995                $src = false;
     
    9741014         * Filters the attachment image source result.
    9751015         *
    9761016         * @since 4.3.0
     1017         * @since 5.6.0 add $post parameter.
    9771018         *
    9781019         * @param array|false  $image         {
    9791020         *     Array of image data, or boolean false if no image is available.
     
    9871028         * @param string|int[] $size          Requested image size. Can be any registered image size name, or
    9881029         *                                    an array of width and height values in pixels (in that order).
    9891030         * @param bool         $icon          Whether the image should be treated as an icon.
     1031         * @param WP_Post      $post          Attachment post object.
    9901032         */
    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 );
    9921034}
    9931035
    9941036/**
     
    10021044 * @since 2.5.0
    10031045 * @since 4.4.0 The `$srcset` and `$sizes` attributes were added.
    10041046 * @since 5.5.0 The `$loading` attribute was added.
     1047 * @since 5.6.0 allow WP_Post object to be passed.
    10051048 *
    1006  * @param int          $attachment_id Image attachment ID.
     1049 * @param int|WP_Post  $attachment    Attachment post or post ID.
    10071050 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array
    10081051 *                                    of width and height values in pixels (in that order). Default 'thumbnail'.
    10091052 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
     
    10231066 * }
    10241067 * @return string HTML img element or empty string on failure.
    10251068 */
    1026 function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
     1069function wp_get_attachment_image( $attachment, $size = 'thumbnail', $icon = false, $attr = '' ) {
    10271070        $html  = '';
    1028         $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
     1071        $image = wp_get_attachment_image_src( $attachment, $size, $icon );
    10291072
    10301073        if ( $image ) {
    10311074                list( $src, $width, $height ) = $image;
    10321075
    1033                 $attachment = get_post( $attachment_id );
     1076                $post       = get_post( $attachment );
    10341077                $hwstring   = image_hwstring( $width, $height );
    10351078                $size_class = $size;
    10361079
     
    10411084                $default_attr = array(
    10421085                        'src'   => $src,
    10431086                        '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 ) ) ),
    10451088                );
    10461089
    10471090                // Add `loading` attribute.
     
    10591102
    10601103                // Generate 'srcset' and 'sizes' if not already present.
    10611104                if ( empty( $attr['srcset'] ) ) {
    1062                         $image_meta = wp_get_attachment_metadata( $attachment_id );
     1105                        $image_meta = wp_get_attachment_metadata( $post->ID );
    10631106
    10641107                        if ( is_array( $image_meta ) ) {
    10651108                                $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 );
    10681111
    10691112                                if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
    10701113                                        $attr['srcset'] = $srcset;
     
    10831126                 *
    10841127                 * @param string[]     $attr       Array of attribute values for the image markup, keyed by attribute name.
    10851128                 *                                 See wp_get_attachment_image().
    1086                  * @param WP_Post      $attachment Image attachment post.
     1129                 * @param WP_Post      $post      Image attachment post.
    10871130                 * @param string|int[] $size       Requested image size. Can be any registered image size name, or
    10881131                 *                                 an array of width and height values in pixels (in that order).
    10891132                 */
    1090                 $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
     1133                $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $post, $size );
    10911134
    10921135                $attr = array_map( 'esc_attr', $attr );
    10931136                $html = rtrim( "<img $hwstring" );
     
    11121155         * @param string[]     $attr          Array of attribute values for the image markup, keyed by attribute name.
    11131156         *                                    See wp_get_attachment_image().
    11141157         */
    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 );
    11161159}
    11171160
    11181161/**
     
    11971240 *
    11981241 * @see wp_calculate_image_srcset()
    11991242 *
    1200  * @param int          $attachment_id Image attachment ID.
     1243 * @param int          $attachment    Attachment post or post ID.
    12011244 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
    12021245 *                                    width and height values in pixels (in that order). Default 'medium'.
    12031246 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
     
    12041247 *                                    Default null.
    12051248 * @return string|false A 'srcset' value string or false.
    12061249 */
    1207 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) {
    1208         $image = wp_get_attachment_image_src( $attachment_id, $size );
     1250function wp_get_attachment_image_srcset( $attachment, $size = 'medium', $image_meta = null ) {
     1251        $image = wp_get_attachment_image_src( $attachment, $size );
    12091252
    12101253        if ( ! $image ) {
    12111254                return false;
    12121255        }
    12131256
     1257        $post = get_post( $attachment );
     1258
    12141259        if ( ! is_array( $image_meta ) ) {
    1215                 $image_meta = wp_get_attachment_metadata( $attachment_id );
     1260                $image_meta = wp_get_attachment_metadata( $post->ID );
    12161261        }
    12171262
    12181263        $image_src  = $image[0];
     
    12211266                absint( $image[2] ),
    12221267        );
    12231268
    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 );
    12251270}
    12261271
    12271272/**
     
    14381483 *
    14391484 * @see wp_calculate_image_sizes()
    14401485 *
    1441  * @param int          $attachment_id Image attachment ID.
     1486 * @param int          $attachment    Attachment post or post ID.
    14421487 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
    14431488 *                                    width and height values in pixels (in that order). Default 'medium'.
    14441489 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
     
    14451490 *                                    Default null.
    14461491 * @return string|false A valid source size value for use in a 'sizes' attribute or false.
    14471492 */
    1448 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
    1449         $image = wp_get_attachment_image_src( $attachment_id, $size );
     1493function wp_get_attachment_image_sizes( $attachment, $size = 'medium', $image_meta = null ) {
     1494        $image = wp_get_attachment_image_src( $attachment, $size );
    14501495
    14511496        if ( ! $image ) {
    14521497                return false;
    14531498        }
    14541499
     1500        $post = get_post( $attachment );
     1501
    14551502        if ( ! is_array( $image_meta ) ) {
    1456                 $image_meta = wp_get_attachment_metadata( $attachment_id );
     1503                $image_meta = wp_get_attachment_metadata( $post->ID );
    14571504        }
    14581505
    14591506        $image_src  = $image[0];
     
    14621509                absint( $image[2] ),
    14631510        );
    14641511
    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 );
    14661513}
    14671514
    14681515/**
  • post.php

     
    62896289 * Retrieves attachment metadata for attachment ID.
    62906290 *
    62916291 * @since 2.1.0
     6292 * @since 5.6.0 allow WP_Post object to be passed.
    62926293 *
    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 *
    62956297 * @return array|false {
    62966298 *     Attachment metadata. False on failure.
    62976299 *
     
    63786380 *
    63796381 * @global string $pagenow
    63806382 *
    6381  * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
     6383 * @param int|WP_Post $attachment Attachment post or post ID. Defaults to global $post.
    63826384 * @return string|false Attachment URL, otherwise false.
    63836385 */
    6384 function wp_get_attachment_url( $attachment_id = 0 ) {
     6386function wp_get_attachment_url( $attachment = 0 ) {
    63856387        global $pagenow;
    63866388
    6387         $attachment_id = (int) $attachment_id;
     6389        $post = get_post( $attachment );
    63886390
    6389         $post = get_post( $attachment_id );
    6390 
    6391         if ( ! $post ) {
     6391        if( ! $post || 'attachment' !== $post->post_type ) {
    63926392                return false;
    63936393        }
    63946394
    6395         if ( 'attachment' !== $post->post_type ) {
    6396                 return false;
    6397         }
    6398 
    63996395        $url = '';
    64006396        // Get attached file.
    64016397        $file = get_post_meta( $post->ID, '_wp_attached_file', true );
     
    64346430         * Filters the attachment URL.
    64356431         *
    64366432         * @since 2.1.0
     6433         * @since 5.6.0 add $post parameter.
    64376434         *
    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.
    64406438         */
    6441         $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
     6439        $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID, $post );
    64426440
    64436441        if ( ! $url ) {
    64446442                return false;
     
    65106508                         * Filters the attachment thumbnail file path.
    65116509                         *
    65126510                         * @since 2.1.0
     6511                         * @since 5.6.0 add $post parameter.
    65136512                         *
    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.
    65166516                         */
    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 );
    65186518                }
    65196519        }
    65206520        return false;
     
    65416541                return false;
    65426542        }
    65436543
    6544         $sized = image_downsize( $post_id, 'thumbnail' );
     6544        $sized = image_downsize( $post->ID, 'thumbnail' );
    65456545        if ( $sized ) {
    65466546                return $sized[0];
    65476547        }
     
    65576557         * Filters the attachment thumbnail URL.
    65586558         *
    65596559         * @since 2.1.0
     6560         * @since 5.6.0 add $post parameter.
    65606561         *
    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.
    65636565         */
    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 );
    65656567}
    65666568
    65676569/**