Make WordPress Core

Ticket #34945: 34945.6.diff

File 34945.6.diff, 6.9 KB (added by joemcgill, 9 years ago)
  • src/wp-includes/deprecated.php

    diff --git src/wp-includes/deprecated.php src/wp-includes/deprecated.php
    index 5d871be..6954ebd 100644
    function popuplinks( $text ) { 
    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

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 6b0187a..ac56119 100644
    function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { 
    351351         */
    352352        $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
    353353
     354        $site_url = get_option( 'siteurl' );
     355        if ( 'https' === substr( $img_src, 0, 5 ) && 'http' === parse_url( $site_url, PHP_URL_SCHEME ) && parse_url( $img_src, PHP_URL_HOST ) === parse_url( $site_url, PHP_URL_HOST ) ) {
     356                $img_src = set_url_scheme( $img_src, 'http' );
     357        }
     358
    354359        $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
    355360
    356361        /**
    function _wp_get_attachment_relative_path( $file ) { 
    903908}
    904909
    905910/**
     911 * Sets the protocol, caches and returns the base URL of the uploads directory.
     912 *
     913 * @since 4.5.0
     914 * @access private
     915 *
     916 * @param string $baseurl The base URL of the uploads directory.
     917 * @return string The base URL, cached.
     918 */
     919function _ssl_image_baseurl( $baseurl ) {
     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        return $baseurl;
     929}
     930
     931/**
    906932 * Get the image size as array from its meta data.
    907933 *
    908934 * Used for responsive images.
    function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 
    10251051        }
    10261052
    10271053        $upload_dir = wp_get_upload_dir();
    1028         $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;
     1054
     1055        // Set URL schema when front-end is https.
     1056        $image_baseurl = _ssl_image_baseurl( $upload_dir['baseurl'] );
     1057        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10291058
    10301059        /*
    10311060         * Images that have been edited in WordPress after being uploaded will
    function wp_prepare_attachment_for_js( $attachment ) { 
    29983027
    29993028        $attachment_url = wp_get_attachment_url( $attachment->ID );
    30003029
     3030        $site_url = get_option( 'siteurl' );
     3031
     3032        if ( 'https' === substr( $attachment_url, 0, 5 ) && 'http' === parse_url( $site_url, PHP_URL_SCHEME ) && parse_url( $attachment_url, PHP_URL_HOST ) === parse_url( $site_url, PHP_URL_HOST ) ) {
     3033                $attachment_url = set_url_scheme( $attachment_url, 'http' );
     3034        }
     3035
    30013036        $response = array(
    30023037                'id'          => $attachment->ID,
    30033038                'title'       => $attachment->post_title,
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index d5f4f0e..5a94321 100644
    function wp_get_attachment_url( $post_id = 0 ) { 
    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

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index d5ae231..1a6c270 100644
    EOF; 
    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}
  • tests/phpunit/tests/post/attachments.php

    diff --git tests/phpunit/tests/post/attachments.php tests/phpunit/tests/post/attachments.php
    index 3df4ce4..7dcf9c8 100644
    class Tests_Post_Attachments extends WP_UnitTestCase { 
    389389        /**
    390390        * @ticket 15928
    391391        */
    392         public function test_wp_get_attachment_url_should_not_force_https_when_administering_over_https_but_siteurl_is_not_https() {
     392        public function test_wp_get_attachment_url_should_force_https_when_administering_over_https_but_siteurl_is_not_https() {
    393393                $siteurl = get_option( 'siteurl' );
    394394                update_option( 'siteurl', set_url_scheme( $siteurl, 'http' ) );
    395395
    class Tests_Post_Attachments extends WP_UnitTestCase { 
    410410                // Cleanup.
    411411                set_current_screen( 'front' );
    412412
    413                 $this->assertSame( set_url_scheme( $url, 'http' ), $url );
     413                $this->assertSame( set_url_scheme( $url, 'https' ), $url );
    414414        }
    415415
    416416        /**