← All changes
|
core/common/modules/events-manager/module.php
+58
-4
4.1.3
→
4.3.0-beta3
View file →
| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Base\Module as BaseModule; |
| 6 | 6 | use Elementor\Core\Common\Modules\Connect\Apps\Base_App; |
| 7 | 7 | use Elementor\Core\Common\Modules\Connect\Apps\Common_App; |
| 8 | +use Elementor\Core\Common\Modules\EventsManager\RestApi\Events_Proxy_REST_API; | |
| 8 | 9 | use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 9 | 10 | use Elementor\Includes\EditorAssetsAPI; |
| 10 | 11 | use Elementor\Utils; |
| 11 | 12 | use Elementor\Plugin; |
| @@ -20,17 +21,23 @@ | ||
| 20 | 21 | const EXPERIMENT_NAME = 'editor_events'; |
| 21 | 22 | const DEFAULT_SESSION_RECORDING_PERCENT = 0; |
| 22 | 23 | const REMOTE_MIXPANEL_CONFIG_URL = 'https://assets.elementor.com/mixpanel/v1/mixpanel.json'; |
| 23 | 24 | |
| 25 | + const API_UPSTREAM_HOST = 'https://api-eu.mixpanel.com'; | |
| 26 | + const LIBS_UPSTREAM_HOST = 'https://cdn.mxpnl.com/libs'; | |
| 27 | + | |
| 28 | + public function __construct() { | |
| 29 | + parent::__construct(); | |
| 30 | + | |
| 31 | + ( new Events_Proxy_REST_API() )->register_hooks(); | |
| 32 | + } | |
| 33 | + | |
| 24 | 34 | public function get_name() { |
| 25 | 35 | return 'events-manager'; |
| 26 | 36 | } |
| 27 | 37 | |
| 28 | 38 | 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 ); | |
| 39 | + $can_send_events = self::can_send_events(); | |
| 33 | 40 | |
| 34 | 41 | $is_flags_enabled = false; |
| 35 | 42 | $session_recording_percent = self::DEFAULT_SESSION_RECORDING_PERCENT; |
| 36 | 43 | $debug = ( defined( 'ELEMENTOR_EDITOR_EVENTS_DEBUG' ) && ELEMENTOR_EDITOR_EVENTS_DEBUG ) ?? false; |
| @@ -58,8 +65,10 @@ | ||
| 58 | 65 | 'site_key' => get_option( Base_App::OPTION_CONNECT_SITE_KEY ), |
| 59 | 66 | 'subscription_id' => self::get_subscription_id(), |
| 60 | 67 | 'subscription' => self::get_subscription(), |
| 61 | 68 | 'token' => ELEMENTOR_EDITOR_EVENTS_MIXPANEL_TOKEN, |
| 69 | + 'proxy_api_host' => rest_url( Events_Proxy_REST_API::API_NAMESPACE . '/' . Events_Proxy_REST_API::API_BASE . '/api' ), | |
| 70 | + 'proxy_lib_base_path' => rest_url( Events_Proxy_REST_API::API_NAMESPACE . '/' . Events_Proxy_REST_API::API_BASE . '/libs/' ), | |
| 62 | 71 | 'flags_enabled' => $is_flags_enabled, |
| 63 | 72 | 'user_id' => self::get_user_id(), |
| 64 | 73 | 'session_recording_percent' => $session_recording_percent, |
| 65 | 74 | ]; |
| @@ -66,8 +75,33 @@ | ||
| 66 | 75 | |
| 67 | 76 | return $settings; |
| 68 | 77 | } |
| 69 | 78 | |
| 79 | + public static function dispatch_event( string $event_name, array $properties = [] ): void { | |
| 80 | + if ( ! self::can_send_events() ) { | |
| 81 | + return; | |
| 82 | + } | |
| 83 | + | |
| 84 | + try { | |
| 85 | + $user_id = self::get_user_id(); | |
| 86 | + | |
| 87 | + $context = [ | |
| 88 | + 'user_id' => $user_id, | |
| 89 | + 'distinct_id' => $user_id, | |
| 90 | + 'subscription_id' => self::get_subscription_id(), | |
| 91 | + 'site_key' => get_option( Base_App::OPTION_CONNECT_SITE_KEY ), | |
| 92 | + 'app_version' => ELEMENTOR_VERSION, | |
| 93 | + 'wp_version' => get_bloginfo( 'version' ), | |
| 94 | + 'site_language' => get_locale(), | |
| 95 | + 'source' => 'server', | |
| 96 | + ]; | |
| 97 | + | |
| 98 | + Server_Events_Client::track( $event_name, array_merge( $properties, $context ) ); | |
| 99 | + } catch ( \Throwable $e ) { | |
| 100 | + return; | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 70 | 104 | public static function get_experimental_data(): array { |
| 71 | 105 | return [ |
| 72 | 106 | 'name' => static::EXPERIMENT_NAME, |
| 73 | 107 | 'title' => esc_html__( 'Elementor Editor Events', 'elementor' ), |
| @@ -81,8 +115,15 @@ | ||
| 81 | 115 | ], |
| 82 | 116 | ]; |
| 83 | 117 | } |
| 84 | 118 | |
| 119 | + private static function can_send_events(): bool { | |
| 120 | + return ! empty( ELEMENTOR_EDITOR_EVENTS_MIXPANEL_TOKEN ) && | |
| 121 | + Tracker::is_allow_track() && | |
| 122 | + ! Tracker::has_terms_changed() && | |
| 123 | + Plugin::$instance->experiments->is_feature_active( self::EXPERIMENT_NAME ); | |
| 124 | + } | |
| 125 | + | |
| 85 | 126 | private static function get_subscription_id() { |
| 86 | 127 | $subscription = self::get_subscription(); |
| 87 | 128 | |
| 88 | 129 | return $subscription['subscription_id'] ?? null; |
| @@ -109,8 +150,21 @@ | ||
| 109 | 150 | ] ); |
| 110 | 151 | |
| 111 | 152 | return $editor_assets_api->get_assets_data(); |
| 112 | 153 | } |
| 154 | + | |
| 155 | + public static function get_mixpanel_api_host() { | |
| 156 | + $mixpanel_config = self::get_remote_mixpanel_config(); | |
| 157 | + | |
| 158 | + return wp_http_validate_url( $mixpanel_config[0]['apiHost'] ?? static::API_UPSTREAM_HOST ); | |
| 159 | + } | |
| 160 | + | |
| 161 | + public static function get_mixpanel_lib_host() { | |
| 162 | + $mixpanel_config = self::get_remote_mixpanel_config(); | |
| 163 | + | |
| 164 | + return wp_http_validate_url( $mixpanel_config[0]['libHost'] ?? static::LIBS_UPSTREAM_HOST ); | |
| 165 | + } | |
| 166 | + | |
| 113 | 167 | private static function get_user_id() { |
| 114 | 168 | $user_common_data = get_user_option( Common_App::OPTION_CONNECT_COMMON_DATA_KEY ); |
| 115 | 169 | |
| 116 | 170 | if ( ! is_array( $user_common_data ) ) { |