<!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>[32648] trunk: When parsing what appears to be a date archive request, check for a post with a clashing permalink before resolving to the archive.</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/32648">32648</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/32648","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-05-29 13:09:26 +0000 (Fri, 29 May 2015)</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'>When parsing what appears to be a date archive request, check for a post with a clashing permalink before resolving to the archive.

A URL like `example.com/2015/05/15/` generally resolves to the May 15, 2015 date
archive. But in certain cases, it could also be the permalink of a post with
the slug `'2015'`. When a conflict of this sort is detected, resolve to the post
instead of the archive.

URL conflicts of this sort should no longer occur for new posts; see <a href="https://core.trac.wordpress.org/changeset/32647">[32647]</a>.

Props valendesigns, boonebgorges, Denis-de-Bernardy.
Fixes <a href="https://core.trac.wordpress.org/ticket/5305">#5305</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpphp">trunk/src/wp-includes/class-wp.php</a></li>
<li><a href="#trunksrcwpincludesrewritephp">trunk/src/wp-includes/rewrite.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsrewritenumericSlugsphp">trunk/tests/phpunit/tests/rewrite/numericSlugs.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpphp"></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/class-wp.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp.php        2015-05-29 12:52:17 UTC (rev 32647)
+++ trunk/src/wp-includes/class-wp.php  2015-05-29 13:09:26 UTC (rev 32648)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -308,6 +308,9 @@
</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">+                // Resolve conflicts between posts with numeric slugs and date archive queries.
+               $this->query_vars = wp_resolve_numeric_slug_conflicts( $this->query_vars );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( (array) $this->private_query_vars as $var) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( isset($this->extra_query_vars[$var]) )
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->query_vars[$var] = $this->extra_query_vars[$var];
</span></span></pre></div>
<a id="trunksrcwpincludesrewritephp"></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/rewrite.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rewrite.php 2015-05-29 12:52:17 UTC (rev 32647)
+++ trunk/src/wp-includes/rewrite.php   2015-05-29 13:09:26 UTC (rev 32648)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -283,7 +283,117 @@
</span><span class="cx" style="display: block; padding: 0 10px">        return $base;
</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">+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Resolve numeric slugs that collide with date permalinks.
+ *
+ * Permalinks of posts with numeric slugs can sometimes look to `WP_Query::parse_query()` like a date archive,
+ * as when your permalink structure is `/%year%/%postname%/` and a post with post_name '05' has the URL
+ * `/2015/05/`. This function detects conflicts of this type and resolves them in favor of the post permalink.
+ *
+ * Note that, since 4.3.0, `wp_unique_post_slug()` prevents the creation of post slugs that would result in a date
+ * archive conflict. The resolution performed in this function is primarily for legacy content, as well as cases when
+ * the admin has changed the site's permalink structure in a way that introduces URL conflicts.
+ *
+ * @since 4.3.0
+ *
+ * @param array $query_vars Query variables for setting up the loop, as determined in `WP::parse_request()`.
+ * @return array Returns the original array of query vars, with date/post conflicts resolved.
+ */
+function wp_resolve_numeric_slug_conflicts( $query_vars = array() ) {
+       if ( ! isset( $query_vars['year'] ) && ! isset( $query_vars['monthnum'] ) && ! isset( $query_vars['day'] ) ) {
+               return $query_vars;
+       }
+
+       // Identify the 'postname' position in the permastruct array.
+       $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
+       $postname_index = array_search( '%postname%', $permastructs );
+
+       if ( false === $postname_index ) {
+               return $query_vars;
+       }
+
+       /*
+        * A numeric slug could be confused with a year, month, or day, depending on position. To account for
+        * the possibility of post pagination (eg 2015/2 for the second page of a post called '2015'), our
+        * `is_*` checks are generous: check for year-slug clashes when `is_year` *or* `is_month`, and check
+        * for month-slug clashes when `is_month` *or* `is_day`.
+        */
+       $compare = '';
+       if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) {
+               $compare = 'year';
+       } elseif ( '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
+               $compare = 'monthnum';
+       } elseif ( '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
+               $compare = 'day';
+       }
+
+       if ( ! $compare ) {
+               return $query_vars;
+       }
+
+       // This is the potentially clashing slug.
+       $value = $query_vars[ $compare ];
+
+       $post = get_page_by_path( $value, OBJECT, 'post' );
+       if ( ! ( $post instanceof WP_Post ) ) {
+               return $query_vars;
+       }
+
+       // If the date of the post doesn't match the date specified in the URL, resolve to the date archive.
+       if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) {
+               // $matches[1] is the year the post was published.
+               if ( intval( $query_vars['year'] ) !== intval( $matches[1] ) ) {
+                       return $query_vars;
+               }
+
+               // $matches[2] is the month the post was published.
+               if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && intval( $query_vars['monthnum'] ) !== intval( $matches[2] ) ) {
+                       return $query_vars;
+               }
+       }
+
+       /*
+        * If the located post contains nextpage pagination, then the URL chunk following postname may be
+        * intended as the page number. Verify that it's a valid page before resolving to it.
+        */
+       $maybe_page = '';
+       if ( 'year' === $compare && isset( $query_vars['monthnum'] ) ) {
+               $maybe_page = $query_vars['monthnum'];
+       } elseif ( 'monthnum' === $compare && isset( $query_vars['day'] ) ) {
+               $maybe_page = $query_vars['day'];
+       }
+
+       $post_page_count = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
+
+       // If the post doesn't have multiple pages, but a 'page' candidate is found, resolve to the date archive.
+       if ( 1 === $post_page_count && $maybe_page ) {
+               return $query_vars;
+       }
+
+       // If the post has multiple pages and the 'page' number isn't valid, resolve to the date archive.
+       if ( $post_page_count > 1 && $maybe_page > $post_page_count ) {
+               return $query_vars;
+       }
+
+       // If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage.
+       if ( '' !== $maybe_page ) {
+               $query_vars['page'] = intval( $maybe_page );
+       }
+
+       // Next, unset autodetected date-related query vars.
+       unset( $query_vars['year'] );
+       unset( $query_vars['monthnum'] );
+       unset( $query_vars['day'] );
+
+       // Then, set the identified post.
+       $query_vars['name'] = $post->post_name;
+
+       // Finally, return the modified query vars.
+       return $query_vars;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Examine a url and try to determine the post ID it represents.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * Checks are supposedly from the hosted site blog.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -401,6 +511,9 @@
</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">+                        // Resolve conflicts between posts with numeric slugs and date archive queries.
+                       $query = wp_resolve_numeric_slug_conflicts( $query );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Do the query
</span><span class="cx" style="display: block; padding: 0 10px">                        $query = new WP_Query( $query );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! empty( $query->posts ) && $query->is_singular )
</span></span></pre></div>
<a id="trunktestsphpunittestsrewritenumericSlugsphp"></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/rewrite/numericSlugs.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rewrite/numericSlugs.php                                (rev 0)
+++ trunk/tests/phpunit/tests/rewrite/numericSlugs.php  2015-05-29 13:09:26 UTC (rev 32648)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,538 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group rewrite
+ * @ticket 5305
+ */
+class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase {
+       private $old_current_user;
+
+       public function setUp() {
+               parent::setUp();
+               $this->author_id = $this->factory->user->create( array( 'role' => 'editor' ) );
+
+               // Override the post/archive slug collision prevention in `wp_unique_post_slug()`.
+               add_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ), 10, 6 );
+       }
+
+       public function tearDown() {
+               remove_filter( 'wp_unique_post_slug', array( $this, 'filter_unique_post_slug' ), 10, 6 );
+       }
+
+       public function test_go_to_year_segment_collision_without_title() {
+               global $wp_rewrite, $wpdb;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '2015',
+                       'post_date'    => '2015-02-01 01:00:00'
+               ) );
+
+               // Force an ID that resembles a year format
+               $wpdb->update(
+                       $wpdb->posts,
+                       array(
+                               'ID'   => '2015',
+                               'guid' => 'http://example.org/?p=2015'
+                       ),
+                       array( 'ID' => $id )
+               );
+
+               $this->go_to( get_permalink( '2015' ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_year_segment_collision_without_title() {
+               global $wp_rewrite, $wpdb;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '2015',
+                       'post_date'    => '2015-02-01 01:00:00'
+               ) );
+
+               // Force an ID that resembles a year format
+               $wpdb->update(
+                       $wpdb->posts,
+                       array(
+                               'ID'   => '2015',
+                               'guid' => 'http://example.org/?p=2015'
+                       ),
+                       array( 'ID' => $id )
+               );
+
+               $this->assertEquals( '2015', url_to_postid( get_permalink( '2015' ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_go_to_year_segment_collision_with_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '2015',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_year_segment_collision_with_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '2015',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_go_to_month_segment_collision_without_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '02',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_month_segment_collision_without_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '02',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_go_to_month_segment_collision_without_title_no_leading_zero() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '2',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_month_segment_collision_without_title_no_leading_zero() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '2',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_go_to_month_segment_collision_with_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_month_segment_collision_with_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_go_to_month_segment_collision_with_title_no_leading_zero() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '2',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_month_segment_collision_with_title_no_leading_zero() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '2',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_go_to_day_segment_collision_without_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '01',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_day_segment_collision_without_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '',
+                       'post_name'    => '01',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_go_to_day_segment_collision_with_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '01',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_url_to_postid_day_segment_collision_with_title() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '01',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_numeric_slug_permalink_conflicts_should_only_be_resolved_for_the_main_query() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => rand_str(),
+                       'post_title'   => '01',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $q = new WP_Query( array(
+                       'year'     => '2015',
+                       'monthnum' => '02',
+                       'day'      => '01',
+               ) );
+
+               $this->assertTrue( $q->is_day );
+               $this->assertFalse( $q->is_single );
+
+               $wp_rewrite->set_permalink_structure('');
+       }
+
+       public function test_month_slug_collision_should_resolve_to_date_archive_when_year_does_not_match_post_year() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               // Make sure a post is published in 2013/02, to avoid 404s.
+               $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'foo',
+                       'post_title'   => 'bar',
+                       'post_date'    => '2013-02-01 01:00:00',
+               ) );
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'foo',
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $permalink = get_permalink( $id );
+               $permalink = str_replace( '/2015/', '/2013/', $permalink );
+
+               $this->go_to( $permalink );
+
+               $this->assertTrue( is_month() );
+       }
+
+       public function test_day_slug_collision_should_resolve_to_date_archive_when_monthnum_does_not_match_post_month() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               // Make sure a post is published on 2015/01/01, to avoid 404s.
+               $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'foo',
+                       'post_title'   => 'bar',
+                       'post_date'    => '2015-01-02 01:00:00',
+               ) );
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'foo',
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-02 01:00:00',
+               ) );
+
+               $permalink = get_permalink( $id );
+               $permalink = str_replace( '/2015/02/', '/2015/01/', $permalink );
+
+               $this->go_to( $permalink );
+
+               $this->assertTrue( is_day() );
+       }
+
+       public function test_date_slug_collision_should_distinguish_valid_pagination_from_date() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-01 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) . '1' );
+
+               $this->assertFalse( is_day() );
+       }
+
+       public function test_date_slug_collision_should_distinguish_too_high_pagination_from_date() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-05 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) . '5' );
+
+               $this->assertTrue( is_day() );
+       }
+
+       public function test_date_slug_collision_should_not_require_pagination_query_var() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3',
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-05 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) );
+
+               $this->assertQueryTrue( 'is_single', 'is_singular' );
+               $this->assertFalse( is_date() );
+       }
+
+       public function test_date_slug_collision_should_be_ignored_when_pagination_var_is_present_but_post_does_not_have_multiple_pages() {
+               global $wp_rewrite;
+               $wp_rewrite->init();
+               $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
+               $wp_rewrite->flush_rules();
+
+               $id = $this->factory->post->create( array(
+                       'post_author'  => $this->author_id,
+                       'post_status'  => 'publish',
+                       'post_content' => 'This post does not have pagination.',
+                       'post_title'   => '02',
+                       'post_date'    => '2015-02-05 01:00:00',
+               ) );
+
+               $this->go_to( get_permalink( $id ) . '5' );
+
+               $this->assertTrue( is_day() );
+       }
+
+       public function filter_unique_post_slug( $slug, $post_id, $post_status, $post_type, $post_parent, $original_slug ) {
+               return $original_slug;
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>