From ca0979baa5579a8cfd0b889482c42569d0f76c56 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 20 Sep 2022 13:10:24 +0000 Subject: [PATCH] Bootstrap/Load: Send HTTP headers after querying posts in `WP::main()`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By running `WP::send_headers()` after posts have been queried, we can ensure that conditional tags like `is_front_page()`, `is_home()`, etc. work as expected. This provides better context and more flexibility when adjusting HTTP headers via the `wp_headers` filter or `send_headers` action. Previously, the earliest action where conditional tags worked correctly was `wp`. Includes moving the `X-Pingback` header, previously sent in `WP::handle_404()`​ after posts have been queried, to a more appropriate place in `WP::send_headers()`. Follow-up to [2627], [34442]. Props jonoaldersonwp, joostdevalk, peterwilsoncc, adamsilverstein, SergeyBiryukov. Fixes #56068. git-svn-id: https://develop.svn.wordpress.org/trunk@54250 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 23 ++++++++++++++--------- tests/phpunit/tests/wp/sendHeaders.php | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 tests/phpunit/tests/wp/sendHeaders.php diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 8d67820545..0a0a501ee0 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -408,7 +408,8 @@ class WP { * If showing a feed, it will also send Last-Modified, ETag, and 304 status if needed. * * @since 2.0.0 - * @since 4.4.0 `X-Pingback` header is added conditionally after posts have been queried in handle_404(). + * @since 4.4.0 `X-Pingback` header is added conditionally for single posts that allow pings. + * @since 6.1.0 Runs after posts have been queried. */ public function send_headers() { $headers = array(); @@ -504,6 +505,15 @@ class WP { } } + if ( is_singular() ) { + $post = isset( $wp_query->post ) ? $wp_query->post : null; + + // Only set X-Pingback for single posts that allow pings. + if ( $post && pings_open( $post ) ) { + $headers['X-Pingback'] = get_bloginfo( 'pingback_url', 'display' ); + } + } + /** * Filters the HTTP headers before they're sent to the browser. * @@ -701,14 +711,9 @@ class WP { if ( is_singular() ) { $post = isset( $wp_query->post ) ? $wp_query->post : null; - - // Only set X-Pingback for single posts that allow pings. - if ( $post && pings_open( $post ) && ! headers_sent() ) { - header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) ); - } + $next = ''; // Check for paged content that exceeds the max number of pages. - $next = ''; if ( $post && ! empty( $this->query_vars['page'] ) ) { // Check if content is actually intended to be paged. if ( false !== strpos( $post->post_content, $next ) ) { @@ -770,14 +775,14 @@ class WP { $parsed = $this->parse_request( $query_args ); - $this->send_headers(); - if ( $parsed ) { $this->query_posts(); $this->handle_404(); $this->register_globals(); } + $this->send_headers(); + /** * Fires once the WordPress environment has been set up. * diff --git a/tests/phpunit/tests/wp/sendHeaders.php b/tests/phpunit/tests/wp/sendHeaders.php new file mode 100644 index 0000000000..cc956409ef --- /dev/null +++ b/tests/phpunit/tests/wp/sendHeaders.php @@ -0,0 +1,22 @@ +assertQueryTrue( 'is_front_page', 'is_home' ); + } + ); + + $this->go_to( home_url() ); + } +}