← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Logger.php
+109
-3
3.10.0
→
3.10.7
View file →
| @@ -28,9 +28,14 @@ | ||
| 28 | 28 | * Endpoint where to collect logs. |
| 29 | 29 | */ |
| 30 | 30 | const TRACKING_ENDPOINT = 'https://api.themeisle.com/tracking/log'; |
| 31 | 31 | |
| 32 | + /** | |
| 33 | + * Endpoint where to collect telemetry. | |
| 34 | + */ | |
| 35 | + const TELEMETRY_ENDPOINT = 'https://api.themeisle.com/tracking/events'; | |
| 32 | 36 | |
| 37 | + | |
| 33 | 38 | /** |
| 34 | 39 | * Check if we should load the module for this product. |
| 35 | 40 | * |
| 36 | 41 | * @param Product $product Product to load the module for. |
| @@ -37,9 +42,8 @@ | ||
| 37 | 42 | * |
| 38 | 43 | * @return bool Should we load ? |
| 39 | 44 | */ |
| 40 | 45 | public function can_load( $product ) { |
| 41 | - | |
| 42 | 46 | return apply_filters( $product->get_slug() . '_sdk_enable_logger', true ); |
| 43 | 47 | } |
| 44 | 48 | |
| 45 | 49 | /** |
| @@ -52,9 +56,8 @@ | ||
| 52 | 56 | public function load( $product ) { |
| 53 | 57 | $this->product = $product; |
| 54 | 58 | $this->setup_notification(); |
| 55 | 59 | $this->setup_actions(); |
| 56 | - | |
| 57 | 60 | return $this; |
| 58 | 61 | } |
| 59 | 62 | |
| 60 | 63 | /** |
| @@ -75,14 +78,20 @@ | ||
| 75 | 78 | public function setup_actions() { |
| 76 | 79 | if ( ! $this->is_logger_active() ) { |
| 77 | 80 | return; |
| 78 | 81 | } |
| 82 | + | |
| 83 | + $can_load_telemetry = apply_filters( 'themeisle_sdk_enable_telemetry', false ); | |
| 84 | + | |
| 85 | + if ( $can_load_telemetry ) { | |
| 86 | + add_action( 'admin_enqueue_scripts', array( $this, 'load_telemetry' ) ); | |
| 87 | + } | |
| 88 | + | |
| 79 | 89 | $action_key = $this->product->get_key() . '_log_activity'; |
| 80 | 90 | if ( ! wp_next_scheduled( $action_key ) ) { |
| 81 | 91 | wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); |
| 82 | 92 | } |
| 83 | 93 | add_action( $action_key, array( $this, 'send_log' ) ); |
| 84 | - | |
| 85 | 94 | } |
| 86 | 95 | |
| 87 | 96 | /** |
| 88 | 97 | * Check if the logger is active. |
| @@ -174,6 +183,103 @@ | ||
| 174 | 183 | 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), |
| 175 | 184 | ), |
| 176 | 185 | ) |
| 177 | 186 | ); |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Load telemetry. | |
| 191 | + * | |
| 192 | + * @return void | |
| 193 | + */ | |
| 194 | + public function load_telemetry() { | |
| 195 | + // See which products have telemetry enabled. | |
| 196 | + try { | |
| 197 | + $products_with_telemetry = array(); | |
| 198 | + $all_products = Loader::get_products(); | |
| 199 | + $all_products[ $this->product->get_slug() ] = $this->product; // Add current product to the list of products to check for telemetry. | |
| 200 | + | |
| 201 | + foreach ( $all_products as $product_slug => $product ) { | |
| 202 | + | |
| 203 | + // Ignore pro products. | |
| 204 | + if ( false !== strstr( $product_slug, 'pro' ) ) { | |
| 205 | + continue; | |
| 206 | + } | |
| 207 | + | |
| 208 | + $default = 'no'; | |
| 209 | + | |
| 210 | + if ( ! $product->is_wordpress_available() ) { | |
| 211 | + $default = 'yes'; | |
| 212 | + } else { | |
| 213 | + $pro_slug = $product->get_pro_slug(); | |
| 214 | + | |
| 215 | + if ( ! empty( $pro_slug ) && isset( $all_products[ $pro_slug ] ) ) { | |
| 216 | + $default = 'yes'; | |
| 217 | + } | |
| 218 | + } | |
| 219 | + | |
| 220 | + if ( 'yes' === get_option( $product->get_key() . '_logger_flag', $default ) ) { | |
| 221 | + | |
| 222 | + $main_slug = explode( '-', $product_slug ); | |
| 223 | + $main_slug = $main_slug[0]; | |
| 224 | + $pro_slug = $product->get_pro_slug(); | |
| 225 | + $track_hash = Licenser::create_license_hash( str_replace( '-', '_', ! empty( $pro_slug ) ? $pro_slug : $product_slug ) ); | |
| 226 | + | |
| 227 | + // Check if product was already tracked. | |
| 228 | + $active_telemetry = false; | |
| 229 | + foreach ( $products_with_telemetry as &$product_with_telemetry ) { | |
| 230 | + if ( $product_with_telemetry['slug'] === $main_slug ) { | |
| 231 | + $active_telemetry = true; | |
| 232 | + break; | |
| 233 | + } | |
| 234 | + } | |
| 235 | + | |
| 236 | + if ( $active_telemetry ) { | |
| 237 | + continue; | |
| 238 | + } | |
| 239 | + | |
| 240 | + $products_with_telemetry[] = array( | |
| 241 | + 'slug' => $main_slug, | |
| 242 | + 'trackHash' => $track_hash ? $track_hash : 'free', | |
| 243 | + 'consent' => true, | |
| 244 | + ); | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 248 | + $products_with_telemetry = apply_filters( 'themeisle_sdk_telemetry_products', $products_with_telemetry ); | |
| 249 | + | |
| 250 | + if ( 0 === count( $products_with_telemetry ) ) { | |
| 251 | + return; | |
| 252 | + } | |
| 253 | + | |
| 254 | + global $themeisle_sdk_max_path; | |
| 255 | + $asset_file = require $themeisle_sdk_max_path . '/assets/js/build/tracking/tracking.asset.php'; | |
| 256 | + | |
| 257 | + wp_enqueue_script( | |
| 258 | + 'themeisle_sdk_telemetry_script', | |
| 259 | + $this->get_sdk_uri() . 'assets/js/build/tracking/tracking.js', | |
| 260 | + $asset_file['dependencies'], | |
| 261 | + $asset_file['version'], | |
| 262 | + true | |
| 263 | + ); | |
| 264 | + | |
| 265 | + wp_localize_script( | |
| 266 | + 'themeisle_sdk_telemetry_script', | |
| 267 | + 'tiTelemetry', | |
| 268 | + array( | |
| 269 | + 'products' => $products_with_telemetry, | |
| 270 | + 'endpoint' => self::TELEMETRY_ENDPOINT, | |
| 271 | + ) | |
| 272 | + ); | |
| 273 | + } catch ( \Exception $e ) { | |
| 274 | + if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 275 | + error_log( $e->getMessage() ); // phpcs:ignore | |
| 276 | + } | |
| 277 | + } catch ( \Error $e ) { | |
| 278 | + if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 279 | + error_log( $e->getMessage() ); // phpcs:ignore | |
| 280 | + } | |
| 281 | + } finally { | |
| 282 | + return; | |
| 283 | + } | |
| 178 | 284 | } |
| 179 | 285 | } |