<!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>[42589] branches/4.9: 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/42589">42589</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/42589","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>2018-01-24 07:11:40 +0000 (Wed, 24 Jan 2018)</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.
Merge of [42401,42587,42588] to the 4.9 branch.
Props nickmomrik, frank-klein.
Fixes <a href="https://core.trac.wordpress.org/ticket/15397">#15397</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches49srcwpincludesdefaultfiltersphp">branches/4.9/src/wp-includes/default-filters.php</a></li>
<li><a href="#branches49srcwpincludespostphp">branches/4.9/src/wp-includes/post.php</a></li>
<li><a href="#branches49srcwpincludesqueryphp">branches/4.9/src/wp-includes/query.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#branches49testsphpunittestsrewriteoldDateRedirectphp">branches/4.9/tests/phpunit/tests/rewrite/oldDateRedirect.php</a></li>
</ul>
<h3>Property Changed</h3>
<ul>
<li><a href="#branches49">branches/4.9/</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: branches/4.9
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- branches/4.9 2018-01-24 06:47:04 UTC (rev 42588)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ branches/4.9 2018-01-24 07:11:40 UTC (rev 42589)
</ins><a id="branches49"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: branches/4.9</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.1:18031
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.3:20543
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:18512,42132,42134,42136,42138,42140,42144,42146,42148,42150,42152-42153,42155,42157,42159,42161,42163,42169,42171,42173,42175,42177,42181,42183,42185,42187,42189,42191,42193,42199,42203,42210,42214,42220,42222,42226,42242,42244,42247,42251,42258-42261,42350,42362,42364-42368,42374,42388,42390,42417,42421,42423-42425,42430,42433,42437,42441,42443,42446-42447,42449,42451,42453,42457,42459,42462,42491,42521,42529,42531,42533,42536,42538,42541,42543,42545,42549,42566,42568,42570,42572,42574,42576,42579,42582,42584
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:18512,42132,42134,42136,42138,42140,42144,42146,42148,42150,42152-42153,42155,42157,42159,42161,42163,42169,42171,42173,42175,42177,42181,42183,42185,42187,42189,42191,42193,42199,42203,42210,42214,42220,42222,42226,42242,42244,42247,42251,42258-42261,42350,42362,42364-42368,42374,42388,42390,42401,42417,42421,42423-42425,42430,42433,42437,42441,42443,42446-42447,42449,42451,42453,42457,42459,42462,42491,42521,42529,42531,42533,42536,42538,42541,42543,42545,42549,42566,42568,42570,42572,42574,42576,42579,42582,42584,42587-42588
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches49srcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.9/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.9/src/wp-includes/default-filters.php 2018-01-24 06:47:04 UTC (rev 42588)
+++ branches/4.9/src/wp-includes/default-filters.php 2018-01-24 07:11:40 UTC (rev 42589)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -342,6 +342,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="branches49srcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.9/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.9/src/wp-includes/post.php 2018-01-24 06:47:04 UTC (rev 42588)
+++ branches/4.9/src/wp-includes/post.php 2018-01-24 07:11:40 UTC (rev 42589)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5495,6 +5495,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.3
+ *
+ * @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="branches49srcwpincludesqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.9/src/wp-includes/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.9/src/wp-includes/query.php 2018-01-24 06:47:04 UTC (rev 42588)
+++ branches/4.9/src/wp-includes/query.php 2018-01-24 07:11:40 UTC (rev 42589)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -843,13 +843,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">@@ -873,21 +869,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.3
+ *
+ * @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">@@ -920,6 +915,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.3
+ * @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.3
+ * @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="branches49testsphpunittestsrewriteoldDateRedirectphpfromrev42401trunktestsphpunittestsrewriteoldDateRedirectphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: branches/4.9/tests/phpunit/tests/rewrite/oldDateRedirect.php (from rev 42401, trunk/tests/phpunit/tests/rewrite/oldDateRedirect.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.9/tests/phpunit/tests/rewrite/oldDateRedirect.php (rev 0)
+++ branches/4.9/tests/phpunit/tests/rewrite/oldDateRedirect.php 2018-01-24 07:11:40 UTC (rev 42589)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,216 @@
</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;
+
+ public static $post_id;
+
+ public static $attachment_id;
+
+ public static function wpSetUpBeforeClass() {
+ self::$post_id = self::factory()->post->create( array(
+ 'post_title' => 'Foo Bar',
+ 'post_name' => 'foo-bar',
+ ) );
+
+ self::$attachment_id = self::factory()->attachment->create_object(
+ array(
+ 'file' => DIR_TESTDATA . '/images/canola.jpg',
+ 'post_mime_type' => 'image/jpeg',
+ 'post_name' => 'my-attachment',
+ 'post_parent' => self::$post_id,
+ )
+ );
+ }
+
+ public function setUp() {
+ parent::setUp();
+
+ 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;
+ }
+
+ public function test_old_date_redirect() {
+ $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => self::$post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ ) );
+
+ $permalink = user_trailingslashit( get_permalink( self::$post_id ) );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertSame( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_redirect() {
+ $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => self::$post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ 'post_name' => 'bar-baz',
+ ) );
+
+ $permalink = user_trailingslashit( get_permalink( self::$post_id ) );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertSame( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_redirect_attachment() {
+ $old_permalink = get_attachment_link( self::$attachment_id );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => self::$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( self::$attachment_id );
+
+ wp_update_post( array(
+ 'ID' => self::$attachment_id,
+ 'post_name' => 'the-attachment',
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'the-attachment' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertSame( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_redirect_attachment() {
+ $old_permalink = get_attachment_link( self::$attachment_id );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => self::$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( self::$attachment_id );
+
+ wp_update_post( array(
+ 'ID' => self::$attachment_id,
+ 'post_name' => 'the-attachment',
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'the-attachment' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertSame( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_redirect_paged() {
+ wp_update_post( array(
+ 'ID' => self::$post_id,
+ 'post_content' => 'Test<!--nextpage-->Test',
+ ) );
+
+ $old_permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
+
+ $time = '2004-01-03 00:00:00';
+ wp_update_post( array(
+ 'ID' => self::$post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertSame( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_redirect_paged() {
+ wp_update_post( array(
+ 'ID' => self::$post_id,
+ 'post_content' => 'Test<!--nextpage-->Test',
+ ) );
+
+ $old_permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
+
+ $time = '2004-01-04 12:00:00';
+ wp_update_post( array(
+ 'ID' => self::$post_id,
+ 'post_date' => $time,
+ 'post_date_gmt' => get_gmt_from_date( $time ),
+ 'post_name' => 'bar-baz',
+ ) );
+
+ $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
+
+ $this->go_to( $old_permalink );
+ wp_old_slug_redirect();
+ $this->assertSame( $permalink, $this->old_date_redirect_url );
+ }
+
+ public function test_old_date_slug_doesnt_redirect_when_reused() {
+ $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
+
+ $time = '2004-01-04 12:00:00';
+ wp_update_post( array(
+ 'ID' => self::$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->assertSame( $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>