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