PHP 8.2.31
Preview: BlocksUtil.php Size: 2.36 KB
/home/nshryvcy/radiantskinclinics.org/wp-content/plugins/woocommerce/src/Internal/Utilities/BlocksUtil.php

<?php
declare( strict_types = 1 );

namespace Automattic\WooCommerce\Internal\Utilities;

/**
 * Helper functions for working with blocks.
 */
class BlocksUtil {

	/**
	 * Return blocks with their inner blocks flattened.
	 *
	 * @param array $blocks Array of blocks as returned by parse_blocks().
	 * @return array All blocks.
	 */
	public static function flatten_blocks( $blocks ) {
		return array_reduce(
			$blocks,
			function ( $carry, $block ) {
				array_push( $carry, array_diff_key( $block, array_flip( array( 'innerBlocks' ) ) ) );
				if ( isset( $block['innerBlocks'] ) ) {
					$inner_blocks = self::flatten_blocks( $block['innerBlocks'] );
					return array_merge( $carry, $inner_blocks );
				}

				return $carry;
			},
			array()
		);
	}

	/**
	 * Get all instances of the specified block from the widget area.
	 *
	 * @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`.
	 * @return array Array of blocks as returned by parse_blocks().
	 */
	public static function get_blocks_from_widget_area( $block_name ) {
		$blocks = get_option( 'widget_block' );

		if ( ! is_array( $blocks ) || empty( $blocks ) ) {
			return array();
		}

		return array_reduce(
			$blocks,
			function ( $acc, $block ) use ( $block_name ) {
				$parsed_blocks = ! empty( $block['content'] ) ? parse_blocks( $block['content'] ) : array();
				if ( ! empty( $parsed_blocks ) && $block_name === $parsed_blocks[0]['blockName'] ) {
					array_push( $acc, $parsed_blocks[0] );
				}
				return $acc;
			},
			array()
		);
	}

	/**
	 * Get all instances of the specified block on a specific template part.
	 *
	 * @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`.
	 * @param string $template_part_slug The woo page to search, e.g. `header`.
	 * @return array Array of blocks as returned by parse_blocks().
	 */
	public static function get_block_from_template_part( $block_name, $template_part_slug ) {
		$template = get_block_template( get_stylesheet() . '//' . $template_part_slug, 'wp_template_part' );

		if ( ! $template || null === $template->content ) {
			return array();
		}

		$blocks = parse_blocks( $template->content );

		$flatten_blocks = self::flatten_blocks( $blocks );

		return array_values(
			array_filter(
				$flatten_blocks,
				function ( $block ) use ( $block_name ) {
					return ( $block_name === $block['blockName'] );
				}
			)
		);
	}
}

Directory Contents

Dirs: 0 × Files: 14

Name Size Perms Modified Actions
3.07 KB lrw-r--r-- 2025-11-24 23:10:10
Edit Download
2.36 KB lrw-r--r-- 2026-01-26 10:40:36
Edit Download
6.04 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
16.10 KB lrw-r--r-- 2024-09-23 20:44:04
Edit Download
7.63 KB lrw-r--r-- 2026-03-30 17:12:24
Edit Download
3.10 KB lrw-r--r-- 2024-04-30 19:35:34
Edit Download
6.65 KB lrw-r--r-- 2024-12-18 22:19:16
Edit Download
14.01 KB lrw-r--r-- 2024-12-18 22:19:16
Edit Download
1.16 KB lrw-r--r-- 2025-10-06 17:56:06
Edit Download
1.97 KB lrw-r--r-- 2024-12-16 15:24:32
Edit Download
13.10 KB lrw-r--r-- 2024-12-16 15:24:32
Edit Download
191 B lrw-r--r-- 2022-04-20 06:50:54
Edit Download
9.47 KB lrw-r--r-- 2026-02-23 17:58:34
Edit Download
5.37 KB lrw-r--r-- 2024-12-18 22:19:16
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).