After [35402], don't unnecessary run wp_get_attachment_metadata(), wp_get_attachment_image_srcset(), and wp_get_attachment_image_sizes() in wp_img_add_srcset_and_sizes().

See #34379.
Built from https://develop.svn.wordpress.org/trunk@35405


git-svn-id: http://core.svn.wordpress.org/trunk@35369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2015-10-26 18:42:24 +00:00
parent 1e59e53dd2
commit 1acf9e9e49
2 changed files with 17 additions and 15 deletions

View File

@ -1145,7 +1145,7 @@ function wp_img_add_srcset_and_sizes( $image ) {
$size = preg_match( '/size-([^\s|"]+)/i', $image, $match_size ) ? $match_size[1] : false; $size = preg_match( '/size-([^\s|"]+)/i', $image, $match_size ) ? $match_size[1] : false;
$width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : false; $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : false;
if ( $id && false === $size ) { if ( $id && ! $size ) {
$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false; $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false;
if ( $width && $height ) { if ( $width && $height ) {
@ -1153,18 +1153,18 @@ function wp_img_add_srcset_and_sizes( $image ) {
} }
} }
$meta = wp_get_attachment_metadata( $id );
/* /*
* If attempts to parse the size value failed, attempt to use the image * If attempts to parse the size value failed, attempt to use the image
* metadata to match the 'src' against the available sizes for an attachment. * metadata to match the 'src' against the available sizes for an attachment.
*/ */
if ( ! $size && ! empty( $id ) && is_array( $meta ) ) { if ( $id && ! $size ) {
$meta = wp_get_attachment_metadata( $id );
// Parse the image src value from the img element. // Parse the image src value from the img element.
$src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false; $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false;
// Return early if the src value is empty. // Return early if the metadata does not exist or the src value is empty.
if ( ! $src ) { if ( ! $meta || ! $src ) {
return $image; return $image;
} }
@ -1187,16 +1187,18 @@ function wp_img_add_srcset_and_sizes( $image ) {
} }
$srcset = wp_get_attachment_image_srcset( $id, $size );
$sizes = wp_get_attachment_image_sizes( $id, $size, $width );
// If ID and size exist, try for 'srcset' and 'sizes' and update the markup. // If ID and size exist, try for 'srcset' and 'sizes' and update the markup.
if ( $id && $size && $srcset && $sizes ) { if ( $id && $size ) {
// Format the srcset and sizes string and escape attributes. $srcset = wp_get_attachment_image_srcset( $id, $size );
$srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) ); $sizes = wp_get_attachment_image_sizes( $id, $size, $width );
// Add srcset and sizes attributes to the image markup. if ( $srcset && $sizes ) {
$image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image ); // Format the srcset and sizes string and escape attributes.
$srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) );
// Add srcset and sizes attributes to the image markup.
$image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image );
}
} }
return $image; return $image;

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-beta1-35404'; $wp_version = '4.4-beta1-35405';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.