module.php
5 months ago
opt-in-page.php
4 months ago
panel-chip.php
10 months ago
welcome-screen.php
8 months ago
welcome-screen.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor\Modules\AtomicOptIn; |
| 4 | |
| 5 | use Elementor\Core\Isolation\Elementor_Adapter; |
| 6 | use Elementor\Core\Isolation\Elementor_Adapter_Interface; |
| 7 | use Elementor\Modules\ElementorCounter\Module as Elementor_Counter; |
| 8 | use Elementor\Utils; |
| 9 | |
| 10 | class WelcomeScreen { |
| 11 | private Elementor_Adapter_Interface $elementor_adapter; |
| 12 | |
| 13 | public function __construct() { |
| 14 | $this->elementor_adapter = new Elementor_Adapter(); |
| 15 | } |
| 16 | |
| 17 | public function init() { |
| 18 | add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'maybe_enqueue_welcome_popover' ] ); |
| 19 | } |
| 20 | |
| 21 | public function maybe_enqueue_welcome_popover(): void { |
| 22 | if ( $this->is_first_or_second_editor_visit() ) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | if ( $this->has_welcome_popover_been_displayed() ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | $this->enqueue_scripts(); |
| 31 | $this->set_welcome_popover_as_displayed(); |
| 32 | } |
| 33 | |
| 34 | private function is_first_or_second_editor_visit(): bool { |
| 35 | if ( ! $this->elementor_adapter ) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | $editor_visit_count = $this->elementor_adapter->get_count( Elementor_Counter::EDITOR_COUNTER_KEY ); |
| 40 | return $editor_visit_count < 3; |
| 41 | } |
| 42 | |
| 43 | private function has_welcome_popover_been_displayed(): bool { |
| 44 | return get_user_meta( $this->get_current_user_id(), Module::WELCOME_POPOVER_DISPLAYED_OPTION, true ); |
| 45 | } |
| 46 | |
| 47 | private function set_welcome_popover_as_displayed(): void { |
| 48 | update_user_meta( $this->get_current_user_id(), Module::WELCOME_POPOVER_DISPLAYED_OPTION, true ); |
| 49 | } |
| 50 | |
| 51 | private function enqueue_scripts() { |
| 52 | $min_suffix = Utils::is_script_debug() ? '' : '.min'; |
| 53 | |
| 54 | wp_enqueue_script( |
| 55 | Module::MODULE_NAME . '-welcome', |
| 56 | ELEMENTOR_ASSETS_URL . 'js/editor-v4-welcome-opt-in' . $min_suffix . '.js', |
| 57 | [ |
| 58 | 'react', |
| 59 | 'react-dom', |
| 60 | 'elementor-common', |
| 61 | 'elementor-v2-ui', |
| 62 | ], |
| 63 | ELEMENTOR_VERSION, |
| 64 | true |
| 65 | ); |
| 66 | |
| 67 | wp_set_script_translations( Module::MODULE_NAME . '-welcome', 'elementor' ); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | private function get_current_user_id(): int { |
| 72 | $current_user = wp_get_current_user(); |
| 73 | return $current_user->ID ?? 0; |
| 74 | } |
| 75 | } |
| 76 |