| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block Template Utils class |
| 4 |
* |
| 5 |
* @since 4.7.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Block Template Utils class |
| 17 |
*/ |
| 18 |
class BlockTemplateUtils { |
| 19 |
const CACHE_KEY = 'ep_blocks'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Hook cache cleanup calls |
| 23 |
*/ |
| 24 |
public function setup() { |
| 25 |
add_action( 'save_post_wp_template', [ $this, 'regenerate_cache' ] ); |
| 26 |
add_action( 'save_post_wp_template_part', [ $this, 'regenerate_cache' ] ); |
| 27 |
add_action( 'switch_theme', [ $this, 'regenerate_cache' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Delete and regenerate the cache |
| 32 |
*/ |
| 33 |
public function regenerate_cache() { |
| 34 |
delete_transient( self::CACHE_KEY ); |
| 35 |
|
| 36 |
// Simply calling it will reset the transient (if the `ep_blocks_pre_all_blocks` filter isn't in use.) |
| 37 |
$this->get_all_blocks_in_all_templates(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Given a block name, return all its instances across all block templates |
| 42 |
* |
| 43 |
* @param string $block_name The block name, e.g., `elasticpress/facet-meta` |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
public function get_specific_block_in_all_templates( string $block_name ): array { |
| 47 |
$blocks = array_filter( |
| 48 |
$this->get_all_blocks_in_all_templates(), |
| 49 |
function ( $block ) use ( $block_name ) { |
| 50 |
return ( $block['blockName'] === $block_name ); |
| 51 |
} |
| 52 |
); |
| 53 |
|
| 54 |
return $blocks; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get all blocks in all block templates |
| 59 |
* |
| 60 |
* It returns a flat list of all blocks, including innerBlocks. |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function get_all_blocks_in_all_templates(): array { |
| 65 |
/** |
| 66 |
* Short-circuits the process of getting all blocks of a template. |
| 67 |
* |
| 68 |
* Returning a non-null value will effectively short-circuit the function. |
| 69 |
* |
| 70 |
* @since 4.7.0 |
| 71 |
* @hook ep_blocks_pre_all_blocks |
| 72 |
* @param {null} $meta_keys Blocks array |
| 73 |
* @return {null|array} Blocks array or `null` to keep default behavior |
| 74 |
*/ |
| 75 |
$pre_all_blocks = apply_filters( 'ep_blocks_pre_all_blocks', null ); |
| 76 |
if ( null !== $pre_all_blocks ) { |
| 77 |
return (array) $pre_all_blocks; |
| 78 |
} |
| 79 |
|
| 80 |
$cache = get_transient( self::CACHE_KEY ); |
| 81 |
if ( is_array( $cache ) ) { |
| 82 |
return $cache; |
| 83 |
} |
| 84 |
|
| 85 |
$all_blocks = []; |
| 86 |
|
| 87 |
$template_types = [ 'wp_template', 'wp_template_part' ]; |
| 88 |
|
| 89 |
foreach ( $template_types as $template_type ) { |
| 90 |
$block_templates = get_block_templates( [], $template_type ); |
| 91 |
foreach ( $block_templates as $block_template ) { |
| 92 |
$template_blocks = parse_blocks( $block_template->content ); |
| 93 |
|
| 94 |
foreach ( $template_blocks as $block ) { |
| 95 |
$all_blocks[] = [ |
| 96 |
'blockName' => $block['blockName'], |
| 97 |
'attrs' => $block['attrs'], |
| 98 |
]; |
| 99 |
|
| 100 |
$all_blocks = $this->recursively_get_inner_blocks( $all_blocks, $block ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
set_transient( self::CACHE_KEY, $all_blocks, MONTH_IN_SECONDS ); |
| 106 |
|
| 107 |
return $all_blocks; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get all inner blocks recursively |
| 112 |
* |
| 113 |
* @param array $all_blocks All blocks analyzed so far |
| 114 |
* @param array $block Block to be analyzed now |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
protected function recursively_get_inner_blocks( array $all_blocks, array $block ): array { |
| 118 |
if ( empty( $block['innerBlocks'] ) ) { |
| 119 |
return $all_blocks; |
| 120 |
} |
| 121 |
|
| 122 |
foreach ( $block['innerBlocks'] as $inner_block ) { |
| 123 |
$all_blocks[] = [ |
| 124 |
'blockName' => $inner_block['blockName'], |
| 125 |
'attrs' => $inner_block['attrs'], |
| 126 |
]; |
| 127 |
|
| 128 |
$all_blocks = $this->recursively_get_inner_blocks( $all_blocks, $inner_block ); |
| 129 |
} |
| 130 |
|
| 131 |
return $all_blocks; |
| 132 |
} |
| 133 |
} |
| 134 |
|