Make WordPress Core

Ticket #15928: 15928.11.patch

File 15928.11.patch, 2.4 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 39eb6a3..f01a2cf 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..7089df9 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                $is_ssl = is_ssl();
     295                $_SERVER['HTTPS'] = 'off';
     296
     297                // Test that wp_get_attachemt_url returns with http scheme
     298                $url = wp_get_attachment_url( $attachment_id );
     299                $this->assertSame( set_url_scheme( $url, 'http' ), $url );
     300
     301                // Cleanup
     302                $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
     303        }
     304
     305        /**
     306         * @ticket 15928
     307         */
     308        public function test_wp_get_attachment_url_with_https_on_same_host() {
     309                $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     310                $contents = file_get_contents( $filename );
     311
     312                $upload = wp_upload_bits( basename( $filename ), null, $contents );
     313                $this->assertTrue( empty( $upload['error'] ) );
     314
     315                // Set attachment ID
     316                $attachment_id = $this->_make_attachment( $upload );
     317
     318                $is_ssl = is_ssl();
     319                $_SERVER['HTTPS'] = 'on';
     320
     321                $upload_dir = wp_upload_dir();
     322                $_SERVER['HTTP_HOST'] = parse_url( $upload_dir['baseurl'], PHP_URL_HOST );
     323
     324                // Test that wp_get_attachemt_url returns with https scheme
     325                $url = wp_get_attachment_url( $attachment_id );
     326                $this->assertSame( set_url_scheme( $url, 'https' ), $url );
     327
     328                // Cleanup
     329                $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
     330        }
     331
    281332}