From 80817d320992564702b003309d484ef3e945c470 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 15 Apr 2019 23:48:11 +0000 Subject: [PATCH] Bootstrap/Load: After [45016], make sure `wp_die()` does not cause `_doing_it_wrong()` notices if called before `$wp_query` global is set. Props tmdesigned, TimothyBlynJacobs. Fixes #46813. git-svn-id: https://develop.svn.wordpress.org/trunk@45206 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 10 +++++++--- tests/phpunit/tests/includes/helpers.php | 12 ++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index a30e7164fa..d5a9cfb945 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2945,6 +2945,8 @@ function wp_nonce_ays( $action ) { * an integer to be used as the response code. * @since 5.1.0 The `$link_url`, `$link_text`, and `$exit` arguments were added. * + * @global WP_Query $wp_query Global WP_Query instance. + * * @param string|WP_Error $message Optional. Error message. If this is a WP_Error object, * and not an Ajax or XML-RPC request, the error's messages are used. * Default empty. @@ -2971,6 +2973,7 @@ function wp_nonce_ays( $action ) { * } */ function wp_die( $message = '', $title = '', $args = array() ) { + global $wp_query; if ( is_int( $args ) ) { $args = array( 'response' => $args ); @@ -3016,9 +3019,10 @@ function wp_die( $message = '', $title = '', $args = array() ) { */ $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' ); } elseif ( wp_is_xml_request() - || function_exists( 'is_feed' ) && is_feed() - || function_exists( 'is_comment_feed' ) && is_comment_feed() - || function_exists( 'is_trackback' ) && is_trackback() ) { + || isset( $wp_query ) && + ( function_exists( 'is_feed' ) && is_feed() + || function_exists( 'is_comment_feed' ) && is_comment_feed() + || function_exists( 'is_trackback' ) && is_trackback() ) ) { /** * Filters the callback for killing WordPress execution for XML requests. * diff --git a/tests/phpunit/tests/includes/helpers.php b/tests/phpunit/tests/includes/helpers.php index 6e488b72f2..d96207606b 100644 --- a/tests/phpunit/tests/includes/helpers.php +++ b/tests/phpunit/tests/includes/helpers.php @@ -274,6 +274,18 @@ class Tests_TestHelpers extends WP_UnitTestCase { wp_die( new WP_Error( 'test', 'test' ) ); } + /** + * @ticket 46813 + * @expectedException WPDieException + */ + public function test_die_handler_should_not_cause_doing_it_wrong_notice_without_wp_query_set() { + unset( $GLOBALS['wp_query'] ); + + wp_die(); + + $this->assertEmpty( $this->caught_doing_it_wrong ); + } + /** * @ticket 45933 * @dataProvider data_die_process_input