Make WordPress Core

Ticket #34945: 34945.8.diff

File 34945.8.diff, 7.2 KB (added by jeremyfelt, 9 years ago)
  • src/wp-admin/includes/media.php

     
    28192819        $media_dims = apply_filters( 'media_meta', $media_dims, $post );
    28202820
    28212821        $att_url = wp_get_attachment_url( $post->ID );
     2822        // The "File URL" on the Edit Media screen should be the URL expected in content.
     2823        $att_url = _wp_ssl_url( $att_url, 'content' );
    28222824?>
    28232825        <div class="misc-pub-section misc-pub-attachment">
    28242826                <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  • 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

     
    351351         */
    352352        $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
    353353
     354        $img_src = _wp_ssl_url( $img_src, 'content' );
    354355        $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
    355356
    356357        /**
     
    903904}
    904905
    905906/**
     907 * For local URLs, sets https scheme if needed.
     908 *
     909 * @since 4.5.0
     910 * @access private
     911 *
     912 * @param string $url     URL.
     913 * @param string $context Whether the URL is being used in a "content" or "admin" context.
     914 * @return string The same URL with proper scheme.
     915 */
     916function _wp_ssl_url( $url, $context = 'admin' ) {
     917
     918        if ( 'content' === $context ) {
     919                /**
     920                 * If we're inserting a URL into content, the site's siteurl is configured for HTTP, and the URL host
     921                 * matches that of the siteurl, then we convert the URL to HTTP.
     922                 */
     923                $site_url = parse_url( get_option( 'siteurl' ) );
     924                if ( 'https' === substr( $url, 0, 5 ) && 'http' === $site_url['scheme'] && parse_url( $url, PHP_URL_HOST ) === $site_url['host'] ) {
     925                        $url = set_url_scheme( $url, 'http' );
     926                }
     927        } else {
     928                /*
     929                 * If we're using a URL outside of content and are currently viewing an HTTPS page, prefer HTTPS URLs
     930                 * as we know they are supported by the domain.
     931                 */
     932                if ( is_ssl() && 'https' !== substr( $url, 0, 5 ) && parse_url( $url, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) {
     933                        $url = set_url_scheme( $url, 'https' );
     934                }
     935        }
     936
     937        return $url;
     938}
     939
     940/**
    906941 * Get the image size as array from its meta data.
    907942 *
    908943 * Used for responsive images.
     
    10251060        }
    10261061
    10271062        $upload_dir = wp_get_upload_dir();
    1028         $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;
     1063
     1064        // Set URL scheme when front-end is https.
     1065        $image_baseurl = _wp_ssl_url( $upload_dir['baseurl'] );
     1066        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10291067
    10301068        /*
    10311069         * Images that have been edited in WordPress after being uploaded will
     
    29983036
    29993037        $attachment_url = wp_get_attachment_url( $attachment->ID );
    30003038
     3039        // @todo When displaying images in the media grid, this is an admin context. Is this different elsewhere?
     3040        $attachment_url = _wp_ssl_url( $attachment_url, 'admin' );
     3041
    30013042        $response = array(
    30023043                'id'          => $attachment->ID,
    30033044                'title'       => $attachment->post_title,
  • 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 = _wp_ssl_url( $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
     1422                // Mock meta for the image.
     1423                $image_meta = array(
     1424                        'width' => 1200,
     1425                        'height' => 600,
     1426                        'file' => 'test.jpg',
     1427                        'sizes' => array(
     1428                                'thumbnail' => array(
     1429                                        'file' => 'test-150x150.jpg',
     1430                                        'width' => 150,
     1431                                        'height' => 150,
     1432                                ),
     1433                                'medium' => array(
     1434                                        'file' => 'test-300x150.jpg',
     1435                                        'width' => 300,
     1436                                        'height' => 150,
     1437                                ),
     1438                                'large' => array(
     1439                                        'file' => 'test-1024x512.jpg',
     1440                                        'width' => 1024,
     1441                                        'height' => 512,
     1442                                ),
     1443                        )
     1444                );
     1445
     1446                // Test using the large file size.
     1447                $size_array = array(1024, 512);
     1448                $image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
     1449
     1450                $_SERVER['HTTPS'] = 'on';
     1451
     1452                $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';
     1453
     1454                $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
     1455
     1456                // Cleanup
     1457                $_SERVER['HTTPS'] = $is_ssl;
    14101458        }
    14111459}
  • tests/phpunit/tests/post/attachments.php

     
    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
     
    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        /**