ArrayUtil.php
6 months ago
BlocksUtil.php
4 months ago
COTMigrationUtil.php
1 year ago
DatabaseUtil.php
1 year ago
FilesystemUtil.php
6 months ago
HtmlSanitizer.php
2 years ago
LegacyRestApiStub.php
1 year ago
PluginInstaller.php
1 year ago
ProductUtil.php
7 months ago
Types.php
1 year ago
URL.php
1 year ago
URLException.php
4 years ago
Users.php
3 months ago
WebhookUtil.php
1 year ago
BlocksUtil.php
87 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Internal\Utilities; |
| 5 | |
| 6 | /** |
| 7 | * Helper functions for working with blocks. |
| 8 | */ |
| 9 | class BlocksUtil { |
| 10 | |
| 11 | /** |
| 12 | * Return blocks with their inner blocks flattened. |
| 13 | * |
| 14 | * @param array $blocks Array of blocks as returned by parse_blocks(). |
| 15 | * @return array All blocks. |
| 16 | */ |
| 17 | public static function flatten_blocks( $blocks ) { |
| 18 | return array_reduce( |
| 19 | $blocks, |
| 20 | function ( $carry, $block ) { |
| 21 | array_push( $carry, array_diff_key( $block, array_flip( array( 'innerBlocks' ) ) ) ); |
| 22 | if ( isset( $block['innerBlocks'] ) ) { |
| 23 | $inner_blocks = self::flatten_blocks( $block['innerBlocks'] ); |
| 24 | return array_merge( $carry, $inner_blocks ); |
| 25 | } |
| 26 | |
| 27 | return $carry; |
| 28 | }, |
| 29 | array() |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get all instances of the specified block from the widget area. |
| 35 | * |
| 36 | * @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`. |
| 37 | * @return array Array of blocks as returned by parse_blocks(). |
| 38 | */ |
| 39 | public static function get_blocks_from_widget_area( $block_name ) { |
| 40 | $blocks = get_option( 'widget_block' ); |
| 41 | |
| 42 | if ( ! is_array( $blocks ) || empty( $blocks ) ) { |
| 43 | return array(); |
| 44 | } |
| 45 | |
| 46 | return array_reduce( |
| 47 | $blocks, |
| 48 | function ( $acc, $block ) use ( $block_name ) { |
| 49 | $parsed_blocks = ! empty( $block['content'] ) ? parse_blocks( $block['content'] ) : array(); |
| 50 | if ( ! empty( $parsed_blocks ) && $block_name === $parsed_blocks[0]['blockName'] ) { |
| 51 | array_push( $acc, $parsed_blocks[0] ); |
| 52 | } |
| 53 | return $acc; |
| 54 | }, |
| 55 | array() |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get all instances of the specified block on a specific template part. |
| 61 | * |
| 62 | * @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`. |
| 63 | * @param string $template_part_slug The woo page to search, e.g. `header`. |
| 64 | * @return array Array of blocks as returned by parse_blocks(). |
| 65 | */ |
| 66 | public static function get_block_from_template_part( $block_name, $template_part_slug ) { |
| 67 | $template = get_block_template( get_stylesheet() . '//' . $template_part_slug, 'wp_template_part' ); |
| 68 | |
| 69 | if ( ! $template || null === $template->content ) { |
| 70 | return array(); |
| 71 | } |
| 72 | |
| 73 | $blocks = parse_blocks( $template->content ); |
| 74 | |
| 75 | $flatten_blocks = self::flatten_blocks( $blocks ); |
| 76 | |
| 77 | return array_values( |
| 78 | array_filter( |
| 79 | $flatten_blocks, |
| 80 | function ( $block ) use ( $block_name ) { |
| 81 | return ( $block_name === $block['blockName'] ); |
| 82 | } |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 |