Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/wp-includes/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Comment thread
hbhalodia marked this conversation as resolved.
Resolved
// 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;
}

Expand Down
66 changes: 66 additions & 0 deletions tests/phpunit/tests/oembed/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<non-falsy-string, array{ non-falsy-string }> 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( '' );

Expand Down
Loading