woocommerce
/
src
/
Internal
/
Features
/
ProductBlockEditor
/
ProductTemplates
/
AbstractProductFormTemplate.php
AbstractProductFormTemplate.php
2 years ago
DownloadableProductTrait.php
2 years ago
Group.php
1 year ago
ProductBlock.php
2 years ago
ProductVariationTemplate.php
1 month ago
Section.php
1 year ago
SimpleProductTemplate.php
1 month ago
Subsection.php
1 year ago
AbstractProductFormTemplate.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Features\ProductBlockEditor\ProductTemplates; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\BlockTemplates\BlockInterface; |
| 6 | use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\GroupInterface; |
| 7 | use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\ProductFormTemplateInterface; |
| 8 | use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\SectionInterface; |
| 9 | use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\SubsectionInterface; |
| 10 | use Automattic\WooCommerce\Internal\Admin\BlockTemplates\AbstractBlockTemplate; |
| 11 | |
| 12 | /** |
| 13 | * Block template class. |
| 14 | */ |
| 15 | abstract class AbstractProductFormTemplate extends AbstractBlockTemplate implements ProductFormTemplateInterface { |
| 16 | /** |
| 17 | * Get the template area. |
| 18 | */ |
| 19 | public function get_area(): string { |
| 20 | return 'product-form'; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Get a group block by ID. |
| 25 | * |
| 26 | * @param string $group_id The group block ID. |
| 27 | * @throws \UnexpectedValueException If block is not of type GroupInterface. |
| 28 | */ |
| 29 | public function get_group_by_id( string $group_id ): ?GroupInterface { |
| 30 | $group = $this->get_block( $group_id ); |
| 31 | if ( $group && ! $group instanceof GroupInterface ) { |
| 32 | throw new \UnexpectedValueException( 'Block with specified ID is not a group.' ); |
| 33 | } |
| 34 | return $group; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get a section block by ID. |
| 39 | * |
| 40 | * @param string $section_id The section block ID. |
| 41 | * @throws \UnexpectedValueException If block is not of type SectionInterface. |
| 42 | */ |
| 43 | public function get_section_by_id( string $section_id ): ?SectionInterface { |
| 44 | $section = $this->get_block( $section_id ); |
| 45 | if ( $section && ! $section instanceof SectionInterface ) { |
| 46 | throw new \UnexpectedValueException( 'Block with specified ID is not a section.' ); |
| 47 | } |
| 48 | return $section; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get a subsection block by ID. |
| 53 | * |
| 54 | * @param string $subsection_id The subsection block ID. |
| 55 | * @throws \UnexpectedValueException If block is not of type SubsectionInterface. |
| 56 | */ |
| 57 | public function get_subsection_by_id( string $subsection_id ): ?SubsectionInterface { |
| 58 | $subsection = $this->get_block( $subsection_id ); |
| 59 | if ( $subsection && ! $subsection instanceof SubsectionInterface ) { |
| 60 | throw new \UnexpectedValueException( 'Block with specified ID is not a subsection.' ); |
| 61 | } |
| 62 | return $subsection; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get a block by ID. |
| 67 | * |
| 68 | * @param string $block_id The block block ID. |
| 69 | */ |
| 70 | public function get_block_by_id( string $block_id ): ?BlockInterface { |
| 71 | return $this->get_block( $block_id ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Add a custom block type to this template. |
| 76 | * |
| 77 | * @param array $block_config The block data. |
| 78 | */ |
| 79 | public function add_group( array $block_config ): GroupInterface { |
| 80 | $block = new Group( $block_config, $this->get_root_template(), $this ); |
| 81 | return $this->add_inner_block( $block ); |
| 82 | } |
| 83 | } |
| 84 |