Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::sanitize_field()publicWC 8.7.0

Sanitize an additional field against any custom sanitization rules.

Method of the class: CheckoutFields{}

Return

Mixed.

Usage

$CheckoutFields = new CheckoutFields();
$CheckoutFields->sanitize_field( $field_key, $field_value );
$field_key(string) (required)
The key of the field.
$field_value(mixed) (required)
The value of the field.

Changelog

Since 8.7.0 Introduced.

CheckoutFields::sanitize_field() code WC 9.0.1

public function sanitize_field( $field_key, $field_value ) {
	try {
		$field = $this->additional_fields[ $field_key ] ?? null;

		if ( $field ) {
			$field_value = call_user_func( $field['sanitize_callback'], $field_value, $field );
		}

		/**
		 * Allow custom sanitization of an additional field.
		 *
		 * @param mixed  $field_value The value of the field being sanitized.
		 * @param string $field_key   Key of the field being sanitized.
		 *
		 * @since 8.6.0
		 * @deprecated 8.7.0 Use woocommerce_sanitize_additional_field instead.
		 */
		$field_value = apply_filters_deprecated( '__experimental_woocommerce_blocks_sanitize_additional_field', array( $field_value, $field_key ), '8.7.0', 'woocommerce_sanitize_additional_field', 'This action has been graduated, use woocommerce_sanitize_additional_field instead.' );

		/**
		 * Allow custom sanitization of an additional field.
		 *
		 * @param mixed  $field_value The value of the field being sanitized.
		 * @param string $field_key   Key of the field being sanitized.
		 *
		 * @since 8.7.0
		 */
		return apply_filters( 'woocommerce_sanitize_additional_field', $field_value, $field_key );

	} catch ( \Throwable $e ) {
		// One of the filters errored so skip it. This allows the checkout process to continue.
		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
		trigger_error(
			sprintf(
				'Field sanitization for %s encountered an error. %s',
				esc_html( $field_key ),
				esc_html( $e->getMessage() )
			),
			E_USER_WARNING
		);
	}

	return $field_value;
}