Ticket #34379: 34379.3.patch
| File 34379.3.patch, 3.4 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/media.php
809 809 if ( empty($default_attr['alt']) ) 810 810 $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title 811 811 812 $attr = wp_parse_args( $attr, $default_attr ); 813 $srcset = wp_get_attachment_image_srcset( $attachment_id, $size ); 814 $sizes = wp_get_attachment_image_sizes( $attachment_id, $size, $width ); 812 $attr = wp_parse_args( $attr, $default_attr ); 815 813 816 814 // Generate srcset and sizes if not already present. 817 if ( empty( $attr['srcset'] ) && $srcset && $sizes ) { 818 $attr['srcset'] = $srcset; 815 if ( empty( $attr['srcset'] ) ) { 816 $srcset = wp_get_attachment_image_srcset( $attachment_id, $size ); 817 $sizes = wp_get_attachment_image_sizes( $attachment_id, $size, $width ); 819 818 820 if ( empty( $attr['sizes'] ) ) { 821 $attr['sizes'] = $sizes; 819 if ( $srcset && $sizes ) { 820 $attr['srcset'] = $srcset; 821 822 if ( empty( $attr['sizes'] ) ) { 823 $attr['sizes'] = $sizes; 824 } 822 825 } 823 826 } 824 827 … … 1142 1145 $size = preg_match( '/size-([^\s|"]+)/i', $image, $match_size ) ? $match_size[1] : false; 1143 1146 $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : false; 1144 1147 1145 if ( $id && false ===$size ) {1148 if ( $id && ! $size ) { 1146 1149 $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false; 1147 1150 1148 1151 if ( $width && $height ) { … … 1150 1153 } 1151 1154 } 1152 1155 1153 $meta = wp_get_attachment_metadata( $id );1154 1155 1156 /* 1156 1157 * If attempts to parse the size value failed, attempt to use the image 1157 1158 * metadata to match the 'src' against the available sizes for an attachment. 1158 1159 */ 1159 if ( ! $size && ! empty( $id ) && is_array( $meta ) ) { 1160 if ( $id && ! $size ) { 1161 $meta = wp_get_attachment_metadata( $id ); 1162 1160 1163 // Parse the image src value from the img element. 1161 1164 $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false; 1162 1165 1163 // Return early if the src value is empty.1164 if ( ! $ src ) {1166 // Return early if the metadata does not exist or the src value is empty. 1167 if ( ! $meta || ! $src ) { 1165 1168 return $image; 1166 1169 } 1167 1170 … … 1184 1187 1185 1188 } 1186 1189 1187 $srcset = wp_get_attachment_image_srcset( $id, $size );1188 $sizes = wp_get_attachment_image_sizes( $id, $size, $width );1189 1190 1190 // If ID and size exist, try for 'srcset' and 'sizes' and update the markup. 1191 if ( $id && $size && $srcset && $sizes) {1192 // Format the srcset and sizes string and escape attributes.1193 $s rcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes));1191 if ( $id && $size ) { 1192 $srcset = wp_get_attachment_image_srcset( $id, $size ); 1193 $sizes = wp_get_attachment_image_sizes( $id, $size, $width ); 1194 1194 1195 // Add srcset and sizes attributes to the image markup. 1196 $image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image ); 1195 if ( $srcset && $sizes ) { 1196 // Format the srcset and sizes string and escape attributes. 1197 $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) ); 1198 1199 // Add srcset and sizes attributes to the image markup. 1200 $image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image ); 1201 } 1197 1202 } 1198 1203 1199 1204 return $image;