Automattic\WooCommerce\Blocks

BlockTypesController::block_should_have_data_attributes()publicWC 1.0

Check if a block should have data attributes appended on render. If it's in an allowed namespace, or the block has explicitly been added to the allowed block list, or if one of the block's parents is in the WooCommerce namespace it can have data attributes.

Method of the class: BlockTypesController{}

Return

true|false.

Usage

$BlockTypesController = new BlockTypesController();
$BlockTypesController->block_should_have_data_attributes( $block_name );
$block_name(string) (required)
Name of the block to check.

BlockTypesController::block_should_have_data_attributes() code WC 9.3.3

public function block_should_have_data_attributes( $block_name ) {
	$block_namespace = strtok( $block_name ?? '', '/' );

	/**
	 * Filters the list of allowed block namespaces.
	 *
	 * This hook defines which block namespaces should have block name and attribute `data-` attributes appended on render.
	 *
	 * @since 5.9.0
	 *
	 * @param array $allowed_namespaces List of namespaces.
	 */
	$allowed_namespaces = array_merge( array( 'woocommerce', 'woocommerce-checkout' ), (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_namespace', array() ) );

	/**
	 * Filters the list of allowed Block Names
	 *
	 * This hook defines which block names should have block name and attribute data- attributes appended on render.
	 *
	 * @since 5.9.0
	 *
	 * @param array $allowed_namespaces List of namespaces.
	 */
	$allowed_blocks = (array) apply_filters( '__experimental_woocommerce_blocks_add_data_attributes_to_block', array() );

	$blocks_with_woo_parents   = $this->get_registered_blocks_with_woocommerce_parent();
	$block_has_woo_parent      = in_array( $block_name, array_keys( $blocks_with_woo_parents ), true );
	$in_allowed_namespace_list = in_array( $block_namespace, $allowed_namespaces, true );
	$in_allowed_block_list     = in_array( $block_name, $allowed_blocks, true );

	return $block_has_woo_parent || $in_allowed_block_list || $in_allowed_namespace_list;
}