Add get() and set() methods to WP_User_Query for modifying query variables the way you might with WP_Query. props wonderboymusic. fixes #21426.

git-svn-id: http://core.svn.wordpress.org/trunk@21995 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2012-09-25 05:01:59 +00:00
parent 40bd91c86f
commit 0ada88ef52

View File

@ -313,6 +313,15 @@ function delete_user_option( $user_id, $option_name, $global = false ) {
*/ */
class WP_User_Query { class WP_User_Query {
/**
* Query vars, after parsing
*
* @since 3.5.0
* @access public
* @var array
*/
var $query_vars = array();
/** /**
* List of found user ids * List of found user ids
* *
@ -396,7 +405,7 @@ class WP_User_Query {
$this->query_fields = "$wpdb->users.ID"; $this->query_fields = "$wpdb->users.ID";
} }
if ( $this->query_vars['count_total'] ) if ( $qv['count_total'] )
$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
$this->query_from = "FROM $wpdb->users"; $this->query_from = "FROM $wpdb->users";
@ -528,29 +537,60 @@ class WP_User_Query {
function query() { function query() {
global $wpdb; global $wpdb;
if ( is_array( $this->query_vars['fields'] ) || 'all' == $this->query_vars['fields'] ) { $qv =& $this->query_vars;
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("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
} else { } 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("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
} }
if ( $this->query_vars['count_total'] ) if ( $qv['count_total'] )
$this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
if ( !$this->results ) if ( !$this->results )
return; return;
if ( 'all_with_meta' == $this->query_vars['fields'] ) { if ( 'all_with_meta' == $qv['fields'] ) {
cache_users( $this->results ); cache_users( $this->results );
$r = array(); $r = array();
foreach ( $this->results as $userid ) foreach ( $this->results as $userid )
$r[ $userid ] = new WP_User( $userid, '', $this->query_vars['blog_id'] ); $r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
$this->results = $r; $this->results = $r;
} }
} }
/**
* Retrieve query variable.
*
* @since 3.5.0
* @access public
*
* @param string $query_var Query variable key.
* @return mixed
*/
function get( $query_var ) {
if ( isset( $this->query_vars[$query_var] ) )
return $this->query_vars[$query_var];
return null;
}
/**
* Set query variable.
*
* @since 3.5.0
* @access public
*
* @param string $query_var Query variable key.
* @param mixed $value Query variable value.
*/
function set( $query_var, $value ) {
$this->query_vars[$query_var] = $value;
}
/* /*
* Used internally to generate an SQL string for searching across multiple columns * Used internally to generate an SQL string for searching across multiple columns
* *