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