Onboarding.php
5 months ago
OnboardingHelper.php
1 year ago
OnboardingIndustries.php
2 years ago
OnboardingJetpack.php
3 years ago
OnboardingMailchimp.php
3 years ago
OnboardingProducts.php
3 years ago
OnboardingProfile.php
1 year ago
OnboardingSetupWizard.php
1 year ago
OnboardingSync.php
2 years ago
OnboardingSync.php
153 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Onboarding |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin\Onboarding; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile; |
| 9 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; |
| 10 | |
| 11 | /** |
| 12 | * Contains backend logic for the onboarding profile and checklist feature. |
| 13 | */ |
| 14 | class OnboardingSync { |
| 15 | /** |
| 16 | * Class instance. |
| 17 | * |
| 18 | * @var OnboardingSync instance |
| 19 | */ |
| 20 | private static $instance = null; |
| 21 | |
| 22 | /** |
| 23 | * Get class instance. |
| 24 | */ |
| 25 | final public static function instance() { |
| 26 | if ( ! static::$instance ) { |
| 27 | static::$instance = new static(); |
| 28 | } |
| 29 | return static::$instance; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Init. |
| 34 | */ |
| 35 | public function init() { |
| 36 | add_action( 'update_option_' . OnboardingProfile::DATA_OPTION, array( $this, 'send_profile_data_on_update' ), 10, 2 ); |
| 37 | add_action( 'woocommerce_helper_connected', array( $this, 'send_profile_data_on_connect' ) ); |
| 38 | |
| 39 | if ( ! is_admin() ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | add_action( 'current_screen', array( $this, 'redirect_wccom_install' ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Send profile data to WooCommerce.com. |
| 48 | */ |
| 49 | private function send_profile_data() { |
| 50 | if ( 'yes' !== get_option( 'woocommerce_allow_tracking', 'no' ) ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if ( ! class_exists( '\WC_Helper_API' ) || ! method_exists( '\WC_Helper_API', 'put' ) ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if ( ! class_exists( '\WC_Helper_Options' ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $auth = \WC_Helper_Options::get( 'auth' ); |
| 63 | if ( empty( $auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | $profile = get_option( OnboardingProfile::DATA_OPTION, array() ); |
| 68 | $base_location = wc_get_base_location(); |
| 69 | $defaults = array( |
| 70 | 'plugins' => 'skipped', |
| 71 | 'industry' => array(), |
| 72 | 'product_types' => array(), |
| 73 | 'product_count' => '0', |
| 74 | 'selling_venues' => 'no', |
| 75 | 'number_employees' => '1', |
| 76 | 'revenue' => 'none', |
| 77 | 'other_platform' => 'none', |
| 78 | 'business_extensions' => array(), |
| 79 | 'theme' => get_stylesheet(), |
| 80 | 'setup_client' => false, |
| 81 | 'store_location' => $base_location['country'], |
| 82 | 'default_currency' => get_woocommerce_currency(), |
| 83 | ); |
| 84 | |
| 85 | // Prepare industries as an array of slugs if they are in array format. |
| 86 | if ( isset( $profile['industry'] ) && is_array( $profile['industry'] ) ) { |
| 87 | $industry_slugs = array(); |
| 88 | foreach ( $profile['industry'] as $industry ) { |
| 89 | $industry_slugs[] = is_array( $industry ) ? $industry['slug'] : $industry; |
| 90 | } |
| 91 | $profile['industry'] = $industry_slugs; |
| 92 | } |
| 93 | $body = wp_parse_args( $profile, $defaults ); |
| 94 | |
| 95 | \WC_Helper_API::put( |
| 96 | 'profile', |
| 97 | array( |
| 98 | 'authenticated' => true, |
| 99 | 'body' => wp_json_encode( $body ), |
| 100 | 'headers' => array( |
| 101 | 'Content-Type' => 'application/json', |
| 102 | ), |
| 103 | ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Send profiler data on profiler change to completion. |
| 109 | * |
| 110 | * @param array $old_value Previous value. |
| 111 | * @param array $value Current value. |
| 112 | */ |
| 113 | public function send_profile_data_on_update( $old_value, $value ) { |
| 114 | if ( ! isset( $value['completed'] ) || ! $value['completed'] ) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | $this->send_profile_data(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Send profiler data after a site is connected. |
| 123 | */ |
| 124 | public function send_profile_data_on_connect() { |
| 125 | $profile = get_option( OnboardingProfile::DATA_OPTION, array() ); |
| 126 | if ( ! isset( $profile['completed'] ) || ! $profile['completed'] ) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $this->send_profile_data(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Redirects the user to the task list if the task list is enabled and finishing a wccom checkout. |
| 135 | * |
| 136 | * @todo Once URL params are added to the redirect, we can check those instead of the referer. |
| 137 | */ |
| 138 | public function redirect_wccom_install() { |
| 139 | $task_list = TaskLists::get_list( 'setup' ); |
| 140 | |
| 141 | if ( |
| 142 | ! $task_list || |
| 143 | $task_list->is_hidden() || |
| 144 | ! isset( $_SERVER['HTTP_REFERER'] ) || |
| 145 | 0 !== strpos( wp_unslash( $_SERVER['HTTP_REFERER'] ), 'https://woocommerce.com/checkout?utm_medium=product' ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 146 | ) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | wp_safe_redirect( wc_admin_url() ); |
| 151 | } |
| 152 | } |
| 153 |