Skip to content

Commit cd58eac

Browse files
committed
Merge branch 'dev'
Conflicts: wp-tevko-responsive-images.php
2 parents ada954f + 46dfaf2 commit cd58eac

File tree

4 files changed

+71
-59
lines changed

4 files changed

+71
-59
lines changed

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,22 @@ We use a hook because if you attempt to dequeue a script before it's enqueued, w
140140

141141
## Version
142142

143-
2.5.0
143+
2.5.1
144144

145145
## Changelog
146146

147+
- Query all images in single request before replacing
148+
- Minor fix to prevent a potential undefined variable notice
149+
- Remove third fallback query from the display filter
150+
151+
**2.5.0**
152+
147153
- Responsify all post images by adding `srcset` and `sizes` through a display filter.
148154
- Improve method used to build paths in `tevkori_get_srcset_array()`
149155
- Adds linthub config files
150156
- Returns single source arrays in `tevkori_get_srcset_array()`
151157
- Add tests for PHP7 to our Travis matrix
152-
- Add test coverage for `tevkori_filter_attachment_image_attributes()`
158+
- Add test coverage for `tevkori_filter_attachment_image_attributes()`
153159

154160
**2.4.0**
155161

readme.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://app.etapestry.com/hosted/BoweryResidentsCommittee/OnlineDon
44
Tags: Responsive, Images, Responsive Images, SRCSET, Picturefill
55
Requires at least: 4.1
66
Tested up to: 4.3
7-
Stable tag: 2.5.0
7+
Stable tag: 2.5.1
88
License: GPLv2
99
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
1010

@@ -26,13 +26,19 @@ This plugin works by including all available image sizes for each image upload.
2626

2727
== Changelog ==
2828

29+
= 2.5.1 =
30+
* Query all images in single request before replacing
31+
* Minor fix to prevent a potential undefined variable notice
32+
* Remove third fallback query from the display filter
33+
34+
2935
= 2.5.0 =
3036
* Responsify all post images by adding `srcset` and `sizes` through a display filter.
3137
* Improve method used to build paths in `tevkori_get_srcset_array()`
3238
* Adds linthub config files
3339
* Returns single source arrays in `tevkori_get_srcset_array()`
3440
* Add tests for PHP7 to our Travis matrix
35-
* Add test coverage for `tevkori_filter_attachment_image_attributes()`
41+
* Add test coverage for `tevkori_filter_attachment_image_attributes()`
3642

3743
= 2.4.0 =
3844
* Added filter for tevkori_get_sizes, with tests

tests/test-suite.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,11 @@ function test_tevkori_filter_content_images() {
385385
// Function used to build HTML for the editor.
386386
$img = get_image_tag( $id, '', '', '', 'medium' );
387387
$img_no_size = str_replace( 'size-', '', $img );
388-
$img_no_size_id = str_replace( 'wp-attachment-', '', $img_no_size );
388+
$img_no_size_id = str_replace( 'wp-image-', 'id-', $img_no_size );
389389

390390
// Manually add srcset and sizes to the markup from get_image_tag();
391391
$respimg = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img);
392392
$respimg_no_size = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size);
393-
$respimg_no_size_id = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_id);
394393

395394
$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>
396395
<p>First things first:</p>
@@ -421,7 +420,7 @@ function test_tevkori_filter_content_images() {
421420
</ul>';
422421

423422
$content_unfiltered = sprintf( $content, $img, $img_no_size, $img_no_size_id );
424-
$content_filtered = sprintf( $content, $respimg, $respimg_no_size, $respimg_no_size_id );
423+
$content_filtered = sprintf( $content, $respimg, $respimg_no_size, $img_no_size_id );
425424

426425
$this->assertSame( $content_filtered, tevkori_filter_content_images( $content_unfiltered ) );
427426
}

