Skip to content

Commit

Permalink
Merge pull request #238 from ResponsiveImagesCG/227-sizes-attr-check
Browse files Browse the repository at this point in the history
Check if there is no sizes attribute before adding it (Resolves #227)
  • Loading branch information
joemcgill committed Nov 13, 2015
2 parents 17258c4 + 340f48f commit da3dd7d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 7 additions & 3 deletions tests/test-suite.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,12 @@ function test_tevkori_filter_content_images() {
$img = get_image_tag( $id, '', '', '', 'medium' );
$img_no_size = str_replace( 'size-', '', $img );
$img_no_size_id = str_replace( 'wp-image-', 'id-', $img_no_size );
$img_with_sizes = str_replace( '<img ', '<img sizes="99vw" ', $img );

// Manually add srcset and sizes to the markup from get_image_tag().
$respimg = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
$respimg_no_size = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size );
$respimg_with_sizes = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes );

$content = '<p>Welcome to WordPress! This post contains important information. After you read it, you can make it private to hide it from visitors but still have the information handy for future reference.</p>
<p>First things first:</p>
Expand Down Expand Up @@ -476,10 +478,12 @@ function test_tevkori_filter_content_images() {
<li>Modify and prettify your website&#8217;s links at <a href="http://wordpress.org" title="For example, select a link structure like: http://example.com/1999/12/post-name">Settings &#8250; Permalinks</a></li>
<li>Import content from another system or WordPress site at <a href="http://wordpress.org" title="WordPress comes with importers for the most common publishing systems">Tools &#8250; Import</a></li>
<li>Find answers to your questions at the <a href="http://wordpress.orgs" title="The official WordPress documentation, maintained by the WordPress community">WordPress Codex</a></li>
</ul>';
</ul>
%4$s';

$content_unfiltered = sprintf( $content, $img, $img_no_size, $img_no_size_id );
$content_filtered = sprintf( $content, $respimg, $respimg_no_size, $img_no_size_id );
$content_unfiltered = sprintf( $content, $img, $img_no_size, $img_no_size_id, $img_with_sizes );
$content_filtered = sprintf( $content, $respimg, $respimg_no_size, $img_no_size_id, $respimg_with_sizes );

$this->assertSame( $content_filtered, tevkori_filter_content_images( $content_unfiltered ) );
}
Expand Down
17 changes: 13 additions & 4 deletions wp-tevko-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function wp_make_content_images_responsive( $content ) {
$selected_images = $attachment_ids = array();

foreach( $images as $image ) {
if ( false === strpos( $image, ' srcset="' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) &&
( $attachment_id = absint( $class_id[1] ) ) ) {

/*
Expand Down Expand Up @@ -465,15 +465,24 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
$srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );

if ( $srcset ) {
$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
// Check if there is already a 'sizes' attribute.
$sizes = strpos( $image, ' sizes=' );

if ( ! $sizes ) {
$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
}
}

if ( $srcset && $sizes ) {
// Format the 'srcset' and 'sizes' string and escape attributes.
$srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) );
$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );

if ( is_string( $sizes ) ) {
$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
}

// Add 'srcset' and 'sizes' attributes to the image markup.
$image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $srcset_and_sizes . ' />', $image );
$image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
}

return $image;
Expand Down

0 comments on commit da3dd7d

Please sign in to comment.