WordPress.org

Make WordPress Core

Changeset 37022


Ignore:
Timestamp:
03/16/2016 10:48:13 PM (3 years ago)
Author:
johnbillion
Message:

Media: When generating the base URL to be used in the srcset attribute, use an https scheme when the image base URL's host matches that of the current host, and the request is being served over HTTPS. This prevents mixed content warnings caused by http embedded media.

See #34945
Props joemcgill

Location:
trunk
Files:
2 edited

Legend:

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

    r37018 r37022  
    10271027    $upload_dir = wp_get_upload_dir();
    10281028    $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;
     1029
     1030    /*
     1031     * If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain
     1032     * (which is to say, when they share the domain name of the current request).
     1033     */
     1034    if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) {
     1035        $image_baseurl = set_url_scheme( $image_baseurl, 'https' );
     1036    }
    10291037
    10301038    /*
  • trunk/tests/phpunit/tests/media.php

    r37018 r37022  
    14811481
    14821482        $this->assertSame( $expected, $actual );
    1483     }
    14841483}
     1484
     1485    /**
     1486     * @ticket 34945
     1487     * @ticket 33641
     1488     */
     1489    function test_wp_get_attachment_image_with_https_on() {
     1490        // Mock meta for the image.
     1491        $image_meta = array(
     1492            'width'  => 1200,
     1493            'height' => 600,
     1494            'file'   => 'test.jpg',
     1495            'sizes'  => array(
     1496                'thumbnail' => array(
     1497                    'file'   => 'test-150x150.jpg',
     1498                    'width'  => 150,
     1499                    'height' => 150,
     1500                ),
     1501                'medium'    => array(
     1502                    'file'   => 'test-300x150.jpg',
     1503                    'width'  => 300,
     1504                    'height' => 150,
     1505                ),
     1506                'large'     => array(
     1507                    'file'   => 'test-1024x512.jpg',
     1508                    'width'  => 1024,
     1509                    'height' => 512,
     1510                ),
     1511            )
     1512        );
     1513
     1514        // Test using the large file size.
     1515        $size_array = array( 1024, 512 );
     1516        $image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
     1517
     1518        $_SERVER['HTTPS'] = 'on';
     1519
     1520        $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w';
     1521        $actual   = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
     1522
     1523        $this->assertSame( $expected, $actual );
     1524    }
     1525}
Note: See TracChangeset for help on using the changeset viewer.