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 |