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
13 changes: 12 additions & 1 deletion src/wp-includes/class-wp-oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,19 @@ private function _fetch_with_format( $provider_url_with_args, $format ) {
}

$parse_method = "_parse_$format";
$data = $this->$parse_method( $body );

return $this->$parse_method( $body );
/*
* Some providers ignore the `format` query parameter when it's already
* implied by the request URL (e.g. a `.xml` endpoint). If JSON parsing
* failed, try parsing the same response body as XML before giving up,
* since XML parsing predictably fails on non-XML content.
*/
if ( false === $data && 'json' === $format && false !== stripos( $body, '<oembed' ) ) {
$data = $this->_parse_xml( $body );
}
Comment thread
shrivastavanolo marked this conversation as resolved.

return $data;
}

/**
Expand Down
85 changes: 85 additions & 0 deletions tests/phpunit/tests/oembed/wpOembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,89 @@ public function test_get_provider_handles_provider_without_regex_flag(): void {

$this->assertSame( 'https://example.site/api/oembed', $result );
}

/**
* @ticket 44231
* @covers WP_oEmbed::fetch
*/
public function test_fetch_falls_back_to_xml_when_json_parsing_fails() {
$xml_body = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
. '<oembed><type>rich</type><version>1.0</version>'
. '<html>&lt;p&gt;Test&lt;/p&gt;</html><width>400</width><height>300</height></oembed>';

$filter = static function ( $preempt, $parsed_args, $url ) use ( $xml_body ) {
if ( str_contains( $url, 'format=json' ) ) {
return array(
'body' => $xml_body,
'response' => array( 'code' => 200 ),
);
}
return $preempt;
};

add_filter( 'pre_http_request', $filter, 10, 3 );
$result = $this->oembed->fetch( 'https://example.com/oembed', 'https://example.com/some-video' );
remove_filter( 'pre_http_request', $filter, 10 );

$this->assertInstanceOf( 'stdClass', $result );
$this->assertSame( 'rich', $result->type );
$this->assertSame( '<p>Test</p>', $result->html );
}

/**
* @ticket 44231
* @covers WP_oEmbed::fetch
*/
public function test_fetch_does_not_fall_back_to_xml_for_non_oembed_xml() {
$error_xml = '<?xml version="1.0" encoding="utf-8"?><error>Not found</error>';

$filter = static function ( $preempt, $parsed_args, $url ) use ( $error_xml ) {
if ( str_contains( $url, 'format=json' ) ) {
return array(
'body' => $error_xml,
'response' => array( 'code' => 200 ),
);
}
return $preempt;
};

add_filter( 'pre_http_request', $filter, 10, 3 );
$result = $this->oembed->fetch( 'https://example.com/oembed', 'https://example.com/some-video' );
remove_filter( 'pre_http_request', $filter, 10 );

$this->assertFalse( $result );
}

/**
* @ticket 44231
* @covers WP_oEmbed::fetch
*/
public function test_fetch_still_parses_valid_json_normally() {
$json_body = wp_json_encode(
array(
'type' => 'rich',
'version' => '1.0',
'html' => '<p>Test</p>',
'width' => 400,
'height' => 300,
)
);

$filter = static function ( $preempt, $parsed_args, $url ) use ( $json_body ) {
if ( str_contains( $url, 'format=json' ) ) {
return array(
'body' => $json_body,
'response' => array( 'code' => 200 ),
);
}
return $preempt;
};

add_filter( 'pre_http_request', $filter, 10, 3 );
$result = $this->oembed->fetch( 'https://example.com/oembed', 'https://example.com/some-video' );
remove_filter( 'pre_http_request', $filter, 10 );

$this->assertInstanceOf( 'stdClass', $result );
$this->assertSame( 'rich', $result->type );
}
}
Loading