BP_REST_Groups_Types_Endpoint::prepare_item_for_response( array $type, WP_REST_Request $request )

Prepares single Groups type to return as an object.

Description

Parameters

$type

(Required) Groups Type.

$request

(Required) Full data about the request.

Return

(WP_REST_Response)

Source

File: bp-groups/classes/class-bp-rest-groups-types-endpoint.php

	public function prepare_item_for_response( $type, $request ) {
		$data = array(
			'labels'                => array(
				'name'          => ( isset( $type->labels['name'] ) && ! empty( $type->labels['name'] ) ) ? $type->labels['name'] : '',
				'singular_name' => ( isset( $type->labels['singular_name'] ) && ! empty( $type->labels['singular_name'] ) ) ? $type->labels['singular_name'] : '',
			),
			'name'                  => ( isset( $type->name ) ? $type->name : '' ),
			'description'           => ( isset( $type->description ) ? $type->description : '' ),
			'directory_slug'        => ( isset( $type->directory_slug ) ? $type->directory_slug : '' ),
			'has_directory'         => ( isset( $type->has_directory ) ? $type->has_directory : false ),
			'show_in_create_screen' => ( isset( $type->show_in_create_screen ) ? $type->show_in_create_screen : '' ),
			'show_in_list'          => ( isset( $type->show_in_list ) ? $type->show_in_list : '' ),
			'create_screen_checked' => ( isset( $type->create_screen_checked ) ? $type->create_screen_checked : '' ),
		);

		// Get the edit schema.
		$schema  = $this->get_item_schema();
		$schema  = $schema['properties'];
		$post_id = $this->bp_rest_group_type_post_by_type( $data['name'] );

		// Define default visibility property.
		if ( isset( $schema['enable_filter'] ) ) {
			if ( ! empty( $post_id ) ) {
				$data['enable_filter'] = (
					! empty( get_post_meta( $post_id, '_bp_group_type_enable_filter', true ) )
					? get_post_meta( $post_id, '_bp_group_type_enable_filter', true )
					: ''
				);
			} else {
				$data['enable_filter'] = false;
			}
		}

		if ( isset( $schema['enable_remove'] ) ) {
			if ( ! empty( $post_id ) ) {
				$data['enable_remove'] = (
					! empty( get_post_meta( $post_id, '_bp_group_type_enable_remove', true ) )
					? get_post_meta( $post_id, '_bp_group_type_enable_remove', true )
					: ''
				);
			} else {
				$data['enable_remove'] = false;
			}
		}

		if ( isset( $schema['restrict-invites-user-same-group-type'] ) ) {
			if ( ! empty( $post_id ) ) {
				$data['restrict-invites-user-same-group-type'] = (
					! empty( get_post_meta( $post_id, '_bp_group_type_restrict_invites_user_same_group_type', true ) )
					? get_post_meta( $post_id, '_bp_group_type_restrict_invites_user_same_group_type', true )
					: ''
				);
			} else {
				$data['restrict-invites-user-same-group-type'] = false;
			}
		}

		if ( isset( $schema['member_type_group_invites'] ) ) {
			$member_types              = bp_get_member_types( array(), 'names' );
			$get_selected_member_types = ( ! empty( $post_id ) && ! empty( get_post_meta( $post_id, '_bp_group_type_enabled_member_type_group_invites', true ) ) ) ? get_post_meta( $post_id, '_bp_group_type_enabled_member_type_group_invites', true ) : array();
			$member_types_invite       = array();
			if ( ! empty( $member_types ) ) {
				foreach ( $member_types as $member_type ) {
					$member_types_invite[] = array(
						'name'     => $member_type,
						'selected' => in_array( $member_type, $get_selected_member_types, true ),
					);
				}
			}
			$data['member_type_group_invites'] = $member_types_invite;
		}

		if ( isset( $schema['member_type_join'] ) ) {
			$member_types              = bp_get_member_types( array(), 'names' );
			$get_selected_member_types = ( ! empty( $post_id ) && ! empty( get_post_meta( $post_id, '_bp_group_type_enabled_member_type_join', true ) ) ) ? get_post_meta( $post_id, '_bp_group_type_enabled_member_type_join', true ) : array();
			$member_types_join         = array();
			if ( ! empty( $member_types ) ) {
				foreach ( $member_types as $member_type ) {
					$member_types_join[] = array(
						'name'     => $member_type,
						'selected' => in_array( $member_type, $get_selected_member_types, true ),
					);
				}
			}
			$data['member_type_join'] = $member_types_join;
		}

		if ( isset( $schema['group_type_role_labels'] ) ) {
			if ( ! empty( $post_id ) ) {
				$data['group_type_role_labels'] = (
					! empty( get_post_meta( $post_id, '_bp_group_type_role_labels', true ) )
					? get_post_meta( $post_id, '_bp_group_type_role_labels', true )
					: ''
				);
			} else {
				$data['group_type_role_labels'] = array();
			}
		}

		$response = rest_ensure_response( $data );

		/**
		 * Filter the Groups type field returned from the API.
		 *
		 * @param WP_REST_Response $response The response data.
		 * @param WP_REST_Request  $request  Request used to generate the response.
		 * @param array            $type     Xprofile Type
		 *
		 * @since 0.1.0
		 */
		return apply_filters( 'bp_rest_groups_types_prepare_value', $response, $request, $type );
	}

Changelog

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.