Canonical: Limit post types searched by `redirect_guess_404_permalink()`.

Limit the post types searched in `redirect_guess_404_permalink()` to public, searchable post types. This prevents redirects to 404 pages and the exposure of private post type slugs.

Props francescocarlucci, peterwilsoncc, rajinsharwar.
Fixes #59795.


Built from https://develop.svn.wordpress.org/trunk@57645


git-svn-id: http://core.svn.wordpress.org/trunk@57146 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2024-02-16 23:34:11 +00:00
parent eadb61542a
commit 112f61146d
2 changed files with 12 additions and 3 deletions

View File

@ -949,6 +949,9 @@ function redirect_guess_404_permalink() {
}
if ( get_query_var( 'name' ) ) {
$publicly_viewable_statuses = array_filter( get_post_stati(), 'is_post_status_viewable' );
$publicly_viewable_post_types = array_filter( get_post_types( array( 'exclude_from_search' => false ) ), 'is_post_type_viewable' );
/**
* Filters whether to perform a strict guess for a 404 redirect.
*
@ -969,12 +972,19 @@ function redirect_guess_404_permalink() {
// If any of post_type, year, monthnum, or day are set, use them to refine the query.
if ( get_query_var( 'post_type' ) ) {
if ( is_array( get_query_var( 'post_type' ) ) ) {
$post_types = array_intersect( get_query_var( 'post_type' ), $publicly_viewable_post_types );
if ( empty( $post_types ) ) {
return false;
}
$where .= " AND post_type IN ('" . join( "', '", esc_sql( get_query_var( 'post_type' ) ) ) . "')";
} else {
if ( ! in_array( get_query_var( 'post_type' ), $publicly_viewable_post_types, true ) ) {
return false;
}
$where .= $wpdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
}
} else {
$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
$where .= " AND post_type IN ('" . implode( "', '", esc_sql( $publicly_viewable_post_types ) ) . "')";
}
if ( get_query_var( 'year' ) ) {
@ -987,7 +997,6 @@ 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 IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')" );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-beta1-57644';
$wp_version = '6.5-beta1-57645';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.