AbstractPageTemplate.php
1 year ago
AbstractTemplate.php
10 months ago
AbstractTemplateCompatibility.php
1 year ago
AbstractTemplatePart.php
2 years ago
AbstractTemplateWithFallback.php
7 months ago
ArchiveProductTemplatesCompatibility.php
1 year ago
CartTemplate.php
10 months ago
CheckoutHeaderTemplate.php
2 years ago
CheckoutTemplate.php
10 months ago
ClassicTemplatesCompatibility.php
1 year ago
ComingSoonSocialLinksTemplate.php
1 year ago
ComingSoonTemplate.php
1 year ago
ExternalProductAddToCartWithOptionsTemplate.php
1 year ago
GroupedProductAddToCartWithOptionsTemplate.php
1 year ago
MiniCartTemplate.php
2 years ago
OrderConfirmationTemplate.php
1 year ago
ProductAttributeTemplate.php
7 months ago
ProductBrandTemplate.php
7 months ago
ProductCatalogTemplate.php
7 months ago
ProductCategoryTemplate.php
7 months ago
ProductSearchResultsTemplate.php
7 months ago
ProductTagTemplate.php
7 months ago
SimpleProductAddToCartWithOptionsTemplate.php
1 year ago
SingleProductTemplate.php
2 months ago
SingleProductTemplateCompatibility.php
10 months ago
VariableProductAddToCartWithOptionsTemplate.php
1 year ago
AbstractTemplateWithFallback.php
63 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | namespace Automattic\WooCommerce\Blocks\Templates; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; |
| 6 | |
| 7 | /** |
| 8 | * AbstractTemplateWithFallback class. |
| 9 | * |
| 10 | * Shared logic for templates with fallbacks. |
| 11 | * |
| 12 | * @internal |
| 13 | */ |
| 14 | abstract class AbstractTemplateWithFallback extends AbstractTemplate { |
| 15 | /** |
| 16 | * The fallback template to render if the existing template is not available. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | public string $fallback_template; |
| 21 | |
| 22 | /** |
| 23 | * Initialization method. |
| 24 | */ |
| 25 | public function init() { |
| 26 | add_filter( 'taxonomy_template_hierarchy', array( $this, 'template_hierarchy' ), 1 ); |
| 27 | add_action( 'template_redirect', array( $this, 'render_block_template' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Add the fallback template to the hierarchy, right after the current template. |
| 32 | * |
| 33 | * @param array $templates Templates that match the taxonomy_template_hierarchy. |
| 34 | */ |
| 35 | public function template_hierarchy( $templates ) { |
| 36 | $index = array_search( static::SLUG, $templates, true ); |
| 37 | if ( false === $index ) { |
| 38 | $index = array_search( static::SLUG . '.php', $templates, true ); |
| 39 | } |
| 40 | |
| 41 | if ( |
| 42 | false !== $index && ( |
| 43 | ! array_key_exists( $index + 1, $templates ) || $templates[ $index + 1 ] !== $this->fallback_template |
| 44 | ) ) { |
| 45 | array_splice( $templates, $index + 1, 0, $this->fallback_template ); |
| 46 | } |
| 47 | |
| 48 | return $templates; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * This method is hooked to WordPress' 'template_redirect' action and allows |
| 53 | * template classes to: |
| 54 | * 1. Decide when block templates should be rendered based on the context. |
| 55 | * 2. Execute specific logic, such as managing the compatibility layer for |
| 56 | * legacy template support. |
| 57 | * |
| 58 | * Child classes must implement this method to define their template |
| 59 | * rendering conditions and any additional template-specific behavior. |
| 60 | */ |
| 61 | abstract public function render_block_template(); |
| 62 | } |
| 63 |