1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
<?php
namespace Elementor\Modules\Checklist;
use Elementor\Modules\Checklist\Steps\Assign_Homepage; use Elementor\Modules\Checklist\Steps\Create_Pages; use Elementor\Modules\Checklist\Steps\Setup_Header; use Elementor\Modules\Checklist\Steps\Add_Logo; use Elementor\Modules\Checklist\Steps\Step_Base; use Elementor\Modules\Checklist\Steps\Set_Fonts_And_Colors;
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }
class Steps_Manager { /** @var Step_Base[] $step_instances */ private array $step_instances = [];
private static array $step_ids = [ Add_Logo::STEP_ID, Set_Fonts_And_Colors::STEP_ID, Create_Pages::STEP_ID, Setup_Header::STEP_ID, Assign_Homepage::STEP_ID, ];
private Checklist_Module_Interface $module;
public function __construct( Checklist_Module_Interface $module ) { $this->module = $module; $this->register_steps();
add_action( 'elementor/init', function() { $this->filter_steps(); } ); }
/** * Gets formatted and ordered array of step ( step data, is_marked_completed and is_completed ) * * @return array */ public function get_steps_for_frontend() : array { $formatted_steps = [];
foreach ( self::$step_ids as $step_id ) { $instance = $this->step_instances[ $step_id ]; $instance->maybe_immutably_mark_as_completed();
$step = [ Step_Base::MARKED_AS_COMPLETED_KEY => $instance->is_marked_as_completed(), Step_Base::IMMUTABLE_COMPLETION_KEY => $instance->is_immutable_completed(), Step_Base::ABSOLUTE_COMPLETION_KEY => $instance->is_absolute_completed(), 'config' => $this->get_step_config( $step_id ), ];
$formatted_steps[] = $step; }
return $formatted_steps; }
public function update_step( string $step_id, array $data ) : void { $step = $this->get_step_by_id( $step_id );
if ( ! $step ) { return; }
$step->update_step( $data ); }
/** * Marks a step as completed, returns true if the step was found and marked or false otherwise * * @param string $step_id * * @return void */ public function mark_step_as_completed( string $step_id ) : void { $this->update_step( $step_id, [ Step_Base::MARKED_AS_COMPLETED_KEY => true ] ); }
/** * Unmarks a step as completed, returns true if the step was found and unmarked or false otherwise * * @param string $step_id * * @return void */ public function unmark_step_as_completed( string $step_id ) : void { $this->update_step( $step_id, [ Step_Base::MARKED_AS_COMPLETED_KEY => false ] ); }
/** * Maybe marks a step as completed (depending on if source allows it), returns true if the step was found and marked or false otherwise * * @param $step_id * * @return void */ public function maybe_set_step_as_immutable_completed( string $step_id ) : void { $step = $this->get_step_by_id( $step_id );
if ( ! $step ) { return; }
$step->maybe_immutably_mark_as_completed(); }
public function get_step_by_id( string $step_id ) : ?Step_Base { return $this->step_instances[ $step_id ] ?? null; }
/** * @return array */ public function get_step_config( $step_id ) : array { $step_instance = $this->step_instances[ $step_id ];
return $step_instance ? [ 'id' => $step_instance->get_id(), 'title' => $step_instance->get_title(), 'description' => $step_instance->get_description(), 'learn_more_text' => $step_instance->get_learn_more_text(), 'learn_more_url' => $step_instance->get_learn_more_url(), Step_Base::IS_COMPLETION_IMMUTABLE => $step_instance->get_is_completion_immutable(), 'cta_text' => $step_instance->get_cta_text(), 'cta_url' => $step_instance->get_cta_url(), 'image_src' => $step_instance->get_image_src(), 'promotion_data' => $step_instance->get_promotion_data(), ] : []; }
/** * Getting the step instances array based on source's order * * @return void */ private function register_steps() : void { foreach ( self::$step_ids as $step_id ) { $step_instance = $this->get_step_instance( $step_id );
if ( $step_instance && ! isset( $this->step_instances[ $step_id ] ) ) { $this->step_instances[ $step_id ] = $step_instance; } } }
/** * Returns the steps config from source * * @return array */ public static function get_step_ids() : array { return self::$step_ids; }
/** * Using step data->id, instantiates and returns the step class or null if the class does not exist * * @param $step_data * * @return Step_Base|null */ private function get_step_instance( string $step_id ) : ?Step_Base { $class_name = '\\Elementor\\Modules\\Checklist\\Steps\\' . $step_id;
if ( ! class_exists( $class_name ) ) { return null; }
/** @var Step_Base $step */ return new $class_name( $this->module, $this->module->get_wordpress_adapter(), $this->module->get_elementor_adapter() ); }
private function filter_steps() { $step_ids = []; $filtered_steps = apply_filters( 'elementor/checklist/steps', $this->step_instances );
foreach ( $filtered_steps as $step_id => $step_instance ) { if ( ! $step_instance instanceof Step_Base ) { continue; }
if ( ! $step_instance->is_visible() ) { continue; }
$this->step_instances[ $step_id ] = $step_instance; $step_ids[] = $step_id; }
self::$step_ids = $step_ids; } }
|