← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Logger.php
+124
-20
3.1.3
→
3.11.10
View file →
| @@ -26,11 +26,16 @@ | ||
| 26 | 26 | class Logger extends Abstract_Module { |
| 27 | 27 | /** |
| 28 | 28 | * Endpoint where to collect logs. |
| 29 | 29 | */ |
| 30 | - const TRACKING_ENDPOINT = 'http://log.themeisle.com/wp-json/v1/logs/'; | |
| 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,26 @@ | ||
| 75 | 78 | public function setup_actions() { |
| 76 | 79 | if ( ! $this->is_logger_active() ) { |
| 77 | 80 | return; |
| 78 | 81 | } |
| 82 | + | |
| 83 | + add_action( | |
| 84 | + 'admin_enqueue_scripts', | |
| 85 | + function() { | |
| 86 | + if ( ! apply_filters( 'themeisle_sdk_enable_telemetry', false ) ) { | |
| 87 | + return; | |
| 88 | + } | |
| 89 | + | |
| 90 | + $this->load_telemetry(); | |
| 91 | + }, | |
| 92 | + PHP_INT_MAX | |
| 93 | + ); | |
| 94 | + | |
| 79 | 95 | $action_key = $this->product->get_key() . '_log_activity'; |
| 80 | 96 | if ( ! wp_next_scheduled( $action_key ) ) { |
| 81 | - wp_schedule_single_event( time() + ( rand( 1, 24 ) * 3600 ), $action_key ); | |
| 97 | + wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); | |
| 82 | 98 | } |
| 83 | 99 | add_action( $action_key, array( $this, 'send_log' ) ); |
| 84 | - | |
| 85 | 100 | } |
| 86 | 101 | |
| 87 | 102 | /** |
| 88 | 103 | * Check if the logger is active. |
| @@ -89,21 +104,24 @@ | ||
| 89 | 104 | * |
| 90 | 105 | * @return bool Is logger active? |
| 91 | 106 | */ |
| 92 | 107 | private function is_logger_active() { |
| 108 | + $default = 'no'; | |
| 109 | + | |
| 93 | 110 | if ( ! $this->product->is_wordpress_available() ) { |
| 94 | - return true; | |
| 95 | - } | |
| 96 | - $pro_slug = $this->product->get_pro_slug(); | |
| 111 | + $default = 'yes'; | |
| 112 | + } else { | |
| 113 | + $pro_slug = $this->product->get_pro_slug(); | |
| 97 | 114 | |
| 98 | - if ( ! empty( $pro_slug ) ) { | |
| 99 | - $all_products = Loader::get_products(); | |
| 100 | - if ( isset( $all_products[ $pro_slug ] ) ) { | |
| 101 | - return true; | |
| 115 | + if ( ! empty( $pro_slug ) ) { | |
| 116 | + $all_products = Loader::get_products(); | |
| 117 | + if ( isset( $all_products[ $pro_slug ] ) ) { | |
| 118 | + $default = 'yes'; | |
| 119 | + } | |
| 102 | 120 | } |
| 103 | 121 | } |
| 104 | 122 | |
| 105 | - return ( get_option( $this->product->get_key() . '_logger_flag', 'no' ) === 'yes' ); | |
| 123 | + return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); | |
| 106 | 124 | } |
| 107 | 125 | |
| 108 | 126 | /** |
| 109 | 127 | * Add notification to queue. |
| @@ -113,9 +131,9 @@ | ||
| 113 | 131 | * @return array All notifications. |
| 114 | 132 | */ |
| 115 | 133 | public function add_notification( $all_notifications ) { |
| 116 | 134 | |
| 117 | - $message = apply_filters( $this->product->get_key() . '_logger_heading', 'Do you enjoy <b>{product}</b>? Become a contributor by opting in to our anonymous data tracking. We guarantee no sensitive data is collected.' ); | |
| 135 | + $message = apply_filters( $this->product->get_key() . '_logger_heading', Loader::$labels['logger']['notice'] ); | |
| 118 | 136 | |
| 119 | 137 | $message = str_replace( |
| 120 | 138 | array( '{product}' ), |
| 121 | 139 | $this->product->get_friendly_name(), |
| @@ -120,10 +138,10 @@ | ||
| 120 | 138 | array( '{product}' ), |
| 121 | 139 | $this->product->get_friendly_name(), |
| 122 | 140 | $message |
| 123 | 141 | ); |
| 124 | - $button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', 'Sure, I would love to help.' ); | |
| 125 | - $button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', 'No, thanks.' ); | |
| 142 | + $button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', Loader::$labels['logger']['cta_y'] ); | |
| 143 | + $button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', Loader::$labels['logger']['cta_n'] ); | |
| 126 | 144 | |
| 127 | 145 | $all_notifications[] = [ |
| 128 | 146 | 'id' => $this->product->get_key() . '_logger_flag', |
| 129 | 147 | 'message' => $message, |
| @@ -150,10 +168,11 @@ | ||
| 150 | 168 | $theme = wp_get_theme(); |
| 151 | 169 | $environment['theme'] = array(); |
| 152 | 170 | $environment['theme']['name'] = $theme->get( 'Name' ); |
| 153 | 171 | $environment['theme']['author'] = $theme->get( 'Author' ); |
| 172 | + $environment['theme']['parent'] = $theme->parent() !== false ? $theme->parent()->get( 'Name' ) : $theme->get( 'Name' ); | |
| 154 | 173 | $environment['plugins'] = get_option( 'active_plugins' ); |
| 155 | - | |
| 174 | + global $wp_version; | |
| 156 | 175 | wp_remote_post( |
| 157 | 176 | self::TRACKING_ENDPOINT, |
| 158 | 177 | array( |
| 159 | 178 | 'method' => 'POST', |
| @@ -158,15 +177,14 @@ | ||
| 158 | 177 | array( |
| 159 | 178 | 'method' => 'POST', |
| 160 | 179 | 'timeout' => 3, |
| 161 | 180 | 'redirection' => 5, |
| 162 | - 'headers' => array( | |
| 163 | - 'X-ThemeIsle-Event' => 'log_site', | |
| 164 | - ), | |
| 165 | 181 | 'body' => array( |
| 166 | 182 | 'site' => get_site_url(), |
| 167 | 183 | 'slug' => $this->product->get_slug(), |
| 168 | 184 | 'version' => $this->product->get_version(), |
| 185 | + 'wp_version' => $wp_version, | |
| 186 | + 'locale' => get_locale(), | |
| 169 | 187 | 'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), |
| 170 | 188 | 'environment' => $environment, |
| 171 | 189 | 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), |
| 172 | 190 | ), |
| @@ -171,6 +189,92 @@ | ||
| 171 | 189 | 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), |
| 172 | 190 | ), |
| 173 | 191 | ) |
| 174 | 192 | ); |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Load telemetry. | |
| 197 | + * | |
| 198 | + * @return void | |
| 199 | + */ | |
| 200 | + public function load_telemetry() { | |
| 201 | + // See which products have telemetry enabled. | |
| 202 | + try { | |
| 203 | + $products_with_telemetry = array(); | |
| 204 | + $all_products = Loader::get_products(); | |
| 205 | + $all_products[ $this->product->get_slug() ] = $this->product; // Add current product to the list of products to check for telemetry. | |
| 206 | + | |
| 207 | + // Register telemetry params for eligible products. | |
| 208 | + foreach ( $all_products as $product_slug => $product ) { | |
| 209 | + | |
| 210 | + // Ignore PRO products. | |
| 211 | + if ( false !== strstr( $product_slug, 'pro' ) ) { | |
| 212 | + continue; | |
| 213 | + } | |
| 214 | + | |
| 215 | + $pro_slug = $product->get_pro_slug(); | |
| 216 | + $logger_key = $product->get_key() . '_logger_flag'; | |
| 217 | + | |
| 218 | + // If the product is not available in the WordPress store, or it's PRO version is installed, activate the logger if it was not initialized -- Pro users are opted in by default. | |
| 219 | + if ( ! $product->is_wordpress_available() || ( ! empty( $pro_slug ) && isset( $all_products[ $pro_slug ] ) ) ) { | |
| 220 | + $logger_flag = get_option( $logger_key ); | |
| 221 | + | |
| 222 | + if ( false === $logger_flag ) { | |
| 223 | + update_option( $logger_key, 'yes' ); | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + if ( 'yes' === get_option( $product->get_key() . '_logger_flag', 'no' ) ) { | |
| 228 | + | |
| 229 | + $main_slug = explode( '-', $product_slug ); | |
| 230 | + $main_slug = $main_slug[0]; | |
| 231 | + $track_hash = Licenser::create_license_hash( str_replace( '-', '_', ! empty( $pro_slug ) ? $pro_slug : $product_slug ) ); | |
| 232 | + | |
| 233 | + // Check if product was already tracked. | |
| 234 | + $active_telemetry = false; | |
| 235 | + foreach ( $products_with_telemetry as $product_with_telemetry ) { | |
| 236 | + if ( $product_with_telemetry['slug'] === $main_slug ) { | |
| 237 | + $active_telemetry = true; | |
| 238 | + break; | |
| 239 | + } | |
| 240 | + } | |
| 241 | + | |
| 242 | + if ( $active_telemetry ) { | |
| 243 | + continue; | |
| 244 | + } | |
| 245 | + | |
| 246 | + $products_with_telemetry[] = array( | |
| 247 | + 'slug' => $main_slug, | |
| 248 | + 'trackHash' => $track_hash ? $track_hash : 'free', | |
| 249 | + 'consent' => true, | |
| 250 | + ); | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + $products_with_telemetry = apply_filters( 'themeisle_sdk_telemetry_products', $products_with_telemetry ); | |
| 255 | + | |
| 256 | + if ( 0 === count( $products_with_telemetry ) ) { | |
| 257 | + return; | |
| 258 | + } | |
| 259 | + | |
| 260 | + $tracking_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'tracking' ); | |
| 261 | + if ( ! empty( $tracking_handler ) ) { | |
| 262 | + do_action( 'themeisle_sdk_dependency_enqueue_script', 'tracking' ); | |
| 263 | + wp_localize_script( | |
| 264 | + $tracking_handler, | |
| 265 | + 'tiTelemetry', | |
| 266 | + array( | |
| 267 | + 'products' => $products_with_telemetry, | |
| 268 | + 'endpoint' => self::TELEMETRY_ENDPOINT, | |
| 269 | + ) | |
| 270 | + ); | |
| 271 | + } | |
| 272 | + } catch ( \Exception $e ) { | |
| 273 | + if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 274 | + error_log( $e->getMessage() ); // phpcs:ignore | |
| 275 | + } | |
| 276 | + } finally { | |
| 277 | + return; | |
| 278 | + } | |
| 175 | 279 | } |
| 176 | 280 | } |