From 55b4eee4c2d777ef737c6e0e077e391fc5486e12 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 17 Feb 2014 21:41:12 +0000 Subject: [PATCH] Make `WP_User_Query::prepare_query()` public by allowing it to be passed an array of args. Previously, if the `WP_User_Query` constructor was not passed args, the object was basically unusable. Adds unit tests, all other tests pass. Props scribu, for the initial patch. Fixes #21119. Built from https://develop.svn.wordpress.org/trunk@27185 git-svn-id: http://core.svn.wordpress.org/trunk@27045 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/user.php | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/wp-includes/user.php b/wp-includes/user.php index 7a2d98d738..d90a41a147 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -431,7 +431,24 @@ class WP_User_Query { * @return WP_User_Query */ function __construct( $query = null ) { - if ( !empty( $query ) ) { + if ( ! empty( $query ) ) { + $this->prepare_query( $query ); + $this->query(); + } + } + + /** + * Prepare the query variables + * + * @since 3.1.0 + * + * @param string|array $args The query variables + */ + function prepare_query( $query = array() ) { + global $wpdb; + + 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' => '', @@ -450,20 +467,7 @@ class WP_User_Query { 'fields' => 'all', 'who' => '' ) ); - - $this->prepare_query(); - $this->query(); } - } - - /** - * Prepare the query variables - * - * @since 3.1.0 - * @access private - */ - function prepare_query() { - global $wpdb; $qv =& $this->query_vars; @@ -649,17 +653,18 @@ class WP_User_Query { * Execute the query, with the current variables * * @since 3.1.0 - * @access private */ function query() { global $wpdb; $qv =& $this->query_vars; + $query = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; + if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) { - $this->results = $wpdb->get_results("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"); + $this->results = $wpdb->get_results( $query ); } else { - $this->results = $wpdb->get_col("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"); + $this->results = $wpdb->get_col( $query ); } /**