| | 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 | |
| | 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 | } |
| | 2633 | No newline at end of file |