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
ClassicTemplatesCompatibility.php
82 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Templates; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry; |
| 5 | |
| 6 | /** |
| 7 | * ClassicTemplatesCompatibility class. |
| 8 | * |
| 9 | * To bridge the gap on compatibility with widget blocks and classic PHP core templates. |
| 10 | * |
| 11 | * @internal |
| 12 | */ |
| 13 | class ClassicTemplatesCompatibility { |
| 14 | |
| 15 | /** |
| 16 | * Instance of the asset data registry. |
| 17 | * |
| 18 | * @var AssetDataRegistry |
| 19 | */ |
| 20 | protected $asset_data_registry; |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @param AssetDataRegistry $asset_data_registry Instance of the asset data registry. |
| 26 | */ |
| 27 | public function __construct( AssetDataRegistry $asset_data_registry ) { |
| 28 | $this->asset_data_registry = $asset_data_registry; |
| 29 | $this->init(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Initialization method. |
| 34 | */ |
| 35 | protected function init() { // phpcs:ignore WooCommerce.Functions.InternalInjectionMethod.MissingPublic |
| 36 | if ( ! wp_is_block_theme() ) { |
| 37 | add_action( 'template_redirect', array( $this, 'set_classic_template_data' ) ); |
| 38 | // We need to set this data on the widgets screen so the filters render previews. |
| 39 | add_action( 'load-widgets.php', array( $this, 'set_filterable_product_data' ) ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Executes the methods which set the necessary data needed for filter blocks to work correctly as widgets in Classic templates. |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public function set_classic_template_data() { |
| 49 | $this->set_filterable_product_data(); |
| 50 | $this->set_php_template_data(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * This method passes the value `has_filterable_products` to the front-end for product archive pages, |
| 55 | * so that widget product filter blocks are aware of the context they are in and can render accordingly. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function set_filterable_product_data() { |
| 60 | global $pagenow; |
| 61 | |
| 62 | if ( is_shop() || is_product_taxonomy() || 'widgets.php' === $pagenow ) { |
| 63 | $this->asset_data_registry->add( 'hasFilterableProducts', true ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * This method passes the value `is_rendering_php_template` to the front-end of Classic themes, |
| 69 | * so that widget product filter blocks are aware of how to filter the products. |
| 70 | * |
| 71 | * This data only matters on WooCommerce product archive pages. |
| 72 | * On non-archive pages the merchant could be using the All Products block which is not a PHP template. |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function set_php_template_data() { |
| 77 | if ( is_shop() || is_product_taxonomy() ) { |
| 78 | $this->asset_data_registry->add( 'isRenderingPhpTemplate', true ); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 |