Make WordPress Core

Changeset 36307


Ignore:
Timestamp:
01/15/2016 07:55:19 AM (11 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.

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-rewrite.php

    r36255 r36307  
    10231023                        $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
    10241024
     1025                        // Create query and regex for embeds.
     1026                        $embedmatch = $match . $embedregex;
     1027                        $embedquery = $embedindex . '?' . $query . '&embed=true';
     1028
    10251029                        // If asked to, turn the feed queries into comment feed ones.
    10261030                        if ( $forcomments ) {
     
    10341038                        // ...adding on /feed/ regexes => queries
    10351039                        if ( $feed ) {
    1036                                 $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 );
     1040                                $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2, $embedmatch => $embedquery );
    10371041                        }
    10381042
  • trunk/src/wp-includes/embed.php

    r36059 r36307  
    346346        $output = '';
    347347
    348         if ( is_singular() && ! is_front_page() ) {
     348        if ( is_singular() ) {
    349349                $output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
    350350
     
    388388        }
    389389
    390         if ( get_option( 'permalink_structure' ) ) {
    391                 $embed_url = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' );
    392         } else {
     390        $embed_url     = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( 'embed' );
     391        $path_conflict = get_page_by_path( str_replace( home_url(), '', $embed_url ), OBJECT, get_post_types( array( 'public' => true ) ) );
     392
     393        if ( ! get_option( 'permalink_structure' ) || $path_conflict ) {
    393394                $embed_url = add_query_arg( array( 'embed' => 'true' ), get_permalink( $post ) );
    394395        }
  • trunk/src/wp-includes/post.php

    r36269 r36307  
    35753575                 * @param string $slug     The post slug.
    35763576                 */
    3577                 if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
     3577                if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
    35783578                        $suffix = 2;
    35793579                        do {
     
    36053605                 * @param int    $post_parent Post parent ID.
    36063606                 */
    3607                 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 ) ) {
     3607                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 ) ) {
    36083608                        $suffix = 2;
    36093609                        do {
     
    36503650                 * @param string $post_type Post type.
    36513651                 */
    3652                 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 ) ) {
     3652                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 ) ) {
    36533653                        $suffix = 2;
    36543654                        do {
  • trunk/src/wp-includes/query.php

    r36278 r36307  
    14571457                        , 'fields'
    14581458                        , 'menu_order'
     1459                        , 'embed'
    14591460                );
    14601461
     
    17571758                        $this->is_feed = true;
    17581759
     1760                if ( '' != $qv['embed'] ) {
     1761                        $this->is_embed = true;
     1762                }
     1763
    17591764                if ( '' != $qv['tb'] )
    17601765                        $this->is_trackback = true;
     
    17891794                        if ( isset($_query['pagename']) && '' == $_query['pagename'] )
    17901795                                unset($_query['pagename']);
     1796
     1797                        unset( $_query['embed'] );
     1798
    17911799                        if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
    17921800                                $this->is_page = true;
     
    18601868                        $this->set_404();
    18611869
    1862                 $this->is_embed = isset( $qv['embed'] ) && ( $this->is_singular || $this->is_404 );
     1870                $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 );
    18631871
    18641872                $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  • trunk/src/wp-includes/rewrite.php

    r36217 r36307  
    479479        }
    480480
     481        // Get rid of the #anchor
     482        $url_split = explode('#', $url);
     483        $url = $url_split[0];
     484
     485        // Get rid of URL ?query=string
     486        $url_split = explode('?', $url);
     487        $url = $url_split[0];
     488
     489        // Set the correct URL scheme.
     490        $url = set_url_scheme( $url );
     491
     492        // Add 'www.' if it is absent and should be there
     493        if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
     494                $url = str_replace('://', '://www.', $url);
     495
     496        // Strip 'www.' if it is present and shouldn't be
     497        if ( false === strpos(home_url(), '://www.') )
     498                $url = str_replace('://www.', '://', $url);
     499
     500        if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
     501                $page_on_front = get_option( 'page_on_front' );
     502
     503                if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
     504                        return (int) $page_on_front;
     505                }
     506        }
     507
    481508        // Check to see if we are using rewrite rules
    482509        $rewrite = $wp_rewrite->wp_rewrite_rules();
     
    485512        if ( empty($rewrite) )
    486513                return 0;
    487 
    488         // Get rid of the #anchor
    489         $url_split = explode('#', $url);
    490         $url = $url_split[0];
    491 
    492         // Get rid of URL ?query=string
    493         $url_split = explode('?', $url);
    494         $url = $url_split[0];
    495 
    496         // Set the correct URL scheme.
    497         $url = set_url_scheme( $url );
    498 
    499         // Add 'www.' if it is absent and should be there
    500         if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
    501                 $url = str_replace('://', '://www.', $url);
    502 
    503         // Strip 'www.' if it is present and shouldn't be
    504         if ( false === strpos(home_url(), '://www.') )
    505                 $url = str_replace('://www.', '://', $url);
    506514
    507515        // Strip 'index.php/' if we're not using path info permalinks
  • trunk/tests/phpunit/tests/oembed/controller.php

    r35654 r36307  
    170170        }
    171171
    172         function test_request_xml() {
    173                 $user = self::factory()->user->create_and_get( array(
    174                         'display_name' => 'John Doe',
    175                 ) );
     172        /**
     173         * @ticket 34971
     174         */
     175        function test_request_static_front_page() {
    176176                $post = self::factory()->post->create_and_get( array(
    177                         'post_author' => $user->ID,
    178                         'post_title'  => 'Hello World',
    179                 ) );
    180 
    181                 $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
    182                 $request->set_param( 'url', get_permalink( $post->ID ) );
    183                 $request->set_param( 'format', 'xml' );
     177                        'post_title' => 'Front page',
     178                        'post_type'  => 'page',
     179                ) );
     180
     181                update_option( 'show_on_front', 'page' );
     182                update_option( 'page_on_front', $post->ID );
     183
     184                $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
     185                $request->set_param( 'url', home_url() );
    184186                $request->set_param( 'maxwidth', 400 );
    185187
     
    202204                $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
    203205                $this->assertEquals( get_home_url(), $data['provider_url'] );
     206                $this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
     207                $this->assertEquals( get_home_url(), $data['author_url'] );
     208                $this->assertEquals( $post->post_title, $data['title'] );
     209                $this->assertEquals( 'rich', $data['type'] );
     210                $this->assertTrue( $data['width'] <= $request['maxwidth'] );
     211
     212                update_option( 'show_on_front', 'posts' );
     213        }
     214
     215        function test_request_xml() {
     216                $user = self::factory()->user->create_and_get( array(
     217                        'display_name' => 'John Doe',
     218                ) );
     219                $post = self::factory()->post->create_and_get( array(
     220                        'post_author' => $user->ID,
     221                        'post_title'  => 'Hello World',
     222                ) );
     223
     224                $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
     225                $request->set_param( 'url', get_permalink( $post->ID ) );
     226                $request->set_param( 'format', 'xml' );
     227                $request->set_param( 'maxwidth', 400 );
     228
     229                $response = $this->server->dispatch( $request );
     230                $data     = $response->get_data();
     231
     232                $this->assertInternalType( 'array', $data );
     233                $this->assertNotEmpty( $data );
     234
     235                $this->assertArrayHasKey( 'version', $data );
     236                $this->assertArrayHasKey( 'provider_name', $data );
     237                $this->assertArrayHasKey( 'provider_url', $data );
     238                $this->assertArrayHasKey( 'author_name', $data );
     239                $this->assertArrayHasKey( 'author_url', $data );
     240                $this->assertArrayHasKey( 'title', $data );
     241                $this->assertArrayHasKey( 'type', $data );
     242                $this->assertArrayHasKey( 'width', $data );
     243
     244                $this->assertEquals( '1.0', $data['version'] );
     245                $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
     246                $this->assertEquals( get_home_url(), $data['provider_url'] );
    204247                $this->assertEquals( $user->display_name, $data['author_name'] );
    205248                $this->assertEquals( get_author_posts_url( $user->ID, $user->user_nicename ), $data['author_url'] );
  • trunk/tests/phpunit/tests/oembed/discovery.php

    r36059 r36307  
    1010
    1111        function test_add_oembed_discovery_links_front_page() {
    12                 $this->go_to( home_url('/') );
     12                $this->go_to( home_url() );
    1313                $this->assertSame( '', get_echo( 'wp_oembed_add_discovery_links' ) );
     14                $this->assertSame( 0, url_to_postid( home_url() ) );
    1415        }
    1516
     17        /**
     18         * @ticket 34971
     19         */
    1620        function test_add_oembed_discovery_links_static_front_page() {
    1721                update_option( 'show_on_front', 'page' );
    18                 update_option( 'page_for_posts', self::factory()->post->create( array( 'post_title' => 'blog-page', 'post_type' => 'page' ) ) );
    1922                update_option( 'page_on_front', self::factory()->post->create( array( 'post_title' => 'front-page', 'post_type' => 'page' ) ) );
    2023
    21                 $this->go_to( home_url('/') );
    22                 $this->assertSame( '', get_echo( 'wp_oembed_add_discovery_links' ) );
     24                $this->go_to( home_url() );
     25                $this->assertQueryTrue( 'is_front_page', 'is_singular', 'is_page' );
     26
     27                $expected = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
     28                $expected .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
     29
     30                $this->assertSame( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
    2331
    2432                update_option( 'show_on_front', 'posts' );
  • 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}
  • trunk/tests/phpunit/tests/post/wpUniquePostSlug.php

    r35243 r36307  
    303303                $this->assertEquals( '32', $found );
    304304        }
     305
     306        /**
     307         * @ticket 34971
     308         */
     309        public function test_embed_slug_should_be_suffixed_for_posts() {
     310                $this->set_permalink_structure( '/%postname%/' );
     311
     312                $p = self::factory()->post->create( array(
     313                        'post_type' => 'post',
     314                        'post_name' => 'embed',
     315                ) );
     316
     317                $found = wp_unique_post_slug( 'embed', $p, 'publish', 'post', 0 );
     318                $this->assertSame( 'embed-2', $found );
     319        }
     320
     321        /**
     322         * @ticket 34971
     323         */
     324        public function test_embed_slug_should_be_suffixed_for_pages() {
     325                $this->set_permalink_structure( '/%postname%/' );
     326
     327                $p = self::factory()->post->create( array(
     328                        'post_type' => 'page',
     329                        'post_name' => 'embed',
     330                ) );
     331
     332                $found = wp_unique_post_slug( 'embed', $p, 'publish', 'paage', 0 );
     333                $this->assertSame( 'embed-2', $found );
     334        }
     335
     336        /**
     337         * @ticket 34971
     338         */
     339        public function test_embed_slug_should_be_suffixed_for_attachments() {
     340                $this->set_permalink_structure( '/%postname%/' );
     341
     342                $p = self::factory()->post->create( array(
     343                        'post_type' => 'attachment',
     344                        'post_name' => 'embed',
     345                ) );
     346
     347                $found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 );
     348                $this->assertSame( 'embed-2', $found );
     349        }
    305350}
  • trunk/tests/phpunit/tests/rewrite.php

    r36254 r36307  
    305305
    306306        /**
     307         * @ticket 34971
     308         */
     309        function test_url_to_postid_static_front_page() {
     310                $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     311
     312                $this->assertSame( 0, url_to_postid( home_url() ) );
     313
     314                update_option( 'show_on_front', 'page' );
     315                update_option( 'page_on_front', $post_id );
     316
     317                $this->assertSame( $post_id, url_to_postid( set_url_scheme( home_url(), 'http' ) ) );
     318                $this->assertSame( $post_id, url_to_postid( set_url_scheme( home_url(), 'https' ) ) );
     319                $this->assertSame( $post_id, url_to_postid( str_replace( array( 'http://', 'https://' ), 'http://www.', home_url() ) ) );
     320                $this->assertSame( $post_id, url_to_postid( home_url() . '#random' ) );
     321                $this->assertSame( $post_id, url_to_postid( home_url() . '?random' ) );
     322
     323                update_option( 'show_on_front', 'posts' );
     324        }
     325
     326        /**
    307327         * @ticket 21970
    308328         */
Note: See TracChangeset for help on using the changeset viewer.