Automattic\WooCommerce\Blocks

BlockTypesController::has_block_variation()privateWC 1.0

Check if the current post has a block with a specific attribute value.

Method of the class: BlockTypesController{}

No Hooks.

Return

true|false.

Usage

// private - for code of main (parent) class only
$result = $this->has_block_variation( $block_id, $attribute, $value );
$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.

BlockTypesController::has_block_variation() code WC 9.4.2

private function has_block_variation( $block_id, $attribute, $value ) {
	$post = get_post();

	if ( ! $post ) {
		return false;
	}

	if ( has_block( $block_id, $post->ID ) ) {
		$blocks = (array) parse_blocks( $post->post_content );

		foreach ( $blocks as $block ) {
			if ( isset( $block['attrs'][ $attribute ] ) && $value === $block['attrs'][ $attribute ] ) {
				return true;
			}
		}
	}

	return false;
}