BP_Groups_Template

The main Groups template loop class.

Description

Responsible for loading a group of groups into a loop for display.

Source

File: bp-groups/classes/class-bp-groups-template.php

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
class BP_Groups_Template {
 
    /**
     * The loop iterator.
     *
     * @var int
     * @since BuddyPress 1.2.0
     */
    public $current_group = -1;
 
    /**
     * The number of groups returned by the paged query.
     *
     * @var int
     * @since BuddyPress 1.2.0
     */
    public $group_count;
 
    /**
     * Array of groups located by the query.
     *
     * @var array
     * @since BuddyPress 1.2.0
     */
    public $groups;
 
    /**
     * The group object currently being iterated on.
     *
     * @var object
     * @since BuddyPress 1.2.0
     */
    public $group;
 
    /**
     * A flag for whether the loop is currently being iterated.
     *
     * @var bool
     * @since BuddyPress 1.2.0
     */
    public $in_the_loop;
 
    /**
     * The page number being requested.
     *
     * @var string
     * @since BuddyPress 1.2.0
     */
    public $pag_page;
 
    /**
     * The number of items being requested per page.
     *
     * @var string
     * @since BuddyPress 1.2.0
     */
    public $pag_num;
 
    /**
     * An HTML string containing pagination links.
     *
     * @var string
     * @since BuddyPress 1.2.0
     */
    public $pag_links;
 
    /**
     * The total number of groups matching the query parameters.
     *
     * @var int
     * @since BuddyPress 1.2.0
     */
    public $total_group_count;
 
    /**
     * Whether the template loop is for a single group page.
     *
     * @var bool
     * @since BuddyPress 1.2.0
     */
    public $single_group = false;
 
    /**
     * Field to sort by.
     *
     * @var string
     * @since BuddyPress 1.2.0
     */
    public $sort_by;
 
    /**
     * Sort order.
     *
     * @var string
     * @since BuddyPress 1.2.0
     */
    public $order;
 
    /**
     * Constructor method.
     *
     * @see BP_Groups_Group::get() for an in-depth description of arguments.
     *
     * @param array $args {
     *     Array of arguments. Accepts all arguments accepted by
     *     {@link BP_Groups_Group::get()}. In cases where the default
     *     values of the params differ, they have been discussed below.
     *     @type int $per_page Default: 20.
     *     @type int $page Default: 1.
     * }
     */
    function __construct( $args = array() ){
 
        // Backward compatibility with old method of passing arguments.
        if ( ! is_array( $args ) || func_num_args() > 1 ) {
            _deprecated_argument( __METHOD__, '1.7', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddyboss' ), __METHOD__, __FILE__ ) );
 
            $old_args_keys = array(
                0  => 'user_id',
                1  => 'type',
                2  => 'page',
                3  => 'per_page',
                4  => 'max',
                5  => 'slug',
                6  => 'search_terms',
                7  => 'populate_extras',
                8  => 'include',
                9  => 'exclude',
                10 => 'show_hidden',
                11 => 'page_arg',
            );
 
            $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
        }
 
        $defaults = array(
            'page'               => 1,
            'per_page'           => 20,
            'page_arg'           => 'grpage',
            'max'                => false,
            'type'               => 'active',
            'order'              => 'DESC',
            'orderby'            => 'date_created',
            'show_hidden'        => false,
            'user_id'            => 0,
            'slug'               => false,
            'include'            => false,
            'exclude'            => false,
            'parent_id'          => null,
            'search_terms'       => '',
            'search_columns'     => array(),
            'group_type'         => '',
            'group_type__in'     => '',
            'group_type__not_in' => '',
            'meta_query'         => false,
            'update_meta_cache'  => true,
            'update_admin_cache' => false,
        );
 
        $r = bp_parse_args( $args, $defaults, 'groups_template' );
        extract( $r );
 
        $this->pag_arg  = sanitize_key( $r['page_arg'] );
        $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
        $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
 
        if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) ) {
            $show_hidden = true;
        }
 
        if ( 'invites' == $type ) {
            $this->groups = groups_get_invites_for_user( $user_id, $this->pag_num, $this->pag_page, $exclude );
        } elseif ( 'single-group' == $type ) {
            $this->single_group = true;
 
            if ( groups_get_current_group() ) {
                $group = groups_get_current_group();
 
            } else {
                $group = groups_get_group( BP_Groups_Group::get_id_from_slug( $r['slug'] ) );
            }
 
            // Backwards compatibility - the 'group_id' variable is not part of the
            // BP_Groups_Group object, but we add it here for devs doing checks against it
            //
            //
            // this is subject to removal in a future release; devs should check against
            // $group->id instead.
            $group->group_id = $group->id;
 
            $this->groups = array( $group );
 
        } else {
            $this->groups = groups_get_groups( array(
                'type'               => $type,
                'order'              => $order,
                'orderby'            => $orderby,
                'per_page'           => $this->pag_num,
                'page'               => $this->pag_page,
                'user_id'            => $user_id,
                'search_terms'       => $search_terms,
                'search_columns'     => $search_columns,
                'meta_query'         => $meta_query,
                'group_type'         => $group_type,
                'group_type__in'     => $group_type__in,
                'group_type__not_in' => $group_type__not_in,
                'include'            => $include,
                'exclude'            => $exclude,
                'parent_id'          => $parent_id,
                'update_meta_cache'  => $update_meta_cache,
                'update_admin_cache' => $update_admin_cache,
                'show_hidden'        => $show_hidden,
            ) );
        }
 
