BP_REST_Activity_Endpoint::update_favorite( WP_REST_Request $request )
Adds or removes the activity from the current user’s favorites.
Description
Parameters
- $request
-
(Required) Full details about the request.
Return
(WP_REST_Response) | WP_Error
Source
File: bp-activity/classes/class-bp-rest-activity-endpoint.php
public function update_favorite( $request ) { $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, ) ); } $user_id = get_current_user_id(); $result = false; if ( in_array( $activity->id, $this->get_user_favorites(), true ) ) { $result = bp_activity_remove_user_favorite( $activity->id, $user_id ); $message = __( 'Sorry, you cannot remove the activity from your favorites.', 'buddyboss' ); // Update the user favorites, removing the activity ID. $this->user_favorites = array_diff( $this->get_user_favorites(), array( $activity->id ) ); } else { $result = bp_activity_add_user_favorite( $activity->id, $user_id ); $message = __( 'Sorry, you cannot add the activity to your favorites.', 'buddyboss' ); // Update the user favorites, adding the activity ID. $this->user_favorites[] = (int) $activity->id; } if ( ! $result ) { return new WP_Error( 'bp_rest_user_cannot_update_activity_favorite', $message, array( 'status' => 500, ) ); } // Setting context. $request->set_param( 'context', 'edit' ); // Prepare the response now the user favorites has been updated. $retval = $this->prepare_response_for_collection( $this->prepare_item_for_response( $activity, $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after user favorited activities has been updated via the REST API. * * @param BP_Activity_Activity $activity The updated activity. * @param array $user_favorites The updated user favorites. * @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_update_favorite', $activity, $this->get_user_favorites(), $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.