| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\Onboarding\Storage; |
| 4 |
|
| 5 |
use Elementor\App\Modules\Onboarding\Module; |
| 6 |
use Elementor\App\Modules\Onboarding\Storage\Entities\User_Choices; |
| 7 |
use Elementor\App\Modules\Onboarding\Storage\Entities\User_Progress; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class Onboarding_Progress_Manager { |
| 14 |
|
| 15 |
const PROGRESS_OPTION_KEY = 'elementor_onboarding_progress'; |
| 16 |
const CHOICES_OPTION_KEY = 'elementor_onboarding_choices'; |
| 17 |
const DEFAULT_TOTAL_STEPS = 5; |
| 18 |
|
| 19 |
private static ?Onboarding_Progress_Manager $instance = null; |
| 20 |
|
| 21 |
public static function instance(): Onboarding_Progress_Manager { |
| 22 |
if ( null === self::$instance ) { |
| 23 |
self::$instance = new self(); |
| 24 |
} |
| 25 |
|
| 26 |
return self::$instance; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_progress(): User_Progress { |
| 30 |
$data = get_option( self::PROGRESS_OPTION_KEY, [] ); |
| 31 |
|
| 32 |
return User_Progress::from_array( $data ); |
| 33 |
} |
| 34 |
|
| 35 |
public function save_progress( User_Progress $progress ): User_Progress { |
| 36 |
update_option( self::PROGRESS_OPTION_KEY, $progress->to_array() ); |
| 37 |
|
| 38 |
return $progress; |
| 39 |
} |
| 40 |
|
| 41 |
public function update_progress( array $params ): User_Progress { |
| 42 |
$progress = $this->get_progress(); |
| 43 |
|
| 44 |
if ( isset( $params['current_step'] ) ) { |
| 45 |
$progress->set_current_step( (int) $params['current_step'] ); |
| 46 |
} |
| 47 |
|
| 48 |
if ( isset( $params['completed_steps'] ) ) { |
| 49 |
$progress->set_completed_steps( (array) $params['completed_steps'] ); |
| 50 |
} |
| 51 |
|
| 52 |
if ( isset( $params['exit_type'] ) ) { |
| 53 |
$progress->set_exit_type( $params['exit_type'] ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( isset( $params['complete_step'] ) ) { |
| 57 |
$step = $params['complete_step']; |
| 58 |
$progress->add_completed_step( $step ); |
| 59 |
|
| 60 |
$step_index = $params['step_index'] ?? $progress->get_current_step_index(); |
| 61 |
$total_steps = $params['total_steps'] ?? self::DEFAULT_TOTAL_STEPS; |
| 62 |
$next_index = $step_index + 1; |
| 63 |
|
| 64 |
if ( $next_index < $total_steps ) { |
| 65 |
$progress->set_current_step_index( $next_index ); |
| 66 |
$progress->set_current_step_id( null ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! empty( $params['skip_step'] ) ) { |
| 71 |
$step_index = $params['step_index'] ?? $progress->get_current_step_index(); |
| 72 |
$total_steps = $params['total_steps'] ?? self::DEFAULT_TOTAL_STEPS; |
| 73 |
$next_index = $step_index + 1; |
| 74 |
|
| 75 |
if ( $next_index < $total_steps ) { |
| 76 |
$progress->set_current_step_index( $next_index ); |
| 77 |
$progress->set_current_step_id( null ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if ( isset( $params['start'] ) && $params['start'] ) { |
| 82 |
$progress->set_started_at( current_time( 'timestamp' ) ); |
| 83 |
$progress->set_exit_type( null ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( isset( $params['complete'] ) && $params['complete'] ) { |
| 87 |
$progress->set_exit_type( 'user_exit' ); |
| 88 |
update_option( Module::ONBOARDING_OPTION, Module::VERSION ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( isset( $params['user_exit'] ) && $params['user_exit'] ) { |
| 92 |
$progress->set_exit_type( 'user_exit' ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( isset( $params['starter_dismissed'] ) && $params['starter_dismissed'] ) { |
| 96 |
$progress->set_starter_dismissed( true ); |
| 97 |
} |
| 98 |
|
| 99 |
$progress->set_last_active_timestamp( current_time( 'timestamp' ) ); |
| 100 |
|
| 101 |
return $this->save_progress( $progress ); |
| 102 |
} |
| 103 |
|
| 104 |
public function get_choices(): User_Choices { |
| 105 |
$data = get_option( self::CHOICES_OPTION_KEY, [] ); |
| 106 |
|
| 107 |
return User_Choices::from_array( $data ); |
| 108 |
} |
| 109 |
|
| 110 |
public function save_choices( User_Choices $choices ): User_Choices { |
| 111 |
update_option( self::CHOICES_OPTION_KEY, $choices->to_array() ); |
| 112 |
|
| 113 |
return $choices; |
| 114 |
} |
| 115 |
|
| 116 |
public function update_choices( array $params ): User_Choices { |
| 117 |
$choices = $this->get_choices(); |
| 118 |
|
| 119 |
if ( isset( $params['building_for'] ) ) { |
| 120 |
$choices->set_building_for( $params['building_for'] ); |
| 121 |
} |
| 122 |
|
| 123 |
if ( isset( $params['site_about'] ) ) { |
| 124 |
$choices->set_site_about( (array) $params['site_about'] ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( isset( $params['experience_level'] ) ) { |
| 128 |
$choices->set_experience_level( $params['experience_level'] ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( isset( $params['theme_selection'] ) ) { |
| 132 |
$choices->set_theme_selection( $params['theme_selection'] ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( isset( $params['site_features'] ) ) { |
| 136 |
$choices->set_site_features( (array) $params['site_features'] ); |
| 137 |
} |
| 138 |
|
| 139 |
return $this->save_choices( $choices ); |
| 140 |
} |
| 141 |
|
| 142 |
public function reset(): void { |
| 143 |
delete_option( self::PROGRESS_OPTION_KEY ); |
| 144 |
delete_option( self::CHOICES_OPTION_KEY ); |
| 145 |
} |
| 146 |
|
| 147 |
private function __construct() {} |
| 148 |
} |
| 149 |
|