wp-tevko-responsive-images.php

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Plugin Name: RICG Responsive Images
99
* Plugin URI: http://www.smashingmagazine.com/2015/02/24/ricg-responsive-images-for-wordpress/
1010
* Description: Bringing automatic default responsive images to wordpress
11-
* Version: 2.5.0
11+
* Version: 2.5.1
1212
* Author: The RICG
1313
* Author URI: http://responsiveimages.org/
1414
* License: GPL-2.0+
@@ -294,6 +294,38 @@ function tevkori_filter_content_images( $content ) {
294294
$uploads_dir = wp_upload_dir();
295295
$path_to_upload_dir = $uploads_dir['baseurl'];
296296

297+
preg_match_all( '|<img ([^>]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i', $content, $matches );
298+
299+
$images = $matches[0];
300+
$ids = array();
301+
302+
foreach( $images as $image ) {
303+
if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
304+
(int) $id = $class_id[1];
305+
if( $id ) {
306+
$ids[] = $id;
307+
}
308+
}
309+
}
310+
311+
if ( 0 < count( $ids ) ) {
312+
/**
313+
* Warm object caches for use with wp_get_attachment_metadata.
314+
*
315+
* To avoid making a database call for each image, WP_Query is called
316+
* as a single query with the IDs of all images in the post. This warms
317+
* the object cache with the meta information for all images.
318+
*
319+
* This loop is not used directly.
320+
**/
321+
$attachments = new WP_Query(array(
322+
'post_type' => 'attachment',
323+
'posts_per_page' => '-1',
324+
'post_status' => 'inherit',
325+
'post__in' => $ids,
326+
));
327+
}
328+
297329
$content = preg_replace_callback(
298330
'|<img ([^>]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i',
299331
'_tevkori_filter_content_images_callback',
@@ -342,75 +374,44 @@ function _tevkori_filter_content_images_callback( $image ) {
342374
}
343375

344376
if ( $id && false === $size ) {
345-
preg_match( '/width="([0-9]+)"/', $atts, $width );
346-
preg_match( '/height="([0-9]+)"/', $atts, $height );
347-
348-
$size = array(
349-
(int) $width[1],
350-
(int) $height[1]
351-
);
377+
if ( preg_match( '/ width="([0-9]+)"/', $atts, $width ) && preg_match( '/ height="([0-9]+)"/', $atts, $height ) ) {
378+
$size = array(
379+
(int) $width[1],
380+
(int) $height[1]
381+
);
382+
}
352383
}
353384

354385
/*
355-
* If attempts to get values for ID and size failed, use the
356-
* src to query for matching values in '_wp_attachment_metadata'.
386+
* If attempts to parse the size value failed, attempt to use the image
387+
* metadata to match the `src` angainst the available sizes for an attachment.
357388
*/
358-
if ( false === $id || false === $size ) {
389+
if ( ! $size && ! empty( $id ) && $meta = wp_get_attachment_metadata( $id ) ) {
390+
359391
preg_match( '/src="([^"]+)"/', $atts, $url );
360392

393+
// Sanity check the `src` value and bail early it doesn't exist.
361394
if ( ! $url[1] ) {
362395
return $image_html;
363396
}
364397

365398
$image_filename = basename( $url[1] );
366399

367400
/*
368-
* If we already have an ID, we use it to get the attachment metadata
369-
* using 'wp_get_attachment_metadata()'. Otherwise, we'll use the image
370-
* 'src' url to query the postmeta table for both the attachement ID and
371-
* metadata, which we'll use later to get the size.
401+
* First, see if the file is the full size image. If not, we loop through
402+
* the intermediate sizes until we find a match.
372403
*/
373-
if ( ! empty( $id ) ) {
374-
$meta = wp_get_attachment_metadata( $id );
404+
if ( $image_filename === basename( $meta['file'] ) ) {
405+
$size = 'full';
375406
} else {
376-
global $wpdb;
377-
$meta_object = $wpdb->get_row( $wpdb->prepare(
378-
"SELECT `post_id`, `meta_value` FROM $wpdb->postmeta WHERE `meta_key` = '_wp_attachment_metadata' AND `meta_value` LIKE %s",
379-
'%' . $image_filename . '%'
380-
) );
381-
382-
// If the query is successful, we can determine the ID and size.
383-
if ( is_object( $meta_object ) ) {
384-
$id = $meta_object->post_id;
385-
386-
// Unserialize the meta_value returned in our query.
387-
$meta = maybe_unserialize( $meta_object->meta_value );
388-
} else {
389-
$meta = false;
390-
}
391-
}
392-
393-
/*
394-
* Now that we have the attachment ID and metadata, we can retrieve the
395-
* size by matching the original image's 'src' filename with the sizes
396-
* included in the attachment metadata.
397-
*/
398-
if ( $id && $meta ) {
399-
/*
400-
* First, see if the file is the full size image. If not, we loop through
401-
* the intermediate sizes until we find a match.
402-
*/
403-
if ( $image_filename === basename( $meta['file'] ) ) {
404-
$size = 'full';
405-
} else {
406-
foreach( $meta['sizes'] as $image_size => $image_size_data ) {
407-
if ( $image_filename === $image_size_data['file'] ) {
408-
$size = $image_size;
409-
break;
410-
}
407+
foreach( $meta['sizes'] as $image_size => $image_size_data ) {
408+
if ( $image_filename === $image_size_data['file'] ) {
409+
$size = $image_size;
410+
break;
411411
}
412412
}
413413
}
414+
414415
}
415416

416417
// If we have an ID and size, try for 'srcset' and 'sizes' and update the markup.

0 commit comments

Comments
 (0)