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 #15397 for trunk.


Built from https://develop.svn.wordpress.org/trunk@42401


git-svn-id: http://core.svn.wordpress.org/trunk@42230 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2017-12-15 08:31:47 +00:00
parent 7c77266b9b
commit 6da54f0d9b
4 changed files with 133 additions and 17 deletions

View File

@ -354,6 +354,10 @@ add_action( 'template_redirect', 'wp_old_slug_redirect' );
add_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 );
add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 );
// 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 );
// Nonce check for Post Previews
add_action( 'init', '_show_post_preview' );

View File

@ -5707,6 +5707,47 @@ function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
}
}
/**
* 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 );
}
}
/**
* Retrieve the private post SQL based on capability.
*

View File

@ -845,13 +845,9 @@ function the_comment() {
* Attempts to find the current slug from the past slugs.
*
* @since 2.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
function wp_old_slug_redirect() {
if ( is_404() && '' !== get_query_var( 'name' ) ) {
global $wpdb;
// Guess the current post_type based on the query vars.
if ( get_query_var( 'post_type' ) ) {
$post_type = get_query_var( 'post_type' );
@ -875,21 +871,20 @@ function wp_old_slug_redirect() {
return;
}
$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' ) );
$id = _find_post_by_old_slug( $post_type );
// 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' ) );
if ( ! $id ) {
$id = _find_post_by_old_date( $post_type );
}
$id = (int) $wpdb->get_var( $query );
/**
* 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 );
if ( ! $id ) {
return;
@ -921,6 +916,82 @@ function wp_old_slug_redirect() {
}
}
/**
* 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;
}
/**
* Set up global post data.
*

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-alpha-42400';
$wp_version = '5.0-alpha-42401';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.