BP_REST_Messages_Endpoint::get_items( WP_REST_Request $request )

Retrieve threads.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

File: bp-messages/classes/class-bp-rest-messages-endpoint.php

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
public function get_items( $request ) {
    $args = array(
        'user_id'      => $request['user_id'],
        'box'          => $request['box'],
        'type'         => $request['type'],
        'page'         => $request['page'],
        'per_page'     => $request['per_page'],
        'search_terms' => $request['search'],
    );
 
    // Include the meta_query for starred messages.
    if ( 'starred' === $args['box'] ) {
        $args['meta_query'] = array( // phpcs:ignore
            array(
                'key'   => 'starred_by_user',
                'value' => $args['user_id'],
            ),
        );
    }
 
    /**
     * 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_messages_get_items_query_args', $args, $request );
 
    add_filter( 'bp_messages_default_per_page', array( $this, 'bp_rest_messages_default_per_page' ) );
 
    // Actually, query it.
    $messages_box = new BP_Messages_Box_Template( $args );
 
    remove_filter( 'bp_messages_default_per_page', array( $this, 'bp_rest_messages_default_per_page' ) );
 
    $retval = array();
    if ( ! empty( $messages_box->threads ) ) {
        foreach ( (array) $messages_box->threads as $thread ) {
            $retval[] = $this->prepare_response_for_collection(
                $this->prepare_item_for_response( $thread, $request )
            );
        }
    }
 
    // Added header for the unread count for box=inbox.
    $response = rest_ensure_response( $retval );
    $response = bp_rest_response_add_total_headers( $response, $messages_box->total_thread_count, $args['per_page'] );
 
    /**
     * Fires after a thread is fetched via the REST API.
     *
     * @param BP_Messages_Box_Template $messages_box Fetched thread.
     * @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_messages_get_items', $messages_box, $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.