Query: Simplify the logic in WP::handle_404() to allow for easier modifications.

See #45337.
Built from https://develop.svn.wordpress.org/trunk@47738


git-svn-id: http://core.svn.wordpress.org/trunk@47514 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-05-02 08:48:08 +00:00
parent 25c841026d
commit 4abdb06f72
2 changed files with 47 additions and 51 deletions

View File

@ -623,13 +623,13 @@ class WP {
/** /**
* Set the Headers for 404, if nothing is found for requested URL. * Set the Headers for 404, if nothing is found for requested URL.
* *
* Issue a 404 if a request doesn't match any posts and doesn't match * Issue a 404 if a request doesn't match any posts and doesn't match any object
* any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already * (e.g. an existing-but-empty category, tag, author) and a 404 was not already issued,
* issued, and if the request was not a search or the homepage. * and if the request was not a search or the homepage.
* *
* Otherwise, issue a 200. * Otherwise, issue a 200.
* *
* This sets headers after posts have been queried. handle_404() really means "handle status." * This sets headers after posts have been queried. handle_404() really means "handle status".
* By inspecting the result of querying posts, seemingly successful requests can be switched to * By inspecting the result of querying posts, seemingly successful requests can be switched to
* a 404 so that canonical redirection logic can kick in. * a 404 so that canonical redirection logic can kick in.
* *
@ -660,68 +660,64 @@ class WP {
return; return;
} }
// Never 404 for the admin, robots, favicon, or if we found posts. $set_404 = true;
if ( is_admin() || is_robots() || is_favicon() || $wp_query->posts ) {
$success = true; // Never 404 for the admin, robots, or favicon.
if ( is_admin() || is_robots() || is_favicon() ) {
$set_404 = false;
// If posts were found, check for paged content.
} elseif ( $wp_query->posts ) {
$content_found = true;
$post = isset( $wp_query->post ) ? $wp_query->post : null;
// Only set X-Pingback for single posts that allow pings.
if ( is_singular() && $post && pings_open( $post ) && ! headers_sent() ) {
header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
}
// Check for paged content that exceeds the max number of pages.
if ( is_singular() ) { if ( is_singular() ) {
$p = false;
if ( $wp_query->post instanceof WP_Post ) {
$p = clone $wp_query->post;
}
// Only set X-Pingback for single posts that allow pings.
if ( $p && pings_open( $p ) && ! headers_sent() ) {
header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
}
// Check for paged content that exceeds the max number of pages.
$next = '<!--nextpage-->'; $next = '<!--nextpage-->';
if ( $p && ! empty( $this->query_vars['page'] ) ) { if ( $post && ! empty( $this->query_vars['page'] ) ) {
// Check if content is actually intended to be paged. // Check if content is actually intended to be paged.
if ( false !== strpos( $p->post_content, $next ) ) { if ( false !== strpos( $post->post_content, $next ) ) {
$page = trim( $this->query_vars['page'], '/' ); $page = trim( $this->query_vars['page'], '/' );
$success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 ); $content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 );
} else { } else {
$success = false; $content_found = false;
} }
} }
} }
if ( $success ) { if ( $content_found ) {
status_header( 200 ); $set_404 = false;
return;
} }
}
// We will 404 for paged queries, as no posts were found. // We will 404 for paged queries, as no posts were found.
if ( ! is_paged() ) { } elseif ( ! is_paged() ) {
$author = get_query_var( 'author' );
// Don't 404 for authors without posts as long as they matched an author on this site. // Don't 404 for authors without posts as long as they matched an author on this site.
$author = get_query_var( 'author' ); if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author )
if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author ) ) { // Don't 404 for these queries if they matched an object.
status_header( 200 ); || ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object()
return; // Don't 404 for these queries either.
} || is_home() || is_search() || is_feed()
) {
// Don't 404 for these queries if they matched an object. $set_404 = false;
if ( ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object() ) {
status_header( 200 );
return;
}
// Don't 404 for these queries either.
if ( is_home() || is_search() || is_feed() ) {
status_header( 200 );
return;
} }
} }
// Guess it's time to 404. if ( $set_404 ) {
$wp_query->set_404(); // Guess it's time to 404.
status_header( 404 ); $wp_query->set_404();
nocache_headers(); status_header( 404 );
nocache_headers();
} else {
status_header( 200 );
}
} }
/** /**

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.5-alpha-47737'; $wp_version = '5.5-alpha-47738';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.