Navigation
5 years ago
ActivityPanels.php
5 years ago
Analytics.php
5 years ago
AnalyticsDashboard.php
5 years ago
Coupons.php
5 years ago
CouponsMovedTrait.php
5 years ago
CustomerEffortScoreTracks.php
5 years ago
Features.php
5 years ago
Homescreen.php
5 years ago
Marketing.php
5 years ago
MobileAppBanner.php
5 years ago
Onboarding.php
5 years ago
OnboardingTasks.php
5 years ago
Settings.php
5 years ago
ShippingLabelBanner.php
5 years ago
ShippingLabelBannerDisplayRules.php
5 years ago
OnboardingTasks.php
471 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Onboarding Tasks |
| 4 | * NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-admin. |
| 5 | */ |
| 6 | |
| 7 | namespace Automattic\WooCommerce\Admin\Features; |
| 8 | |
| 9 | use \Automattic\WooCommerce\Admin\Loader; |
| 10 | use Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore; |
| 11 | |
| 12 | /** |
| 13 | * Contains the logic for completing onboarding tasks. |
| 14 | */ |
| 15 | class OnboardingTasks { |
| 16 | /** |
| 17 | * Class instance. |
| 18 | * |
| 19 | * @var OnboardingTasks instance |
| 20 | */ |
| 21 | protected static $instance = null; |
| 22 | |
| 23 | /** |
| 24 | * Name of the active task transient. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | const ACTIVE_TASK_TRANSIENT = 'wc_onboarding_active_task'; |
| 29 | |
| 30 | /** |
| 31 | * Get class instance. |
| 32 | */ |
| 33 | public static function get_instance() { |
| 34 | if ( ! self::$instance ) { |
| 35 | self::$instance = new self(); |
| 36 | } |
| 37 | return self::$instance; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | // This hook needs to run when options are updated via REST. |
| 45 | add_action( 'add_option_woocommerce_task_list_complete', array( $this, 'track_completion' ), 10, 2 ); |
| 46 | add_action( 'add_option_woocommerce_extended_task_list_complete', array( $this, 'track_extended_completion' ), 10, 2 ); |
| 47 | add_action( 'add_option_woocommerce_task_list_tracked_completed_tasks', array( $this, 'track_task_completion' ), 10, 2 ); |
| 48 | add_action( 'update_option_woocommerce_task_list_tracked_completed_tasks', array( $this, 'track_task_completion' ), 10, 2 ); |
| 49 | add_action( 'admin_enqueue_scripts', array( $this, 'update_option_extended_task_list' ), 15 ); |
| 50 | |
| 51 | if ( ! is_admin() ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | add_action( 'admin_enqueue_scripts', array( $this, 'add_media_scripts' ) ); |
| 56 | // Old settings injection. |
| 57 | // Run after Onboarding. |
| 58 | add_filter( 'woocommerce_components_settings', array( __CLASS__, 'component_settings' ), 30 ); |
| 59 | // New settings injection. |
| 60 | add_filter( 'woocommerce_shared_settings', array( $this, 'component_settings' ), 30 ); |
| 61 | |
| 62 | add_action( 'admin_init', array( $this, 'set_active_task' ), 5 ); |
| 63 | add_action( 'admin_enqueue_scripts', array( $this, 'add_onboarding_product_notice_admin_script' ) ); |
| 64 | add_action( 'admin_enqueue_scripts', array( $this, 'add_onboarding_homepage_notice_admin_script' ) ); |
| 65 | add_action( 'admin_enqueue_scripts', array( $this, 'add_onboarding_tax_notice_admin_script' ) ); |
| 66 | add_action( 'admin_enqueue_scripts', array( $this, 'add_onboarding_product_import_notice_admin_script' ) ); |
| 67 | add_filter( 'woocommerce_paypal_payments_onboarding_redirect_url', array( $this, 'ppcp_ob_after_onboarding_redirect_url' ) ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Enqueue scripts and styles. |
| 72 | */ |
| 73 | public function add_media_scripts() { |
| 74 | wp_enqueue_media(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Get task item data for settings filter. |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | public static function get_settings() { |
| 85 | $settings = array(); |
| 86 | $wc_pay_is_connected = false; |
| 87 | if ( class_exists( '\WC_Payments' ) ) { |
| 88 | $wc_payments_gateway = \WC_Payments::get_gateway(); |
| 89 | $wc_pay_is_connected = method_exists( $wc_payments_gateway, 'is_connected' ) |
| 90 | ? $wc_payments_gateway->is_connected() |
| 91 | : false; |
| 92 | } |
| 93 | |
| 94 | $gateways = WC()->payment_gateways->get_available_payment_gateways(); |
| 95 | $enabled_gateways = array_filter( |
| 96 | $gateways, |
| 97 | function( $gateway ) { |
| 98 | return 'yes' === $gateway->enabled; |
| 99 | } |
| 100 | ); |
| 101 | |
| 102 | // @todo We may want to consider caching some of these and use to check against |
| 103 | // task completion along with cache busting for active tasks. |
| 104 | $settings['automatedTaxSupportedCountries'] = self::get_automated_tax_supported_countries(); |
| 105 | $settings['hasHomepage'] = self::check_task_completion( 'homepage' ) || 'classic' === get_option( 'classic-editor-replace' ); |
| 106 | $settings['hasPaymentGateway'] = ! empty( $enabled_gateways ); |
| 107 | $settings['enabledPaymentGateways'] = array_keys( $enabled_gateways ); |
| 108 | $settings['hasPhysicalProducts'] = count( |
| 109 | wc_get_products( |
| 110 | array( |
| 111 | 'virtual' => false, |
| 112 | 'limit' => 1, |
| 113 | ) |
| 114 | ) |
| 115 | ) > 0; |
| 116 | $settings['hasProducts'] = self::check_task_completion( 'products' ); |
| 117 | $settings['isAppearanceComplete'] = get_option( 'woocommerce_task_list_appearance_complete' ); |
| 118 | $settings['isTaxComplete'] = self::check_task_completion( 'tax' ); |
| 119 | $settings['shippingZonesCount'] = count( \WC_Shipping_Zones::get_zones() ); |
| 120 | $settings['stripeSupportedCountries'] = self::get_stripe_supported_countries(); |
| 121 | $settings['stylesheet'] = get_option( 'stylesheet' ); |
| 122 | $settings['taxJarActivated'] = class_exists( 'WC_Taxjar' ); |
| 123 | $settings['themeMods'] = get_theme_mods(); |
| 124 | $settings['wcPayIsConnected'] = $wc_pay_is_connected; |
| 125 | |
| 126 | return $settings; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Add task items to component settings. |
| 131 | * |
| 132 | * @param array $settings Component settings. |
| 133 | * @return array |
| 134 | */ |
| 135 | public function component_settings( $settings ) { |
| 136 | // Bail early if not on a wc-admin powered page, or task list shouldn't be shown. |
| 137 | if ( |
| 138 | ! \Automattic\WooCommerce\Admin\Loader::is_admin_page() || |
| 139 | ! \Automattic\WooCommerce\Admin\Features\Onboarding::should_show_tasks() |
| 140 | ) { |
| 141 | return $settings; |
| 142 | } |
| 143 | |
| 144 | // If onboarding isn't enabled this will throw warnings. |
| 145 | if ( ! isset( $settings['onboarding'] ) ) { |
| 146 | $settings['onboarding'] = array(); |
| 147 | } |
| 148 | |
| 149 | $settings['onboarding'] = array_merge( |
| 150 | $settings['onboarding'], |
| 151 | array( |
| 152 | 'tasksStatus' => self::get_settings(), |
| 153 | ) |
| 154 | ); |
| 155 | |
| 156 | return $settings; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Temporarily store the active task to persist across page loads when neccessary (such as publishing a product). Most tasks do not need to do this. |
| 161 | */ |
| 162 | public static function set_active_task() { |
| 163 | if ( isset( $_GET[ self::ACTIVE_TASK_TRANSIENT ] ) ) { // phpcs:ignore csrf ok. |
| 164 | $task = sanitize_title_with_dashes( wp_unslash( $_GET[ self::ACTIVE_TASK_TRANSIENT ] ) ); // phpcs:ignore csrf ok. |
| 165 | |
| 166 | if ( self::check_task_completion( $task ) ) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | set_transient( |
| 171 | self::ACTIVE_TASK_TRANSIENT, |
| 172 | $task, |
| 173 | DAY_IN_SECONDS |
| 174 | ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get the name of the active task. |
| 180 | * |
| 181 | * @return string |
| 182 | */ |
| 183 | public static function get_active_task() { |
| 184 | return get_transient( self::ACTIVE_TASK_TRANSIENT ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Check for active task completion, and clears the transient. |
| 189 | * |
| 190 | * @return bool |
| 191 | */ |
| 192 | public static function is_active_task_complete() { |
| 193 | $active_task = self::get_active_task(); |
| 194 | |
| 195 | if ( ! $active_task ) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if ( self::check_task_completion( $active_task ) ) { |
| 200 | delete_transient( self::ACTIVE_TASK_TRANSIENT ); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check for task completion of a given task. |
| 209 | * |
| 210 | * @param string $task Name of task. |
| 211 | * @return bool |
| 212 | */ |
| 213 | public static function check_task_completion( $task ) { |
| 214 | switch ( $task ) { |
| 215 | case 'products': |
| 216 | $products = wp_count_posts( 'product' ); |
| 217 | return (int) $products->publish > 0; |
| 218 | case 'homepage': |
| 219 | $homepage_id = get_option( 'woocommerce_onboarding_homepage_post_id', false ); |
| 220 | if ( ! $homepage_id ) { |
| 221 | return false; |
| 222 | } |
| 223 | $post = get_post( $homepage_id ); |
| 224 | $completed = $post && 'publish' === $post->post_status; |
| 225 | return $completed; |
| 226 | case 'tax': |
| 227 | return 'yes' === get_option( 'wc_connect_taxes_enabled' ) || |
| 228 | count( DataStore::get_taxes( array() ) ) > 0 || |
| 229 | false !== get_option( 'woocommerce_no_sales_tax' ); |
| 230 | } |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Sets the URL users are redirected to after PayPal Payments has received onboarding information from PayPal. |
| 237 | * |
| 238 | * @param string $url the current redirect url. |
| 239 | * @return string redirect url redirecting to WC Admin home screen. |
| 240 | */ |
| 241 | public static function ppcp_ob_after_onboarding_redirect_url( $url ) { |
| 242 | if ( isset( $_GET['ppcpobw'] ) && 1 === absint( $_GET['ppcpobw'] ) ) { // phpcs:ignore csrf ok, sanitization ok. |
| 243 | $url = wc_admin_url( '&task=payments&method=paypal&onboarding=complete' ); |
| 244 | } |
| 245 | |
| 246 | return $url; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | * Hooks into the product page to add a notice to return to the task list if a product was added. |
| 252 | * |
| 253 | * @param string $hook Page hook. |
| 254 | */ |
| 255 | public static function add_onboarding_product_notice_admin_script( $hook ) { |
| 256 | global $post; |
| 257 | if ( |
| 258 | 'post.php' !== $hook || |
| 259 | 'product' !== $post->post_type || |
| 260 | 'products' !== self::get_active_task() || |
| 261 | ! self::is_active_task_complete() |
| 262 | ) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | $script_assets_filename = Loader::get_script_asset_filename( 'wp-admin-scripts', 'onboarding-product-notice' ); |
| 267 | $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . 'wp-admin-scripts/' . $script_assets_filename; |
| 268 | |
| 269 | wp_enqueue_script( |
| 270 | 'onboarding-product-notice', |
| 271 | Loader::get_url( 'wp-admin-scripts/onboarding-product-notice', 'js' ), |
| 272 | array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'] ), |
| 273 | WC_ADMIN_VERSION_NUMBER, |
| 274 | true |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Hooks into the post page to display a different success notice and sets the active page as the site's home page if visted from onboarding. |
| 280 | * |
| 281 | * @param string $hook Page hook. |
| 282 | */ |
| 283 | public static function add_onboarding_homepage_notice_admin_script( $hook ) { |
| 284 | global $post; |
| 285 | if ( 'post.php' === $hook && 'page' === $post->post_type && isset( $_GET[ self::ACTIVE_TASK_TRANSIENT ] ) && 'homepage' === $_GET[ self::ACTIVE_TASK_TRANSIENT ] ) { // phpcs:ignore csrf ok. |
| 286 | $script_assets_filename = Loader::get_script_asset_filename( 'wp-admin-scripts', 'onboarding-homepage-notice' ); |
| 287 | $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . 'wp-admin-scripts/' . $script_assets_filename; |
| 288 | |
| 289 | wp_enqueue_script( |
| 290 | 'onboarding-homepage-notice', |
| 291 | Loader::get_url( 'wp-admin-scripts/onboarding-homepage-notice', 'js' ), |
| 292 | array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'] ), |
| 293 | WC_ADMIN_VERSION_NUMBER, |
| 294 | true |
| 295 | ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Adds a notice to return to the task list when the save button is clicked on tax settings pages. |
| 301 | */ |
| 302 | public static function add_onboarding_tax_notice_admin_script() { |
| 303 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; // phpcs:ignore csrf ok, sanitization ok. |
| 304 | $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : ''; // phpcs:ignore csrf ok, sanitization ok. |
| 305 | |
| 306 | if ( |
| 307 | 'wc-settings' === $page && |
| 308 | 'tax' === $tab && |
| 309 | 'tax' === self::get_active_task() && |
| 310 | ! self::is_active_task_complete() |
| 311 | ) { |
| 312 | $script_assets_filename = Loader::get_script_asset_filename( 'wp-admin-scripts', 'onboarding-tax-notice' ); |
| 313 | $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . 'wp-admin-scripts/' . $script_assets_filename; |
| 314 | |
| 315 | wp_enqueue_script( |
| 316 | 'onboarding-tax-notice', |
| 317 | Loader::get_url( 'wp-admin-scripts/onboarding-tax-notice', 'js' ), |
| 318 | array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'] ), |
| 319 | WC_ADMIN_VERSION_NUMBER, |
| 320 | true |
| 321 | ); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Adds a notice to return to the task list when the product importeris done running. |
| 327 | * |
| 328 | * @param string $hook Page hook. |
| 329 | */ |
| 330 | public function add_onboarding_product_import_notice_admin_script( $hook ) { |
| 331 | $step = isset( $_GET['step'] ) ? $_GET['step'] : ''; // phpcs:ignore csrf ok, sanitization ok. |
| 332 | if ( 'product_page_product_importer' === $hook && 'done' === $step && 'product-import' === self::get_active_task() ) { |
| 333 | delete_transient( self::ACTIVE_TASK_TRANSIENT ); |
| 334 | |
| 335 | $script_assets_filename = Loader::get_script_asset_filename( 'wp-admin-scripts', 'onboarding-product-import-notice' ); |
| 336 | $script_assets = require WC_ADMIN_ABSPATH . WC_ADMIN_DIST_JS_FOLDER . 'wp-admin-scripts/' . $script_assets_filename; |
| 337 | |
| 338 | wp_enqueue_script( |
| 339 | 'onboarding-product-import-notice', |
| 340 | Loader::get_url( 'wp-admin-scripts/onboarding-product-import-notice', 'js' ), |
| 341 | array_merge( array( WC_ADMIN_APP ), $script_assets ['dependencies'] ), |
| 342 | WC_ADMIN_VERSION_NUMBER, |
| 343 | true |
| 344 | ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Get an array of countries that support automated tax. |
| 350 | * |
| 351 | * @return array |
| 352 | */ |
| 353 | public static function get_automated_tax_supported_countries() { |
| 354 | // https://developers.taxjar.com/api/reference/#countries . |
| 355 | $tax_supported_countries = array_merge( |
| 356 | array( 'US', 'CA', 'AU' ), |
| 357 | WC()->countries->get_european_union_countries() |
| 358 | ); |
| 359 | |
| 360 | return $tax_supported_countries; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Returns a list of Stripe supported countries. This method can be removed once merged to core. |
| 365 | * |
| 366 | * @return array |
| 367 | */ |
| 368 | private static function get_stripe_supported_countries() { |
| 369 | // https://stripe.com/global. |
| 370 | return array( |
| 371 | 'AU', |
| 372 | 'AT', |
| 373 | 'BE', |
| 374 | 'BG', |
| 375 | 'BR', |
| 376 | 'CA', |
| 377 | 'CY', |
| 378 | 'CZ', |
| 379 | 'DK', |
| 380 | 'EE', |
| 381 | 'FI', |
| 382 | 'FR', |
| 383 | 'DE', |
| 384 | 'GR', |
| 385 | 'HK', |
| 386 | 'IN', |
| 387 | 'IE', |
| 388 | 'IT', |
| 389 | 'JP', |
| 390 | 'LV', |
| 391 | 'LT', |
| 392 | 'LU', |
| 393 | 'MY', |
| 394 | 'MT', |
| 395 | 'MX', |
| 396 | 'NL', |
| 397 | 'NZ', |
| 398 | 'NO', |
| 399 | 'PL', |
| 400 | 'PT', |
| 401 | 'RO', |
| 402 | 'SG', |
| 403 | 'SK', |
| 404 | 'SI', |
| 405 | 'ES', |
| 406 | 'SE', |
| 407 | 'CH', |
| 408 | 'GB', |
| 409 | 'US', |
| 410 | 'PR', |
| 411 | ); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Records an event when all tasks are completed in the task list. |
| 416 | * |
| 417 | * @param mixed $old_value Old value. |
| 418 | * @param mixed $new_value New value. |
| 419 | */ |
| 420 | public static function track_completion( $old_value, $new_value ) { |
| 421 | if ( $new_value ) { |
| 422 | wc_admin_record_tracks_event( 'tasklist_tasks_completed' ); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Records an event when all tasks are completed in the extended task list. |
| 428 | * |
| 429 | * @param mixed $old_value Old value. |
| 430 | * @param mixed $new_value New value. |
| 431 | */ |
| 432 | public static function track_extended_completion( $old_value, $new_value ) { |
| 433 | if ( $new_value ) { |
| 434 | wc_admin_record_tracks_event( 'extended_tasklist_tasks_completed' ); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Records an event for individual task completion. |
| 440 | * |
| 441 | * @param mixed $old_value Old value. |
| 442 | * @param mixed $new_value New value. |
| 443 | */ |
| 444 | public static function track_task_completion( $old_value, $new_value ) { |
| 445 | $old_value = is_array( $old_value ) ? $old_value : array(); |
| 446 | $untracked_tasks = array_diff( $new_value, $old_value ); |
| 447 | |
| 448 | foreach ( $untracked_tasks as $task ) { |
| 449 | wc_admin_record_tracks_event( 'tasklist_task_completed', array( 'task_name' => $task ) ); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Update registered extended task list items. |
| 455 | */ |
| 456 | public static function update_option_extended_task_list() { |
| 457 | if ( |
| 458 | ! \Automattic\WooCommerce\Admin\Loader::is_admin_page() || |
| 459 | ! \Automattic\WooCommerce\Admin\Features\Onboarding::should_show_tasks() |
| 460 | ) { |
| 461 | return; |
| 462 | } |
| 463 | $extended_tasks_list_items = get_option( 'woocommerce_extended_task_list_items', array() ); |
| 464 | $registered_extended_tasks_list_items = apply_filters( 'woocommerce_get_registered_extended_tasks', array() ); |
| 465 | if ( $registered_extended_tasks_list_items !== $extended_tasks_list_items ) { |
| 466 | update_option( 'woocommerce_extended_task_list_items', $registered_extended_tasks_list_items ); |
| 467 | update_option( 'woocommerce_extended_task_list_hidden', 'no' ); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 |