Skip to content
Closed
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/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@
add_action( 'rest_api_init', 'wp_oembed_register_route' );
add_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4 );

add_action( 'wp_head', 'wp_oembed_add_discovery_links' );
add_action( 'wp_head', 'wp_oembed_add_discovery_links', 4 ); // Printed after feed_links() and feed_links_extra().
add_action( 'wp_head', 'wp_oembed_add_discovery_links' ); // Unhooked the first time that wp_oembed_add_discovery_links() runs for back-compat.
add_action( 'wp_head', 'wp_oembed_add_host_js' ); // Back-compat for sites disabling oEmbed host JS by removing action.
add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' );

Expand Down
11 changes: 11 additions & 0 deletions src/wp-includes/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,19 @@ function wp_oembed_register_route() {
*
* @since 4.4.0
* @since 6.8.0 Output was adjusted to only embed if the post supports it.
* @since 6.9.0 Now runs first at `wp_head` priority 4, with a fallback to priority 10. This helps ensure the discovery links appear within the first 150KB.
*/
function wp_oembed_add_discovery_links() {
if ( doing_action( 'wp_head' ) ) {
// For back-compat, short-circuit if a plugin has removed the action at the original priority.
if ( ! has_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ) ) {
return;
}

// Prevent running again at the original priority.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
}

$output = '';

if ( is_singular() && is_post_embeddable() ) {
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/tests/oembed/discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function test_add_oembed_discovery_links_to_post() {
$expected .= '<link rel="alternate" title="oEmbed (XML)" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";

$this->assertSame( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );

add_filter( 'oembed_discovery_links', '__return_empty_string' );
$this->assertSame( '', get_echo( 'wp_oembed_add_discovery_links' ), 'Expected filtering oembed_discovery_links to empty string to result in no wp_oembed_add_discovery_links() output.' );
}

public function test_add_oembed_discovery_links_to_page() {
Expand Down Expand Up @@ -100,4 +103,39 @@ public function test_wp_oembed_add_discovery_links_non_embeddable_post_type_outp

$this->assertFalse( get_oembed_response_data( $post, 100 ) );
}

/**
* @ticket 64178
* @covers ::wp_oembed_add_discovery_links
*/
public function test_wp_oembed_add_discovery_links_back_compat() {
$action = 'wp_head';
$old_priority = 10;
$new_priority = 4;
$callback = 'wp_oembed_add_discovery_links';

$this->assertTrue( has_action( $action, $callback, $old_priority ), 'Expected wp_oembed_add_discovery_links() to be hooked at wp_head with old priority.' );
$this->assertTrue( has_action( $action, $callback, $new_priority ), 'Expected wp_oembed_add_discovery_links() to be hooked at wp_head with new priority.' );

// Remove all wp_head actions and re-add just the later old one.
remove_all_actions( $action );
add_action( $action, $callback, $old_priority );
add_action( $action, $callback, $new_priority );

$post_id = self::factory()->post->create();
$this->go_to( get_permalink( $post_id ) );
$this->assertQueryTrue( 'is_single', 'is_singular' );

$mock_action = new MockAction();
add_filter( 'oembed_discovery_links', array( $mock_action, 'filter' ) );

$wp_head_output = get_echo( 'wp_head' );
$this->assertSame( 1, $mock_action->get_call_count() );

$expected = '<link rel="alternate" title="oEmbed (JSON)" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
$expected .= '<link rel="alternate" title="oEmbed (XML)" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";

$this->assertSame( $expected, $wp_head_output, 'Expected wp_head output to be the same as the wp_oembed_add_discovery_links() output.' );
$this->assertSame( $expected, get_echo( $callback ), 'Expected wp_oembed_add_discovery_links() output to be the same as the wp_head output when called outside of wp_head.' );
}
}
Loading