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.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / BlockPatterns.php
woocommerce / src / Blocks Last commit date
AI 1 year ago AIContent 1 year ago Assets 3 weeks ago BlockTypes 1 week ago Domain 1 month ago Images 1 year ago Integrations 2 years ago Patterns 6 months ago Payments 5 months ago Registry 2 years ago SharedStores 1 month ago Shipping 7 months ago Templates 1 month ago Utils 3 weeks ago Assets.php 2 years ago AssetsController.php 3 weeks ago BlockPatterns.php 7 months ago BlockTemplatesController.php 7 months ago BlockTemplatesRegistry.php 10 months ago BlockTypesController.php 3 weeks ago DependencyDetection.php 5 months ago InboxNotifications.php 2 years ago Installer.php 1 month ago Library.php 2 years ago Options.php 2 years ago Package.php 1 year ago QueryFilters.php 7 months ago TemplateOptions.php 1 year ago
BlockPatterns.php
281 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 $pattern_path = $this->patterns_path . '/' . $pattern['source'];
112 $pattern['source'] = $pattern_path;
113
114 $content = $this->load_pattern_content( $pattern_path );
115 $pattern['content'] = $content;
116
117 $this->pattern_registry->register_block_pattern( $pattern_path, $pattern );
118 }
119 }
120
121 /**
122 * Gets block pattern data from the cache if available
123 *
124 * @return array Block pattern data.
125 */
126 private function get_block_patterns() {
127 $pattern_data = $this->get_pattern_cache();
128
129 if ( is_array( $pattern_data ) ) {
130 return $pattern_data;
131 }
132
133 $default_headers = array(
134 'title' => 'Title',
135 'slug' => 'Slug',
136 'description' => 'Description',
137 'viewportWidth' => 'Viewport Width',
138 'categories' => 'Categories',
139 'keywords' => 'Keywords',
140 'blockTypes' => 'Block Types',
141 'inserter' => 'Inserter',
142 'featureFlag' => 'Feature Flag',
143 'templateTypes' => 'Template Types',
144 );
145
146 if ( ! file_exists( $this->patterns_path ) ) {
147 return array();
148 }
149
150 $files = glob( $this->patterns_path . '/*.php' );
151 if ( ! $files ) {
152 return array();
153 }
154
155 $patterns = array();
156
157 foreach ( $files as $file ) {
158 $data = get_file_data( $file, $default_headers );
159 // We want to store the relative path in the cache, so we can use it later to register the pattern.
160 $data['source'] = str_replace( $this->patterns_path . '/', '', $file );
161 $patterns[] = $data;
162 }
163
164 $this->set_pattern_cache( $patterns );
165 return $patterns;
166 }
167
168 /**
169 * Gets block pattern cache.
170 *
171 * @return array|false Returns an array of patterns if cache is found, otherwise false.
172 */
173 private function get_pattern_cache() {
174 $pattern_data = get_site_transient( 'woocommerce_blocks_patterns' );
175
176 if ( is_array( $pattern_data ) && WOOCOMMERCE_VERSION === $pattern_data['version'] ) {
177 return $pattern_data['patterns'];
178 }
179
180 return false;
181 }
182
183 /**
184 * Sets block pattern cache.
185 *
186 * @param array $patterns Block patterns data to set in cache.
187 */
188 private function set_pattern_cache( array $patterns ) {
189 $pattern_data = array(
190 'version' => WOOCOMMERCE_VERSION,
191 'patterns' => $patterns,
192 );
193
194 set_site_transient( 'woocommerce_blocks_patterns', $pattern_data, MONTH_IN_SECONDS );
195 }
196
197 /**
198 * Register patterns from the Patterns Toolkit.
199 *
200 * @return void
201 */
202 public function register_ptk_patterns() {
203 // Only if the user has allowed tracking, we register the patterns from the PTK.
204 $allow_tracking = 'yes' === get_option( 'woocommerce_allow_tracking' );
205 if ( ! $allow_tracking ) {
206 return;
207 }
208
209 // The most efficient way to check for an existing action is to use `as_has_scheduled_action`, but in unusual
210 // cases where another plugin has loaded a very old version of Action Scheduler, it may not be available to us.
211 $has_scheduled_action = function_exists( 'as_has_scheduled_action' ) ? 'as_has_scheduled_action' : 'as_next_scheduled_action';
212
213 $patterns = $this->ptk_patterns_store->get_patterns();
214 if ( empty( $patterns ) || ! is_array( $patterns ) ) {
215 // Only log once per day by using a transient.
216 $transient_key = 'wc_ptk_pattern_store_warning';
217 // By only logging when patterns are empty and no fetch is scheduled,
218 // we ensure that warnings are only generated in genuinely problematic situations,
219 // such as when the pattern fetching mechanism has failed entirely.
220 if ( ! get_transient( $transient_key ) && ! call_user_func( $has_scheduled_action, 'fetch_patterns' ) ) {
221 wc_get_logger()->warning(
222 __( 'Empty patterns received from the PTK Pattern Store', 'woocommerce' ),
223 );
224 // Set the transient to true to indicate that the warning has been logged in the current day.
225 set_transient( $transient_key, true, DAY_IN_SECONDS );
226 }
227 return;
228 }
229
230 $patterns = $this->parse_categories( $patterns );
231
232 foreach ( $patterns as $pattern ) {
233 $pattern['slug'] = $pattern['name'];
234 $pattern['content'] = $pattern['html'];
235
236 $this->pattern_registry->register_block_pattern( $pattern['ID'], $pattern );
237 }
238 }
239
240 /**
241 * Parse prefixed categories from the PTK patterns into the actual WooCommerce categories.
242 *
243 * @param array $patterns The patterns to parse.
244 * @return array The parsed patterns.
245 */
246 private function parse_categories( array $patterns ) {
247 return array_map(
248 function ( $pattern ) {
249 if ( ! isset( $pattern['categories'] ) ) {
250 $pattern['categories'] = array();
251 }
252
253 $values = array_values( $pattern['categories'] );
254
255 foreach ( $values as $value ) {
256 if ( ! isset( $value['title'] ) || ! isset( $value['slug'] ) ) {
257 $pattern['categories'] = array();
258 }
259 }
260
261 $pattern['categories'] = array_map(
262 function ( $category ) {
263 foreach ( self::CATEGORIES_PREFIXES as $prefix ) {
264 if ( strpos( $category['title'], $prefix ) !== false ) {
265 $parsed_category = str_replace( $prefix, '', $category['title'] );
266 $parsed_category = str_replace( '_', ' ', $parsed_category );
267 $category['title'] = ucfirst( $parsed_category );
268 }
269 }
270
271 return $category;
272 },
273 $pattern['categories']
274 );
275 return $pattern;
276 },
277 $patterns
278 );
279 }
280 }
281