|
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.0 |
| 11 | + * Version: 2.5.1 |
12 | 12 | * Author: The RICG
|
13 | 13 | * Author URI: http://responsiveimages.org/
|
14 | 14 | * License: GPL-2.0+
|
@@ -294,6 +294,38 @@ function tevkori_filter_content_images( $content ) {
|
294 | 294 | $uploads_dir = wp_upload_dir();
|
295 | 295 | $path_to_upload_dir = $uploads_dir['baseurl'];
|
296 | 296 |
|
| 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 | + |
297 | 329 | $content = preg_replace_callback(
|
298 | 330 | '|<img ([^>]+' . $path_to_upload_dir . '[^>]+)[\s?][\/?]>|i',
|
299 | 331 | '_tevkori_filter_content_images_callback',
|
@@ -342,75 +374,44 @@ function _tevkori_filter_content_images_callback( $image ) {
|
342 | 374 | }
|
343 | 375 |
|
344 | 376 | 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 | + } |
352 | 383 | }
|
353 | 384 |
|
354 | 385 | /*
|
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. |
357 | 388 | */
|
358 |
| - if ( false === $id || false === $size ) { |
| 389 | + if ( ! $size && ! empty( $id ) && $meta = wp_get_attachment_metadata( $id ) ) { |
| 390 | + |
359 | 391 | preg_match( '/src="([^"]+)"/', $atts, $url );
|
360 | 392 |
|
| 393 | + // Sanity check the `src` value and bail early it doesn't exist. |
361 | 394 | if ( ! $url[1] ) {
|
362 | 395 | return $image_html;
|
363 | 396 | }
|
364 | 397 |
|
365 | 398 | $image_filename = basename( $url[1] );
|
366 | 399 |
|
367 | 400 | /*
|
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. |
372 | 403 | */
|
373 |
| - if ( ! empty( $id ) ) { |
374 |
| - $meta = wp_get_attachment_metadata( $id ); |
| 404 | + if ( $image_filename === basename( $meta['file'] ) ) { |
| 405 | + $size = 'full'; |
375 | 406 | } 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; |
411 | 411 | }
|
412 | 412 | }
|
413 | 413 | }
|
| 414 | + |
414 | 415 | }
|
415 | 416 |
|
416 | 417 | // If we have an ID and size, try for 'srcset' and 'sizes' and update the markup.
|
|
0 commit comments