Make WordPress Core

Ticket #24188: 24188.diff

File 24188.diff, 3.5 KB (added by kovshenin, 12 years ago)
  • wp-includes/media.php

     
    24602460                                        $meta['image']
    24612461                                );
    24622462                        }
     2463
     2464                        $attachment_id = img_html_to_post_id( $meta['image'], $matched_html );
     2465                        if ( $attachment_id && $matched_html ) {
     2466                                $meta['image'] = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $meta['image'] );
     2467                                $attachment = wp_get_attachment_image_src( $attachment_id, $attached_size );
     2468                                $attachment_width = ( ! empty( $attachment[1] ) ) ? $attachment[1] : 0;
     2469
     2470                                if ( $attachment_width && preg_match_all( '#width=[\'"](.+?)[\'"]#is', $meta['image'], $matches ) && ! empty( $matches ) )
     2471                                        foreach ( $matches[1] as $width )
     2472                                                if ( $width != $attachment_width )
     2473                                                        $meta['image'] = str_replace( $matches[0], sprintf( 'width="%d"', $attachment_width ), $meta['image'] );
     2474                        }
     2475
    24632476                        $image = do_shortcode( $meta['image'] );
    24642477                } elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) {
    24652478                        // not HTML, assume URL
    2466                         $image = sprintf( '<img src="%s" alt="" />', esc_url( $meta['image'] ) );
     2479                        $attachment_id = attachment_url_to_postid( $meta['image'] );
     2480                        if ( $attachment_id )
     2481                                $image = wp_get_attachment_image( $attachment_id, $attached_size );
     2482                        else
     2483                                $image = sprintf( '<img src="%s" alt="" />', esc_url( $meta['image'] ) );
    24672484                } else {
    24682485                        // assume HTML
    24692486                        $image = $meta['image'];
     2487                        $attachment_id = img_html_to_post_id( $image, $matched_html );
     2488                        if ( $attachment_id && $matched_html )
     2489                                $image = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $image );
    24702490                }
    24712491
    24722492                if ( false === strpos( $image, '<a ' ) )
     
    25402560        $htmls = get_content_images( $content, true, true, 1 );
    25412561        if ( ! empty( $htmls ) ) {
    25422562                $html = reset( $htmls );
     2563
     2564                $attachment_id = img_html_to_post_id( $html, $matched_html );
     2565                if ( $attachment_id && $matched_html )
     2566                        $html = str_replace( $matched_html, wp_get_attachment_image( $attachment_id, $attached_size ), $html );
     2567
    25432568                $post->split_content = $content;
    25442569                $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html );
    25452570                return $post->format_content[ $cache_key ];
     
    25772602
    25782603        return 0;
    25792604}
     2605
     2606/**
     2607 * Retrieve the attachment post id from HTML containing an image.
     2608 *
     2609 * @since 3.6.0
     2610 *
     2611 * @param string $html The html, possibly with an image
     2612 * @param string $matched_html Passed by reference, will be set to to the matched img string
     2613 * @return int The attachment id if found, or 0.
     2614 */
     2615function img_html_to_post_id( $html, &$matched_html = null ) {
     2616        $attachment_id = 0;
     2617
     2618        // Look for an <img /> tag
     2619        if ( ! preg_match( '#' . get_tag_regex( 'img' ) .  '#i', $html, $matches ) || empty( $matches ) )
     2620                return $attachment_id;
     2621
     2622        $matched_html = $matches[0];
     2623
     2624        // Look for attributes.
     2625        if ( ! preg_match_all( '#(src|class)=[\'"](.+?)[\'"]#is', $matched_html, $matches ) || empty( $matches ) )
     2626                return $attachment_id;
     2627
     2628        $attr = array();
     2629        foreach ( $matches[1] as $key => $attribute_name )
     2630                $attr[ $attribute_name ] = $matches[2][ $key ];
     2631
     2632        if ( ! empty( $attr['class'] ) && false !== strpos( $attr['class'], 'wp-image-' ) )
     2633                if ( preg_match( '#wp-image-([0-9]+)#i', $attr['class'], $matches ) )
     2634                        $attachment_id = absint( $matches[1] );
     2635
     2636        if ( ! $attachment_id && ! empty( $src ) )
     2637                $attachment_id = attachment_url_to_postid( $src );
     2638
     2639        return $attachment_id;
     2640}
     2641 No newline at end of file