diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index 21d013ca28..1bc08aa491 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/wp-includes/class-wp-user-query.php @@ -75,6 +75,40 @@ class WP_User_Query { } } + /** + * Fills in missing query variables with default values. + * + * @since 4.4.0 + * @access public + * + * @param array $args Query vars, as passed to `WP_User_Query`. + * @return array Complete query variables with undefined ones filled in with defaults. + */ + public static function fill_query_vars( $args ) { + $defaults = array( + 'blog_id' => $GLOBALS['blog_id'], + 'role' => '', + 'meta_key' => '', + 'meta_value' => '', + 'meta_compare' => '', + 'include' => array(), + 'exclude' => array(), + 'search' => '', + 'search_columns' => array(), + 'orderby' => 'login', + 'order' => 'ASC', + 'offset' => '', + 'number' => '', + 'paged' => 1, + 'count_total' => true, + 'fields' => 'all', + 'who' => '', + 'has_published_posts' => null, + ); + + return wp_parse_args( $args, $defaults ); + } + /** * Prepare the query variables. * @@ -146,26 +180,7 @@ class WP_User_Query { if ( empty( $this->query_vars ) || ! empty( $query ) ) { $this->query_limit = null; - $this->query_vars = wp_parse_args( $query, array( - 'blog_id' => $GLOBALS['blog_id'], - 'role' => '', - 'meta_key' => '', - 'meta_value' => '', - 'meta_compare' => '', - 'include' => array(), - 'exclude' => array(), - 'search' => '', - 'search_columns' => array(), - 'orderby' => 'login', - 'order' => 'ASC', - 'offset' => '', - 'number' => '', - 'paged' => 1, - 'count_total' => true, - 'fields' => 'all', - 'who' => '', - 'has_published_posts' => null, - ) ); + $this->query_vars = $this->fill_query_vars( $query ); } /** @@ -181,7 +196,9 @@ class WP_User_Query { */ do_action( 'pre_get_users', $this ); + // Ensure that query vars are filled after 'pre_get_users'. $qv =& $this->query_vars; + $qv = $this->fill_query_vars( $qv ); if ( is_array( $qv['fields'] ) ) { $qv['fields'] = array_unique( $qv['fields'] ); diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index 6bb95d5730..064c73f1ff 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -874,4 +874,26 @@ class Tests_User_Query extends WP_UnitTestCase { $this->assertEquals( array( $users[2], $users[1] ), $q->results ); } + + /** + * @ticket 33449 + */ + public function test_query_vars_should_be_filled_in_after_pre_get_users() { + $query_vars = array( 'blog_id', 'role', 'meta_key', 'meta_value', 'meta_compare', 'include', 'exclude', 'search', 'search_columns', 'orderby', 'order', 'offset', 'number', 'paged', 'count_total', 'fields', 'who', 'has_published_posts' ); + + add_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) ); + $q = new WP_User_Query( array_fill_keys( $query_vars, '1' ) ); + remove_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) ); + + foreach ( $query_vars as $query_var ) { + $this->assertTrue( array_key_exists( $query_var, $q->query_vars ), "$query_var does not exist." ); + } + + } + + public function filter_pre_get_users_args( $q ) { + foreach ( $q->query_vars as $k => $v ) { + unset( $q->query_vars[ $k ] ); + } + } }