Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Utilitie...
File: BlocksUtil.php
<?php
[0] Fix | Delete
declare( strict_types = 1 );
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\Internal\Utilities;
[3] Fix | Delete
[4] Fix | Delete
/**
[5] Fix | Delete
* Helper functions for working with blocks.
[6] Fix | Delete
*/
[7] Fix | Delete
class BlocksUtil {
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Return blocks with their inner blocks flattened.
[11] Fix | Delete
*
[12] Fix | Delete
* @param array $blocks Array of blocks as returned by parse_blocks().
[13] Fix | Delete
* @return array All blocks.
[14] Fix | Delete
*/
[15] Fix | Delete
public static function flatten_blocks( $blocks ) {
[16] Fix | Delete
return array_reduce(
[17] Fix | Delete
$blocks,
[18] Fix | Delete
function ( $carry, $block ) {
[19] Fix | Delete
array_push( $carry, array_diff_key( $block, array_flip( array( 'innerBlocks' ) ) ) );
[20] Fix | Delete
if ( isset( $block['innerBlocks'] ) ) {
[21] Fix | Delete
$inner_blocks = self::flatten_blocks( $block['innerBlocks'] );
[22] Fix | Delete
return array_merge( $carry, $inner_blocks );
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
return $carry;
[26] Fix | Delete
},
[27] Fix | Delete
array()
[28] Fix | Delete
);
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Get all instances of the specified block from the widget area.
[33] Fix | Delete
*
[34] Fix | Delete
* @param array $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`.
[35] Fix | Delete
* @return array Array of blocks as returned by parse_blocks().
[36] Fix | Delete
*/
[37] Fix | Delete
public static function get_blocks_from_widget_area( $block_name ) {
[38] Fix | Delete
$blocks = get_option( 'widget_block' );
[39] Fix | Delete
[40] Fix | Delete
if ( ! is_array( $blocks ) || empty( $blocks ) ) {
[41] Fix | Delete
return array();
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
return array_reduce(
[45] Fix | Delete
$blocks,
[46] Fix | Delete
function ( $acc, $block ) use ( $block_name ) {
[47] Fix | Delete
$parsed_blocks = ! empty( $block['content'] ) ? parse_blocks( $block['content'] ) : array();
[48] Fix | Delete
if ( ! empty( $parsed_blocks ) && $block_name === $parsed_blocks[0]['blockName'] ) {
[49] Fix | Delete
array_push( $acc, $parsed_blocks[0] );
[50] Fix | Delete
}
[51] Fix | Delete
return $acc;
[52] Fix | Delete
},
[53] Fix | Delete
array()
[54] Fix | Delete
);
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Get all instances of the specified block on a specific template part.
[59] Fix | Delete
*
[60] Fix | Delete
* @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`.
[61] Fix | Delete
* @param string $template_part_slug The woo page to search, e.g. `header`.
[62] Fix | Delete
* @return array Array of blocks as returned by parse_blocks().
[63] Fix | Delete
*/
[64] Fix | Delete
public static function get_block_from_template_part( $block_name, $template_part_slug ) {
[65] Fix | Delete
$template = get_block_template( get_stylesheet() . '//' . $template_part_slug, 'wp_template_part' );
[66] Fix | Delete
$blocks = parse_blocks( $template->content );
[67] Fix | Delete
[68] Fix | Delete
$flatten_blocks = self::flatten_blocks( $blocks );
[69] Fix | Delete
[70] Fix | Delete
return array_values(
[71] Fix | Delete
array_filter(
[72] Fix | Delete
$flatten_blocks,
[73] Fix | Delete
function ( $block ) use ( $block_name ) {
[74] Fix | Delete
return ( $block_name === $block['blockName'] );
[75] Fix | Delete
}
[76] Fix | Delete
)
[77] Fix | Delete
);
[78] Fix | Delete
}
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function