Make WordPress Core

Ticket #34890: 34890.2.diff

File 34890.2.diff, 2.5 KB (added by peterwilsoncc, 9 years ago)
  • src/wp-includes/link-template.php

    diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php
    index 5aabffa..1b17329 100644
    a b function rel_canonical() { 
    35023502        $url = get_permalink( $id );
    35033503
    35043504        $page = get_query_var( 'page' );
    3505         if ( $page ) {
    3506                 $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' );
     3505        if ( ( $page ) && ( 1 != $page ) ) {
     3506                if ( '' == get_option( 'permalink_structure' ) ) {
     3507                        $url = add_query_arg( 'page', $page, $url );
     3508                }
     3509                else {
     3510                        $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' );
     3511                }
    35073512        }
    35083513
    35093514        $cpage = get_query_var( 'cpage' );
  • tests/phpunit/tests/url.php

    diff --git a/tests/phpunit/tests/url.php b/tests/phpunit/tests/url.php
    index 3c3c19a..5656da6 100644
    a b class Tests_URL extends WP_UnitTestCase { 
    270270                force_ssl_admin( $forced_admin );
    271271        }
    272272
     273        public function test_canonical_meta_tag() {
     274                $now = time();
     275                $post_id = self::factory()->post->create( array(
     276                        'post_content' => rand_str() . "\n\n<!--nextpage-->\n\n" . rand_str(),
     277                ) );
     278
     279                $this->set_permalink_structure( '' );
     280
     281                $url1 = add_query_arg( array(
     282                        'page' => 2,
     283                        'p'    => $post_id,
     284                ), home_url() );
     285                $url2 = add_query_arg( array(
     286                        'p'    => $post_id,
     287                        'page' => 2,
     288                ), home_url() );
     289
     290                // Ensure the two URLs differ.
     291                // If add_query_arg changes in the future and sorts by key, these
     292                // tests will need to be rewritten to check different URLs return
     293                // the same rel_canonical meta tag.
     294                $this->assertNotEquals( ' ' . $url1, $url2 );
     295
     296                $this->go_to( $url1 );
     297                ob_start();
     298                rel_canonical();
     299                $canonical1 = ob_get_clean();
     300
     301                $this->go_to( $url2 );
     302                ob_start();
     303                rel_canonical();
     304                $canonical2 = ob_get_clean();
     305
     306                $this->assertEquals( $canonical1, $canonical2 );
     307
     308                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     309
     310                $permalink = get_permalink( $post_id );
     311                $permalink_page_two = untrailingslashit( $permalink ) . '/2/';
     312                $canonical3 = '<link rel="canonical" href="' . esc_url( $permalink_page_two ) . "\" />\n";
     313
     314                $this->go_to( $permalink_page_two );
     315                ob_start();
     316                rel_canonical();
     317                $canonical4 = ob_get_clean();
     318
     319                $this->assertEquals( $canonical3, $canonical4 );
     320        }
     321
    273322        public function test_get_adjacent_post() {
    274323                $now = time();
    275324                $post_id = self::factory()->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );