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
OnboardingSetupWizard.php
349 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Onboarding Setup Wizard |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Admin\Onboarding; |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\PageController; |
| 9 | use Automattic\WooCommerce\Admin\WCAdminHelper; |
| 10 | use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile; |
| 11 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; |
| 12 | use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\Init; |
| 13 | use Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions\ProcessCoreProfilerPluginInstallOptions; |
| 14 | |
| 15 | /** |
| 16 | * Contains backend logic for the onboarding profile and checklist feature. |
| 17 | */ |
| 18 | class OnboardingSetupWizard { |
| 19 | /** |
| 20 | * Class instance. |
| 21 | * |
| 22 | * @var OnboardingSetupWizard instance |
| 23 | */ |
| 24 | private static $instance = null; |
| 25 | |
| 26 | /** |
| 27 | * Get class instance. |
| 28 | */ |
| 29 | final public static function instance() { |
| 30 | if ( ! static::$instance ) { |
| 31 | static::$instance = new static(); |
| 32 | } |
| 33 | return static::$instance; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Add onboarding actions. |
| 38 | */ |
| 39 | public function init() { |
| 40 | // should be placed before is_admin() check as this hook is triggered in AJAX calls. |
| 41 | add_action( |
| 42 | 'woocommerce_plugins_install_before', |
| 43 | function ( $slug, $source ) { |
| 44 | $this->install_options_for_core_profiler_plugin_install( $slug, $source ); |
| 45 | }, |
| 46 | 10, |
| 47 | 2 |
| 48 | ); |
| 49 | |
| 50 | if ( ! is_admin() ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // Old settings injection. |
| 55 | // Run after Automattic\WooCommerce\Internal\Admin\Loader. |
| 56 | add_filter( 'woocommerce_components_settings', array( $this, 'component_settings' ), 20 ); |
| 57 | // New settings injection. |
| 58 | add_filter( 'woocommerce_admin_shared_settings', array( $this, 'component_settings' ), 20 ); |
| 59 | add_filter( 'woocommerce_admin_preload_settings', array( $this, 'preload_settings' ) ); |
| 60 | add_filter( 'admin_body_class', array( $this, 'add_loading_classes' ) ); |
| 61 | add_action( 'admin_init', array( $this, 'do_admin_redirects' ) ); |
| 62 | add_action( 'current_screen', array( $this, 'redirect_to_profiler' ) ); |
| 63 | add_filter( 'woocommerce_show_admin_notice', array( $this, 'remove_old_install_notice' ), 10, 2 ); |
| 64 | add_filter( 'admin_viewport_meta', array( $this, 'set_viewport_meta_tag' ) ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Test whether the context of execution comes from async action scheduler. |
| 69 | * Note: this is a polyfill for wc_is_running_from_async_action_scheduler() |
| 70 | * which was introduced in WC 4.0. |
| 71 | * |
| 72 | * @return bool |
| 73 | */ |
| 74 | private function is_running_from_async_action_scheduler() { |
| 75 | if ( function_exists( '\wc_is_running_from_async_action_scheduler' ) ) { |
| 76 | return \wc_is_running_from_async_action_scheduler(); |
| 77 | } |
| 78 | |
| 79 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 80 | return isset( $_REQUEST['action'] ) && 'as_async_request_queue_runner' === $_REQUEST['action']; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Handle redirects to setup/welcome page after install and updates. |
| 85 | * |
| 86 | * For setup wizard, transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters. |
| 87 | */ |
| 88 | public function do_admin_redirects() { |
| 89 | // Don't run this fn from Action Scheduler requests, as it would clear _wc_activation_redirect transient. |
| 90 | // That means OBW would never be shown. |
| 91 | if ( $this->is_running_from_async_action_scheduler() ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // Setup wizard redirect. |
| 96 | // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment |
| 97 | if ( get_transient( '_wc_activation_redirect' ) && apply_filters( 'woocommerce_enable_setup_wizard', true ) ) { |
| 98 | $do_redirect = true; |
| 99 | $current_page = isset( $_GET['page'] ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification |
| 100 | $is_onboarding_path = ! isset( $_GET['path'] ) || '/setup-wizard' === wc_clean( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 101 | |
| 102 | // On these pages, or during these events, postpone the redirect. |
| 103 | // phpcs:ignore WordPress.WP.Capabilities.Unknown |
| 104 | if ( wp_doing_ajax() || is_network_admin() || ! current_user_can( 'manage_woocommerce' ) ) { |
| 105 | $do_redirect = false; |
| 106 | } |
| 107 | |
| 108 | // On these pages, or during these events, disable the redirect. |
| 109 | if ( |
| 110 | ( 'wc-admin' === $current_page && $is_onboarding_path ) || |
| 111 | // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment |
| 112 | apply_filters( 'woocommerce_prevent_automatic_wizard_redirect', false ) || |
| 113 | isset( $_GET['activate-multi'] ) // phpcs:ignore WordPress.Security.NonceVerification |
| 114 | ) { |
| 115 | delete_transient( '_wc_activation_redirect' ); |
| 116 | $do_redirect = false; |
| 117 | } |
| 118 | |
| 119 | if ( $do_redirect ) { |
| 120 | delete_transient( '_wc_activation_redirect' ); |
| 121 | wp_safe_redirect( wc_admin_url() ); |
| 122 | exit; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Trigger the woocommerce_onboarding_profile_completed action |
| 129 | * |
| 130 | * @param array $old_value Previous value. |
| 131 | * @param array $value Current value. |
| 132 | */ |
| 133 | public function trigger_profile_completed_action( $old_value, $value ) { |
| 134 | if ( isset( $old_value['completed'] ) && $old_value['completed'] ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | if ( ! isset( $value['completed'] ) || ! $value['completed'] ) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Action hook fired when the onboarding profile (or onboarding wizard, |
| 144 | * or profiler) is completed. |
| 145 | * |
| 146 | * @since 1.5.0 |
| 147 | */ |
| 148 | do_action( 'woocommerce_onboarding_profile_completed' ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns true if the profiler should be displayed (not completed and not skipped). |
| 153 | * |
| 154 | * @return bool |
| 155 | */ |
| 156 | private function should_show() { |
| 157 | if ( $this->is_setup_wizard() ) { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | return OnboardingProfile::needs_completion(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Redirect to the profiler on homepage if completion is needed. |
| 166 | */ |
| 167 | public function redirect_to_profiler() { |
| 168 | if ( ! $this->is_homepage() || ! OnboardingProfile::needs_completion() ) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | wp_safe_redirect( wc_admin_url( '&path=/setup-wizard' ) ); |
| 173 | exit; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Check if the current page is the profile wizard. |
| 178 | * |
| 179 | * @return bool |
| 180 | */ |
| 181 | private function is_setup_wizard() { |
| 182 | /* phpcs:disable WordPress.Security.NonceVerification */ |
| 183 | return isset( $_GET['page'] ) && |
| 184 | 'wc-admin' === $_GET['page'] && |
| 185 | isset( $_GET['path'] ) && |
| 186 | '/setup-wizard' === $_GET['path']; |
| 187 | /* phpcs: enable */ |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Check if the current page is the homepage. |
| 192 | * |
| 193 | * @return bool |
| 194 | */ |
| 195 | private function is_homepage() { |
| 196 | /* phpcs:disable WordPress.Security.NonceVerification */ |
| 197 | return isset( $_GET['page'] ) && |
| 198 | 'wc-admin' === $_GET['page'] && |
| 199 | ! isset( $_GET['path'] ); |
| 200 | /* phpcs: enable */ |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Determine if the current page is one of the WC Admin pages. |
| 205 | * |
| 206 | * @return bool |
| 207 | */ |
| 208 | private function is_woocommerce_page() { |
| 209 | $current_page = PageController::get_instance()->get_current_page(); |
| 210 | if ( ! $current_page || ! isset( $current_page['path'] ) ) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | return 0 === strpos( $current_page['path'], 'wc-admin' ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Add profiler items to component settings. |
| 219 | * |
| 220 | * @param array $settings Component settings. |
| 221 | * |
| 222 | * @return array |
| 223 | */ |
| 224 | public function component_settings( $settings ) { |
| 225 | $profile = (array) get_option( OnboardingProfile::DATA_OPTION, array() ); |
| 226 | $settings['onboarding'] = array( |
| 227 | 'profile' => $profile, |
| 228 | ); |
| 229 | |
| 230 | // Only fetch if the onboarding wizard OR the task list is incomplete or currently shown |
| 231 | // or the current page is one of the WooCommerce Admin pages. |
| 232 | if ( |
| 233 | ( ! $this->should_show() && ! count( TaskLists::get_visible() ) |
| 234 | // phpcs:ignore Generic.CodeAnalysis.RequireExplicitBooleanOperatorPrecedence.MissingParentheses |
| 235 | || |
| 236 | ! $this->is_woocommerce_page() |
| 237 | ) |
| 238 | ) { |
| 239 | return $settings; |
| 240 | } |
| 241 | |
| 242 | include_once WC_ABSPATH . 'includes/admin/helper/class-wc-helper-options.php'; |
| 243 | $wccom_auth = \WC_Helper_Options::get( 'auth' ); |
| 244 | $profile['wccom_connected'] = empty( $wccom_auth['access_token'] ) ? false : true; |
| 245 | |
| 246 | $settings['onboarding']['currencySymbols'] = get_woocommerce_currency_symbols(); |
| 247 | $settings['onboarding']['euCountries'] = WC()->countries->get_european_union_countries(); |
| 248 | $settings['onboarding']['localeInfo'] = include WC()->plugin_path() . '/i18n/locale-info.php'; |
| 249 | $settings['onboarding']['profile'] = $profile; |
| 250 | |
| 251 | if ( $this->is_setup_wizard() ) { |
| 252 | $settings['onboarding']['pageCount'] = (int) ( wp_count_posts( 'page' ) )->publish; |
| 253 | $settings['onboarding']['postCount'] = (int) ( wp_count_posts( 'post' ) )->publish; |
| 254 | $settings['onboarding']['isBlockTheme'] = wp_is_block_theme(); |
| 255 | } |
| 256 | |
| 257 | // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment |
| 258 | return apply_filters( 'woocommerce_admin_onboarding_preloaded_data', $settings ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Preload WC setting options to prime state of the application. |
| 263 | * |
| 264 | * @param array $options Array of options to preload. |
| 265 | * @return array |
| 266 | */ |
| 267 | public function preload_settings( $options ) { |
| 268 | $options[] = 'general'; |
| 269 | |
| 270 | return $options; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Set the admin full screen class when loading to prevent flashes of unstyled content. |
| 275 | * |
| 276 | * @param bool $classes Body classes. |
| 277 | * @return array |
| 278 | */ |
| 279 | public function add_loading_classes( $classes ) { |
| 280 | /* phpcs:disable WordPress.Security.NonceVerification */ |
| 281 | if ( $this->is_setup_wizard() ) { |
| 282 | $classes .= ' woocommerce-admin-full-screen'; |
| 283 | } |
| 284 | /* phpcs: enable */ |
| 285 | |
| 286 | return $classes; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Remove the install notice that prompts the user to visit the old onboarding setup wizard. |
| 291 | * |
| 292 | * @param bool $show Show or hide the notice. |
| 293 | * @param string $notice The slug of the notice. |
| 294 | * @return bool |
| 295 | */ |
| 296 | public function remove_old_install_notice( $show, $notice ) { |
| 297 | if ( 'install' === $notice ) { |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | return $show; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Set the viewport meta tag for the setup wizard. |
| 306 | * |
| 307 | * @param string $viewport_meta Viewport meta content value. |
| 308 | * @return string Viewport meta content value. |
| 309 | * |
| 310 | * @since 9.0.0 |
| 311 | */ |
| 312 | public function set_viewport_meta_tag( $viewport_meta ) { |
| 313 | if ( ! $this->is_setup_wizard() ) { |
| 314 | return $viewport_meta; |
| 315 | } |
| 316 | |
| 317 | return 'width=device-width, initial-scale=1.0, maximum-scale=1.0'; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Install options for core profiler plugin install. |
| 322 | * |
| 323 | * When a plugin is installed from the core profiler, this method is called to process the install options. |
| 324 | * |
| 325 | * Install options are a list of options that are set for the plugin being installed. |
| 326 | * |
| 327 | * @param string $slug Plugin slug. |
| 328 | * @param string $source Source of the plugin install. |
| 329 | * |
| 330 | * @return void|null |
| 331 | */ |
| 332 | public function install_options_for_core_profiler_plugin_install( $slug, $source ) { |
| 333 | // Only proceed if the plugin install was initiated from the core profiler. |
| 334 | if ( 'core-profiler' !== $source ) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | // Retrieve the core profiler spec. |
| 339 | $specs = array_filter( Init::get_specs(), fn( $spec ) => 'obw/core-profiler' === $spec->key ); |
| 340 | |
| 341 | if ( ! $specs ) { |
| 342 | return null; |
| 343 | } |
| 344 | |
| 345 | $install_options = new ProcessCoreProfilerPluginInstallOptions( current( $specs )->plugins, $slug, wc_get_logger() ); |
| 346 | $install_options->process_install_options(); |
| 347 | } |
| 348 | } |
| 349 |