Skip to:
Content

bbPress.org

Changeset 6332


Ignore:
Timestamp:
02/27/2017 11:08:15 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Engagements: First pass at a user_query, which will be used for looping through users.

  • Introduces BBP_User_Query which extends WP_User_Query
  • Includes functions for use within template parts

See #3068.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r6331 r6332  
    276276        $this->current_user_id      = 0; // Current topic tag id
    277277
    278         $this->forum_query    = new WP_Query();      // Main forum query
    279         $this->topic_query    = new WP_Query();      // Main topic query
    280         $this->reply_query    = new WP_Query();      // Main reply query
    281         $this->search_query   = new WP_Query();      // Main search query
    282         $this->user_query     = new WP_User_Query(); // Main user query
     278        $this->forum_query    = new WP_Query();       // Main forum query
     279        $this->topic_query    = new WP_Query();       // Main topic query
     280        $this->reply_query    = new WP_Query();       // Main reply query
     281        $this->search_query   = new WP_Query();       // Main search query
     282        $this->user_query     = new BBP_User_Query(); // Main user query
    283283
    284284        /** Theme Compat ******************************************************/
  • trunk/src/includes/users/template.php

    r6320 r6332  
    1010// Exit if accessed directly
    1111defined( 'ABSPATH' ) || exit;
     12
     13/** User Loop *****************************************************************/
     14
     15/**
     16 * Extension of WP_User_Query to allow easy looping
     17 *
     18 * @since 2.6.0 bbPress (r6330)
     19 */
     20class BBP_User_Query extends WP_User_Query {
     21
     22    /**
     23     * The amount of users for the current query.
     24     *
     25     * @since 2.6.0 bbPress (r6330)
     26     * @access public
     27     * @var int
     28     */
     29    public $user_count = 0;
     30
     31    /**
     32     * Index of the current item in the loop.
     33     *
     34     * @since 2.6.0 bbPress (r6330)
     35     * @access public
     36     * @var int
     37     */
     38    public $current_user = -1;
     39
     40    /**
     41     * Whether the loop has started and the caller is in the loop.
     42     *
     43     * @since 2.6.0 bbPress (r6330)
     44     * @access public
     45     * @var bool
     46     */
     47    public $in_the_loop = false;
     48
     49    /**
     50     * The current user.
     51     *
     52     * @since 2.6.0 bbPress (r6330)
     53     * @access public
     54     * @var WP_User
     55     */
     56    public $user;
     57
     58    /**
     59     * Set up the next user and iterate current user index.
     60     *
     61     * @since 2.6.0 bbPress (r6330)
     62     * @access public
     63     *
     64     * @return WP_User Next user.
     65     */
     66    public function next_user() {
     67
     68        $this->current_user++;
     69
     70        $this->user = $this->users[ $this->current_user ];
     71
     72        return $this->user;
     73    }
     74
     75    /**
     76     * Sets up the current user.
     77     *
     78     * Retrieves the next user, sets up the user, sets the 'in the loop'
     79     * property to true.
     80     *
     81     * @since 2.6.0 bbPress (r6330)
     82     * @access public
     83     *
     84     * @global WP_User $user
     85     */
     86    public function the_user() {
     87        $this->in_the_loop = true;
     88
     89        // loop has just started
     90        if ( $this->current_user === -1 ) {
     91            /**
     92             * Fires once the loop is started.
     93             *
     94             * @since 2.6.0 bbPress
     95             *
     96             * @param WP_Query &$this The WP_Query instance (passed by reference).
     97             */
     98            do_action_ref_array( 'loop_start', array( &$this ) );
     99        }
     100
     101        $this->user = $this->next_user();
     102    }
     103
     104    /**
     105     * Determines whether there are more users available in the loop.
     106     *
     107     * Calls the {@see 'loop_end'} action when the loop is complete.
     108     *
     109     * @since 2.6.0 bbPress (r6330)
     110     * @access public
     111     *
     112     * @return bool True if users are available, false if end of loop.
     113     */
     114    public function have_users() {
     115        if ( ( $this->current_user + 1 ) < $this->user_count ) {
     116            return true;
     117        } elseif ( ( ( $this->current_user + 1 ) === $this->user_count ) && ( $this->user_count > 0 ) ) {
     118
     119            /**
     120             * Fires once the loop has ended.
     121             *
     122             * @since 2.0.0
     123             *
     124             * @param WP_Query &$this The WP_Query instance (passed by reference).
     125             */
     126            do_action_ref_array( 'loop_end', array( &$this ) );
     127
     128            // Do some cleaning up after the loop
     129            $this->rewind_users();
     130        }
     131
     132        $this->in_the_loop = false;
     133
     134        return false;
     135    }
     136
     137    /**
     138     * Rewind the users and reset user index.
     139     *
     140     * @since 2.6.0 bbPress (r6330)
     141     * @access public
     142     */
     143    public function rewind_users() {
     144        $this->current_user = -1;
     145
     146        if ( $this->user_count > 0 ) {
     147            $this->user = $this->users[ 0 ];
     148        }
     149    }
     150}
     151
     152/**
     153 * The main user loop.
     154 *
     155 * @since 2.6.0 bbPress (r6330)
     156 *
     157 * @param array $args All the arguments supported by {@link WP_User_Query}
     158 * @uses BBP_User_Query To make query and get the users
     159 * @uses apply_filters() Calls 'bbp_has_users' with
     160 *                        bbPress::user_query::have_users()
     161 *                        and bbPress::user_query
     162 * @return object Multidimensional array of user information
     163 */
     164function bbp_has_users( $args = array() ) {
     165
     166    // Parse arguments with default user query for most circumstances
     167    $r = bbp_parse_args( $args, array(
     168        'include'     => array(),
     169        'orderby'     => 'login',
     170        'order'       => 'ASC',
     171        'count_total' => false,
     172        'fields'      => 'all',
     173    ), 'has_users' );
     174
     175    // Run the query
     176    $bbp             = bbpress();
     177    $bbp->user_query = new BBP_User_Query( $r );
     178
     179    return apply_filters( 'bbp_has_users', $bbp->user_query->have_users(), $bbp->user_query );
     180}
     181
     182/**
     183 * Whether there are more users available in the loop
     184 *
     185 * @since 2.6.0 bbPress (r2464)
     186 *
     187 * @uses bbPress:user_query::have_users() To check if there are more users
     188 *                                          available
     189 * @return object User information
     190 */
     191function bbp_users() {
     192    return bbpress()->user_query->have_users();;
     193}
     194
     195/**
     196 * Loads up the current user in the loop
     197 *
     198 * @since 2.6.0 bbPress (r2464)
     199 *
     200 * @uses bbPress:user_query::the_user() To get the current user
     201 * @return object User information
     202 */
     203function bbp_the_user() {
     204    return bbpress()->user_query->the_user();
     205}
    12206
    13207/** Users *********************************************************************/
Note: See TracChangeset for help on using the changeset viewer.