Make WordPress Core


Ignore:
Timestamp:
05/10/2013 10:49:24 PM (12 years ago)
Author:
markjaquith
Message:

Return the requested image size in get_the_post_format_image()

props kovshenin. fixes #24188.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r24198 r24240  
    24542454                );
    24552455            }
     2456
     2457            $attachment_id = img_html_to_post_id( $meta['image'], $matched_html );
     2458            if ( $attachment_id && $matched_html ) {
     2459                $meta['image'] = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $meta['image'] );
     2460                $attachment = wp_get_attachment_image_src( $attachment_id, $attached_size );
     2461                $attachment_width = ( ! empty( $attachment[1] ) ) ? $attachment[1] : 0;
     2462
     2463                if ( $attachment_width && preg_match_all( '#width=([\'"])(.+?)\1#is', $meta['image'], $matches ) && ! empty( $matches ) )
     2464                    foreach ( $matches[2] as $width )
     2465                        if ( $width != $attachment_width )
     2466                            $meta['image'] = str_replace( $matches[0], sprintf( 'width="%d"', $attachment_width ), $meta['image'] );
     2467            }
     2468
    24562469            $image = do_shortcode( $meta['image'] );
    24572470        } elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) {
    24582471            // not HTML, assume URL
    2459             $image = sprintf( '<img src="%s" alt="" />', esc_url( $meta['image'] ) );
     2472            $attachment_id = attachment_url_to_postid( $meta['image'] );
     2473            if ( $attachment_id )
     2474                $image = wp_get_attachment_image( $attachment_id, $attached_size );
     2475            else
     2476                $image = sprintf( '<img src="%s" alt="" />', esc_url( $meta['image'] ) );
    24602477        } else {
    24612478            // assume HTML
    24622479            $image = $meta['image'];
     2480            $attachment_id = img_html_to_post_id( $image, $matched_html );
     2481            if ( $attachment_id && $matched_html )
     2482                $image = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $image );
    24632483        }
    24642484
     
    25332553    if ( ! empty( $htmls ) ) {
    25342554        $html = reset( $htmls );
     2555
     2556        $attachment_id = img_html_to_post_id( $html, $matched_html );
     2557        if ( $attachment_id && $matched_html )
     2558            $html = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $html );
     2559
    25352560        $post->split_content = $content;
    25362561        $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html );
     
    25702595    return 0;
    25712596}
     2597
     2598/**
     2599 * Retrieve the attachment post id from HTML containing an image.
     2600 *
     2601 * @since 3.6.0
     2602 *
     2603 * @param string $html The html, possibly with an image
     2604 * @param string $matched_html Passed by reference, will be set to to the matched img string
     2605 * @return int The attachment id if found, or 0.
     2606 */
     2607function img_html_to_post_id( $html, &$matched_html = null ) {
     2608    $attachment_id = 0;
     2609
     2610    // Look for an <img /> tag
     2611    if ( ! preg_match( '#' . get_tag_regex( 'img' ) .  '#i', $html, $matches ) || empty( $matches ) )
     2612        return $attachment_id;
     2613
     2614    $matched_html = $matches[0];
     2615
     2616    // Look for attributes.
     2617    if ( ! preg_match_all( '#(src|class)=([\'"])(.+?)\2#is', $matched_html, $matches ) || empty( $matches ) )
     2618        return $attachment_id;
     2619
     2620    $attr = array();
     2621    foreach ( $matches[1] as $key => $attribute_name )
     2622        $attr[ $attribute_name ] = $matches[3][ $key ];
     2623
     2624    if ( ! empty( $attr['class'] ) && false !== strpos( $attr['class'], 'wp-image-' ) )
     2625        if ( preg_match( '#wp-image-([0-9]+)#i', $attr['class'], $matches ) )
     2626            $attachment_id = absint( $matches[1] );
     2627
     2628    if ( ! $attachment_id && ! empty( $attr['src'] ) )
     2629        $attachment_id = attachment_url_to_postid( $attr['src'] );
     2630
     2631    return $attachment_id;
     2632}
Note: See TracChangeset for help on using the changeset viewer.