Make WordPress Core

Ticket #34335: 34335.2.diff

File 34335.2.diff, 3.4 KB (added by swissspidy, 8 years ago)
  • src/wp-includes/class-oembed.php

    diff --git src/wp-includes/class-oembed.php src/wp-includes/class-oembed.php
    index 052a215..19d2f82 100644
    class WP_oEmbed { 
    464464
    465465                $provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
    466466                $provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
    467                 $provider = add_query_arg( 'url', urlencode($url), $provider );
     467
     468                /*
     469                 * Only add the 'url' query variable if it is not already present.
     470                 *
     471                 * @see https://core.trac.wordpress.org/ticket/34335
     472                 */
     473                $url_query = parse_url( $provider, PHP_URL_QUERY );
     474                parse_str( $url_query, $query_variables );
     475
     476                if ( ! isset ( $query_variables['url'] ) ) {
     477                        $provider = add_query_arg( 'url', urlencode( $url ), $provider );
     478                }
    468479
    469480                /**
    470481                 * Filters the oEmbed URL to be fetched.
  • tests/phpunit/tests/oembed/wpOembed.php

    diff --git tests/phpunit/tests/oembed/wpOembed.php tests/phpunit/tests/oembed/wpOembed.php
    index b7e6887..a1fecd4 100644
    class Tests_WP_oEmbed extends WP_UnitTestCase { 
    1111
    1212        public $pre_oembed_result_filtered = false;
    1313
     14        /**
     15         * @var string
     16         */
     17        protected $_provider_url = null;
     18
    1419        public function setUp() {
    1520                parent::setUp();
    1621
    class Tests_WP_oEmbed extends WP_UnitTestCase { 
    1823                $this->oembed = _wp_oembed_get_object();
    1924
    2025                $this->pre_oembed_result_filtered = false;
     26                $this->_provider_url = null;
    2127        }
    2228
    2329        public function _filter_pre_oembed_result( $result ) {
    class Tests_WP_oEmbed extends WP_UnitTestCase { 
    6975                $this->assertTrue( false !== $this->pre_oembed_result_filtered );
    7076                $this->assertFalse( $actual );
    7177        }
     78
     79        /**
     80         * @ticket 34335
     81         */
     82        public function test_fetch_of_provider_with_url() {
     83                add_filter( 'pre_http_request', array( $this, '_http_mock_response' ), 10, 3 );
     84
     85                //Discovered endpoint contains an url query variable, this should be used instead of the passed url
     86                $this->oembed->fetch( 'https://example.com/provider/endpoint?url=https://example.com/foo', 'https://example.com/bar', array(
     87                        'discovered' => true
     88                ) );
     89
     90                remove_filter( 'pre_http_request', array( $this, '_http_mock_response' ) );
     91
     92                $url_query = parse_url( $this->_provider_url, PHP_URL_QUERY );
     93                parse_str( $url_query, $query_variables );
     94                $this->assertEquals( 'https://example.com/foo', $query_variables['url'] );
     95        }
     96
     97        /**
     98         * @ticket 34335
     99         */
     100        public function test_fetch_of_provider_without_url() {
     101                add_filter( 'pre_http_request', array( $this, '_http_mock_response' ), 10, 3 );
     102
     103                //Discovered endpoint does not contain an url query variable, we should insert the passed url
     104                $this->oembed->fetch( 'https://example.com/provider/endpoint', 'https://example.com/bar', array(
     105                        'discovered' => true
     106                ) );
     107
     108                remove_filter( 'pre_http_request', array( $this, '_http_mock_response' ) );
     109
     110                $url_query = parse_url( $this->_provider_url, PHP_URL_QUERY );
     111                parse_str( $url_query, $query_variables );
     112                $this->assertEquals( 'https://example.com/bar', $query_variables['url'] );
     113        }
     114
     115        public function _http_mock_response( $response,  $request_args, $request_url ) {
     116                $this->_provider_url = $request_url;
     117                return array(
     118                        'headers'  => array(),
     119                        'body'     => '',
     120                        'response' => array(
     121                                'code'    => 200,
     122                                'message' => '',
     123                        ),
     124                        'cookies'  => array(),
     125                        'filename' => false,
     126                );
     127        }
    72128}