BP_REST_Activity_Endpoint::prepare_item_for_response( BP_Activity_Activity $activity, WP_REST_Request $request )
Prepares activity data for return as an object.
Description
Parameters
- $activity
-
(Required) Activity data.
- $request
-
(Required) Full details about the request.
Return
(WP_REST_Response)
Source
File: bp-activity/classes/class-bp-rest-activity-endpoint.php
public function prepare_item_for_response( $activity, $request ) {
$top_level_parent_id = 'activity_comment' === $activity->type ? $activity->item_id : 0;
global $activities_template;
$activities_template = new \stdClass();
$activities_template->disable_blogforum_replies = (bool) bp_core_get_root_option( 'bp-disable-blogforum-comments' );
$activities_template->activity = $activity;
$data = array(
'user_id' => $activity->user_id,
'name' => bp_core_get_user_displayname( $activity->user_id ),
'component' => $activity->component,
'content' => array(
'raw' => $activity->content,
'rendered' => $this->render_item( $activity ),
),
'date' => bp_rest_prepare_date_response( $activity->date_recorded ),
'id' => $activity->id,
'link' => bp_activity_get_permalink( $activity->id ),
'primary_item_id' => $activity->item_id,
'secondary_item_id' => $activity->secondary_item_id,
'status' => $activity->is_spam ? 'spam' : 'published',
'title' => $activity->action,
'type' => $activity->type,
'favorited' => in_array( $activity->id, $this->get_user_favorites(), true ),
// extend response.
'can_favorite' => bp_activity_can_favorite(),
'favorite_count' => $this->get_activity_favorite_count( $activity->id ),
'can_comment' => ( 'activity_comment' === $activity->type ) ? bp_activity_can_comment_reply( $activity ) : bp_activity_can_comment(),
'can_delete' => bp_activity_user_can_delete( $activity ),
'content_stripped' => html_entity_decode( wp_strip_all_tags( $activity->content ) ),
'privacy' => ( isset( $activity->privacy ) ? $activity->privacy : false ),
);
// Get item schema.
$schema = $this->get_item_schema();
// Get comments (count).
if ( ! empty( $activity->children ) ) {
$comment_count = wp_filter_object_list( $activity->children, array( 'type' => 'activity_comment' ), 'AND', 'id' );
$data['comment_count'] = ! empty( $comment_count ) ? count( $comment_count ) : 0;
if ( ! empty( $schema['properties']['comments'] ) && 'threaded' === $request['display_comments'] ) {
$data['comments'] = $this->prepare_activity_comments( $activity->children, $request );
}
} else {
$activities = BP_Activity_Activity::get_activity_comments( $activity->id, $activity->mptt_left, $activity->mptt_right, $request['status'], $top_level_parent_id );
$data['comment_count'] = ! empty( $activities ) ? count( $activities ) : 0;
}
if ( ! empty( $schema['properties']['user_avatar'] ) ) {
$data['user_avatar'] = array(
'full' => bp_core_fetch_avatar(
array(
'item_id' => $activity->user_id,
'html' => false,
'type' => 'full',
)
),
'thumb' => bp_core_fetch_avatar(
array(
'item_id' => $activity->user_id,
'html' => false,
)
),
);
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $activity ) );
/**
* Filter an activity value returned from the API.
*
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request Request used to generate the response.
* @param BP_Activity_Activity $activity The activity object.
*
* @since 0.1.0
*/
return apply_filters( 'bp_rest_activity_prepare_value', $response, $request, $activity );
}
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.