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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Admin / BlockTemplates / BlockContainerTrait.php
woocommerce / src / Internal / Admin / BlockTemplates Last commit date
AbstractBlock.php 1 month ago AbstractBlockTemplate.php 2 years ago Block.php 2 years ago BlockContainerTrait.php 1 month ago BlockFormattedTemplateTrait.php 2 years ago BlockTemplate.php 2 years ago BlockTemplateLogger.php 2 years ago
BlockContainerTrait.php
385 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\ContainerInterface;
7
8 /**
9 * Trait for block containers.
10 */
11 trait BlockContainerTrait {
12 use BlockFormattedTemplateTrait {
13 get_formatted_template as get_block_formatted_template;
14 }
15
16 /**
17 * The inner blocks.
18 *
19 * @var BlockInterface[]
20 */
21 private $inner_blocks = array();
22
23 // phpcs doesn't take into account exceptions thrown by called methods.
24 // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
25
26 /**
27 * Add a block to the block container.
28 *
29 * @param BlockInterface $block The block.
30 *
31 * @throws \ValueError If the block configuration is invalid.
32 * @throws \ValueError If a block with the specified ID already exists in the template.
33 * @throws \UnexpectedValueException If the block container is not the parent of the block.
34 * @throws \UnexpectedValueException If the block container's root template is not the same as the block's root template.
35 */
36 protected function &add_inner_block( BlockInterface $block ): BlockInterface {
37 if ( $block->get_parent() !== $this ) {
38 throw new \UnexpectedValueException( 'The block container is not the parent of the block.' );
39 }
40
41 if ( $block->get_root_template() !== $this->get_root_template() ) {
42 throw new \UnexpectedValueException( 'The block container\'s root template is not the same as the block\'s root template.' );
43 }
44
45 $is_detached = method_exists( $this, 'is_detached' ) && $this->is_detached();
46 if ( ! $is_detached ) {
47 $this->get_root_template()->cache_block( $block );
48 }
49
50 $this->inner_blocks[] = &$block;
51
52 $this->do_after_add_block_action( $block );
53 $this->do_after_add_specific_block_action( $block );
54
55 return $block;
56 }
57
58 // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
59
60 /**
61 * Checks if a block is a descendant of the block container.
62 *
63 * @param BlockInterface $block The block.
64 */
65 private function is_block_descendant( BlockInterface $block ): bool {
66 $parent = $block->get_parent();
67
68 if ( $parent === $this ) {
69 return true;
70 }
71
72 if ( ! $parent instanceof BlockInterface ) {
73 return false;
74 }
75
76 return $this->is_block_descendant( $parent );
77 }
78
79 /**
80 * Get a block by ID.
81 *
82 * @param string $block_id The block ID.
83 */
84 public function get_block( string $block_id ): ?BlockInterface {
85 foreach ( $this->inner_blocks as $block ) {
86 if ( $block->get_id() === $block_id ) {
87 return $block;
88 }
89 }
90
91 foreach ( $this->inner_blocks as $block ) {
92 if ( $block instanceof ContainerInterface ) {
93 $block = $block->get_block( $block_id );
94
95 if ( $block ) {
96 return $block;
97 }
98 }
99 }
100
101 return null;
102 }
103
104 /**
105 * Remove a block from the block container.
106 *
107 * @param string $block_id The block ID.
108 *
109 * @throws \UnexpectedValueException If the block container is not an ancestor of the block.
110 */
111 public function remove_block( string $block_id ) {
112 $root_template = $this->get_root_template();
113
114 $block = $root_template->get_block( $block_id );
115
116 if ( ! $block ) {
117 return;
118 }
119
120 if ( ! $this->is_block_descendant( $block ) ) {
121 throw new \UnexpectedValueException( 'The block container is not an ancestor of the block.' );
122 }
123
124 // If the block is a container, remove all of its blocks.
125 if ( $block instanceof ContainerInterface ) {
126 $block->remove_blocks();
127 }
128
129 $parent = $block->get_parent();
130 $parent->remove_inner_block( $block );
131 }
132
133 /**
134 * Remove all blocks from the block container.
135 */
136 public function remove_blocks() {
137 array_map(
138 function ( BlockInterface $block ) {
139 $this->remove_block( $block->get_id() );
140 },
141 $this->inner_blocks
142 );
143 }
144
145 /**
146 * Remove a block from the block container's inner blocks. This is an internal method and should not be called directly
147 * except for from the BlockContainerTrait's remove_block() method.
148 *
149 * @param BlockInterface $block The block.
150 */
151 public function remove_inner_block( BlockInterface $block ) {
152 // Remove block from root template's cache.
153 $root_template = $this->get_root_template();
154 $root_template->uncache_block( $block->get_id() );
155
156 $this->inner_blocks = array_filter(
157 $this->inner_blocks,
158 function ( BlockInterface $inner_block ) use ( $block ) {
159 return $inner_block !== $block;
160 }
161 );
162
163 $this->do_after_remove_block_action( $block );
164 $this->do_after_remove_specific_block_action( $block );
165 }
166
167 /**
168 * Get the inner blocks sorted by order.
169 */
170 private function get_inner_blocks_sorted_by_order(): array {
171 $sorted_inner_blocks = $this->inner_blocks;
172
173 usort(
174 $sorted_inner_blocks,
175 function( BlockInterface $a, BlockInterface $b ) {
176 return $a->get_order() <=> $b->get_order();
177 }
178 );
179
180 return $sorted_inner_blocks;
181 }
182
183 /**
184 * Get the inner blocks as a formatted template.
185 */
186 public function get_formatted_template(): array {
187 $arr = $this->get_block_formatted_template();
188
189 $inner_blocks = $this->get_inner_blocks_sorted_by_order();
190
191 if ( ! empty( $inner_blocks ) ) {
192 $arr[] = array_map(
193 function( BlockInterface $block ) {
194 return $block->get_formatted_template();
195 },
196 $inner_blocks
197 );
198 }
199
200 return $arr;
201 }
202
203 /**
204 * Do the `woocommerce_block_template_after_add_block` action.
205 * Handle exceptions thrown by the action.
206 *
207 * @param BlockInterface $block The block.
208 */
209 private function do_after_add_block_action( BlockInterface $block ) {
210 try {
211 /**
212 * Action called after a block is added to a block container.
213 *
214 * This action can be used to perform actions after a block is added to the block container,
215 * such as adding a dependent block.
216 *
217 * @param BlockInterface $block The block.
218 *
219 * @since 8.2.0
220 * @deprecated 10.9.0 This hook will be removed in WooCommerce 11.0.
221 */
222 wc_do_deprecated_action(
223 'woocommerce_block_template_after_add_block',
224 array( $block ),
225 '10.9.0',
226 null,
227 'This block template hook will be removed in WooCommerce 11.0.'
228 );
229 } catch ( \Exception $e ) {
230 $this->do_after_add_block_error_action( $block, 'woocommerce_block_template_after_add_block', $e );
231 }
232 }
233
234 /**
235 * Do the `woocommerce_block_template_area_{template_area}_after_add_block_{block_id}` action.
236 * Handle exceptions thrown by the action.
237 *
238 * @param BlockInterface $block The block.
239 */
240 private function do_after_add_specific_block_action( BlockInterface $block ) {
241 $hook_name = "woocommerce_block_template_area_{$this->get_root_template()->get_area()}_after_add_block_{$block->get_id()}";
242
243 try {
244 /**
245 * Action called after a specific block is added to a template with a specific area.
246 *
247 * This action can be used to perform actions after a specific block is added to a template with a specific area,
248 * such as adding a dependent block.
249 *
250 * @param BlockInterface $block The block.
251 *
252 * @since 8.2.0
253 * @deprecated 10.9.0 This hook will be removed in WooCommerce 11.0.
254 */
255 wc_do_deprecated_action(
256 $hook_name,
257 array( $block ),
258 '10.9.0',
259 null,
260 'This block template hook will be removed in WooCommerce 11.0.'
261 );
262 } catch ( \Exception $e ) {
263 $this->do_after_add_block_error_action( $block, $hook_name, $e );
264 }
265 }
266
267 /**
268 * Do the `woocommerce_block_after_add_block_error` action.
269 *
270 * @param BlockInterface $block The block.
271 * @param string $action The action that threw the exception.
272 * @param \Exception $e The exception.
273 */
274 private function do_after_add_block_error_action( BlockInterface $block, string $action, \Exception $e ) {
275 /**
276 * Action called after an exception is thrown by a `woocommerce_block_template_after_add_block` action hook.
277 *
278 * @param BlockInterface $block The block.
279 * @param string $action The action that threw the exception.
280 * @param \Exception $exception The exception.
281 *
282 * @since 8.4.0
283 * @deprecated 10.9.0 This hook will be removed in WooCommerce 11.0.
284 */
285 wc_do_deprecated_action(
286 'woocommerce_block_template_after_add_block_error',
287 array( $block, $action, $e ),
288 '10.9.0',
289 null,
290 'This block template hook will be removed in WooCommerce 11.0.'
291 );
292 }
293
294 /**
295 * Do the `woocommerce_block_template_after_remove_block` action.
296 * Handle exceptions thrown by the action.
297 *
298 * @param BlockInterface $block The block.
299 */
300 private function do_after_remove_block_action( BlockInterface $block ) {
301 try {
302 /**
303 * Action called after a block is removed from a block container.
304 *
305 * This action can be used to perform actions after a block is removed from the block container,
306 * such as removing a dependent block.
307 *
308 * @param BlockInterface $block The block.
309 *
310 * @since 8.2.0
311 * @deprecated 10.9.0 This hook will be removed in WooCommerce 11.0.
312 */
313 wc_do_deprecated_action(
314 'woocommerce_block_template_after_remove_block',
315 array( $block ),
316 '10.9.0',
317 null,
318 'This block template hook will be removed in WooCommerce 11.0.'
319 );
320 } catch ( \Exception $e ) {
321 $this->do_after_remove_block_error_action( $block, 'woocommerce_block_template_after_remove_block', $e );
322 }
323 }
324
325 /**
326 * Do the `woocommerce_block_template_area_{template_area}_after_remove_block_{block_id}` action.
327 * Handle exceptions thrown by the action.
328 *
329 * @param BlockInterface $block The block.
330 */
331 private function do_after_remove_specific_block_action( BlockInterface $block ) {
332 $hook_name = "woocommerce_block_template_area_{$this->get_root_template()->get_area()}_after_remove_block_{$block->get_id()}";
333
334 try {
335 /**
336 * Action called after a specific block is removed from a template with a specific area.
337 *
338 * This action can be used to perform actions after a specific block is removed from a template with a specific area,
339 * such as removing a dependent block.
340 *
341 * @param BlockInterface $block The block.
342 *
343 * @since 8.2.0
344 * @deprecated 10.9.0 This hook will be removed in WooCommerce 11.0.
345 */
346 wc_do_deprecated_action(
347 $hook_name,
348 array( $block ),
349 '10.9.0',
350 null,
351 'This block template hook will be removed in WooCommerce 11.0.'
352 );
353 } catch ( \Exception $e ) {
354 $this->do_after_remove_block_error_action( $block, $hook_name, $e );
355 }
356 }
357
358 /**
359 * Do the `woocommerce_block_after_remove_block_error` action.
360 *
361 * @param BlockInterface $block The block.
362 * @param string $action The action that threw the exception.
363 * @param \Exception $e The exception.
364 */
365 private function do_after_remove_block_error_action( BlockInterface $block, string $action, \Exception $e ) {
366 /**
367 * Action called after an exception is thrown by a `woocommerce_block_template_after_remove_block` action hook.
368 *
369 * @param BlockInterface $block The block.
370 * @param string $action The action that threw the exception.
371 * @param \Exception $exception The exception.
372 *
373 * @since 8.4.0
374 * @deprecated 10.9.0 This hook will be removed in WooCommerce 11.0.
375 */
376 wc_do_deprecated_action(
377 'woocommerce_block_template_after_remove_block_error',
378 array( $block, $action, $e ),
379 '10.9.0',
380 null,
381 'This block template hook will be removed in WooCommerce 11.0.'
382 );
383 }
384 }
385