Make WordPress Core

Changeset 42589


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:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/default-filters.php

    r42445 r42589  
    343343add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 );
    344344
     345// Redirect Old Dates
     346add_action( 'post_updated',       'wp_check_for_changed_dates', 12, 3 );
     347add_action( 'attachment_updated', 'wp_check_for_changed_dates', 12, 3 );
     348
    345349// Nonce check for Post Previews
    346350add_action( 'init', '_show_post_preview' );
  • 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 *
  • branches/4.9/src/wp-includes/query.php

    r41168 r42589  
    844844 *
    845845 * @since 2.1.0
    846  *
    847  * @global wpdb $wpdb WordPress database abstraction object.
    848846 */
    849847function wp_old_slug_redirect() {
    850848    if ( is_404() && '' !== get_query_var( 'name' ) ) {
    851         global $wpdb;
    852 
    853849        // Guess the current post_type based on the query vars.
    854850        if ( get_query_var( 'post_type' ) ) {
     
    874870        }
    875871
    876         $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' ) );
    877 
    878         // if year, monthnum, or day have been specified, make our query more precise
    879         // just in case there are multiple identical _wp_old_slug values
    880         if ( get_query_var( 'year' ) ) {
    881             $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) );
     872        $id = _find_post_by_old_slug( $post_type );
     873
     874        if ( ! $id ) {
     875            $id = _find_post_by_old_date( $post_type );
    882876        }
    883         if ( get_query_var( 'monthnum' ) ) {
    884             $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
    885         }
    886         if ( get_query_var( 'day' ) ) {
    887             $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
    888         }
    889 
    890         $id = (int) $wpdb->get_var( $query );
     877
     878        /**
     879         * Filters the old slug redirect post ID.
     880         *
     881         * @since 4.9.3
     882         *
     883         * @param string $id The redirect post ID.
     884         */
     885        $id = apply_filters( 'old_slug_redirect_post_id', $id );
    891886
    892887        if ( ! $id ) {
     
    921916
    922917/**
     918 * Find the post ID for redirecting an old slug.
     919 *
     920 * @see wp_old_slug_redirect()
     921 *
     922 * @since 4.9.3
     923 * @access private
     924 *
     925 * @global wpdb $wpdb WordPress database abstraction object.
     926 *
     927 * @param string $post_type The current post type based on the query vars.
     928 * @return int $id The Post ID.
     929 */
     930function _find_post_by_old_slug( $post_type ) {
     931    global $wpdb;
     932
     933    $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' ) );
     934
     935    // if year, monthnum, or day have been specified, make our query more precise
     936    // just in case there are multiple identical _wp_old_slug values
     937    if ( get_query_var( 'year' ) ) {
     938        $query .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) );
     939    }
     940    if ( get_query_var( 'monthnum' ) ) {
     941        $query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
     942    }
     943    if ( get_query_var( 'day' ) ) {
     944        $query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
     945    }
     946
     947    $id = (int) $wpdb->get_var( $query );
     948
     949    return $id;
     950}
     951
     952/**
     953 * Find the post ID for redirecting an old date.
     954 *
     955 * @see wp_old_slug_redirect()
     956 *
     957 * @since 4.9.3
     958 * @access private
     959 *
     960 * @global wpdb $wpdb WordPress database abstraction object.
     961 *
     962 * @param string $post_type The current post type based on the query vars.
     963 * @return int $id The Post ID.
     964 */
     965
     966function _find_post_by_old_date( $post_type ) {
     967    global $wpdb;
     968
     969    $date_query = '';
     970    if ( get_query_var( 'year' ) ) {
     971        $date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) );
     972    }
     973    if ( get_query_var( 'monthnum' ) ) {
     974        $date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) );
     975    }
     976    if ( get_query_var( 'day' ) ) {
     977        $date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) );
     978    }
     979
     980    $id = 0;
     981    if ( $date_query ) {
     982        $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' ) ) );
     983
     984        if ( ! $id ) {
     985            // Check to see if an old slug matches the old date
     986            $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' ) ) );
     987        }
     988    }
     989
     990    return $id;
     991}
     992
     993/**
    923994 * Set up global post data.
    924995 *
  • branches/4.9/tests/phpunit/tests/rewrite/oldDateRedirect.php

    r42401 r42589  
    77    protected $old_date_redirect_url;
    88
    9     protected $post_id;
     9    public static $post_id;
     10
     11    public static $attachment_id;
     12
     13    public static function wpSetUpBeforeClass() {
     14        self::$post_id = self::factory()->post->create( array(
     15            'post_title' => 'Foo Bar',
     16            'post_name'  => 'foo-bar',
     17        ) );
     18
     19        self::$attachment_id = self::factory()->attachment->create_object(
     20            array(
     21                'file'           => DIR_TESTDATA . '/images/canola.jpg',
     22                'post_mime_type' => 'image/jpeg',
     23                'post_name'      => 'my-attachment',
     24                'post_parent'    => self::$post_id,
     25            )
     26        );
     27    }
    1028
    1129    public function setUp() {
    1230        parent::setUp();
    1331
    14         $this->post_id = self::factory()->post->create( array(
    15             'post_title' => 'Foo Bar',
    16             'post_name'  => 'foo-bar',
    17         ) );
    18 
    1932        add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10, 1 );
    2033
     
    3144
    3245        $this->old_date_redirect_url = null;
    33 
    34         remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10 );
    3546    }
    3647
    3748    public function test_old_date_redirect() {
    38         $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
    39 
    40         $time = '2004-01-03 00:00:00';
    41         wp_update_post( array(
    42             'ID'            => $this->post_id,
    43             'post_date'     => $time,
    44             'post_date_gmt' => get_gmt_from_date( $time ),
    45         ) );
    46 
    47         $permalink = user_trailingslashit( get_permalink( $this->post_id ) );
    48 
    49         $this->go_to( $old_permalink );
    50         wp_old_slug_redirect();
    51         $this->assertEquals( $permalink, $this->old_date_redirect_url );
     49        $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
     50
     51        $time = '2004-01-03 00:00:00';
     52        wp_update_post( array(
     53            'ID'            => self::$post_id,
     54            'post_date'     => $time,
     55            'post_date_gmt' => get_gmt_from_date( $time ),
     56        ) );
     57
     58        $permalink = user_trailingslashit( get_permalink( self::$post_id ) );
     59
     60        $this->go_to( $old_permalink );
     61        wp_old_slug_redirect();
     62        $this->assertSame( $permalink, $this->old_date_redirect_url );
    5263    }
    5364
    5465    public function test_old_date_slug_redirect() {
    55         $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
    56 
    57         $time = '2004-01-03 00:00:00';
    58         wp_update_post( array(
    59             'ID'            => $this->post_id,
    60             'post_date'     => $time,
    61             'post_date_gmt' => get_gmt_from_date( $time ),
    62             'post_name'     => 'bar-baz',
    63         ) );
    64 
    65         $permalink = user_trailingslashit( get_permalink( $this->post_id ) );
    66 
    67         $this->go_to( $old_permalink );
    68         wp_old_slug_redirect();
    69         $this->assertEquals( $permalink, $this->old_date_redirect_url );
     66        $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
     67
     68        $time = '2004-01-03 00:00:00';
     69        wp_update_post( array(
     70            'ID'            => self::$post_id,
     71            'post_date'     => $time,
     72            'post_date_gmt' => get_gmt_from_date( $time ),
     73            'post_name'     => 'bar-baz',
     74        ) );
     75
     76        $permalink = user_trailingslashit( get_permalink( self::$post_id ) );
     77
     78        $this->go_to( $old_permalink );
     79        wp_old_slug_redirect();
     80        $this->assertSame( $permalink, $this->old_date_redirect_url );
    7081    }
    7182
    7283    public function test_old_date_redirect_attachment() {
    73         $file          = DIR_TESTDATA . '/images/canola.jpg';
    74         $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array(
    75             'post_mime_type' => 'image/jpeg',
    76             'post_name'      => 'my-attachment',
    77         ) );
    78 
    79         $old_permalink = get_attachment_link( $attachment_id );
    80 
    81         $time = '2004-01-03 00:00:00';
    82         wp_update_post( array(
    83             'ID'            => $this->post_id,
     84        $old_permalink = get_attachment_link( self::$attachment_id );
     85
     86        $time = '2004-01-03 00:00:00';
     87        wp_update_post( array(
     88            'ID'            => self::$post_id,
    8489            'post_date'     => $time,
    8590            'post_date_gmt' => get_gmt_from_date( $time ),
     
    9196        $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
    9297
    93         $old_permalink = get_attachment_link( $attachment_id );
    94 
    95         wp_update_post( array(
    96             'ID'        => $attachment_id,
     98        $old_permalink = get_attachment_link( self::$attachment_id );
     99
     100        wp_update_post( array(
     101            'ID'        => self::$attachment_id,
    97102            'post_name' => 'the-attachment',
    98103        ) );
    99104
    100         $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' );
    101 
    102         $this->go_to( $old_permalink );
    103         wp_old_slug_redirect();
    104         $this->assertEquals( $permalink, $this->old_date_redirect_url );
     105        $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'the-attachment' );
     106
     107        $this->go_to( $old_permalink );
     108        wp_old_slug_redirect();
     109        $this->assertSame( $permalink, $this->old_date_redirect_url );
    105110    }
    106111
    107112    public function test_old_date_slug_redirect_attachment() {
    108         $file          = DIR_TESTDATA . '/images/canola.jpg';
    109         $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array(
    110             'post_mime_type' => 'image/jpeg',
    111             'post_name'      => 'my-attachment',
    112         ) );
    113 
    114         $old_permalink = get_attachment_link( $attachment_id );
    115 
    116         $time = '2004-01-03 00:00:00';
    117         wp_update_post( array(
    118             'ID'            => $this->post_id,
     113        $old_permalink = get_attachment_link( self::$attachment_id );
     114
     115        $time = '2004-01-03 00:00:00';
     116        wp_update_post( array(
     117            'ID'            => self::$post_id,
    119118            'post_date'     => $time,
    120119            'post_date_gmt' => get_gmt_from_date( $time ),
     
    127126        $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
    128127
    129         $old_permalink = get_attachment_link( $attachment_id );
    130 
    131         wp_update_post( array(
    132             'ID'        => $attachment_id,
     128        $old_permalink = get_attachment_link( self::$attachment_id );
     129
     130        wp_update_post( array(
     131            'ID'        => self::$attachment_id,
    133132            'post_name' => 'the-attachment',
    134133        ) );
    135134
    136         $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' );
    137 
    138         $this->go_to( $old_permalink );
    139         wp_old_slug_redirect();
    140         $this->assertEquals( $permalink, $this->old_date_redirect_url );
     135        $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'the-attachment' );
     136
     137        $this->go_to( $old_permalink );
     138        wp_old_slug_redirect();
     139        $this->assertSame( $permalink, $this->old_date_redirect_url );
    141140    }
    142141
    143142    public function test_old_date_redirect_paged() {
    144143        wp_update_post( array(
    145             'ID'           => $this->post_id,
     144            'ID'           => self::$post_id,
    146145            'post_content' => 'Test<!--nextpage-->Test',
    147146        ) );
    148147
    149         $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
    150 
    151         $time = '2004-01-03 00:00:00';
    152         wp_update_post( array(
    153             'ID'            => $this->post_id,
    154             'post_date'     => $time,
    155             'post_date_gmt' => get_gmt_from_date( $time ),
    156         ) );
    157 
    158         $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
    159 
    160         $this->go_to( $old_permalink );
    161         wp_old_slug_redirect();
    162         $this->assertEquals( $permalink, $this->old_date_redirect_url );
     148        $old_permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
     149
     150        $time = '2004-01-03 00:00:00';
     151        wp_update_post( array(
     152            'ID'            => self::$post_id,
     153            'post_date'     => $time,
     154            'post_date_gmt' => get_gmt_from_date( $time ),
     155        ) );
     156
     157        $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
     158
     159        $this->go_to( $old_permalink );
     160        wp_old_slug_redirect();
     161        $this->assertSame( $permalink, $this->old_date_redirect_url );
    163162    }
    164163
    165164    public function test_old_date_slug_redirect_paged() {
    166165        wp_update_post( array(
    167             'ID'           => $this->post_id,
     166            'ID'           => self::$post_id,
    168167            'post_content' => 'Test<!--nextpage-->Test',
    169168        ) );
    170169
    171         $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
     170        $old_permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
    172171
    173172        $time = '2004-01-04 12:00:00';
    174173        wp_update_post( array(
    175             'ID'            => $this->post_id,
    176             'post_date'     => $time,
    177             'post_date_gmt' => get_gmt_from_date( $time ),
    178             'post_name'     => 'bar-baz',
    179         ) );
    180 
    181         $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
    182 
    183         $this->go_to( $old_permalink );
    184         wp_old_slug_redirect();
    185         $this->assertEquals( $permalink, $this->old_date_redirect_url );
     174            'ID'            => self::$post_id,
     175            'post_date'     => $time,
     176            'post_date_gmt' => get_gmt_from_date( $time ),
     177            'post_name'     => 'bar-baz',
     178        ) );
     179
     180        $permalink = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . 'page/2' );
     181
     182        $this->go_to( $old_permalink );
     183        wp_old_slug_redirect();
     184        $this->assertSame( $permalink, $this->old_date_redirect_url );
    186185    }
    187186
    188187    public function test_old_date_slug_doesnt_redirect_when_reused() {
    189         $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
     188        $old_permalink = user_trailingslashit( get_permalink( self::$post_id ) );
    190189
    191190        $time = '2004-01-04 12:00:00';
    192191        wp_update_post( array(
    193             'ID'            => $this->post_id,
     192            'ID'            => self::$post_id,
    194193            'post_date'     => $time,
    195194            'post_date_gmt' => get_gmt_from_date( $time ),
     
    204203        $permalink = user_trailingslashit( get_permalink( $new_post_id ) );
    205204
    206         $this->assertEquals( $old_permalink, $permalink );
     205        $this->assertSame( $old_permalink, $permalink );
    207206
    208207        $this->go_to( $old_permalink );
Note: See TracChangeset for help on using the changeset viewer.