8
8
* Plugin Name: RICG Responsive Images
9
9
* Plugin URI: http://www.smashingmagazine.com/2015/02/24/ricg-responsive-images-for-wordpress/
10
10
* Description: Bringing automatic default responsive images to wordpress
11
- * Version: 2.5.1
11
+ * Version: 2.5.2
12
12
* Author: The RICG
13
13
* Author URI: http://responsiveimages.org/
14
14
* License: GPL-2.0+
@@ -54,23 +54,31 @@ function tevkori_get_picturefill() {
54
54
* @since 2.2.0
55
55
*
56
56
* @param int $id Image attachment ID.
57
- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
57
+ * @param string $size Optional. Name of image size. Default value: 'medium '.
58
58
* @param array $args {
59
59
* Optional. Arguments to retrieve posts.
60
60
*
61
61
* @type array|string $sizes An array or string containing of size information.
62
+ * @type int $width A single width value used in the default `sizes` string.
62
63
* }
63
64
* @return string|bool A valid source size value for use in a 'sizes' attribute or false.
64
65
*/
65
- function tevkori_get_sizes ( $ id , $ size = 'thumbnail ' , $ args = null ) {
66
+ function tevkori_get_sizes ( $ id , $ size = 'medium ' , $ args = null ) {
66
67
67
- // See which image is being returned and bail if none is found.
68
- if ( ! $ img = image_downsize ( $ id , $ size ) ) {
68
+ // Try to get the image width from `$args` before calling `image_downsize()`.
69
+ if ( is_array ( $ args ) && ! empty ( $ args ['width ' ] ) ) {
70
+ $ img_width = (int ) $ args ['width ' ];
71
+ } elseif ( $ img = image_downsize ( $ id , $ size ) ) {
72
+ $ img_width = $ img [1 ];
73
+ }
74
+
75
+ // Bail early if ``$image_width` isn't set.
76
+ if ( ! $ img_width ) {
69
77
return false ;
70
78
}
71
79
72
- // Get the image width.
73
- $ img_width = $ img [ 1 ] . 'px ' ;
80
+ // Set the image width in pixels .
81
+ $ img_width = $ img_width . 'px ' ;
74
82
75
83
// Set up our default values.
76
84
$ defaults = array (
@@ -148,15 +156,16 @@ function tevkori_get_sizes( $id, $size = 'thumbnail', $args = null ) {
148
156
* @since 2.2.0
149
157
*
150
158
* @param int $id Image attachment ID.
151
- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
159
+ * @param string $size Optional. Name of image size. Default value: 'medium '.
152
160
* @param array $args {
153
161
* Optional. Arguments to retrieve posts.
154
162
*
155
163
* @type array|string $sizes An array or string containing of size information.
164
+ * @type int $width A single width value used in the default `sizes` string.
156
165
* }
157
166
* @return string|bool A valid source size list as a 'sizes' attribute or false.
158
167
*/
159
- function tevkori_get_sizes_string ( $ id , $ size = 'thumbnail ' , $ args = null ) {
168
+ function tevkori_get_sizes_string ( $ id , $ size = 'medium ' , $ args = null ) {
160
169
$ sizes = tevkori_get_sizes ( $ id , $ size , $ args );
161
170
162
171
return $ sizes ? 'sizes=" ' . $ sizes . '" ' : false ;
@@ -166,47 +175,51 @@ function tevkori_get_sizes_string( $id, $size = 'thumbnail', $args = null ) {
166
175
* Get an array of image sources candidates for use in a 'srcset' attribute.
167
176
*
168
177
* @param int $id Image attachment ID.
169
- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
178
+ * @param string $size Optional. Name of image size. Default value: 'medium '.
170
179
* @return array|bool An array of of srcset values or false.
171
180
*/
172
- function tevkori_get_srcset_array ( $ id , $ size = 'thumbnail ' ) {
181
+ function tevkori_get_srcset_array ( $ id , $ size = 'medium ' ) {
173
182
$ arr = array ();
174
183
175
- // See which image is being returned and bail if none is found .
176
- if ( ! $ img = wp_get_attachment_image_src ( $ id , $ size ) ) {
177
- return false ;
178
- }
184
+ // Get the intermediate size .
185
+ $ image = image_get_intermediate_size ( $ id , $ size );
186
+ // Get the post meta.
187
+ $ img_meta = wp_get_attachment_metadata ( $ id );
179
188
180
- // Break image data into url, width, and height.
181
- list ( $ img_url , $ img_width , $ img_height ) = $ img ;
189
+ // Extract the height and width from the intermediate or the full size.
190
+ $ img_width = ( $ image ) ? $ image ['width ' ] : $ img_meta ['width ' ];
191
+ $ img_height = ( $ image ) ? $ image ['height ' ] : $ img_meta ['height ' ];
182
192
183
- // If we have no width to work with, we should bail (see issue #118) .
184
- if ( 0 == $ img_width ) {
193
+ // Bail early if the width isn't greater that zero .
194
+ if ( ! $ img_width > 0 ) {
185
195
return false ;
186
196
}
187
197
188
- // Get the image meta data and bail if none is found.
189
- if ( ! is_array ( $ img_meta = wp_get_attachment_metadata ( $ id ) ) ) {
190
- return false ;
198
+ // Use the url from the intermediate size or build the url from the metadata.
199
+ if ( ! empty ( $ image ['url ' ] ) ) {
200
+ $ img_url = $ image ['url ' ];
201
+ } else {
202
+ $ uploads_dir = wp_upload_dir ();
203
+ $ img_file = ( $ image ) ? path_join ( dirname ( $ img_meta ['file ' ] ) , $ image ['file ' ] ) : $ img_meta ['file ' ];
204
+ $ img_url = $ uploads_dir ['baseurl ' ] . '/ ' . $ img_file ;
191
205
}
192
206
193
- // Build an array with image sizes.
194
207
$ img_sizes = $ img_meta ['sizes ' ];
195
208
196
209
// Add full size to the img_sizes array.
197
210
$ img_sizes ['full ' ] = array (
198
211
'width ' => $ img_meta ['width ' ],
199
212
'height ' => $ img_meta ['height ' ],
200
- 'file ' => basename ( $ img_meta ['file ' ] )
213
+ 'file ' => wp_basename ( $ img_meta ['file ' ] )
201
214
);
202
215
203
216
// Calculate the image aspect ratio.
204
217
$ img_ratio = $ img_height / $ img_width ;
205
218
206
219
/*
207
220
* Images that have been edited in WordPress after being uploaded will
208
- * contain a unique hash. We look for that hash and use it later to filter
209
- * out images that are leftovers from previous renditions .
221
+ * contain a unique hash. Look for that hash and use it later to filter
222
+ * out images that are leftovers from previous versions .
210
223
*/
211
224
$ img_edited = preg_match ( '/-e[0-9]{13}/ ' , $ img_url , $ img_edit_hash );
212
225
@@ -248,10 +261,10 @@ function tevkori_get_srcset_array( $id, $size = 'thumbnail' ) {
248
261
* @since 2.3.0
249
262
*
250
263
* @param int $id Image attachment ID.
251
- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
264
+ * @param string $size Optional. Name of image size. Default value: 'medium '.
252
265
* @return string|bool A 'srcset' value string or false.
253
266
*/
254
- function tevkori_get_srcset ( $ id , $ size = 'thumbnail ' ) {
267
+ function tevkori_get_srcset ( $ id , $ size = 'medium ' ) {
255
268
$ srcset_array = tevkori_get_srcset_array ( $ id , $ size );
256
269
257
270
if ( count ( $ srcset_array ) <= 1 ) {
@@ -267,10 +280,10 @@ function tevkori_get_srcset( $id, $size = 'thumbnail' ) {
267
280
* @since 2.1.0
268
281
*
269
282
* @param int $id Image attachment ID.
270
- * @param string $size Optional. Name of image size. Default value: 'thumbnail '.
283
+ * @param string $size Optional. Name of image size. Default value: 'medium '.
271
284
* @return string|bool A full 'srcset' string or false.
272
285
*/
273
- function tevkori_get_srcset_string ( $ id , $ size = 'thumbnail ' ) {
286
+ function tevkori_get_srcset_string ( $ id , $ size = 'medium ' ) {
274
287
$ srcset_value = tevkori_get_srcset ( $ id , $ size );
275
288
276
289
if ( empty ( $ srcset_value ) ) {
@@ -294,43 +307,41 @@ function tevkori_filter_content_images( $content ) {
294
307
$ uploads_dir = wp_upload_dir ();
295
308
$ path_to_upload_dir = $ uploads_dir ['baseurl ' ];
296
309
297
- preg_match_all ( '|<img ([^>]+ ' . $ path_to_upload_dir . '[^>]+)[\s?][\/?]>|i ' , $ content , $ matches );
310
+ // Pattern for matching all images with a `src` from the uploads directory.
311
+ $ pattern = '|<img ([^>]+ ' . preg_quote ( $ path_to_upload_dir ) . '[^>]+)>|i ' ;
312
+ preg_match_all ( $ pattern , $ content , $ matches );
298
313
299
314
$ images = $ matches [0 ];
300
315
$ ids = array ();
301
316
302
317
foreach ( $ images as $ image ) {
303
318
if ( preg_match ( '/wp-image-([0-9]+)/i ' , $ image , $ class_id ) ) {
304
319
(int ) $ id = $ class_id [1 ];
305
- if ( $ id ) {
320
+ if ( $ id ) {
306
321
$ ids [] = $ id ;
307
322
}
308
323
}
309
324
}
310
325
311
326
if ( 0 < count ( $ ids ) ) {
312
- /**
327
+ /*
313
328
* Warm object caches for use with wp_get_attachment_metadata.
314
329
*
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.
330
+ * To avoid making a database call for each image, a single query
331
+ * warms the object cache with the meta information for all images.
320
332
**/
321
- $ attachments = new WP_Query (array (
322
- 'post_type ' => 'attachment ' ,
323
- 'posts_per_page ' => '-1 ' ,
324
- 'post_status ' => 'inherit ' ,
325
- 'post__in ' => $ ids ,
326
- ));
333
+ _prime_post_caches ( $ ids , false , true );
327
334
}
328
335
329
- $ content = preg_replace_callback (
330
- '|<img ([^>]+ ' . $ path_to_upload_dir . '[^>]+)[\s?][\/?]>|i ' ,
331
- '_tevkori_filter_content_images_callback ' ,
332
- $ content
333
- );
336
+ foreach ( $ matches [0 ] as $ k => $ image ) {
337
+ $ match = array ( $ image , $ matches [1 ][$ k ] );
338
+ $ needle = $ image ;
339
+ $ replacement = _tevkori_filter_content_images_callback ( $ match );
340
+ if ( false === $ replacement ) {
341
+ continue ;
342
+ }
343
+ $ content = str_replace ( $ image , $ replacement , $ content );
344
+ }
334
345
335
346
return $ content ;
336
347
}
@@ -348,7 +359,6 @@ function _tevkori_filter_content_images_callback( $image ) {
348
359
}
349
360
350
361
list ( $ image_html , $ atts ) = $ image ;
351
- $ id = $ size = false ;
352
362
353
363
// Bail early if a 'srcset' attribute already exists.
354
364
if ( false !== strpos ( $ atts , 'srcset= ' ) ) {
@@ -366,31 +376,27 @@ function _tevkori_filter_content_images_callback( $image ) {
366
376
}
367
377
368
378
// Grab ID and size info from core classes.
369
- if ( preg_match ( '/wp-image-([0-9]+)/i ' , $ atts , $ class_id ) ) {
370
- (int ) $ id = $ class_id [1 ];
371
- }
372
- if ( preg_match ( '/size-([^\s|"]+)/i ' , $ atts , $ class_size ) ) {
373
- $ size = $ class_size [1 ];
374
- }
379
+ $ id = preg_match ( '/wp-image-([0-9]+)/i ' , $ atts , $ class_id ) ? (int ) $ class_id [1 ] : false ;
380
+ $ size = preg_match ( '/size-([^\s|"]+)/i ' , $ atts , $ class_size ) ? $ class_size [1 ] : false ;
381
+ $ width = preg_match ( '/ width="([0-9]+)"/ ' , $ atts , $ atts_width ) ? (int ) $ atts_width [1 ] : false ;
382
+ $ height = preg_match ( '/ height="([0-9]+)"/ ' , $ atts , $ atts_height ) ? (int ) $ atts_height [1 ] : false ;
375
383
376
384
if ( $ id && false === $ size ) {
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
- }
385
+ $ size = array (
386
+ $ width ,
387
+ $ height
388
+ );
383
389
}
384
390
385
391
/*
386
392
* 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.
393
+ * metadata to match the ' src' angainst the available sizes for an attachment.
388
394
*/
389
395
if ( ! $ size && ! empty ( $ id ) && $ meta = wp_get_attachment_metadata ( $ id ) ) {
390
396
391
397
preg_match ( '/src="([^"]+)"/ ' , $ atts , $ url );
392
398
393
- // Sanity check the ` src` value and bail early it doesn't exist.
399
+ // Sanity check the ' src' value and bail early it doesn't exist.
394
400
if ( ! $ url [1 ] ) {
395
401
return $ image_html ;
396
402
}
@@ -416,7 +422,18 @@ function _tevkori_filter_content_images_callback( $image ) {
416
422
417
423
// If we have an ID and size, try for 'srcset' and 'sizes' and update the markup.
418
424
if ( $ id && $ size && $ srcset = tevkori_get_srcset_string ( $ id , $ size ) ) {
419
- $ sizes = tevkori_get_sizes_string ( $ id , $ size );
425
+
426
+ // Pass height and width to `tevkori_get_sizes_string()`.
427
+ $ args = array (
428
+ 'width ' => $ width ,
429
+ 'height ' => $ height ,
430
+ );
431
+
432
+ $ sizes = tevkori_get_sizes_string ( $ id , $ size , $ args );
433
+
434
+ // Strip trailing slashes and whitespaces from the `$atts` string.
435
+ $ atts = trim ( rtrim ( $ atts , '/ ' ) );
436
+
420
437
$ image_html = "<img " . $ atts . " " . $ srcset . " " . $ sizes . " /> " ;
421
438
};
422
439
0 commit comments