PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.4
Elementor Website Builder – more than just a page builder v3.28.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / checklist / module.php

module.php in Elementor Website Builder – more than just a page builder 3.28.4, at modules/checklist/module.php

278 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Checklist;
4
5 use Elementor\Core\Base\Module as BaseModule;
6 use Elementor\Core\Experiments\Manager;
7 use Elementor\Modules\ElementorCounter\Module as Elementor_Counter;
8 use Elementor\Core\Isolation\Wordpress_Adapter;
9 use Elementor\Core\Isolation\Wordpress_Adapter_Interface;
10 use Elementor\Core\Isolation\Elementor_Adapter;
11 use Elementor\Core\Isolation\Elementor_Adapter_Interface;
12 use Elementor\Core\Isolation\Elementor_Counter_Adapter_Interface;
13 use Elementor\Plugin;
14 use Elementor\Utils;
15 use Elementor\Modules\Checklist\Data\Controller;
16 use Elementor\Core\Utils\Isolation_Manager;
17 use Elementor\Modules\EditorAppBar\Module as AppBarModule;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit; // Exit if accessed directly.
21 }
22
23 class Module extends BaseModule implements Checklist_Module_Interface {
24 const EXPERIMENT_ID = 'launchpad-checklist';
25 const DB_OPTION_KEY = 'elementor_checklist';
26 const VISIBILITY_SWITCH_ID = 'show_launchpad_checklist';
27 const FIRST_CLOSED_CHECKLIST_IN_EDITOR = 'first_closed_checklist_in_editor';
28 const LAST_OPENED_TIMESTAMP = 'last_opened_timestamp';
29 const SHOULD_OPEN_IN_EDITOR = 'should_open_in_editor';
30 const IS_POPUP_MINIMIZED_KEY = 'is_popup_minimized';
31
32 private Steps_Manager $steps_manager;
33 private Wordpress_Adapter_Interface $wordpress_adapter;
34 private Elementor_Adapter_Interface $elementor_adapter;
35 private Elementor_Counter_Adapter_Interface $counter_adapter;
36 private $user_progress = null;
37
38 /**
39 * @param ?Wordpress_Adapter_Interface $wordpress_adapter
40 * @param ?Elementor_Adapter_Interface $elementor_adapter
41 *
42 * @return void
43 */
44 public function __construct(
45 ?Wordpress_Adapter_Interface $wordpress_adapter = null,
46 ?Elementor_Adapter_Interface $elementor_adapter = null
47 ) {
48 $this->wordpress_adapter = $wordpress_adapter ?? Isolation_Manager::get_adapter( Wordpress_Adapter::class );
49 $this->elementor_adapter = $elementor_adapter ?? Isolation_Manager::get_adapter( Elementor_Adapter::class );
50
51 parent::__construct();
52 $this->init_user_progress();
53
54 if ( ! $this->is_experiment_active() ) {
55 return;
56 }
57
58 Plugin::$instance->data_manager_v2->register_controller( new Controller() );
59 $this->user_progress = $this->user_progress ?? $this->get_user_progress_from_db();
60 $this->handle_checklist_visibility_with_kit();
61 $this->steps_manager = new Steps_Manager( $this );
62
63 if ( ! current_user_can( 'manage_options' ) ) {
64 return;
65 }
66
67 $this->enqueue_editor_scripts();
68 }
69
70 /**
71 * Get the module name.
72 *
73 * @return string
74 */
75 public function get_name(): string {
76 return 'e-checklist';
77 }
78
79 /**
80 * Checks if the experiment is active
81 *
82 * @return bool
83 */
84 public function is_experiment_active(): bool {
85 return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_ID );
86 }
87
88 /**
89 * Gets user's progress from db
90 *
91 * @return array {
92 * @type bool $is_hidden
93 * @type int $last_opened_timestamp
94 * @type array $steps {
95 * @type string $step_id => {
96 * @type bool $is_marked_completed
97 * @type bool $is_absolute_competed
98 * @type bool $is_immutable_completed
99 * }
100 * }
101 * }
102 */
103 public function get_user_progress_from_db(): array {
104 $db_progress = json_decode( $this->wordpress_adapter->get_option( self::DB_OPTION_KEY ), true );
105 $db_progress = is_array( $db_progress ) ? $db_progress : [];
106
107 $progress = array_merge( $this->get_default_user_progress(), $db_progress );
108
109 $editor_visit_count = $this->elementor_adapter->get_count( Elementor_Counter::EDITOR_COUNTER_KEY );
110 $progress[ self::SHOULD_OPEN_IN_EDITOR ] = 2 === $editor_visit_count && ! $progress[ self::LAST_OPENED_TIMESTAMP ];
111
112 return $progress;
113 }
114
115 /**
116 * Using the step's ID, get the progress of the step should it exist
117 *
118 * @param $step_id
119 *
120 * @return null|array {
121 * @type bool $is_marked_completed
122 * @type bool $is_completed
123 * }
124 */
125 public function get_step_progress( $step_id ): ?array {
126 return $this->user_progress['steps'][ $step_id ] ?? null;
127 }
128
129 /**
130 * Update the progress of a step
131 *
132 * @param $step_id
133 * @param $step_progress
134 *
135 * @return void
136 */
137 public function set_step_progress( $step_id, $step_progress ): void {
138 $this->user_progress['steps'][ $step_id ] = $step_progress;
139 $this->update_user_progress_in_db();
140 }
141
142 public function update_user_progress( $new_data ): void {
143 $allowed_properties = [
144 self::FIRST_CLOSED_CHECKLIST_IN_EDITOR => $new_data[ self::FIRST_CLOSED_CHECKLIST_IN_EDITOR ] ?? null,
145 self::LAST_OPENED_TIMESTAMP => $new_data[ self::LAST_OPENED_TIMESTAMP ] ?? null,
146 self::IS_POPUP_MINIMIZED_KEY => $new_data[ self::IS_POPUP_MINIMIZED_KEY ] ?? null,
147 ];
148
149 foreach ( $allowed_properties as $key => $value ) {
150 if ( null !== $value ) {
151 $this->user_progress[ $key ] = $this->get_formatted_value( $key, $value );
152 }
153 }
154
155 $this->update_user_progress_in_db();
156
157 if ( isset( $new_data[ Elementor_Counter::EDITOR_COUNTER_KEY ] ) ) {
158 $this->elementor_adapter->set_count( Elementor_Counter::EDITOR_COUNTER_KEY, $new_data[ Elementor_Counter::EDITOR_COUNTER_KEY ] );
159 }
160 }
161
162 /**
163 * @return Steps_Manager
164 */
165 public function get_steps_manager(): Steps_Manager {
166 return $this->steps_manager;
167 }
168
169 /**
170 * @return Wordpress_Adapter
171 */
172 public function get_wordpress_adapter(): Wordpress_Adapter {
173 return $this->wordpress_adapter;
174 }
175
176 /**
177 * @return Elementor_Adapter
178 */
179 public function get_elementor_adapter(): Elementor_Adapter {
180 return $this->elementor_adapter;
181 }
182
183 public function enqueue_editor_scripts(): void {
184 add_action( 'elementor/editor/before_enqueue_scripts', function () {
185 $min_suffix = Utils::is_script_debug() ? '' : '.min';
186
187 wp_enqueue_script(
188 $this->get_name(),
189 ELEMENTOR_ASSETS_URL . 'js/checklist' . $min_suffix . '.js',
190 [
191 'react',
192 'react-dom',
193 'elementor-common',
194 'elementor-v2-ui',
195 'elementor-v2-icons',
196 'elementor-v2-editor-app-bar',
197 'elementor-web-cli',
198 ],
199 ELEMENTOR_VERSION,
200 true
201 );
202
203 wp_set_script_translations( $this->get_name(), 'elementor' );
204 } );
205 }
206
207 public function is_preference_switch_on(): bool {
208 if ( $this->should_switch_preferences_off() ) {
209 return false;
210 }
211
212 $user_preferences = $this->wordpress_adapter->get_user_preferences( self::VISIBILITY_SWITCH_ID );
213
214 return 'yes' === $user_preferences || $this->wordpress_adapter->is_new_installation();
215 }
216
217 public function should_switch_preferences_off(): bool {
218 return ! $this->elementor_adapter->is_active_kit_default() && ! $this->user_progress[ self::LAST_OPENED_TIMESTAMP ] && ! $this->elementor_adapter->get_count( Elementor_Counter::EDITOR_COUNTER_KEY );
219 }
220
221 public static function get_experimental_data(): array {
222 return [
223 'name' => self::EXPERIMENT_ID,
224 'title' => esc_html__( 'Launchpad Checklist', 'elementor' ),
225 'description' => esc_html__( 'Launchpad Checklist feature to boost productivity and deliver your site faster', 'elementor' ),
226 'release_status' => Manager::RELEASE_STATUS_STABLE,
227 'hidden' => true,
228 'new_site' => [
229 'default_active' => true,
230 'minimum_installation_version' => '3.25.0',
231 ],
232 ];
233 }
234
235 private function init_user_progress(): void {
236 $default_settings = $this->get_default_user_progress();
237
238 $this->wordpress_adapter->add_option( self::DB_OPTION_KEY, wp_json_encode( $default_settings ) );
239 }
240
241 private function get_default_user_progress(): array {
242 return [
243 self::LAST_OPENED_TIMESTAMP => null,
244 self::FIRST_CLOSED_CHECKLIST_IN_EDITOR => false,
245 self::IS_POPUP_MINIMIZED_KEY => false,
246 'steps' => [],
247 ];
248 }
249
250 private function update_user_progress_in_db(): void {
251 $this->wordpress_adapter->update_option( self::DB_OPTION_KEY, wp_json_encode( $this->user_progress ) );
252 }
253
254 private function get_formatted_value( $key, $value ) {
255 if ( self::LAST_OPENED_TIMESTAMP === $key ) {
256 return $value ? time() : null;
257 }
258
259 return $value;
260 }
261
262 private function handle_checklist_visibility_with_kit() {
263 if ( ! $this->should_switch_preferences_off() ) {
264 return;
265 }
266
267 add_action( 'elementor/editor/init', function () {
268 $this->wordpress_adapter->set_user_preferences( self::VISIBILITY_SWITCH_ID, '' );
269 }, 11 );
270 }
271
272 public static function should_display_checklist_toggle_control(): bool {
273 return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_ID ) &&
274 Plugin::$instance->experiments->is_feature_active( AppBarModule::EXPERIMENT_NAME ) &&
275 current_user_can( 'manage_options' );
276 }
277 }
278