Embeds: Allow embedding static front pages and pages having a child page with an embed slug.

This makes `embed` a special slug that can't be used for new pages/posts. When `https://example.com/foo/embed/` is an existing page, embeds fall back to `https://example.com/foo/?embed=true`.
Adds unit tests.

Fixes #34971.

git-svn-id: https://develop.svn.wordpress.org/trunk@36307 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2016-01-15 07:55:19 +00:00
parent ee60e36a2c
commit d80a3c7ccd
10 changed files with 241 additions and 27 deletions
+43
View File
@@ -169,6 +169,49 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
$this->assertTrue( $data['width'] <= $request['maxwidth'] );
}
/**
* @ticket 34971
*/
function test_request_static_front_page() {
$post = self::factory()->post->create_and_get( array(
'post_title' => 'Front page',
'post_type' => 'page',
) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post->ID );
$request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
$request->set_param( 'url', home_url() );
$request->set_param( 'maxwidth', 400 );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertInternalType( 'array', $data );
$this->assertNotEmpty( $data );
$this->assertArrayHasKey( 'version', $data );
$this->assertArrayHasKey( 'provider_name', $data );
$this->assertArrayHasKey( 'provider_url', $data );
$this->assertArrayHasKey( 'author_name', $data );
$this->assertArrayHasKey( 'author_url', $data );
$this->assertArrayHasKey( 'title', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertArrayHasKey( 'width', $data );
$this->assertEquals( '1.0', $data['version'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
$this->assertEquals( get_home_url(), $data['provider_url'] );
$this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
$this->assertEquals( get_home_url(), $data['author_url'] );
$this->assertEquals( $post->post_title, $data['title'] );
$this->assertEquals( 'rich', $data['type'] );
$this->assertTrue( $data['width'] <= $request['maxwidth'] );
update_option( 'show_on_front', 'posts' );
}
function test_request_xml() {
$user = self::factory()->user->create_and_get( array(
'display_name' => 'John Doe',
+12 -4
View File
@@ -9,17 +9,25 @@ class Tests_oEmbed_Discovery extends WP_UnitTestCase {
}
function test_add_oembed_discovery_links_front_page() {
$this->go_to( home_url('/') );
$this->go_to( home_url() );
$this->assertSame( '', get_echo( 'wp_oembed_add_discovery_links' ) );
$this->assertSame( 0, url_to_postid( home_url() ) );
}
/**
* @ticket 34971
*/
function test_add_oembed_discovery_links_static_front_page() {
update_option( 'show_on_front', 'page' );
update_option( 'page_for_posts', self::factory()->post->create( array( 'post_title' => 'blog-page', 'post_type' => 'page' ) ) );
update_option( 'page_on_front', self::factory()->post->create( array( 'post_title' => 'front-page', 'post_type' => 'page' ) ) );
$this->go_to( home_url('/') );
$this->assertSame( '', get_echo( 'wp_oembed_add_discovery_links' ) );
$this->go_to( home_url() );
$this->assertQueryTrue( 'is_front_page', 'is_singular', 'is_page' );
$expected = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
$expected .= '<link rel="alternate" 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' ) );
update_option( 'show_on_front', 'posts' );
}
+84 -7
View File
@@ -4,28 +4,105 @@
* @group oembed
*/
class Tests_Post_Embed_URL extends WP_UnitTestCase {
function test_get_post_embed_url_non_existent_post() {
function test_non_existent_post() {
$embed_url = get_post_embed_url( 0 );
$this->assertFalse( $embed_url );
}
function test_get_post_embed_url_with_pretty_permalinks() {
update_option( 'permalink_structure', '/%postname%' );
function test_with_pretty_permalinks() {
$this->set_permalink_structure( '/%postname%' );
$post_id = self::factory()->post->create();
$permalink = get_permalink( $post_id );
$embed_url = get_post_embed_url( $post_id );
$this->assertEquals( user_trailingslashit( trailingslashit( $permalink ) . 'embed' ), $embed_url );
update_option( 'permalink_structure', '' );
$this->assertEquals( $permalink . '/embed', $embed_url );
}
function test_get_post_embed_url_with_ugly_permalinks() {
function test_with_ugly_permalinks() {
$post_id = self::factory()->post->create();
$permalink = get_permalink( $post_id );
$embed_url = get_post_embed_url( $post_id );
$this->assertEquals( $permalink . '&embed=true', $embed_url );
}
/**
* @ticket 34971
*/
function test_static_front_page() {
$this->set_permalink_structure( '/%postname%/' );
$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post_id );
$embed_url = get_post_embed_url( $post_id );
$this->assertSame( user_trailingslashit( trailingslashit( home_url() ) . 'embed' ), $embed_url );
update_option( 'show_on_front', 'posts' );
}
/**
* @ticket 34971
*/
function test_static_front_page_with_ugly_permalinks() {
$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post_id );
$embed_url = get_post_embed_url( $post_id );
$this->assertSame( trailingslashit( home_url() ) . '?embed=true', $embed_url );
update_option( 'show_on_front', 'posts' );
}
/**
* @ticket 34971
*/
function test_page_conflicts_with_embed_slug() {
$this->set_permalink_structure( '/%postname%/' );
$parent_page = self::factory()->post->create( array( 'post_type' => 'page' ) );
add_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
$child_page = self::factory()->post->create( array(
'post_type' => 'page',
'post_parent' => $parent_page,
'post_name' => 'embed',
) );
remove_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
$this->assertSame( get_permalink( $parent_page ) . '?embed=true', get_post_embed_url( $parent_page ) );
$this->assertSame( get_permalink( $child_page ) . 'embed/', get_post_embed_url( $child_page ) );
}
/**
* @ticket 34971
*/
function test_static_front_page_conflicts_with_embed_slug() {
$this->set_permalink_structure( '/%postname%/' );
// Create a post with the 'embed' post_name
add_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
$post_embed_slug = self::factory()->post->create( array( 'post_name' => 'embed' ) );
remove_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
$page_front = self::factory()->post->create( array( 'post_type' => 'page' ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $page_front );
$this->assertSame( home_url() . '/embed/embed/', get_post_embed_url( $post_embed_slug ) );
$this->assertSame( home_url() . '/?embed=true', get_post_embed_url( $page_front ) );
update_option( 'show_on_front', 'posts' );
}
public function filter_unique_post_slug() {
return 'embed';
}
}
@@ -302,4 +302,49 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase {
$found = wp_unique_post_slug( '32', $p, 'publish', 'post', 0 );
$this->assertEquals( '32', $found );
}
/**
* @ticket 34971
*/
public function test_embed_slug_should_be_suffixed_for_posts() {
$this->set_permalink_structure( '/%postname%/' );
$p = self::factory()->post->create( array(
'post_type' => 'post',
'post_name' => 'embed',
) );
$found = wp_unique_post_slug( 'embed', $p, 'publish', 'post', 0 );
$this->assertSame( 'embed-2', $found );
}
/**
* @ticket 34971
*/
public function test_embed_slug_should_be_suffixed_for_pages() {
$this->set_permalink_structure( '/%postname%/' );
$p = self::factory()->post->create( array(
'post_type' => 'page',
'post_name' => 'embed',
) );
$found = wp_unique_post_slug( 'embed', $p, 'publish', 'paage', 0 );
$this->assertSame( 'embed-2', $found );
}
/**
* @ticket 34971
*/
public function test_embed_slug_should_be_suffixed_for_attachments() {
$this->set_permalink_structure( '/%postname%/' );
$p = self::factory()->post->create( array(
'post_type' => 'attachment',
'post_name' => 'embed',
) );
$found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 );
$this->assertSame( 'embed-2', $found );
}
}
+20
View File
@@ -303,6 +303,26 @@ class Tests_Rewrite extends WP_UnitTestCase {
$this->set_permalink_structure();
}
/**
* @ticket 34971
*/
function test_url_to_postid_static_front_page() {
$post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$this->assertSame( 0, url_to_postid( home_url() ) );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post_id );
$this->assertSame( $post_id, url_to_postid( set_url_scheme( home_url(), 'http' ) ) );
$this->assertSame( $post_id, url_to_postid( set_url_scheme( home_url(), 'https' ) ) );
$this->assertSame( $post_id, url_to_postid( str_replace( array( 'http://', 'https://' ), 'http://www.', home_url() ) ) );
$this->assertSame( $post_id, url_to_postid( home_url() . '#random' ) );
$this->assertSame( $post_id, url_to_postid( home_url() . '?random' ) );
update_option( 'show_on_front', 'posts' );
}
/**
* @ticket 21970
*/