Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Template...
File: AbstractTemplateCompatibility.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Templates;
[1] Fix | Delete
[2] Fix | Delete
/**
[3] Fix | Delete
* AbstractTemplateCompatibility class.
[4] Fix | Delete
*
[5] Fix | Delete
* To bridge the gap on compatibility with PHP hooks and blockified templates.
[6] Fix | Delete
*
[7] Fix | Delete
* @internal
[8] Fix | Delete
*/
[9] Fix | Delete
abstract class AbstractTemplateCompatibility {
[10] Fix | Delete
/**
[11] Fix | Delete
* The data of supported hooks, containing the hook name, the block name,
[12] Fix | Delete
* position, and the callbacks.
[13] Fix | Delete
*
[14] Fix | Delete
* @var array $hook_data The hook data.
[15] Fix | Delete
*/
[16] Fix | Delete
protected $hook_data;
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Initialization method.
[20] Fix | Delete
*/
[21] Fix | Delete
public function init() {
[22] Fix | Delete
$this->set_hook_data();
[23] Fix | Delete
[24] Fix | Delete
add_filter(
[25] Fix | Delete
'render_block_data',
[26] Fix | Delete
function ( $parsed_block, $source_block, $parent_block ) {
[27] Fix | Delete
/**
[28] Fix | Delete
* Filter to disable the compatibility layer for the blockified templates.
[29] Fix | Delete
*
[30] Fix | Delete
* This hook allows to disable the compatibility layer for the blockified templates.
[31] Fix | Delete
*
[32] Fix | Delete
* @since 7.6.0
[33] Fix | Delete
* @param boolean.
[34] Fix | Delete
*/
[35] Fix | Delete
$is_disabled_compatility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false );
[36] Fix | Delete
[37] Fix | Delete
if ( $is_disabled_compatility_layer ) {
[38] Fix | Delete
return $parsed_block;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
return $this->update_render_block_data( $parsed_block, $source_block, $parent_block );
[42] Fix | Delete
},
[43] Fix | Delete
10,
[44] Fix | Delete
3
[45] Fix | Delete
);
[46] Fix | Delete
[47] Fix | Delete
add_filter(
[48] Fix | Delete
'render_block',
[49] Fix | Delete
function ( $block_content, $block ) {
[50] Fix | Delete
/**
[51] Fix | Delete
* Filter to disable the compatibility layer for the blockified templates.
[52] Fix | Delete
*
[53] Fix | Delete
* This hook allows to disable the compatibility layer for the blockified.
[54] Fix | Delete
*
[55] Fix | Delete
* @since 7.6.0
[56] Fix | Delete
* @param boolean.
[57] Fix | Delete
*/
[58] Fix | Delete
$is_disabled_compatibility_layer = apply_filters( 'woocommerce_disable_compatibility_layer', false );
[59] Fix | Delete
[60] Fix | Delete
if ( $is_disabled_compatibility_layer ) {
[61] Fix | Delete
return $block_content;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
return $this->inject_hooks( $block_content, $block );
[65] Fix | Delete
},
[66] Fix | Delete
10,
[67] Fix | Delete
2
[68] Fix | Delete
);
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* Update the render block data to inject our custom attribute needed to
[73] Fix | Delete
* determine which blocks belong to an inherited Products block.
[74] Fix | Delete
*
[75] Fix | Delete
* @param array $parsed_block The block being rendered.
[76] Fix | Delete
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
[77] Fix | Delete
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
[78] Fix | Delete
*
[79] Fix | Delete
* @return array
[80] Fix | Delete
*/
[81] Fix | Delete
abstract public function update_render_block_data( $parsed_block, $source_block, $parent_block );
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Inject hooks to rendered content of corresponding blocks.
[85] Fix | Delete
*
[86] Fix | Delete
* @param mixed $block_content The rendered block content.
[87] Fix | Delete
* @param mixed $block The parsed block data.
[88] Fix | Delete
* @return string
[89] Fix | Delete
*/
[90] Fix | Delete
abstract public function inject_hooks( $block_content, $block );
[91] Fix | Delete
[92] Fix | Delete
/**
[93] Fix | Delete
* The hook data to inject to the rendered content of blocks. This also
[94] Fix | Delete
* contains hooked functions that will be removed by remove_default_hooks.
[95] Fix | Delete
*
[96] Fix | Delete
* The array format:
[97] Fix | Delete
* [
[98] Fix | Delete
* <hook-name> => [
[99] Fix | Delete
* block_names => [ <block-name>, ... ],
[100] Fix | Delete
* position => before|after,
[101] Fix | Delete
* hooked => [
[102] Fix | Delete
* <function-name> => <priority>,
[103] Fix | Delete
* ...
[104] Fix | Delete
* ],
[105] Fix | Delete
* ],
[106] Fix | Delete
* ]
[107] Fix | Delete
* Where:
[108] Fix | Delete
* - hook-name is the name of the hook that will be replaced.
[109] Fix | Delete
* - block-names is the array block names that hook will be attached to.
[110] Fix | Delete
* - position is the position of the block relative to the hook.
[111] Fix | Delete
* - hooked is an array of functions hooked to the hook that will be
[112] Fix | Delete
* replaced. The key is the function name and the value is the
[113] Fix | Delete
* priority.
[114] Fix | Delete
*/
[115] Fix | Delete
abstract protected function set_hook_data();
[116] Fix | Delete
[117] Fix | Delete
/**
[118] Fix | Delete
* Remove the default callback added by WooCommerce. We replaced these
[119] Fix | Delete
* callbacks by blocks so we have to remove them to prevent duplicated
[120] Fix | Delete
* content.
[121] Fix | Delete
*/
[122] Fix | Delete
protected function remove_default_hooks() {
[123] Fix | Delete
foreach ( $this->hook_data as $hook => $data ) {
[124] Fix | Delete
if ( ! isset( $data['hooked'] ) ) {
[125] Fix | Delete
continue;
[126] Fix | Delete
}
[127] Fix | Delete
foreach ( $data['hooked'] as $callback => $priority ) {
[128] Fix | Delete
remove_action( $hook, $callback, $priority );
[129] Fix | Delete
}
[130] Fix | Delete
}
[131] Fix | Delete
$class_name = basename( str_replace( '\\', '/', get_class( $this ) ) );
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* When extensions implement their equivalent blocks of the template
[135] Fix | Delete
* hook functions, they can use this filter to register their old hooked
[136] Fix | Delete
* data here, so in the blockified template, the old hooked functions
[137] Fix | Delete
* can be removed in favor of the new blocks while keeping the old
[138] Fix | Delete
* hooked functions working in classic templates.
[139] Fix | Delete
*
[140] Fix | Delete
* Accepts an array of hooked data. The array should be in the following
[141] Fix | Delete
* format:
[142] Fix | Delete
* [
[143] Fix | Delete
* [
[144] Fix | Delete
* hook => <hook-name>,
[145] Fix | Delete
* function => <function-name>,
[146] Fix | Delete
* priority => <priority>,
[147] Fix | Delete
* ],
[148] Fix | Delete
* ...
[149] Fix | Delete
* ]
[150] Fix | Delete
* Where:
[151] Fix | Delete
* - hook-name is the name of the hook that have the functions hooked to.
[152] Fix | Delete
* - function-name is the hooked function name.
[153] Fix | Delete
* - priority is the priority of the hooked function.
[154] Fix | Delete
*
[155] Fix | Delete
* @since 9.5.0
[156] Fix | Delete
* @param array $data Additional hooked data. Default to empty
[157] Fix | Delete
* @param string $class_name Class name within which the hook is called.
[158] Fix | Delete
* Either ArchiveProductTemplatesCompatibility or SingleProductTemplateCompatibility.
[159] Fix | Delete
*/
[160] Fix | Delete
$additional_hook_data = apply_filters( 'woocommerce_blocks_hook_compatibility_additional_data', array(), $class_name );
[161] Fix | Delete
[162] Fix | Delete
if ( empty( $additional_hook_data ) || ! is_array( $additional_hook_data ) ) {
[163] Fix | Delete
return;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
foreach ( $additional_hook_data as $data ) {
[167] Fix | Delete
if ( ! isset( $data['hook'], $data['function'], $data['priority'] ) ) {
[168] Fix | Delete
continue;
[169] Fix | Delete
}
[170] Fix | Delete
remove_action( $data['hook'], $data['function'], $data['priority'] );
[171] Fix | Delete
}
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Get the buffer content of the hooks to append/prepend to render content.
[176] Fix | Delete
*
[177] Fix | Delete
* @param array $hooks The hooks to be rendered.
[178] Fix | Delete
* @param string $position The position of the hooks.
[179] Fix | Delete
*
[180] Fix | Delete
* @return string
[181] Fix | Delete
*/
[182] Fix | Delete
protected function get_hooks_buffer( $hooks, $position ) {
[183] Fix | Delete
ob_start();
[184] Fix | Delete
foreach ( $hooks as $hook => $data ) {
[185] Fix | Delete
if ( $data['position'] === $position ) {
[186] Fix | Delete
/**
[187] Fix | Delete
* Action to render the content of a hook.
[188] Fix | Delete
*
[189] Fix | Delete
* @since 9.5.0
[190] Fix | Delete
*/
[191] Fix | Delete
do_action( $hook );
[192] Fix | Delete
}
[193] Fix | Delete
}
[194] Fix | Delete
return ob_get_clean();
[195] Fix | Delete
}
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function