BP_REST_Reply_Endpoint::get_items( WP_REST_Request $request )
Retrieve Replies.
Description
- from bbp_has_replies().
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
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
| 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.