| 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\Assets_Config_Provider; |
| 8 |
use Elementor\Modules\AtomicWidgets\OptIn\Opt_In as Atomic_Widgets_Opt_In; |
| 9 |
use Elementor\Modules\ElementorCounter\Module as Elementor_Counter; |
| 10 |
use Elementor\Core\Upgrade\Manager as Upgrade_Manager; |
| 11 |
use Elementor\Utils; |
| 12 |
|
| 13 |
class WelcomeScreen { |
| 14 |
const PACKAGE_NAME = 'v4-activation-modal'; |
| 15 |
|
| 16 |
private Elementor_Adapter_Interface $elementor_adapter; |
| 17 |
|
| 18 |
public function __construct() { |
| 19 |
$this->elementor_adapter = new Elementor_Adapter(); |
| 20 |
} |
| 21 |
|
| 22 |
public function init() { |
| 23 |
add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'maybe_enqueue_welcome_popover' ] ); |
| 24 |
} |
| 25 |
|
| 26 |
public function maybe_enqueue_welcome_popover(): void { |
| 27 |
if ( ! $this->was_opt_in_clicked() ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
if ( $this->is_first_or_second_editor_visit() ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
if ( $this->has_welcome_popover_been_displayed() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
if ( Upgrade_Manager::is_new_installation() ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$this->register_package(); |
| 44 |
wp_enqueue_script( 'elementor-v2-' . self::PACKAGE_NAME ); |
| 45 |
wp_set_script_translations( 'elementor-v2-' . self::PACKAGE_NAME, 'elementor' ); |
| 46 |
$this->set_welcome_popover_as_displayed(); |
| 47 |
} |
| 48 |
|
| 49 |
private function was_opt_in_clicked(): bool { |
| 50 |
return (bool) get_option( Atomic_Widgets_Opt_In::OPT_IN_CLICKED_OPTION ); |
| 51 |
} |
| 52 |
|
| 53 |
private function is_first_or_second_editor_visit(): bool { |
| 54 |
if ( ! $this->elementor_adapter ) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
$editor_visit_count = $this->elementor_adapter->get_count( Elementor_Counter::EDITOR_COUNTER_KEY ); |
| 59 |
return $editor_visit_count < 3; |
| 60 |
} |
| 61 |
|
| 62 |
private function has_welcome_popover_been_displayed(): bool { |
| 63 |
return get_user_meta( $this->get_current_user_id(), Module::WELCOME_POPOVER_DISPLAYED_OPTION, true ); |
| 64 |
} |
| 65 |
|
| 66 |
private function set_welcome_popover_as_displayed(): void { |
| 67 |
update_user_meta( $this->get_current_user_id(), Module::WELCOME_POPOVER_DISPLAYED_OPTION, true ); |
| 68 |
} |
| 69 |
|
| 70 |
private function register_package(): void { |
| 71 |
$min_suffix = Utils::is_script_debug() ? '' : '.min'; |
| 72 |
$package = self::PACKAGE_NAME; |
| 73 |
|
| 74 |
$assets_config_provider = ( new Assets_Config_Provider() ) |
| 75 |
->set_path_resolver( function ( $name ) { |
| 76 |
return ELEMENTOR_ASSETS_PATH . "js/packages/{$name}/{$name}.asset.php"; |
| 77 |
} ); |
| 78 |
|
| 79 |
$config = $assets_config_provider->load( $package )->get( $package ); |
| 80 |
|
| 81 |
if ( ! $config ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
wp_register_script( |
| 86 |
$config['handle'], |
| 87 |
ELEMENTOR_ASSETS_URL . "js/packages/{$package}/{$package}{$min_suffix}.js", |
| 88 |
$config['deps'], |
| 89 |
ELEMENTOR_VERSION, |
| 90 |
true |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
private function get_current_user_id(): int { |
| 95 |
$current_user = wp_get_current_user(); |
| 96 |
return $current_user->ID ?? 0; |
| 97 |
} |
| 98 |
} |
| 99 |
|