Make WordPress Core

Ticket #17262: 17262.9.2.diff

File 17262.9.2.diff, 4.0 KB (added by killua99, 5 years ago)

Sorry, I didn't update the branch against trunk / master. Now is clean and updated to the latest trunk state.

  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index 019086c3b8..ecac41b7a2 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 * @param int          $attachment_id Attachment ID.
     952 * @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array
     953 *                           of width and height values. Default 'thumbnail'
     954 *
     955 * @return string|null Attachment path or null if no image is available.
     956 */
     957function wp_get_attachment_image_file( $attachment_id, $size = 'thumbnail' ) {
     958
     959        if ( 0 === (int) $attachment_id ) {
     960                return null;
     961        }
     962
     963        $attachment_metadata = wp_get_attachment_metadata( $attachment_id );
     964
     965        if ( ! $attachment_metadata ) {
     966                return null;
     967        }
     968
     969        $file_name = isset( $attachment_metadata['sizes'][ $size ]['file'] ) ? $attachment_metadata['sizes'][ $size ]['file'] : false;
     970
     971        $file = dirname( $attachment_metadata['file'] ) . '/' . $file_name;
     972
     973        $file_path = _wp_get_attachment_relative_path( $file );
     974
     975        if ( empty( $file_path ) ) {
     976                return null;
     977        }
     978
     979        $wp_upload_dir = wp_upload_dir( null, null );
     980
     981        $path = $wp_upload_dir['basedir'] . '/' . trailingslashit( $file_path ) . $file_name;
     982
     983        return $path;
     984
     985}
     986
     987
    948988/**
    949989 * Get the attachment path relative to the upload directory.
    950990 *
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index eb69ac727b..5250aaa294 100644
    a b function wp_get_attachment_caption( $post_id = 0 ) { 
    56895689 * Retrieve thumbnail for an attachment.
    56905690 *
    56915691 * @since 2.1.0
     5692 * @deprecated 5.2 Use wp_get_attachment_image_file( $attachment_id, $size = 'thumbnail' )
    56925693 *
    56935694 * @param int $post_id Optional. Attachment ID. Default 0.
    56945695 * @return string|false False on failure. Thumbnail file path on success.
  • 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}