BP_REST_XProfile_Fields_Endpoint::assemble_response_data( BP_XProfile_Field $field, WP_REST_Request $request )

Assembles single XProfile field data for return as an object.

Description

Parameters

$field

(Required) XProfile field object.

$request

(Required) Full data about the request.

Return

(array)

Source

File: bp-xprofile/classes/class-bp-rest-xprofile-fields-endpoint.php

	public function assemble_response_data( $field, $request ) {
		$data = array(
			'id'                => (int) $field->id,
			'group_id'          => (int) $field->group_id,
			'parent_id'         => (int) $field->parent_id,
			'type'              => $field->type,
			'name'              => $field->name,
			'alternate_name'    => '',
			'description'       => array(
				'raw'      => $field->description,
				'rendered' => apply_filters( 'bp_get_the_profile_field_description', $field->description ),
			),
			'is_required'       => (bool) $field->is_required,
			'can_delete'        => (bool) $field->can_delete,
			'field_order'       => (int) $field->field_order,
			'option_order'      => (int) $field->option_order,
			'order_by'          => $field->order_by,
			'is_default_option' => (bool) $field->is_default_option,
			'options'           => '',
		);

		if ( ! empty( $request['fetch_visibility_level'] ) ) {
			$data['visibility_level']        = $field->visibility_level;
			$data['allow_custom_visibility'] = (
				! empty( $field->__get( 'allow_custom_visibility' ) )
				&& 'allowed' === $field->__get( 'allow_custom_visibility' )
			) ? $field->__get( 'allow_custom_visibility' ) : 'disabled';
		}

		if ( true === wp_validate_boolean( $request->get_param( 'fetch_field_data' ) ) ) {
			if ( isset( $field->data->id ) ) {
				$data['data']['id'] = $field->data->id;
			}

			$data['data']['value'] = array(
				'raw'          => ( isset( $field->data->value ) ? $field->data->value : '' ),
				'unserialized' => $this->get_profile_field_unserialized_value( ( isset( $field->data->value ) ? $field->data->value : '' ) ),
				'rendered'     => $this->get_profile_field_rendered_value( ( isset( $field->data->value ) ? $field->data->value : '' ), $field ),
			);
		}

		// Added settings for date field.
		if ( 'datebox' === $field->type ) {
			$datebox_field    = new BP_XProfile_Field_Type_Datebox();
			$data['settings'] = $datebox_field::get_field_settings( $field->id );
		}

		// Added settings and format options for phone field.
		if ( 'telephone' === $field->type ) {
			$telephone_field                    = new BP_XProfile_Field_Type_Telephone();
			$data['settings']                   = $telephone_field->get_field_settings( $field );
			$data['settings']['format_options'] = $telephone_field->get_phone_formats();
		}

		// Added options for membertype field.
		if ( 'membertypes' === $field->type && function_exists( 'bp_check_member_type_field_have_options' ) && true === bp_check_member_type_field_have_options() ) {
			$data['options'] = $this->get_member_type_options( $field, $request );
		}

		// Added options for membertype field.
		if ( 'socialnetworks' === $field->type ) {
			$data['options'] = $this->get_socialnetworks_type_options( $field, $request );
		}

		// Added options for selectbox, multiselectbox, radio and checkbox fields.
		if ( 'selectbox' === $field->type || 'multiselectbox' === $field->type || 'radio' === $field->type || 'checkbox' === $field->type ) {
			$data['options'] = $field->get_children();
		}

		if ( 'gender' === $field->type ) {
			$data['options'] = $this->get_gender_type_options( $field, $request );
		}

		// Added options for date field.
		if ( 'datebox' === $field->type ) {
			if ( empty( $data['options'] ) ) {
				$data['options'] = array();
			}
			$data['options']['day']   = $this->get_date_field_options_array( $field, 'day' );
			$data['options']['month'] = $this->get_date_field_options_array( $field, 'month' );
			$data['options']['year']  = $this->get_date_field_options_array( $field, 'year' );
		}

		$is_repeater_enabled = 'on' === bp_xprofile_get_meta( $field->group_id, 'group', 'is_repeater_enabled', true ) ? true : false;
		if ( $is_repeater_enabled ) {
			$data['repeater_data'] = $this->get_repeater_fields_data( $field, $request );
		}

		// Get alternate name for the field.
		$alternate_name = bp_xprofile_get_meta( (int) $field->id, 'field', 'alternate_name' );
		if ( ! empty( $alternate_name ) ) {
			$data['alternate_name'] = $alternate_name;
		}

		$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;
	}

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.