Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Utils
File: BlockHooksTrait.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Utils;
[1] Fix | Delete
[2] Fix | Delete
/**
[3] Fix | Delete
* BlockHooksTrait
[4] Fix | Delete
*
[5] Fix | Delete
* Shared functionality for using the Block Hooks API with WooCommerce Blocks.
[6] Fix | Delete
*/
[7] Fix | Delete
trait BlockHooksTrait {
[8] Fix | Delete
/**
[9] Fix | Delete
* Callback for `hooked_block_types` to auto-inject the mini-cart block into headers after navigation.
[10] Fix | Delete
*
[11] Fix | Delete
* @param array $hooked_blocks An array of block slugs hooked into a given context.
[12] Fix | Delete
* @param string $position Position of the block insertion point.
[13] Fix | Delete
* @param string $anchor_block The block acting as the anchor for the inserted block.
[14] Fix | Delete
* @param array|\WP_Post|\WP_Block_Template $context Where the block is embedded.
[15] Fix | Delete
* @since 8.5.0
[16] Fix | Delete
* @return array An array of block slugs hooked into a given context.
[17] Fix | Delete
*/
[18] Fix | Delete
public function register_hooked_block( $hooked_blocks, $position, $anchor_block, $context ) {
[19] Fix | Delete
// If the block has no hook placements, return early.
[20] Fix | Delete
if ( ! isset( $this->hooked_block_placements ) || empty( $this->hooked_block_placements ) ) {
[21] Fix | Delete
return $hooked_blocks;
[22] Fix | Delete
}
[23] Fix | Delete
[24] Fix | Delete
// Cache the block hooks version.
[25] Fix | Delete
static $block_hooks_version = null;
[26] Fix | Delete
if ( defined( 'WP_RUN_CORE_TESTS' ) || is_null( $block_hooks_version ) ) {
[27] Fix | Delete
$block_hooks_version = get_option( 'woocommerce_hooked_blocks_version' );
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
// If block hooks are disabled or the version is not set, return early.
[31] Fix | Delete
if ( 'no' === $block_hooks_version || false === $block_hooks_version ) {
[32] Fix | Delete
return $hooked_blocks;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
// Valid placements are those that have no version specified,
[36] Fix | Delete
// or have a version that is less than or equal to version specified in the woocommerce_hooked_blocks_version option.
[37] Fix | Delete
$valid_placements = array_filter(
[38] Fix | Delete
$this->hooked_block_placements,
[39] Fix | Delete
function ( $placement ) use ( $block_hooks_version ) {
[40] Fix | Delete
$placement_version = isset( $placement['version'] ) ? $placement['version'] : null;
[41] Fix | Delete
return is_null( $placement_version ) || ! is_null( $placement_version ) && version_compare( $block_hooks_version, $placement_version, '>=' );
[42] Fix | Delete
}
[43] Fix | Delete
);
[44] Fix | Delete
[45] Fix | Delete
if ( $context && ! empty( $valid_placements ) ) {
[46] Fix | Delete
foreach ( $valid_placements as $placement ) {
[47] Fix | Delete
[48] Fix | Delete
if ( $placement['position'] === $position && $placement['anchor'] === $anchor_block ) {
[49] Fix | Delete
// If an area has been specified for this placement.
[50] Fix | Delete
if (
[51] Fix | Delete
isset( $placement['area'] ) &&
[52] Fix | Delete
! $this->has_block_in_content( $context )
[53] Fix | Delete
&& $this->is_target_area( $context, $placement['area'] )
[54] Fix | Delete
) {
[55] Fix | Delete
$hooked_blocks[] = $this->namespace . '/' . $this->block_name;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
// If no area has been specified for this placement just insert the block.
[59] Fix | Delete
// This is likely to be the case when we're inserting into the navigation block
[60] Fix | Delete
// where we don't have a specific area to target.
[61] Fix | Delete
if ( ! isset( $placement['area'] ) ) {
[62] Fix | Delete
$hooked_blocks[] = $this->namespace . '/' . $this->block_name;
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
// If a callback has been specified for this placement, call it. This allows for custom block-specific logic to be run.
[66] Fix | Delete
$callback = isset( $placement['callback'] ) && is_callable( array( $this, $placement['callback'] ) ) ? array( $this, $placement['callback'] ) : null;
[67] Fix | Delete
if ( null !== $callback ) {
[68] Fix | Delete
$modified_hooked_blocks = $callback( $hooked_blocks, $position, $anchor_block, $context );
[69] Fix | Delete
if ( is_array( $modified_hooked_blocks ) ) {
[70] Fix | Delete
$hooked_blocks = $modified_hooked_blocks;
[71] Fix | Delete
}
[72] Fix | Delete
}
[73] Fix | Delete
}
[74] Fix | Delete
}
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
return $hooked_blocks;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Checks if the provided context contains a the block already.
[82] Fix | Delete
*
[83] Fix | Delete
* @param array|\WP_Block_Template $context Where the block is embedded.
[84] Fix | Delete
* @return boolean
[85] Fix | Delete
*/
[86] Fix | Delete
protected function has_block_in_content( $context ) {
[87] Fix | Delete
$content = $this->get_context_content( $context );
[88] Fix | Delete
return strpos( $content, 'wp:' . $this->namespace . '/' . $this->block_name ) !== false;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Given a provided context, returns the content of the context.
[93] Fix | Delete
*
[94] Fix | Delete
* @param array|\WP_Post|\WP_Block_Template $context Where the block is embedded.
[95] Fix | Delete
* @since 8.5.0
[96] Fix | Delete
* @return string
[97] Fix | Delete
*/
[98] Fix | Delete
protected function get_context_content( $context ) {
[99] Fix | Delete
$content = is_array( $context ) && isset( $context['content'] ) ? $context['content'] : '';
[100] Fix | Delete
$content = '' === $content && $context instanceof \WP_Block_Template ? $context->content : $content;
[101] Fix | Delete
$content = '' === $content && $context instanceof \WP_Post ? $context->post_content : $content;
[102] Fix | Delete
return $content;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Given a provided context, returns whether the context refers to header content.
[107] Fix | Delete
*
[108] Fix | Delete
* @param array|\WP_Post|\WP_Block_Template $context Where the block is embedded.
[109] Fix | Delete
* @param string $area The area to check against before inserting.
[110] Fix | Delete
* @since 8.5.0
[111] Fix | Delete
* @return boolean
[112] Fix | Delete
*/
[113] Fix | Delete
protected function is_template_part_or_pattern( $context, $area ) {
[114] Fix | Delete
$is_pattern = is_array( $context ) &&
[115] Fix | Delete
(
[116] Fix | Delete
( isset( $context['blockTypes'] ) && in_array( 'core/template-part/' . $area, $context['blockTypes'], true ) ) ||
[117] Fix | Delete
( isset( $context['categories'] ) && in_array( $area, $context['categories'], true ) )
[118] Fix | Delete
);
[119] Fix | Delete
$is_template_part = $context instanceof \WP_Block_Template && $area === $context->area;
[120] Fix | Delete
return ( $is_pattern || $is_template_part );
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Given a provided context, returns whether the context refers to the target area and isn't marked as excluded.
[125] Fix | Delete
*
[126] Fix | Delete
* @param array|\WP_Post|\WP_Block_Template $context the context to check.
[127] Fix | Delete
* @param string $area The area to check against before inserting.
[128] Fix | Delete
* @since 8.5.0
[129] Fix | Delete
* @return boolean
[130] Fix | Delete
*/
[131] Fix | Delete
protected function is_target_area( $context, $area ) {
[132] Fix | Delete
if ( $this->is_template_part_or_pattern( $context, $area ) && ! $this->pattern_is_excluded( $context ) ) {
[133] Fix | Delete
return true;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
return false;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Returns whether the pattern is excluded or not
[141] Fix | Delete
*
[142] Fix | Delete
* @since 8.5.0
[143] Fix | Delete
*
[144] Fix | Delete
* @param array|\WP_Block_Template $context Where the block is embedded.
[145] Fix | Delete
* @return boolean
[146] Fix | Delete
*/
[147] Fix | Delete
protected function pattern_is_excluded( $context ) {
[148] Fix | Delete
/**
[149] Fix | Delete
* A list of pattern slugs to exclude from auto-insert (useful when there are patterns that have a very specific location for the block)
[150] Fix | Delete
* Note: The patterns that are currently excluded are the ones that don't work well with the mini-cart block or customer-account block.
[151] Fix | Delete
*
[152] Fix | Delete
* @since 8.5.0
[153] Fix | Delete
*/
[154] Fix | Delete
$pattern_exclude_list = apply_filters(
[155] Fix | Delete
'woocommerce_hooked_blocks_pattern_exclude_list',
[156] Fix | Delete
array_unique( array_merge( isset( $this->hooked_block_excluded_patterns ) ? $this->hooked_block_excluded_patterns : array(), array( 'twentytwentytwo/header-centered-logo', 'twentytwentytwo/header-stacked' ) ) )
[157] Fix | Delete
);
[158] Fix | Delete
[159] Fix | Delete
$pattern_slug = is_array( $context ) && isset( $context['slug'] ) ? $context['slug'] : '';
[160] Fix | Delete
if ( ! $pattern_slug ) {
[161] Fix | Delete
/**
[162] Fix | Delete
* Woo patterns have a slug property in $context, but core/theme patterns dont.
[163] Fix | Delete
* In that case, we fallback to the name property, as they're the same.
[164] Fix | Delete
*/
[165] Fix | Delete
$pattern_slug = is_array( $context ) && isset( $context['name'] ) ? $context['name'] : '';
[166] Fix | Delete
}
[167] Fix | Delete
return in_array( $pattern_slug, $pattern_exclude_list, true );
[168] Fix | Delete
}
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function