WordPress.org

Make WordPress Core

Ticket #35106: 35106.diff

File 35106.diff, 9.6 KB (added by dd32, 6 years ago)
  • src/wp-includes/media.php

    function wp_get_attachment_image($attach 
    866866 *
    867867 * @since 4.4.0
    868868 *
    869869 * @param int          $attachment_id Image attachment ID.
    870870 * @param string|array $size          Optional. Image size to retrieve. Accepts any valid image size, or an array
    871871 *                                    of width and height values in pixels (in that order). Default 'thumbnail'.
    872872 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
    873873 * @return string|false Attachment URL or false if no image is available.
    874874 */
    875875function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
    876876        $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
    877877        return isset( $image['0'] ) ? $image['0'] : false;
    878878}
    879879
    880880/**
     881 * Get the attachment path relative to the upload directory.
     882 *
     883 * @since 4.4.1
     884 * @access private
     885 *
     886 * @param string $file Attachment file name.
     887 * @return string Attachment path relative to the upload directory.
     888 */
     889function _wp_get_attachment_relative_path( $file ) {
     890        $dirname = dirname( $file );
     891
     892        if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) {
     893                // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads)
     894                $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
     895                $dirname = ltrim( $dirname, '/' );
     896        }
     897
     898        if ( '.' === $dirname ) {
     899                return '';
     900        }
     901
     902        return $dirname;
     903}
     904
     905/**
    881906 * Caches and returns the base URL of the uploads directory.
    882907 *
    883908 * @since 4.4.0
    884909 * @access private
    885910 *
    886911 * @return string The base URL, cached.
    887912 */
    888913function _wp_upload_dir_baseurl() {
    889914        static $baseurl = array();
    890915
    891916        $blog_id = get_current_blog_id();
    892917
    893918        if ( empty( $baseurl[$blog_id] ) ) {
    894919                $uploads_dir = wp_upload_dir();
    895920                $baseurl[$blog_id] = $uploads_dir['baseurl'];
    function wp_calculate_image_srcset( $siz 
    9941019         * To avoid hiding animation in user content, if src is a full size GIF, a srcset attribute is not generated.
    9951020         * If src is an intermediate size GIF, the full size is excluded from srcset to keep a flattened GIF from becoming animated.
    9961021         */
    9971022        if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) {
    9981023                $image_sizes['full'] = array(
    9991024                        'width'  => $image_meta['width'],
    10001025                        'height' => $image_meta['height'],
    10011026                        'file'   => $image_basename,
    10021027                );
    10031028        } elseif ( strpos( $image_src, $image_meta['file'] ) ) {
    10041029                return false;
    10051030        }
    10061031
    10071032        // Uploads are (or have been) in year/month sub-directories.
    10081033        if ( $image_basename !== $image_meta['file'] ) {
    1009                 $dirname = dirname( $image_meta['file'] );
     1034                $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
    10101035
    1011                 if ( $dirname !== '.' ) {
     1036                if ( $dirname ) {
    10121037                        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10131038                }
    10141039        }
    10151040
    10161041        $image_baseurl = trailingslashit( $image_baseurl );
    10171042
    10181043        /*
    10191044         * Images that have been edited in WordPress after being uploaded will
    10201045         * contain a unique hash. Look for that hash and use it later to filter
    10211046         * out images that are leftovers from previous versions.
    10221047         */
    10231048        $image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash );
    10241049
    10251050        /**
    10261051         * Filter the maximum image width to be included in a 'srcset' attribute.
    function wp_image_add_srcset_and_sizes(  
    12771302        // Return early if we couldn't get the image source.
    12781303        if ( ! $image_src ) {
    12791304                return $image;
    12801305        }
    12811306
    12821307        // Bail early if an image has been inserted and later edited.
    12831308        if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
    12841309                strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    12851310
    12861311                return $image;
    12871312        }
    12881313
    12891314        $base_url = trailingslashit( _wp_upload_dir_baseurl() );
    12901315        $image_base_url = $base_url;
    12911316
    1292         $dirname = dirname( $image_meta['file'] );
    1293         if ( $dirname !== '.' ) {
     1317        $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     1318        if ( $dirname ) {
    12941319                $image_base_url .= trailingslashit( $dirname );
    12951320        }
    12961321
    12971322        $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
    12981323
    12991324        foreach ( $all_sizes as $key => $file ) {
    13001325                $all_sizes[ $key ] = $image_base_url . $file;
    13011326        }
    13021327
    13031328        // Add the original image.
    1304         $all_sizes[] = $base_url . $image_meta['file'];
     1329        $all_sizes[] = $image_base_url . basename( $image_meta['file'] );
    13051330
    13061331        // Bail early if the image src doesn't match any of the known image sizes.
    13071332        if ( ! in_array( $image_src, $all_sizes ) ) {
    13081333                return $image;
    13091334        }
    13101335
    13111336        $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : 0;
    13121337        $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
    13131338
    13141339        if ( ! $width || ! $height ) {
    13151340                /*
    13161341                 * If attempts to parse the size value failed, attempt to use the image meta data to match
    13171342                 * the image file name from 'src' against the available sizes for an attachment.
    13181343                 */
    13191344                $image_filename = wp_basename( $image_src );
  • src/wp-includes/post.php

    function wp_get_attachment_url( $post_id 
    48674867                return false;
    48684868
    48694869        if ( 'attachment' != $post->post_type )
    48704870                return false;
    48714871
    48724872        $url = '';
    48734873        // Get attached file.
    48744874        if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) {
    48754875                // Get upload directory.
    48764876                if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
    48774877                        // Check that the upload base exists in the file location.
    48784878                        if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
    48794879                                // Replace file location with url location.
    48804880                                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
    48814881                        } elseif ( false !== strpos($file, 'wp-content/uploads') ) {
    4882                                 $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
     4882                                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
     4883                                $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file );
    48834884                        } else {
    48844885                                // It's a newly-uploaded file, therefore $file is relative to the basedir.
    48854886                                $url = $uploads['baseurl'] . "/$file";
    48864887                        }
    48874888                }
    48884889        }
    48894890
    48904891        /*
    48914892         * If any of the above options failed, Fallback on the GUID as used pre-2.7,
    48924893         * not recommended to rely upon this.
    48934894         */
    48944895        if ( empty($url) ) {
    48954896                $url = get_the_guid( $post->ID );
    48964897        }
    48974898
  • tests/phpunit/tests/media.php

    EOF; 
    854854                $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] );
    855855                $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] );
    856856                $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] );
    857857                $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] );
    858858
    859859                // Calculate a srcset array.
    860860                $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    861861
    862862                // Test to confirm all sources in the array include the same edit hash.
    863863                foreach ( $sizes as $size ) {
    864864                        $this->assertTrue( false !== strpos( $size, $hash ) );
    865865                }
    866866        }
    867867
    868868        /**
     869         * @ticket 35106
     870         */
     871        function test_wp_calculate_image_srcset_with_absolute_path_in_meta() {
     872                global $_wp_additional_image_sizes;
     873
     874                $year_month = date('Y/m');
     875                $image_meta = wp_get_attachment_metadata( self::$large_id );
     876                $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
     877
     878                // Set up test cases for all expected size names.
     879                $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
     880
     881                // Add any soft crop intermediate sizes.
     882                foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
     883                        if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
     884                                $intermediates[] = $name;
     885                        }
     886                }
     887
     888                $expected = '';
     889
     890                foreach( $image_meta['sizes'] as $name => $size ) {
     891                        // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
     892                        if ( in_array( $name, $intermediates ) ) {
     893                                $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
     894                        }
     895                }
     896
     897                // Add the full size width at the end.
     898                $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
     899
     900                // Prepend an absolute path to simulate a pre-2.7 upload
     901                $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file'];
     902
     903                foreach ( $intermediates as $int ) {
     904                        $image_url = wp_get_attachment_image_url( self::$large_id, $int );
     905                        $size_array = $this->_get_image_size_array_from_name( $int );
     906                        $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     907                }
     908        }
     909
     910        /**
    869911         * @ticket 33641
    870912         */
    871913        function test_wp_calculate_image_srcset_false() {
    872914                $sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() );
    873915
    874916                // For canola.jpg we should return
    875917                $this->assertFalse( $sizes );
    876918        }
    877919
    878920        /**
    879921         * @ticket 33641
    880922         */
    881923        function test_wp_calculate_image_srcset_no_width() {
    882924                $file = get_attached_file( self::$large_id );
    883925                $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );