Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks
File: BlockPatterns.php
<?php
[0] Fix | Delete
declare(strict_types=1);
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\Blocks;
[3] Fix | Delete
[4] Fix | Delete
use Automattic\WooCommerce\Admin\Features\Features;
[5] Fix | Delete
use Automattic\WooCommerce\Blocks\Domain\Package;
[6] Fix | Delete
use Automattic\WooCommerce\Blocks\Patterns\PatternRegistry;
[7] Fix | Delete
use Automattic\WooCommerce\Blocks\Patterns\PTKPatternsStore;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Registers patterns under the `./patterns/` directory and from the PTK API and updates their content.
[11] Fix | Delete
* Each pattern from core is defined as a PHP file and defines its metadata using plugin-style headers.
[12] Fix | Delete
* The minimum required definition is:
[13] Fix | Delete
*
[14] Fix | Delete
* /**
[15] Fix | Delete
* * Title: My Pattern
[16] Fix | Delete
* * Slug: my-theme/my-pattern
[17] Fix | Delete
* *
[18] Fix | Delete
*
[19] Fix | Delete
* The output of the PHP source corresponds to the content of the pattern, e.g.:
[20] Fix | Delete
*
[21] Fix | Delete
* <main><p><?php echo "Hello"; ?></p></main>
[22] Fix | Delete
*
[23] Fix | Delete
* Other settable fields include:
[24] Fix | Delete
*
[25] Fix | Delete
* - Description
[26] Fix | Delete
* - Viewport Width
[27] Fix | Delete
* - Categories (comma-separated values)
[28] Fix | Delete
* - Keywords (comma-separated values)
[29] Fix | Delete
* - Block Types (comma-separated values)
[30] Fix | Delete
* - Inserter (yes/no)
[31] Fix | Delete
*
[32] Fix | Delete
* @internal
[33] Fix | Delete
*/
[34] Fix | Delete
class BlockPatterns {
[35] Fix | Delete
const CATEGORIES_PREFIXES = [ '_woo_', '_dotcom_imported_' ];
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Path to the patterns' directory.
[39] Fix | Delete
*
[40] Fix | Delete
* @var string $patterns_path
[41] Fix | Delete
*/
[42] Fix | Delete
private string $patterns_path;
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* PatternRegistry instance.
[46] Fix | Delete
*
[47] Fix | Delete
* @var PatternRegistry $pattern_registry
[48] Fix | Delete
*/
[49] Fix | Delete
private PatternRegistry $pattern_registry;
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* PTKPatternsStore instance.
[53] Fix | Delete
*
[54] Fix | Delete
* @var PTKPatternsStore $ptk_patterns_store
[55] Fix | Delete
*/
[56] Fix | Delete
private PTKPatternsStore $ptk_patterns_store;
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Constructor for class
[60] Fix | Delete
*
[61] Fix | Delete
* @param Package $package An instance of Package.
[62] Fix | Delete
* @param PatternRegistry $pattern_registry An instance of PatternRegistry.
[63] Fix | Delete
* @param PTKPatternsStore $ptk_patterns_store An instance of PTKPatternsStore.
[64] Fix | Delete
*/
[65] Fix | Delete
public function __construct(
[66] Fix | Delete
Package $package,
[67] Fix | Delete
PatternRegistry $pattern_registry,
[68] Fix | Delete
PTKPatternsStore $ptk_patterns_store
[69] Fix | Delete
) {
[70] Fix | Delete
$this->patterns_path = $package->get_path( 'patterns' );
[71] Fix | Delete
$this->pattern_registry = $pattern_registry;
[72] Fix | Delete
$this->ptk_patterns_store = $ptk_patterns_store;
[73] Fix | Delete
[74] Fix | Delete
add_action( 'init', array( $this, 'register_block_patterns' ) );
[75] Fix | Delete
[76] Fix | Delete
if ( Features::is_enabled( 'pattern-toolkit-full-composability' ) ) {
[77] Fix | Delete
add_action( 'init', array( $this, 'register_ptk_patterns' ) );
[78] Fix | Delete
}
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Loads the content of a pattern.
[83] Fix | Delete
*
[84] Fix | Delete
* @param string $pattern_path The path to the pattern.
[85] Fix | Delete
* @return string The content of the pattern.
[86] Fix | Delete
*/
[87] Fix | Delete
private function load_pattern_content( $pattern_path ) {
[88] Fix | Delete
if ( ! file_exists( $pattern_path ) ) {
[89] Fix | Delete
return '';
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
ob_start();
[93] Fix | Delete
include $pattern_path;
[94] Fix | Delete
return ob_get_clean();
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Register block patterns from core.
[99] Fix | Delete
*
[100] Fix | Delete
* @return void
[101] Fix | Delete
*/
[102] Fix | Delete
public function register_block_patterns() {
[103] Fix | Delete
if ( ! class_exists( 'WP_Block_Patterns_Registry' ) ) {
[104] Fix | Delete
return;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$patterns = $this->get_block_patterns();
[108] Fix | Delete
foreach ( $patterns as $pattern ) {
[109] Fix | Delete
/**
[110] Fix | Delete
* Handle backward compatibility for pattern source paths.
[111] Fix | Delete
* Previously, patterns were stored with absolute paths. Now we store relative paths.
[112] Fix | Delete
* If we encounter a pattern with an absolute path (containing $patterns_path),
[113] Fix | Delete
* we keep it as is. Otherwise, we construct the full path from the relative source.
[114] Fix | Delete
*
[115] Fix | Delete
* Remove the backward compatibility logic in the WooCommerce 10.1 lifecycle: https://github.com/woocommerce/woocommerce/issues/57354.
[116] Fix | Delete
*/
[117] Fix | Delete
$pattern_path = str_contains( $pattern['source'], $this->patterns_path ) ? $pattern['source'] : $this->patterns_path . '/' . $pattern['source'];
[118] Fix | Delete
$pattern['source'] = $pattern_path;
[119] Fix | Delete
[120] Fix | Delete
$content = $this->load_pattern_content( $pattern_path );
[121] Fix | Delete
$pattern['content'] = $content;
[122] Fix | Delete
[123] Fix | Delete
$this->pattern_registry->register_block_pattern( $pattern_path, $pattern );
[124] Fix | Delete
}
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Gets block pattern data from the cache if available
[129] Fix | Delete
*
[130] Fix | Delete
* @return array Block pattern data.
[131] Fix | Delete
*/
[132] Fix | Delete
private function get_block_patterns() {
[133] Fix | Delete
$pattern_data = $this->get_pattern_cache();
[134] Fix | Delete
[135] Fix | Delete
if ( is_array( $pattern_data ) ) {
[136] Fix | Delete
return $pattern_data;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
$default_headers = array(
[140] Fix | Delete
'title' => 'Title',
[141] Fix | Delete
'slug' => 'Slug',
[142] Fix | Delete
'description' => 'Description',
[143] Fix | Delete
'viewportWidth' => 'Viewport Width',
[144] Fix | Delete
'categories' => 'Categories',
[145] Fix | Delete
'keywords' => 'Keywords',
[146] Fix | Delete
'blockTypes' => 'Block Types',
[147] Fix | Delete
'inserter' => 'Inserter',
[148] Fix | Delete
'featureFlag' => 'Feature Flag',
[149] Fix | Delete
'templateTypes' => 'Template Types',
[150] Fix | Delete
);
[151] Fix | Delete
[152] Fix | Delete
if ( ! file_exists( $this->patterns_path ) ) {
[153] Fix | Delete
return array();
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
$files = glob( $this->patterns_path . '/*.php' );
[157] Fix | Delete
if ( ! $files ) {
[158] Fix | Delete
return array();
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
$patterns = array();
[162] Fix | Delete
[163] Fix | Delete
foreach ( $files as $file ) {
[164] Fix | Delete
$data = get_file_data( $file, $default_headers );
[165] Fix | Delete
// We want to store the relative path in the cache, so we can use it later to register the pattern.
[166] Fix | Delete
$data['source'] = str_replace( $this->patterns_path . '/', '', $file );
[167] Fix | Delete
$patterns[] = $data;
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
$this->set_pattern_cache( $patterns );
[171] Fix | Delete
return $patterns;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Gets block pattern cache.
[176] Fix | Delete
*
[177] Fix | Delete
* @return array|false Returns an array of patterns if cache is found, otherwise false.
[178] Fix | Delete
*/
[179] Fix | Delete
private function get_pattern_cache() {
[180] Fix | Delete
$pattern_data = get_site_transient( 'woocommerce_blocks_patterns' );
[181] Fix | Delete
[182] Fix | Delete
if ( is_array( $pattern_data ) && WOOCOMMERCE_VERSION === $pattern_data['version'] ) {
[183] Fix | Delete
return $pattern_data['patterns'];
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
return false;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Sets block pattern cache.
[191] Fix | Delete
*
[192] Fix | Delete
* @param array $patterns Block patterns data to set in cache.
[193] Fix | Delete
*/
[194] Fix | Delete
private function set_pattern_cache( array $patterns ) {
[195] Fix | Delete
$pattern_data = array(
[196] Fix | Delete
'version' => WOOCOMMERCE_VERSION,
[197] Fix | Delete
'patterns' => $patterns,
[198] Fix | Delete
);
[199] Fix | Delete
[200] Fix | Delete
set_site_transient( 'woocommerce_blocks_patterns', $pattern_data, MONTH_IN_SECONDS );
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Register patterns from the Patterns Toolkit.
[205] Fix | Delete
*
[206] Fix | Delete
* @return void
[207] Fix | Delete
*/
[208] Fix | Delete
public function register_ptk_patterns() {
[209] Fix | Delete
// Only if the user has allowed tracking, we register the patterns from the PTK.
[210] Fix | Delete
$allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
[211] Fix | Delete
if ( ! $allow_tracking ) {
[212] Fix | Delete
return;
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
// The most efficient way to check for an existing action is to use `as_has_scheduled_action`, but in unusual
[216] Fix | Delete
// cases where another plugin has loaded a very old version of Action Scheduler, it may not be available to us.
[217] Fix | Delete
$has_scheduled_action = function_exists( 'as_has_scheduled_action' ) ? 'as_has_scheduled_action' : 'as_next_scheduled_action';
[218] Fix | Delete
[219] Fix | Delete
$patterns = $this->ptk_patterns_store->get_patterns();
[220] Fix | Delete
if ( empty( $patterns ) || ! is_array( $patterns ) ) {
[221] Fix | Delete
// Only log once per day by using a transient.
[222] Fix | Delete
$transient_key = 'wc_ptk_pattern_store_warning';
[223] Fix | Delete
// By only logging when patterns are empty and no fetch is scheduled,
[224] Fix | Delete
// we ensure that warnings are only generated in genuinely problematic situations,
[225] Fix | Delete
// such as when the pattern fetching mechanism has failed entirely.
[226] Fix | Delete
if ( ! get_transient( $transient_key ) && ! call_user_func( $has_scheduled_action, 'fetch_patterns' ) ) {
[227] Fix | Delete
wc_get_logger()->warning(
[228] Fix | Delete
__( 'Empty patterns received from the PTK Pattern Store', 'woocommerce' ),
[229] Fix | Delete
);
[230] Fix | Delete
// Set the transient to true to indicate that the warning has been logged in the current day.
[231] Fix | Delete
set_transient( $transient_key, true, DAY_IN_SECONDS );
[232] Fix | Delete
}
[233] Fix | Delete
return;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
$patterns = $this->parse_categories( $patterns );
[237] Fix | Delete
[238] Fix | Delete
foreach ( $patterns as $pattern ) {
[239] Fix | Delete
$pattern['slug'] = $pattern['name'];
[240] Fix | Delete
$pattern['content'] = $pattern['html'];
[241] Fix | Delete
[242] Fix | Delete
$this->pattern_registry->register_block_pattern( $pattern['ID'], $pattern );
[243] Fix | Delete
}
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
/**
[247] Fix | Delete
* Parse prefixed categories from the PTK patterns into the actual WooCommerce categories.
[248] Fix | Delete
*
[249] Fix | Delete
* @param array $patterns The patterns to parse.
[250] Fix | Delete
* @return array The parsed patterns.
[251] Fix | Delete
*/
[252] Fix | Delete
private function parse_categories( array $patterns ) {
[253] Fix | Delete
return array_map(
[254] Fix | Delete
function ( $pattern ) {
[255] Fix | Delete
if ( ! isset( $pattern['categories'] ) ) {
[256] Fix | Delete
$pattern['categories'] = array();
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
$values = array_values( $pattern['categories'] );
[260] Fix | Delete
[261] Fix | Delete
foreach ( $values as $value ) {
[262] Fix | Delete
if ( ! isset( $value['title'] ) || ! isset( $value['slug'] ) ) {
[263] Fix | Delete
$pattern['categories'] = array();
[264] Fix | Delete
}
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
$pattern['categories'] = array_map(
[268] Fix | Delete
function ( $category ) {
[269] Fix | Delete
foreach ( self::CATEGORIES_PREFIXES as $prefix ) {
[270] Fix | Delete
if ( strpos( $category['title'], $prefix ) !== false ) {
[271] Fix | Delete
$parsed_category = str_replace( $prefix, '', $category['title'] );
[272] Fix | Delete
$parsed_category = str_replace( '_', ' ', $parsed_category );
[273] Fix | Delete
$category['title'] = ucfirst( $parsed_category );
[274] Fix | Delete
}
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
return $category;
[278] Fix | Delete
},
[279] Fix | Delete
$pattern['categories']
[280] Fix | Delete
);
[281] Fix | Delete
return $pattern;
[282] Fix | Delete
},
[283] Fix | Delete
$patterns
[284] Fix | Delete
);
[285] Fix | Delete
}
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function