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.

Method of the class: CheckoutFields{}

No Hooks.

Return

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

Usage

// private - for code of main (parent) class only
$result = $this->validate_options( $options );
$options(array) (required) (passed by reference — &)
The options supplied during field registration.

CheckoutFields::validate_options() code WC 9.7.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;
	}

	// Hidden fields are not supported right now. They will be registered with hidden => false.
	if ( ! empty( $options['hidden'] ) && true === $options['hidden'] ) {
		$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.
	}

	return true;
}