Skip to content
Permalink
Browse files

Merge pull request #189 from imath/add/additional-fields

Implement additional fields for Activity, Groups, Members & Notificat…
  • Loading branch information...
renatonascalves committed Jun 29, 2019
2 parents 834f97f + d95cac4 commit d9d64f04f2517c02831b6b989dc6b281281e6f20
@@ -396,9 +396,16 @@ public function create_item( $request ) {
)
);
$activity = current( $activity['activities'] );
$fields_update = $this->update_additional_fields_for_object( $activity, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$retval = array(
$this->prepare_response_for_collection(
$this->prepare_item_for_response( current( $activity['activities'] ), $request )
$this->prepare_item_for_response( $activity, $request )
),
);
@@ -496,7 +503,12 @@ public function update_item( $request ) {
);
}
$activity = $this->get_activity_object( $activity_id );
$activity = $this->get_activity_object( $activity_id );
$fields_update = $this->update_additional_fields_for_object( $activity, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$retval = array(
$this->prepare_response_for_collection(
@@ -1179,7 +1191,7 @@ public function get_activity_object( $request ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Activity', 'buddypress' ),
'title' => 'bp_activity',
'type' => 'object',
'properties' => array(
'id' => array(
@@ -1336,7 +1348,7 @@ public function get_item_schema() {
*
* @param string $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_activity_schema', $schema );
return apply_filters( 'bp_rest_activity_schema', $this->add_additional_fields_schema( $schema ) );
}
/**
@@ -393,7 +393,6 @@ public function prepare_item_for_response( $avatar, $request ) {
}
$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 );
@@ -384,7 +384,6 @@ public function prepare_item_for_response( $avatar, $request ) {
}
$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 );
@@ -292,7 +292,12 @@ public function create_item( $request ) {
);
}
$group = $this->get_group_object( $group_id );
$group = $this->get_group_object( $group_id );
$fields_update = $this->update_additional_fields_for_object( $group, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$retval = array(
$this->prepare_response_for_collection(
@@ -369,7 +374,12 @@ public function update_item( $request ) {
);
}
$group = $this->get_group_object( $group_id );
$group = $this->get_group_object( $group_id );
$fields_update = $this->update_additional_fields_for_object( $group, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$retval = array(
$this->prepare_response_for_collection(
@@ -844,7 +854,7 @@ public function get_group_object( $request ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Group', 'buddypress' ),
'title' => 'bp_groups',
'type' => 'object',
'properties' => array(
'id' => array(
@@ -995,7 +1005,7 @@ public function get_item_schema() {
*
* @param array $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_group_schema', $schema );
return apply_filters( 'bp_rest_group_schema', $this->add_additional_fields_schema( $schema ) );
}
/**
@@ -463,7 +463,7 @@ protected function can_manage_member( $user ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Members', 'buddypress' ),
'title' => 'bp_members',
'type' => 'object',
'properties' => array(
'id' => array(
@@ -666,4 +666,27 @@ public function get_collection_params() {
return $params;
}
/**
* Updates the values of additional fields added to a data object.
*
* This function makes sure updating the field value thanks to the `id` property of
* the created/updated object type is consistent accross BuddyPress components.
*
* @since 0.1.0
*
* @param WP_User $object The WordPress user object.
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error True on success, WP_Error object if a field cannot be updated.
*/
protected function update_additional_fields_for_object( $object, $request ) {
if ( isset( $object->data ) ) {
$member = $object->data;
$member->id = $member->ID;
} else {
return new WP_Error( 'invalid_user', __( 'The data for the user was not found.', 'buddypress' ) );
}
return WP_REST_Controller::update_additional_fields_for_object( $member, $request );
}
}
@@ -494,7 +494,6 @@ public function prepare_item_for_response( $thread, $request ) {
// @todo Set user avatar, user name, and user links for recipients.
// @todo What about starred threads, starred messages in a thread ?
$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 );
@@ -290,7 +290,12 @@ public function create_item( $request ) {
);
}
$notification = $this->get_notification_object( $notification_id );
$notification = $this->get_notification_object( $notification_id );
$fields_update = $this->update_additional_fields_for_object( $notification, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$retval = array(
$this->prepare_response_for_collection(
@@ -372,6 +377,12 @@ public function update_item( $request ) {
);
}
$fields_update = $this->update_additional_fields_for_object( $notification, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$retval = array(
$this->prepare_response_for_collection(
$this->prepare_item_for_response( $notification, $request )
@@ -652,7 +663,7 @@ public function get_notification_object( $request ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Notifications', 'buddypress' ),
'title' => 'bp_notifications',
'type' => 'object',
'properties' => array(
'id' => array(
@@ -706,7 +717,7 @@ public function get_item_schema() {
*
* @param array $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_notifications_schema', $schema );
return apply_filters( 'bp_rest_notifications_schema', $this->add_additional_fields_schema( $schema ) );
}
/**
@@ -283,7 +283,6 @@ public function prepare_item_for_response( $field_data, $request ) {
);
$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 );
@@ -541,7 +541,6 @@ public function prepare_item_for_response( $group, $request ) {
}
$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 );
@@ -629,7 +629,6 @@ public function assemble_response_data( $field, $request ) {
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
return $data;
@@ -249,3 +249,44 @@ function bp_rest_get_user( $user_id ) {
return $user;
}
/**
* Registers a new field on an existing BuddyPress object.
*
* @since 0.1.0
*
* @param string $component_id The name of the *active* component (eg: `activity`, `groups`, `xprofile`).
* Required.
* @param string $attribute The attribute name. Required.
* @param array $args {
* Optional. An array of arguments used to handle the registered field.
* @see `register_rest_field()` for a full description.
* }
* @return bool True if the field has been registered successfully. False otherwise.
*/
function bp_rest_register_field( $component_id, $attribute, $args = array() ) {
$registered_fields = false;
if ( ! $component_id || ! bp_is_active( $component_id ) || ! $attribute ) {
return $registered_fields;
}
$args = bp_parse_args(
$args,
array(
'get_callback' => null,
'update_callback' => null,
'schema' => null,
),
'rest_register_field'
);
// Use the `bp_` prefix as we're using a WordPress global used for Post Types.
register_rest_field( 'bp_' . $component_id, $attribute, $args );
if ( isset( $GLOBALS['wp_rest_additional_fields'][ 'bp_' . $component_id ] ) ) {
$registered_fields = $GLOBALS['wp_rest_additional_fields'][ 'bp_' . $component_id ];
}
return isset( $registered_fields[ $attribute ] );
}
@@ -920,4 +920,88 @@ public function test_context_param() {
$this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertEquals( array( 'view', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
public function update_additional_field( $value, $data, $attribute ) {
return bp_activity_update_meta( $data->id, '_' . $attribute, $value );
}
public function get_additional_field( $data, $attribute ) {
return bp_activity_get_meta( $data['id'], '_' . $attribute );
}
/**
* @group additional_fields
*/
public function test_additional_fields() {
$registered_fields = $GLOBALS['wp_rest_additional_fields'];
bp_rest_register_field( 'activity', 'foo_field', array(
'get_callback' => array( $this, 'get_additional_field' ),
'update_callback' => array( $this, 'update_additional_field' ),
'schema' => array(
'description' => 'Activity Meta Field',
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );
$this->bp->set_current_user( $this->user );
$expected = 'bar_value';
// POST
$request = new WP_REST_Request( 'POST', $this->endpoint_url );
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
$params = $this->set_activity_data( array( 'foo_field' => $expected ) );
$request->set_body_params( $params );
$request->set_param( 'context', 'edit' );
$response = $this->server->dispatch( $request );
$create_data = $response->get_data();
$this->assertTrue( $expected === $create_data[0]['foo_field'] );
// GET
$request = new WP_REST_Request( 'GET', sprintf( $this->endpoint_url . '/%d', $create_data[0]['id'] ) );
$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );
$get_data = $response->get_data();
$this->assertTrue( $expected === $get_data[0]['foo_field'] );
$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
}
/**
* @group additional_fields
*/
public function test_update_additional_fields() {
$registered_fields = $GLOBALS['wp_rest_additional_fields'];
bp_rest_register_field( 'activity', 'bar_field', array(
'get_callback' => array( $this, 'get_additional_field' ),
'update_callback' => array( $this, 'update_additional_field' ),
'schema' => array(
'description' => 'Activity Meta Field',
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );
$this->bp->set_current_user( $this->user );
$expected = 'foo_value';
$a_id = $this->bp_factory->activity->create();
// PUT
$request = new WP_REST_Request( 'PUT', sprintf( $this->endpoint_url . '/%d', $a_id ) );
$request->add_header( 'content-type', 'application/json' );
$params = $this->set_activity_data( array( 'bar_field' => 'foo_value' ) );
$request->set_body( wp_json_encode( $params ) );
$request->set_param( 'context', 'edit' );
$response = $this->server->dispatch( $request );
$update_data = $response->get_data();
$this->assertTrue( $expected === $update_data[0]['bar_field'] );
$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
}
}

0 comments on commit d9d64f0

Please sign in to comment.
You can’t perform that action at this time.