Automattic\WooCommerce\Blocks\Utils

CartCheckoutUtils::has_block_variationpublic staticWC 1.0

Check if the post content contains a block with a specific attribute value.

Method of the class: CartCheckoutUtils{}

No Hooks.

Returns

true|false.

Usage

$result = CartCheckoutUtils::has_block_variation( $block_id, $attribute, $value, $post_content );
$block_id(string) (required)
The block ID to check for.
$attribute(string) (required)
The attribute to check.
$value(string) (required)
The value to check for.
$post_content(string) (required)
The post content to check.

CartCheckoutUtils::has_block_variation() code WC 10.7.0

public static function has_block_variation( $block_id, $attribute, $value, $post_content ) {
	if ( ! $post_content ) {
		return false;
	}

	$scanner = Block_Scanner::create( $post_content );
	if ( ! $scanner ) {
		return false;
	}

	while ( $scanner->next_delimiter() ) {
		if ( ! $scanner->opens_block( $block_id ) ) {
			continue;
		}

		$attrs = $scanner->allocate_and_return_parsed_attributes();

		if ( isset( $attrs[ $attribute ] ) && $value === $attrs[ $attribute ] ) {
			return true;
		}

		// `Cart` is default for `woocommerce/classic-shortcode` so it will be empty in the block attributes.
		if ( 'woocommerce/classic-shortcode' === $block_id &&
			'shortcode' === $attribute &&
			'cart' === $value &&
			! isset( $attrs['shortcode'] ) ) {
			return true;
		}
	}

	return false;
}