WordPress.org

Make WordPress Core

Ticket #15397: 15397-20171212.diff

File 15397-20171212.diff, 13.9 KB (added by nickmomrik, 22 months ago)
  • src/wp-includes/default-filters.php

    diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
    index a7c732fc65..c1f6f673e1 100644
    a b add_action( 'template_redirect', 'wp_old_slug_redirect' ); 
    354354add_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 );
    355355add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 );
    356356
     357// Redirect Old Dates
     358add_action( 'post_updated',       'wp_check_for_changed_dates', 12, 3 );
     359add_action( 'attachment_updated', 'wp_check_for_changed_dates', 12, 3 );
     360
    357361// Nonce check for Post Previews
    358362add_action( 'init', '_show_post_preview' );
    359363
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 5ebf42f89d..b2cebd5015 100644
    a b function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { 
    57075707        }
    57085708}
    57095709
     5710/**
     5711 * Check for changed dates for published post objects and save the old date.
     5712 *
     5713 * The function is used when a post object of any type is updated,
     5714 * by comparing the current and previous post objects.
     5715 *
     5716 * If the date was changed and not already part of the old dates then it will be
     5717 * added to the post meta field ('_wp_old_date') for storing old dates for that
     5718 * post.
     5719 *
     5720 * The most logically usage of this function is redirecting changed post objects, so
     5721 * that those that linked to an changed post will be redirected to the new post.
     5722 *
     5723 * @since
     5724 *
     5725 * @param int     $post_id     Post ID.
     5726 * @param WP_Post $post        The Post Object
     5727 * @param WP_Post $post_before The Previous Post Object
     5728 */
     5729function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
     5730        $previous_date = date( 'Y-m-d', strtotime( $post_before->post_date ) );
     5731        $new_date = date( 'Y-m-d', strtotime( $post->post_date ) );
     5732        // Don't bother if it hasn't changed.
     5733        if ( $new_date == $previous_date ) {
     5734                return;
     5735        }
     5736        // We're only concerned with published, non-hierarchical objects.
     5737        if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
     5738                return;
     5739        }
     5740        $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
     5741        // If we haven't added this old date before, add it now.
     5742        if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates ) ) {
     5743                add_post_meta( $post_id, '_wp_old_date', $previous_date );
     5744        }
     5745        // If the new slug was used previously, delete it from the list.
     5746        if ( in_array( $new_date, $old_dates ) ) {
     5747                delete_post_meta( $post_id, '_wp_old_date', $new_date );
     5748        }
     5749}
     5750
    57105751/**
    57115752 * Retrieve the private post SQL based on capability.
    57125753 *
  • src/wp-includes/query.php

    diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php
    index 255758b352..331004e4a9 100644
    a b function the_comment() { 
    845845 * Attempts to find the current slug from the past slugs.
    846846 *
    847847 * @since 2.1.0
    848  *
    849  * @global wpdb $wpdb WordPress database abstraction object.
    850848 */
    851849function wp_old_slug_redirect() {
    852850        if ( is_404() && '' !== get_query_var( 'name' ) ) {
    853                 global $wpdb;
    854 
    855851                // Guess the current post_type based on the query vars.
    856852                if ( get_query_var( 'post_type' ) ) {
    857853                        $post_type = get_query_var( 'post_type' );
    function wp_old_slug_redirect() { 
    875871                        return;
    876872                }
    877873
    878                 $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' ) );
     874                $id = _find_post_by_old_slug( $post_type );
    879875
    880                 // if year, monthnum, or day have been specified, make our query more precise
    881                 // just in case there are multiple identical _wp_old_slug values
    882                 if ( get_query_var( 'year' ) ) {
    883                         $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
    884                 }
    885                 if ( get_query_var( 'monthnum' ) ) {
    886                         $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
    887                 }
    888                 if ( get_query_var( 'day' ) ) {
    889                         $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
     876                if ( ! $id ) {
     877                        $id = _find_post_by_old_date( $post_type );
    890878                }
    891879
    892                 $id = (int) $wpdb->get_var( $query );
     880                /**
     881                 * Filters the old slug redirect post ID.
     882                 *
     883                 * @since
     884                 *
     885                 * @param string $id The redirect post ID.
     886                 */
     887                $id = apply_filters( 'old_slug_redirect_post_id', $id );
    893888
    894889                if ( ! $id ) {
    895890                        return;
    function wp_old_slug_redirect() { 
    921916        }
    922917}
    923918
     919/**
     920 * Find the post ID for redirecting an old slug
     921 *
     922 * @since
     923 *
     924 * @global wpdb $wpdb WordPress database abstraction object.
     925 *
     926 * @param string $post_type The current post type based on the query vars.
     927 * @return int $id The Post ID.
     928 */
     929function _find_post_by_old_slug( $post_type ) {
     930        global $wpdb;
     931
     932        $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' ) );
     933
     934        // if year, monthnum, or day have been specified, make our query more precise
     935        // just in case there are multiple identical _wp_old_slug values
     936        if ( get_query_var( 'year' ) ) {
     937                $query .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) );
     938        }
     939        if ( get_query_var( 'monthnum' ) ) {
     940                $query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
     941        }
     942        if ( get_query_var( 'day' ) ) {
     943                $query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
     944        }
     945
     946        $id = (int) $wpdb->get_var( $query );
     947
     948        return $id;
     949}
     950
     951/**
     952 * Find the post ID for redirecting an old date
     953 *
     954 * @since
     955 *
     956 * @global wpdb $wpdb WordPress database abstraction object.
     957 *
     958 * @param string $post_type The current post type based on the query vars.
     959 * @return int $id The Post ID.
     960 */
     961
     962function _find_post_by_old_date( $post_type ) {
     963        global $wpdb;
     964
     965        $date_query = '';
     966        if ( get_query_var( 'year' ) ) {
     967                $date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) );
     968        }
     969        if ( get_query_var( 'monthnum' ) ) {
     970                $date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) );
     971        }
     972        if ( get_query_var( 'day' ) ) {
     973                $date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) );
     974        }
     975
     976        $id = 0;
     977        if ( $date_query ) {
     978                $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' ) ) );
     979
     980                if ( ! $id ) {
     981                        // Check to see if an old slug matches the old date
     982                        $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' ) ) );
     983                }
     984        }
     985
     986        return $id;
     987}
     988
    924989/**
    925990 * Set up global post data.
    926991 *
  • new file tests/phpunit/tests/rewrite/oldDateRedirect.php

    diff --git a/tests/phpunit/tests/rewrite/oldDateRedirect.php b/tests/phpunit/tests/rewrite/oldDateRedirect.php
    new file mode 100644
    index 0000000000..9bff121044
    - +  
     1<?php
     2
     3/**
     4 * @group rewrite
     5 */
     6class Tests_Rewrite_OldDateRedirect extends WP_UnitTestCase {
     7        protected $old_date_redirect_url;
     8
     9        protected $post_id;
     10
     11        public function setUp() {
     12                parent::setUp();
     13
     14                $this->post_id = self::factory()->post->create( array(
     15                        'post_title' => 'Foo Bar',
     16                        'post_name'  => 'foo-bar',
     17                ) );
     18
     19                add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10, 1 );
     20
     21                $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     22
     23                add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK );
     24                add_rewrite_endpoint( 'second-endpoint', EP_PERMALINK, 'custom' );
     25
     26                flush_rewrite_rules();
     27        }
     28
     29        public function tearDown() {
     30                parent::tearDown();
     31
     32                $this->old_date_redirect_url = null;
     33
     34                remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10 );
     35        }
     36
     37        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 );
     52        }
     53
     54        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 );
     70        }
     71
     72        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                        'post_date'     => $time,
     85                        'post_date_gmt' => get_gmt_from_date( $time ),
     86                ) );
     87
     88                $this->go_to( $old_permalink );
     89                wp_old_slug_redirect();
     90                $this->assertNull( $this->old_date_redirect_url );
     91                $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
     92
     93                $old_permalink = get_attachment_link( $attachment_id );
     94
     95                wp_update_post( array(
     96                        'ID'        => $attachment_id,
     97                        'post_name' => 'the-attachment',
     98                ) );
     99
     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        }
     106
     107        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,
     119                        'post_date'     => $time,
     120                        'post_date_gmt' => get_gmt_from_date( $time ),
     121                        'post_name'     => 'bar-baz',
     122                ) );
     123
     124                $this->go_to( $old_permalink );
     125                wp_old_slug_redirect();
     126                $this->assertNull( $this->old_date_redirect_url );
     127                $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
     128
     129                $old_permalink = get_attachment_link( $attachment_id );
     130
     131                wp_update_post( array(
     132                        'ID'        => $attachment_id,
     133                        'post_name' => 'the-attachment',
     134                ) );
     135
     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 );
     141        }
     142
     143        public function test_old_date_redirect_paged() {
     144                wp_update_post( array(
     145                        'ID'           => $this->post_id,
     146                        'post_content' => 'Test<!--nextpage-->Test',
     147                ) );
     148
     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 );
     163        }
     164
     165        public function test_old_date_slug_redirect_paged() {
     166                wp_update_post( array(
     167                        'ID'           => $this->post_id,
     168                        'post_content' => 'Test<!--nextpage-->Test',
     169                ) );
     170
     171                $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' );
     172
     173                $time = '2004-01-04 12:00:00';
     174                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 );
     186        }
     187
     188        public function test_old_date_slug_doesnt_redirect_when_reused() {
     189                $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) );
     190
     191                $time = '2004-01-04 12:00:00';
     192                wp_update_post( array(
     193                        'ID'            => $this->post_id,
     194                        'post_date'     => $time,
     195                        'post_date_gmt' => get_gmt_from_date( $time ),
     196                        'post_name'     => 'bar-baz',
     197                ) );
     198
     199                $new_post_id = self::factory()->post->create( array(
     200                        'post_title' => 'Foo Bar',
     201                        'post_name'  => 'foo-bar',
     202                ) );
     203
     204                $permalink = user_trailingslashit( get_permalink( $new_post_id ) );
     205
     206                $this->assertEquals( $old_permalink, $permalink );
     207
     208                $this->go_to( $old_permalink );
     209                wp_old_slug_redirect();
     210                $this->assertNull( $this->old_date_redirect_url );
     211        }
     212
     213        public function filter_old_date_redirect_url( $url ) {
     214                $this->old_date_redirect_url = $url;
     215                return false;
     216        }
     217}