Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/03/2013 05:12:54 PM (7 years ago)
Author:
boonebgorges
Message:

Introduces BP_Group_Member_Query and refactors bp_group_has_members() to use it

BP_Group_Member_Query extends BP_User_Query, which has a number of notable
benefits:

  • Group member queries no longer JOIN against global user tables
  • Less code duplication, since general logic like 'exclude' is handled by BP_User_Query
  • Future access to the additional parameters of BP_User_Query, such as 'type'

Using the new BP_Group_Member_Query, this changeset also changes the way that
group member queries filter by group roles (member, mod, admin). The new
group_role parameter in the bp_group_has_members() stack accepts an array of
group roles. The legacy argument 'exclude_admins_mods' is still accepted, and
translates to 'group_role' => array( 'member' ) when true. These group_role
enhancements will allow for future enhancements in the Groups Admin section of
the Dashboard, and other places where it might be useful to query for the
members of a group matching a specific role. See #4977.

Fixes #4482

Props trishasalas for early patches and feedback. Props johnjamesjacoby for
review.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-classes.php

    r7135 r7141  
    4949
    5050    /**
     51     * Unaltered params as passed to the constructor
     52     *
     53     * @since BuddyPress (1.8)
     54     * @var array
     55     */
     56    public $query_vars_raw = array();
     57
     58    /**
    5159     * Array of variables to query with
    5260     *
     
    120128     */
    121129    public function __construct( $query = null ) {
    122         if ( ! empty( $query ) ) {
    123             $this->query_vars = wp_parse_args( $query, array(
     130
     131        // Store the raw query vars for later access
     132        $this->query_vars_raw = $query;
     133
     134        // Allow extending classes to register action/filter hooks
     135        $this->setup_hooks();
     136
     137        if ( ! empty( $this->query_vars_raw ) ) {
     138            $this->query_vars = wp_parse_args( $this->query_vars_raw, array(
    124139                'type'            => 'newest',
    125140                'per_page'        => 0,
     
    163178
    164179    /**
     180     * Allow extending classes to set up action/filter hooks
     181     *
     182     * When extending BP_User_Query, you may need to use some of its
     183     * internal hooks to modify the output. It's not convenient to call
     184     * add_action() or add_filter() in your class constructor, because
     185     * BP_User_Query::__construct() contains a fair amount of logic that
     186     * you may not want to override in your class. Define this method in
     187     * your own class if you need a place where your extending class can
     188     * add its hooks early in the query-building process. See
     189     * BP_Group_Member_Query::setup_hooks() for an example.
     190     *
     191     * @since BuddyPress (1.8)
     192     */
     193    public function setup_hooks() {}
     194
     195    /**
    165196     * Prepare the query for user_ids
    166197     *
     
    285316
    286317        // 'include' - User ids to include in the results
    287         if ( false !== $include ) {
    288             $include        = wp_parse_id_list( $include );
    289             $include_ids    = $wpdb->escape( implode( ',', (array) $include ) );
     318        $include     = ! empty( $include ) ? wp_parse_id_list( $include ) : array();
     319        $include_ids = $this->get_include_ids( $include );
     320        if ( ! empty( $include_ids ) ) {
     321            $include_ids    = implode( ',', wp_parse_id_list( $include_ids ) );
    290322            $sql['where'][] = "u.{$this->uid_name} IN ({$include_ids})";
    291323        }
     
    428460            }
    429461        }
     462    }
     463
     464    /**
     465     * Fetches the ids of users to put in the IN clause of the main query
     466     *
     467     * By default, returns the value passed to it
     468     * ($this->query_vars['include']). Having this abstracted into a
     469     * standalone method means that extending classes can override the
     470     * logic, parsing together their own user_id limits with the 'include'
     471     * ids passed to the class constructor. See BP_Group_Member_Query for
     472     * an example.
     473     *
     474     * @since BuddyPress (1.8)
     475     * @param array Sanitized array of user ids, as passed to the 'include'
     476     *   parameter of the class constructor
     477     * @return array The list of users to which the main query should be
     478     *   limited
     479     */
     480    public function get_include_ids( $include = array() ) {
     481        return $include;
    430482    }
    431483
Note: See TracChangeset for help on using the changeset viewer.