Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::process_select_field()privateWC 1.0

Processes the options for a select field and returns the new field_options array.

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

Хуков нет.

Возвращает

Массив|false. The updated $field_data array or false if an error was encountered.

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

// private - только в коде основоного (родительского) класса
$result = $this->process_select_field( $field_data, $options );
$field_data(массив) (обязательный)
The field data array to be updated.
$options(массив) (обязательный)
The options supplied during field registration.

Код CheckoutFields::process_select_field() WC 9.8.1

private function process_select_field( $field_data, $options ) {
	$id = $options['id'];

	if ( empty( $options['options'] ) || ! is_array( $options['options'] ) ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'Fields of type "select" must have an array of "options".' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}
	$cleaned_options = [];
	$added_values    = [];

	// Check all entries in $options['options'] has a key and value member.
	foreach ( $options['options'] as $option ) {
		if ( ! isset( $option['value'] ) || ! isset( $option['label'] ) ) {
			$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'Fields of type "select" must have an array of "options" and each option must contain a "value" and "label" member.' );
			_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
			return false;
		}

		$sanitized_value = sanitize_text_field( $option['value'] );
		$sanitized_label = sanitize_text_field( $option['label'] );

		if ( in_array( $sanitized_value, $added_values, true ) ) {
			$message = sprintf( 'Duplicate key found when registering field with id: "%s". The value in each option of "select" fields must be unique. Duplicate value "%s" found. The duplicate key will be removed.', $id, $sanitized_value );
			_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
			continue;
		}

		$added_values[] = $sanitized_value;

		$cleaned_options[] = [
			'value' => $sanitized_value,
			'label' => $sanitized_label,
		];
	}

	$field_data['options'] = $cleaned_options;

	if ( isset( $field_data['placeholder'] ) ) {
		$field_data['placeholder'] = sanitize_text_field( $field_data['placeholder'] );
	}

	return $field_data;
}