BP_Document_Folder_Template

The main document template loop class.

Description

Responsible for loading a group of document folders into a loop for display.

Source

File: bp-document/classes/class-bp-document-folder-template.php

17
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
class BP_Document_Folder_Template {
 
    /**
     * The loop iterator.
     *
     * @since BuddyBoss 1.4.0
     * @var int
     */
    public $current_folder = - 1;
 
    /**
     * The folder count.
     *
     * @since BuddyBoss 1.4.0
     * @var int
     */
    public $folder_count;
 
    /**
     * The total folder count.
     *
     * @since BuddyBoss 1.4.0
     * @var int
     */
    public $total_folder_count;
 
    /**
     * Array of folder located by the query.
     *
     * @since BuddyBoss 1.4.0
     * @var array
     */
    public $folders;
 
    /**
     * The folder object currently being iterated on.
     *
     * @since BuddyBoss 1.4.0
     * @var object
     */
    public $folder;
 
    /**
     * A flag for whether the loop is currently being iterated.
     *
     * @since BuddyBoss 1.4.0
     * @var bool
     */
    public $in_the_loop;
 
    /**
     * URL parameter key for folder pagination. Default: 'acpage'.
     *
     * @since BuddyBoss 1.4.0
     * @var string
     */
    public $pag_arg;
 
    /**
     * The page number being requested.
     *
     * @since BuddyBoss 1.4.0
     * @var int
     */
    public $pag_page;
 
    /**
     * The number of items being requested per page.
     *
     * @since BuddyBoss 1.4.0
     * @var int
     */
    public $pag_num;
 
    /**
     * An HTML string containing pagination links.
     *
     * @since BuddyBoss 1.4.0
     * @var string
     */
    public $pag_links;
 
    /**
     * The displayed user's full name.
     *
     * @since BuddyBoss 1.4.0
     * @var string
     */
    public $full_name;
 
    /**
     * Constructor method.
     * The arguments passed to this class constructor are of the same
     * format as {@link BP_Document_Folder::get()}.
     *
     * @param array $args     {
     *                        Array of arguments. Supports all arguments from
     *                        BP_Document_Folder::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 activity 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.
     * }
     * @see   BP_Document_Folder::get() for a description of the argument
     *        structure, as well as default values.
     * @since BuddyBoss 1.4.0
     */
    public function __construct( $args ) {
 
        $defaults = array(
            'page'         => 1,
            'per_page'     => 20,
            'page_arg'     => 'acpage',
            'max'          => false,
            'user_id'      => false,
            'fields'       => 'all',
            'count_total'  => false,
            'sort'         => false,
            'include'      => false,
            'exclude'      => false,
            'privacy'      => false,
            'search_terms' => 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 folder.
        $this->my_favs = bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_folder', true );
 
        // Fetch specific folder items based on ID's.
        if ( ! empty( $include ) ) {
            $this->folders = bp_folder_get_specific(
                array(
                    'folder_ids'  => explode( ',', $include ),
                    'max'         => $max,
                    'count_total' => $count_total,
                    'page'        => $this->pag_page,
                    'per_page'    => $this->pag_num,
                    'sort'        => $sort,
                    'user_id'     => $user_id,
                )
            );
 
            // Fetch all folder.
        } else {
            $this->folders = bp_folder_get(
                array(
                    'max'          => $max,
                    'count_total'  => $count_total,
                    'per_page'     => $this->pag_num,
                    'page'         => $this->pag_page,
                    'sort'         => $sort,
                    'search_terms' => $search_terms,
                    'user_id'      => $user_id,
                    'group_id'     => $group_id,
                    'exclude'      => $exclude,
                    'privacy'      => $privacy,
                )
            );
        }
 
        // The total_folder_count property will be set only if a
        // 'count_total' query has taken place.
        if ( ! is_null( $this->folders['total'] ) ) {
            if ( ! $max || $max >= (int) $this->folders['total'] ) {
                $this->total_folder_count = (int) $this->folders['total'];
            } else {
                $this->total_folder_count = (int) $max;
            }
        }
 
        $this->has_more_items = $this->folders['has_more_items'];
 
        $this->folders = $this->folders['folders'];
 
        if ( $max ) {
            if ( $max >= count( $this->folders ) ) {
                $this->folder_count = count( $this->folders );
            } else {
                $this->folder_count = (int) $max;
            }
        } else {
            $this->folder_count = count( $this->folders );
        }
 
        if ( (int) $this->total_folder_count && (int) $this->pag_num ) {
            $this->pag_links = paginate_links(
                array(
                    'base'      => add_query_arg( $this->pag_arg, '%#%' ),
                    'format'    => '',
                    'total'     => ceil( (int) $this->total_folder_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 folder items available in the loop.
     *
     * @return bool True if there are items in the loop, otherwise false.
     * @see   bp_has_folders()
     * @since BuddyBoss 1.4.0
     */
    function has_folders() {
        if ( $this->folder_count ) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Whether there are folder items left in the loop to iterate over.
     * This method is used by {@link bp_folders()} as part of the while loop
     * that controls iteration inside the folder loop, eg:
     *     while ( bp_folders() ) { ...
     *
     * @return bool True if there are more folder items to show,
     *              otherwise false.
     * @see   bp_folders()
     * @since BuddyBoss 1.4.0
     */
    public function user_folders() {
        if ( ( $this->current_folder + 1 ) < $this->folder_count ) {
            return true;
        } elseif ( ( $this->current_folder + 1 ) == $this->folder_count ) {
 
            /**
             * Fires right before the rewinding of folder posts.
             *
             * @since BuddyBoss 1.4.0
             */
            do_action( 'folder_loop_end' );
 
            // Do some cleaning up after the loop.
            $this->rewind_folders();
        }
 
        $this->in_the_loop = false;
 
        return false;
    }
 
    /**
     * Rewind the posts and reset post index.
     *
     * @since BuddyBoss 1.4.0
     */
    public function rewind_folders() {
        $this->current_folder = - 1;
        if ( $this->folder_count > 0 ) {
            $this->folder = $this->folders[0];
        }
    }
 
    /**
     * Set up the current folder item inside the loop.
     * Used by {@link bp_the_folder()} to set up the current folder item
     * data while looping, so that template tags used during that iteration
     * make reference to the current folder item.
     *
     * @since BuddyBoss 1.4.0
     * @see   bp_the_folder()
     */
    public function the_folder() {
 
        $this->in_the_loop = true;
        $this->folder      = $this->next_folder();
 
        if ( is_array( $this->folder ) ) {
            $this->folder = (object) $this->folder;
        }
 
        // Loop has just started.
        if ( $this->current_folder == 0 ) {
 
            /**
             * Fires if the current folder item is the first in the activity loop.
             *
             * @since BuddyBoss 1.4.0
             */
            do_action( 'folder_loop_start' );
        }
    }
 
    /**
     * Set up the next folder item and iterate index.
     *
     * @return object The next folder item to iterate over.
     * @since BuddyBoss 1.4.0
     */
    public function next_folder() {
        $this->current_folder ++;
        $this->folder = $this->folders[ $this->current_folder ];
 
        return $this->folder;
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.4.0 Introduced.

Methods

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.