diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index c660d6c849987..007fb4d394b7b 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::find_handler_for_request() does, ignoring case. + 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 6077b088d924a..4dad4e8720127 100644 --- a/tests/phpunit/tests/oembed/controller.php +++ b/tests/phpunit/tests/oembed/controller.php @@ -498,6 +498,72 @@ 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 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( + 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' ) ); + $output = get_echo( '_oembed_rest_pre_serve_request', array( false, $response, $request, rest_get_server() ) ); + + $this->assertSame( '', $output, 'XML should not be served for other routes.' ); + } + public function test_get_oembed_endpoint_url() { $this->set_permalink_structure( '' );