data
1 year ago
steps
1 year ago
checklist-module-interface.php
1 year ago
module.php
1 year ago
steps-manager.php
1 year ago
module.php
279 lines
| 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->register_experiment(); |
| 53 | $this->init_user_progress(); |
| 54 | |
| 55 | if ( ! $this->is_experiment_active() ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | Plugin::$instance->data_manager_v2->register_controller( new Controller() ); |
| 60 | $this->user_progress = $this->user_progress ?? $this->get_user_progress_from_db(); |
| 61 | $this->handle_checklist_visibility_with_kit(); |
| 62 | $this->steps_manager = new Steps_Manager( $this ); |
| 63 | |
| 64 | if ( ! current_user_can( 'manage_options' ) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $this->enqueue_editor_scripts(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the module name. |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public function get_name() : string { |
| 77 | return 'e-checklist'; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks if the experiment is active |
| 82 | * |
| 83 | * @return bool |
| 84 | */ |
| 85 | public function is_experiment_active() : bool { |
| 86 | return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_ID ); |
| 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 register_experiment() : void { |
| 223 | Plugin::$instance->experiments->add_feature( [ |
| 224 | 'name' => self::EXPERIMENT_ID, |
| 225 | 'title' => esc_html__( 'Launchpad Checklist', 'elementor' ), |
| 226 | 'description' => esc_html__( 'Launchpad Checklist feature to boost productivity and deliver your site faster', 'elementor' ), |
| 227 | 'release_status' => Manager::RELEASE_STATUS_ALPHA, |
| 228 | 'hidden' => true, |
| 229 | 'new_site' => [ |
| 230 | 'default_active' => true, |
| 231 | 'minimum_installation_version' => '3.25.0', |
| 232 | ], |
| 233 | ] ); |
| 234 | } |
| 235 | |
| 236 | private function init_user_progress() : void { |
| 237 | $default_settings = $this->get_default_user_progress(); |
| 238 | |
| 239 | $this->wordpress_adapter->add_option( self::DB_OPTION_KEY, wp_json_encode( $default_settings ) ); |
| 240 | } |
| 241 | |
| 242 | private function get_default_user_progress() : array { |
| 243 | return [ |
| 244 | self::LAST_OPENED_TIMESTAMP => null, |
| 245 | self::FIRST_CLOSED_CHECKLIST_IN_EDITOR => false, |
| 246 | self::IS_POPUP_MINIMIZED_KEY => false, |
| 247 | 'steps' => [], |
| 248 | ]; |
| 249 | } |
| 250 | |
| 251 | private function update_user_progress_in_db() : void { |
| 252 | $this->wordpress_adapter->update_option( self::DB_OPTION_KEY, wp_json_encode( $this->user_progress ) ); |
| 253 | } |
| 254 | |
| 255 | private function get_formatted_value( $key, $value ) { |
| 256 | if ( self::LAST_OPENED_TIMESTAMP === $key ) { |
| 257 | return $value ? time() : null; |
| 258 | } |
| 259 | |
| 260 | return $value; |
| 261 | } |
| 262 | |
| 263 | private function handle_checklist_visibility_with_kit() { |
| 264 | if ( ! $this->should_switch_preferences_off() ) { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | add_action( 'elementor/editor/init', function () { |
| 269 | $this->wordpress_adapter->set_user_preferences( self::VISIBILITY_SWITCH_ID, '' ); |
| 270 | }, 11 ); |
| 271 | } |
| 272 | |
| 273 | public static function should_display_checklist_toggle_control() : bool { |
| 274 | return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_ID ) && |
| 275 | Plugin::$instance->experiments->is_feature_active( AppBarModule::EXPERIMENT_NAME ) && |
| 276 | current_user_can( 'manage_options' ); |
| 277 | } |
| 278 | } |
| 279 |