Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::validate_options()privateWC 1.0

Validates the "base" options (id, label, location) and shows warnings if they're not supplied.

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

Хуков нет.

Возвращает

true|false. false if an error was encountered, true otherwise.

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

// private - только в коде основоного (родительского) класса
$result = $this->validate_options( $options );
$options(массив) (обязательный) (передается по ссылке — &)
The options supplied during field registration.

Код CheckoutFields::validate_options() WC 9.8.1

private function validate_options( &$options ) {
	if ( empty( $options['id'] ) ) {
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', 'A checkout field cannot be registered without an id.', '8.6.0' );
		return false;
	}

	// Having fewer than 2 after exploding around a / means there is no namespace.
	if ( count( explode( '/', $options['id'] ) ) < 2 ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'A checkout field id must consist of namespace/name.' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}

	if ( empty( $options['label'] ) ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field label is required.' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}

	if ( empty( $options['location'] ) ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field location is required.' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}

	if ( 'additional' === $options['location'] ) {
		wc_deprecated_argument( 'location', '8.9.0', 'The "additional" location is deprecated. Use "order" instead.' );
		$options['location'] = 'order';
	}

	if ( ! in_array( $options['location'], array_keys( $this->fields_locations ), true ) ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field location is invalid.' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}

	// At this point, the essentials fields and its location should be set and valid.
	$location = $options['location'];
	$id       = $options['id'];

	// Check to see if field is already in the array.
	if ( ! empty( $this->additional_fields[ $id ] ) || in_array( $id, $this->fields_locations[ $location ], true ) ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'The field is already registered.' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}

	if ( ! empty( $options['type'] ) ) {
		if ( ! in_array( $options['type'], $this->supported_field_types, true ) ) {
			$message = sprintf(
				'Unable to register field with id: "%s". Registering a field with type "%s" is not supported. The supported types are: %s.',
				$id,
				$options['type'],
				implode( ', ', $this->supported_field_types )
			);
			_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
			return false;
		}
	}

	if ( ! empty( $options['sanitize_callback'] ) && ! is_callable( $options['sanitize_callback'] ) ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'The sanitize_callback must be a valid callback.' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}

	if ( ! empty( $options['validate_callback'] ) && ! is_callable( $options['validate_callback'] ) ) {
		$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'The validate_callback must be a valid callback.' );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		return false;
	}

	if ( ! empty( $options['hidden'] ) && true === $options['hidden'] ) {
		// Hidden fields are not supported right now. They will be registered with hidden => false.
		$message = sprintf( 'Registering a field with hidden set to true is not supported. The field "%s" will be registered as visible.', $id );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
		// Don't return here unlike the other fields because this is not an issue that will prevent registration.
	}

	if ( Features::is_enabled( 'experimental-blocks' ) && ! empty( $options['rules'] ) ) {
		if ( ! is_array( $options['rules'] ) ) {
			$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The rules must be an array.' );
			_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
			return false;
		}

		$valid = Validation::is_valid_schema( $options['rules'] );

		if ( is_wp_error( $valid ) ) {
			$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], $valid->get_error_message() );
			_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
			return false;
		}
	}

	return true;
}