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 |