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

See #45337.

git-svn-id: https://develop.svn.wordpress.org/trunk@47738 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-05-02 08:46:39 +00:00
parent 7e0a63f8c5
commit b7a8094d32
1 changed files with 46 additions and 50 deletions

View File

@ -623,13 +623,13 @@ class WP {
/**
* 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
* any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
* issued, and if the request was not a search or the homepage.
* Issue a 404 if a request doesn't match any posts and doesn't match any object
* (e.g. an existing-but-empty category, tag, author) and a 404 was not already issued,
* and if the request was not a search or the homepage.
*
* 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
* a 404 so that canonical redirection logic can kick in.
*
@ -660,68 +660,64 @@ class WP {
return;
}
// Never 404 for the admin, robots, favicon, or if we found posts.
if ( is_admin() || is_robots() || is_favicon() || $wp_query->posts ) {
$set_404 = true;
$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() ) {
$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-->';
if ( $p && ! empty( $this->query_vars['page'] ) ) {
if ( $post && ! empty( $this->query_vars['page'] ) ) {
// Check if content is actually intended to be paged.
if ( false !== strpos( $p->post_content, $next ) ) {
$page = trim( $this->query_vars['page'], '/' );
$success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
if ( false !== strpos( $post->post_content, $next ) ) {
$page = trim( $this->query_vars['page'], '/' );
$content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 );
} else {
$success = false;
$content_found = false;
}
}
}
if ( $success ) {
status_header( 200 );
return;
if ( $content_found ) {
$set_404 = false;
}
}
// We will 404 for paged queries, as no posts were found.
if ( ! is_paged() ) {
// We will 404 for paged queries, as no posts were found.
} 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.
$author = get_query_var( 'author' );
if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author ) ) {
status_header( 200 );
return;
}
// Don't 404 for these queries if they matched an object.
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;
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.
|| ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object()
// Don't 404 for these queries either.
|| is_home() || is_search() || is_feed()
) {
$set_404 = false;
}
}
// Guess it's time to 404.
$wp_query->set_404();
status_header( 404 );
nocache_headers();
if ( $set_404 ) {
// Guess it's time to 404.
$wp_query->set_404();
status_header( 404 );
nocache_headers();
} else {
status_header( 200 );
}
}
/**