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.



git-svn-id: https://develop.svn.wordpress.org/trunk@27185 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-02-17 21:40:04 +00:00
parent db1c44bc42
commit 7bdca44b97
2 changed files with 56 additions and 18 deletions

View File

@ -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 );
}
/**

View File

@ -43,7 +43,6 @@ class Tests_User_Query extends WP_UnitTestCase {
$ids = $users->get_results();
$this->assertEquals( array( $this->user_id ), $ids );
}
function test_exclude() {
@ -101,4 +100,38 @@ class Tests_User_Query extends WP_UnitTestCase {
$this->assertEquals( $names, $values );
}
function test_prepare_query() {
$query = new WP_User_Query();
$this->assertEmpty( $query->query_fields );
$this->assertEmpty( $query->query_from );
$this->assertEmpty( $query->query_limit );
$this->assertEmpty( $query->query_orderby );
$this->assertEmpty( $query->query_where );
$this->assertEmpty( $query->query_vars );
$_query_vars = $query->query_vars;
$query->prepare_query();
$this->assertNotEmpty( $query->query_fields );
$this->assertNotEmpty( $query->query_from );
$this->assertEmpty( $query->query_limit );
$this->assertNotEmpty( $query->query_orderby );
$this->assertNotEmpty( $query->query_where );
$this->assertNotEmpty( $query->query_vars );
$this->assertNotEquals( $_query_vars, $query->query_vars );
// All values get reset
$query->prepare_query( array( 'number' => 8 ) );
$this->assertNotEmpty( $query->query_limit );
$this->assertEquals( 'LIMIT 8', $query->query_limit );
// All values get reset
$query->prepare_query( array( 'fields' => 'all' ) );
$this->assertEmpty( $query->query_limit );
$this->assertEquals( '', $query->query_limit );
$_query_vars = $query->query_vars;
$query->prepare_query();
$this->assertEquals( $_query_vars, $query->query_vars );
}
}