Ticket #36168: 36168.diff
File 36168.diff, 5.7 KB (added by , 9 years ago) |
---|
-
src/wp-includes/link-template.php
3539 3539 } 3540 3540 3541 3541 /** 3542 * Returns the canonical URL for a post. 3543 * 3544 * When the post is the same as the current requested page the function will handle the 3545 * pagination arguments too. 3546 * 3547 * @since 4.6.0 3548 * 3549 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. 3550 * @return string|false The canonical URL, or false if the post does not exist or has not 3551 * been published yet. 3552 */ 3553 function wp_get_canonical_url( $post = null ) { 3554 $post = get_post( $post ); 3555 3556 if ( ! $post ) { 3557 return false; 3558 } 3559 3560 if ( 'publish' !== $post->post_status ) { 3561 return false; 3562 } 3563 3564 $canonical_url = get_permalink( $post ); 3565 3566 // If a canonical is being generated for the current page, make sure it has pagination if needed. 3567 if ( $post->ID === get_queried_object_id() ) { 3568 $page = get_query_var( 'page', 0 ); 3569 if ( $page >= 2 ) { 3570 if ( '' == get_option( 'permalink_structure' ) ) { 3571 $canonical_url = add_query_arg( 'page', $page, $canonical_url ); 3572 } else { 3573 $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' ); 3574 } 3575 } 3576 3577 $cpage = get_query_var( 'cpage' ); 3578 if ( $cpage ) { 3579 $canonical_url = get_comments_pagenum_link( $cpage ); 3580 } 3581 } 3582 3583 /** 3584 * Filters the canonical URL for a post. 3585 * 3586 * @since 4.6.0 3587 * 3588 * @param string $string The post's canonical URL. 3589 * @param WP_Post $post Post object. 3590 */ 3591 return apply_filters( 'get_canonical_url', $canonical_url, $post ); 3592 } 3593 3594 /** 3542 3595 * Outputs rel=canonical for singular queries. 3543 3596 * 3544 3597 * @since 2.9.0 3598 * @since 4.6.0 Adjusted to use wp_get_canonical_url() 3545 3599 */ 3546 3600 function rel_canonical() { 3547 3601 if ( ! is_singular() ) { … … 3548 3602 return; 3549 3603 } 3550 3604 3551 if ( ! $id = get_queried_object_id() ) { 3605 $id = get_queried_object_id(); 3606 3607 if ( 0 === $id ) { 3552 3608 return; 3553 3609 } 3554 3610 3555 $url = get_permalink( $id );3611 $url = wp_get_canonical_url( $id ); 3556 3612 3557 $page = get_query_var( 'page' ); 3558 if ( $page >= 2 ) { 3559 if ( '' == get_option( 'permalink_structure' ) ) { 3560 $url = add_query_arg( 'page', $page, $url ); 3561 } else { 3562 $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' ); 3563 } 3613 if ( ! empty( $url ) ) { 3614 echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n"; 3564 3615 } 3565 3566 $cpage = get_query_var( 'cpage' );3567 if ( $cpage ) {3568 $url = get_comments_pagenum_link( $cpage );3569 }3570 echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n";3571 3616 } 3572 3617 3573 3618 /** -
tests/phpunit/tests/link/getCanonicalURL.php
1 <?php 2 3 /** 4 * @group canonical 5 * @group foo 6 */ 7 8 class Tests_GetCanonicalURL extends WP_UnitTestCase { 9 public static $post_id; 10 11 public static function wpSetUpBeforeClass( $factory ) { 12 self::$post_id = $factory->post->create( array( 13 'post_status' => 'publish', 14 ) ); 15 } 16 17 public static function wpTearDownAfterClass() { 18 wp_delete_post( self::$post_id, true ); 19 } 20 21 /** 22 * Test for a non existing post. 23 */ 24 public function test_non_existing_post() { 25 $this->assertFalse( wp_get_canonical_url( -1 ) ); 26 } 27 28 /** 29 * Test for a page that is not published. 30 */ 31 public function test_post_status() { 32 $post_id = self::factory()->post->create( array( 33 'post_status' => 'draft', 34 ) ); 35 36 $this->assertFalse( wp_get_canonical_url( $post_id ) ); 37 } 38 39 /** 40 * Test for a page that is not the queried object. 41 */ 42 public function test_non_current_page() { 43 $this->assertEquals( get_permalink( self::$post_id ), wp_get_canonical_url( self::$post_id ) ); 44 } 45 46 /** 47 * Test non permalink structure page usage. 48 */ 49 public function test_paged() { 50 $link = add_query_arg( array( 51 'page' => 2, 52 'foo' => 'bar', 53 ), get_permalink( self::$post_id ) ); 54 55 $this->go_to( $link ); 56 57 $expected = add_query_arg( array( 58 'page' => 2, 59 ), get_permalink( self::$post_id ) ); 60 61 $this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) ); 62 } 63 64 /** 65 * Test permalink structure page usage. 66 */ 67 public function test_paged_permalink_structure() { 68 $this->set_permalink_structure( '/%postname%/' ); 69 $page = 2; 70 71 $link = add_query_arg( array( 72 'page' => $page, 73 'foo' => 'bar', 74 ), get_permalink( self::$post_id ) ); 75 76 $this->go_to( $link ); 77 78 $expected = trailingslashit( get_permalink( self::$post_id ) ) . user_trailingslashit( $page, 'single_paged' ); 79 80 $this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) ); 81 } 82 83 /** 84 * Test usage of cpage. 85 */ 86 public function test_cpage() { 87 $link = add_query_arg( array( 88 'cpage' => 2, 89 ), get_permalink( self::$post_id ) ); 90 91 $this->go_to( $link ); 92 93 $expected = $link . '#comments'; 94 95 $this->assertEquals( $expected , wp_get_canonical_url( self::$post_id ) ); 96 } 97 98 /** 99 * Test calling of filter. 100 */ 101 public function test_get_canonical_url_filter() { 102 add_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) ); 103 $canonical_url = wp_get_canonical_url( self::$post_id ); 104 remove_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) ); 105 106 $this->assertEquals( $this->canonical_url_filter(), $canonical_url ); 107 } 108 109 /** 110 * Filter callback for testing of filter usage. 111 * 112 * @return string 113 */ 114 public function canonical_url_filter() { 115 return 'http://canonical.example.org/'; 116 } 117 }