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

View File

@ -1022,6 +1022,10 @@ class WP_Rewrite {
$feedmatch2 = $match . $feedregex2;
$feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
// Create query and regex for embeds.
$embedmatch = $match . $embedregex;
$embedquery = $embedindex . '?' . $query . '&embed=true';
// If asked to, turn the feed queries into comment feed ones.
if ( $forcomments ) {
$feedquery .= '&withcomments=1';
@ -1033,7 +1037,7 @@ class WP_Rewrite {
// ...adding on /feed/ regexes => queries
if ( $feed ) {
$rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 );
$rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2, $embedmatch => $embedquery );
}
//...and /page/xx ones

View File

@ -345,7 +345,7 @@ function wp_oembed_register_route() {
function wp_oembed_add_discovery_links() {
$output = '';
if ( is_singular() && ! is_front_page() ) {
if ( is_singular() ) {
$output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
if ( class_exists( 'SimpleXMLElement' ) ) {
@ -387,9 +387,10 @@ function get_post_embed_url( $post = null ) {
return false;
}
if ( get_option( 'permalink_structure' ) ) {
$embed_url = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' );
} else {
$embed_url = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' );
$path_conflict = get_page_by_path( str_replace( home_url(), '', $embed_url ), OBJECT, get_post_types( array( 'public' => true ) ) );
if ( ! get_option( 'permalink_structure' ) || $path_conflict ) {
$embed_url = add_query_arg( array( 'embed' => 'true' ), get_permalink( $post ) );
}

View File

@ -3574,7 +3574,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p
* @param bool $bad_slug Whether the slug would be bad as an attachment slug.
* @param string $slug The post slug.
*/
if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
$suffix = 2;
do {
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
@ -3604,7 +3604,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p
* @param string $post_type Post type.
* @param int $post_parent Post parent ID.
*/
if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
$suffix = 2;
do {
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
@ -3649,7 +3649,7 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p
* @param string $slug The post slug.
* @param string $post_type Post type.
*/
if ( $post_name_check || in_array( $slug, $feeds ) || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
$suffix = 2;
do {
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";

View File

@ -1456,6 +1456,7 @@ class WP_Query {
, 'title'
, 'fields'
, 'menu_order'
, 'embed'
);
foreach ( $keys as $key ) {
@ -1756,6 +1757,10 @@ class WP_Query {
if ( '' != $qv['feed'] )
$this->is_feed = true;
if ( '' != $qv['embed'] ) {
$this->is_embed = true;
}
if ( '' != $qv['tb'] )
$this->is_trackback = true;
@ -1788,6 +1793,9 @@ class WP_Query {
// pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
if ( isset($_query['pagename']) && '' == $_query['pagename'] )
unset($_query['pagename']);
unset( $_query['embed'] );
if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
$this->is_page = true;
$this->is_home = false;
@ -1859,7 +1867,7 @@ class WP_Query {
if ( '404' == $qv['error'] )
$this->set_404();
$this->is_embed = isset( $qv['embed'] ) && ( $this->is_singular || $this->is_404 );
$this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 );
$this->query_vars_hash = md5( serialize( $this->query_vars ) );
$this->query_vars_changed = false;

View File

@ -478,13 +478,6 @@ function url_to_postid( $url ) {
return $id;
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if ( empty($rewrite) )
return 0;
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
@ -504,6 +497,21 @@ function url_to_postid( $url ) {
if ( false === strpos(home_url(), '://www.') )
$url = str_replace('://www.', '://', $url);
if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
$page_on_front = get_option( 'page_on_front' );
if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
return (int) $page_on_front;
}
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if ( empty($rewrite) )
return 0;
// Strip 'index.php/' if we're not using path info permalinks
if ( !$wp_rewrite->using_index_permalinks() )
$url = str_replace( $wp_rewrite->index . '/', '', $url );

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',

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' );
}

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';
}
}

View File

@ -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 );
}
}

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
*/