diff --git a/readme.md b/readme.md index 31f0062..80a2ff9 100644 --- a/readme.md +++ b/readme.md @@ -140,16 +140,22 @@ We use a hook because if you attempt to dequeue a script before it's enqueued, w ## Version -2.5.0 +2.5.1 ## Changelog +- Query all images in single request before replacing +- Minor fix to prevent a potential undefined variable notice +- Remove third fallback query from the display filter + +**2.5.0** + - Responsify all post images by adding `srcset` and `sizes` through a display filter. - Improve method used to build paths in `tevkori_get_srcset_array()` - Adds linthub config files - Returns single source arrays in `tevkori_get_srcset_array()` - Add tests for PHP7 to our Travis matrix -- Add test coverage for `tevkori_filter_attachment_image_attributes()` +- Add test coverage for `tevkori_filter_attachment_image_attributes()` **2.4.0** diff --git a/readme.txt b/readme.txt index 82a9cfc..9b62fb9 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://app.etapestry.com/hosted/BoweryResidentsCommittee/OnlineDon Tags: Responsive, Images, Responsive Images, SRCSET, Picturefill Requires at least: 4.1 Tested up to: 4.3 -Stable tag: 2.5.0 +Stable tag: 2.5.1 License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.txt @@ -26,13 +26,19 @@ This plugin works by including all available image sizes for each image upload. == Changelog == += 2.5.1 = +* Query all images in single request before replacing +* Minor fix to prevent a potential undefined variable notice +* Remove third fallback query from the display filter + + = 2.5.0 = * Responsify all post images by adding `srcset` and `sizes` through a display filter. * Improve method used to build paths in `tevkori_get_srcset_array()` * Adds linthub config files * Returns single source arrays in `tevkori_get_srcset_array()` * Add tests for PHP7 to our Travis matrix -* Add test coverage for `tevkori_filter_attachment_image_attributes()` +* Add test coverage for `tevkori_filter_attachment_image_attributes()` = 2.4.0 = * Added filter for tevkori_get_sizes, with tests diff --git a/tests/test-suite.php b/tests/test-suite.php index b0617f1..7821a01 100644 --- a/tests/test-suite.php +++ b/tests/test-suite.php @@ -385,12 +385,11 @@ function test_tevkori_filter_content_images() { // Function used to build HTML for the editor. $img = get_image_tag( $id, '', '', '', 'medium' ); $img_no_size = str_replace( 'size-', '', $img ); - $img_no_size_id = str_replace( 'wp-attachment-', '', $img_no_size ); + $img_no_size_id = str_replace( 'wp-image-', 'id-', $img_no_size ); // Manually add srcset and sizes to the markup from get_image_tag(); $respimg = preg_replace('|]+) />|', '', $img); $respimg_no_size = preg_replace('|]+) />|', '', $img_no_size); - $respimg_no_size_id = preg_replace('|]+) />|', '', $img_no_size_id); $content = '
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.
First things first:
@@ -421,7 +420,7 @@ function test_tevkori_filter_content_images() { '; $content_unfiltered = sprintf( $content, $img, $img_no_size, $img_no_size_id ); - $content_filtered = sprintf( $content, $respimg, $respimg_no_size, $respimg_no_size_id ); + $content_filtered = sprintf( $content, $respimg, $respimg_no_size, $img_no_size_id ); $this->assertSame( $content_filtered, tevkori_filter_content_images( $content_unfiltered ) ); } diff --git a/wp-tevko-responsive-images.php b/wp-tevko-responsive-images.php index bf57d71..2915172 100644 --- a/wp-tevko-responsive-images.php +++ b/wp-tevko-responsive-images.php @@ -8,7 +8,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.5.0 + * Version: 2.5.1 * Author: The RICG * Author URI: http://responsiveimages.org/ * License: GPL-2.0+ @@ -294,6 +294,38 @@ function tevkori_filter_content_images( $content ) { $uploads_dir = wp_upload_dir(); $path_to_upload_dir = $uploads_dir['baseurl']; + preg_match_all( '|]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i', $content, $matches ); + + $images = $matches[0]; + $ids = array(); + + foreach( $images as $image ) { + if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { + (int) $id = $class_id[1]; + if( $id ) { + $ids[] = $id; + } + } + } + + if ( 0 < count( $ids ) ) { + /** + * Warm object caches for use with wp_get_attachment_metadata. + * + * To avoid making a database call for each image, WP_Query is called + * as a single query with the IDs of all images in the post. This warms + * the object cache with the meta information for all images. + * + * This loop is not used directly. + **/ + $attachments = new WP_Query(array( + 'post_type' => 'attachment', + 'posts_per_page' => '-1', + 'post_status' => 'inherit', + 'post__in' => $ids, + )); + } + $content = preg_replace_callback( '|]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i', '_tevkori_filter_content_images_callback', @@ -342,22 +374,23 @@ function _tevkori_filter_content_images_callback( $image ) { } if ( $id && false === $size ) { - preg_match( '/width="([0-9]+)"/', $atts, $width ); - preg_match( '/height="([0-9]+)"/', $atts, $height ); - - $size = array( - (int) $width[1], - (int) $height[1] - ); + if ( preg_match( '/ width="([0-9]+)"/', $atts, $width ) && preg_match( '/ height="([0-9]+)"/', $atts, $height ) ) { + $size = array( + (int) $width[1], + (int) $height[1] + ); + } } /* - * If attempts to get values for ID and size failed, use the - * src to query for matching values in '_wp_attachment_metadata'. + * If attempts to parse the size value failed, attempt to use the image + * metadata to match the `src` angainst the available sizes for an attachment. */ - if ( false === $id || false === $size ) { + if ( ! $size && ! empty( $id ) && $meta = wp_get_attachment_metadata( $id ) ) { + preg_match( '/src="([^"]+)"/', $atts, $url ); + // Sanity check the `src` value and bail early it doesn't exist. if ( ! $url[1] ) { return $image_html; } @@ -365,52 +398,20 @@ function _tevkori_filter_content_images_callback( $image ) { $image_filename = basename( $url[1] ); /* - * If we already have an ID, we use it to get the attachment metadata - * using 'wp_get_attachment_metadata()'. Otherwise, we'll use the image - * 'src' url to query the postmeta table for both the attachement ID and - * metadata, which we'll use later to get the size. + * First, see if the file is the full size image. If not, we loop through + * the intermediate sizes until we find a match. */ - if ( ! empty( $id ) ) { - $meta = wp_get_attachment_metadata( $id ); + if ( $image_filename === basename( $meta['file'] ) ) { + $size = 'full'; } else { - global $wpdb; - $meta_object = $wpdb->get_row( $wpdb->prepare( - "SELECT `post_id`, `meta_value` FROM $wpdb->postmeta WHERE `meta_key` = '_wp_attachment_metadata' AND `meta_value` LIKE %s", - '%' . $image_filename . '%' - ) ); - - // If the query is successful, we can determine the ID and size. - if ( is_object( $meta_object ) ) { - $id = $meta_object->post_id; - - // Unserialize the meta_value returned in our query. - $meta = maybe_unserialize( $meta_object->meta_value ); - } else { - $meta = false; - } - } - - /* - * Now that we have the attachment ID and metadata, we can retrieve the - * size by matching the original image's 'src' filename with the sizes - * included in the attachment metadata. - */ - if ( $id && $meta ) { - /* - * First, see if the file is the full size image. If not, we loop through - * the intermediate sizes until we find a match. - */ - if ( $image_filename === basename( $meta['file'] ) ) { - $size = 'full'; - } else { - foreach( $meta['sizes'] as $image_size => $image_size_data ) { - if ( $image_filename === $image_size_data['file'] ) { - $size = $image_size; - break; - } + foreach( $meta['sizes'] as $image_size => $image_size_data ) { + if ( $image_filename === $image_size_data['file'] ) { + $size = $image_size; + break; } } } + } // If we have an ID and size, try for 'srcset' and 'sizes' and update the markup.