wp/wp-includes/class-wp-user-query.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
   142 	 *
   142 	 *
   143 	 *     @type int          $blog_id             The site ID. Default is the current site.
   143 	 *     @type int          $blog_id             The site ID. Default is the current site.
   144 	 *     @type string|array $role                An array or a comma-separated list of role names that users must match
   144 	 *     @type string|array $role                An array or a comma-separated list of role names that users must match
   145 	 *                                             to be included in results. Note that this is an inclusive list: users
   145 	 *                                             to be included in results. Note that this is an inclusive list: users
   146 	 *                                             must match *each* role. Default empty.
   146 	 *                                             must match *each* role. Default empty.
   147 	 *     @type array        $role__in            An array of role names. Matched users must have at least one of these
   147 	 *     @type string[]     $role__in            An array of role names. Matched users must have at least one of these
   148 	 *                                             roles. Default empty array.
   148 	 *                                             roles. Default empty array.
   149 	 *     @type array        $role__not_in        An array of role names to exclude. Users matching one or more of these
   149 	 *     @type string[]     $role__not_in        An array of role names to exclude. Users matching one or more of these
   150 	 *                                             roles will not be included in results. Default empty array.
   150 	 *                                             roles will not be included in results. Default empty array.
   151 	 *     @type string       $meta_key            User meta key. Default empty.
   151 	 *     @type string       $meta_key            User meta key. Default empty.
   152 	 *     @type string       $meta_value          User meta value. Default empty.
   152 	 *     @type string       $meta_value          User meta value. Default empty.
   153 	 *     @type string       $meta_compare        Comparison operator to test the `$meta_value`. Accepts '=', '!=',
   153 	 *     @type string       $meta_compare        Comparison operator to test the `$meta_value`. Accepts '=', '!=',
   154 	 *                                             '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
   154 	 *                                             '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
   155 	 *                                             'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP',
   155 	 *                                             'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS', 'REGEXP',
   156 	 *                                             'NOT REGEXP', or 'RLIKE'. Default '='.
   156 	 *                                             'NOT REGEXP', or 'RLIKE'. Default '='.
   157 	 *     @type array        $include             An array of user IDs to include. Default empty array.
   157 	 *     @type int[]        $include             An array of user IDs to include. Default empty array.
   158 	 *     @type array        $exclude             An array of user IDs to exclude. Default empty array.
   158 	 *     @type int[]        $exclude             An array of user IDs to exclude. Default empty array.
   159 	 *     @type string       $search              Search keyword. Searches for possible string matches on columns.
   159 	 *     @type string       $search              Search keyword. Searches for possible string matches on columns.
   160 	 *                                             When `$search_columns` is left empty, it tries to determine which
   160 	 *                                             When `$search_columns` is left empty, it tries to determine which
   161 	 *                                             column to search in based on search string. Default empty.
   161 	 *                                             column to search in based on search string. Default empty.
   162 	 *     @type array        $search_columns      Array of column names to be searched. Accepts 'ID', 'user_login',
   162 	 *     @type string[]     $search_columns      Array of column names to be searched. Accepts 'ID', 'user_login',
   163 	 *                                             'user_email', 'user_url', 'user_nicename', 'display_name'.
   163 	 *                                             'user_email', 'user_url', 'user_nicename', 'display_name'.
   164 	 *                                             Default empty array.
   164 	 *                                             Default empty array.
   165 	 *     @type string|array $orderby             Field(s) to sort the retrieved users by. May be a single value,
   165 	 *     @type string|array $orderby             Field(s) to sort the retrieved users by. May be a single value,
   166 	 *                                             an array of values, or a multi-dimensional array with fields as
   166 	 *                                             an array of values, or a multi-dimensional array with fields as
   167 	 *                                             keys and orders ('ASC' or 'DESC') as values. Accepted values are
   167 	 *                                             keys and orders ('ASC' or 'DESC') as values. Accepted values are
   195 	 *                                             Default empty (all users).
   195 	 *                                             Default empty (all users).
   196 	 *     @type bool|array   $has_published_posts Pass an array of post types to filter results to users who have
   196 	 *     @type bool|array   $has_published_posts Pass an array of post types to filter results to users who have
   197 	 *                                             published posts in those post types. `true` is an alias for all
   197 	 *                                             published posts in those post types. `true` is an alias for all
   198 	 *                                             public post types.
   198 	 *                                             public post types.
   199 	 *     @type string       $nicename            The user nicename. Default empty.
   199 	 *     @type string       $nicename            The user nicename. Default empty.
   200 	 *     @type array        $nicename__in        An array of nicenames to include. Users matching one of these
   200 	 *     @type string[]     $nicename__in        An array of nicenames to include. Users matching one of these
   201 	 *                                             nicenames will be included in results. Default empty array.
   201 	 *                                             nicenames will be included in results. Default empty array.
   202 	 *     @type array        $nicename__not_in    An array of nicenames to exclude. Users matching one of these
   202 	 *     @type string[]     $nicename__not_in    An array of nicenames to exclude. Users matching one of these
   203 	 *                                             nicenames will not be included in results. Default empty array.
   203 	 *                                             nicenames will not be included in results. Default empty array.
   204 	 *     @type string       $login               The user login. Default empty.
   204 	 *     @type string       $login               The user login. Default empty.
   205 	 *     @type array        $login__in           An array of logins to include. Users matching one of these
   205 	 *     @type string[]     $login__in           An array of logins to include. Users matching one of these
   206 	 *                                             logins will be included in results. Default empty array.
   206 	 *                                             logins will be included in results. Default empty array.
   207 	 *     @type array        $login__not_in       An array of logins to exclude. Users matching one of these
   207 	 *     @type string[]     $login__not_in       An array of logins to exclude. Users matching one of these
   208 	 *                                             logins will not be included in results. Default empty array.
   208 	 *                                             logins will not be included in results. Default empty array.
   209 	 * }
   209 	 * }
   210 	 */
   210 	 */
   211 	public function prepare_query( $query = array() ) {
   211 	public function prepare_query( $query = array() ) {
   212 		global $wpdb;
   212 		global $wpdb;
   217 		}
   217 		}
   218 
   218 
   219 		/**
   219 		/**
   220 		 * Fires before the WP_User_Query has been parsed.
   220 		 * Fires before the WP_User_Query has been parsed.
   221 		 *
   221 		 *
   222 		 * The passed WP_User_Query object contains the query variables, not
   222 		 * The passed WP_User_Query object contains the query variables,
   223 		 * yet passed into SQL.
   223 		 * not yet passed into SQL.
   224 		 *
   224 		 *
   225 		 * @since 4.0.0
   225 		 * @since 4.0.0
   226 		 *
   226 		 *
   227 		 * @param WP_User_Query $this The current WP_User_Query instance,
   227 		 * @param WP_User_Query $query Current instance of WP_User_Query (passed by reference).
   228 		 *                            passed by reference.
       
   229 		 */
   228 		 */
   230 		do_action( 'pre_get_users', $this );
   229 		do_action_ref_array( 'pre_get_users', array( &$this ) );
   231 
   230 
   232 		// Ensure that query vars are filled after 'pre_get_users'.
   231 		// Ensure that query vars are filled after 'pre_get_users'.
   233 		$qv =& $this->query_vars;
   232 		$qv =& $this->query_vars;
   234 		$qv = $this->fill_query_vars( $qv );
   233 		$qv = $this->fill_query_vars( $qv );
   235 
   234 
   277 			foreach ( $post_types as &$post_type ) {
   276 			foreach ( $post_types as &$post_type ) {
   278 				$post_type = $wpdb->prepare( '%s', $post_type );
   277 				$post_type = $wpdb->prepare( '%s', $post_type );
   279 			}
   278 			}
   280 
   279 
   281 			$posts_table        = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
   280 			$posts_table        = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
   282 			$this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ', ', $post_types ) . ' ) )';
   281 			$this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . implode( ', ', $post_types ) . ' ) )';
   283 		}
   282 		}
   284 
   283 
   285 		// nicename
   284 		// nicename
   286 		if ( '' !== $qv['nicename'] ) {
   285 		if ( '' !== $qv['nicename'] ) {
   287 			$this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] );
   286 			$this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] );
   543 			 *
   542 			 *
   544 			 * @since 3.6.0
   543 			 * @since 3.6.0
   545 			 *
   544 			 *
   546 			 * @param string[]      $search_columns Array of column names to be searched.
   545 			 * @param string[]      $search_columns Array of column names to be searched.
   547 			 * @param string        $search         Text being searched.
   546 			 * @param string        $search         Text being searched.
   548 			 * @param WP_User_Query $this           The current WP_User_Query instance.
   547 			 * @param WP_User_Query $query          The current WP_User_Query instance.
   549 			 */
   548 			 */
   550 			$search_columns = apply_filters( 'user_search_columns', $search_columns, $search, $this );
   549 			$search_columns = apply_filters( 'user_search_columns', $search_columns, $search, $this );
   551 
   550 
   552 			$this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
   551 			$this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
   553 		}
   552 		}
   574 		 * The passed WP_User_Query object contains SQL parts formed
   573 		 * The passed WP_User_Query object contains SQL parts formed
   575 		 * from parsing the given query.
   574 		 * from parsing the given query.
   576 		 *
   575 		 *
   577 		 * @since 3.1.0
   576 		 * @since 3.1.0
   578 		 *
   577 		 *
   579 		 * @param WP_User_Query $this The current WP_User_Query instance,
   578 		 * @param WP_User_Query $query Current instance of WP_User_Query (passed by reference).
   580 		 *                            passed by reference.
       
   581 		 */
   579 		 */
   582 		do_action_ref_array( 'pre_user_query', array( &$this ) );
   580 		do_action_ref_array( 'pre_user_query', array( &$this ) );
   583 	}
   581 	}
   584 
   582 
   585 	/**
   583 	/**
   595 		$qv =& $this->query_vars;
   593 		$qv =& $this->query_vars;
   596 
   594 
   597 		/**
   595 		/**
   598 		 * Filters the users array before the query takes place.
   596 		 * Filters the users array before the query takes place.
   599 		 *
   597 		 *
   600 		 * Return a non-null value to bypass WordPress's default user queries.
   598 		 * Return a non-null value to bypass WordPress' default user queries.
       
   599 		 *
   601 		 * Filtering functions that require pagination information are encouraged to set
   600 		 * Filtering functions that require pagination information are encouraged to set
   602 		 * the `total_users` property of the WP_User_Query object, passed to the filter
   601 		 * the `total_users` property of the WP_User_Query object, passed to the filter
   603 		 * by reference. If WP_User_Query does not perform a database query, it will not
   602 		 * by reference. If WP_User_Query does not perform a database query, it will not
   604 		 * have enough information to generate these values itself.
   603 		 * have enough information to generate these values itself.
   605 		 *
   604 		 *
   606 		 * @since 5.1.0
   605 		 * @since 5.1.0
   607 		 *
   606 		 *
   608 		 * @param array|null $results Return an array of user data to short-circuit WP's user query
   607 		 * @param array|null    $results Return an array of user data to short-circuit WP's user query
   609 		 *                            or null to allow WP to run its normal queries.
   608 		 *                               or null to allow WP to run its normal queries.
   610 		 * @param WP_User_Query $this The WP_User_Query instance (passed by reference).
   609 		 * @param WP_User_Query $query   The WP_User_Query instance (passed by reference).
   611 		 */
   610 		 */
   612 		$this->results = apply_filters_ref_array( 'users_pre_query', array( null, &$this ) );
   611 		$this->results = apply_filters_ref_array( 'users_pre_query', array( null, &$this ) );
   613 
   612 
   614 		if ( null === $this->results ) {
   613 		if ( null === $this->results ) {
   615 			$this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit";
   614 			$this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit";
   627 				 * @since 3.2.0
   626 				 * @since 3.2.0
   628 				 * @since 5.1.0 Added the `$this` parameter.
   627 				 * @since 5.1.0 Added the `$this` parameter.
   629 				 *
   628 				 *
   630 				 * @global wpdb $wpdb WordPress database abstraction object.
   629 				 * @global wpdb $wpdb WordPress database abstraction object.
   631 				 *
   630 				 *
   632 				 * @param string $sql         The SELECT FOUND_ROWS() query for the current WP_User_Query.
   631 				 * @param string        $sql   The SELECT FOUND_ROWS() query for the current WP_User_Query.
   633 				 * @param WP_User_Query $this The current WP_User_Query instance.
   632 				 * @param WP_User_Query $query The current WP_User_Query instance.
   634 				 */
   633 				 */
   635 				$found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
   634 				$found_users_query = apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()', $this );
   636 
   635 
   637 				$this->total_users = (int) $wpdb->get_var( $found_users_query );
   636 				$this->total_users = (int) $wpdb->get_var( $found_users_query );
   638 			}
   637 			}