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
BlockFormattedTemplateTrait.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Admin\BlockTemplates; |
| 4 | |
| 5 | /** |
| 6 | * Trait for block formatted template. |
| 7 | */ |
| 8 | trait BlockFormattedTemplateTrait { |
| 9 | /** |
| 10 | * Get the block configuration as a formatted template. |
| 11 | * |
| 12 | * @return array The block configuration as a formatted template. |
| 13 | */ |
| 14 | public function get_formatted_template(): array { |
| 15 | $arr = array( |
| 16 | $this->get_name(), |
| 17 | array_merge( |
| 18 | $this->get_attributes(), |
| 19 | array( |
| 20 | '_templateBlockId' => $this->get_id(), |
| 21 | '_templateBlockOrder' => $this->get_order(), |
| 22 | ), |
| 23 | ! empty( $this->get_hide_conditions() ) ? array( |
| 24 | '_templateBlockHideConditions' => $this->get_formatted_hide_conditions(), |
| 25 | ) : array(), |
| 26 | ! empty( $this->get_disable_conditions() ) ? array( |
| 27 | '_templateBlockDisableConditions' => $this->get_formatted_disable_conditions(), |
| 28 | ) : array(), |
| 29 | ), |
| 30 | ); |
| 31 | |
| 32 | return $arr; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the block hide conditions formatted for inclusion in a formatted template. |
| 37 | */ |
| 38 | private function get_formatted_hide_conditions(): array { |
| 39 | return $this->format_conditions( $this->get_hide_conditions() ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the block disable conditions formatted for inclusion in a formatted template. |
| 44 | */ |
| 45 | private function get_formatted_disable_conditions(): array { |
| 46 | return $this->format_conditions( $this->get_disable_conditions() ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Formats conditions in the expected format to include in the template. |
| 51 | * |
| 52 | * @param array $conditions The conditions to format. |
| 53 | */ |
| 54 | private function format_conditions( $conditions ): array { |
| 55 | $formatted_expressions = array_map( |
| 56 | function( $condition ) { |
| 57 | return array( |
| 58 | 'expression' => $condition['expression'], |
| 59 | ); |
| 60 | }, |
| 61 | array_values( $conditions ) |
| 62 | ); |
| 63 | |
| 64 | return $formatted_expressions; |
| 65 | } |
| 66 | } |
| 67 |