BP_Media_Template

The main media template loop class.

Description

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

Source

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

18
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
class BP_Media_Template {
 
    /**
    * The loop iterator.
    *
    * @since BuddyBoss 1.0.0
    * @var int
    */
    public $current_media = -1;
 
    /**
     * The media count.
     *
     * @since BuddyBoss 1.0.0
     * @var int
     */
    public $media_count;
 
    /**
     * The total media count.
     *
     * @since BuddyBoss 1.0.0
     * @var int
     */
    public $total_media_count;
 
    /**
     * Array of media located by the query.
     *
     * @since BuddyBoss 1.0.0
     * @var array
     */
    public $medias;
 
    /**
     * The media object currently being iterated on.
     *
     * @since BuddyBoss 1.0.0
     * @var object
     */
    public $media;
 
    /**
     * A flag for whether the loop is currently being iterated.
     *
     * @since BuddyBoss 1.0.0
     * @var bool
     */
    public $in_the_loop;
 
    /**
     * URL parameter key for media pagination. Default: 'acpage'.
     *
     * @since BuddyBoss 1.0.0
     * @var string
     */
    public $pag_arg;
 
    /**
     * The page number being requested.
     *
     * @since BuddyBoss 1.0.0
     * @var int
     */
    public $pag_page;
 
    /**
     * The number of items being requested per page.
     *
     * @since BuddyBoss 1.0.0
     * @var int
     */
    public $pag_num;
 
    /**
     * An HTML string containing pagination links.
     *
     * @since BuddyBoss 1.0.0
     * @var string
     */
    public $pag_links;
 
    /**
     * The displayed user's full name.
     *
     * @since BuddyBoss 1.0.0
     * @var string
     */
    public $full_name;
 
    /**
     * Constructor method.
     *
     * The arguments passed to this class constructor are of the same
     * format as {@link BP_Media::get()}.
     *
     * @since BuddyBoss 1.0.0
     *
     * @see BP_Media::get() for a description of the argument
     *      structure, as well as default values.
     *
     * @param array $args {
     *     Array of arguments. Supports all arguments from
     *     BP_Media::get(), as well as 'page_arg' and
     *     'include'. Default values for 'per_page'
     *     differ from the originating function, and are described below.
     *     @type string      $page_arg         The string used as a query parameter in
     *                                         pagination links. Default: 'acpage'.
     *     @type array|bool  $include          Pass an array of media IDs to
     *                                         retrieve only those items, or false to noop the 'include'
     *                                         parameter. 'include' differs from 'in' in that 'in' forms
     *                                         an IN clause that works in conjunction with other filters
     *                                         passed to the function, while 'include' is interpreted as
     *                                         an exact list of items to retrieve, which skips all other
     *                                         filter-related parameters. Default: false.
     *     @type int|bool    $per_page         Default: 20.
     * }
     */
    public function __construct( $args ) {
 
        $defaults = array(
            'page'              => 1,
            'per_page'          => 20,
            'page_arg'          => 'acpage',
            'max'               => false,
            'fields'            => 'all',
            'count_total'       => false,
            'sort'              => false,
            'order_by'          => false,
            'include'           => false,
            'exclude'           => false,
            'search_terms'      => false,
            'user_id'           => false,
            'album_id'          => false,
            'group_id'          => false,
            'privacy'           => false,
        );
        $r = wp_parse_args( $args, $defaults );
        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'] );
 
        // Get an array of the logged in user's favorite media.
        $this->my_favs = bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_media', true );
 
        // Fetch specific media items based on ID's.
        if ( !empty( $include ) ) {
            $this->medias = bp_media_get_specific( array(
                'media_ids'         => explode( ',', $include ),
                'max'               => $max,
                'count_total'       => $count_total,
                'page'              => $this->pag_page,
                'per_page'          => $this->pag_num,
                'sort'              => $sort,
                'order_by'          => $order_by,
                'user_id'           => $user_id,
                'album_id'          => $album_id,
            ) );
 
            // Fetch all activity items.
        } else {
            $this->medias = bp_media_get( array(
                'max'               => $max,
                'count_total'       => $count_total,
                'per_page'          => $this->pag_num,
                'page'              => $this->pag_page,
                'sort'              => $sort,
                'order_by'          => $order_by,
                'search_terms'      => $search_terms,
                'user_id'           => $user_id,
                'album_id'          => $album_id,
                'group_id'          => $group_id,
                'exclude'           => $exclude,
                'privacy'           => $privacy,
            ) );
        }
 
        // The total_media_count property will be set only if a
        // 'count_total' query has taken place.
        if ( ! is_null( $this->medias['total'] ) ) {
            if ( ! $max || $max >= (int) $this->medias['total'] ) {
                $this->total_media_count = (int) $this->medias['total'];
            } else {
                $this->total_media_count = (int) $max;
            }
        }
 
        $this->has_more_items = $this->medias['has_more_items'];
 
        $this->medias = $this->medias['medias'];
 
        if ( $max ) {
            if ( $max >= count($this->medias) ) {
                $this->media_count = count( $this->medias );
            } else {
                $this->media_count = (int) $max;
            }
        } else {
            $this->media_count = count( $this->medias );
        }
 
        if ( (int) $this->total_media_count && (int) $this->pag_num ) {
            $this->pag_links = paginate_links( array(
                'base'      => add_query_arg( $this->pag_arg, '%#%' ),
                'format'    => '',
                'total'     => ceil( (int) $this->total_media_count / (int) $this->pag_num ),
                'current'   => (int) $this->pag_page,
                'prev_text' => __( '←', 'buddyboss' ),
                'next_text' => __( '→', 'buddyboss' ),
                'mid_size'  => 1,
                'add_args'  => array(),
            ) );
        }
    }
 
