Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldTypes

SelectFieldType::process_type_options │ protected │ WC 1.0

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

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

Хуков нет.

Возвращает

array|false. The updated $field_data array, or false if an error was encountered.

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

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

Код SelectFieldType::process_type_options() WC 11.2.2

protected function process_type_options( array $field_data, array $options ) {
	$id = $options['id'];

	if ( empty( $options['options'] ) || ! is_array( $options['options'] ) ) {
		return $this->registration_error( $id, 'Fields of type "select" must have an array of "options".', '8.6.0' );
	}

	$cleaned_options = [];

	foreach ( $options['options'] as $option ) {
		if ( ! isset( $option['value'], $option['label'] ) ) {
			return $this->registration_error( $id, 'Fields of type "select" must have an array of "options" and each option must contain a "value" and "label" member.', '8.6.0' );
		}

		$value = sanitize_text_field( $option['value'] );

		if ( isset( $cleaned_options[ $value ] ) ) {
			$this->doing_it_wrong( 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, $value ), '8.6.0' );
			continue;
		}

		$cleaned_options[ $value ] = [
			'value' => $value,
			'label' => sanitize_text_field( $option['label'] ),
		];
	}

	$field_data['options'] = array_values( $cleaned_options );

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

	return $field_data;
}