BP_REST_Reply_Endpoint::get_items( WP_REST_Request $request )

Retrieve Replies.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

File: bp-forums/classes/class-bp-rest-reply-endpoint.php

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
public function get_items( $request ) {
 
    $args = array(
        'post_parent'    => ( ! empty( $request['parent'] ) ? $request['parent'] : 'any' ),
        'orderby'        => ( ! empty( $request['orderby'] ) ? $request['orderby'] : 'date' ),
        'order'          => ( ! empty( $request['order'] ) ? $request['order'] : 'ASC' ),
        'paged'          => ( ! empty( $request['page'] ) ? $request['page'] : '' ),
        'posts_per_page' => ( ! empty( $request['per_page'] ) ? $request['per_page'] : '' ),
    );
 
    if ( ! empty( $request['search'] ) ) {
        $args['s'] = $this->topic_endpoint->bbp_sanitize_search_request( $request['search'] );
    }
 
    if ( ! empty( $request['author'] ) ) {
        $args['author'] = $request['author'];
    }
 
    if ( ! empty( $request['author_exclude'] ) ) {
        $args['author__not_in'] = $request['author_exclude'];
    }
 
    if ( ! empty( $request['exclude'] ) ) {
        $args['post__not_in'] = $request['exclude'];
    }
 
    if ( ! empty( $request['include'] ) ) {
        $args['post__in'] = $request['include'];
    }
 
    if ( ! empty( $request['offset'] ) ) {
        $args['offset'] = $request['offset'];
    }
 
    if ( isset( $request['thread_replies'] ) ) {
        $thread_replies = (bool) $request['thread_replies'];
    } else {
        $thread_replies = (bool) ( bbp_thread_replies() );
    }
 
    /**
     * Filter the query arguments for the request.
     *
     * @param array           $args    Key value array of query var to query value.
     * @param WP_REST_Request $request The request sent to the API.
     *
     * @since 0.1.0
     */
    $args = apply_filters( 'bp_rest_reply_get_items_query_args', $args, $request );
 
    $default_thread_replies = (bool) ( bbp_thread_replies() );
 
    $default = array(
        'post_type'                => bbp_get_reply_post_type(),
        'ignore_sticky_posts'      => true,
        'max_num_pages'            => false,
        'hierarchical'             => $default_thread_replies,
        'update_post_term_cache'   => false,
 
        // Conditionally prime the cache for related posts.
        'update_post_family_cache' => true,
    );
 
    // What are the default allowed statuses (based on user caps).
    if ( bbp_get_view_all( 'edit_others_replies' ) ) {
 
        // Default view=all statuses.
        $post_statuses = array_keys( bbp_get_topic_statuses() );
 
        // Add support for private status.
        if ( current_user_can( 'read_private_replies' ) ) {
            $post_statuses[] = bbp_get_private_status_id();
        }
 
        // Join post statuses together.
        $default['post_status'] = $post_statuses;
 
        // Lean on the 'perm' query var value of 'readable' to provide statuses.
    } else {
        $default['perm'] = 'readable';
    }
 
    // Parse arguments against default values.
    $r = bbp_parse_args( $args, $default, 'has_replies' );
 
    $replies_per_page = $r['posts_per_page'];
 
    // Set posts_per_page value if replies are threaded.
    if ( true === $r['hierarchical'] && true === $thread_replies ) {
        $r['posts_per_page'] = - 1;
        $replies_per_page    = $r['posts_per_page'];
    }
 
    if ( $r['hierarchical'] && empty( $r['s'] ) && true === $thread_replies ) {
        $r['page'] = 1;
        // Run the query.
        $replies_query = new WP_Query( $r );
 
        if ( ! empty( $replies_query->posts ) ) {
            foreach ( $replies_query->posts as $k => $v ) {
                $replies_query->posts[ $k ]->reply_to = (int) get_post_meta( $v->ID, '_bbp_reply_to', true );
            }
        }
 
        // Parse arguments.
        $walk_arg = bbp_parse_args(
            array(),
            array(
                'walker'       => '',
                'max_depth'    => bbp_thread_replies_depth(),
                'callback'     => null,
                'end_callback' => null,
                'page'         => $r['page'],
                'per_page'     => $r['posts_per_page'],
            ),
            'list_replies'
        );
 
        global $buddyboss_thread_reply;
        $buddyboss_thread_reply = array();
 
        $this->bbb_walker_reply->paged_walk( $replies_query->posts, $walk_arg['max_depth'], $walk_arg['page'], $walk_arg['per_page'], $walk_arg );
        $total_parent_replies       = $this->bbb_walker_reply->get_number_of_root_elements( $replies_query->posts );
        $replies_query->posts       = $buddyboss_thread_reply;
        $replies_query->found_posts = $total_parent_replies;
 
    } else {
        // Run the query.
        $replies_query = new WP_Query( $r );
    }
 
    if ( false === $thread_replies && bbp_thread_replies() ) {
        if ( ! empty( $replies_query->posts ) ) {
            foreach ( $replies_query->posts as $k => $v ) {
                $replies_query->posts[ $k ]->reply_to = (int) get_post_meta( $v->ID, '_bbp_reply_to', true );
            }
        }
 
        $r['page'] = 1;
 
        // Parse arguments.
        $walk_arg = bbp_parse_args(
            array(),
            array(
                'walker'       => '',
                'max_depth'    => bbp_thread_replies_depth(),
                'callback'     => null,
                'end_callback' => null,
                'page'         => $r['page'],
                'per_page'     => $replies_per_page,
            ),
            'list_replies'
        );
 
        global $buddyboss_thread_reply;
        $buddyboss_thread_reply = array();
 
        $this->bbb_walker_reply->paged_walk( $replies_query->posts, $walk_arg['max_depth'], $walk_arg['page'], $walk_arg['per_page'], $walk_arg );
        $total_parent_replies       = $replies_query->found_posts;
        $replies_query->posts       = $buddyboss_thread_reply;
        $replies_query->found_posts = $total_parent_replies;
    }
 
    $replies = ( ! empty( $replies_query->posts ) ? $replies_query->posts : array() );
 
    $retval = array();
    foreach ( $replies as $reply ) {
        $retval[] = $this->prepare_response_for_collection(
            $this->prepare_item_for_response( $reply, $request )
        );
    }
 
    $response = rest_ensure_response( $retval );
    $response = bp_rest_response_add_total_headers( $response, $replies_query->found_posts, $replies_per_page );
 
    /**
     * Fires after a list of replies is fetched via the REST API.
     *
     * @param array            $replies  Fetched Replied.
     * @param WP_REST_Response $response The response data.
     * @param WP_REST_Request  $request  The request sent to the API.
     *
     * @since 0.1.0
     */
    do_action( 'bp_rest_replies_get_items', $replies, $response, $request );
 
    return $response;
}

Changelog

Changelog
Version Description
0.1.0 Introduced.

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.