X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/0459461f9ea42e0b090759ff6fe5f48360bef750..refs/tags/wordpress-4.5:/wp-includes/media.php diff --git a/wp-includes/media.php b/wp-includes/media.php index 70b09584..9f35f798 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -902,27 +902,6 @@ function _wp_get_attachment_relative_path( $file ) { return $dirname; } -/** - * Caches and returns the base URL of the uploads directory. - * - * @since 4.4.0 - * @access private - * - * @return string The base URL, cached. - */ -function _wp_upload_dir_baseurl() { - static $baseurl = array(); - - $blog_id = get_current_blog_id(); - - if ( empty( $baseurl[$blog_id] ) ) { - $uploads_dir = wp_upload_dir(); - $baseurl[$blog_id] = $uploads_dir['baseurl']; - } - - return $baseurl[$blog_id]; -} - /** * Get the image size as array from its meta data. * @@ -1006,7 +985,7 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac */ $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id ); - if ( empty( $image_meta['sizes'] ) ) { + if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) || strlen( $image_meta['file'] ) < 4 ) { return false; } @@ -1045,11 +1024,16 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac $dirname = trailingslashit( $dirname ); } - $image_baseurl = _wp_upload_dir_baseurl(); - $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; + $upload_dir = wp_get_upload_dir(); + $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname; - // Calculate the image aspect ratio. - $image_ratio = $image_height / $image_width; + /* + * If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain + * (which is to say, when they share the domain name of the current request). + */ + if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) { + $image_baseurl = set_url_scheme( $image_baseurl, 'https' ); + } /* * Images that have been edited in WordPress after being uploaded will @@ -1073,7 +1057,7 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac /** * To make sure the ID matches our image src, we will check to see if any sizes in our attachment - * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving + * meta match our $image_src. If no matches are found we don't return a srcset to avoid serving * an incorrect image. See #35045. */ $src_matched = false; @@ -1083,10 +1067,16 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac * versions of the same edit. */ foreach ( $image_sizes as $image ) { + $is_src = false; + + // Check if image meta isn't corrupted. + if ( ! is_array( $image ) ) { + continue; + } // If the file name is part of the `src`, we've confirmed a match. if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) { - $src_matched = true; + $src_matched = $is_src = true; } // Filter out images that are from previous edits. @@ -1098,27 +1088,38 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac * Filter out images that are wider than '$max_srcset_image_width' unless * that file is in the 'src' attribute. */ - if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && - false === strpos( $image_src, $image['file'] ) ) { - + if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && ! $is_src ) { continue; } - // Calculate the new image ratio. - if ( $image['width'] ) { - $image_ratio_compare = $image['height'] / $image['width']; + /** + * To check for varying crops, we calculate the expected size of the smaller + * image if the larger were constrained by the width of the smaller and then + * see if it matches what we're expecting. + */ + if ( $image_width > $image['width'] ) { + $constrained_size = wp_constrain_dimensions( $image_width, $image_height, $image['width'] ); + $expected_size = array( $image['width'], $image['height'] ); } else { - $image_ratio_compare = 0; + $constrained_size = wp_constrain_dimensions( $image['width'], $image['height'], $image_width ); + $expected_size = array( $image_width, $image_height ); } - // If the new ratio differs by less than 0.002, use it. - if ( abs( $image_ratio - $image_ratio_compare ) < 0.002 ) { + // If the image dimensions are within 1px of the expected size, use it. + if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) { // Add the URL, descriptor, and value to the sources array to be returned. - $sources[ $image['width'] ] = array( + $source = array( 'url' => $image_baseurl . $image['file'], 'descriptor' => 'w', 'value' => $image['width'], ); + + // The 'src' image has to be the first in the 'srcset', because of a bug in iOS8. See #35030. + if ( $is_src ) { + $sources = array( $image['width'] => $source ) + $sources; + } else { + $sources[ $image['width'] ] = $source; + } } } @@ -2160,9 +2161,9 @@ function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) { * @type string $src URL to the source of the audio file. Default empty. * @type string $loop The 'loop' attribute for the `