From a8fe299c1c6a1fa0f26fc203bd0ff6fc0d98c693 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Fri, 10 May 2013 22:49:24 +0000 Subject: [PATCH] Return the requested image size in get_the_post_format_image() props kovshenin. fixes #24188. git-svn-id: https://develop.svn.wordpress.org/trunk@24240 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/media.php | 63 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 1addcdcc3e..0b28b2b93d 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -2453,13 +2453,33 @@ function get_the_post_format_image( $attached_size = 'full', &$post = null ) { $meta['image'] ); } + + $attachment_id = img_html_to_post_id( $meta['image'], $matched_html ); + if ( $attachment_id && $matched_html ) { + $meta['image'] = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $meta['image'] ); + $attachment = wp_get_attachment_image_src( $attachment_id, $attached_size ); + $attachment_width = ( ! empty( $attachment[1] ) ) ? $attachment[1] : 0; + + if ( $attachment_width && preg_match_all( '#width=([\'"])(.+?)\1#is', $meta['image'], $matches ) && ! empty( $matches ) ) + foreach ( $matches[2] as $width ) + if ( $width != $attachment_width ) + $meta['image'] = str_replace( $matches[0], sprintf( 'width="%d"', $attachment_width ), $meta['image'] ); + } + $image = do_shortcode( $meta['image'] ); } elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) { // not HTML, assume URL - $image = sprintf( '', esc_url( $meta['image'] ) ); + $attachment_id = attachment_url_to_postid( $meta['image'] ); + if ( $attachment_id ) + $image = wp_get_attachment_image( $attachment_id, $attached_size ); + else + $image = sprintf( '', esc_url( $meta['image'] ) ); } else { // assume HTML $image = $meta['image']; + $attachment_id = img_html_to_post_id( $image, $matched_html ); + if ( $attachment_id && $matched_html ) + $image = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $image ); } if ( false === strpos( $image, 'split_content = $content; $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html ); return $post->format_content[ $cache_key ]; @@ -2569,3 +2594,39 @@ function attachment_url_to_postid( $url ) { return 0; } + +/** + * Retrieve the attachment post id from HTML containing an image. + * + * @since 3.6.0 + * + * @param string $html The html, possibly with an image + * @param string $matched_html Passed by reference, will be set to to the matched img string + * @return int The attachment id if found, or 0. + */ +function img_html_to_post_id( $html, &$matched_html = null ) { + $attachment_id = 0; + + // Look for an tag + if ( ! preg_match( '#' . get_tag_regex( 'img' ) . '#i', $html, $matches ) || empty( $matches ) ) + return $attachment_id; + + $matched_html = $matches[0]; + + // Look for attributes. + if ( ! preg_match_all( '#(src|class)=([\'"])(.+?)\2#is', $matched_html, $matches ) || empty( $matches ) ) + return $attachment_id; + + $attr = array(); + foreach ( $matches[1] as $key => $attribute_name ) + $attr[ $attribute_name ] = $matches[3][ $key ]; + + if ( ! empty( $attr['class'] ) && false !== strpos( $attr['class'], 'wp-image-' ) ) + if ( preg_match( '#wp-image-([0-9]+)#i', $attr['class'], $matches ) ) + $attachment_id = absint( $matches[1] ); + + if ( ! $attachment_id && ! empty( $attr['src'] ) ) + $attachment_id = attachment_url_to_postid( $attr['src'] ); + + return $attachment_id; +} \ No newline at end of file