Make WordPress Core


Ignore:
Timestamp:
01/24/2018 07:11:40 AM (7 years ago)
Author:
pento
Message:

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.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/post.php

    r42056 r42589  
    54965496
    54975497/**
     5498 * Check for changed dates for published post objects and save the old date.
     5499 *
     5500 * The function is used when a post object of any type is updated,
     5501 * by comparing the current and previous post objects.
     5502 *
     5503 * If the date was changed and not already part of the old dates then it will be
     5504 * added to the post meta field ('_wp_old_date') for storing old dates for that
     5505 * post.
     5506 *
     5507 * The most logically usage of this function is redirecting changed post objects, so
     5508 * that those that linked to an changed post will be redirected to the new post.
     5509 *
     5510 * @since 4.9.3
     5511 *
     5512 * @param int     $post_id     Post ID.
     5513 * @param WP_Post $post        The Post Object
     5514 * @param WP_Post $post_before The Previous Post Object
     5515 */
     5516function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
     5517    $previous_date = date( 'Y-m-d', strtotime( $post_before->post_date ) );
     5518    $new_date = date( 'Y-m-d', strtotime( $post->post_date ) );
     5519    // Don't bother if it hasn't changed.
     5520    if ( $new_date == $previous_date ) {
     5521        return;
     5522    }
     5523    // We're only concerned with published, non-hierarchical objects.
     5524    if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
     5525        return;
     5526    }
     5527    $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
     5528    // If we haven't added this old date before, add it now.
     5529    if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates ) ) {
     5530        add_post_meta( $post_id, '_wp_old_date', $previous_date );
     5531    }
     5532    // If the new slug was used previously, delete it from the list.
     5533    if ( in_array( $new_date, $old_dates ) ) {
     5534        delete_post_meta( $post_id, '_wp_old_date', $new_date );
     5535    }
     5536}
     5537
     5538/**
    54985539 * Retrieve the private post SQL based on capability.
    54995540 *
Note: See TracChangeset for help on using the changeset viewer.