Skip to content
Permalink
Browse files

Adding hooks to prepare links and other improvements

  • Loading branch information...
renatonascalves committed Jul 13, 2019
1 parent f574c69 commit 10b85cfd6fabedc695871288eebd6fd0b130c409
@@ -1071,8 +1071,8 @@ protected function prepare_item_for_database( $request ) {
*
* @since 0.1.0
*
* @param object $activity Activity object.
* @return array Links for the given plugin.
* @param BP_Activity_Activity $activity Activity object.
* @return array
*/
protected function prepare_links( $activity ) {
$base = sprintf( '/%s/%s/', $this->namespace, $this->rest_base );
@@ -1113,7 +1113,15 @@ protected function prepare_links( $activity ) {
);
}
return $links;
/**
* Filter links prepared for the REST response.
*
* @since 0.1.0
*
* @param array $links The prepared links of the REST response.
* @param BP_Activity_Activity $activity Activity object.
*/
return apply_filters( 'bp_rest_activity_prepare_links', $links, $activity );
}
/**
@@ -1138,7 +1146,7 @@ protected function can_see( $request ) {
*
* @param string $component The activity component.
* @param int $item_id The activity item ID.
* @return boolean
* @return bool
*/
protected function show_hidden( $component, $item_id ) {
$user_id = get_current_user_id();
@@ -1479,6 +1487,11 @@ public function get_collection_params() {
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
/**
* Filters the collection query params.
*
* @param array $params Query params.
*/
return apply_filters( 'bp_rest_activity_collection_params', $params );
}
}
@@ -393,8 +393,10 @@ 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 );
// @todo add prepare_links
$response = rest_ensure_response( $data );
/**
@@ -404,8 +406,9 @@ public function prepare_item_for_response( $avatar, $request ) {
*
* @param WP_REST_Response $response Response.
* @param WP_REST_Request $request Request used to generate the response.
* @param stdClass|string $avatar Avatar object or string with url or image with html.
*/
return apply_filters( 'bp_rest_attachments_group_avatar_prepare_value', $response, $request );
return apply_filters( 'bp_rest_attachments_group_avatar_prepare_value', $response, $request, $avatar );
}
/**
@@ -418,7 +421,7 @@ public function prepare_item_for_response( $avatar, $request ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Group Avatar', 'buddypress' ),
'title' => 'bp_attachments_group_avatar',
'type' => 'object',
'properties' => array(
'full' => array(
@@ -441,7 +444,7 @@ public function get_item_schema() {
*
* @param string $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_group_avatar_schema', $schema );
return apply_filters( 'bp_rest_attachments_group_avatar_schema', $this->add_additional_fields_schema( $schema ) );
}
/**
@@ -480,6 +483,11 @@ public function get_item_collection_params() {
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
/**
* Filters the item collection query params.
*
* @param array $params Query params.
*/
return apply_filters( 'bp_rest_attachments_group_avatar_collection_params', $params );
}
}
@@ -384,8 +384,10 @@ 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 );
// @todo add prepare_links
$response = rest_ensure_response( $data );
/**
@@ -395,12 +397,13 @@ public function prepare_item_for_response( $avatar, $request ) {
*
* @param WP_REST_Response $response Response.
* @param WP_REST_Request $request Request used to generate the response.
* @param stdClass|string $avatar Avatar object or string with url or image with html.
*/
return apply_filters( 'bp_rest_attachments_avatar_prepare_value', $response, $request );
return apply_filters( 'bp_rest_attachments_avatar_prepare_value', $response, $request, $avatar );
}
/**
* Get the plugin schema, conforming to JSON Schema.
* Get the member avatar schema, conforming to JSON Schema.
*
* @since 0.1.0
*
@@ -409,7 +412,7 @@ public function prepare_item_for_response( $avatar, $request ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Member Avatar', 'buddypress' ),
'title' => 'bp_attachments_avatar',
'type' => 'object',
'properties' => array(
'full' => array(
@@ -432,7 +435,7 @@ public function get_item_schema() {
*
* @param string $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_member_avatar_schema', $schema );
return apply_filters( 'bp_rest_attachments_avatar_schema', $this->add_additional_fields_schema( $schema ) );
}
/**
@@ -479,6 +482,11 @@ public function get_item_collection_params() {
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
/**
* Filters the item collection query params.
*
* @param array $params Query params.
*/
return apply_filters( 'bp_rest_attachments_avatar_collection_params', $params );
}
}
@@ -287,7 +287,7 @@ public function update_item_permissions_check( $request ) {
*
* @since 0.1.0
*
* @param array $component Component.
* @param array $component The component and its values.
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response
*/
@@ -296,6 +296,7 @@ public function prepare_item_for_response( $component, $request ) {
$data = $this->add_additional_fields_to_object( $component, $request );
$data = $this->filter_response_by_context( $data, $context );
// @todo add prepare_links
$response = rest_ensure_response( $data );
/**
@@ -315,14 +316,12 @@ public function prepare_item_for_response( $component, $request ) {
*
* @since 0.1.0
*
* @param string $id Component id.
* @param string $name Component name.
* @return string
*/
protected function verify_component_status( $id ) {
$active = __( 'active', 'buddypress' );
if ( 'core' === $id || bp_is_active( $id ) ) {
return $active;
protected function verify_component_status( $name ) {
if ( 'core' === $name || bp_is_active( $name ) ) {
return __( 'active', 'buddypress' );
}
return __( 'inactive', 'buddypress' );
@@ -419,13 +418,11 @@ public function get_component_info( $component ) {
* @return bool
*/
protected function component_exists( $component ) {
$keys = array_keys( bp_core_get_components() );
return in_array( $component, $keys, true );
return in_array( $component, array_keys( bp_core_get_components() ), true );
}
/**
* Get the plugin schema, conforming to JSON Schema.
* Get the components schema, conforming to JSON Schema.
*
* @since 0.1.0
*
@@ -434,7 +431,7 @@ protected function component_exists( $component ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Components', 'buddypress' ),
'title' => 'bp_components',
'type' => 'object',
'properties' => array(
'name' => array(
@@ -478,7 +475,7 @@ public function get_item_schema() {
*
* @param string $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_components_schema', $schema );
return apply_filters( 'bp_rest_components_schema', $this->add_additional_fields_schema( $schema ) );
}
/**
@@ -510,6 +507,11 @@ public function get_collection_params() {
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
/**
* Filters the collection query params.
*
* @param array $params Query params.
*/
return apply_filters( 'bp_rest_components_collection_params', $params );
}
}
@@ -449,7 +449,7 @@ public function delete_item_permissions_check( $request ) {
*
* @since 0.1.0
*
* @param stdClass $invite Invited user object.
* @param stdClass $invite Invite object.
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response
*/
@@ -475,7 +475,7 @@ public function prepare_item_for_response( $invite, $request ) {
*
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request Request used to generate the response.
* @param stdClass $invite The group invite.
* @param stdClass $invite The invite object.
*/
return apply_filters( 'bp_rest_group_invites_prepare_value', $response, $request, $invite );
}
@@ -485,12 +485,12 @@ public function prepare_item_for_response( $invite, $request ) {
*
* @since 0.1.0
*
* @param stdClass $user Invited user object.
* @param stdClass $invite Invite object.
* @return array Links for the given plugin.
*/
protected function prepare_links( $user ) {
protected function prepare_links( $invite ) {
$base = sprintf( '/%s/%s/', $this->namespace, $this->rest_base );
$url = $base . $user->user_id;
$url = $base . $invite->user_id;
// Entity meta.
$links = array(
@@ -501,12 +501,20 @@ protected function prepare_links( $user ) {
'href' => rest_url( $base ),
),
'user' => array(
'href' => rest_url( bp_rest_get_user_url( $user->user_id ) ),
'href' => rest_url( bp_rest_get_user_url( $invite->user_id ) ),
'embeddable' => true,
),
);
return $links;
/**
* Filter links prepared for the REST response.
*
* @since 0.1.0
*
* @param array $links The prepared links of the REST response.
* @param stdClass $invite Invite object.
*/
return apply_filters( 'bp_rest_group_invites_prepare_links', $links, $invite );
}
/**
@@ -535,7 +543,7 @@ protected function can_see( $group_id ) {
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => esc_html__( 'Group Invites', 'buddypress' ),
'title' => 'bp_group_invites',
'type' => 'object',
'properties' => array(
'user_id' => array(
@@ -567,7 +575,7 @@ public function get_item_schema() {
*
* @param array $schema The endpoint schema.
*/
return apply_filters( 'bp_rest_group_invites_schema', $schema );
return apply_filters( 'bp_rest_group_invites_schema', $this->add_additional_fields_schema( $schema ) );
}
/**
@@ -597,6 +605,11 @@ public function get_collection_params() {
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
/**
* Filters the collection query params.
*
* @param array $params Query params.
*/
return apply_filters( 'bp_rest_group_invites_collection_params', $params );
}
}

0 comments on commit 10b85cf

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