BP_REST_Groups_Endpoint::prepare_item_for_response( BP_Groups_Group $item, WP_REST_Request $request )
Prepares group data for return as an object.
Description
Parameters
- $item
-
(Required) Group object.
- $request
-
(Required) Full details about the request.
Return
(WP_REST_Response)
Source
File: bp-groups/classes/class-bp-rest-groups-endpoint.php
public function prepare_item_for_response( $item, $request ) { $data = array( 'id' => $item->id, 'creator_id' => bp_get_group_creator_id( $item ), 'parent_id' => $item->parent_id, 'date_created' => bp_rest_prepare_date_response( $item->date_created ), 'description' => array( 'raw' => $item->description, 'rendered' => bp_get_group_description( $item ), ), 'enable_forum' => $this->bp_rest_group_is_forum_enabled( $item ), 'link' => bp_get_group_permalink( $item ), 'name' => bp_get_group_name( $item ), 'slug' => bp_get_group_slug( $item ), 'status' => bp_get_group_status( $item ), 'types' => bp_groups_get_group_type( $item->id, false ), 'subgroups_id' => $this->bp_rest_get_sub_groups( $item->id ), 'admins' => array(), 'mods' => array(), 'total_member_count' => null, 'last_activity' => null, 'is_member' => groups_is_user_member( get_current_user_id(), $item->id ) ? true : false, 'invite_id' => groups_is_user_invited( get_current_user_id(), $item->id ), 'request_id' => groups_is_user_pending( get_current_user_id(), $item->id ), 'is_admin' => ( ! empty( groups_is_user_admin( get_current_user_id(), $item->id ) ) ? true : false ), 'is_mod' => ( ! empty( groups_is_user_mod( get_current_user_id(), $item->id ) ) ? true : false ), 'members_count' => groups_get_total_member_count( $item->id ), 'role' => '', 'plural_role' => '', 'can_join' => $this->bp_rest_user_can_join( $item ), ); // BuddyBoss Platform support. if ( function_exists( 'bp_get_user_group_role_title' ) && bp_loggedin_user_id() ) { $data['role'] = bp_get_user_group_role_title( bp_loggedin_user_id(), $item->id ); // BuddyPress support. } elseif ( function_exists( 'bp_groups_get_group_roles' ) && bp_loggedin_user_id() ) { $group_role = bp_groups_get_group_roles(); if ( groups_is_user_admin( bp_loggedin_user_id(), $item->id ) ) { $data['role'] = $group_role['admin']->name; } elseif ( groups_is_user_mod( bp_loggedin_user_id(), $item->id ) ) { $data['role'] = $group_role['mod']->name; } elseif ( groups_is_user_member( bp_loggedin_user_id(), $item->id ) ) { $data['role'] = $group_role['member']->name; } } if ( function_exists( 'bp_get_group_member_section_title' ) && bp_loggedin_user_id() ) { $data['plural_role'] = $this->bp_get_group_member_section_title( $item->id, bp_loggedin_user_id() ); if ( empty( $data['plural_role'] ) ) { $data['plural_role'] = $data['role']; } } else { $data['plural_role'] = $data['role']; } // Get item schema. $schema = $this->get_item_schema(); // Avatars. if ( ! empty( $schema['properties']['avatar_urls'] ) ) { $data['avatar_urls'] = array( 'thumb' => bp_core_fetch_avatar( array( 'html' => false, 'object' => 'group', 'item_id' => $item->id, 'type' => 'thumb', ) ), 'full' => bp_core_fetch_avatar( array( 'html' => false, 'object' => 'group', 'item_id' => $item->id, 'type' => 'full', ) ), ); } // Cover Image. if ( ! empty( $schema['properties']['cover_url'] ) && function_exists( 'bp_get_group_cover_url' ) ) { $data['cover_url'] = bp_get_group_cover_url( $item ); } if ( $this->bp_rest_group_is_forum_enabled( $item ) && function_exists( 'bbpress' ) ) { $data['forum'] = groups_get_groupmeta( $item->id, 'forum_id' ); if ( is_array( $data['forum'] ) && ! empty( $data['forum'][0] ) ) { $data['forum'] = $data['forum'][0]; } else { $data['forum'] = 0; } } else { $data['forum'] = 0; } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; // Get group type(s). if ( false === $data['types'] ) { $data['types'] = array(); } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; // If this is the 'edit' context, fill in more details--similar to "populate_extras". if ( 'edit' === $context ) { $data['total_member_count'] = groups_get_groupmeta( $item->id, 'total_member_count' ); $data['last_activity'] = bp_rest_prepare_date_response( groups_get_groupmeta( $item->id, 'last_activity' ) ); // Add admins and moderators to their respective arrays. $admin_mods = groups_get_group_members( array( 'group_id' => $item->id, 'group_role' => array( 'admin', 'mod', ), ) ); foreach ( (array) $admin_mods['members'] as $user ) { // Make sure to unset private data. $private_keys = array_intersect( array_keys( get_object_vars( $user ) ), array( 'user_pass', 'user_email', 'user_activation_key', ) ); foreach ( $private_keys as $private_key ) { unset( $user->{$private_key} ); } if ( ! empty( $user->is_admin ) ) { $data['admins'][] = $user; } else { $data['mods'][] = $user; } } } $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( $item ) ); /** * Filter a group 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_Groups_Group $item Group object. * * @since 0.1.0 */ return apply_filters( 'bp_rest_groups_prepare_value', $response, $request, $item ); }
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.