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
This commit is contained in:
Peter Wilson
2022-04-01 03:23:07 +00:00
parent b1b305b826
commit afc55a72de
2 changed files with 63 additions and 1 deletions

View File

@@ -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;

View File

@@ -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.
*