AbstractBlock.php
4 weeks ago
AbstractBlockTemplate.php
2 years ago
Block.php
2 years ago
BlockContainerTrait.php
4 weeks ago
BlockFormattedTemplateTrait.php
2 years ago
BlockTemplate.php
2 years ago
BlockTemplateLogger.php
2 years ago
AbstractBlockTemplate.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface; |
| 6 | use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface; |
| 7 | |
| 8 | /** |
| 9 | * Block template class. |
| 10 | */ |
| 11 | abstract class AbstractBlockTemplate implements BlockTemplateInterface { |
| 12 | use BlockContainerTrait; |
| 13 | |
| 14 | /** |
| 15 | * Get the template ID. |
| 16 | */ |
| 17 | abstract public function get_id(): string; |
| 18 | |
| 19 | /** |
| 20 | * Get the template title. |
| 21 | */ |
| 22 | public function get_title(): string { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get the template description. |
| 28 | */ |
| 29 | public function get_description(): string { |
| 30 | return ''; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the template area. |
| 35 | */ |
| 36 | public function get_area(): string { |
| 37 | return 'uncategorized'; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * The block cache. |
| 42 | * |
| 43 | * @var BlockInterface[] |
| 44 | */ |
| 45 | private $block_cache = []; |
| 46 | |
| 47 | /** |
| 48 | * Get a block by ID. |
| 49 | * |
| 50 | * @param string $block_id The block ID. |
| 51 | */ |
| 52 | public function get_block( string $block_id ): ?BlockInterface { |
| 53 | return $this->block_cache[ $block_id ] ?? null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Caches a block in the template. This is an internal method and should not be called directly |
| 58 | * except for from the BlockContainerTrait's add_inner_block() method. |
| 59 | * |
| 60 | * @param BlockInterface $block The block to cache. |
| 61 | * |
| 62 | * @throws \ValueError If a block with the specified ID already exists in the template. |
| 63 | * @throws \ValueError If the block template that the block belongs to is not this template. |
| 64 | * |
| 65 | * @ignore |
| 66 | */ |
| 67 | public function cache_block( BlockInterface &$block ) { |
| 68 | $id = $block->get_id(); |
| 69 | |
| 70 | if ( isset( $this->block_cache[ $id ] ) ) { |
| 71 | throw new \ValueError( 'A block with the specified ID already exists in the template.' ); |
| 72 | } |
| 73 | |
| 74 | if ( $block->get_root_template() !== $this ) { |
| 75 | throw new \ValueError( 'The block template that the block belongs to must be the same as this template.' ); |
| 76 | } |
| 77 | |
| 78 | $this->block_cache[ $id ] = $block; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Uncaches a block in the template. This is an internal method and should not be called directly |
| 83 | * except for from the BlockContainerTrait's remove_block() method. |
| 84 | * |
| 85 | * @param string $block_id The block ID. |
| 86 | * |
| 87 | * @ignore |
| 88 | */ |
| 89 | public function uncache_block( string $block_id ) { |
| 90 | if ( isset( $this->block_cache[ $block_id ] ) ) { |
| 91 | unset( $this->block_cache[ $block_id ] ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Generate a block ID based on a base. |
| 97 | * |
| 98 | * @param string $id_base The base to use when generating an ID. |
| 99 | * @return string |
| 100 | */ |
| 101 | public function generate_block_id( string $id_base ): string { |
| 102 | $instance_count = 0; |
| 103 | |
| 104 | do { |
| 105 | $instance_count++; |
| 106 | $block_id = $id_base . '-' . $instance_count; |
| 107 | } while ( isset( $this->block_cache[ $block_id ] ) ); |
| 108 | |
| 109 | return $block_id; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the root template. |
| 114 | */ |
| 115 | public function &get_root_template(): BlockTemplateInterface { |
| 116 | return $this; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the inner blocks as a formatted template. |
| 121 | */ |
| 122 | public function get_formatted_template(): array { |
| 123 | $inner_blocks = $this->get_inner_blocks_sorted_by_order(); |
| 124 | |
| 125 | $inner_blocks_formatted_template = array_map( |
| 126 | function( BlockInterface $block ) { |
| 127 | return $block->get_formatted_template(); |
| 128 | }, |
| 129 | $inner_blocks |
| 130 | ); |
| 131 | |
| 132 | return $inner_blocks_formatted_template; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the template as JSON like array. |
| 137 | * |
| 138 | * @return array The JSON. |
| 139 | */ |
| 140 | public function to_json(): array { |
| 141 | return array( |
| 142 | 'id' => $this->get_id(), |
| 143 | 'title' => $this->get_title(), |
| 144 | 'description' => $this->get_description(), |
| 145 | 'area' => $this->get_area(), |
| 146 | 'blockTemplates' => $this->get_formatted_template(), |
| 147 | ); |
| 148 | } |
| 149 | } |
| 150 |