From b60a603d505e4919574045532625e346c290dac8 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 14 Jul 2017 16:04:04 +0000 Subject: [PATCH] REST API: Ensure `maxwidth` and `maxheight` params are forwarded to oEmbed provider in proxy requests. Also correct phpdoc return tag on `WP_oEmbed_Controller::get_proxy_item()` and remove dead code in oEmbed controller phpunit tests. Amends [40628]. See #40450. Fixes #41299. git-svn-id: https://develop.svn.wordpress.org/trunk@41047 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-oembed-controller.php | 10 +++++++- tests/phpunit/tests/oembed/controller.php | 23 ++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-oembed-controller.php b/src/wp-includes/class-wp-oembed-controller.php index e6dae0670d..63cc1842ed 100644 --- a/src/wp-includes/class-wp-oembed-controller.php +++ b/src/wp-includes/class-wp-oembed-controller.php @@ -153,7 +153,7 @@ final class WP_oEmbed_Controller { * * @see WP_oEmbed::get_html() * @param WP_REST_Request $request Full data about the request. - * @return WP_Error|array oEmbed response data or WP_Error on failure. + * @return object|WP_Error oEmbed response data or WP_Error on failure. */ public function get_proxy_item( $request ) { $args = $request->get_params(); @@ -169,6 +169,14 @@ final class WP_oEmbed_Controller { $url = $request['url']; unset( $args['url'] ); + // Copy maxwidth/maxheight to width/height since WP_oEmbed::fetch() uses these arg names. + if ( isset( $args['maxwidth'] ) ) { + $args['width'] = $args['maxwidth']; + } + if ( isset( $args['maxheight'] ) ) { + $args['height'] = $args['maxheight']; + } + $data = _wp_oembed_get_object()->get_data( $url, $args ); if ( false === $data ) { diff --git a/tests/phpunit/tests/oembed/controller.php b/tests/phpunit/tests/oembed/controller.php index ef6b42edd0..529811ceb8 100644 --- a/tests/phpunit/tests/oembed/controller.php +++ b/tests/phpunit/tests/oembed/controller.php @@ -49,7 +49,7 @@ class Test_oEmbed_Controller extends WP_UnitTestCase { public function tearDown() { parent::tearDown(); - remove_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10, 3 ); + remove_filter( 'pre_http_request', array( $this, 'mock_embed_request' ), 10 ); } /** @@ -70,10 +70,12 @@ class Test_oEmbed_Controller extends WP_UnitTestCase { public function mock_embed_request( $preempt, $r, $url ) { unset( $preempt, $r ); + $parsed_url = wp_parse_url( $url ); + parse_str( $parsed_url['query'], $query_params ); $this->request_count += 1; // Mock request to YouTube Embed. - if ( false !== strpos( $url, self::YOUTUBE_VIDEO_ID ) ) { + if ( ! empty( $query_params['url'] ) && false !== strpos( $query_params['url'], self::YOUTUBE_VIDEO_ID ) ) { return array( 'response' => array( 'code' => 200, @@ -84,14 +86,14 @@ class Test_oEmbed_Controller extends WP_UnitTestCase { 'type' => 'video', 'provider_name' => 'YouTube', 'provider_url' => 'https://www.youtube.com', - 'thumbnail_width' => 480, - 'width' => 500, - 'thumbnail_height' => 360, - 'html' => '', + 'thumbnail_width' => $query_params['maxwidth'], + 'width' => $query_params['maxwidth'], + 'thumbnail_height' => $query_params['maxheight'], + 'height' => $query_params['maxheight'], + 'html' => '', 'author_name' => 'Yosemitebear62', 'thumbnail_url' => 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg', 'title' => 'Yosemitebear Mountain Double Rainbow 1-8-10', - 'height' => 375, ) ), ); @@ -477,13 +479,14 @@ class Test_oEmbed_Controller extends WP_UnitTestCase { $response = $this->server->dispatch( $request ); $this->assertEquals( 400, $response->get_status() ); - $data = $response->get_data(); } public function test_proxy_with_valid_oembed_provider() { wp_set_current_user( self::$editor ); $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' ); $request->set_param( 'url', 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID ); + $request->set_param( 'maxwidth', 456 ); + $request->set_param( 'maxheight', 789 ); $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) ); $response = $this->server->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); @@ -498,6 +501,8 @@ class Test_oEmbed_Controller extends WP_UnitTestCase { $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' ); $request->set_param( 'url', 'https://www.youtube.com/watch?v=' . self::YOUTUBE_VIDEO_ID ); $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) ); + $request->set_param( 'maxwidth', 456 ); + $request->set_param( 'maxheight', 789 ); $response = $this->server->dispatch( $request ); $this->assertEquals( 1, $this->request_count ); @@ -508,6 +513,8 @@ class Test_oEmbed_Controller extends WP_UnitTestCase { $this->assertTrue( is_object( $data ) ); $this->assertEquals( 'YouTube', $data->provider_name ); $this->assertEquals( 'https://i.ytimg.com/vi/' . self::YOUTUBE_VIDEO_ID . '/hqdefault.jpg', $data->thumbnail_url ); + $this->assertEquals( $data->width, $request['maxwidth'] ); + $this->assertEquals( $data->height, $request['maxheight'] ); } public function test_proxy_with_invalid_oembed_provider_no_discovery() {