Make WordPress Core

Changeset 48329


Ignore:
Timestamp:
07/05/2020 11:30:36 PM (3 years ago)
Author:
azaozz
Message:

Media:

  • Introduce wp_image_file_matches_image_meta() utility function to check whether the image meta (retrieved by attachment ID) matches an image path or URI. A mismatch may happen in some cases, for example after the posts have been exported from one website and imported in another.
  • Add unit tests for the new function.
  • Improve wp_image_src_get_dimensions() a bit and use the new function to prevent these edge cases.

Fixes #50543.

Location:
trunk
Files:
2 edited

Legend:

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

    r48288 r48329  
    14991499
    15001500/**
     1501 * Determines if the image meta data is for the image source file.
     1502 *
     1503 * The image meta data is retrieved by attachment post ID. In some cases the post IDs may change.
     1504 * For example when the website is exported and imported at another website. Then the
     1505 * attachment post IDs that are in post_content for the exported website may not match
     1506 * the same attachments at the new website.
     1507 *
     1508 * @since 5.5.0
     1509 *
     1510 * @param string $image_location  The full path or URI to the image file.
     1511 * @param array  $image_meta      The attachment meta data as returned by 'wp_get_attachment_metadata()'.
     1512 * @return bool Whether the image meta is for this image file.
     1513 */
     1514function wp_image_file_matches_image_meta( $image_location, $image_meta ) {
     1515    $match = false;
     1516
     1517    // Ensure the $image_meta is valid.
     1518    if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) {
     1519        // Remove quiery args if image URI.
     1520        list( $image_location ) = explode( '?', $image_location );
     1521
     1522        // Check if the relative image path from the image meta is at the end of $image_location.
     1523        if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) {
     1524            $match = true;
     1525        }
     1526
     1527        if ( ! empty( $image_meta['sizes'] ) ) {
     1528            // Retrieve the uploads sub-directory from the full size image.
     1529            $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     1530
     1531            if ( $dirname ) {
     1532                $dirname = trailingslashit( $dirname );
     1533            }
     1534
     1535            foreach ( $image_meta['sizes'] as $image_size_data ) {
     1536                $relative_path = $dirname . $image_size_data['file'];
     1537
     1538                if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) {
     1539                    $match = true;
     1540                    break;
     1541                }
     1542            }
     1543        }
     1544    }
     1545
     1546    /**
     1547     * Filter whether an image path or URI matches image meta.
     1548     *
     1549     * @since 5.5.0
     1550     *
     1551     * @param bool   $match          Whether the image relative path from the image meta
     1552     *                               matches the end of the URI or path to the image file.
     1553     * @param string $image_location Full path or URI to the tested image file.
     1554     * @param array  $image_meta     The image meta data being tested.
     1555     */
     1556    return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta );
     1557}
     1558
     1559/**
    15011560 * Determines an image's width and height dimensions based on the source file.
    15021561 *
     
    15091568 */
    15101569function wp_image_src_get_dimensions( $image_src, $image_meta ) {
    1511     $image_filename = wp_basename( $image_src );
    1512 
    1513     if ( wp_basename( $image_meta['file'] ) === $image_filename ) {
     1570    if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta ) ) {
     1571        return false;
     1572    }
     1573
     1574    // Is it a full size image?
     1575    if ( strpos( $image_src, $image_meta['file'] ) !== false ) {
    15141576        return array(
    15151577            (int) $image_meta['width'],
     
    15181580    }
    15191581
    1520     foreach ( $image_meta['sizes'] as $image_size_data ) {
    1521         if ( $image_filename === $image_size_data['file'] ) {
    1522             return array(
    1523                 (int) $image_size_data['width'],
    1524                 (int) $image_size_data['height'],
    1525             );
     1582    if ( ! empty( $image_meta['sizes'] ) ) {
     1583        $src_filename = wp_basename( $image_src );
     1584
     1585        foreach ( $image_meta['sizes'] as $image_size_data ) {
     1586            if ( $src_filename === $image_size_data['file'] ) {
     1587                return array(
     1588                    (int) $image_size_data['width'],
     1589                    (int) $image_size_data['height'],
     1590                );
     1591            }
    15261592        }
    15271593    }
  • trunk/tests/phpunit/tests/media.php

    r48275 r48329  
    28092809        );
    28102810    }
     2811
     2812    /**
     2813     * @ticket 50543
     2814     */
     2815    function test_wp_image_file_matches_image_meta() {
     2816        $image_meta       = wp_get_attachment_metadata( self::$large_id );
     2817        $image_src_full   = wp_get_attachment_image_url( self::$large_id, 'full' );
     2818        $image_src_medium = wp_get_attachment_image_url( self::$large_id, 'medium' );
     2819
     2820        $this->assertTrue( wp_image_file_matches_image_meta( $image_src_full, $image_meta ) );
     2821        $this->assertTrue( wp_image_file_matches_image_meta( $image_src_medium, $image_meta ) );
     2822    }
     2823
     2824    /**
     2825     * @ticket 50543
     2826     */
     2827    function test_wp_image_file_matches_image_meta_no_subsizes() {
     2828        $image_meta = wp_get_attachment_metadata( self::$large_id );
     2829        $image_src  = wp_get_attachment_image_url( self::$large_id, 'full' );
     2830        $image_meta['sizes'] = array();
     2831
     2832        $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2833    }
     2834
     2835    /**
     2836     * @ticket 50543
     2837     */
     2838    function test_wp_image_file_matches_image_meta_invalid_meta() {
     2839        $image_meta = ''; // Attachment is not an image.
     2840        $image_src  = $this->img_url;
     2841
     2842        $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2843    }
     2844
     2845    /**
     2846     * @ticket 50543
     2847     */
     2848    function test_wp_image_file_matches_image_meta_different_meta() {
     2849        $image_meta = wp_get_attachment_metadata( self::$large_id );
     2850        $image_src  = $this->img_url; // Different image.
     2851
     2852        $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     2853    }
    28112854}
    28122855
Note: See TracChangeset for help on using the changeset viewer.