Skip to:
Content

BuddyPress.org

Changeset 8503


Ignore:
Timestamp:
06/11/2014 10:30:00 PM (12 years ago)
Author:
johnjamesjacoby
Message:

Improvements to bp_has_groups():

  • Add phpdoc from BP_Groups_Template::get()
  • Add inline doc better explaining what default values are.
  • Move $defaults into bp_parse_args() call to remove one-time-use variable.
  • Fix seemingly unused $order parameter noted in r7087 to check action variable in the my-groups context.
  • Remove $bp global touch.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-template.php

    r8459 r8503  
    293293 * Start the Groups Template Loop
    294294 *
    295  * See the $defaults definition below for a description of parameters.
    296  *
    297  * Note that the 'type' parameter overrides 'order' and 'orderby'. See
    298  * BP_Groups_Group::get() for more details.
    299  *
    300  * @param array $args
     295 * @since BuddyPress (1.0.0)
     296 *
     297 * @param array $args {
     298 *     Array of parameters. All items are optional.
     299 *     @type string $type Optional. Shorthand for certain orderby/
     300 *           order combinations. 'newest', 'active', 'popular',
     301 *           'alphabetical', 'random'. When present, will override
     302 *           orderby and order params. Default: null.
     303 *     @type string $orderby Optional. Property to sort by.
     304 *           'date_created', 'last_activity', 'total_member_count',
     305 *           'name', 'random'. Default: 'date_created'.
     306 *     @type string $order Optional. Sort order. 'ASC' or 'DESC'.
     307 *           Default: 'DESC'.
     308 *     @type int $per_page Optional. Number of items to return per page
     309 *           of results. Default: null (no limit).
     310 *     @type int $page Optional. Page offset of results to return.
     311 *           Default: null (no limit).
     312 *     @type int $user_id Optional. If provided, results will be limited
     313 *           to groups of which the specified user is a member. Default:
     314 *           null.
     315 *     @type string $search_terms Optional. If provided, only groups
     316 *           whose names or descriptions match the search terms will be
     317 *           returned. Default: false.
     318 *     @type array $meta_query Optional. An array of meta_query
     319 *           conditions. See {@link WP_Meta_Query::queries} for
     320 *           description.
     321 *     @type array|string Optional. Array or comma-separated list of
     322 *           group IDs. Results will be limited to groups within the
     323 *           list. Default: false.
     324 *     @type bool $populate_extras Whether to fetch additional
     325 *           information (such as member count) about groups. Default:
     326 *           true.
     327 *     @type array|string Optional. Array or comma-separated list of
     328 *           group IDs. Results will exclude the listed groups.
     329 *           Default: false.
     330 *     @type bool $show_hidden Whether to include hidden groups in
     331 *           results. Default: false.
     332 * }
     333 *
    301334 * @return bool True if there are groups to display that match the params
    302335 */
    303336function bp_has_groups( $args = '' ) {
    304         global $groups_template, $bp;
     337        global $groups_template;
    305338
    306339        /***
    307          * Set the defaults based on the current page. Any of these will be overridden
    308          * if arguments are directly passed into the loop. Custom plugins should always
    309          * pass their parameters directly to the loop.
     340         * Defaults based on the current page & overridden by parsed $args
    310341         */
    311         $slug    = false;
    312         $type    = '';
    313         $user_id = 0;
    314         $order   = '';
    315 
    316         // User filtering
    317         if ( bp_displayed_user_id() )
    318                 $user_id = bp_displayed_user_id();
    319 
    320         // Type
    321         // @todo What is $order? At some point it was removed incompletely?
     342        $slug         = false;
     343        $type         = '';
     344        $search_terms = false;
     345
     346        // When looking your own groups, check for two action variables
    322347        if ( bp_is_current_action( 'my-groups' ) ) {
    323                 if ( 'most-popular' == $order ) {
     348                if ( bp_is_action_variable( 'most-popular', 0 ) ) {
    324349                        $type = 'popular';
    325                 } elseif ( 'alphabetically' == $order ) {
     350                } elseif ( bp_is_action_variable( 'alphabetically', 0 ) ) {
    326351                        $type = 'alphabetical';
    327352                }
     353
     354        // When looking at invites, set type to invites
    328355        } elseif ( bp_is_current_action( 'invites' ) ) {
    329356                $type = 'invites';
    330         } elseif ( isset( $bp->groups->current_group->slug ) && $bp->groups->current_group->slug ) {
     357
     358        // When looking at a single group, set the type and slug
     359        } elseif ( bp_get_current_group_slug() ) {
    331360                $type = 'single-group';
    332                 $slug = $bp->groups->current_group->slug;
    333         }
    334 
    335         // Default search string
     361                $slug = bp_get_current_group_slug();
     362        }
     363
     364        // Default search string (too soon to escape here)
    336365        if ( ! empty( $_REQUEST['group-filter-box'] ) ) {
    337366                $search_terms = $_REQUEST['group-filter-box'];
    338         } elseif ( isset( $_REQUEST['s'] ) && !empty( $_REQUEST['s'] ) ) {
     367        } elseif ( !empty( $_REQUEST['s'] ) ) {
    339368                $search_terms = $_REQUEST['s'];
    340         } else {
    341                 $search_terms = false;
    342         }
    343 
    344         $defaults = array(
    345                 'type'              => $type, // 'type' is an override for 'order' and 'orderby'. See docblock.
     369        }
     370
     371        // Parse defaults and requested arguments
     372        $r = bp_parse_args( $args, array(
     373                'type'              => $type,
    346374                'order'             => 'DESC',
    347375                'orderby'           => 'last_activity',
     
    350378                'max'               => false,
    351379                'show_hidden'       => false,
    352 
    353                 'page_arg'          => 'grpage', // See https://buddypress.trac.wordpress.org/ticket/3679
    354 
    355                 'user_id'           => $user_id, // Pass a user ID to limit to groups this user has joined
    356                 'slug'              => $slug,    // Pass a group slug to only return that group
    357                 'search_terms'      => $search_terms, // Pass search terms to return only matching groups
    358                 'meta_query'        => false,    // Filter by groupmeta. See WP_Meta_Query for format
    359                 'include'           => false,    // Pass comma separated list or array of group ID's to return only these groups
    360                 'exclude'           => false,    // Pass comma separated list or array of group ID's to exclude these groups
    361 
    362                 'populate_extras'   => true,     // Get extra meta - is_member, is_banned
     380                'page_arg'          => 'grpage',
     381                'user_id'           => bp_displayed_user_id(),
     382                'slug'              => $slug,
     383                'search_terms'      => $search_terms,
     384                'meta_query'        => false,
     385                'include'           => false,
     386                'exclude'           => false,
     387                'populate_extras'   => true,
    363388                'update_meta_cache' => true,
    364         );
    365 
    366         $r = bp_parse_args( $args, $defaults, 'has_groups' );
    367 
     389        ), 'has_groups' );
     390
     391        // Setup the Groups template global
    368392        $groups_template = new BP_Groups_Template( array(
    369393                'type'              => $r['type'],
     
    385409        ) );
    386410
     411        // Filter and return whether or not the groups loop has groups in it
    387412        return apply_filters( 'bp_has_groups', $groups_template->has_groups(), $groups_template, $r );
    388413}
Note: See TracChangeset for help on using the changeset viewer.