Make WordPress Core

Ticket #15928: 15928.12.patch

File 15928.12.patch, 3.9 KB (added by joemcgill, 9 years ago)
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 3b3d55f..b5534f3 100644
    function wp_get_attachment_url( $post_id = 0 ) { 
    49464946                $url = get_the_guid( $post->ID );
    49474947        }
    49484948
     4949        // Return an https url when calling from an ssl connection on the same host
     4950        if ( is_ssl() && 'https' !== substr( $url, 0, 5 ) ) {
     4951                if ( parse_url( $url, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) {
     4952                        $url = set_url_scheme( $url, 'https' );
     4953                }
     4954        }
     4955
    49494956        /**
    49504957         * Filter the attachment URL.
    49514958         *
  • tests/phpunit/tests/post/attachments.php

    diff --git tests/phpunit/tests/post/attachments.php tests/phpunit/tests/post/attachments.php
    index d079654..015e2f8 100644
    class Tests_Post_Attachments extends WP_UnitTestCase { 
    278278                $this->assertEquals( $attachment->post_parent, $post_id );
    279279        }
    280280
     281        /**
     282         * @ticket 15928
     283         */
     284        public function test_wp_get_attachment_with_https_off() {
     285                $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     286                $contents = file_get_contents( $filename );
     287
     288                $upload = wp_upload_bits( basename( $filename ), null, $contents );
     289                $this->assertTrue( empty( $upload['error'] ) );
     290
     291                // Set attachment ID
     292                $attachment_id = $this->_make_attachment( $upload );
     293
     294                // Save server data for cleanup
     295                $is_ssl = is_ssl();
     296                $_SERVER['HTTPS'] = 'off';
     297
     298                // Find out what scheme is coming out of wp_upload_dir()
     299                $upload_dir = wp_upload_dir();
     300                $scheme = parse_url( $upload_dir['baseurl'] , PHP_URL_SCHEME );
     301
     302                // Test that wp_get_attachemt_url returns the same scheme as the uploads dir
     303                $url = wp_get_attachment_url( $attachment_id );
     304                $this->assertSame( set_url_scheme( $url, $scheme ), $url );
     305
     306                // Cleanup
     307                $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
     308        }
     309
     310        /**
     311         * @ticket 15928
     312         */
     313        public function test_wp_get_attachment_url_with_https_on_same_host() {
     314                $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     315                $contents = file_get_contents( $filename );
     316
     317                $upload = wp_upload_bits( basename( $filename ), null, $contents );
     318                $this->assertTrue( empty( $upload['error'] ) );
     319
     320                // Set attachment ID
     321                $attachment_id = $this->_make_attachment( $upload );
     322
     323                // Save server data for cleanup
     324                $is_ssl = is_ssl();
     325                $http_host = $_SERVER['HTTP_HOST'];
     326
     327                $_SERVER['HTTPS'] = 'on';
     328
     329                // Set server host to match the host of wp_upload_dir()
     330                $upload_dir = wp_upload_dir();
     331                $_SERVER['HTTP_HOST'] = parse_url( $upload_dir['baseurl'], PHP_URL_HOST );
     332
     333                // Test that wp_get_attachemt_url returns with https scheme
     334                $url = wp_get_attachment_url( $attachment_id );
     335                $this->assertSame( set_url_scheme( $url, 'https' ), $url );
     336
     337                // Cleanup
     338                $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
     339                $_SERVER['HTTP_HOST'] = $http_host;
     340        }
     341
     342        /**
     343        * @ticket 15928
     344        */
     345        public function test_wp_get_attachment_url_with_https_on_diff_host() {
     346                $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     347                $contents = file_get_contents( $filename );
     348
     349                $upload = wp_upload_bits( basename( $filename ), null, $contents );
     350                $this->assertTrue( empty( $upload['error'] ) );
     351
     352                // Set attachment ID
     353                $attachment_id = $this->_make_attachment( $upload );
     354
     355                // Save server data for cleanup
     356                $is_ssl = is_ssl();
     357                $http_host = $_SERVER['HTTP_HOST'];
     358
     359                $_SERVER['HTTPS'] = 'on';
     360
     361                // Set server host to something random
     362                $_SERVER['HTTP_HOST'] = 'some.otherhostname.com';
     363
     364                // Find out what scheme is coming out of wp_upload_dir()
     365                $upload_dir = wp_upload_dir();
     366                $scheme = parse_url( $upload_dir['baseurl'] , PHP_URL_SCHEME );
     367
     368                // Test that wp_get_attachemt_url returns the same scheme as the uploads dir
     369                $url = wp_get_attachment_url( $attachment_id );
     370                $this->assertSame( set_url_scheme( $url, $scheme ), $url );
     371
     372                // Cleanup
     373                $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
     374                $_SERVER['HTTP_HOST'] = $http_host;
     375        }
     376
    281377}