        if ( 'invites' == $type ) {
            $this->total_group_count = (int) $this->groups['total'];
            $this->group_count       = (int) $this->groups['total'];
            $this->groups            = $this->groups['groups'];
        } elseif ( 'single-group' == $type ) {
            if ( empty( $group->id ) ) {
                $this->total_group_count = 0;
                $this->group_count       = 0;
            } else {
                $this->total_group_count = 1;
                $this->group_count       = 1;
            }
        } else {
            if ( empty( $max ) || $max >= (int) $this->groups['total'] ) {
                $this->total_group_count = (int) $this->groups['total'];
            } else {
                $this->total_group_count = (int) $max;
            }
 
            $this->groups = $this->groups['groups'];
 
            if ( !empty( $max ) ) {
                if ( $max >= count( $this->groups ) ) {
                    $this->group_count = count( $this->groups );
                } else {
                    $this->group_count = (int) $max;
                }
            } else {
                $this->group_count = count( $this->groups );
            }
        }
 
        // Build pagination links.
        if ( (int) $this->total_group_count && (int) $this->pag_num ) {
            $pag_args = array(
                $this->pag_arg => '%#%'
            );
 
            if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
                $base = remove_query_arg( 's', wp_get_referer() );
            } else {
                $base = '';
            }
 
            $add_args = array(
                'num'     => $this->pag_num,
                'sortby'  => $this->sort_by,
                'order'   => $this->order,
            );
 
            if ( ! empty( $search_terms ) ) {
                $query_arg = bp_core_get_component_search_query_arg( 'groups' );
                $add_args[ $query_arg ] = urlencode( $search_terms );
            }
 
            $this->pag_links = paginate_links( array(
                'base'      => add_query_arg( $pag_args, $base ),
                'format'    => '',
                'total'     => ceil( (int) $this->total_group_count / (int) $this->pag_num ),
                'current'   => $this->pag_page,
                'prev_text' => __( '←', 'buddyboss' ),
                'next_text' => __( '→', 'buddyboss' ),
                'mid_size'  => 1,
                'add_args'  => $add_args,
            ) );
        }
    }
 
    /**
     * Whether there are groups available in the loop.
     *
     * @since BuddyPress 1.2.0
     *
     * @see bp_has_groups()
     *
     * @return bool True if there are items in the loop, otherwise false.
     */
    function has_groups() {
        if ( $this->group_count ) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Set up the next group and iterate index.
     *
     * @since BuddyPress 1.2.0
     *
     * @return object The next group to iterate over.
     */
    function next_group() {
        $this->current_group++;
        $this->group = $this->groups[$this->current_group];
 
        return $this->group;
    }
 
    /**
     * Rewind the groups and reset member index.
     *
     * @since BuddyPress 1.2.0
     */
    function rewind_groups() {
        $this->current_group = -1;
        if ( $this->group_count > 0 ) {
            $this->group = $this->groups[0];
        }
    }
 
    /**
     * Whether there are groups left in the loop to iterate over.
     *
     * This method is used by {@link bp_groups()} as part of the while loop
     * that controls iteration inside the groups loop, eg:
     *     while ( bp_groups() ) { ...
     *
     * @since BuddyPress 1.2.0
     *
     * @see bp_groups()
     *
     * @return bool True if there are more groups to show, otherwise false.
     */
    function groups() {
        if ( $this->current_group + 1 < $this->group_count ) {
            return true;
        } elseif ( $this->current_group + 1 == $this->group_count ) {
 
            /**
             * Fires right before the rewinding of groups list.
             *
             * @since BuddyPress 1.5.0
             */
            do_action('group_loop_end');
            // Do some cleaning up after the loop.
            $this->rewind_groups();
        }
 
        $this->in_the_loop = false;
        return false;
    }
 
    /**
     * Set up the current group inside the loop.
     *
     * Used by {@link bp_the_group()} to set up the current group data
     * while looping, so that template tags used during that iteration make
     * reference to the current member.
     *
     * @since BuddyPress 1.2.0
     *
     * @see bp_the_group()
     */
    function the_group() {
        $this->in_the_loop = true;
        $this->group       = $this->next_group();
 
        if ( 0 == $this->current_group ) {
 
            /**
             * Fires if the current group item is the first in the loop.
             *
             * @since BuddyPress 1.1.0
             */
            do_action( 'group_loop_start' );
        }
    }
}

Changelog

Changelog
Version Description
BuddyPress 1.2.0 Introduced.

Methods

  • __construct — Constructor method.
  • groups — Whether there are groups left in the loop to iterate over.
  • has_groups — Whether there are groups available in the loop.
  • next_group — Set up the next group and iterate index.
  • rewind_groups — Rewind the groups and reset member index.
  • the_group — Set up the current group inside the loop.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.