API
4 months ago
Admin
4 months ago
Emails
1 year ago
Framework
3 months ago
Gateway
3 months ago
Handlers
3 months ago
Sync
6 months ago
Utilities
3 months ago
AJAX.php
2 years ago
API.php
4 months ago
Admin.php
10 months ago
Coupons.php
4 months ago
Functions.php
3 years ago
Gateway.php
3 months ago
Lifecycle.php
2 years ago
Plugin.php
4 months ago
Settings.php
4 months ago
WC_Order_Square.php
2 years ago
WC_Payments_Compatibility.php
1 year ago
Lifecycle.php
687 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 GNU General Public License v3.0 or later |
| 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 or later |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use WooCommerce\Square\Handlers\Product; |
| 29 | |
| 30 | /** |
| 31 | * The plugin lifecycle handler. |
| 32 | * |
| 33 | * @since 2.0.0 |
| 34 | * |
| 35 | * @method Plugin get_plugin() |
| 36 | */ |
| 37 | class Lifecycle extends \WooCommerce\Square\Framework\Lifecycle { |
| 38 | |
| 39 | /** |
| 40 | * Lifecycle constructor. |
| 41 | * |
| 42 | * @since 2.0.0 |
| 43 | * |
| 44 | * @param Plugin $plugin main instance. |
| 45 | */ |
| 46 | public function __construct( Plugin $plugin ) { |
| 47 | |
| 48 | parent::__construct( $plugin ); |
| 49 | |
| 50 | // plugin upgrade path: maps automatically each semver to upgrade_to_x_y_z() protected method. |
| 51 | $this->upgrade_versions = array( |
| 52 | '2.0.0', |
| 53 | '2.0.4', |
| 54 | '2.1.5', |
| 55 | '2.2.0', |
| 56 | '2.3.0', |
| 57 | '3.0.2', |
| 58 | '3.2.0', |
| 59 | '3.7.1', |
| 60 | '3.8.3', |
| 61 | '4.7.0', |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Performs plugin installation. |
| 68 | * |
| 69 | * @since 2.0.0 |
| 70 | */ |
| 71 | protected function install() { |
| 72 | |
| 73 | // create the db table for the customer index. |
| 74 | Gateway\Customer_Helper::create_table(); |
| 75 | |
| 76 | /** |
| 77 | * Fires upon plugin installed. |
| 78 | * |
| 79 | * @since 2.0.0 |
| 80 | * |
| 81 | * @param string $version plugin version (available from v2.0.0) |
| 82 | */ |
| 83 | do_action( 'wc_square_installed', Plugin::VERSION ); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Performs upgrade tasks. |
| 89 | * |
| 90 | * @since 2.0.0 |
| 91 | * |
| 92 | * @param string $installed_version semver. |
| 93 | */ |
| 94 | protected function upgrade( $installed_version ) { |
| 95 | |
| 96 | parent::upgrade( $installed_version ); |
| 97 | |
| 98 | /** |
| 99 | * Fires upon plugin upgraded (legacy hook). |
| 100 | * |
| 101 | * @since 1.0.0 |
| 102 | * |
| 103 | * @param string $version version updating to (available from v2.0.0) |
| 104 | * @param string $version version updating from (available from v2.0.0) |
| 105 | */ |
| 106 | do_action( 'wc_square_updated', Plugin::VERSION, $installed_version ); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Upgrades to version 2.0.0 |
| 112 | * |
| 113 | * @since 2.0.0 |
| 114 | */ |
| 115 | protected function upgrade_to_2_0_0() { |
| 116 | |
| 117 | // create the db table for the customer index. |
| 118 | Gateway\Customer_Helper::create_table(); |
| 119 | |
| 120 | wc_set_time_limit( 300 ); |
| 121 | |
| 122 | // migrate all the things! |
| 123 | $syncing_products = $this->migrate_plugin_settings(); |
| 124 | |
| 125 | $this->migrate_gateway_settings(); |
| 126 | $this->migrate_orders(); |
| 127 | |
| 128 | // only set the products "sync" status if v2 is now configured to sync products. |
| 129 | if ( $syncing_products ) { |
| 130 | |
| 131 | $this->migrate_products(); |
| 132 | |
| 133 | // assume a last sync occurred before upgrading. |
| 134 | $this->get_plugin()->get_sync_handler()->set_last_synced_at(); |
| 135 | $this->get_plugin()->get_sync_handler()->set_inventory_last_synced_at(); |
| 136 | } |
| 137 | |
| 138 | $this->migrate_customers(); |
| 139 | |
| 140 | // mark upgrade complete. |
| 141 | update_option( 'wc_square_updated_to_2_0_0', true ); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Upgrades to version 2.0.4. |
| 147 | * |
| 148 | * @since 2.0.4 |
| 149 | */ |
| 150 | protected function upgrade_to_2_0_4() { |
| 151 | |
| 152 | $v1_settings = get_option( 'woocommerce_squareconnect_settings', array() ); |
| 153 | $v2_settings = get_option( 'wc_square_settings', array() ); |
| 154 | |
| 155 | $v2_settings = $this->get_migrated_system_of_record( $v1_settings, $v2_settings ); |
| 156 | |
| 157 | update_option( 'wc_square_settings', $v2_settings ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Upgrades to version 2.1.5 |
| 162 | * |
| 163 | * 2.1.5 updated the woocommerce_square_customers database schema. |
| 164 | * |
| 165 | * @see https://github.com/woocommerce/woocommerce-square/issues/359 |
| 166 | * @since 2.1.5 |
| 167 | */ |
| 168 | protected function upgrade_to_2_1_5() { |
| 169 | Gateway\Customer_Helper::create_table(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Adds the missing Square customer table. |
| 174 | * |
| 175 | * @see https://github.com/woocommerce/woocommerce-square/issues/825 |
| 176 | * @since 3.2.0 |
| 177 | */ |
| 178 | protected function upgrade_to_3_2_0() { |
| 179 | Gateway\Customer_Helper::create_table(); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Deletes all transient data related to payment tokens. |
| 184 | * |
| 185 | * @see https://github.com/woocommerce/woocommerce-square/issues/1050 |
| 186 | * @since 3.8.3 |
| 187 | */ |
| 188 | protected function upgrade_to_3_8_3() { |
| 189 | wc_square()->get_gateway()->get_payment_tokens_handler()->clear_all_transients(); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Upgrades to version 3.7.1 |
| 194 | * |
| 195 | * This upgrade disables gift cards, and shows a notice to inform the merchant. |
| 196 | * Gift Cards are in beta status and not recommended for production. |
| 197 | * |
| 198 | * @see https://github.com/woocommerce/woocommerce-square/issues/1003 |
| 199 | * @see https://github.com/woocommerce/woocommerce-square/issues/1009 |
| 200 | * @since 3.7.1. |
| 201 | */ |
| 202 | protected function upgrade_to_3_7_1() { |
| 203 | // Early return if Gift Cards are already disabled. |
| 204 | $gateway_settings = get_option( 'woocommerce_square_credit_card_settings', array() ); |
| 205 | if ( ! isset( $gateway_settings['enable_gift_cards'] ) || 'no' === $gateway_settings['enable_gift_cards'] ) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Force-disable Gift Cards (only if store has it enabled). |
| 210 | $gateway_settings['enable_gift_cards'] = 'no'; |
| 211 | update_option( 'woocommerce_square_credit_card_settings', $gateway_settings ); |
| 212 | |
| 213 | // Store an option to inform the merchant that Gift Cards has been force-disabled. |
| 214 | // Using version number in the option name so it's easy to remove in the future. |
| 215 | update_option( 'woocommerce_square_3_7_1_gift_cards_force_disable_notice', 'yes' ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Generates a milestone notice message. |
| 220 | * |
| 221 | * @since 2.1.7 |
| 222 | * |
| 223 | * @param string $custom_message Custom text that notes what milestone was completed. |
| 224 | * @return string |
| 225 | */ |
| 226 | protected function generate_milestone_notice_message( $custom_message ) { |
| 227 | |
| 228 | $message = ''; |
| 229 | |
| 230 | if ( $this->get_plugin()->get_reviews_url() ) { |
| 231 | |
| 232 | // to be prepended at random to each milestone notice. |
| 233 | $exclamations = array( |
| 234 | __( 'Awesome', 'woocommerce-square' ), |
| 235 | __( 'Congratulations', 'woocommerce-square' ), |
| 236 | __( 'Great', 'woocommerce-square' ), |
| 237 | __( 'Fantastic', 'woocommerce-square' ), |
| 238 | ); |
| 239 | |
| 240 | $message = $exclamations[ array_rand( $exclamations ) ] . ', ' . esc_html( $custom_message ) . ' '; |
| 241 | |
| 242 | $message .= sprintf( |
| 243 | /* translators: Placeholders: %1$s - plugin name, %2$s - <a> tag, %3$s - </a> tag, %4$s - <a> tag, %5$s - </a> tag */ |
| 244 | __( 'Are you having a great experience with %1$s so far? Please consider %2$sleaving a review%3$s! If things aren\'t going quite as expected, we\'re happy to help -- please %4$sreach out to our support team%5$s.', 'woocommerce-square' ), |
| 245 | '<strong>' . esc_html( $this->get_plugin()->get_plugin_name() ) . '</strong>', |
| 246 | '<a href="' . esc_url( $this->get_plugin()->get_reviews_url() ) . '">', |
| 247 | '</a>', |
| 248 | '<a href="' . esc_url( $this->get_plugin()->get_support_url() ) . '">', |
| 249 | '</a>' |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | return $message; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Upgrades to version 2.2.0. |
| 258 | * |
| 259 | * @since 2.2.0 |
| 260 | */ |
| 261 | protected function upgrade_to_2_2_0() { |
| 262 | |
| 263 | $v1_settings = get_option( 'wc_square_settings', array() ); |
| 264 | |
| 265 | $v2_settings = $this->set_environment_location_id( $v1_settings ); |
| 266 | |
| 267 | update_option( 'wc_square_settings', $v2_settings ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Upgrades to version 2.3.0. |
| 272 | * |
| 273 | * @since 2.3.0 |
| 274 | */ |
| 275 | protected function upgrade_to_2_3_0() { |
| 276 | // Set `enable_digital_wallets` default to no for existing stores |
| 277 | $gateway_settings = get_option( 'woocommerce_square_credit_card_settings', array() ); |
| 278 | |
| 279 | if ( ! isset( $gateway_settings['enable_digital_wallets'] ) ) { |
| 280 | $gateway_settings['enable_digital_wallets'] = 'no'; |
| 281 | } |
| 282 | |
| 283 | update_option( 'woocommerce_square_credit_card_settings', $gateway_settings ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Deletes the transient that holds locations data. |
| 288 | * |
| 289 | * @see https://github.com/woocommerce/woocommerce-square/issues/786#issuecomment-1121388650 |
| 290 | * |
| 291 | * @since 3.0.2 |
| 292 | */ |
| 293 | protected function upgrade_to_3_0_2() { |
| 294 | delete_transient( 'wc_square_locations' ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Upgrades to version 4.7.0. |
| 299 | * |
| 300 | * @since 4.7.0 |
| 301 | */ |
| 302 | protected function upgrade_to_4_7_0() { |
| 303 | |
| 304 | // Skip if already upgraded. |
| 305 | if ( get_option( 'wc_square_updated_to_4_7_0' ) ) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | // Migrate Gift Cards settings. |
| 310 | $this->migrate_gateway_settings_dynamically( |
| 311 | 'woocommerce_square_credit_card_settings', |
| 312 | 'woocommerce_gift_cards_pay_settings', |
| 313 | array( |
| 314 | 'enable_gift_cards' => 'enabled', |
| 315 | ) |
| 316 | ); |
| 317 | |
| 318 | // Migrate Gateway fields to the Square settings. |
| 319 | $this->migrate_gateway_settings_dynamically( |
| 320 | 'woocommerce_square_credit_card_settings', |
| 321 | 'wc_square_settings', |
| 322 | array( |
| 323 | 'debug_mode' => 'debug_mode', |
| 324 | 'enable_customer_decline_messages' => 'enable_customer_decline_messages', |
| 325 | ), |
| 326 | false |
| 327 | ); |
| 328 | |
| 329 | // Mark upgrade complete. |
| 330 | update_option( 'wc_square_updated_to_4_7_0', true ); |
| 331 | |
| 332 | // Skip redirect existing users to the setup wizard on upgrade. |
| 333 | add_option( 'wc_square_show_wizard_on_activation', true, '', 'no' ); |
| 334 | |
| 335 | // Mark the onboarding wizard as visited for existing users. |
| 336 | add_option( 'wc_square_connected_page_visited', true, '', 'no' ); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Migrates plugin settings from v1 to v2. |
| 341 | * |
| 342 | * @see Lifecycle::upgrade_to_2_0_0() |
| 343 | * |
| 344 | * @since 2.0.0 |
| 345 | * |
| 346 | * @return bool whether a Sync setting was enabled from migration |
| 347 | */ |
| 348 | private function migrate_plugin_settings() { |
| 349 | |
| 350 | $this->get_plugin()->log( 'Migrating plugin settings...' ); |
| 351 | |
| 352 | // get legacy and new default settings. |
| 353 | $new_settings = get_option( 'wc_square_settings', array() ); |
| 354 | $legacy_settings = get_option( 'woocommerce_squareconnect_settings', array() ); |
| 355 | $email_settings = get_option( 'woocommerce_wc_square_sync_completed_settings', array() ); |
| 356 | |
| 357 | // bail if they already have v2 settings present. |
| 358 | if ( ! empty( $new_settings ) ) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | // handle access token first. |
| 363 | $legacy_access_token = get_option( 'woocommerce_square_merchant_access_token' ); |
| 364 | if ( $legacy_access_token ) { |
| 365 | |
| 366 | // the access token was previously stored unencrypted. |
| 367 | if ( ! empty( $legacy_access_token ) && Utilities\Encryption_Utility::is_encryption_supported() ) { |
| 368 | |
| 369 | $encryption = new Utilities\Encryption_Utility(); |
| 370 | |
| 371 | try { |
| 372 | $legacy_access_token = $encryption->encrypt_data( $legacy_access_token ); |
| 373 | } catch ( \Exception $exception ) { |
| 374 | // log the event, but don't halt the process. |
| 375 | $this->get_plugin()->log( 'Could not encrypt access token during upgrade. ' . $exception->getMessage() ); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // previously only 'production' environment was assumed. |
| 380 | $access_tokens = get_option( 'wc_square_access_tokens', array() ); |
| 381 | $access_tokens['production'] = is_string( $legacy_access_token ) ? $legacy_access_token : ''; |
| 382 | |
| 383 | update_option( 'wc_square_access_tokens', $access_tokens ); |
| 384 | } |
| 385 | |
| 386 | // migrate store location. |
| 387 | if ( ! empty( $legacy_settings['location'] ) ) { |
| 388 | $new_settings['location_id'] = $legacy_settings['location']; |
| 389 | } |
| 390 | |
| 391 | // toggle debug logging. |
| 392 | $new_settings['debug_logging_enabled'] = isset( $legacy_settings['logging'] ) && in_array( $legacy_settings['logging'], array( 'yes', 'no' ), true ) ? $legacy_settings['logging'] : 'no'; |
| 393 | |
| 394 | // set the SOR and inventory sync values. |
| 395 | $new_settings = $this->get_migrated_system_of_record( $legacy_settings, $new_settings ); |
| 396 | |
| 397 | // migrate email notification settings: if there's a recipient, we enable email and pass recipient(s) to email setting. |
| 398 | if ( isset( $legacy_settings['sync_email'] ) && is_string( $legacy_settings['sync_email'] ) && '' !== trim( $legacy_settings['sync_email'] ) ) { |
| 399 | $email_settings['enabled'] = 'yes'; |
| 400 | $email_settings['recipient'] = $legacy_settings['sync_email']; |
| 401 | } else { |
| 402 | $email_settings['enabled'] = 'no'; |
| 403 | $email_settings['recipient'] = ''; |
| 404 | } |
| 405 | |
| 406 | // save email settings. |
| 407 | update_option( 'woocommerce_wc_square_sync_completed_settings', $email_settings ); |
| 408 | // save plugin settings. |
| 409 | update_option( 'wc_square_settings', $new_settings ); |
| 410 | |
| 411 | $this->get_plugin()->log( 'Plugin settings migration complete.' ); |
| 412 | |
| 413 | return isset( $new_settings['system_of_record'] ) && Settings::SYSTEM_OF_RECORD_DISABLED !== $new_settings['system_of_record']; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | /** |
| 418 | * Migrates gateway settings from v1 to v2. |
| 419 | * |
| 420 | * @see Lifecycle::upgrade_to_2_0_0() |
| 421 | * |
| 422 | * @since 2.0.0 |
| 423 | */ |
| 424 | private function migrate_gateway_settings() { |
| 425 | |
| 426 | $this->get_plugin()->log( 'Migrating gateway settings...' ); |
| 427 | |
| 428 | $legacy_settings = get_option( 'woocommerce_square_settings', array() ); |
| 429 | $new_settings = get_option( 'woocommerce_square_credit_card_settings', array() ); |
| 430 | |
| 431 | // bail if they already have v2 settings present. |
| 432 | if ( ! empty( $new_settings ) ) { |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | if ( isset( $legacy_settings['enabled'] ) ) { |
| 437 | $new_settings['enabled'] = 'yes' === $legacy_settings['enabled'] ? 'yes' : 'no'; |
| 438 | } |
| 439 | |
| 440 | if ( isset( $legacy_settings['title'] ) && is_string( $legacy_settings['title'] ) ) { |
| 441 | $new_settings['title'] = $legacy_settings['title']; |
| 442 | } |
| 443 | |
| 444 | if ( isset( $legacy_settings['description'] ) && is_string( $legacy_settings['description'] ) ) { |
| 445 | $new_settings['description'] = $legacy_settings['description']; |
| 446 | } |
| 447 | |
| 448 | // note: the following is not an error, the setting on v1 intends "delayed" capture, hence authorization only, if set. |
| 449 | if ( isset( $legacy_settings['capture'] ) ) { |
| 450 | $new_settings['transaction_type'] = 'yes' === $legacy_settings['capture'] ? Gateway::TRANSACTION_TYPE_AUTHORIZATION : Gateway::TRANSACTION_TYPE_CHARGE; |
| 451 | } |
| 452 | |
| 453 | // not quite the same, since tokenization is a new thing, but we could presume the intention to let customers save their payment details. |
| 454 | if ( isset( $legacy_settings['create_customer'] ) ) { |
| 455 | $new_settings['tokenization'] = 'yes' === $legacy_settings['create_customer'] ? 'yes' : 'no'; |
| 456 | } |
| 457 | |
| 458 | if ( isset( $legacy_settings['logging'] ) ) { |
| 459 | $new_settings['debug_mode'] = 'yes' === $legacy_settings['logging'] ? 'log' : 'off'; |
| 460 | } |
| 461 | |
| 462 | // there was no card types setting in v1. |
| 463 | $new_settings['card_types'] = array( |
| 464 | 'VISA', |
| 465 | 'MC', |
| 466 | 'AMEX', |
| 467 | 'JCB', |
| 468 | // purposefully omit dinersclub & discover. |
| 469 | ); |
| 470 | |
| 471 | // save migrated settings. |
| 472 | update_option( 'woocommerce_square_credit_card_settings', $new_settings ); |
| 473 | |
| 474 | $this->get_plugin()->log( 'Gateway settings migration complete.' ); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Migrates gateway settings dynamically. |
| 479 | * |
| 480 | * @param string $legacy_option legacy option name. |
| 481 | * @param string $new_option new option name. |
| 482 | * @param array $fields fields to migrate. |
| 483 | * |
| 484 | * @since 4.7.0 |
| 485 | */ |
| 486 | private function migrate_gateway_settings_dynamically( $legacy_option, $new_option, $fields = array(), $bail = true ) { |
| 487 | $this->get_plugin()->log( |
| 488 | sprintf( |
| 489 | // translators: 1: legacy option name, 2: new option name. |
| 490 | __( |
| 491 | 'Migrating gateway settings from %1$s to %2$s...', |
| 492 | 'woocommerce-square' |
| 493 | ), |
| 494 | $legacy_option, |
| 495 | $new_option |
| 496 | ) |
| 497 | ); |
| 498 | |
| 499 | $legacy_settings = get_option( $legacy_option, array() ); |
| 500 | $new_settings = get_option( $new_option, array() ); |
| 501 | |
| 502 | // Bail if they already have new settings present. |
| 503 | if ( ! empty( $new_settings ) && $bail ) { |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | // Migrate the fields. |
| 508 | foreach ( $fields as $legacy_field => $new_field ) { |
| 509 | if ( isset( $legacy_settings[ $legacy_field ] ) ) { |
| 510 | $new_settings[ $new_field ] = $legacy_settings[ $legacy_field ]; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // Save migrated settings. |
| 515 | update_option( $new_option, $new_settings ); |
| 516 | |
| 517 | $this->get_plugin()->log( |
| 518 | sprintf( |
| 519 | // translators: 1: legacy option name, 2: new option name. |
| 520 | __( |
| 521 | 'Gateway settings migration from %1$s to %2$s completed.', |
| 522 | 'woocommerce-square' |
| 523 | ), |
| 524 | $legacy_option, |
| 525 | $new_option |
| 526 | ) |
| 527 | ); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /** |
| 532 | * Migrates order data from v1 to v2. |
| 533 | * |
| 534 | * @see Lifecycle::upgrade_to_2_0_0() |
| 535 | * |
| 536 | * @since 2.0.0 |
| 537 | */ |
| 538 | private function migrate_orders() { |
| 539 | global $wpdb; |
| 540 | |
| 541 | $this->get_plugin()->log( 'Migrating orders data...' ); |
| 542 | |
| 543 | $wpdb->update( $wpdb->postmeta, array( 'meta_key' => '_wc_square_credit_card_charge_captured' ), array( 'meta_key' => '_square_charge_captured' ) ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 544 | |
| 545 | // move payment ID to new gateway ID meta key value. |
| 546 | $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 547 | $wpdb->postmeta, |
| 548 | array( |
| 549 | 'meta_value' => 'square_credit_card', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 550 | ), |
| 551 | array( |
| 552 | 'meta_key' => '_payment_method', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 553 | 'meta_value' => 'square', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 554 | ) |
| 555 | ); |
| 556 | $this->get_plugin()->log( 'Orders migration complete.' ); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | /** |
| 561 | * Migrates product data from v1 to v2. |
| 562 | * |
| 563 | * @see Lifecycle::upgrade_to_2_0_0() |
| 564 | * |
| 565 | * @since 2.0.0 |
| 566 | */ |
| 567 | private function migrate_products() { |
| 568 | global $wpdb; |
| 569 | |
| 570 | $this->get_plugin()->log( 'Migrating products data...' ); |
| 571 | |
| 572 | // the handling in v1 was reversed, so we want products where sync wasn't disabled. |
| 573 | $legacy_product_ids = get_posts( |
| 574 | array( |
| 575 | 'nopaging' => true, |
| 576 | 'post_type' => 'product', |
| 577 | 'post_status' => 'all', |
| 578 | 'fields' => 'ids', |
| 579 | 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 580 | |
| 581 | 'relation' => 'OR', |
| 582 | array( |
| 583 | 'key' => '_wcsquare_disable_sync', |
| 584 | 'value' => 'no', |
| 585 | ), |
| 586 | array( |
| 587 | 'key' => '_wcsquare_disable_sync', |
| 588 | 'compare' => 'NOT EXISTS', |
| 589 | ), |
| 590 | ), |
| 591 | ) |
| 592 | ); |
| 593 | |
| 594 | // in v2 we turn those products as flagged to be sync-enabled instead. |
| 595 | if ( ! empty( $legacy_product_ids ) ) { |
| 596 | |
| 597 | $failed_products = array(); |
| 598 | |
| 599 | // ensure taxonomy is registered at this stage. |
| 600 | if ( ! taxonomy_exists( Product::SYNCED_WITH_SQUARE_TAXONOMY ) ) { |
| 601 | Product::init_taxonomies(); |
| 602 | } |
| 603 | |
| 604 | // will not create the term if already exists. |
| 605 | wp_create_term( 'yes', Product::SYNCED_WITH_SQUARE_TAXONOMY ); |
| 606 | |
| 607 | // set Square sync status via taxonomy term. |
| 608 | foreach ( $legacy_product_ids as $i => $product_id ) { |
| 609 | |
| 610 | $set_term = wp_set_object_terms( $product_id, array( 'yes' ), Product::SYNCED_WITH_SQUARE_TAXONOMY ); |
| 611 | |
| 612 | if ( ! is_array( $set_term ) ) { |
| 613 | |
| 614 | unset( $legacy_product_ids[ $i ] ); |
| 615 | |
| 616 | $failed_products[] = $product_id; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // log any errors. |
| 621 | if ( ! empty( $failed_products ) ) { |
| 622 | $this->get_plugin()->log( 'Could not update sync with Square status for products with ID: ' . implode( ', ', array_unique( $failed_products ) ) . '.' ); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | $this->get_plugin()->log( 'Products migration complete.' ); |
| 627 | } |
| 628 | |
| 629 | |
| 630 | /** |
| 631 | * Migrates customer data. |
| 632 | * |
| 633 | * @since 2.0.0 |
| 634 | */ |
| 635 | private function migrate_customers() { |
| 636 | global $wpdb; |
| 637 | |
| 638 | $this->get_plugin()->log( 'Migrating customer data.' ); |
| 639 | |
| 640 | $rows = (int) $wpdb->update( $wpdb->usermeta, array( 'meta_key' => 'wc_square_customer_id' ), array( 'meta_key' => '_square_customer_id' ) ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 641 | |
| 642 | $this->get_plugin()->log( sprintf( '%d customers migrated', $rows ) ); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | /** |
| 647 | * Adds the Sync setting setting to the v2 plugin settings depending on v1 setting values. |
| 648 | * |
| 649 | * @since 2.0.2 |
| 650 | * |
| 651 | * @param array $v1_settings v1 plugin settings. |
| 652 | * @param array $v2_settings v2 plugin settings. |
| 653 | * @return array |
| 654 | */ |
| 655 | private function get_migrated_system_of_record( $v1_settings, $v2_settings ) { |
| 656 | |
| 657 | $sync_products = isset( $v1_settings['sync_products'] ) && 'yes' === $v1_settings['sync_products']; |
| 658 | $sync_inventory = $sync_products && isset( $v1_settings['sync_inventory'] ) && 'yes' === $v1_settings['sync_inventory']; |
| 659 | $inventory_polling = isset( $v1_settings['inventory_polling'] ) && 'yes' === $v1_settings['inventory_polling']; |
| 660 | |
| 661 | $v2_settings['system_of_record'] = $sync_products && $inventory_polling ? Settings::SYSTEM_OF_RECORD_SQUARE : Settings::SYSTEM_OF_RECORD_DISABLED; |
| 662 | $v2_settings['enable_inventory_sync'] = $inventory_polling || $sync_inventory ? 'yes' : 'no'; |
| 663 | |
| 664 | return $v2_settings; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Adds environment specific location_id to, and removes general location_id from v1 setting array. |
| 669 | * |
| 670 | * @since 2.2.0 |
| 671 | * |
| 672 | * @param array $v1_settings v1 plugin settings. |
| 673 | * @return array |
| 674 | */ |
| 675 | private function set_environment_location_id( $v1_settings ) { |
| 676 | |
| 677 | $environment = isset( $v1_settings['enable_sandbox'] ) && 'yes' === $v1_settings['enable_sandbox'] ? 'sandbox' : 'production'; |
| 678 | |
| 679 | if ( ! isset( $v1_settings[ $environment . '_location_id' ] ) ) { |
| 680 | $v1_location_id = isset( $v1_settings['location_id'] ) ? $v1_settings['location_id'] : ''; |
| 681 | $v1_settings[ $environment . '_location_id' ] = $v1_location_id; |
| 682 | } |
| 683 | |
| 684 | return $v1_settings; |
| 685 | } |
| 686 | } |
| 687 |