PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
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 4.3.0-beta1, at modules/checklist/module.php

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