From 6da54f0d9be5e2cf8659427a39f1f23b0465ce72 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 15 Dec 2017 08:31:47 +0000 Subject: [PATCH] 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 --- wp-includes/default-filters.php | 4 ++ wp-includes/post.php | 41 +++++++++++++ wp-includes/query.php | 103 +++++++++++++++++++++++++++----- wp-includes/version.php | 2 +- 4 files changed, 133 insertions(+), 17 deletions(-) diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index a7c732fc65..c1f6f673e1 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -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' ); diff --git a/wp-includes/post.php b/wp-includes/post.php index 779b7a3e28..1796ddeded 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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. * diff --git a/wp-includes/query.php b/wp-includes/query.php index 255758b352..b757ae708f 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -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. * diff --git a/wp-includes/version.php b/wp-includes/version.php index aa8fcfcc41..838b31fc9f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.