diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index a7c732fc65..c1f6f673e1 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -354,6 +354,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/src/wp-includes/post.php b/src/wp-includes/post.php index 779b7a3e28..1796ddeded 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -5707,6 +5707,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.2 + * + * @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/src/wp-includes/query.php b/src/wp-includes/query.php index 255758b352..b757ae708f 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -845,13 +845,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' ); @@ -875,21 +871,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.2 + * + * @param string $id The redirect post ID. + */ + $id = apply_filters( 'old_slug_redirect_post_id', $id ); if ( ! $id ) { return; @@ -921,6 +916,82 @@ function wp_old_slug_redirect() { } } +/** + * Find the post ID for redirecting an old slug. + * + * @see wp_old_slug_redirect() + * + * @since 4.9.2 + * @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.2 + * @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/tests/phpunit/tests/rewrite/oldDateRedirect.php b/tests/phpunit/tests/rewrite/oldDateRedirect.php new file mode 100644 index 0000000000..9bff121044 --- /dev/null +++ b/tests/phpunit/tests/rewrite/oldDateRedirect.php @@ -0,0 +1,217 @@ +post_id = self::factory()->post->create( array( + 'post_title' => 'Foo Bar', + 'post_name' => 'foo-bar', + ) ); + + add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10, 1 ); + + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK ); + add_rewrite_endpoint( 'second-endpoint', EP_PERMALINK, 'custom' ); + + flush_rewrite_rules(); + } + + public function tearDown() { + parent::tearDown(); + + $this->old_date_redirect_url = null; + + remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_date_redirect_url' ), 10 ); + } + + public function test_old_date_redirect() { + $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + $time = '2004-01-03 00:00:00'; + wp_update_post( array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + ) ); + + $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertEquals( $permalink, $this->old_date_redirect_url ); + } + + public function test_old_date_slug_redirect() { + $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + $time = '2004-01-03 00:00:00'; + wp_update_post( array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + 'post_name' => 'bar-baz', + ) ); + + $permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertEquals( $permalink, $this->old_date_redirect_url ); + } + + public function test_old_date_redirect_attachment() { + $file = DIR_TESTDATA . '/images/canola.jpg'; + $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array( + 'post_mime_type' => 'image/jpeg', + 'post_name' => 'my-attachment', + ) ); + + $old_permalink = get_attachment_link( $attachment_id ); + + $time = '2004-01-03 00:00:00'; + wp_update_post( array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + ) ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertNull( $this->old_date_redirect_url ); + $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' ); + + $old_permalink = get_attachment_link( $attachment_id ); + + wp_update_post( array( + 'ID' => $attachment_id, + 'post_name' => 'the-attachment', + ) ); + + $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertEquals( $permalink, $this->old_date_redirect_url ); + } + + public function test_old_date_slug_redirect_attachment() { + $file = DIR_TESTDATA . '/images/canola.jpg'; + $attachment_id = self::factory()->attachment->create_object( $file, $this->post_id, array( + 'post_mime_type' => 'image/jpeg', + 'post_name' => 'my-attachment', + ) ); + + $old_permalink = get_attachment_link( $attachment_id ); + + $time = '2004-01-03 00:00:00'; + wp_update_post( array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + 'post_name' => 'bar-baz', + ) ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertNull( $this->old_date_redirect_url ); + $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' ); + + $old_permalink = get_attachment_link( $attachment_id ); + + wp_update_post( array( + 'ID' => $attachment_id, + 'post_name' => 'the-attachment', + ) ); + + $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertEquals( $permalink, $this->old_date_redirect_url ); + } + + public function test_old_date_redirect_paged() { + wp_update_post( array( + 'ID' => $this->post_id, + 'post_content' => 'TestTest', + ) ); + + $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); + + $time = '2004-01-03 00:00:00'; + wp_update_post( array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + ) ); + + $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertEquals( $permalink, $this->old_date_redirect_url ); + } + + public function test_old_date_slug_redirect_paged() { + wp_update_post( array( + 'ID' => $this->post_id, + 'post_content' => 'TestTest', + ) ); + + $old_permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); + + $time = '2004-01-04 12:00:00'; + wp_update_post( array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + 'post_name' => 'bar-baz', + ) ); + + $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'page/2' ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertEquals( $permalink, $this->old_date_redirect_url ); + } + + public function test_old_date_slug_doesnt_redirect_when_reused() { + $old_permalink = user_trailingslashit( get_permalink( $this->post_id ) ); + + $time = '2004-01-04 12:00:00'; + wp_update_post( array( + 'ID' => $this->post_id, + 'post_date' => $time, + 'post_date_gmt' => get_gmt_from_date( $time ), + 'post_name' => 'bar-baz', + ) ); + + $new_post_id = self::factory()->post->create( array( + 'post_title' => 'Foo Bar', + 'post_name' => 'foo-bar', + ) ); + + $permalink = user_trailingslashit( get_permalink( $new_post_id ) ); + + $this->assertEquals( $old_permalink, $permalink ); + + $this->go_to( $old_permalink ); + wp_old_slug_redirect(); + $this->assertNull( $this->old_date_redirect_url ); + } + + public function filter_old_date_redirect_url( $url ) { + $this->old_date_redirect_url = $url; + return false; + } +}