Changeset 24240 for trunk/wp-includes/media.php
- Timestamp:
- 05/10/2013 10:49:24 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/media.php
r24198 r24240 2454 2454 ); 2455 2455 } 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 2456 2469 $image = do_shortcode( $meta['image'] ); 2457 2470 } elseif ( ! preg_match( '#<[^>]+>#', $meta['image'] ) ) { 2458 2471 // 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'] ) ); 2460 2477 } else { 2461 2478 // assume HTML 2462 2479 $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 ); 2463 2483 } 2464 2484 … … 2533 2553 if ( ! empty( $htmls ) ) { 2534 2554 $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 2535 2560 $post->split_content = $content; 2536 2561 $post->format_content[ $cache_key ] = sprintf( $link_fmt, $html ); … … 2570 2595 return 0; 2571 2596 } 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 */ 2607 function 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.