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rc.2 3.1.0 6.2.1 3.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 / Patterns / PTKPatternsStore.php
woocommerce / src / Blocks / Patterns Last commit date
AIPatterns.php 1 year ago PTKClient.php 1 year ago PTKPatternsStore.php 6 months ago PatternRegistry.php 1 year ago
PTKPatternsStore.php
316 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Blocks\Patterns;
4
5 use Automattic\WooCommerce\Admin\Features\Features;
6 use WP_Upgrader;
7
8 /**
9 * PTKPatterns class.
10 *
11 * @internal
12 */
13 class PTKPatternsStore {
14 const OPTION_NAME = 'ptk_patterns';
15
16 /**
17 * Hook and action name used to trigger fetching patterns.
18 */
19 const FETCH_PATTERNS_ACTION = 'fetch_patterns';
20
21 const CATEGORY_MAPPING = array(
22 'testimonials' => 'reviews',
23 );
24
25 /**
26 * PatternsToolkit instance.
27 *
28 * @var PTKClient $ptk_client
29 */
30 private PTKClient $ptk_client;
31
32 /**
33 * Constructor for the class.
34 *
35 * @param PTKClient $ptk_client An instance of PatternsToolkit.
36 */
37 public function __construct( PTKClient $ptk_client ) {
38 $this->ptk_client = $ptk_client;
39
40 if ( Features::is_enabled( 'pattern-toolkit-full-composability' ) ) {
41 // We want to flush the cached patterns when:
42 // - The WooCommerce plugin is deactivated.
43 // - The `woocommerce_allow_tracking` option is disabled.
44 //
45 // We also want to re-fetch the patterns and update the cache when:
46 // - The `woocommerce_allow_tracking` option changes to enabled.
47 // - The WooCommerce plugin is activated (if `woocommerce_allow_tracking` is enabled).
48 // - The WooCommerce plugin is updated.
49
50 add_action( 'woocommerce_activated_plugin', array( $this, 'flush_or_fetch_patterns' ), 10, 2 );
51 add_action( 'update_option_woocommerce_allow_tracking', array( $this, 'flush_or_fetch_patterns' ), 10, 2 );
52 add_action( 'deactivated_plugin', array( $this, 'flush_cached_patterns' ), 10, 2 );
53 add_action( 'upgrader_process_complete', array( $this, 'fetch_patterns_on_plugin_update' ), 10, 2 );
54 add_action( 'action_scheduler_ensure_recurring_actions', array( $this, 'ensure_recurring_fetch_patterns_if_enabled' ) );
55
56 // This is the scheduled action that takes care of flushing and re-fetching the patterns from the PTK API.
57 add_action( self::FETCH_PATTERNS_ACTION, array( $this, 'fetch_patterns' ) );
58 }
59 }
60
61 /**
62 * Resets the cached patterns when the `woocommerce_allow_tracking` option is disabled.
63 * Resets and fetch the patterns from the PTK when it is enabled (if the scheduler
64 * is initialized, it's done asynchronously via a scheduled action).
65 *
66 * @return void
67 */
68 public function flush_or_fetch_patterns() {
69 if ( $this->allowed_tracking_is_enabled() ) {
70 $this->schedule_fetch_patterns();
71 return;
72 }
73
74 $this->flush_cached_patterns();
75 }
76
77 /**
78 * Schedule an async action to fetch the PTK patterns when the scheduler is initialized.
79 *
80 * @return void
81 */
82 private function schedule_fetch_patterns() {
83 if ( did_action( 'action_scheduler_init' ) ) {
84 $this->schedule_action_if_not_pending( self::FETCH_PATTERNS_ACTION );
85 } else {
86 add_action(
87 'action_scheduler_init',
88 function () {
89 $this->schedule_action_if_not_pending( self::FETCH_PATTERNS_ACTION );
90 }
91 );
92 }
93 }
94
95 /**
96 * Ensure a recurring fetch patterns action is scheduled.
97 * This is called by the `action_scheduler_ensure_recurring_actions` hook.
98 *
99 * @return void
100 */
101 public function ensure_recurring_fetch_patterns_if_enabled() {
102 if ( ! $this->allowed_tracking_is_enabled() ) {
103 return;
104 }
105
106 $this->schedule_action_if_not_pending( self::FETCH_PATTERNS_ACTION );
107 }
108
109 /**
110 * Schedule an action if it's not already pending.
111 *
112 * @param string $action The action name to schedule.
113 * @return void
114 */
115 private function schedule_action_if_not_pending( $action ) {
116 if ( as_has_scheduled_action( $action, array(), 'woocommerce' ) ) {
117 return;
118 }
119
120 as_schedule_recurring_action( time(), DAY_IN_SECONDS, $action, array(), 'woocommerce' );
121 }
122
123 /**
124 * Get the patterns from the Patterns Toolkit cache.
125 *
126 * @return array
127 */
128 public function get_patterns() {
129 $patterns = get_option( self::OPTION_NAME );
130
131 // If the current data doesn't exist or is invalid, schedule fetching the patterns from the PTK.
132 if ( false === $patterns || ! $this->ptk_client->is_valid_schema( $patterns ) ) {
133 $this->schedule_fetch_patterns();
134 return array();
135 }
136
137 return $patterns;
138 }
139
140 /**
141 * Filter the patterns that have external dependencies.
142 *
143 * @param array $patterns The patterns to filter.
144 * @return array
145 */
146 private function filter_patterns( array $patterns ) {
147 return array_values(
148 array_filter(
149 $patterns,
150 function ( $pattern ) {
151 if ( ! isset( $pattern['ID'] ) ) {
152 return true;
153 }
154
155 if ( isset( $pattern['post_type'] ) && 'wp_block' !== $pattern['post_type'] ) {
156 return false;
157 }
158
159 if ( $this->has_external_dependencies( $pattern ) ) {
160 return false;
161 }
162
163 return true;
164 }
165 )
166 );
167 }
168
169 /**
170 * Re-fetch the patterns when the WooCommerce plugin is updated.
171 *
172 * @param WP_Upgrader $upgrader_object WP_Upgrader instance.
173 * @param array $options Array of bulk item update data.
174 *
175 * @return void
176 */
177 public function fetch_patterns_on_plugin_update( $upgrader_object, $options ) {
178 if ( 'update' === $options['action'] && 'plugin' === $options['type'] && isset( $options['plugins'] ) ) {
179 foreach ( $options['plugins'] as $plugin ) {
180 if ( str_contains( $plugin, 'woocommerce.php' ) ) {
181 $this->schedule_fetch_patterns();
182 }
183 }
184 }
185 }
186
187 /**
188 * Reset the cached patterns to fetch them again from the PTK.
189 *
190 * @since 10.4.1 Unscheduling is deferred if Action Scheduler hasn't initialized yet.
191 * @return void
192 */
193 public function flush_cached_patterns() {
194 delete_option( self::OPTION_NAME );
195
196 if ( ! function_exists( 'as_unschedule_all_actions' ) ) {
197 return;
198 }
199
200 // Unschedule any existing fetch_patterns actions.
201 // Defer unscheduling until Action Scheduler is ready to avoid errors during early initialization.
202 if ( did_action( 'action_scheduler_init' ) ) {
203 as_unschedule_all_actions( self::FETCH_PATTERNS_ACTION, array(), 'woocommerce' );
204 } else {
205 add_action(
206 'action_scheduler_init',
207 function () {
208 as_unschedule_all_actions( self::FETCH_PATTERNS_ACTION, array(), 'woocommerce' );
209 }
210 );
211 }
212 }
213
214 /**
215 * Reset the cached patterns and fetch them again from the PTK API.
216 *
217 * @return void
218 */
219 public function fetch_patterns() {
220 if ( ! $this->allowed_tracking_is_enabled() ) {
221 return;
222 }
223
224 $patterns = $this->ptk_client->fetch_patterns(
225 array(
226 // This is the site where the patterns are stored. Despite the 'wpcomstaging.com' domain suggesting a staging environment, this URL points to the production environment where stable versions of the patterns are maintained.
227 'site' => 'wooblockpatterns.wpcomstaging.com',
228 'categories' => array(
229 '_woo_intro',
230 '_woo_featured_selling',
231 '_woo_about',
232 '_woo_reviews',
233 '_woo_social_media',
234 '_woo_woocommerce',
235 '_dotcom_imported_intro',
236 '_dotcom_imported_about',
237 '_dotcom_imported_services',
238 '_dotcom_imported_reviews',
239 ),
240 )
241 );
242
243 if ( is_wp_error( $patterns ) ) {
244 wc_get_logger()->warning(
245 sprintf(
246 // translators: %s is a generated error message.
247 __( 'Failed to get WooCommerce patterns from the PTK: "%s"', 'woocommerce' ),
248 $patterns->get_error_message()
249 ),
250 );
251 return;
252 }
253
254 $patterns = $this->filter_patterns( $patterns );
255 $patterns = $this->map_categories( $patterns );
256
257 update_option( self::OPTION_NAME, $patterns, false );
258 }
259
260 /**
261 * Check if the user allowed tracking.
262 *
263 * @return bool
264 */
265 private function allowed_tracking_is_enabled(): bool {
266 return 'yes' === get_option( 'woocommerce_allow_tracking' );
267 }
268
269 /**
270 * Change the categories of the patterns to match the ones used in the CYS flow
271 *
272 * @param array $patterns The patterns to map categories for.
273 * @return array The patterns with the categories mapped.
274 */
275 private function map_categories( array $patterns ) {
276 return array_map(
277 function ( $pattern ) {
278 if ( isset( $pattern['categories'] ) ) {
279 foreach ( $pattern['categories'] as $key => $category ) {
280 if ( isset( $category['slug'] ) && isset( self::CATEGORY_MAPPING[ $key ] ) ) {
281 $new_category = self::CATEGORY_MAPPING[ $key ];
282 unset( $pattern['categories'][ $key ] );
283 $pattern['categories'][ $new_category ]['slug'] = $new_category;
284 $pattern['categories'][ $new_category ]['title'] = ucfirst( $new_category );
285 }
286 }
287 }
288
289 return $pattern;
290 },
291 $patterns
292 );
293 }
294
295 /**
296 * Check if the pattern has external dependencies.
297 *
298 * @param array $pattern The pattern to check.
299 *
300 * @return bool
301 */
302 private function has_external_dependencies( $pattern ) {
303 if ( ! isset( $pattern['dependencies'] ) || ! is_array( $pattern['dependencies'] ) ) {
304 return false;
305 }
306
307 foreach ( $pattern['dependencies'] as $dependency ) {
308 if ( 'woocommerce' !== $dependency ) {
309 return true;
310 }
311 }
312
313 return false;
314 }
315 }
316