~canonical-sysadmins/wordpress/4.4.2

« back to all changes in this revision

Viewing changes to wp-includes/media.php

  • Committer: Barry Price
  • Date: 2016-01-07 06:09:42 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: barry.price@canonical.com-20160107060942-1ghv2tqiwm8z0j36
Merge WP4.4.1 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
878
878
}
879
879
 
880
880
/**
 
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
 */
 
889
function _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
/**
881
906
 * Caches and returns the base URL of the uploads directory.
882
907
 *
883
908
 * @since 4.4.0
971
996
 * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
972
997
 */
973
998
function 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
 
974
1009
        if ( empty( $image_meta['sizes'] ) ) {
975
1010
                return false;
976
1011
        }
987
1022
        }
988
1023
 
989
1024
        $image_basename = wp_basename( $image_meta['file'] );
990
 
        $image_baseurl = _wp_upload_dir_baseurl();
991
1025
 
992
1026
        /*
993
1027
         * WordPress flattens animated GIFs into one frame when generating intermediate sizes.
1004
1038
                return false;
1005
1039
        }
1006
1040
 
1007
 
        // Uploads are (or have been) in year/month sub-directories.
1008
 
        if ( $image_basename !== $image_meta['file'] ) {
1009
 
                $dirname = dirname( $image_meta['file'] );
 
1041
        // Retrieve the uploads sub-directory from the full size image.
 
1042
        $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
1010
1043
 
1011
 
                if ( $dirname !== '.' ) {
1012
 
                        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
1013
 
                }
 
1044
        if ( $dirname ) {
 
1045
                $dirname = trailingslashit( $dirname );
1014
1046
        }
1015
1047
 
1016
 
        $image_baseurl = trailingslashit( $image_baseurl );
 
1048
        $image_baseurl = _wp_upload_dir_baseurl();
 
1049
        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
1017
1050
 
1018
1051
        // Calculate the image aspect ratio.
1019
1052
        $image_ratio = $image_height / $image_width;
1038
1071
        // Array to hold URL candidates.
1039
1072
        $sources = array();
1040
1073
 
 
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
 
1041
1081
        /*
1042
1082
         * Loop through available images. Only use images that are resized
1043
1083
         * versions of the same edit.
1044
1084
         */
1045
1085
        foreach ( $image_sizes as $image ) {
1046
1086
 
 
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
 
1047
1092
                // Filter out images that are from previous edits.
1048
1093
                if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
1049
1094
                        continue;
1050
1095
                }
1051
1096
 
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
 
1054
1104
                        continue;
1055
1105
                }
1056
1106
 
1096
1146
        $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
1097
1147
 
1098
1148
        // Only return a 'srcset' value if there is more than one source.
1099
 
        if ( count( $sources ) < 2 ) {
 
1149
        if ( ! $src_matched || count( $sources ) < 2 ) {
1100
1150
                return false;
1101
1151
        }
1102
1152
 
1278
1328
                return $image;
1279
1329
        }
1280
1330
 
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 ) ) {
1300
 
                return $image;
1301
 
        }
1302
 
 
1303
1331
        $width  = preg_match( '/ width="([0-9]+)"/',  $image, $match_width  ) ? (int) $match_width[1]  : 0;
1304
1332
        $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
1305
1333