| 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 | |
| 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 | */ |
| 2615 | function 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 |