This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Single Product Template: Add compatibility layer (#8442)
* Add minimum structure for Single Product Details block * Add Product Image Gallery #8233 Add Product Image Gallery * Add tests for Single Product Details block * Add the initial basis for the Add to Cart button * Trigger the single product add to cart action for each product type. * wip: create block structure and add initial styles * Add block details to the SingleProductDetails.php file * Rename the block from add-to-cart-button to add-to-cart-form * Update to use the cart icon. * Implement the skeleton for the editor preview. * Render tabs title with empty content * Use woocommerce_output_product_data_tabs function to retrieve tabs data * Update styles and add Notice for the display in the Editor. * Update CSS. * Add base tests for the new Add to Cart Form component. * Add Product Image Gallery block * remove support global styles * remove support global styles * Update the button CSS. * Remove customizations for the Single Product Details block * Update styles for the cart form. * update td style. * Update divs and CSS. * Use conventional input instead of the experimental InputControl * address CSS feedback * add support for the custom classname * remove save function * Remove unnecessary console.log from the Edit.tsx file * Remove block classname from block wrapper * Remove unnecessary WooCommerce tabs filter from the BlockTemplatesController * Remove attributes property from the block registration * Remove isExperimental flag for the Single Product Details block * Remove get_classes_and_styles_by_attributes method from SingleProductDetails block * Prevent Single Product Details block from apppearing in Pages or Posts * add second parameter to the subscribe function * Implement the new design and copy provided for the editor. * Make the notice compatible with dark themes. * Some additional CSS tweaks * adjust the padding for the input * wrap the Single Product Template in a div with the product class * Fix PHP Coding Standards warnings * improve logic and increase coverage of unit test * improve logic and increase coverage of unit test * fix test * format HTML * fix edge case * update @types/wordpress__data package * update placeholder, icon and description * update tsconfig * update block name * fix SCSS linter error * address feedback * create SingleProductTemplateCompatibility class * Add Hooks compatibility * remove not used file * remove not used files * Add compatibility layer for the Single Product template * fix check * address feedback * remove unused import * double empty line * remove logic in the constructor * remove hook * generate the docs * add missing hooks * fix docs * address feedback * fix linter * fix import * Disable compatibility layer when the WooCommerce Product Grid Block block and WooCommerce Single Product Block are used (#8538) * disable compatibility layer via hook * update docs * generate the docs * fix version * fix import * fix code after merge --------- Co-authored-by: Alexandre Lara <allexandrelara@gmail.com> Co-authored-by: Patricia Hillebrandt <patriciahillebrandt@gmail.com>
- Loading branch information
1 parent
50aced3
commit b73fbca
Showing
10 changed files
with
694 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?php | ||
namespace Automattic\WooCommerce\Blocks\Templates; | ||
|
||
/** | ||
* BlockTemplatesCompatibility class. | ||
* | ||
* To bridge the gap on compatibility with PHP hooks and blockified templates. | ||
* | ||
* @internal | ||
*/ | ||
abstract class AbstractTemplateCompatibility { | ||
/** | ||
* The data of supported hooks, containing the hook name, the block name, | ||
* position, and the callbacks. | ||
* | ||
* @var array $hook_data The hook data. | ||
*/ | ||
protected $hook_data; | ||
|
||
/** | ||
* Initialization method. | ||
*/ | ||
public function init() { | ||
if ( ! wc_current_theme_is_fse_theme() ) { | ||
return; | ||
} | ||
|
||
$this->set_hook_data(); | ||
|
||
add_filter( | ||
'render_block_data', | ||
function( $parsed_block, $source_block, $parent_block ) { | ||
/** | ||
* Filter to disable the compatibility layer for the blockified templates. | ||
* | ||
* This hook allows to disable the compatibility layer for the blockified templates. | ||
* | ||
* @since TBD | ||
* @param boolean. | ||
*/ | ||
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false ); | ||
|
||
if ( $is_disabled_compatility_layer ) { | ||
return $parsed_block; | ||
} | ||
|
||
return $this->update_render_block_data( $parsed_block, $source_block, $parent_block ); | ||
|
||
}, | ||
10, | ||
3 | ||
); | ||
|
||
add_filter( | ||
'render_block', | ||
function ( $block_content, $block ) { | ||
/** | ||
* Filter to disable the compatibility layer for the blockified templates. | ||
* | ||
* This hook allows to disable the compatibility layer for the blockified. | ||
* | ||
* @since TBD | ||
* @param boolean. | ||
*/ | ||
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false ); | ||
|
||
if ( $is_disabled_compatility_layer ) { | ||
return $block_content; | ||
} | ||
|
||
return $this->inject_hooks( $block_content, $block ); | ||
}, | ||
10, | ||
2 | ||
); | ||
} | ||
|
||
/** | ||
* Update the render block data to inject our custom attribute needed to | ||
* determine which blocks belong to an inherited Products block. | ||
* | ||
* @param array $parsed_block The block being rendered. | ||
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. | ||
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. | ||
* | ||
* @return array | ||
*/ | ||
abstract public function update_render_block_data( $parsed_block, $source_block, $parent_block ); | ||
|
||
/** | ||
* Inject hooks to rendered content of corresponding blocks. | ||
* | ||
* @param mixed $block_content The rendered block content. | ||
* @param mixed $block The parsed block data. | ||
* @return string | ||
*/ | ||
abstract public function inject_hooks( $block_content, $block ); | ||
|
||
/** | ||
* The hook data to inject to the rendered content of blocks. This also | ||
* contains hooked functions that will be removed by remove_default_hooks. | ||
* | ||
* The array format: | ||
* [ | ||
* <hook-name> => [ | ||
* block_name => <block-name>, | ||
* position => before|after, | ||
* hooked => [ | ||
* <function-name> => <priority>, | ||
* ... | ||
* ], | ||
* ], | ||
* ] | ||
* Where: | ||
* - hook-name is the name of the hook that will be replaced. | ||
* - block-name is the name of the block that will replace the hook. | ||
* - position is the position of the block relative to the hook. | ||
* - hooked is an array of functions hooked to the hook that will be | ||
* replaced. The key is the function name and the value is the | ||
* priority. | ||
*/ | ||
abstract protected function set_hook_data(); | ||
|
||
|
||
/** | ||
* Remove the default callback added by WooCommerce. We replaced these | ||
* callbacks by blocks so we have to remove them to prevent duplicated | ||
* content. | ||
*/ | ||
protected function remove_default_hooks() { | ||
foreach ( $this->hook_data as $hook => $data ) { | ||
if ( ! isset( $data['hooked'] ) ) { | ||
continue; | ||
} | ||
foreach ( $data['hooked'] as $callback => $priority ) { | ||
remove_action( $hook, $callback, $priority ); | ||
} | ||
} | ||
|
||
/** | ||
* When extensions implement their equivalent blocks of the template | ||
* hook functions, they can use this filter to register their old hooked | ||
* data here, so in the blockified template, the old hooked functions | ||
* can be removed in favor of the new blocks while keeping the old | ||
* hooked functions working in classic templates. | ||
* | ||
* Accepts an array of hooked data. The array should be in the following | ||
* format: | ||
* [ | ||
* [ | ||
* hook => <hook-name>, | ||
* function => <function-name>, | ||
* priority => <priority>, | ||
* ], | ||
* ... | ||
* ] | ||
* Where: | ||
* - hook-name is the name of the hook that have the functions hooked to. | ||
* - function-name is the hooked function name. | ||
* - priority is the priority of the hooked function. | ||
* | ||
* @since 9.5.0 | ||
* @param array $data Additional hooked data. Default to empty | ||
*/ | ||
$additional_hook_data = apply_filters( 'woocommerce_blocks_hook_compatibility_additional_data', array() ); | ||
|
||
if ( empty( $additional_hook_data ) || ! is_array( $additional_hook_data ) ) { | ||
return; | ||
} | ||
|
||
foreach ( $additional_hook_data as $data ) { | ||
if ( ! isset( $data['hook'], $data['function'], $data['priority'] ) ) { | ||
continue; | ||
} | ||
remove_action( $data['hook'], $data['function'], $data['priority'] ); | ||
} | ||
} | ||
|
||
/** | ||
* Get the buffer content of the hooks to append/prepend to render content. | ||
* | ||
* @param array $hooks The hooks to be rendered. | ||
* @param string $position The position of the hooks. | ||
* | ||
* @return string | ||
*/ | ||
protected function get_hooks_buffer( $hooks, $position ) { | ||
ob_start(); | ||
foreach ( $hooks as $hook => $data ) { | ||
if ( $data['position'] === $position ) { | ||
/** | ||
* Action to render the content of a hook. | ||
* | ||
* @since 9.5.0 | ||
*/ | ||
do_action( $hook ); | ||
} | ||
} | ||
return ob_get_clean(); | ||
} | ||
} |
Oops, something went wrong.