API
6 years ago
Admin
6 years ago
Emails
6 years ago
Gateway
6 years ago
Handlers
6 years ago
Sync
6 years ago
Utilities
6 years ago
admin
6 years ago
AJAX.php
6 years ago
API.php
6 years ago
Admin.php
6 years ago
Functions.php
6 years ago
Gateway.php
6 years ago
Lifecycle.php
6 years ago
Plugin.php
6 years ago
Settings.php
6 years ago
Lifecycle.php
381 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square; |
| 25 | |
| 26 | defined( 'ABSPATH' ) or exit; |
| 27 | |
| 28 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 29 | use WooCommerce\Square\Handlers\Product; |
| 30 | |
| 31 | /** |
| 32 | * The plugin lifecycle handler. |
| 33 | * |
| 34 | * @since 2.0.0 |
| 35 | * |
| 36 | * @method Plugin get_plugin() |
| 37 | */ |
| 38 | class Lifecycle extends Framework\Plugin\Lifecycle { |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Lifecycle constructor. |
| 43 | * |
| 44 | * @since 2.0.0 |
| 45 | * |
| 46 | * @param Plugin $plugin main instance |
| 47 | */ |
| 48 | public function __construct( Plugin $plugin ) { |
| 49 | |
| 50 | parent::__construct( $plugin ); |
| 51 | |
| 52 | // plugin upgrade path: maps automatically each semver to upgrade_to_x_y_z() protected method |
| 53 | $this->upgrade_versions = [ |
| 54 | '2.0.0', |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Performs plugin installation. |
| 61 | * |
| 62 | * @since 2.0.0 |
| 63 | */ |
| 64 | protected function install() { |
| 65 | |
| 66 | // create the db table for the customer index |
| 67 | Gateway\Customer_Helper::create_table(); |
| 68 | |
| 69 | /** |
| 70 | * Fires upon plugin installed. |
| 71 | * |
| 72 | * @since 2.0.0 |
| 73 | * |
| 74 | * @param string $version plugin version (available from v2.0.0) |
| 75 | */ |
| 76 | do_action( 'wc_square_installed', Plugin::VERSION ); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Performs upgrade tasks. |
| 82 | * |
| 83 | * @since 2.0.0 |
| 84 | * |
| 85 | * @param string $installed_version semver |
| 86 | */ |
| 87 | protected function upgrade( $installed_version ) { |
| 88 | |
| 89 | parent::upgrade( $installed_version ); |
| 90 | |
| 91 | /** |
| 92 | * Fires upon plugin upgraded (legacy hook). |
| 93 | * |
| 94 | * @since 1.0.0 |
| 95 | * |
| 96 | * @param string $version version updating to (available from v2.0.0) |
| 97 | * @param string $version version updating from (available from v2.0.0) |
| 98 | */ |
| 99 | do_action( 'wc_square_updated', Plugin::VERSION, $installed_version ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Upgrades to version 2.0.0 |
| 105 | * |
| 106 | * @since 2.0.0 |
| 107 | */ |
| 108 | protected function upgrade_to_2_0_0() { |
| 109 | |
| 110 | // create the db table for the customer index |
| 111 | Gateway\Customer_Helper::create_table(); |
| 112 | |
| 113 | /** @see \wc_set_time_limit() */ |
| 114 | if ( function_exists( 'set_time_limit' ) |
| 115 | && ! ini_get( 'safe_mode' ) |
| 116 | && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) ) { |
| 117 | |
| 118 | @set_time_limit( 300 ); |
| 119 | } |
| 120 | |
| 121 | // migrate all the things! |
| 122 | $this->migrate_plugin_settings(); |
| 123 | $this->migrate_gateway_settings(); |
| 124 | $this->migrate_orders(); |
| 125 | $this->migrate_products(); |
| 126 | $this->migrate_customers(); |
| 127 | |
| 128 | // assume a last sync occurred before upgrading |
| 129 | $this->get_plugin()->get_sync_handler()->set_last_synced_at(); |
| 130 | $this->get_plugin()->get_sync_handler()->set_inventory_last_synced_at(); |
| 131 | |
| 132 | // mark upgrade complete |
| 133 | update_option( 'wc_square_updated_to_2_0_0', true ); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Migrates plugin settings from v1 to v2. |
| 139 | * |
| 140 | * @see Lifecycle::upgrade_to_2_0_0() |
| 141 | * |
| 142 | * @since 2.0.0 |
| 143 | */ |
| 144 | private function migrate_plugin_settings() { |
| 145 | |
| 146 | $this->get_plugin()->log( 'Migrating plugin settings...' ); |
| 147 | |
| 148 | // get legacy and new default settings |
| 149 | $new_settings = get_option( 'wc_square_settings', [] ); |
| 150 | $legacy_settings = get_option( 'woocommerce_squareconnect_settings', [] ); |
| 151 | $email_settings = get_option( 'woocommerce_wc_square_sync_completed_settings', [] ); |
| 152 | |
| 153 | // bail if they already have v2 settings present |
| 154 | if ( ! empty( $new_settings ) ) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | // handle access token first |
| 159 | if ( $legacy_access_token = get_option( 'woocommerce_square_merchant_access_token' ) ) { |
| 160 | |
| 161 | // the access token was previously stored unencrypted |
| 162 | if ( ! empty( $legacy_access_token ) && Utilities\Encryption_Utility::is_encryption_supported() ) { |
| 163 | |
| 164 | $encryption = new Utilities\Encryption_Utility(); |
| 165 | |
| 166 | try { |
| 167 | $legacy_access_token = $encryption->encrypt_data( $legacy_access_token ); |
| 168 | } catch ( Framework\SV_WC_Plugin_Exception $exception ) { |
| 169 | // log the event, but don't halt the process |
| 170 | $this->get_plugin()->log( 'Could not encrypt access token during upgrade. ' . $exception->getMessage() ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // previously only 'production' environment was assumed |
| 175 | $access_tokens = get_option( 'wc_square_access_tokens', [] ); |
| 176 | $access_tokens['production'] = is_string( $legacy_access_token ) ? $legacy_access_token : ''; |
| 177 | |
| 178 | update_option( 'wc_square_access_tokens', $access_tokens ); |
| 179 | } |
| 180 | |
| 181 | // migrate store location |
| 182 | if ( ! empty( $legacy_settings['location'] ) ) { |
| 183 | $new_settings['location_id'] = $legacy_settings['location']; |
| 184 | } |
| 185 | |
| 186 | // toggle debug logging |
| 187 | $new_settings['debug_logging_enabled'] = isset( $legacy_settings['logging'] ) && in_array( $legacy_settings['logging'], [ 'yes', 'no' ], true ) ? $legacy_settings['logging'] : 'no'; |
| 188 | |
| 189 | // handle system of record: previously only Square was supported |
| 190 | if ( isset( $legacy_settings['sync_products'] ) && 'yes' === $legacy_settings['sync_products'] ) { |
| 191 | $new_settings['system_of_record'] = 'square'; |
| 192 | } else { |
| 193 | $new_settings['system_of_record'] = 'disabled'; |
| 194 | } |
| 195 | |
| 196 | // migrate email notification settings: if there's a recipient, we enable email and pass recipient(s) to email setting |
| 197 | if ( isset( $legacy_settings['sync_email'] ) && is_string( $legacy_settings['sync_email'] ) && '' !== trim( $legacy_settings['sync_email'] ) ) { |
| 198 | $email_settings['enabled'] = 'yes'; |
| 199 | $email_settings['recipient'] = $legacy_settings['sync_email'] ; |
| 200 | } else { |
| 201 | $email_settings['enabled'] = 'no'; |
| 202 | $email_settings['recipient'] = ''; |
| 203 | } |
| 204 | |
| 205 | // save email settings |
| 206 | update_option( 'woocommerce_wc_square_sync_completed_settings', $email_settings ); |
| 207 | // save plugin settings |
| 208 | update_option( 'wc_square_settings', $new_settings ); |
| 209 | |
| 210 | $this->get_plugin()->log( 'Plugin settings migration complete.' ); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * Migrates gateway settings from v1 to v2. |
| 216 | * |
| 217 | * @see Lifecycle::upgrade_to_2_0_0() |
| 218 | * |
| 219 | * @since 2.0.0 |
| 220 | */ |
| 221 | private function migrate_gateway_settings() { |
| 222 | |
| 223 | $this->get_plugin()->log( 'Migrating gateway settings...' ); |
| 224 | |
| 225 | $legacy_settings = get_option( 'woocommerce_square_settings', [] ); |
| 226 | $new_settings = get_option( 'woocommerce_square_credit_card_settings', [] ); |
| 227 | |
| 228 | // bail if they already have v2 settings present |
| 229 | if ( ! empty( $new_settings ) ) { |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | if ( isset( $legacy_settings['enabled'] ) ) { |
| 234 | $new_settings['enabled'] = 'yes' === $legacy_settings['enabled'] ? 'yes' : 'no'; |
| 235 | } |
| 236 | |
| 237 | if ( isset( $legacy_settings['title'] ) && is_string( $legacy_settings['title'] ) ) { |
| 238 | $new_settings['title'] = $legacy_settings['title']; |
| 239 | } |
| 240 | |
| 241 | if ( isset( $legacy_settings['description'] ) && is_string( $legacy_settings['description'] ) ) { |
| 242 | $new_settings['description'] = $legacy_settings['description']; |
| 243 | } |
| 244 | |
| 245 | // note: the following is not an error, the setting on v1 intends "delayed" capture, hence authorization only, if set |
| 246 | if ( isset( $legacy_settings['capture'] ) ) { |
| 247 | $new_settings['transaction_type'] = 'yes' === $legacy_settings['capture'] ? Gateway::TRANSACTION_TYPE_AUTHORIZATION : Gateway::TRANSACTION_TYPE_CHARGE; |
| 248 | } |
| 249 | |
| 250 | // not quite the same, since tokenization is a new thing, but we could presume the intention to let customers save their payment details |
| 251 | if ( isset( $legacy_settings['create_customer'] ) ) { |
| 252 | $new_settings['tokenization'] = 'yes' === $legacy_settings['create_customer'] ? 'yes' : 'no'; |
| 253 | } |
| 254 | |
| 255 | if ( isset( $legacy_settings['logging'] ) ) { |
| 256 | $new_settings['debug_mode'] = 'yes' === $legacy_settings['logging'] ? 'log' : 'off'; |
| 257 | } |
| 258 | |
| 259 | // there was no card types setting in v1 |
| 260 | $new_settings['card_types'] = [ |
| 261 | 'VISA', |
| 262 | 'MC', |
| 263 | 'AMEX', |
| 264 | 'JCB', |
| 265 | // purposefully omit dinersclub & discover |
| 266 | ]; |
| 267 | |
| 268 | // save migrated settings |
| 269 | update_option( 'woocommerce_square_credit_card_settings', $new_settings ); |
| 270 | |
| 271 | $this->get_plugin()->log( 'Gateway settings migration complete.' ); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * Migrates order data from v1 to v2. |
| 277 | * |
| 278 | * @see Lifecycle::upgrade_to_2_0_0() |
| 279 | * |
| 280 | * @since 2.0.0 |
| 281 | */ |
| 282 | private function migrate_orders() { |
| 283 | global $wpdb; |
| 284 | |
| 285 | $this->get_plugin()->log( 'Migrating orders data...' ); |
| 286 | |
| 287 | // move charge captured flag in orders to SkyVerge framework meta key |
| 288 | $wpdb->update( $wpdb->postmeta, [ 'meta_key' => '_wc_square_credit_card_charge_captured' ], [ 'meta_key' => '_square_charge_captured' ] ); |
| 289 | |
| 290 | // move payment ID to new gateway ID meta key value |
| 291 | $wpdb->update( $wpdb->postmeta, [ 'meta_value' => 'square_credit_card' ], [ 'meta_key' => '_payment_method', 'meta_value' => 'square' ] ); |
| 292 | |
| 293 | $this->get_plugin()->log( 'Orders migration complete.' ); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
| 298 | * Migrates product data from v1 to v2. |
| 299 | * |
| 300 | * @see Lifecycle::upgrade_to_2_0_0() |
| 301 | * |
| 302 | * @since 2.0.0 |
| 303 | */ |
| 304 | private function migrate_products() { |
| 305 | global $wpdb; |
| 306 | |
| 307 | $this->get_plugin()->log( 'Migrating products data...' ); |
| 308 | |
| 309 | // the handling in v1 was reversed, so we want products where sync wasn't disabled |
| 310 | $legacy_product_ids = get_posts( [ |
| 311 | 'nopaging' => true, |
| 312 | 'post_type' => 'product', |
| 313 | 'post_status' => 'all', |
| 314 | 'fields' => 'ids', |
| 315 | 'meta_query' => [ |
| 316 | 'relation' => 'OR', |
| 317 | [ |
| 318 | 'key' => '_wcsquare_disable_sync', |
| 319 | 'value' => 'no', |
| 320 | ], |
| 321 | [ |
| 322 | 'key' => '_wcsquare_disable_sync', |
| 323 | 'compare' => 'NOT EXISTS', |
| 324 | ] |
| 325 | ], |
| 326 | ] ); |
| 327 | |
| 328 | // in v2 we turn those products as flagged to be sync-enabled instead |
| 329 | if ( ! empty( $legacy_product_ids ) ) { |
| 330 | |
| 331 | $failed_products = []; |
| 332 | |
| 333 | // ensure taxonomy is registered at this stage |
| 334 | if ( ! taxonomy_exists( Product::SYNCED_WITH_SQUARE_TAXONOMY ) ) { |
| 335 | Product::init_taxonomies(); |
| 336 | } |
| 337 | |
| 338 | // will not create the term if already exists |
| 339 | wp_create_term( 'yes', Product::SYNCED_WITH_SQUARE_TAXONOMY ); |
| 340 | |
| 341 | // set Square sync status via taxonomy term |
| 342 | foreach ( $legacy_product_ids as $i => $product_id ) { |
| 343 | |
| 344 | $set_term = wp_set_object_terms( $product_id, [ 'yes' ], Product::SYNCED_WITH_SQUARE_TAXONOMY ); |
| 345 | |
| 346 | if ( ! is_array( $set_term ) ) { |
| 347 | |
| 348 | unset( $legacy_product_ids[ $i ] ); |
| 349 | |
| 350 | $failed_products[] = $product_id; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // log any errors |
| 355 | if ( ! empty( $failed_products ) ) { |
| 356 | $this->get_plugin()->log( 'Could not update sync with Square status for products with ID: ' . implode( ', ', array_unique( $failed_products ) ) . '.' ); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | $this->get_plugin()->log( 'Products migration complete.' ); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /** |
| 365 | * Migrates customer data. |
| 366 | * |
| 367 | * @since 2.0.0 |
| 368 | */ |
| 369 | private function migrate_customers() { |
| 370 | global $wpdb; |
| 371 | |
| 372 | $this->get_plugin()->log( 'Migrating customer data.' ); |
| 373 | |
| 374 | $rows = (int) $wpdb->update( $wpdb->usermeta, [ 'meta_key' => 'wc_square_customer_id' ], [ 'meta_key' => '_square_customer_id' ] ); |
| 375 | |
| 376 | $this->get_plugin()->log( sprintf( '%d customers migrated', $rows ) ); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | } |
| 381 |