Automattic\WooCommerce\Blocks\BlockTypes

ProductDetails::validate_hooked_blocksprivateWC 1.0

Validate hooked blocks data. Remove duplicated entries with the same title and invalid entries with invalid content. Log errors to the WC logger.

Method of the class: ProductDetails{}

No Hooks.

Returns

Array. Validated hooked blocks.

Usage

// private - for code of main (parent) class only
$result = $this->validate_hooked_blocks( $hooked_blocks );
$hooked_blocks(array) (required)
{ Hooked blocks data. @type string $title Title of the hooked block. @type string $content Content of the hooked block, as block markup. }.

ProductDetails::validate_hooked_blocks() code WC 10.8.1

private function validate_hooked_blocks( $hooked_blocks ) {
	$logger                  = wc_get_logger();
	$validated_hooked_blocks = [];

	foreach ( $hooked_blocks as $block ) {
		$invalid = ! is_array( $block ) ||
			! isset( $block['title'] ) ||
			! isset( $block['content'] ) ||
			! is_string( $block['title'] ) ||
			! is_string( $block['content'] );

		if ( ! $invalid ) {
			$parsed_content = parse_blocks( $block['content'] );

			foreach ( $parsed_content as $content_block ) {
				if ( ! isset( $content_block['blockName'] ) ) {
					$invalid = true;
					break;
				}
			}
		}

		if ( $invalid ) {
			$logger->error( 'Invalid hooked block data. Expected array with `title` and `content` keys with string values. Content must be valid block markup.', $block );
			continue;
		}

		$slug = sanitize_title( $block['title'] );

		/**
		 * If the block is already registered, replace the block. We use the
		 * last registered block for the same slug. This makes overriding
		 * hooked block easier.
		 */
		if ( isset( $validated_hooked_blocks[ $slug ] ) ) {
			$validated_hooked_blocks[ $slug ] = $block;
			continue;
		}

		$validated_hooked_blocks[ $slug ] = $block;
	}

	return $validated_hooked_blocks;
}