| 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\Core\Isolation\Wordpress_Adapter; |
| 8 |
use Elementor\Core\Isolation\Wordpress_Adapter_Interface; |
| 9 |
use Elementor\Plugin; |
| 10 |
use Elementor\Utils; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
class Module extends BaseModule implements Checklist_Module_Interface { |
| 17 |
const EXPERIMENT_ID = 'launchpad-checklist'; |
| 18 |
const DB_OPTION_KEY = 'elementor_checklist'; |
| 19 |
|
| 20 |
private $user_progress = null; |
| 21 |
private Steps_Manager $steps_manager; |
| 22 |
private Wordpress_Adapter_Interface $wordpress_adapter; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param ?Wordpress_Adapter_Interface $wordpress_adapter |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function __construct( ?Wordpress_Adapter_Interface $wordpress_adapter = null ) { |
| 30 |
$this->wordpress_adapter = $wordpress_adapter ?? new Wordpress_Adapter(); |
| 31 |
parent::__construct(); |
| 32 |
|
| 33 |
$this->register_experiment(); |
| 34 |
|
| 35 |
if ( ! $this->is_experiment_active() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$this->init_user_progress(); |
| 40 |
$this->user_progress = $this->user_progress ?? $this->get_user_progress_from_db(); |
| 41 |
$this->steps_manager = new Steps_Manager( $this ); |
| 42 |
$this->enqueue_editor_scripts(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get the module name. |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function get_name() : string { |
| 51 |
return 'e-checklist'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Checks if the experiment is active |
| 56 |
* |
| 57 |
* @return bool |
| 58 |
*/ |
| 59 |
public function is_experiment_active() : bool { |
| 60 |
return Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_ID ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Gets user's progress from db |
| 65 |
* |
| 66 |
* @return array { |
| 67 |
* @type bool $is_hidden |
| 68 |
* @type int $last_opened_timestamp |
| 69 |
* @type array $steps { |
| 70 |
* @type string $step_id => { |
| 71 |
* @type bool $is_marked_completed |
| 72 |
* @type bool $is_completed |
| 73 |
* } |
| 74 |
* } |
| 75 |
* } |
| 76 |
*/ |
| 77 |
public function get_user_progress_from_db() : array { |
| 78 |
return json_decode( $this->wordpress_adapter->get_option( self::DB_OPTION_KEY ), true ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Using the step's ID, get the progress of the step should it exist |
| 83 |
* |
| 84 |
* @param $step_id |
| 85 |
* |
| 86 |
* @return null|array { |
| 87 |
* @type bool $is_marked_completed |
| 88 |
* @type bool $is_completed |
| 89 |
* } |
| 90 |
*/ |
| 91 |
public function get_step_progress( $step_id ) : ?array { |
| 92 |
return $this->user_progress['steps'][ $step_id ] ?? null; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Update the progress of a step |
| 97 |
* |
| 98 |
* @param $step_id |
| 99 |
* @param $step_progress |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function set_step_progress( $step_id, $step_progress ) : void { |
| 104 |
$this->user_progress['steps'][ $step_id ] = $step_progress; |
| 105 |
$this->update_user_progress_in_db(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @return Steps_Manager |
| 110 |
*/ |
| 111 |
public function get_steps_manager() : Steps_Manager { |
| 112 |
return $this->steps_manager; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @return Wordpress_Adapter |
| 117 |
*/ |
| 118 |
public function get_wordpress_adapter() : Wordpress_Adapter { |
| 119 |
return $this->wordpress_adapter; |
| 120 |
} |
| 121 |
|
| 122 |
public function enqueue_editor_scripts() : void { |
| 123 |
add_action( 'elementor/editor/before_enqueue_scripts', function () { |
| 124 |
$min_suffix = Utils::is_script_debug() ? '' : '.min'; |
| 125 |
|
| 126 |
wp_enqueue_script( |
| 127 |
$this->get_name(), |
| 128 |
ELEMENTOR_ASSETS_URL . 'js/checklist' . $min_suffix . '.js', |
| 129 |
[ |
| 130 |
'react', |
| 131 |
'react-dom', |
| 132 |
'elementor-common', |
| 133 |
'elementor-v2-ui', |
| 134 |
'elementor-v2-icons', |
| 135 |
'elementor-v2-editor-app-bar', |
| 136 |
'elementor-web-cli', |
| 137 |
], |
| 138 |
ELEMENTOR_VERSION, |
| 139 |
true |
| 140 |
); |
| 141 |
|
| 142 |
wp_set_script_translations( $this->get_name(), 'elementor' ); |
| 143 |
} ); |
| 144 |
} |
| 145 |
|
| 146 |
private function register_experiment() : void { |
| 147 |
Plugin::$instance->experiments->add_feature( [ |
| 148 |
'name' => self::EXPERIMENT_ID, |
| 149 |
'title' => esc_html__( 'Launchpad Checklist', 'elementor' ), |
| 150 |
'description' => esc_html__( 'Launchpad Checklist feature to boost productivity and deliver your site faster', 'elementor' ), |
| 151 |
'release_status' => Manager::RELEASE_STATUS_ALPHA, |
| 152 |
'hidden' => true, |
| 153 |
] ); |
| 154 |
} |
| 155 |
|
| 156 |
private function init_user_progress() : void { |
| 157 |
$default_settings = [ |
| 158 |
'is_hidden' => false, |
| 159 |
'last_opened_timestamp' => time(), |
| 160 |
'steps' => [], |
| 161 |
]; |
| 162 |
|
| 163 |
$this->wordpress_adapter->add_option( self::DB_OPTION_KEY, wp_json_encode( $default_settings ) ); |
| 164 |
} |
| 165 |
|
| 166 |
private function update_user_progress_in_db() : void { |
| 167 |
$this->wordpress_adapter->update_option( self::DB_OPTION_KEY, wp_json_encode( $this->user_progress ) ); |
| 168 |
} |
| 169 |
} |
| 170 |
|