<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[36121] trunk: Responsive images: fix the check whether the attachment meta matches the image src to work with http/https and CDNs.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36121">36121</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36121","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-12-30 01:03:11 +0000 (Wed, 30 Dec 2015)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Responsive images: fix the check whether the attachment meta matches the image src to work with http/https and CDNs.
Props webaware, joemcgill, azaozz.
Fixes <a href="https://core.trac.wordpress.org/ticket/35045">#35045</a> and <a href="https://core.trac.wordpress.org/ticket/35102">#35102</a> for trunk.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
<li><a href="#trunktestsphpunittestsmediaphp">trunk/tests/phpunit/tests/media.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php 2015-12-30 00:05:07 UTC (rev 36120)
+++ trunk/src/wp-includes/media.php 2015-12-30 01:03:11 UTC (rev 36121)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -996,6 +996,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return string|bool The 'srcset' attribute value. False on error or when only one source exists.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
+ *
+ * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
+ * @param array $size_array Array of width and height values in pixels (in that order).
+ * @param string $image_src The 'src' of the image.
+ * @param int $attachment_id The image attachment ID or 0 if not supplied.
+ */
+ $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $image_meta['sizes'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1012,7 +1022,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $image_basename = wp_basename( $image_meta['file'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $image_baseurl = _wp_upload_dir_baseurl();
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * WordPress flattens animated GIFs into one frame when generating intermediate sizes.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1029,16 +1038,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Uploads are (or have been) in year/month sub-directories.
- if ( $image_basename !== $image_meta['file'] ) {
- $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Retrieve the uploads sub-directory from the full size image.
+ $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $dirname ) {
- $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $dirname ) {
+ $dirname = trailingslashit( $dirname );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $image_baseurl = trailingslashit( $image_baseurl );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $image_baseurl = _wp_upload_dir_baseurl();
+ $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Images that have been edited in WordPress after being uploaded will
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1060,12 +1068,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Array to hold URL candidates.
</span><span class="cx" style="display: block; padding: 0 10px"> $sources = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * 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
+ * an incorrect image. See #35045.
+ */
+ $src_matched = false;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * Loop through available images. Only use images that are resized
</span><span class="cx" style="display: block; padding: 0 10px"> * versions of the same edit.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $image_sizes as $image ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Filter out images that are from previous edits.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1129,7 +1149,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Only return a 'srcset' value if there is more than one source.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( count( $sources ) < 2 ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $src_matched || count( $sources ) < 2 ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1311,28 +1331,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $image;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $base_url = trailingslashit( _wp_upload_dir_baseurl() );
- $image_base_url = $base_url;
-
- $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
- if ( $dirname ) {
- $image_base_url .= trailingslashit( $dirname );
- }
-
- $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
-
- foreach ( $all_sizes as $key => $file ) {
- $all_sizes[ $key ] = $image_base_url . $file;
- }
-
- // Add the original image.
- $all_sizes[] = $image_base_url . basename( $image_meta['file'] );
-
- // Bail early if the image src doesn't match any of the known image sizes.
- if ( ! in_array( $image_src, $all_sizes ) ) {
- return $image;
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
</span><span class="cx" style="display: block; padding: 0 10px"> $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/media.php 2015-12-30 00:05:07 UTC (rev 36120)
+++ trunk/tests/phpunit/tests/media.php 2015-12-30 01:03:11 UTC (rev 36121)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1265,4 +1265,42 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Intermediate sized GIFs should not include the full size in the srcset.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @ticket 35045
+ * @ticket 33641
+ */
+ function test_wp_make_content_images_responsive_schemes() {
+ $image_meta = wp_get_attachment_metadata( self::$large_id );
+ $size_array = $this->_get_image_size_array_from_name( 'medium' );
+
+ $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
+ $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
+
+ // Build HTML for the editor.
+ $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
+ $img_https = str_replace( 'http://', 'https://', $img );
+ $img_relative = str_replace( 'http://', '//', $img );
+
+ // Manually add srcset and sizes to the markup from get_image_tag().
+ $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
+ $respimg_https = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
+ $respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
+
+ $content = '
+ <p>Image, http: protocol. Should have srcset and sizes.</p>
+ %1$s
+
+ <p>Image, https: protocol. Should have srcset and sizes.</p>
+ %2$s
+
+ <p>Image, protocol-relative. Should have srcset and sizes.</p>
+ %3$s';
+
+ $unfiltered = sprintf( $content, $img, $img_https, $img_relative );
+ $expected = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
+ $actual = wp_make_content_images_responsive( $unfiltered );
+
+ $this->assertSame( $expected, $actual );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>