diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index 3fb8968c7c62c..f80ddea2cf09c 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -663,6 +663,14 @@ function get_oembed_response_data_for_url( $url, $args ) { if ( $path ) { $qv['path'] = get_network()->path . $path . '/'; + /* + * In a subdirectory configuration of multisite, the `/blog` prefix is used by + * default on the main site to avoid URL collisions. Remove it to correctly + * identify the main site. + */ + if ( str_starts_with( $qv['path'], '/blog/' ) ) { + $qv['path'] = preg_replace( '|^/blog|', '', $qv['path'] ); + } } } diff --git a/tests/phpunit/tests/oembed/wpOembed.php b/tests/phpunit/tests/oembed/wpOembed.php index bc10c2a10a7eb..59ef33f75b3bd 100644 --- a/tests/phpunit/tests/oembed/wpOembed.php +++ b/tests/phpunit/tests/oembed/wpOembed.php @@ -276,4 +276,37 @@ public function test_wp_filter_pre_oembed_result_multisite_restores_state_if_no_ $this->assertFalse( $actual ); $this->assertSame( $current_blog_id, get_current_blog_id() ); } + + /** + * @ticket 45052 + * @group multisite + * @group ms-required + * + * @covers ::get_html + */ + public function test_wp_filter_pre_oembed_result_multisite_sub_main_pretty_permalink() { + $this->set_permalink_structure( '/blog/%postname%/' ); + + $post_id = self::factory()->post->create(); + $permalink = get_permalink( $post_id ); + $user_id = self::$user_id; + $blog_id = self::factory()->blog->create( + array( + 'user_id' => $user_id, + ) + ); + + switch_to_blog( $blog_id ); + + $this->set_permalink_structure( '/%postname%/' ); + + add_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) ); + $actual = $this->oembed->get_html( $permalink ); + remove_filter( 'pre_oembed_result', array( $this, '_filter_pre_oembed_result' ) ); + + restore_current_blog(); + + $this->assertNotNull( $this->pre_oembed_result_filtered ); + $this->assertEquals( $this->pre_oembed_result_filtered, $actual ); + } }