mirror of
https://github.com/WordPress/WordPress.git
synced 2025-01-16 21:31:22 +01:00
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
This commit is contained in:
parent
366e0ada72
commit
3cc1a69391
@ -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' );
|
||||
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user