Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldTypes

AbstractFieldType::process_attributes │ protected │ WC 1.0

Processes the attributes supplied during field registration.

Invalid attributes are dropped with a warning rather than preventing registration.

Method of the class: AbstractFieldType{}

No Hooks.

Returns

array. The processed attributes.

Usage

// protected - for code of main (parent) or child class
$result = $this->process_attributes( $id, $attributes ): array;
$id(string) (required)
The field ID.
$attributes(mixed) (required)
The attributes supplied during field registration.

AbstractFieldType::process_attributes() code WC 11.2.1

protected function process_attributes( $id, $attributes ): array {
	if ( empty( $attributes ) ) {
		return [];
	}

	if ( ! is_array( $attributes ) ) {
		$this->doing_it_wrong( sprintf( 'An invalid attributes value was supplied when registering field with id: "%s". Attributes must be a non-empty array.', $id ), '8.6.0' );
		return [];
	}

	// These are formatted in camelCase because React components expect them that way.
	$allowed_attributes = [ 'maxLength', 'readOnly', 'pattern', 'autocomplete', 'autocapitalize', 'title' ];

	$valid_attributes = array_filter(
		$attributes,
		function ( $_, $key ) use ( $allowed_attributes ) {
			return in_array( $key, $allowed_attributes, true ) || strpos( $key, 'aria-' ) === 0 || strpos( $key, 'data-' ) === 0;
		},
		ARRAY_FILTER_USE_BOTH
	);

	if ( count( $attributes ) !== count( $valid_attributes ) ) {
		$invalid_attributes = array_keys( array_diff_key( $attributes, $valid_attributes ) );
		$this->doing_it_wrong( sprintf( 'Invalid attribute found when registering field with id: "%s". Attributes: %s are not allowed.', $id, implode( ', ', $invalid_attributes ) ), '8.6.0' );
	}

	return array_map( 'esc_attr', $valid_attributes );
}