WordPress.org

Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4/src/wp-includes/media.php

    r35821 r36152  
    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 ( '.' === $dirname ) {
     893        return '';
     894    }
     895
     896    if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) {
     897        // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads)
     898        $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
     899        $dirname = ltrim( $dirname, '/' );
     900    }
     901
     902    return $dirname;
     903}
     904
     905/**
    881906 * Caches and returns the base URL of the uploads directory.
    882907 *
     
    972997 */
    973998function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
     999    /**
     1000     * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
     1001     *
     1002     * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
     1003     * @param array  $size_array    Array of width and height values in pixels (in that order).
     1004     * @param string $image_src     The 'src' of the image.
     1005     * @param int    $attachment_id The image attachment ID or 0 if not supplied.
     1006     */
     1007    $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
     1008
    9741009    if ( empty( $image_meta['sizes'] ) ) {
    9751010        return false;
     
    9881023
    9891024    $image_basename = wp_basename( $image_meta['file'] );
    990     $image_baseurl = _wp_upload_dir_baseurl();
    9911025
    9921026    /*
     
    10051039    }
    10061040
    1007     // Uploads are (or have been) in year/month sub-directories.
    1008     if ( $image_basename !== $image_meta['file'] ) {
    1009         $dirname = dirname( $image_meta['file'] );
    1010 
    1011         if ( $dirname !== '.' ) {
    1012             $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    1013         }
    1014     }
    1015 
    1016     $image_baseurl = trailingslashit( $image_baseurl );
     1041    // Retrieve the uploads sub-directory from the full size image.
     1042    $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     1043
     1044    if ( $dirname ) {
     1045        $dirname = trailingslashit( $dirname );
     1046    }
     1047
     1048    $image_baseurl = _wp_upload_dir_baseurl();
     1049    $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10171050
    10181051    // Calculate the image aspect ratio.
     
    10391072    $sources = array();
    10401073
     1074    /**
     1075     * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
     1076     * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving
     1077     * an incorrect image. See #35045.
     1078     */
     1079    $src_matched = false;
     1080
    10411081    /*
    10421082     * Loop through available images. Only use images that are resized
     
    10451085    foreach ( $image_sizes as $image ) {
    10461086
     1087        // If the file name is part of the `src`, we've confirmed a match.
     1088        if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
     1089            $src_matched = true;
     1090        }
     1091
    10471092        // Filter out images that are from previous edits.
    10481093        if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
     
    10501095        }
    10511096
    1052         // Filter out images that are wider than '$max_srcset_image_width'.
    1053         if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width ) {
     1097        /*
     1098         * Filter out images that are wider than '$max_srcset_image_width' unless
     1099         * that file is in the 'src' attribute.
     1100         */
     1101        if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width &&
     1102            false === strpos( $image_src, $image['file'] ) ) {
     1103
    10541104            continue;
    10551105        }
     
    10971147
    10981148    // Only return a 'srcset' value if there is more than one source.
    1099     if ( count( $sources ) < 2 ) {
     1149    if ( ! $src_matched || count( $sources ) < 2 ) {
    11001150        return false;
    11011151    }
     
    12761326        strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    12771327
    1278         return $image;
    1279     }
    1280 
    1281     $base_url = trailingslashit( _wp_upload_dir_baseurl() );
    1282     $image_base_url = $base_url;
    1283 
    1284     $dirname = dirname( $image_meta['file'] );
    1285     if ( $dirname !== '.' ) {
    1286         $image_base_url .= trailingslashit( $dirname );
    1287     }
    1288 
    1289     $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
    1290 
    1291     foreach ( $all_sizes as $key => $file ) {
    1292         $all_sizes[ $key ] = $image_base_url . $file;
    1293     }
    1294 
    1295     // Add the original image.
    1296     $all_sizes[] = $base_url . $image_meta['file'];
    1297 
    1298     // Bail early if the image src doesn't match any of the known image sizes.
    1299     if ( ! in_array( $image_src, $all_sizes ) ) {
    13001328        return $image;
    13011329    }
Note: See TracChangeset for help on using the changeset viewer.