From a3fa4e626e18dbfe58b653098209c7a20f6f1c34 Mon Sep 17 00:00:00 2001 From: hbhalodia Date: Wed, 18 Nov 2026 13:23:04 +0530 Subject: [PATCH 1/3] Fix oEmbed endpoint to match case insensitive --- src/wp-includes/embed.php | 3 +- tests/phpunit/tests/oembed/controller.php | 70 +++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index c660d6c849987..e5133e671b364 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -787,7 +787,8 @@ function wp_oembed_ensure_format( $format ) { function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { $params = $request->get_params(); - if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) { + // Match the route the way WP_REST_Server::match_request_to_handler() does, ignoring case. + if ( ! preg_match( '@^/oembed/1.0/embed$@i', $request->get_route() ) || 'GET' !== $request->get_method() ) { return $served; } diff --git a/tests/phpunit/tests/oembed/controller.php b/tests/phpunit/tests/oembed/controller.php index 6077b088d924a..df0b3d819a8bc 100644 --- a/tests/phpunit/tests/oembed/controller.php +++ b/tests/phpunit/tests/oembed/controller.php @@ -498,6 +498,76 @@ public function test_rest_pre_serve_request_wrong_method() { $this->assertTrue( _oembed_rest_pre_serve_request( true, $response, $request, rest_get_server() ) ); } + /** + * Ensures XML is served for every spelling of the embed route that the + * REST server dispatches to the oEmbed controller. + * + * @ticket 66723 + * + * @covers ::_oembed_rest_pre_serve_request + * + * @dataProvider data_embed_route_spellings + * + * @param string $route Requested embed route. + */ + public function test_rest_pre_serve_request_serves_xml_for_every_route_spelling( string $route ): void { + $post = self::factory()->post->create_and_get( + array( + 'post_title' => 'Hello World', + ) + ); + + $request = new WP_REST_Request( 'GET', $route ); + $request->set_param( 'url', get_permalink( $post->ID ) ); + $request->set_param( 'format', 'xml' ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status(), 'The oEmbed controller should handle the request.' ); + + $output = get_echo( '_oembed_rest_pre_serve_request', array( false, $response, $request, rest_get_server() ) ); + $xml = simplexml_load_string( $output ); + + $this->assertInstanceOf( 'SimpleXMLElement', $xml, 'The response should be served as XML.' ); + $this->assertSame( 'Hello World', (string) $xml->title, 'The XML should contain the embedded post title.' ); + } + + /** + * Data provider for spellings of the embed route that the REST server + * dispatches to the oEmbed controller. + * + * @return array Test cases. + */ + public static function data_embed_route_spellings(): array { + return array( + 'lowercase' => array( '/oembed/1.0/embed' ), + 'mixed case' => array( '/oEmbed/1.0/embed' ), + 'uppercase' => array( '/OEMBED/1.0/EMBED' ), + 'trailing newline' => array( "/oembed/1.0/embed\n" ), + ); + } + + /** + * Ensures XML is not served for other oEmbed routes. + * + * @ticket 66723 + * + * @covers ::_oembed_rest_pre_serve_request + */ + public function test_rest_pre_serve_request_ignores_other_routes(): void { + $request = new WP_REST_Request( 'GET', '/oEmbed/1.0/proxy' ); + $request->set_param( 'format', 'xml' ); + + $response = new WP_REST_Response( array( 'title' => 'Hello World' ) ); + + ob_start(); + $served = _oembed_rest_pre_serve_request( false, $response, $request, rest_get_server() ); + $output = ob_get_clean(); + + $this->assertFalse( $served, 'The request should be left for the REST server to serve.' ); + $this->assertSame( '', $output, 'Nothing should be printed for other routes.' ); + } + public function test_get_oembed_endpoint_url() { $this->set_permalink_structure( '' ); From ae5fd2c943ce2963d71389d95dd17368875b586e Mon Sep 17 00:00:00 2001 From: hbhalodia Date: Thu, 19 Nov 2026 13:43:43 +0530 Subject: [PATCH 2/3] Address feedbacks --- src/wp-includes/embed.php | 2 +- tests/phpunit/tests/oembed/controller.php | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index e5133e671b364..64572a240fddf 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -788,7 +788,7 @@ function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { $params = $request->get_params(); // Match the route the way WP_REST_Server::match_request_to_handler() does, ignoring case. - if ( ! preg_match( '@^/oembed/1.0/embed$@i', $request->get_route() ) || 'GET' !== $request->get_method() ) { + if ( 'GET' !== $request->get_method() || ! preg_match( '@^/oembed/1.0/embed$@i', $request->get_route() ) ) { return $served; } diff --git a/tests/phpunit/tests/oembed/controller.php b/tests/phpunit/tests/oembed/controller.php index df0b3d819a8bc..4dad4e8720127 100644 --- a/tests/phpunit/tests/oembed/controller.php +++ b/tests/phpunit/tests/oembed/controller.php @@ -508,7 +508,7 @@ public function test_rest_pre_serve_request_wrong_method() { * * @dataProvider data_embed_route_spellings * - * @param string $route Requested embed route. + * @param non-falsy-string $route Requested embed route. */ public function test_rest_pre_serve_request_serves_xml_for_every_route_spelling( string $route ): void { $post = self::factory()->post->create_and_get( @@ -536,7 +536,7 @@ public function test_rest_pre_serve_request_serves_xml_for_every_route_spelling( * Data provider for spellings of the embed route that the REST server * dispatches to the oEmbed controller. * - * @return array Test cases. + * @return array Test cases. */ public static function data_embed_route_spellings(): array { return array( @@ -559,13 +559,9 @@ public function test_rest_pre_serve_request_ignores_other_routes(): void { $request->set_param( 'format', 'xml' ); $response = new WP_REST_Response( array( 'title' => 'Hello World' ) ); + $output = get_echo( '_oembed_rest_pre_serve_request', array( false, $response, $request, rest_get_server() ) ); - ob_start(); - $served = _oembed_rest_pre_serve_request( false, $response, $request, rest_get_server() ); - $output = ob_get_clean(); - - $this->assertFalse( $served, 'The request should be left for the REST server to serve.' ); - $this->assertSame( '', $output, 'Nothing should be printed for other routes.' ); + $this->assertSame( '', $output, 'XML should not be served for other routes.' ); } public function test_get_oembed_endpoint_url() { From a74fb06aef9516122091fc9b25f9fa2a267a36b2 Mon Sep 17 00:00:00 2001 From: hbhalodia Date: Fri, 20 Nov 2026 10:06:57 +0530 Subject: [PATCH 3/3] Address feedbacks --- src/wp-includes/embed.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index 64572a240fddf..007fb4d394b7b 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -787,7 +787,7 @@ function wp_oembed_ensure_format( $format ) { function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { $params = $request->get_params(); - // Match the route the way WP_REST_Server::match_request_to_handler() does, ignoring case. + // Match the route the way WP_REST_Server::find_handler_for_request() does, ignoring case. if ( 'GET' !== $request->get_method() || ! preg_match( '@^/oembed/1.0/embed$@i', $request->get_route() ) ) { return $served; }