BP_REST_Activity_Comment_Endpoint

Activity endpoints.

Description

Source

File: bp-activity/classes/class-bp-rest-activity-comment-endpoint.php

16
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
class BP_REST_Activity_Comment_Endpoint extends WP_REST_Controller {
 
    /**
     * Activity endpoints class.
     *
     * @since 0.1.0
     *
     * @var BP_REST_Activity_Endpoint
     */
    protected $activity_endpoint;
 
    /**
     * Constructor.
     *
     * @since 0.1.0
     */
    public function __construct() {
        $this->namespace         = bp_rest_namespace() . '/' . bp_rest_version();
        $this->rest_base         = buddypress()->activity->id;
        $this->activity_endpoint = new BP_REST_Activity_Endpoint();
    }
 
    /**
     * Register the component routes.
     *
     * @since 0.1.0
     */
    public function register_routes() {
        $activity_endpoint = '/' . $this->rest_base . '/(?P<id>[\d]+)';
 
        register_rest_route(
            $this->namespace,
            $activity_endpoint . '/comment',
            array(
                array(
                    'methods'             => WP_REST_Server::READABLE,
                    'callback'            => array( $this, 'get_items' ),
                    'permission_callback' => array( $this, 'get_items_permissions_check' ),
                    'args'                => $this->get_collection_params(),
                ),
                array(
                    'methods'             => WP_REST_Server::CREATABLE,
                    'callback'            => array( $this, 'create_item' ),
                    'permission_callback' => array( $this, 'create_item_permissions_check' ),
                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
                ),
                'schema' => array( $this, 'get_item_schema' ),
            )
        );
    }
 
