<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[37685] trunk: Canonical: Introduce `wp_get_canonical_url()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/37685">37685</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/37685","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-11 13:30:23 +0000 (Sat, 11 Jun 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/36168">#36168</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeslinktemplatephp">trunk/src/wp-includes/link-template.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestslinkwpGetCanonicalURLphp">trunk/tests/phpunit/tests/link/wpGetCanonicalURL.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeslinktemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/link-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/link-template.php   2016-06-11 05:14:18 UTC (rev 37684)
+++ trunk/src/wp-includes/link-template.php     2016-06-11 13:30:23 UTC (rev 37685)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3539,35 +3539,80 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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 );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Outputs rel=canonical for singular queries.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function rel_canonical() {
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! is_singular() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! $id = get_queried_object_id() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = get_queried_object_id();
+
+       if ( 0 === $id ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $url = get_permalink( $id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $url = wp_get_canonical_url( $id );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $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' );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $url ) ) {
+               echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-       $cpage = get_query_var( 'cpage' );
-       if ( $cpage ) {
-               $url = get_comments_pagenum_link( $cpage );
-       }
-       echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n";
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestslinkwpGetCanonicalURLphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/link/wpGetCanonicalURL.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/link/wpGetCanonicalURL.php                              (rev 0)
+++ trunk/tests/phpunit/tests/link/wpGetCanonicalURL.php        2016-06-11 13:30:23 UTC (rev 37685)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,143 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group link
+ * @group canonical
+ * @covers ::wp_get_canonical_url
+ */
+class Tests_WpGetCanonicalURL extends WP_UnitTestCase {
+       public static $post_id;
+
+       public static function wpSetUpBeforeClass( $factory ) {
+               self::$post_id = $factory->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/';
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>