From 979660717e4dc12486f7de3a9b4d84cfb58dd1e5 Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Sat, 11 Jun 2016 13:30:23 +0000 Subject: [PATCH] Canonical: Introduce `wp_get_canonical_url()`. `wp_get_canonical_url()` encapsulates the URL logic of `rel_canonical()` to provide a consistent way to retrieve the canonical URL for a post. The new filter `get_canonical_url` allows to customize the canonical URL. Props joostdevalk, jipmoors, DrewAPicture, ocean90. Fixes #36168. git-svn-id: https://develop.svn.wordpress.org/trunk@37685 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/link-template.php | 75 +++++++-- .../phpunit/tests/link/wpGetCanonicalURL.php | 143 ++++++++++++++++++ 2 files changed, 203 insertions(+), 15 deletions(-) create mode 100644 tests/phpunit/tests/link/wpGetCanonicalURL.php diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 6ad7f058b9..0d06a3f9ca 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -3538,36 +3538,81 @@ function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) { return apply_filters( 'edit_profile_url', $url, $user_id, $scheme); } +/** + * Returns the canonical URL for a post. + * + * When the post is the same as the current requested page the function will handle the + * pagination arguments too. + * + * @since 4.6.0 + * + * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. + * @return string|false The canonical URL, or false if the post does not exist or has not + * been published yet. + */ +function wp_get_canonical_url( $post = null ) { + $post = get_post( $post ); + + if ( ! $post ) { + return false; + } + + if ( 'publish' !== $post->post_status ) { + return false; + } + + $canonical_url = get_permalink( $post ); + + // If a canonical is being generated for the current page, make sure it has pagination if needed. + if ( $post->ID === get_queried_object_id() ) { + $page = get_query_var( 'page', 0 ); + if ( $page >= 2 ) { + if ( '' == get_option( 'permalink_structure' ) ) { + $canonical_url = add_query_arg( 'page', $page, $canonical_url ); + } else { + $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' ); + } + } + + $cpage = get_query_var( 'cpage', 0 ); + if ( $cpage ) { + $canonical_url = get_comments_pagenum_link( $cpage ); + } + } + + /** + * Filters the canonical URL for a post. + * + * @since 4.6.0 + * + * @param string $string The post's canonical URL. + * @param WP_Post $post Post object. + */ + return apply_filters( 'get_canonical_url', $canonical_url, $post ); +} + /** * Outputs rel=canonical for singular queries. * * @since 2.9.0 + * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`. */ function rel_canonical() { if ( ! is_singular() ) { return; } - if ( ! $id = get_queried_object_id() ) { + $id = get_queried_object_id(); + + if ( 0 === $id ) { return; } - $url = get_permalink( $id ); + $url = wp_get_canonical_url( $id ); - $page = get_query_var( 'page' ); - if ( $page >= 2 ) { - if ( '' == get_option( 'permalink_structure' ) ) { - $url = add_query_arg( 'page', $page, $url ); - } else { - $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' ); - } + if ( ! empty( $url ) ) { + echo '' . "\n"; } - - $cpage = get_query_var( 'cpage' ); - if ( $cpage ) { - $url = get_comments_pagenum_link( $cpage ); - } - echo '\n"; } /** diff --git a/tests/phpunit/tests/link/wpGetCanonicalURL.php b/tests/phpunit/tests/link/wpGetCanonicalURL.php new file mode 100644 index 0000000000..dbbd630008 --- /dev/null +++ b/tests/phpunit/tests/link/wpGetCanonicalURL.php @@ -0,0 +1,143 @@ +post->create( array( + 'post_status' => 'publish', + ) ); + } + + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post_id, true ); + } + + /** + * Test for a non existing post. + */ + public function test_non_existing_post() { + $this->assertFalse( wp_get_canonical_url( -1 ) ); + } + + /** + * Test for a post that is not published. + */ + public function test_post_status() { + $post_id = self::factory()->post->create( array( + 'post_status' => 'draft', + ) ); + + $this->assertFalse( wp_get_canonical_url( $post_id ) ); + } + + /** + * Test for a page that is not the queried object. + */ + public function test_non_current_page() { + $this->assertEquals( get_permalink( self::$post_id ), wp_get_canonical_url( self::$post_id ) ); + } + + /** + * Test non permalink structure page usage. + */ + public function test_paged_with_plain_permalink_structure() { + $link = add_query_arg( array( + 'page' => 2, + 'foo' => 'bar', + ), get_permalink( self::$post_id ) ); + + $this->go_to( $link ); + + $expected = add_query_arg( array( + 'page' => 2, + ), get_permalink( self::$post_id ) ); + + $this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) ); + } + + /** + * Test permalink structure page usage. + */ + public function test_paged_with_custom_permalink_structure() { + $this->set_permalink_structure( '/%postname%/' ); + $page = 2; + + $link = add_query_arg( array( + 'page' => $page, + 'foo' => 'bar', + ), get_permalink( self::$post_id ) ); + + $this->go_to( $link ); + + $expected = trailingslashit( get_permalink( self::$post_id ) ) . user_trailingslashit( $page, 'single_paged' ); + + $this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) ); + } + + /** + * Test non permalink structure comment page usage. + */ + public function test_comments_paged_with_plain_permalink_structure() { + $cpage = 2; + + $link = add_query_arg( array( + 'cpage' => $cpage, + 'foo' => 'bar', + ), get_permalink( self::$post_id ) ); + + $this->go_to( $link ); + + $expected = add_query_arg( array( + 'cpage' => $cpage, + ), get_permalink( self::$post_id ) . '#comments' ); + + $this->assertEquals( $expected , wp_get_canonical_url( self::$post_id ) ); + } + + /** + * Test permalink structure comment page usage. + */ + public function test_comments_paged_with_pretty_permalink_structure() { + global $wp_rewrite; + + $this->set_permalink_structure( '/%postname%/' ); + $cpage = 2; + + $link = add_query_arg( array( + 'cpage' => $cpage, + 'foo' => 'bar', + ), get_permalink( self::$post_id ) ); + + $this->go_to( $link ); + + $expected = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ) . '#comments'; + + $this->assertEquals( $expected , wp_get_canonical_url( self::$post_id ) ); + } + + /** + * Test calling of filter. + */ + public function test_get_canonical_url_filter() { + add_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) ); + $canonical_url = wp_get_canonical_url( self::$post_id ); + remove_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) ); + + $this->assertEquals( $this->canonical_url_filter(), $canonical_url ); + } + + /** + * Filter callback for testing of filter usage. + * + * @return string + */ + public function canonical_url_filter() { + return 'http://canonical.example.org/'; + } +}