Make WordPress Core

Ticket #34971: 34971.diff

File 34971.diff, 10.5 KB (added by swissspidy, 9 years ago)
  • src/wp-includes/class-wp-rewrite.php

    diff --git src/wp-includes/class-wp-rewrite.php src/wp-includes/class-wp-rewrite.php
    index 1773929..490507b 100644
    class WP_Rewrite { 
    10001000                        $feedmatch2 = $match . $feedregex2;
    10011001                        $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
    10021002
     1003                        // Create query and regex for embeds.
     1004                        $embedmatch = $match . $embedregex;
     1005                        $embedquery = $embedindex . '?' . $query . '&embed=true';
     1006
    10031007                        // If asked to, turn the feed queries into comment feed ones.
    10041008                        if ( $forcomments ) {
    10051009                                $feedquery .= '&withcomments=1';
    class WP_Rewrite { 
    10111015
    10121016                        // ...adding on /feed/ regexes => queries
    10131017                        if ( $feed ) {
    1014                                 $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 );
     1018                                $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2, $embedmatch => $embedquery );
    10151019                        }
    10161020
    10171021                        //...and /page/xx ones
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 55f6172..ef5b00c 100644
    class WP_Query { 
    14561456                        , 'title'
    14571457                        , 'fields'
    14581458                        , 'menu_order'
     1459                        , 'embed'
    14591460                );
    14601461
    14611462                foreach ( $keys as $key ) {
    class WP_Query { 
    17561757                if ( '' != $qv['feed'] )
    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;
    17611766
    class WP_Query { 
    17881793                        // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
    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;
    17931801                                $this->is_home = false;
    class WP_Query { 
    18591867                if ( '404' == $qv['error'] )
    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 ) );
    18651873                $this->query_vars_changed = false;
  • src/wp-includes/rewrite.php

    diff --git src/wp-includes/rewrite.php src/wp-includes/rewrite.php
    index 7f3b2ed..b867cdc 100644
    function url_to_postid( $url ) { 
    445445                        return $id;
    446446        }
    447447
    448         // Check to see if we are using rewrite rules
    449         $rewrite = $wp_rewrite->wp_rewrite_rules();
    450 
    451         // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
    452         if ( empty($rewrite) )
    453                 return 0;
    454 
    455448        // Get rid of the #anchor
    456449        $url_split = explode('#', $url);
    457450        $url = $url_split[0];
    function url_to_postid( $url ) { 
    471464        if ( false === strpos(home_url(), '://www.') )
    472465                $url = str_replace('://www.', '://', $url);
    473466
     467        if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
     468                $page_on_front = get_option( 'page_on_front' );
     469
     470                if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
     471                        return (int) $page_on_front;
     472                }
     473        }
     474
     475        // Check to see if we are using rewrite rules
     476        $rewrite = $wp_rewrite->wp_rewrite_rules();
     477
     478        // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
     479        if ( empty($rewrite) )
     480                return 0;
     481
    474482        // Strip 'index.php/' if we're not using path info permalinks
    475483        if ( !$wp_rewrite->using_index_permalinks() )
    476484                $url = str_replace( $wp_rewrite->index . '/', '', $url );
  • tests/phpunit/tests/oembed/controller.php

    diff --git tests/phpunit/tests/oembed/controller.php tests/phpunit/tests/oembed/controller.php
    index 5cca696..9aa1524 100644
    class Test_oEmbed_Controller extends WP_UnitTestCase { 
    169169                $this->assertTrue( $data['width'] <= $request['maxwidth'] );
    170170        }
    171171
     172        /**
     173         * @ticket 34971
     174         */
     175        function test_request_static_front_page() {
     176                $show_on_front = get_option( 'show_on_front' );
     177                $page_on_front = get_option( 'page_on_front' );
     178
     179                $post = self::factory()->post->create_and_get( array(
     180                        'post_title' => 'Front page',
     181                        'post_type'  => 'page',
     182                ) );
     183
     184                update_option( 'show_on_front', 'page' );
     185                update_option( 'page_on_front', $post->ID );
     186
     187                $request = new WP_REST_Request( 'GET', '/oembed/1.0/embed' );
     188                $request->set_param( 'url', home_url() );
     189                $request->set_param( 'maxwidth', 400 );
     190
     191                $response = $this->server->dispatch( $request );
     192                $data     = $response->get_data();
     193
     194                $this->assertInternalType( 'array', $data );
     195                $this->assertNotEmpty( $data );
     196
     197                $this->assertArrayHasKey( 'version', $data );
     198                $this->assertArrayHasKey( 'provider_name', $data );
     199                $this->assertArrayHasKey( 'provider_url', $data );
     200                $this->assertArrayHasKey( 'author_name', $data );
     201                $this->assertArrayHasKey( 'author_url', $data );
     202                $this->assertArrayHasKey( 'title', $data );
     203                $this->assertArrayHasKey( 'type', $data );
     204                $this->assertArrayHasKey( 'width', $data );
     205
     206                $this->assertEquals( '1.0', $data['version'] );
     207                $this->assertEquals( get_bloginfo( 'name' ), $data['provider_name'] );
     208                $this->assertEquals( get_home_url(), $data['provider_url'] );
     209                $this->assertEquals( get_bloginfo( 'name' ), $data['author_name'] );
     210                $this->assertEquals( get_home_url(), $data['author_url'] );
     211                $this->assertEquals( $post->post_title, $data['title'] );
     212                $this->assertEquals( 'rich', $data['type'] );
     213                $this->assertTrue( $data['width'] <= $request['maxwidth'] );
     214
     215                update_option( 'show_on_front', $show_on_front );
     216                update_option( 'page_on_front', $page_on_front );
     217        }
     218
    172219        function test_request_xml() {
    173220                $user = self::factory()->user->create_and_get( array(
    174221                        'display_name' => 'John Doe',
  • tests/phpunit/tests/oembed/discovery.php

    diff --git tests/phpunit/tests/oembed/discovery.php tests/phpunit/tests/oembed/discovery.php
    index 63eb5bb..f3d5e2c 100644
    class Tests_oEmbed_Discovery extends WP_UnitTestCase { 
    4747
    4848                $this->assertEquals( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
    4949        }
     50
     51        /**
     52         * @ticket 34971
     53         */
     54        function test_add_oembed_discovery_links_static_front_page() {
     55                $show_on_front = get_option( 'show_on_front' );
     56                $page_on_front = get_option( 'page_on_front' );
     57
     58                $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     59
     60                $this->assertSame( 0, url_to_postid( home_url() ) );
     61
     62                update_option( 'show_on_front', 'page' );
     63                update_option( 'page_on_front', $page_id );
     64
     65                $this->go_to( home_url() );
     66                $this->assertQueryTrue( 'is_front_page', 'is_singular', 'is_page' );
     67
     68                $expected = '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
     69                $expected .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
     70
     71                $this->assertEquals( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
     72
     73                update_option( 'show_on_front', $show_on_front );
     74                update_option( 'page_on_front', $page_on_front );
     75        }
    5076}
  • tests/phpunit/tests/oembed/postEmbedUrl.php

    diff --git tests/phpunit/tests/oembed/postEmbedUrl.php tests/phpunit/tests/oembed/postEmbedUrl.php
    index dda0f3c..c38a249 100644
    class Tests_Post_Embed_URL extends WP_UnitTestCase { 
    2121                update_option( 'permalink_structure', '' );
    2222        }
    2323
     24        /**
     25         * @ticket 34971
     26         */
     27        function test_get_post_embed_url_static_front_page() {
     28                $show_on_front = get_option( 'show_on_front' );
     29                $page_on_front = get_option( 'page_on_front' );
     30
     31                update_option( 'permalink_structure', '/%postname%' );
     32
     33                $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     34
     35                update_option( 'show_on_front', 'page' );
     36                update_option( 'page_on_front', $post_id );
     37
     38                $embed_url = get_post_embed_url( $post_id );
     39
     40                $this->assertEquals( user_trailingslashit( trailingslashit( home_url() ) . 'embed' ), $embed_url );
     41
     42                update_option( 'show_on_front', $show_on_front );
     43                update_option( 'page_on_front', $page_on_front );
     44                update_option( 'permalink_structure', '' );
     45        }
     46
    2447        function test_get_post_embed_url_with_ugly_permalinks() {
    2548                $post_id   = self::factory()->post->create();
    2649                $permalink = get_permalink( $post_id );
    class Tests_Post_Embed_URL extends WP_UnitTestCase { 
    2851
    2952                $this->assertEquals( $permalink . '&embed=true', $embed_url );
    3053        }
     54
     55        /**
     56         * @ticket 34971
     57         */
     58        function test_get_post_embed_url_static_front_page_with_ugly_permalinks() {
     59                $show_on_front = get_option( 'show_on_front' );
     60                $page_on_front = get_option( 'page_on_front' );
     61
     62                $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     63
     64                update_option( 'show_on_front', 'page' );
     65                update_option( 'page_on_front', $post_id );
     66
     67                $embed_url = get_post_embed_url( $post_id );
     68
     69                $this->assertEquals( trailingslashit( home_url() ) . '?embed=true', $embed_url );
     70
     71                update_option( 'show_on_front', $show_on_front );
     72                update_option( 'page_on_front', $page_on_front );
     73        }
    3174}
  • tests/phpunit/tests/rewrite.php

    diff --git tests/phpunit/tests/rewrite.php tests/phpunit/tests/rewrite.php
    index 1f7426e..3df47fb 100644
    class Tests_Rewrite extends WP_UnitTestCase { 
    304304        }
    305305
    306306        /**
     307         * @ticket 34971
     308         */
     309        function test_url_to_postid_static_front_page() {
     310                $show_on_front = get_option( 'show_on_front' );
     311                $page_on_front = get_option( 'page_on_front' );
     312
     313                $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
     314
     315                $this->assertSame( 0, url_to_postid( home_url() ) );
     316
     317                update_option( 'show_on_front', 'page' );
     318                update_option( 'page_on_front', $post_id );
     319
     320                $this->assertSame( $post_id, url_to_postid( set_url_scheme( home_url(), 'http' ) ) );
     321                $this->assertSame( $post_id, url_to_postid( set_url_scheme( home_url(), 'https' ) ) );
     322                $this->assertSame( $post_id, url_to_postid( str_replace( array( 'http://', 'https://' ), 'http://www.', home_url() ) ) );
     323                $this->assertSame( $post_id, url_to_postid( home_url() . '#random' ) );
     324                $this->assertSame( $post_id, url_to_postid( home_url() . '?random' ) );
     325
     326                update_option( 'show_on_front', $show_on_front );
     327                update_option( 'page_on_front', $page_on_front );
     328        }
     329
     330        /**
    307331         * @ticket 21970
    308332         */
    309333        function test_parse_request_with_post_slug_that_clashes_with_a_trashed_page() {