From afc55a72de6f78a0f6813f67abc0991cbebd7cf6 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 1 Apr 2022 03:23:07 +0000 Subject: [PATCH] Canonical: Include all public status in 404 redirects. In `redirect_guess_404_permalink()` search for posts using all publicly queryable statuses rather than limiting options to the `publish` status. Props goaroundagain, costdev, htdat, audrasjb, chaion07. Fixes #47911. git-svn-id: https://develop.svn.wordpress.org/trunk@53043 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/canonical.php | 3 +- tests/phpunit/tests/canonical.php | 61 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 0defa8227d..9404ed6ea5 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -957,8 +957,9 @@ function redirect_guess_404_permalink() { $where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); } + $publicly_viewable_statuses = array_filter( get_post_stati(), 'is_post_status_viewable' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared - $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" ); + $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')" ); if ( ! $post_id ) { return false; diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php index 93eed462fc..63e5c4078b 100644 --- a/tests/phpunit/tests/canonical.php +++ b/tests/phpunit/tests/canonical.php @@ -275,6 +275,67 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase { $this->assertFalse( redirect_guess_404_permalink() ); } + /** + * Ensure public posts with custom public statuses are guessed. + * + * @ticket 47911 + * @dataProvider data_redirect_guess_404_permalink_with_custom_statuses + * + * @covers ::redirect_guess_404_permalink + */ + public function test_redirect_guess_404_permalink_with_custom_statuses( $status_args, $redirects ) { + register_post_status( 'custom', $status_args ); + + $post = self::factory()->post->create( + array( + 'post_title' => 'custom-status-public-guess-404-permalink', + 'post_status' => 'custom', + ) + ); + + $this->go_to( 'custom-status-public-guess-404-permalink' ); + + $expected = $redirects ? get_permalink( $post ) : false; + + $this->assertSame( $expected, redirect_guess_404_permalink() ); + } + + /** + * Data provider for test_redirect_guess_404_permalink_with_custom_statuses(). + * + * return array[] { + * array Arguments used to register custom status + * bool Whether the 404 link is expected to redirect + * } + */ + public function data_redirect_guess_404_permalink_with_custom_statuses() { + return array( + 'public status' => array( + 'status_args' => array( 'public' => true ), + 'redirects' => true, + ), + 'private status' => array( + 'status_args' => array( 'public' => false ), + 'redirects' => false, + ), + 'internal status' => array( + 'status_args' => array( 'internal' => true ), + 'redirects' => false, + ), + 'protected status' => array( + 'status_args' => array( 'protected' => true ), + 'redirects' => false, + ), + 'protected status flagged as public' => array( + 'status_args' => array( + 'protected' => true, + 'public' => true, + ), + 'redirects' => false, + ), + ); + } + /** * Ensure multiple post types do not throw a notice. *