WordPress.org

Make WordPress Core

Changeset 47738


Ignore:
Timestamp:
05/02/2020 08:46:39 AM (7 months ago)
Author:
SergeyBiryukov
Message:

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

See #45337.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp.php

    r47727 r47738  
    624624     * Set the Headers for 404, if nothing is found for requested URL.
    625625     *
    626      * Issue a 404 if a request doesn't match any posts and doesn't match
    627      * any object (e.g. an existing-but-empty category, tag, author) and a 404 was not already
    628      * issued, and if the request was not a search or the homepage.
     626     * Issue a 404 if a request doesn't match any posts and doesn't match any object
     627     * (e.g. an existing-but-empty category, tag, author) and a 404 was not already issued,
     628     * and if the request was not a search or the homepage.
    629629     *
    630630     * Otherwise, issue a 200.
    631631     *
    632      * This sets headers after posts have been queried. handle_404() really means "handle status."
     632     * This sets headers after posts have been queried. handle_404() really means "handle status".
    633633     * By inspecting the result of querying posts, seemingly successful requests can be switched to
    634634     * a 404 so that canonical redirection logic can kick in.
     
    661661        }
    662662
    663         // Never 404 for the admin, robots, favicon, or if we found posts.
    664         if ( is_admin() || is_robots() || is_favicon() || $wp_query->posts ) {
    665 
    666             $success = true;
     663        $set_404 = true;
     664
     665        // Never 404 for the admin, robots, or favicon.
     666        if ( is_admin() || is_robots() || is_favicon() ) {
     667            $set_404 = false;
     668
     669            // If posts were found, check for paged content.
     670        } elseif ( $wp_query->posts ) {
     671            $content_found = true;
     672
     673            $post = isset( $wp_query->post ) ? $wp_query->post : null;
     674
     675            // Only set X-Pingback for single posts that allow pings.
     676            if ( is_singular() && $post && pings_open( $post ) && ! headers_sent() ) {
     677                header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
     678            }
     679
     680            // Check for paged content that exceeds the max number of pages.
    667681            if ( is_singular() ) {
    668                 $p = false;
    669 
    670                 if ( $wp_query->post instanceof WP_Post ) {
    671                     $p = clone $wp_query->post;
    672                 }
    673 
    674                 // Only set X-Pingback for single posts that allow pings.
    675                 if ( $p && pings_open( $p ) && ! headers_sent() ) {
    676                     header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
    677                 }
    678 
    679                 // Check for paged content that exceeds the max number of pages.
    680682                $next = '<!--nextpage-->';
    681                 if ( $p && ! empty( $this->query_vars['page'] ) ) {
     683                if ( $post && ! empty( $this->query_vars['page'] ) ) {
    682684                    // Check if content is actually intended to be paged.
    683                     if ( false !== strpos( $p->post_content, $next ) ) {
    684                         $page    = trim( $this->query_vars['page'], '/' );
    685                         $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
     685                    if ( false !== strpos( $post->post_content, $next ) ) {
     686                        $page          = trim( $this->query_vars['page'], '/' );
     687                        $content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 );
    686688                    } else {
    687                         $success = false;
     689                        $content_found = false;
    688690                    }
    689691                }
    690692            }
    691693
    692             if ( $success ) {
    693                 status_header( 200 );
    694                 return;
    695             }
    696         }
    697 
    698         // We will 404 for paged queries, as no posts were found.
    699         if ( ! is_paged() ) {
     694            if ( $content_found ) {
     695                $set_404 = false;
     696            }
     697
     698            // We will 404 for paged queries, as no posts were found.
     699        } elseif ( ! is_paged() ) {
     700            $author = get_query_var( 'author' );
    700701
    701702            // Don't 404 for authors without posts as long as they matched an author on this site.
    702             $author = get_query_var( 'author' );
    703             if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author ) ) {
    704                 status_header( 200 );
    705                 return;
    706             }
    707 
    708             // Don't 404 for these queries if they matched an object.
    709             if ( ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object() ) {
    710                 status_header( 200 );
    711                 return;
    712             }
    713 
    714             // Don't 404 for these queries either.
    715             if ( is_home() || is_search() || is_feed() ) {
    716                 status_header( 200 );
    717                 return;
    718             }
    719         }
    720 
    721         // Guess it's time to 404.
    722         $wp_query->set_404();
    723         status_header( 404 );
    724         nocache_headers();
     703            if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author )
     704                // Don't 404 for these queries if they matched an object.
     705                || ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object()
     706                // Don't 404 for these queries either.
     707                || is_home() || is_search() || is_feed()
     708            ) {
     709                $set_404 = false;
     710            }
     711        }
     712
     713        if ( $set_404 ) {
     714            // Guess it's time to 404.
     715            $wp_query->set_404();
     716            status_header( 404 );
     717            nocache_headers();
     718        } else {
     719            status_header( 200 );
     720        }
    725721    }
    726722
Note: See TracChangeset for help on using the changeset viewer.