From 3cc1a69391d0f3a73697f5b835eed7b11c6c1d81 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 24 Jan 2018 07:12:39 +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. Merge of [42401,42587,42588] to the 4.9 branch. Props nickmomrik, frank-klein. Fixes #15397. Built from https://develop.svn.wordpress.org/branches/4.9@42589 git-svn-id: http://core.svn.wordpress.org/branches/4.9@42418 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 d64363cf32..d28c07b12b 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -342,6 +342,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 1dbf0c9867..fa9b0f3185 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -5494,6 +5494,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.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 ); + } +} + /** * Retrieve the private post SQL based on capability. * diff --git a/wp-includes/query.php b/wp-includes/query.php index 0ed1fab575..168e548da8 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -843,13 +843,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' ); @@ -873,21 +869,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.3 + * + * @param string $id The redirect post ID. + */ + $id = apply_filters( 'old_slug_redirect_post_id', $id ); if ( ! $id ) { return; @@ -919,6 +914,82 @@ function wp_old_slug_redirect() { } } +/** + * 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; +} + /** * Set up global post data. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 4e6c3a8f23..95fd6008e4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.9.3-alpha-42586'; +$wp_version = '4.9.3-alpha-42589'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.