Skip to content

Commit

Permalink
Merge pull request #712 from GoogleChromeLabs/fix/main-query-check-fo…
Browse files Browse the repository at this point in the history
…r-unauthenticating-user

Only unauthenticate user when `parse_query` is for the main query
  • Loading branch information
westonruter authored Apr 15, 2022
2 parents 4133d08 + 8553fd8 commit 3206f03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 20 additions & 0 deletions tests/test-general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,24 @@ public function test_wp_unauthenticate_error_template_requests( $request_url, $a
$this->assertEquals( 0, get_current_user_id() );
}
}

/**
* Test that that `wp_unauthenticate_error_template_requests()` running at the `parse_query` action doesn't cause
* an incorrect usage notice if there is not global `$wp_query` yet, such as when doing a subquery early in the WP
* execution flow.
*
* @covers ::wp_unauthenticate_error_template_requests()
*/
public function test_wp_unauthenticate_error_template_requests_for_subqueries() {
global $wp_query;
$wp_query = null;

$user_id = $this->factory()->user->create( array( 'role' => 'author' ) );
wp_set_current_user( $user_id );

$query = new WP_Query();
$query->parse_query( 's=test' );

$this->assertTrue( is_user_logged_in() );
}
}
6 changes: 4 additions & 2 deletions wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ function wp_add_error_template_no_robots() {
* will persist even after they have authenticated.
*
* @since 0.5
*
* @param WP_Query $query Query.
*/
function wp_unauthenticate_error_template_requests() {
if ( is_offline() || is_500() ) {
function wp_unauthenticate_error_template_requests( WP_Query $query ) {
if ( $query->is_main_query() && ( is_offline() || is_500() ) ) {
wp_set_current_user( 0 );
}
}
Expand Down

0 comments on commit 3206f03

Please sign in to comment.