Make WordPress Core

Ticket #50543: 50543.2.diff

File 50543.2.diff, 5.4 KB (added by azaozz, 4 years ago)
  • src/wp-includes/media.php

     
    14981498}
    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 *
    15031562 * @since 5.5.0
     
    15081567 *                     or false if dimensions cannot be determined.
    15091568 */
    15101569function wp_image_src_get_dimensions( $image_src, $image_meta ) {
    1511         $image_filename = wp_basename( $image_src );
     1570        if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta ) ) {
     1571                return false;
     1572        }
    15121573
    1513         if ( wp_basename( $image_meta['file'] ) === $image_filename ) {
     1574        // Is it a full size image?
     1575        if ( strpos( $image_src, $image_meta['file'] ) !== false ) {
    15141576                return array(
    15151577                        (int) $image_meta['width'],
    15161578                        (int) $image_meta['height'],
     
    15171579                );
    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        }
    15281594
  • tests/phpunit/tests/media.php

     
    28082808                        'arbitrary context => true'       => array( 'something_completely_arbitrary', true ),
    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
    28132856/**