<!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>[42401] trunk: Canonical URLs: Redirect to the correct URL when the post date changes.</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/42401">42401</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/42401","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>pento</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-12-15 08:30:50 +0000 (Fri, 15 Dec 2017)</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 URLs: Redirect to the correct URL when the post date changes.
When a post slug is changed, we store a copy of the old slug, so that we can redirect visitors visiting the old URL to the new URL.
In the same way, this stores a copy of the old date, when the post date changes, so we can redirect visitors to the new URL.
Props nickmomrik.
Fixes <a href="https://core.trac.wordpress.org/ticket/15397">#15397</a> for trunk.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunksrcwpincludesqueryphp">trunk/src/wp-includes/query.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsrewriteoldDateRedirectphp">trunk/tests/phpunit/tests/rewrite/oldDateRedirect.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesdefaultfiltersphp"></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/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2017-12-15 05:15:33 UTC (rev 42400)
+++ trunk/src/wp-includes/default-filters.php 2017-12-15 08:30:50 UTC (rev 42401)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -354,6 +354,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Redirect Old Dates
+add_action( 'post_updated', 'wp_check_for_changed_dates', 12, 3 );
+add_action( 'attachment_updated', 'wp_check_for_changed_dates', 12, 3 );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Nonce check for Post Previews
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'init', '_show_post_preview' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></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/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php 2017-12-15 05:15:33 UTC (rev 42400)
+++ trunk/src/wp-includes/post.php 2017-12-15 08:30:50 UTC (rev 42401)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5708,6 +5708,47 @@
</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">+ * Check for changed dates for published post objects and save the old date.
+ *
+ * The function is used when a post object of any type is updated,
+ * by comparing the current and previous post objects.
+ *
+ * If the date was changed and not already part of the old dates then it will be
+ * added to the post meta field ('_wp_old_date') for storing old dates for that
+ * post.
+ *
+ * The most logically usage of this function is redirecting changed post objects, so
+ * that those that linked to an changed post will be redirected to the new post.
+ *
+ * @since 4.9.2
+ *
+ * @param int $post_id Post ID.
+ * @param WP_Post $post The Post Object
+ * @param WP_Post $post_before The Previous Post Object
+ */
+function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
+ $previous_date = date( 'Y-m-d', strtotime( $post_before->post_date ) );
+ $new_date = date( 'Y-m-d', strtotime( $post->post_date ) );
+ // Don't bother if it hasn't changed.
+ if ( $new_date == $previous_date ) {
+ return;
+ }
+ // We're only concerned with published, non-hierarchical objects.
+ if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
+ return;
+ }
+ $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
+ // If we haven't added this old date before, add it now.
+ if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates ) ) {
+ add_post_meta( $post_id, '_wp_old_date', $previous_date );
+ }
+ // If the new slug was used previously, delete it from the list.
+ if ( in_array( $new_date, $old_dates ) ) {
+ delete_post_meta( $post_id, '_wp_old_date', $new_date );
+ }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Retrieve the private post SQL based on capability.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * This function provides a standardized way to appropriately select on the
</span></span></pre></div>
<a id="trunksrcwpincludesqueryphp"></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/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/query.php 2017-12-15 05:15:33 UTC (rev 42400)
+++ trunk/src/wp-includes/query.php 2017-12-15 08:30:50 UTC (rev 42401)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -845,13 +845,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Attempts to find the current slug from the past slugs.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.1.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
- * @global wpdb $wpdb WordPress database abstraction object.
</del><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_old_slug_redirect() {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_404() && '' !== get_query_var( 'name' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wpdb;
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Guess the current post_type based on the query vars.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( get_query_var( 'post_type' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $post_type = get_query_var( 'post_type' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -875,21 +871,20 @@
</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">- $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $id = _find_post_by_old_slug( $post_type );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // if year, monthnum, or day have been specified, make our query more precise
- // just in case there are multiple identical _wp_old_slug values
- if ( get_query_var( 'year' ) ) {
- $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $id ) {
+ $id = _find_post_by_old_date( $post_type );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( get_query_var( 'monthnum' ) ) {
- $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
- }
- if ( get_query_var( 'day' ) ) {
- $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
- }
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = (int) $wpdb->get_var( $query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Filters the old slug redirect post ID.
+ *
+ * @since 4.9.2
+ *
+ * @param string $id The redirect post ID.
+ */
+ $id = apply_filters( 'old_slug_redirect_post_id', $id );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -922,6 +917,82 @@
</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">+ * Find the post ID for redirecting an old slug.
+ *
+ * @see wp_old_slug_redirect()
+ *
+ * @since 4.9.2
+ * @access private
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param string $post_type The current post type based on the query vars.
+ * @return int $id The Post ID.
+ */
+function _find_post_by_old_slug( $post_type ) {
+ global $wpdb;
+
+ $query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );
+
+ // if year, monthnum, or day have been specified, make our query more precise
+ // just in case there are multiple identical _wp_old_slug values
+ if ( get_query_var( 'year' ) ) {
+ $query .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) );
+ }
+ if ( get_query_var( 'monthnum' ) ) {
+ $query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
+ }
+ if ( get_query_var( 'day' ) ) {
+ $query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
+ }
+
+ $id = (int) $wpdb->get_var( $query );
+
+ return $id;
+}
+
+/**
+ * Find the post ID for redirecting an old date.
+ *
+ * @see wp_old_slug_redirect()
+ *
+ * @since 4.9.2
+ * @access private
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param string $post_type The current post type based on the query vars.
+ * @return int $id The Post ID.
+ */
+
+function _find_post_by_old_date( $post_type ) {
+ global $wpdb;
+
+ $date_query = '';
+ if ( get_query_var( 'year' ) ) {
+ $date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) );
+ }
+ if ( get_query_var( 'monthnum' ) ) {
+ $date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) );
+ }
+ if ( get_query_var( 'day' ) ) {
+ $date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) );
+ }
+
+ $id = 0;
+ if ( $date_query ) {
+ $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) );
+
+ if ( ! $id ) {
+ // Check to see if an old slug matches the old date
+ $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
+ }
+ }
+
+ return $id;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Set up global post data.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 1.5.0
</span></span></pre></div>
<a id="trunktestsphpunittestsrewriteoldDateRedirectphp"></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/oldDateRedirect.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rewrite/oldDateRedirect.php (rev 0)
+++ trunk/tests/phpunit/tests/rewrite/oldDateRedirect.php 2017-12-15 08:30:50 UTC (rev 42401)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,217 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group rewrite
+ */
+class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase {
+ protected $old_date_redirect_url;
+
+ protected $post_id;
+
+ public function setUp() {
+ parent::setUp();
+
+ $this->post_id = self::factory()->post->create( array(
+ 'post_title' => 'Foo Bar',
+ 'post_name' => 'foo-bar',
+ ) );
+
+ add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10, 1 );
+
+ $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
+
+ add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK );
+ add_rewrite_endpoint( 'second-endpoint', EP_PERMALINK, 'custom' );
+
+ flush_rewrite_rules();
+ }
+
+ public function tearDown() {
+ parent::tearDown();
+
+ $this->old_date_redirect_url = null;
+
+ remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10 );
+ }
+
+ public function test_old_date_redirect() {
+ $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ ) );
+
+ $permalink = user_trailingslashit( get_permalink( $this->post_id ) );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertEquals( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_redirect() {
+ $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ 'post_name' => 'bar-baz',
+ ) );
+
+ $permalink = user_trailingslashit( get_permalink( $this->post_id ) );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertEquals( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_redirect_attachment() {
+ $file = DIR_TESTDATA . '/images/canola.jpg';
+ $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array(
+ 'post_mime_type' => 'image/jpeg',
+ 'post_name' => 'my-attachment',
+ ) );
+
+ $old_permalink = get_attachment_link( $attachment_id );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ ) );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertNull( $this->old_date_redirect_url );
+ $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
+
+ $old_permalink = get_attachment_link( $attachment_id );
+
+ wp_update_post( array(
+ 'ID' => $attachment_id,
+ 'post_name' => 'the-attachment',
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertEquals( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_redirect_attachment() {
+ $file = DIR_TESTDATA . '/images/canola.jpg';
+ $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array(
+ 'post_mime_type' => 'image/jpeg',
+ 'post_name' => 'my-attachment',
+ ) );
+
+ $old_permalink = get_attachment_link( $attachment_id );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ 'post_name' => 'bar-baz',
+ ) );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertNull( $this->old_date_redirect_url );
+ $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
+
+ $old_permalink = get_attachment_link( $attachment_id );
+
+ wp_update_post( array(
+ 'ID' => $attachment_id,
+ 'post_name' => 'the-attachment',
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertEquals( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_redirect_paged() {
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_content' => 'Test<!--nextpage-->Test',
+ ) );
+
+ $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertEquals( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_redirect_paged() {
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_content' => 'Test<!--nextpage-->Test',
+ ) );
+
+ $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
+
+ $time = '2004-01-04 12:00:00';
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ 'post_name' => 'bar-baz',
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertEquals( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_doesnt_redirect_when_reused() {
+ $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
+
+ $time = '2004-01-04 12:00:00';
+ wp_update_post( array(
+ 'ID' => $this->post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ 'post_name' => 'bar-baz',
+ ) );
+
+ $new_post_id = self::factory()->post->create( array(
+ 'post_title' => 'Foo Bar',
+ 'post_name' => 'foo-bar',
+ ) );
+
+ $permalink = user_trailingslashit( get_permalink( $new_post_id ) );
+
+ $this->assertEquals( $old_permalink, $permalink );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertNull( $this->old_date_redirect_url );
+ }
+
+ public function filter_old_date_redirect_url( $url ) {
+ $this->old_date_redirect_url = $url;
+ return false;
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>