    /**
     * Retrieve activity comments.
     *
     * @since 0.1.0
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return WP_REST_Response | WP_Error
     * @api            {GET} /wp-json/buddyboss/v1/activity/:id/comment Get activity comments
     * @apiName        GetActivityComment
     * @apiGroup       Activity
     * @apiDescription Get all comments for an activity.
     * @apiVersion     1.0.0
     * @apiPermission  LoggedInUser if the site is in Private Network.
     * @apiParam {Number} id A unique numeric ID for the activity.
     * @apiParam {String=threaded,stream,false} [display_comments=threaded] Comments by default, stream for within stream display, threaded for below each activity item.
     */
    public function get_items( $request ) {
 
        $retval = array();
 
        $activity = $this->get_activity_object( $request );
 
        if ( empty( $activity->id ) ) {
            return new WP_Error(
                'bp_rest_invalid_id',
                __( 'Invalid activity ID.', 'buddyboss' ),
                array(
                    'status' => 404,
                )
            );
        }
 
        if ( ! empty( $activity->children ) ) {
            $request->set_param( 'context', 'view' );
            $retval['comments'] = $this->prepare_activity_comments( $activity->children, $request );
        }
 
        $response = rest_ensure_response( $retval );
 
        /**
         * Fires after a list of activity details is fetched via the REST API.
         *
         * @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_activity_comment_get_items', $response, $request );
 
        return $response;
    }
 
    /**
     * Check if a given request has access to activity comment.
     *
     * @param WP_REST_Request $request Full data about the request.
     *
     * @return bool|WP_Error
     * @since 0.1.0
     */
    public function get_items_permissions_check( $request ) {
        $retval = true;
 
        if ( function_exists( 'bp_enable_private_network' ) && true !== bp_enable_private_network() && ! is_user_logged_in() ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, Restrict access to only logged-in members.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        if ( true === $retval && ! bp_is_active( 'activity' ) ) {
            $retval = new WP_Error(
                'bp_rest_component_required',
                __( 'Sorry, Activity component was not enabled.', 'buddyboss' ),
                array(
                    'status' => '404',
                )
            );
        }
 
        /**
         * Filter the activity comment permissions check.
         *
         * @param bool|WP_Error $retval Returned value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_activity_comment_get_items_permissions_check', $retval, $request );
    }
 
    /**
     * Create an activity comment.
     *
     * @param WP_REST_Request $request Full data about the request.
     *
     * @return WP_REST_Response | WP_Error
     * @since 0.1.0
     *
     * @api {POST} /wp-json/buddyboss/v1/activity/:id/comment Create activity comment
     * @apiName CreateActivityComment
     * @apiGroup Activity
     * @apiDescription Create comment under activity.
     * @apiVersion 1.0.0
     * @apiPermission LoggedInUser
     * @apiParam {Number} id A unique numeric ID for the activity.
     * @apiParam {Number} [parent_id] ID of the parent activity/comment item.
     * @apiParam {String} content The content of the comment.
     * @apiParam {String=threaded,stream,false} [display_comments=threaded] Comments by default, stream for within stream display, threaded for below each activity item.
     */
    public function create_item( $request ) {
 
        if ( true === $this->activity_endpoint->bp_rest_activity_content_validate( $request ) ) {
            return new WP_Error(
                'bp_rest_comment_blank_content',
                __( 'Please do not leave the comment area blank.', 'buddyboss' ),
                array(
                    'status' => 400,
                )
            );
        }
 
        if ( empty( $request['parent_id'] ) ) {
            $request['parent_id'] = '';
        }
 
        if ( empty( $request['content'] ) ) {
            $request['content'] = '&#8203;';
        }
 
        $comment_id = bp_activity_new_comment(
            array(
                'activity_id' => $request['id'],
                'content'     => $request['content'],
                'parent_id'   => $request['parent_id'],
            )
        );
 
        if ( is_wp_error( $comment_id ) ) {
            return new WP_Error(
                'comment_error',
                esc_html( $comment_id->get_error_message() ),
                array(
                    'status' => 500,
                )
            );
        }
 
        $activity_comment = new BP_Activity_Activity( $comment_id );
 
        $fields_update = $this->update_additional_fields_for_object( $activity_comment, $request );
 
        if ( is_wp_error( $fields_update ) ) {
            return $fields_update;
        }
 
        $retval            = array();
        $retval['created'] = true;
 
        if ( empty( $request['display_comments'] ) ) {
            $request->set_param( 'display_comments', 'threaded' );
        }
 
        $activity = $this->get_activity_object( $request );
 
        if ( ! empty( $activity->children ) ) {
            $request->set_param( 'context', 'view' );
            $retval['comments'] = $this->prepare_activity_comments( $activity->children, $request );
        }
 
        $response = rest_ensure_response( $retval );
 
        /**
         * Fires after an activity comment is created via the REST API.
         *
         * @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_activity_create_item', $response, $request );
 
        return $response;
    }
 
    /**
     * Checks if a given request has access to create an activity comment.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return bool|WP_Error
     * @since 0.1.0
     */
    public function create_item_permissions_check( $request ) {
        $retval = true;
 
        if ( ! is_user_logged_in() ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you are not allowed to create an activity comment.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        $activity = $this->get_activity_object( $request );
 
        if ( empty( $activity ) || empty( $activity->id ) ) {
            return new WP_Error(
                'bp_rest_invalid_id',
                __( 'Invalid activity ID.', 'buddyboss' ),
                array(
                    'status' => 404,
                )
            );
        }
 
        /**
         * Filter the activity comment `create_item` permissions check.
         *
         * @param bool|WP_Error $retval Returned value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_activity_comment_create_item_permissions_check', $retval, $request );
    }
 
    /**
     * Edit the type of the some properties for the CREATABLE methods.
     *
     * @param string $method Optional. HTTP method of the request.
     *
     * @return array Endpoint arguments.
     * @since 0.1.0
     */
    public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) {
        $args = $this->get_collection_params();
        $key  = 'get_item';
 
        if ( WP_REST_Server::CREATABLE === $method ) {
            $key = 'create_item';
 
            $args['context']['default'] = 'edit';
 
            $args['content'] = array(
                'description'       => __( 'The content for the comment.', 'buddyboss' ),
                'required'          => false,
                'type'              => 'string',
                'validate_callback' => 'rest_validate_request_arg',
            );
 
            $args['parent_id'] = array(
                'description'       => __( 'Parent comment ID.', 'buddyboss' ),
                'type'              => 'integer',
                'validate_callback' => 'rest_validate_request_arg',
                'sanitize_callback' => 'absint',
                'validate_callback' => 'rest_validate_request_arg',
            );
        }
 
        /**
         * Filters the method query arguments.
         *
         * @param array $args Query arguments.
         * @param string $method HTTP method of the request.
         *
         * @since 0.1.0
         */
        return apply_filters( "bp_rest_activity_comment_{$key}_query_arguments", $args, $method );
    }
 
    /**
     * Get the plugin schema, conforming to JSON Schema.
     *
     * @return array
     * @since 0.1.0
     */
    public function get_item_schema() {
        $schema = array(
            '$schema'    => 'http://json-schema.org/draft-04/schema#',
            'title'      => 'activity_comments',
            'type'       => 'object',
            'properties' => array(
                'created'  => array(
                    'context'     => array( 'embed', 'edit' ),
                    'description' => __( 'Whether the comment created or not.', 'buddyboss' ),
                    'type'        => 'boolean',
                    'readonly'    => true,
                ),
                'comments' => array(
                    'context'     => array( 'embed', 'view', 'edit' ),
                    'description' => __( 'A list of comments for activity.', 'buddyboss' ),
                    'type'        => 'array',
                    'readonly'    => true,
                ),
            ),
        );
 
        /**
         * Filters the activity details schema.
         *
         * @param string $schema The endpoint schema.
         */
        return apply_filters( 'bp_rest_activity_comment_details_schema', $this->add_additional_fields_schema( $schema ) );
    }
 
    /**
     * Get the query params for collections of plugins.
     *
     * @return array
     * @since 0.1.0
     */
    public function get_collection_params() {
        $params                       = parent::get_collection_params();
        $params['context']['default'] = 'view';
 
        // Removing unused params.
        unset( $params['search'], $params['page'], $params['per_page'] );
 
        $params['id'] = array(
            'description' => __( 'A unique numeric ID for the activity.', 'buddyboss' ),
            'type'        => 'integer',
            'reqiured'    => true,
        );
 
        $params['display_comments'] = array(
            'description'       => __( 'Comments by default, stream for within stream display, threaded for below each activity item.', 'buddyboss' ),
            'default'           => 'threaded',
            'enum'              => array( 'stream', 'threaded' ),
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_key',
            'validate_callback' => 'rest_validate_request_arg',
        );
 
        /**
         * Filters the collection query params.
         *
         * @param array $params Query params.
         */
        return apply_filters( 'bp_rest_activity_collection_params', $params );
    }
 
    /**
     * Get activity object.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return BP_Activity_Activity|string An activity object.
     * @since 0.1.0
     */
    protected function get_activity_object( $request ) {
        $activity_id      = is_numeric( $request ) ? $request : (int) $request['id'];
        $display_comments = ( array_key_exists( 'display_comments', $request ) ? $request['display_comments'] : true );
        $activity         = bp_activity_get_specific(
            array(
                'activity_ids'     => array( $activity_id ),
                'display_comments' => $display_comments,
            )
        );
 
        if ( is_array( $activity ) && ! empty( $activity['activities'][0] ) ) {
            return $activity['activities'][0];
        }
 
        return '';
    }
 
    /**
     * Prepare activity comments.
     *
     * @param array           $comments Comments.
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return array           An array of activity comments.
     * @since 0.1.0
     */
    protected function prepare_activity_comments( $comments, $request ) {
        $data = array();
 
        if ( empty( $comments ) ) {
            return $data;
        }
 
        foreach ( $comments as $comment ) {
            $data[] = $this->activity_endpoint->prepare_response_for_collection(
                $this->activity_endpoint->prepare_item_for_response( $comment, $request )
            );
        }
 
        /**
         * Filter activity comments returned from the API.
         *
         * @param array $data An array of activity comments.
         * @param array $comments Comments.
         * @param WP_REST_Request $request Request used to generate the response.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_activity_prepare_comments', $data, $comments, $request );
    }
}

Changelog

Changelog
Version Description
0.1.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.