WordPress.org

Make WordPress Core

Ticket #17262: 17262.10.diff

File 17262.10.diff, 6.3 KB (added by killua99, 18 months ago)

Adding filter to the new function and moving deprecated function to the right file.

  • src/wp-admin/includes/deprecated.php

    diff --git a/src/wp-admin/includes/deprecated.php b/src/wp-admin/includes/deprecated.php
    index e65fe5a79c..e49d8acb10 100644
    a b function options_permalink_add_js() { 
    15141514        </script>
    15151515        <?php
    15161516}
     1517
     1518
     1519/**
     1520 * Retrieve thumbnail for an attachment.
     1521 *
     1522 * @since 2.1.0
     1523 * @deprecated 5.2 Use wp_get_attachment_image_file( $attachment_id, $size = 'thumbnail' )
     1524 *
     1525 * @param int $post_id Optional. Attachment ID. Default 0.
     1526 * @return string|false False on failure. Thumbnail file path on success.
     1527 */
     1528function wp_get_attachment_thumb_file( $post_id = 0 ) {
     1529        _deprecated_function( __FUNCTION__, '5.2' );
     1530
     1531        $file = wp_get_attachment_image_file( $post_id );
     1532
     1533        if ( $file ) {
     1534                /**
     1535                 * Filters the attachment thumbnail file path.
     1536                 *
     1537                 * @since 2.1.0
     1538                 * @deprecated 5.2 Use filter wp_get_attachment_image_file( $file, $attachment_id, $size )
     1539                 *
     1540                 * @param string $thumbfile File path to the attachment thumbnail.
     1541                 * @param int    $post_id   Attachment ID.
     1542                 */
     1543                return apply_filters( 'wp_get_attachment_thumb_file', $file, $post->ID );
     1544        }
     1545        return false;
     1546}
  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index 019086c3b8..578e99713d 100644
    a b function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon 
    945945        return isset( $image['0'] ) ? $image['0'] : false;
    946946}
    947947
     948/**
     949 * Get the size path of an image attachment.
     950 *
     951 * @since 5.2
     952 *
     953 * @param int          $attachment_id Attachment ID.
     954 * @param string|array $size          Optional. Image size to retrieve. Accepts any valid image size, or an array
     955 *                                    of width and height values. Default 'thumbnail'
     956 *
     957 * @return string|null Attachment path or null if no image is available.
     958 */
     959function wp_get_attachment_image_file( $attachment_id, $size = 'thumbnail' ) {
     960
     961        if ( 0 === (int) $attachment_id ) {
     962                return null;
     963        }
     964
     965        $attachment_metadata = wp_get_attachment_metadata( $attachment_id );
     966
     967        if ( ! $attachment_metadata ) {
     968                return null;
     969        }
     970
     971        $file_name = isset( $attachment_metadata['sizes'][ $size ]['file'] ) ? $attachment_metadata['sizes'][ $size ]['file'] : false;
     972
     973        $file = dirname( $attachment_metadata['file'] ) . '/' . $file_name;
     974
     975        $file_path = _wp_get_attachment_relative_path( $file );
     976
     977        if ( empty( $file_path ) ) {
     978                return null;
     979        }
     980
     981        $wp_upload_dir = wp_upload_dir( null, null );
     982
     983        $path = $wp_upload_dir['basedir'] . '/' . trailingslashit( $file_path ) . $file_name;
     984
     985        /**
     986         * Filters the attachment image file path.
     987         *
     988         * @since 5.2
     989         *
     990         * @param string $path          File path to the image file.
     991         * @param int    $attachment_id Attachment ID.
     992         * @param string $size          Image size used to get the size.
     993         */
     994        return apply_filters( 'wp_get_attachment_image_file', $path, $attachment_id, $size );
     995}
     996
     997
    948998/**
    949999 * Get the attachment path relative to the upload directory.
    9501000 *
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index eb69ac727b..27873153ce 100644
    a b function wp_get_attachment_caption( $post_id = 0 ) { 
    56855685        return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
    56865686}
    56875687
    5688 /**
    5689  * Retrieve thumbnail for an attachment.
    5690  *
    5691  * @since 2.1.0
    5692  *
    5693  * @param int $post_id Optional. Attachment ID. Default 0.
    5694  * @return string|false False on failure. Thumbnail file path on success.
    5695  */
    5696 function wp_get_attachment_thumb_file( $post_id = 0 ) {
    5697         $post_id = (int) $post_id;
    5698         if ( ! $post = get_post( $post_id ) ) {
    5699                 return false;
    5700         }
    5701         if ( ! is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) {
    5702                 return false;
    5703         }
    5704 
    5705         $file = get_attached_file( $post->ID );
    5706 
    5707         if ( ! empty( $imagedata['thumb'] ) && ( $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ) ) && file_exists( $thumbfile ) ) {
    5708                 /**
    5709                  * Filters the attachment thumbnail file path.
    5710                  *
    5711                  * @since 2.1.0
    5712                  *
    5713                  * @param string $thumbfile File path to the attachment thumbnail.
    5714                  * @param int    $post_id   Attachment ID.
    5715                  */
    5716                 return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );
    5717         }
    5718         return false;
    5719 }
    5720 
    57215688/**
    57225689 * Retrieve URL for an attachment thumbnail.
    57235690 *
  • new file tests/phpunit/tests/media/getImageAttachmentSize.php

    diff --git a/tests/phpunit/tests/media/getImageAttachmentSize.php b/tests/phpunit/tests/media/getImageAttachmentSize.php
    new file mode 100644
    index 0000000000..37f2162f53
    - +  
     1<?php
     2
     3/**
     4 * @group media
     5 */
     6class Tests_Media_GetImageAttachmentSize extends WP_UnitTestCase {
     7
     8        /** @var array WP Upload Dir. */
     9        protected $wp_upload_dir;
     10
     11        public function setUp() {
     12                $this->wp_upload_dir = wp_upload_dir();
     13
     14                parent::setUp();
     15        }
     16
     17        /**
     18         * Creating file.
     19         *
     20         * @param string $file_name Image file name.
     21         *
     22         * @return int Attachment ID.
     23         */
     24        protected function upload_image( $file_name ) {
     25                $post_id = self::factory()->post->create( [ 'post_author' => 1 ] );
     26
     27                \copy( DIR_TESTDATA . '/images/' . $file_name, $this->wp_upload_dir['path'] . '/' . $file_name );
     28
     29                $file = $this->wp_upload_dir['path'] . '/' . $file_name;
     30
     31                $wp_filetype = wp_check_filetype( basename( $file ), null );
     32
     33                $attachment_id = wp_insert_attachment(
     34                        [
     35                                'post_mime_type' => $wp_filetype['type'],
     36                                'post_status'    => 'inherit',
     37                                'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $file ) ),
     38                                'post_content'   => '',
     39                        ],
     40                        $file,
     41                        $post_id
     42                );
     43
     44                $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file );
     45
     46                wp_update_attachment_metadata( $attachment_id, $attachment_data, $post_id );
     47
     48                return $attachment_id;
     49        }
     50
     51        public function test_get_attachment_image_size() {
     52                $attachment_id = $this->upload_image( 'waffles.jpg' );
     53
     54                $file_size = wp_get_attachment_image_file( $attachment_id, 'medium' );
     55
     56                $this->assertContains( 'waffles-300x200.jpg', $file_size );
     57        }
     58
     59        public function test_get_custom_attachment_image_size() {
     60                add_image_size( 'custom', 400, 400, true );
     61
     62                $attachment_id = $this->upload_image( 'canola.jpg' );
     63
     64                $file_size = wp_get_attachment_image_file( $attachment_id, 'custom' );
     65
     66                $this->assertContains( 'canola-400x400.jpg', $file_size );
     67        }
     68}