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 |