diff --git a/readme.md b/readme.md index 2b160bb..eaca33c 100644 --- a/readme.md +++ b/readme.md @@ -135,10 +135,17 @@ We use a hook because if you attempt to dequeue a script before it's enqueued, w ##Version -2.3.0 +2.3.1 ##Changelog +- First char no longer stripped from file name if there's no slash +- Adding test for when uploads directory not organized by date +- Don't calculate a srcset when the image data returns no width +- Add test for image_downsize returning 0 as a width + +**2.3.0** + - Improved performance of `get_srcset_array` - Added advanced image compression option (available by adding hook to functions.php) - Duplicate entires now filtered out from srcset array diff --git a/tests/test-suite.php b/tests/test-suite.php index 95546dd..d9c7368 100644 --- a/tests/test-suite.php +++ b/tests/test-suite.php @@ -147,6 +147,32 @@ function test_tevkori_get_srcset_array() { $this->assertSame( $expected, $sizes ); } + function test_tevkori_get_srcset_array_no_date_upoads() { + // Save the current setting for uploads folders + $uploads_use_yearmonth_folders = get_option( 'uploads_use_yearmonth_folders' ); + + // Disable date organized uploads + update_option( 'uploads_use_yearmonth_folders', 0 ); + + // make an image + $id = $this->_test_img(); + $sizes = tevkori_get_srcset_array( $id, 'medium' ); + + $image = wp_get_attachment_metadata( $id ); + $filename_base = substr( $image['file'], 0, strrpos($image['file'], '.png') ); + + $expected = array( + 'http://example.org/wp-content/uploads/' . $image['sizes']['medium']['file'] . ' ' . $image['sizes']['medium']['width'] . 'w', + 'http://example.org/wp-content/uploads/' . $image['sizes']['large']['file'] . ' ' . $image['sizes']['large']['width'] . 'w', + 'http://example.org/wp-content/uploads/' . $image['file'] . ' ' . $image['width'] .'w' + ); + + $this->assertSame( $expected, $sizes ); + + // Leave the uploads option the way you found it. + update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders ); + } + function test_tevkori_get_srcset_array_thumb() { // make an image $id = $this->_test_img(); @@ -171,6 +197,28 @@ function test_tevkori_get_srcset_array_false() { // make an image $this->assertFalse( $sizes ); } + function test_tevkori_get_srcset_array_no_width() { + // Filter image_downsize() output. + add_filter( 'image_downsize', array( $this, '_test_tevkori_get_srcset_array_no_width_filter' ) ); + + // Make our attachement. + $id = $this->_test_img(); + $srcset = tevkori_get_srcset_array( $id, 'medium' ); + + // The srcset should be false + $this->assertFalse( $srcset ); + + // Remove filter. + remove_filter( 'image_downsize', array( $this, '_test_tevkori_get_srcset_array_no_width_filter' ) ); + } + + /** + * Helper funtion to filter image_downsize and return zero values for width and height. + */ + public function _test_tevkori_get_srcset_array_no_width_filter() { + return array( 'http://example.org/foo.jpg', 0, 0, false ); + } + function test_tevkori_get_srcset_string() { // make an image $id = $this->_test_img(); diff --git a/wp-tevko-responsive-images.php b/wp-tevko-responsive-images.php index 96e1f88..550b838 100644 --- a/wp-tevko-responsive-images.php +++ b/wp-tevko-responsive-images.php @@ -10,7 +10,7 @@ * Plugin Name: RICG Responsive Images * Plugin URI: http://www.smashingmagazine.com/2015/02/24/ricg-responsive-images-for-wordpress/ * Description: Bringing automatic default responsive images to wordpress - * Version: 2.3.0 + * Version: 2.3.1 * Author: The RICG * Author URI: http://responsiveimages.org/ * License: GPL-2.0+ @@ -153,6 +153,11 @@ function tevkori_get_srcset_array( $id, $size = 'thumbnail' ) { // Break image data into url, width, and height. list( $img_url, $img_width, $img_height ) = $img; + // If we have no width to work with, we should bail (see issue #118). + if ( 0 == $img_width ) { + return false; + } + // Get the image meta data. $img_meta = wp_get_attachment_metadata( $id ); @@ -163,8 +168,11 @@ function tevkori_get_srcset_array( $id, $size = 'thumbnail' ) { $img_sizes['full'] = array( 'width' => $img_meta['width'], 'height' => $img_meta['height'], - 'file' => substr( $img_meta['file'], strrpos( $img_meta['file'], '/' ) + 1 ) + 'file' => $img_meta['file'] ); + if ( strrpos( $img_meta['file'], '/' ) !== false ) { + $img_sizes['full']['file'] = substr( $img_meta['file'], strrpos( $img_meta['file'], '/' ) + 1 ); + } // Get the image base url. $img_base_url = substr( $img_url, 0, strrpos( $img_url, '/' ) + 1 );