Make WordPress Core

Changeset 37002


Ignore:
Timestamp:
03/16/2016 02:17:19 AM (7 years ago)
Author:
azaozz
Message:

Responsive images: do not attempt to create srcset when the image meta is missing or corrupted.

Props overclokk, jaspermdegroot, joemcgill.
Fixes #35480.

Location:
trunk
Files:
2 edited

Legend:

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

    r36887 r37002  
    986986    $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
    987987
    988     if ( empty( $image_meta['sizes'] ) ) {
     988    if ( empty( $image_meta['sizes'] ) || strlen( $image_meta['file'] ) < 4 ) {
    989989        return false;
    990990    }
     
    10601060     */
    10611061    foreach ( $image_sizes as $image ) {
     1062
     1063        // Check if image meta isn't corrupted.
     1064        if ( ! is_array( $image ) ) {
     1065            continue;
     1066        }
    10621067
    10631068        // If the file name is part of the `src`, we've confirmed a match.
  • trunk/tests/phpunit/tests/media.php

    r36565 r37002  
    11301130
    11311131    /**
     1132     * @ticket 35480
     1133     */
     1134    function test_wp_calculate_image_srcset_corrupted_image_meta() {
     1135        $size_array = array( 300, 150 );
     1136        $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png';
     1137        $image_meta = array(
     1138            'width' => 1600,
     1139            'height' => 800,
     1140            'file' => '2015/12/test.png',
     1141            'sizes' => array(
     1142                'thumbnail' => array(
     1143                    'file' => 'test-150x150.png',
     1144                    'width' => 150,
     1145                    'height' => 150,
     1146                    'mime-type' => 'image/png',
     1147                ),
     1148                'medium' => array(
     1149                    'file' => 'test-300x150.png',
     1150                    'width' => 300,
     1151                    'height' => 150,
     1152                    'mime-type' => 'image/png',
     1153                ),
     1154                'medium_large' => array(
     1155                    'file' => 'test-768x384.png',
     1156                    'width' => 768,
     1157                    'height' => 384,
     1158                    'mime-type' => 'image/png',
     1159                ),
     1160                'large' => array(
     1161                    'file' => 'test-1024x512.png',
     1162                    'width' => 1024,
     1163                    'height' => 512,
     1164                    'mime-type' => 'image/png',
     1165                ),
     1166            ),
     1167        );
     1168
     1169        $srcset = array(
     1170            300  => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w',
     1171            768  => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w',
     1172            1024 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w',
     1173            1600 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 1600w',
     1174        );
     1175
     1176        // No sizes array
     1177        $image_meta1 = $image_meta;
     1178        unset( $image_meta1['sizes'] );
     1179        $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta1 ) );
     1180
     1181        // Sizes is string instead of array; only full size available means no srcset.
     1182        $image_meta2 = $image_meta;
     1183        $image_meta2['sizes'] = '';
     1184        $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta2 ) );
     1185
     1186        // File name is incorrect
     1187        $image_meta4 = $image_meta;
     1188        $image_meta4['file'] = '/';
     1189        $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta4 ) );
     1190
     1191        // Intermediate size is string instead of array.
     1192        $image_meta3 = $image_meta;
     1193        $image_meta3['sizes']['medium_large'] = '';
     1194        unset( $srcset[768] );
     1195        $expected_srcset = implode( ', ', $srcset );
     1196        $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta3 ) );
     1197    }
     1198
     1199    /**
    11321200     * @ticket 33641
    11331201     */
Note: See TracChangeset for help on using the changeset viewer.