| 1 |
<?php |
| 2 |
|
| 3 |
use Updraftplus\Wp_Optimize\Wizard\Onboarding\Onboarding; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 6 |
|
| 7 |
require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/team-updraft/lib-onboarding-wizard/autoload.php'); |
| 8 |
|
| 9 |
/** |
| 10 |
* Class WPO_Onboarding |
| 11 |
*/ |
| 12 |
class WPO_Onboarding { |
| 13 |
|
| 14 |
const MAILING_LIST_FREE_ID = 132; |
| 15 |
|
| 16 |
const MAILING_LIST_PREMIUM_ID = 133; |
| 17 |
|
| 18 |
const MAILING_LIST_ENDPOINT = 'https://teamupdraft.com/?fluentcrm=1&route=contact&hash=69902751-58c5-460b-bd9f-456d62033c2b'; |
| 19 |
|
| 20 |
private $is_premium = false; |
| 21 |
|
| 22 |
private $prefix = 'wp-optimize'; |
| 23 |
|
| 24 |
private $caller_slug = 'wp-optimize'; |
| 25 |
|
| 26 |
private $upgrade_url = ''; |
| 27 |
|
| 28 |
private $is_multisite; |
| 29 |
|
| 30 |
/** |
| 31 |
* Private constructor to prevent direct instantiation |
| 32 |
*/ |
| 33 |
private function __construct() { |
| 34 |
$this->is_premium = WP_Optimize::is_premium(); |
| 35 |
$this->is_multisite = is_multisite(); |
| 36 |
|
| 37 |
if ($this->is_multisite) { |
| 38 |
$this->prefix = 'wp-optimize-mu'; |
| 39 |
} |
| 40 |
$this->upgrade_url = WP_Optimize()->premium_version_link; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get a singleton instance |
| 45 |
* |
| 46 |
* @return WPO_Onboarding |
| 47 |
*/ |
| 48 |
public static function instance(): WPO_Onboarding { |
| 49 |
static $instance = null; |
| 50 |
if (null === $instance) { |
| 51 |
$instance = new self(); |
| 52 |
} |
| 53 |
return $instance; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Setup hooks. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function init(): void { |
| 62 |
|
| 63 |
add_action( $this->prefix.'_onboarding_update_options', array($this, 'update_step_settings'), 10, 2 ); |
| 64 |
add_filter( $this->prefix.'_onboarding_steps', array($this, 'load_steps')); |
| 65 |
add_action( 'admin_post_wpo_reopen_onboarding', array($this, 'handle_reopen_onboarding') ); |
| 66 |
|
| 67 |
$this->setup_onboarding(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Handles the "Quick Setup" button: re-activates the onboarding wizard and redirects to the dashboard. |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function handle_reopen_onboarding(): void { |
| 76 |
check_admin_referer('wpo_reopen_onboarding'); |
| 77 |
|
| 78 |
$capability = $this->is_multisite ? 'manage_network_options' : 'manage_options'; |
| 79 |
if (!current_user_can($capability)) { |
| 80 |
wp_die(esc_html__('Sorry, you are not allowed to do this.', 'wp-optimize')); |
| 81 |
} |
| 82 |
|
| 83 |
$this->activate_onboarding_wizard(); |
| 84 |
|
| 85 |
wp_safe_redirect(WP_Optimize()->get_options()->admin_page_url('WP-Optimize')); |
| 86 |
exit; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns the URL that reopens the onboarding wizard, including a nonce. |
| 91 |
* |
| 92 |
* @return string |
| 93 |
*/ |
| 94 |
public function get_reopen_onboarding_url(): string { |
| 95 |
return wp_nonce_url( |
| 96 |
admin_url('admin-post.php?action=wpo_reopen_onboarding'), |
| 97 |
'wpo_reopen_onboarding' |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get Features Status |
| 103 |
* |
| 104 |
* @param array $settings Settings data |
| 105 |
* @param array $step_fields Step Fields data |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function update_step_settings(array $settings, array $step_fields): void { |
| 110 |
$step_ids = array_flip(array_column($step_fields, 'id')); |
| 111 |
|
| 112 |
if (empty($settings)) { |
| 113 |
return; |
| 114 |
} |
| 115 |
foreach ($settings as $setting) { |
| 116 |
if (empty($setting['id'])) continue; |
| 117 |
|
| 118 |
if (!isset($step_ids[$setting['id']])) continue; |
| 119 |
|
| 120 |
$is_lock = isset($setting['is_lock']) ? (bool) $setting['is_lock'] : false; |
| 121 |
if ($is_lock) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
$id = (string) $setting['id']; |
| 126 |
$value = isset($setting['value']) ? (bool) $setting['value'] : false; |
| 127 |
|
| 128 |
$this->apply_setting($id, $value); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Dispatch a setting change to its handler. |
| 134 |
* |
| 135 |
* @param string $id Setting identifier. |
| 136 |
* @param bool $value Whether to enable or disable the setting. |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
private function apply_setting(string $id, bool $value): void { |
| 140 |
$handlers = array( |
| 141 |
'enable_caching_onboarding' => array($this, 'apply_caching_setting'), |
| 142 |
'enable_minify_onboarding' => array($this, 'apply_minify_setting'), |
| 143 |
'enable_image_optimization_onboarding' => array($this, 'apply_image_setting'), |
| 144 |
); |
| 145 |
|
| 146 |
if (isset($handlers[$id])) { |
| 147 |
call_user_func($handlers[$id], $value); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Enable or disable page caching. |
| 153 |
* |
| 154 |
* Some related handling is intentionally commented out pending further work. |
| 155 |
* |
| 156 |
* @param bool $value True to enable, false to disable. |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
private function apply_caching_setting(bool $value): void { |
| 160 |
$page_cache = WP_Optimize()->get_page_cache(); |
| 161 |
|
| 162 |
if ($value) { |
| 163 |
$page_cache->enable(true); |
| 164 |
// $this->enable_gzip(); |
| 165 |
// $this->enable_browser_cache(); |
| 166 |
} else { |
| 167 |
$page_cache->disable(); |
| 168 |
// WP_Optimize()->get_gzip_compression()->disable(); |
| 169 |
// WP_Optimize()->get_browser_cache()->disable(); |
| 170 |
} |
| 171 |
|
| 172 |
if ($this->is_premium) { |
| 173 |
// $this->set_show_avatars($value); |
| 174 |
WP_Optimize_LCP_Settings::instance()->update_setting($value); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Enable gzip compression. |
| 180 |
* |
| 181 |
* @return void |
| 182 |
*/ |
| 183 |
private function enable_gzip(): void { |
| 184 |
WP_Optimize()->get_gzip_compression()->enable_gzip_command_handler(array('enable' => true)); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Enable browser cache using saved expiry options. |
| 189 |
* |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
private function enable_browser_cache(): void { |
| 193 |
$options = WP_Optimize()->get_options(); |
| 194 |
WP_Optimize()->get_browser_cache()->enable_browser_cache_command_handler(array( |
| 195 |
'browser_cache_expire_days' => $options->get_option('browser_cache_expire_days', '28'), |
| 196 |
'browser_cache_expire_hours' => $options->get_option('browser_cache_expire_hours', '0'), |
| 197 |
)); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Enable or disable avatar display (premium only). |
| 202 |
* |
| 203 |
* @param bool $value True to show avatars, false to hide. |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
private function set_show_avatars(bool $value): void { |
| 207 |
$commands = new WP_Optimize_Cache_Commands_Premium(); |
| 208 |
if (is_callable(array($commands, 'change_show_avatars'))) { |
| 209 |
$commands->change_show_avatars(array('show_avatars' => $value)); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Enable or disable CSS/JS minification. |
| 215 |
* |
| 216 |
* @param bool $value True to enable, false to disable. |
| 217 |
* @return void |
| 218 |
*/ |
| 219 |
private function apply_minify_setting(bool $value): void { |
| 220 |
$params = array('enabled' => $value); |
| 221 |
|
| 222 |
if ($this->is_premium) { |
| 223 |
$params['enable_capo_js'] = $value; |
| 224 |
$params['host_local_google_fonts'] = $value; |
| 225 |
} |
| 226 |
|
| 227 |
wp_optimize_minify_config()->update($params); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Enable or disable image optimization and related premium features like lazy load and image dimensions. |
| 232 |
* |
| 233 |
* @param bool $value True to enable, false to disable. |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
private function apply_image_setting(bool $value): void { |
| 237 |
Updraft_Smush_Manager()->update_smush_options(array('autosmush' => $value)); |
| 238 |
|
| 239 |
WP_Optimize()->get_webp_instance()->save_webp_settings(array('webp_conversion' => $value)); |
| 240 |
|
| 241 |
if (!$this->is_premium) return; |
| 242 |
|
| 243 |
WP_Optimize()->get_options()->save_lazy_load_settings(array( |
| 244 |
'lazyload' => array( |
| 245 |
'images' => $value, |
| 246 |
'iframes' => $value, |
| 247 |
'backgrounds' => $value, |
| 248 |
'youtube_preview' => $value, |
| 249 |
'skip_classes' => '', |
| 250 |
), |
| 251 |
)); |
| 252 |
|
| 253 |
WP_Optimize()->get_options()->update_option('image_dimensions', (int) $value); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Load the steps for the onboarding process. |
| 258 |
* |
| 259 |
* Builds and returns the steps depending on the user's |
| 260 |
* premium status and license connection. |
| 261 |
* |
| 262 |
* @param array $steps Unused parameter for compatibility. |
| 263 |
* |
| 264 |
* @return array Ordered an array of steps for the onboarding wizard. |
| 265 |
*/ |
| 266 |
public function load_steps($steps = array()): array { |
| 267 |
// Build steps |
| 268 |
$steps = array(); // To override all previous steps. |
| 269 |
|
| 270 |
// Step 1: Feature settings |
| 271 |
$steps[] = $this->features_step(); |
| 272 |
|
| 273 |
// Step 2: License step (only for premium users without a connected license) |
| 274 |
if ($this->should_add_license_step()) { |
| 275 |
$steps[] = $this->license_step(); |
| 276 |
} |
| 277 |
|
| 278 |
// Step 3: Newsletter signup |
| 279 |
$steps[] = $this->newsletter_step(); |
| 280 |
|
| 281 |
// Step 4: Recommended plugins |
| 282 |
$steps[] = $this->plugins_install_step(); |
| 283 |
|
| 284 |
// Step 5: Go Premium step (only for non-premium users) |
| 285 |
if (!$this->is_premium) { |
| 286 |
$steps[] = $this->go_premium_step(); |
| 287 |
} |
| 288 |
|
| 289 |
// Step 6: Final step |
| 290 |
$steps[] = $this->last_step(); |
| 291 |
|
| 292 |
return $steps; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Determine if the license step should be added. |
| 297 |
* |
| 298 |
* @return bool True if the plugin is premium but the license is not connected. |
| 299 |
*/ |
| 300 |
private function should_add_license_step(): bool { |
| 301 |
return $this->is_premium && !$this->is_license_connected(); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Build the license activation step for the onboarding wizard. |
| 306 |
* |
| 307 |
* This step includes fields for email and password, conditional |
| 308 |
* titles/subtitles based on license status, and a button to |
| 309 |
* activate the license. |
| 310 |
* |
| 311 |
* @return array Step configuration including ID, type, icon, titles, subtitles, fields, and button info. |
| 312 |
*/ |
| 313 |
private function license_step(): array { |
| 314 |
return array( |
| 315 |
'id' => 'license', |
| 316 |
'type' => 'license', |
| 317 |
'icon' => 'user-lock', |
| 318 |
'title' => __('Connect and activate your license', 'wp-optimize'), |
| 319 |
'title_conditional' => array( |
| 320 |
'licenseActivated' => __('License activated!', 'wp-optimize'), |
| 321 |
'isUpdating' => __('Activating your Premium license...', 'wp-optimize'), |
| 322 |
), |
| 323 |
'subtitle' => __('Please enter your TeamUpdraft credentials to start using Premium features.', 'wp-optimize'), |
| 324 |
'subtitle_conditional' => array( |
| 325 |
'licenseActivated' => '', |
| 326 |
'isUpdating' => '', |
| 327 |
), |
| 328 |
'fields' => array( |
| 329 |
array( |
| 330 |
'id' => 'registration_email', |
| 331 |
'type' => 'email', |
| 332 |
'label' => __('Email', 'wp-optimize'), |
| 333 |
), |
| 334 |
array( |
| 335 |
'id' => 'registration_password', |
| 336 |
'type' => 'password', |
| 337 |
'label' => __('Password', 'wp-optimize'), |
| 338 |
), |
| 339 |
), |
| 340 |
'button' => array( |
| 341 |
'id' => 'activate', |
| 342 |
'label'=> __('Confirm and activate', 'wp-optimize'), |
| 343 |
'icon' => 'EastRoundedIcon', |
| 344 |
), |
| 345 |
'skip_step' => true |
| 346 |
); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Build the step for the best-practice features in the onboarding wizard. |
| 351 |
* |
| 352 |
* @return array Step data including title, subtitle, feature fields, button info, and skip step info. |
| 353 |
*/ |
| 354 |
private function features_step(): array { |
| 355 |
$features = $this->get_feature_settings(); |
| 356 |
return array( |
| 357 |
'id' => 'page_features', |
| 358 |
'type' => 'settings', |
| 359 |
'title' => __('Your site is already faster!', 'wp-optimize'), |
| 360 |
'subtitle' => __('We enabled these settings optimised for your site.', 'wp-optimize').' '.__('Turn off anything you don\'t need.', 'wp-optimize'), |
| 361 |
'fields' => $features, |
| 362 |
'button' => array( |
| 363 |
'id' => 'save', |
| 364 |
'label'=> __('Save and continue', 'wp-optimize'), |
| 365 |
'icon' => 'EastRoundedIcon', |
| 366 |
), |
| 367 |
'skip_step' => array( |
| 368 |
'icon' => 'info', |
| 369 |
'tooltip' => array( |
| 370 |
'text' => __('All above features will be disabled if you skip.', 'wp-optimize'), |
| 371 |
), |
| 372 |
), |
| 373 |
); |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Build the step for a newsletter in the onboarding wizard. |
| 378 |
* |
| 379 |
* @return array Step data including title, subtitle, email fields, and button info. |
| 380 |
*/ |
| 381 |
private function newsletter_step(): array { |
| 382 |
$email_fields = $this->get_email_fields(); |
| 383 |
return array( |
| 384 |
'id' => 'email', |
| 385 |
'type' => 'email', |
| 386 |
'icon' => 'mail', |
| 387 |
'title' => __('Get lightning-fast insights!', 'wp-optimize'), |
| 388 |
'subtitle' => __('Join our newsletter for speed-optimization tips and best practices.', 'wp-optimize').' '.__('Delivered straight to your inbox.', 'wp-optimize'), |
| 389 |
'fields' => $email_fields, |
| 390 |
'button' => array( |
| 391 |
'id' => 'save', |
| 392 |
'label'=> __('Save and continue', 'wp-optimize'), |
| 393 |
'icon' => 'EastRoundedIcon', |
| 394 |
), |
| 395 |
'skip_step' => true |
| 396 |
); |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Build the step for the recommended plugins installation in the onboarding wizard. |
| 401 |
* |
| 402 |
* @return array Step data including title, subtitle, plugin fields, and button info. |
| 403 |
*/ |
| 404 |
private function plugins_install_step(): array { |
| 405 |
return array( |
| 406 |
'id' => 'plugins', |
| 407 |
'type' => 'plugins', |
| 408 |
'icon' => 'plugin', |
| 409 |
'first_run_only' => false, |
| 410 |
'title' => __('Recommended for your setup', 'wp-optimize'), |
| 411 |
'title_conditional' => array( |
| 412 |
'all_installed' => __('Best-practice plugins enabled', 'wp-optimize'), |
| 413 |
), |
| 414 |
'subtitle' => __('We\'ve carefully handpicked these plugins to match your website\'s setup, so everything works just the way it should.', 'wp-optimize'), |
| 415 |
'subtitle_conditional' => array( |
| 416 |
'all_installed' => __('Wow, your site already meets all our plugin recommendations, let\'s move on!', 'wp-optimize'), |
| 417 |
), |
| 418 |
'fields' => array( |
| 419 |
array( |
| 420 |
'id' => 'plugins', |
| 421 |
'type' => 'plugins', |
| 422 |
), |
| 423 |
), |
| 424 |
'button' => array( |
| 425 |
'id' => 'save', |
| 426 |
'label' => __('Install and continue', 'wp-optimize'), |
| 427 |
'icon' => 'EastRoundedIcon', |
| 428 |
), |
| 429 |
'skip_step' => true |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Build the "Go Premium" onboarding step array. |
| 435 |
* |
| 436 |
* @return array Step data including title, subtitle, bullets, and button info. |
| 437 |
*/ |
| 438 |
private function go_premium_step(): array { |
| 439 |
$go_premium_step_bullets = $this->get_go_premium_bullets(); |
| 440 |
return array( |
| 441 |
'id' => 'go_premium', |
| 442 |
'type' => 'go_premium', |
| 443 |
'icon' => 'bolt', |
| 444 |
'title' => __('Upgrade to Premium', 'wp-optimize'), |
| 445 |
'subtitle' => __('The complete optimization suite with safe defaults and expert help.', 'wp-optimize'), |
| 446 |
'bullets' => $go_premium_step_bullets, |
| 447 |
'enable_premium_btn' => true, |
| 448 |
'premium_btn_text' => __('Upgrade to Premium', 'wp-optimize'), |
| 449 |
'skip_step' => true |
| 450 |
); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Build the final "Completed" onboarding step array. |
| 455 |
* |
| 456 |
* @return array Step data including title, subtitle, bullets, and finish button info. |
| 457 |
*/ |
| 458 |
private function last_step(): array { |
| 459 |
list($last_step_subtitle, $last_step_subtitle_is_installing) = $this->get_last_step_subtitles(); |
| 460 |
|
| 461 |
$last_step_bullets = $this->get_last_step_bullets(); |
| 462 |
|
| 463 |
return array( |
| 464 |
'id' => 'completed', |
| 465 |
'type' => 'completed', |
| 466 |
'icon' => 'CheckRoundedIcon', |
| 467 |
'title' => __('You\'re all set!', 'wp-optimize'), |
| 468 |
'title_conditional' => array( |
| 469 |
'isInstalling' => __('Almost done, finalizing...', 'wp-optimize'), |
| 470 |
), |
| 471 |
'subtitle' => $last_step_subtitle, |
| 472 |
'subtitle_conditional' => array( |
| 473 |
'isInstalling' => $last_step_subtitle_is_installing |
| 474 |
), |
| 475 |
'bullets' => $last_step_bullets, |
| 476 |
'button' => array( |
| 477 |
'id' => 'finish', |
| 478 |
'label' => __('Go to settings', 'wp-optimize'), |
| 479 |
), |
| 480 |
); |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* Check if the plugin license is connected via the Updraft updater instance. |
| 485 |
* |
| 486 |
* @return bool True if the license is connected, false otherwise. |
| 487 |
*/ |
| 488 |
private function is_license_connected(): bool { |
| 489 |
global $updraft_updater_instance; |
| 490 |
|
| 491 |
if (!isset($updraft_updater_instance)) { |
| 492 |
return false; |
| 493 |
} |
| 494 |
|
| 495 |
return (bool) $updraft_updater_instance->is_connected(); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Get bullet points for the "Go Premium" step. |
| 500 |
* |
| 501 |
* Each bullet highlights a benefit of upgrading to the Premium version. |
| 502 |
* |
| 503 |
* @return array Array of bullet strings, each wrapped in its own array. |
| 504 |
*/ |
| 505 |
private function get_go_premium_bullets(): array { |
| 506 |
return array( |
| 507 |
array( |
| 508 |
__('Ranking high', 'wp-optimize') |
| 509 |
), |
| 510 |
array( |
| 511 |
__('Loading at top speed', 'wp-optimize') |
| 512 |
), |
| 513 |
array( |
| 514 |
__('Turning more visits into sales', 'wp-optimize'), |
| 515 |
), |
| 516 |
array( |
| 517 |
__('Serving visitors & customers better', 'wp-optimize'), |
| 518 |
), |
| 519 |
); |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Get subtitles for the last onboarding step. |
| 524 |
* |
| 525 |
* Provides two strings: |
| 526 |
* 1. Standard subtitle for the completed step. |
| 527 |
* 2. Subtitle showed when redirecting to settings (with a link to the plugin page). |
| 528 |
* |
| 529 |
* @return array Array containing two strings: [standard_subtitle, installing_subtitle]. |
| 530 |
*/ |
| 531 |
private function get_last_step_subtitles(): array { |
| 532 |
|
| 533 |
$subtitle = __('WP-Optimize is running.', 'wp-optimize'); |
| 534 |
$subtitle .= ' '; |
| 535 |
$subtitle .= __('You can review and adjust any settings anytime from the settings page.', 'wp-optimize'); |
| 536 |
|
| 537 |
$installing = __('Setting things up in the background...', 'wp-optimize') . '<br>'; |
| 538 |
$installing .= __('This will only take a moment.', 'wp-optimize'); |
| 539 |
|
| 540 |
return array($subtitle, $installing); |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Get the list of feature bullets shown in the final onboarding step. |
| 545 |
* |
| 546 |
* Returns premium-only feature bullet points. |
| 547 |
* If the user is not premium, an empty array is returned. |
| 548 |
* |
| 549 |
* @return array List of bullet point groups. |
| 550 |
*/ |
| 551 |
private function get_last_step_bullets(): array { |
| 552 |
if (!$this->is_premium) { |
| 553 |
return array(); |
| 554 |
} |
| 555 |
|
| 556 |
return array( |
| 557 |
array( |
| 558 |
__('Lazy loading', 'wp-optimize'), |
| 559 |
__('Remove unused images', 'wp-optimize'), |
| 560 |
), |
| 561 |
array( |
| 562 |
__('Multisite optimization', 'wp-optimize'), |
| 563 |
__('Preload key requests', 'wp-optimize'), |
| 564 |
), |
| 565 |
array( |
| 566 |
__('User and role-based cache', 'wp-optimize'), |
| 567 |
__('Premium support and more', 'wp-optimize'), |
| 568 |
), |
| 569 |
); |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Get email-related form fields for the onboarding wizard. |
| 574 |
* |
| 575 |
* Returns configuration for email input and consent checkbox fields. |
| 576 |
* |
| 577 |
* @return array List of email field definitions. |
| 578 |
*/ |
| 579 |
private function get_email_fields(): array { |
| 580 |
return array( |
| 581 |
array( |
| 582 |
'id' => 'email_reports_mailinglist', |
| 583 |
'key' => 'email_reports_mailinglist', |
| 584 |
'type' => 'email', |
| 585 |
'label' => __('Email', 'wp-optimize'), |
| 586 |
'default' => '', |
| 587 |
), |
| 588 |
array( |
| 589 |
'id' => 'tips_tricks_mailinglist', |
| 590 |
'key' => 'tips_tricks_mailinglist', |
| 591 |
'type' => 'checkbox', |
| 592 |
'label' => __('I agree to receive emails with tips, updates and marketing content.', 'wp-optimize').' '.__('I understand I can unsubscribe at any time.', 'wp-optimize'), |
| 593 |
'default' => false, |
| 594 |
'show_privacy_link' => true, |
| 595 |
), |
| 596 |
); |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* Get the list of feature settings for the onboarding wizard. |
| 601 |
* |
| 602 |
* Generates an array of feature configuration options, including |
| 603 |
* labels, types, defaults, and details. |
| 604 |
* |
| 605 |
* @return array List of feature setting definitions. |
| 606 |
*/ |
| 607 |
private function get_feature_settings(): array { |
| 608 |
$label_details = __('Details', 'wp-optimize'); |
| 609 |
|
| 610 |
$default_config = array( |
| 611 |
'type' => 'checkbox', |
| 612 |
'subtype' => 'switch', |
| 613 |
'layout' => 'card', |
| 614 |
'show_details_label' => $label_details, |
| 615 |
'hide_details_label' => $label_details, |
| 616 |
'default' => true, |
| 617 |
); |
| 618 |
|
| 619 |
return array( |
| 620 |
array_merge($default_config, $this->get_cache_feature()), |
| 621 |
array_merge($default_config, $this->get_minify_feature()), |
| 622 |
array_merge($default_config, $this->get_image_feature()), |
| 623 |
); |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Build the cache feature definition for the onboarding wizard. |
| 628 |
* |
| 629 |
* Covers page caching. |
| 630 |
* For premium users, it also includes the Auto LCP preload description. |
| 631 |
* |
| 632 |
* @return array Feature field definition array. |
| 633 |
*/ |
| 634 |
private function get_cache_feature(): array { |
| 635 |
$details = __('Enables page caching.', 'wp-optimize'); |
| 636 |
|
| 637 |
if ($this->is_premium) { |
| 638 |
$details .= ' '.__('Automatically improves Largest Contentful Paint (LCP) when supported.', 'wp-optimize'); |
| 639 |
} |
| 640 |
|
| 641 |
$details .= ' '.__('You can turn these off individually anytime.', 'wp-optimize'); |
| 642 |
return array( |
| 643 |
'id' => 'enable_caching_onboarding', |
| 644 |
'key' => 'enable_caching_onboarding', |
| 645 |
'label' => __('Faster pages', 'wp-optimize'), |
| 646 |
'icon' => 'speed', |
| 647 |
'summary' => __('Pages refresh in the background, so return visits are always fast.', 'wp-optimize'), |
| 648 |
'details' => $details, |
| 649 |
); |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
* Build the minification feature definition for the onboarding wizard. |
| 654 |
* |
| 655 |
* Covers CSS and JS minification and file bundling. |
| 656 |
* For premium users, it also includes Capo.js and local Google Fonts hosting. |
| 657 |
* |
| 658 |
* @return array Feature field definition array. |
| 659 |
*/ |
| 660 |
private function get_minify_feature(): array { |
| 661 |
$details = __('Minifies CSS and JavaScript, and bundles related files together.', 'wp-optimize'); |
| 662 |
$details .= ' '.__('Payment provider scripts are excluded automatically.', 'wp-optimize'); |
| 663 |
|
| 664 |
if ($this->is_premium) { |
| 665 |
$details .= ' '.__('Includes Capo.js for smarter script loading and hosts Google Fonts locally to reduce external requests.', 'wp-optimize'); |
| 666 |
} |
| 667 |
|
| 668 |
return array( |
| 669 |
'id' => 'enable_minify_onboarding', |
| 670 |
'key' => 'enable_minify_onboarding', |
| 671 |
'icon' => 'devices', |
| 672 |
'label' => __('Lighter on every device', 'wp-optimize'), |
| 673 |
'summary' => __('Smaller files reach browsers faster, especially on mobile.', 'wp-optimize'), |
| 674 |
'details' => $details, |
| 675 |
); |
| 676 |
} |
| 677 |
|
| 678 |
/** |
| 679 |
* Build the image optimization feature definition for the onboarding wizard. |
| 680 |
* |
| 681 |
* Covers image compression and WebP conversion. |
| 682 |
* For premium users, it also includes lazy loading and image dimensions. |
| 683 |
* |
| 684 |
* @return array Feature field definition array. |
| 685 |
*/ |
| 686 |
private function get_image_feature(): array { |
| 687 |
$details = __('Bundles image compression and WebP conversion.', 'wp-optimize'); |
| 688 |
$details .= ' '.__('New uploads are compressed automatically and originals are backed up.', 'wp-optimize'); |
| 689 |
|
| 690 |
if ($this->is_premium) { |
| 691 |
$details .= ' '.__('Also adds image dimensions and lazy loading to improve perceived speed.', 'wp-optimize'); |
| 692 |
} |
| 693 |
|
| 694 |
$details .= ' '.__('You can change this anytime.', 'wp-optimize'); |
| 695 |
|
| 696 |
return array( |
| 697 |
'id' => 'enable_image_optimization_onboarding', |
| 698 |
'key' => 'enable_image_optimization_onboarding', |
| 699 |
'icon' => 'images', |
| 700 |
'label' => __('Optimised media', 'wp-optimize'), |
| 701 |
'summary' => __('We\'ll compress your images.', 'wp-optimize').' '.__('Same quality, faster load.', 'wp-optimize'), |
| 702 |
'details' => $details, |
| 703 |
); |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* Array of UTM parameters for the onboarding wizard. |
| 708 |
* |
| 709 |
* @param string $content UTM content. |
| 710 |
* @param string $format UTM creative_format. |
| 711 |
* |
| 712 |
* @return array |
| 713 |
*/ |
| 714 |
private function get_utm_params_to_override($content = 'onboarding', $format = 'text'): array { |
| 715 |
$type = $this->is_premium ? 'prem' : 'free'; |
| 716 |
return array( |
| 717 |
'utm_content' => $content, |
| 718 |
'utm_campaign' => sprintf('paac-%s-onboarding-wizard', $type), |
| 719 |
'utm_creative_format' => $format, |
| 720 |
); |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* Initialize the onboarding |
| 725 |
* |
| 726 |
* @return void |
| 727 |
*/ |
| 728 |
private function setup_onboarding(): void { |
| 729 |
$onboarding = new Onboarding(); |
| 730 |
if ($onboarding::is_onboarding_active($this->prefix, $this->caller_slug)) { |
| 731 |
|
| 732 |
$support_link = $this->is_premium ? WP_Optimize_Utils::add_utm_params('https://teamupdraft.com/support/premium-support/', $this->get_utm_params_to_override('premium-support')) : esc_url('https://wordpress.org/support/plugin/wp-optimize/'); |
| 733 |
|
| 734 |
$onboarding->is_pro = $this->is_premium; |
| 735 |
$onboarding->logo_path = esc_url(trailingslashit(WPO_PLUGIN_URL) . 'images/notices/wp_optimize_logo.png'); |
| 736 |
$onboarding->prefix = $this->prefix; |
| 737 |
$onboarding->plugin_name = $this->is_premium ? 'WP-Optimize Premium' : 'WP-Optimize'; |
| 738 |
$onboarding->mailing_list = array($this->is_premium ? self::MAILING_LIST_PREMIUM_ID : self::MAILING_LIST_FREE_ID); |
| 739 |
$onboarding->mailing_list_endpoint = self::MAILING_LIST_ENDPOINT; |
| 740 |
$onboarding->caller_slug = $this->caller_slug; |
| 741 |
$onboarding->capability = $this->is_multisite ? 'manage_network_options' : 'manage_options'; |
| 742 |
$onboarding->support_url = $support_link; |
| 743 |
$onboarding->privacy_url_label = __('Privacy Policy.', 'wp-optimize'); |
| 744 |
$onboarding->privacy_statement_url = WP_Optimize_Utils::add_utm_params('https://teamupdraft.com/privacy/', $this->get_utm_params_to_override('privacy-statement')); |
| 745 |
$onboarding->forgot_password_url = WP_Optimize_Utils::add_utm_params('https://teamupdraft.com/my-account/lost-password/', $this->get_utm_params_to_override('forgot-password')); |
| 746 |
$onboarding->documentation_url = WP_Optimize_Utils::add_utm_params('https://teamupdraft.com/documentation/wp-optimize/', $this->get_utm_params_to_override('documentation')); |
| 747 |
$onboarding->upgrade_url = WP_Optimize_Utils::add_utm_params($this->upgrade_url, $this->get_utm_params_to_override('upgrade-to-premium', 'button'), true); |
| 748 |
$onboarding->page_prefix = $this->caller_slug; |
| 749 |
$onboarding->version = WPO_VERSION; |
| 750 |
$onboarding->languages_dir = WPO_PLUGIN_MAIN_PATH . 'languages'; |
| 751 |
$onboarding->text_domain = 'wp-optimize'; |
| 752 |
$onboarding->exit_wizard_text = __('Exit setup', 'wp-optimize'); |
| 753 |
$onboarding->reload_settings_page_on_finish = true; |
| 754 |
$onboarding->udmupdater_muid = 2; |
| 755 |
$onboarding->udmupdater_slug = 'wp-optimize-premium'; |
| 756 |
|
| 757 |
$onboarding->init(); |
| 758 |
|
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
/** |
| 763 |
* Check if the onboarding wizard is currently active (i.e. its modal is/will be shown) |
| 764 |
* |
| 765 |
* @return bool |
| 766 |
*/ |
| 767 |
public function is_active(): bool { |
| 768 |
return Onboarding::is_onboarding_active($this->prefix, $this->caller_slug); |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Enable the onboarding wizard for the plugin. |
| 773 |
* |
| 774 |
* This method performs two actions: |
| 775 |
* |
| 776 |
* 1. Sets an option using site option functions, so it works for both single and multisite to trigger the onboarding |
| 777 |
* wizard the next time a WP-Optimize page loads. |
| 778 |
* 2. Removes the installation source flag, ensuring that the plugin does not |
| 779 |
* treat the current activation as originating from the onboarding wizard. |
| 780 |
* |
| 781 |
* WordPress Internally, uses the site wrappers for update_option() and delete_option() |
| 782 |
* to ensure compatibility with both single-site and multisite environments. |
| 783 |
* |
| 784 |
* @return void |
| 785 |
*/ |
| 786 |
public function activate_onboarding_wizard(): void { |
| 787 |
// to enable onboarding wizard on WP-Optimize pages |
| 788 |
update_site_option($this->prefix.'_start_onboarding', true); |
| 789 |
|
| 790 |
// to reset that this plugin was installed from the onboarding wizard |
| 791 |
delete_site_option('teamupdraft_installation_source_'.$this->caller_slug); |
| 792 |
} |
| 793 |
} |
| 794 |
|