Make WordPress Core


Ignore:
Timestamp:
01/15/2016 07:55:19 AM (9 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/oembed/postEmbedUrl.php

    r35242 r36307  
    55 */
    66class Tests_Post_Embed_URL extends WP_UnitTestCase {
    7     function test_get_post_embed_url_non_existent_post() {
     7    function test_non_existent_post() {
    88        $embed_url = get_post_embed_url( 0 );
    99        $this->assertFalse( $embed_url );
    1010    }
    1111
    12     function test_get_post_embed_url_with_pretty_permalinks() {
    13         update_option( 'permalink_structure', '/%postname%' );
     12    function test_with_pretty_permalinks() {
     13        $this->set_permalink_structure( '/%postname%' );
    1414
    1515        $post_id   = self::factory()->post->create();
     
    1717        $embed_url = get_post_embed_url( $post_id );
    1818
    19         $this->assertEquals( user_trailingslashit( trailingslashit( $permalink ) . 'embed' ), $embed_url );
    20 
    21         update_option( 'permalink_structure', '' );
     19        $this->assertEquals( $permalink . '/embed', $embed_url );
    2220    }
    2321
    24     function test_get_post_embed_url_with_ugly_permalinks() {
     22    function test_with_ugly_permalinks() {
    2523        $post_id   = self::factory()->post->create();
    2624        $permalink = get_permalink( $post_id );
     
    2927        $this->assertEquals( $permalink . '&embed=true', $embed_url );
    3028    }
     29
     30    /**
     31     * @ticket 34971
     32     */
     33    function test_static_front_page() {
     34        $this->set_permalink_structure( '/%postname%/' );
     35
     36        $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     37
     38        update_option( 'show_on_front', 'page' );
     39        update_option( 'page_on_front', $post_id );
     40
     41        $embed_url = get_post_embed_url( $post_id );
     42
     43        $this->assertSame( user_trailingslashit( trailingslashit( home_url() ) . 'embed' ), $embed_url );
     44
     45        update_option( 'show_on_front', 'posts' );
     46    }
     47
     48    /**
     49     * @ticket 34971
     50     */
     51    function test_static_front_page_with_ugly_permalinks() {
     52        $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     53
     54        update_option( 'show_on_front', 'page' );
     55        update_option( 'page_on_front', $post_id );
     56
     57        $embed_url = get_post_embed_url( $post_id );
     58
     59        $this->assertSame( trailingslashit( home_url() ) . '?embed=true', $embed_url );
     60
     61        update_option( 'show_on_front', 'posts' );
     62    }
     63
     64    /**
     65     * @ticket 34971
     66     */
     67    function test_page_conflicts_with_embed_slug() {
     68        $this->set_permalink_structure( '/%postname%/' );
     69
     70        $parent_page = self::factory()->post->create( array( 'post_type' => 'page' ) );
     71
     72        add_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
     73        $child_page = self::factory()->post->create( array(
     74            'post_type'   => 'page',
     75            'post_parent' => $parent_page,
     76            'post_name'   => 'embed',
     77        ) );
     78        remove_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
     79
     80        $this->assertSame( get_permalink( $parent_page ) . '?embed=true', get_post_embed_url( $parent_page ) );
     81        $this->assertSame( get_permalink( $child_page ) . 'embed/', get_post_embed_url( $child_page ) );
     82    }
     83
     84    /**
     85     * @ticket 34971
     86     */
     87    function test_static_front_page_conflicts_with_embed_slug() {
     88        $this->set_permalink_structure( '/%postname%/' );
     89
     90        // Create a post with the 'embed' post_name
     91        add_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
     92        $post_embed_slug = self::factory()->post->create( array( 'post_name' => 'embed' ) );
     93        remove_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ) );
     94        $page_front = self::factory()->post->create( array( 'post_type' => 'page' ) );
     95
     96        update_option( 'show_on_front', 'page' );
     97        update_option( 'page_on_front', $page_front );
     98
     99        $this->assertSame( home_url() . '/embed/embed/', get_post_embed_url( $post_embed_slug ) );
     100        $this->assertSame( home_url() . '/?embed=true', get_post_embed_url( $page_front ) );
     101
     102        update_option( 'show_on_front', 'posts' );
     103    }
     104
     105    public function filter_unique_post_slug() {
     106        return 'embed';
     107    }
    31108}
Note: See TracChangeset for help on using the changeset viewer.