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