ACF_Rest_Api::update_fields()publicACF 1.0

Update any incoming field values for the given object. This method is not a part of any public API and is only public as it is required by WordPress.

Метод класса: ACF_Rest_Api{}

Хуков нет.

Возвращает

true|false|WP_Error.

Использование

$ACF_Rest_Api = new ACF_Rest_Api();
$ACF_Rest_Api->update_fields( $data, $object, $property, $request, $object_sub_type );
$data(массив) (обязательный)
-
$object(WP_Post|WP_Term|WP_User) (обязательный)
-
$property(строка) (обязательный)
'acf'
$request(WP_REST_Request) (обязательный)
-
$object_sub_type(строка) (обязательный)
This will be the post type, the taxonomy, or 'user'.

Код ACF_Rest_Api::update_fields() ACF 6.0.4

public function update_fields( $data, $object, $property, $request, $object_sub_type ) {
	// If 'acf' data object is empty, don't do anything.
	if ( empty( $data ) ) {
		return true;
	}

	// Determine the object context (type & ID). If the context can't be determined from the current request, throw an
	// error as the fields are not updateable. This handles in line with WordPress' \WP_REST_Request::sanitize_params().
	$object_id   = acf_get_object_id( $object );
	$object_type = $this->request->object_type;
	if ( ! $object_id or ! $object_type ) {
		return new WP_Error(
			'acf_rest_object_unknown',
			__( sprintf( 'Unable to determine the %s object ID or type. The %s property cannot be updated.', get_class( $object ), $property ), 'acf' ),
			array( 'status' => 400 )
		);
	}

	// Determine the ACF selector for the current object.
	$post_id = $this->make_identifier( $object_id, $object_type );

	// Allow unrestricted update of fields by field key when saving via the WordPress admin. Admin mode will
	// update fields using their field keys to lookup the field. The field lookup is not scoped to field groups
	// located on the given object so any field can be updated. Given the field keys are not defined in the
	// schema, core validation/sanitisation are also bypassed.
	// if ( $this->is_admin_mode( $data ) ) {
	// Loop through payload and save fields using field keys.
	// foreach ( $data as $field_key => $value ) {
	// if ( $field = acf_get_field( $field_key ) ) {
	// acf_update_value( $value, $post_id, $field );
	// }
	// }
	//
	// return true;
	// }

	// todo - consider/discuss handling this in the request object instead
	// If the incoming data defines field group keys, extract it from the data. This is used to scope the
	// field lookup in \ACF_Rest_Api::get_field_groups_by_id();
	$field_group_scope = acf_extract_var( $data, '_acf_field_group_scope', array() );

	// Get all field groups for the current object.
	$field_groups = $this->get_field_groups_by_id( $object_id, $object_type, $object_sub_type, $field_group_scope );
	if ( empty( $field_groups ) ) {
		return true;
	}

	// Collect all fields from matching field groups.
	$all_fields = array();
	foreach ( $field_groups as $field_group ) {
		if ( $fields = $this->get_fields( $field_group, $object_id ) ) {
			$all_fields = array_merge( $fields, $all_fields );
		}
	}

	if ( $all_fields ) {
		// todo - consider/discuss handling this in the request object instead.
		// If the incoming request has a map of field names to keys, extract it for use in the subsequent
		// field search.
		$field_key_map = acf_extract_var( $data, '_acf_field_key_map', array() );

		// Loop through the inbound data payload, find the field matching the incoming field name, and
		// update the field.
		foreach ( $data as $field_name => $value ) {

			// If the field name has a key explicitly mapped to it, use the field key to find the field.
			if ( isset( $field_key_map[ $field_name ] ) ) {
				$field_name = $field_key_map[ $field_name ];
			}

			if ( $field = acf_search_fields( $field_name, $all_fields ) ) {
				acf_update_value( $value, $post_id, $field );
			}
		}
	}

	return true;
}