BP_REST_XProfile_Fields_Endpoint::get_item( WP_REST_Request $request )

Retrieve single XProfile field.


Description Description


Parameters Parameters

$request

(Required) Full data about the request.


Top ↑

Return Return

(WP_REST_Response|WP_Error)


Top ↑

Source Source

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

	public function get_item( $request ) {
		$profile_field_id = (int) $request['id'];

		$field = $this->get_xprofile_field_object( $profile_field_id );

		if ( empty( $profile_field_id ) || empty( $field->id ) ) {
			return new WP_Error(
				'bp_rest_invalid_id',
				__( 'Invalid field ID.', 'buddypress' ),
				array(
					'status' => 404,
				)
			);
		}

		if ( ! empty( $request['user_id'] ) ) {
			$field->data = new stdClass();

			// Ensure that the requester is allowed to see this field.
			$hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $request['user_id'] );

			if ( in_array( $profile_field_id, $hidden_user_fields, true ) ) {
				$field->data->value = __( 'Value suppressed.', 'buddypress' );
			} else {
				// Get the raw value for the field.
				$field->data->value = BP_XProfile_ProfileData::get_value_byid( $profile_field_id, $request['user_id'] );
			}

			// Set 'fetch_field_data' to true so that the data is included in the response.
			$request['fetch_field_data'] = true;
		}

		$retval = array(
			$this->prepare_response_for_collection(
				$this->prepare_item_for_response( $field, $request )
			),
		);

		$response = rest_ensure_response( $retval );

		/**
		 * Fires after XProfile field is fetched via the REST API.
		 *
		 * @since 5.0.0
		 *
		 * @param BP_XProfile_Field $field    Fetched field object.
		 * @param WP_REST_Response  $response The response data.
		 * @param WP_REST_Request   $request  The request sent to the API.
		 */
		do_action( 'bp_rest_xprofile_fields_get_item', $field, $response, $request );

		return $response;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.