woocommerce/product-meta', 'woocommerce/product-price', 'woocommerce/breadcrumbs' ); $found = false; foreach ( $parsed_blocks as $block ) { if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $single_product_template_blocks, true ) ) { $found = true; break; } $found = self::has_single_product_template_blocks( $block['innerBlocks'], $single_product_template_blocks ); if ( $found ) { break; } } return $found; } /** * Group blocks in this way: * B1 + TP1 + B2 + B3 + B4 + TP2 + B5 * (B = Block, TP = Template Part) * becomes: * [[B1], [TP1], [B2, B3, B4], [TP2], [B5]] * * @param array $parsed_blocks Array of parsed block objects. * @return array Array of blocks grouped by template part. */ private static function group_blocks( $parsed_blocks ) { return array_reduce( $parsed_blocks, function( $carry, $block ) { if ( 'core/template-part' === $block['blockName'] ) { $carry[] = array( $block ); return $carry; } $last_element_index = count( $carry ) - 1; if ( isset( $carry[ $last_element_index ][0]['blockName'] ) && 'core/template-part' !== $carry[ $last_element_index ][0]['blockName'] ) { $carry[ $last_element_index ][] = $block; return $carry; } $carry[] = array( $block ); return $carry; }, array() ); } /** * Inject the hooks after the div wrapper. * * @param string $block_content Block Content. * @param array $hooks Hooks to inject. * @return array */ private function inject_hooks_after_the_wrapper( $block_content, $hooks ) { $closing_tag_position = strpos( $block_content, '>' ); return substr_replace( $block_content, $this->get_hooks_buffer( $hooks, 'before' ), // Add 1 to the position to inject the content after the closing tag. $closing_tag_position + 1, 0 ); } /** * Plain custom HTML block is parsed as block with an empty blockName with a filled innerHTML. * * @param array $block Parse block. * @return bool */ private static function is_custom_html( $block ) { return empty( $block['blockName'] ) && ! empty( $block['innerHTML'] ); } /** * Serialize template. * * @param array $parsed_blocks Parsed blocks. * @return string */ private static function serialize_blocks( $parsed_blocks ) { return array_reduce( $parsed_blocks, function( $carry, $item ) { if ( is_array( $item ) ) { return $carry . serialize_blocks( $item ); } return $carry . serialize_block( $item ); }, '' ); } }