    /**
     * Whether there are media items available in the loop.
     *
     * @since BuddyBoss 1.0.0
     *
     * @see bp_has_media()
     *
     * @return bool True if there are items in the loop, otherwise false.
     */
    function has_media() {
        if ( $this->media_count ) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Set up the next media item and iterate index.
     *
     * @since BuddyBoss 1.0.0
     *
     * @return object The next media item to iterate over.
     */
    public function next_media() {
        $this->current_media++;
        $this->media = $this->medias[ $this->current_media ];
 
        return $this->media;
    }
 
    /**
     * Rewind the posts and reset post index.
     *
     * @since BuddyBoss 1.0.0
     */
    public function rewind_medias() {
        $this->current_media = -1;
        if ( $this->media_count > 0 ) {
            $this->media = $this->medias[0];
        }
    }
 
    /**
     * Whether there are media items left in the loop to iterate over.
     *
     * This method is used by {@link bp_media()} as part of the while loop
     * that controls iteration inside the media loop, eg:
     *     while ( bp_media() ) { ...
     *
     * @since BuddyBoss 1.0.0
     *
     * @see bp_media()
     *
     * @return bool True if there are more media items to show,
     *              otherwise false.
     */
    public function user_medias() {
        if ( ( $this->current_media + 1 ) < $this->media_count ) {
            return true;
        } elseif ( ( $this->current_media + 1 ) == $this->media_count ) {
 
            /**
             * Fires right before the rewinding of media posts.
             *
             * @since BuddyBoss 1.1.0
             */
            do_action( 'media_loop_end' );
 
            // Do some cleaning up after the loop.
            $this->rewind_medias();
        }
 
        $this->in_the_loop = false;
 
        return false;
    }
 
    /**
     * Set up the current media item inside the loop.
     *
     * Used by {@link bp_the_media()} to set up the current media item
     * data while looping, so that template tags used during that iteration
     * make reference to the current media item.
     *
     * @since BuddyBoss 1.0.0
     *
     * @see bp_the_media()
     */
    public function the_media() {
 
        $this->in_the_loop = true;
        $this->media       = $this->next_media();
 
        if ( is_array( $this->media ) ) {
            $this->media = (object) $this->media;
        }
 
        // Loop has just started.
        if ( $this->current_media == 0 ) {
 
            /**
             * Fires if the current media item is the first in the activity loop.
             *
             * @since BuddyBoss 1.1.0
             */
            do_action('media_loop_start');
        }
    }
}

Changelog

Changelog
Version Description
BuddyPress 1.0.0 Introduced.

Methods

  • __construct — Constructor method.
  • has_media — Whether there are media items available in the loop.
  • next_media — Set up the next media item and iterate index.
  • rewind_medias — Rewind the posts and reset post index.
  • the_media — Set up the current media item inside the loop.
  • user_medias — Whether there are media items left in the loop to iterate over.

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.