Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
Conflicts:
	wp-tevko-responsive-images.php
  • Loading branch information
tevko committed Sep 30, 2015
2 parents ada954f + 46dfaf2 commit cd58eac
Showing 4 changed files with 71 additions and 59 deletions.
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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**

10 changes: 8 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions tests/test-suite.php
Original file line number Diff line number Diff line change
@@ -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 ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img);
$respimg_no_size = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size);
$respimg_no_size_id = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_id);

$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>
@@ -421,7 +420,7 @@ function test_tevkori_filter_content_images() {
</ul>';

$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 ) );
}
105 changes: 53 additions & 52 deletions wp-tevko-responsive-images.php
Original file line number Diff line number Diff line change
@@ -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( '|<img ([^>]+' . $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(
'|<img ([^>]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i',
'_tevkori_filter_content_images_callback',
@@ -342,75 +374,44 @@ 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;
}

$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.

0 comments on commit cd58eac

Please sign in to comment.