BP_Blogs_Template

The main blog template loop class.

Description

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

Source

File: bp-blogs/classes/class-bp-blogs-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
class BP_Blogs_Template {
 
    /**
     * The loop iterator.
     *
     * @var int
     */
    public $current_blog = -1;
 
    /**
     * The number of blogs returned by the paged query.
     *
     * @var int
     */
    public $blog_count = 0;
 
    /**
     * Array of blogs located by the query..
     *
     * @var array
     */
    public $blogs = array();
 
    /**
     * The blog object currently being iterated on.
     *
     * @var object
     */
    public $blog;
 
    /**
     * A flag for whether the loop is currently being iterated.
     *
     * @var bool
     */
    public $in_the_loop = false;
 
    /**
     * The page number being requested.
     *
     * @var int
     */
    public $pag_page = 1;
 
    /**
     * The number of items being requested per page.
     *
     * @var int
     */
    public $pag_num = 20;
 
    /**
     * An HTML string containing pagination links.
     *
     * @var string
     */
    public $pag_links = '';
 
    /**
     * The total number of blogs matching the query parameters.
     *
     * @var int
     */
    public $total_blog_count = 0;
 
    /**
     * Constructor method.
     *
     * @see BP_Blogs_Blog::get() for a description of parameters.
     *
     * @param string     $type              See {@link BP_Blogs_Blog::get()}.
     * @param string     $page              See {@link BP_Blogs_Blog::get()}.
     * @param string     $per_page          See {@link BP_Blogs_Blog::get()}.
     * @param string     $max               See {@link BP_Blogs_Blog::get()}.
     * @param string     $user_id           See {@link BP_Blogs_Blog::get()}.
     * @param string     $search_terms      See {@link BP_Blogs_Blog::get()}.
     * @param string     $page_arg          The string used as a query parameter in
     *                                      pagination links. Default: 'bpage'.
     * @param bool       $update_meta_cache Whether to pre-fetch metadata for
     *                                      queried blogs.
     * @param array|bool $include_blog_ids  Array of blog IDs to include.
     */
    public function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true, $include_blog_ids = false ) {
 
        $this->pag_arg  = sanitize_key( $page_arg );
        $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $page     );
        $this->pag_num  = bp_sanitize_pagination_arg( 'num',          $per_page );
 
        // Backwards compatibility support for blogs by first letter.
        if ( ! empty( $_REQUEST['letter'] ) ) {
            $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
 
        // Typical blogs query.
        } else {
            $this->blogs = bp_blogs_get_blogs( array(
                'type'              => $type,
                'per_page'          => $this->pag_num,
                'page'              => $this->pag_page,
                'user_id'           => $user_id,
                'search_terms'      => $search_terms,
                'update_meta_cache' => $update_meta_cache,
                'include_blog_ids'  => $include_blog_ids,
            ) );
        }
 
        // Set the total blog count.
        if ( empty( $max ) || ( $max >= (int) $this->blogs['total'] ) ) {
            $this->total_blog_count = (int) $this->blogs['total'];
        } else {
            $this->total_blog_count = (int) $max;
        }
 
        // Set the blogs array (to loop through later.
        $this->blogs = $this->blogs['blogs'];
 
        // Get the current blog count to compare maximum against.
        $blog_count = count( $this->blogs );
 
        // Set the current blog count.
        if ( empty( $max ) || ( $max >= (int) $blog_count ) ) {
            $this->blog_count = (int) $blog_count;
        } else {
            $this->blog_count = (int) $max;
        }
 
        // Build pagination links based on total blogs and current page number.
        if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) {
            $this->pag_links = paginate_links( array(
                'base'      => add_query_arg( $this->pag_arg, '%#%' ),
                'format'    => '',
                'total'     => ceil( (int) $this->total_blog_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 blogs available in the loop.
     *
     * @see bp_has_blogs()
     *
     * @return bool True if there are items in the loop, otherwise false.
     */
    public function has_blogs() {
        return (bool) ! empty( $this->blog_count );
    }
 
    /**
     * Set up the next blog and iterate index.
     *
     * @return object The next blog to iterate over.
     */
    public function next_blog() {
        $this->current_blog++;
        $this->blog = $this->blogs[ $this->current_blog ];
 
        return $this->blog;
    }
 
    /**
     * Rewind the blogs and reset blog index.
     */
    public function rewind_blogs() {
        $this->current_blog = -1;
        if ( $this->blog_count > 0 ) {
            $this->blog = $this->blogs[0];
        }
    }
 
    /**
     * Whether there are blogs left in the loop to iterate over.
     *
     * This method is used by {@link bp_blogs()} as part of the while loop
     * that controls iteration inside the blogs loop, eg:
     *     while ( bp_blogs() ) { ...
     *
     * @see bp_blogs()
     *
     * @return bool True if there are more blogs to show, otherwise false.
     */
    public function blogs() {
        if ( ( $this->current_blog + 1 ) < $this->blog_count ) {
            return true;
        } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) {
 
            /**
             * Fires right before the rewinding of blogs listing after all are shown.
             *
             * @since BuddyPress 1.5.0
             */
            do_action( 'blog_loop_end' );
            // Do some cleaning up after the loop.
            $this->rewind_blogs();
        }
 
        $this->in_the_loop = false;
        return false;
    }
 
    /**
     * Set up the current blog inside the loop.
     *
     * Used by {@link bp_the_blog()} to set up the current blog data while
     * looping, so that template tags used during that iteration make
     * reference to the current blog.
     *
     * @see bp_the_blog()
     */
    public function the_blog() {
 
        $this->in_the_loop = true;
        $this->blog        = $this->next_blog();
 
        // Loop has just started.
        if ( 0 === $this->current_blog ) {
 
            /**
             * Fires if on the first blog in the loop.
             *
             * @since BuddyPress 1.5.0
             */
            do_action( 'blog_loop_start' );
        }
    }
}

Methods

  • __construct — Constructor method.
  • blogs — Whether there are blogs left in the loop to iterate over.
  • has_blogs — Whether there are blogs available in the loop.
  • next_blog — Set up the next blog and iterate index.
  • rewind_blogs — Rewind the blogs and reset blog index.
  • the_blog — Set up the current blog 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.