PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.3
Elementor Website Builder – more than just a page builder v3.30.3
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.30.3, at modules/checklist/module.php

247 lines 7.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
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit; // Exit if accessed directly.
20 }
21
22 class Module extends BaseModule implements Checklist_Module_Interface {
23 const DB_OPTION_KEY = 'elementor_checklist';
24 const VISIBILITY_SWITCH_ID = 'show_launchpad_checklist';
25 const FIRST_CLOSED_CHECKLIST_IN_EDITOR = 'first_closed_checklist_in_editor';
26 const LAST_OPENED_TIMESTAMP = 'last_opened_timestamp';
27 const SHOULD_OPEN_IN_EDITOR = 'should_open_in_editor';
28 const IS_POPUP_MINIMIZED_KEY = 'is_popup_minimized';
29
30 private Steps_Manager $steps_manager;
31 private Wordpress_Adapter_Interface $wordpress_adapter;
32 private Elementor_Adapter_Interface $elementor_adapter;
33 private Elementor_Counter_Adapter_Interface $counter_adapter;
34 private $user_progress = null;
35
36 /**
37 * @param ?Wordpress_Adapter_Interface $wordpress_adapter
38 * @param ?Elementor_Adapter_Interface $elementor_adapter
39 *
40 * @return void
41 */
42 public function __construct(
43 ?Wordpress_Adapter_Interface $wordpress_adapter = null,
44 ?Elementor_Adapter_Interface $elementor_adapter = null
45 ) {
46 $this->wordpress_adapter = $wordpress_adapter ?? Isolation_Manager::get_adapter( Wordpress_Adapter::class );
47 $this->elementor_adapter = $elementor_adapter ?? Isolation_Manager::get_adapter( Elementor_Adapter::class );
48
49 parent::__construct();
50 $this->init_user_progress();
51
52 Plugin::$instance->data_manager_v2->register_controller( new Controller() );
53 $this->user_progress = $this->user_progress ?? $this->get_user_progress_from_db();
54 $this->handle_checklist_visibility_with_kit();
55 $this->steps_manager = new Steps_Manager( $this );
56
57 if ( ! current_user_can( 'manage_options' ) ) {
58 return;
59 }
60
61 $this->enqueue_editor_scripts();
62 }
63
64 /**
65 * Get the module name.
66 *
67 * @return string
68 */
69 public function get_name(): string {
70 return 'e-checklist';
71 }
72
73 /**
74 * Gets user's progress from db
75 *
76 * @return array {
77 * @type bool $is_hidden
78 * @type int $last_opened_timestamp
79 * @type array $steps {
80 * @type string $step_id => {
81 * @type bool $is_marked_completed
82 * @type bool $is_absolute_competed
83 * @type bool $is_immutable_completed
84 * }
85 * }
86 * }
87 */
88 public function get_user_progress_from_db(): array {
89 $db_progress = json_decode( $this->wordpress_adapter->get_option( self::DB_OPTION_KEY ), true );
90 $db_progress = is_array( $db_progress ) ? $db_progress : [];
91
92 $progress = array_merge( $this->get_default_user_progress(), $db_progress );
93
94 $editor_visit_count = $this->elementor_adapter->get_count( Elementor_Counter::EDITOR_COUNTER_KEY );
95 $progress[ self::SHOULD_OPEN_IN_EDITOR ] = 2 === $editor_visit_count && ! $progress[ self::LAST_OPENED_TIMESTAMP ];
96
97 return $progress;
98 }
99
100 /**
101 * Using the step's ID, get the progress of the step should it exist
102 *
103 * @param $step_id
104 *
105 * @return null|array {
106 * @type bool $is_marked_completed
107 * @type bool $is_completed
108 * }
109 */
110 public function get_step_progress( $step_id ): ?array {
111 return $this->user_progress['steps'][ $step_id ] ?? null;
112 }
113
114 /**
115 * Update the progress of a step
116 *
117 * @param $step_id
118 * @param $step_progress
119 *
120 * @return void
121 */
122 public function set_step_progress( $step_id, $step_progress ): void {
123 $this->user_progress['steps'][ $step_id ] = $step_progress;
124 $this->update_user_progress_in_db();
125 }
126
127 public function update_user_progress( $new_data ): void {
128 $allowed_properties = [
129 self::FIRST_CLOSED_CHECKLIST_IN_EDITOR => $new_data[ self::FIRST_CLOSED_CHECKLIST_IN_EDITOR ] ?? null,
130 self::LAST_OPENED_TIMESTAMP => $new_data[ self::LAST_OPENED_TIMESTAMP ] ?? null,
131 self::IS_POPUP_MINIMIZED_KEY => $new_data[ self::IS_POPUP_MINIMIZED_KEY ] ?? null,
132 ];
133
134 foreach ( $allowed_properties as $key => $value ) {
135 if ( null !== $value ) {
136 $this->user_progress[ $key ] = $this->get_formatted_value( $key, $value );
137 }
138 }
139
140 $this->update_user_progress_in_db();
141
142 if ( isset( $new_data[ Elementor_Counter::EDITOR_COUNTER_KEY ] ) ) {
143 $this->elementor_adapter->set_count( Elementor_Counter::EDITOR_COUNTER_KEY, $new_data[ Elementor_Counter::EDITOR_COUNTER_KEY ] );
144 }
145 }
146
147 /**
148 * @return Steps_Manager
149 */
150 public function get_steps_manager(): Steps_Manager {
151 return $this->steps_manager;
152 }
153
154 /**
155 * @return Wordpress_Adapter
156 */
157 public function get_wordpress_adapter(): Wordpress_Adapter {
158 return $this->wordpress_adapter;
159 }
160
161 /**
162 * @return Elementor_Adapter
163 */
164 public function get_elementor_adapter(): Elementor_Adapter {
165 return $this->elementor_adapter;
166 }
167
168 public function enqueue_editor_scripts(): void {
169 add_action( 'elementor/editor/before_enqueue_scripts', function () {
170 $min_suffix = Utils::is_script_debug() ? '' : '.min';
171
172 wp_enqueue_script(
173 $this->get_name(),
174 ELEMENTOR_ASSETS_URL . 'js/checklist' . $min_suffix . '.js',
175 [
176 'react',
177 'react-dom',
178 'elementor-common',
179 'elementor-v2-ui',
180 'elementor-v2-icons',
181 'elementor-v2-editor-app-bar',
182 'elementor-web-cli',
183 ],
184 ELEMENTOR_VERSION,
185 true
186 );
187
188 wp_set_script_translations( $this->get_name(), 'elementor' );
189 } );
190 }
191
192 public function is_preference_switch_on(): bool {
193 if ( $this->should_switch_preferences_off() ) {
194 return false;
195 }
196
197 $user_preferences = $this->wordpress_adapter->get_user_preferences( self::VISIBILITY_SWITCH_ID );
198
199 return 'yes' === $user_preferences || $this->wordpress_adapter->is_new_installation();
200 }
201
202 public function should_switch_preferences_off(): bool {
203 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 );
204 }
205
206 private function init_user_progress(): void {
207 $default_settings = $this->get_default_user_progress();
208
209 $this->wordpress_adapter->add_option( self::DB_OPTION_KEY, wp_json_encode( $default_settings ) );
210 }
211
212 private function get_default_user_progress(): array {
213 return [
214 self::LAST_OPENED_TIMESTAMP => null,
215 self::FIRST_CLOSED_CHECKLIST_IN_EDITOR => false,
216 self::IS_POPUP_MINIMIZED_KEY => false,
217 'steps' => [],
218 ];
219 }
220
221 private function update_user_progress_in_db(): void {
222 $this->wordpress_adapter->update_option( self::DB_OPTION_KEY, wp_json_encode( $this->user_progress ) );
223 }
224
225 private function get_formatted_value( $key, $value ) {
226 if ( self::LAST_OPENED_TIMESTAMP === $key ) {
227 return $value ? time() : null;
228 }
229
230 return $value;
231 }
232
233 private function handle_checklist_visibility_with_kit() {
234 if ( ! $this->should_switch_preferences_off() ) {
235 return;
236 }
237
238 add_action( 'elementor/editor/init', function () {
239 $this->wordpress_adapter->set_user_preferences( self::VISIBILITY_SWITCH_ID, '' );
240 }, 11 );
241 }
242
243 public static function should_display_checklist_toggle_control(): bool {
244 return current_user_can( 'manage_options' );
245 }
246 }
247