Make WordPress Core

Ticket #34945: 34945.4.diff

File 34945.4.diff, 5.0 KB (added by jeremyfelt, 8 years ago)
  • src/wp-includes/deprecated.php

     
    37473747 * @return string The base URL.
    37483748 */
    37493749function _wp_upload_dir_baseurl() {
     3750        _deprecated_function( __FUNCTION__, '4.5' );
    37503751        $upload_dir = wp_get_upload_dir();
    37513752        return $upload_dir['baseurl'];
    37523753}
  • src/wp-includes/media.php

     
    903903}
    904904
    905905/**
     906 * Sets the protocol, caches and returns the base URL of the uploads directory.
     907 *
     908 * @since 4.5.0
     909 * @access private
     910 *
     911 * @param string $baseurl The base URL of the uploads directory.
     912 * @return string The base URL, cached.
     913 */
     914function _ssl_image_baseurl( $baseurl ) {
     915        static $tested = array();
     916
     917        $blog_id = get_current_blog_id();
     918
     919        if ( empty( $tested[$blog_id] ) ) {
     920                /*
     921                 * If currently on SSL, prefer HTTPS URLs when we know they're supported by the domain
     922                 * (which is to say, when they share the domain name of the current SSL page).
     923                 */
     924                if ( is_ssl() && 'https' !== substr( $baseurl, 0, 5 ) && parse_url( $baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) {
     925                        $baseurl = set_url_scheme( $baseurl, 'https' );
     926                }
     927
     928                $tested[$blog_id] = $baseurl;
     929        }
     930
     931        return $tested[$blog_id];
     932}
     933
     934/**
    906935 * Get the image size as array from its meta data.
    907936 *
    908937 * Used for responsive images.
     
    10251054        }
    10261055
    10271056        $upload_dir = wp_get_upload_dir();
    1028         $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;
     1057
     1058        // Set URL schema when front-end is https.
     1059        $image_baseurl = _ssl_image_baseurl( $upload_dir['baseurl'] );
     1060        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10291061
    10301062        /*
    10311063         * Images that have been edited in WordPress after being uploaded will
  • src/wp-includes/post.php

     
    50155015        if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) {
    50165016                // Get upload directory.
    50175017                if ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) {
     5018                        $image_baseurl = _ssl_image_baseurl( $uploads['baseurl'] );
    50185019                        // Check that the upload base exists in the file location.
    50195020                        if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
    50205021                                // Replace file location with url location.
    5021                                 $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
     5022                                $url = str_replace( $uploads['basedir'], $image_baseurl, $file );
    50225023                        } elseif ( false !== strpos($file, 'wp-content/uploads') ) {
    50235024                                // Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
    5024                                 $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file );
     5025                                $url = trailingslashit( $image_baseurl . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file );
    50255026                        } else {
    50265027                                // It's a newly-uploaded file, therefore $file is relative to the basedir.
    5027                                 $url = $uploads['baseurl'] . "/$file";
     5028                                $url = trailingslashit( $image_baseurl ) . $file;
    50285029                        }
    50295030                }
    50305031        }
  • tests/phpunit/tests/media.php

     
    14071407                $actual     = wp_make_content_images_responsive( $unfiltered );
    14081408
    14091409                $this->assertSame( $expected, $actual );
     1410}
     1411
     1412        /**
     1413         * @ticket 34945
     1414         * @ticket 33641
     1415         */
     1416        function test_wp_get_attachment_image_with_https_on() {
     1417                global $blog_id;
     1418               
     1419                // Save server data for cleanup
     1420                $is_ssl = is_ssl();
     1421                $current_blog_id = get_current_blog_id();
     1422
     1423                // Mock meta for the image.
     1424                $image_meta = array(
     1425                        'width' => 1200,
     1426                        'height' => 600,
     1427                        'file' => 'test.jpg',
     1428                        'sizes' => array(
     1429                                'thumbnail' => array(
     1430                                        'file' => 'test-150x150.jpg',
     1431                                        'width' => 150,
     1432                                        'height' => 150,
     1433                                ),
     1434                                'medium' => array(
     1435                                        'file' => 'test-300x150.jpg',
     1436                                        'width' => 300,
     1437                                        'height' => 150,
     1438                                ),
     1439                                'large' => array(
     1440                                        'file' => 'test-1024x512.jpg',
     1441                                        'width' => 1024,
     1442                                        'height' => 512,
     1443                                ),
     1444                        )
     1445                );
     1446
     1447                // Test using the large file size.
     1448                $size_array = array(1024, 512);
     1449                $image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
     1450
     1451                $_SERVER['HTTPS'] = 'on';
     1452                $blog_id = 42; // Used to bust the cache in _ssl_image_baseurl()
     1453
     1454                $expected = 'https://example.org/wp-content/uploads/test-300x150.jpg 300w, https://example.org/wp-content/uploads/test-1024x512.jpg 1024w, https://example.org/wp-content/uploads/test.jpg 1200w';
     1455
     1456                $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     1457
     1458                // Cleanup
     1459                $_SERVER['HTTPS'] = $is_ssl;
     1460                $blog_id = $current_blog_id;
    14101461        }
    14111462}