| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Core\Common\Modules\EventsManager; |
| 4 |
|
| 5 |
use Elementor\Core\Base\Module as BaseModule; |
| 6 |
use Elementor\Core\Common\Modules\Connect\Apps\Base_App; |
| 7 |
use Elementor\Core\Common\Modules\Connect\Apps\Common_App; |
| 8 |
use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 9 |
use Elementor\Includes\EditorAssetsAPI; |
| 10 |
use Elementor\Utils; |
| 11 |
use Elementor\Plugin; |
| 12 |
use Elementor\Tracker; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
class Module extends BaseModule { |
| 19 |
|
| 20 |
const EXPERIMENT_NAME = 'editor_events'; |
| 21 |
const DEFAULT_SESSION_RECORDING_PERCENT = 0; |
| 22 |
const REMOTE_MIXPANEL_CONFIG_URL = 'https://assets.elementor.com/mixpanel/v1/mixpanel.json'; |
| 23 |
|
| 24 |
public function get_name() { |
| 25 |
return 'events-manager'; |
| 26 |
} |
| 27 |
|
| 28 |
public static function get_editor_events_config() { |
| 29 |
$can_send_events = ! empty( ELEMENTOR_EDITOR_EVENTS_MIXPANEL_TOKEN ) && |
| 30 |
Tracker::is_allow_track() && |
| 31 |
! Tracker::has_terms_changed( '2025-07-07' ) && |
| 32 |
Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ); |
| 33 |
|
| 34 |
$is_flags_enabled = false; |
| 35 |
$session_recording_percent = self::DEFAULT_SESSION_RECORDING_PERCENT; |
| 36 |
$debug = ( defined( 'ELEMENTOR_EDITOR_EVENTS_DEBUG' ) && ELEMENTOR_EDITOR_EVENTS_DEBUG ) ?? false; |
| 37 |
|
| 38 |
if ( $can_send_events ) { |
| 39 |
$mixpanel_config = self::get_remote_mixpanel_config(); |
| 40 |
$has_config = EditorAssetsAPI::has_valid_nested_array( $mixpanel_config, [ 0 ] ); |
| 41 |
|
| 42 |
if ( $has_config ) { |
| 43 |
$is_flags_enabled = (bool) ( $mixpanel_config[0]['flags'] ?? false ); |
| 44 |
|
| 45 |
$session_replays = $mixpanel_config[0]['sessionReplays'] ?? []; |
| 46 |
$session_recording_percent = $session_replays['recordSessionsPercent'] ?? null; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$settings = [ |
| 51 |
'can_send_events' => $can_send_events, |
| 52 |
'debug' => $debug, |
| 53 |
'elementor_version' => ELEMENTOR_VERSION, |
| 54 |
'site_url' => hash( 'sha256', get_site_url() ), |
| 55 |
'wp_version' => get_bloginfo( 'version' ), |
| 56 |
'user_agent' => esc_html( Utils::get_super_global_value( $_SERVER, 'HTTP_USER_AGENT' ) ), |
| 57 |
'site_language' => get_locale(), |
| 58 |
'site_key' => get_option( Base_App::OPTION_CONNECT_SITE_KEY ), |
| 59 |
'subscription_id' => self::get_subscription_id(), |
| 60 |
'subscription' => self::get_subscription(), |
| 61 |
'token' => ELEMENTOR_EDITOR_EVENTS_MIXPANEL_TOKEN, |
| 62 |
'flags_enabled' => $is_flags_enabled, |
| 63 |
'user_id' => self::get_user_id(), |
| 64 |
'session_recording_percent' => $session_recording_percent, |
| 65 |
]; |
| 66 |
|
| 67 |
return $settings; |
| 68 |
} |
| 69 |
|
| 70 |
public static function get_experimental_data(): array { |
| 71 |
return [ |
| 72 |
'name' => static::EXPERIMENT_NAME, |
| 73 |
'title' => esc_html__( 'Elementor Editor Events', 'elementor' ), |
| 74 |
'description' => esc_html__( 'Editor events processing', 'elementor' ), |
| 75 |
'hidden' => true, |
| 76 |
'release_status' => Experiments_Manager::RELEASE_STATUS_ALPHA, |
| 77 |
'default' => Experiments_Manager::STATE_INACTIVE, |
| 78 |
'new_site' => [ |
| 79 |
'default_active' => true, |
| 80 |
'minimum_installation_version' => '3.32.0', |
| 81 |
], |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
private static function get_subscription_id() { |
| 86 |
$subscription = self::get_subscription(); |
| 87 |
|
| 88 |
return $subscription['subscription_id'] ?? null; |
| 89 |
} |
| 90 |
|
| 91 |
private static function get_subscription() { |
| 92 |
if ( ! Utils::has_pro() ) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
$license_data = get_option( '_elementor_pro_license_v2_data' ); |
| 97 |
if ( ! isset( $license_data['value'] ) ) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
return json_decode( $license_data['value'], true ); |
| 102 |
} |
| 103 |
|
| 104 |
private static function get_remote_mixpanel_config() { |
| 105 |
$editor_assets_api = new EditorAssetsAPI( [ |
| 106 |
EditorAssetsAPI::ASSETS_DATA_URL => static::REMOTE_MIXPANEL_CONFIG_URL, |
| 107 |
EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_mixpanel_config', |
| 108 |
EditorAssetsAPI::ASSETS_DATA_KEY => 'mixpanel', |
| 109 |
] ); |
| 110 |
|
| 111 |
return $editor_assets_api->get_assets_data(); |
| 112 |
} |
| 113 |
private static function get_user_id() { |
| 114 |
$user_common_data = get_user_option( Common_App::OPTION_CONNECT_COMMON_DATA_KEY ); |
| 115 |
|
| 116 |
if ( ! is_array( $user_common_data ) ) { |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
return Common_App::get_connect_user_id_from_access_token( $user_common_data['access_token'] ?? null ); |
| 121 |
} |
| 122 |
} |
| 123 |
|