← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Logger.php
+66
-75
4.0.7
→
3.10.7
View file →
| @@ -54,10 +54,10 @@ | ||
| 54 | 54 | * @return Logger Module object. |
| 55 | 55 | */ |
| 56 | 56 | public function load( $product ) { |
| 57 | 57 | $this->product = $product; |
| 58 | - add_action( 'wp_loaded', array( $this, 'setup_actions' ) ); | |
| 59 | - add_action( 'admin_init', array( $this, 'setup_notification' ) ); | |
| 58 | + $this->setup_notification(); | |
| 59 | + $this->setup_actions(); | |
| 60 | 60 | return $this; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
| @@ -63,12 +63,14 @@ | ||
| 63 | 63 | /** |
| 64 | 64 | * Setup notification on admin. |
| 65 | 65 | */ |
| 66 | 66 | public function setup_notification() { |
| 67 | - if ( $this->is_logger_active() ) { | |
| 67 | + if ( ! $this->product->is_wordpress_available() ) { | |
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | + | |
| 70 | 71 | add_filter( 'themeisle_sdk_registered_notifications', [ $this, 'add_notification' ] ); |
| 72 | + | |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | /** |
| 74 | 76 | * Setup tracking actions. |
| @@ -76,20 +78,15 @@ | ||
| 76 | 78 | public function setup_actions() { |
| 77 | 79 | if ( ! $this->is_logger_active() ) { |
| 78 | 80 | return; |
| 79 | 81 | } |
| 80 | - add_action( | |
| 81 | - 'admin_enqueue_scripts', | |
| 82 | - function() { | |
| 83 | - if ( ! apply_filters( 'themeisle_sdk_enable_telemetry', false ) ) { | |
| 84 | - return; | |
| 85 | - } | |
| 86 | 82 | |
| 87 | - $this->load_telemetry(); | |
| 88 | - }, | |
| 89 | - PHP_INT_MAX | |
| 90 | - ); | |
| 83 | + $can_load_telemetry = apply_filters( 'themeisle_sdk_enable_telemetry', false ); | |
| 91 | 84 | |
| 85 | + if ( $can_load_telemetry ) { | |
| 86 | + add_action( 'admin_enqueue_scripts', array( $this, 'load_telemetry' ) ); | |
| 87 | + } | |
| 88 | + | |
| 92 | 89 | $action_key = $this->product->get_key() . '_log_activity'; |
| 93 | 90 | if ( ! wp_next_scheduled( $action_key ) ) { |
| 94 | 91 | wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); |
| 95 | 92 | } |
| @@ -101,42 +98,26 @@ | ||
| 101 | 98 | * |
| 102 | 99 | * @return bool Is logger active? |
| 103 | 100 | */ |
| 104 | 101 | private function is_logger_active() { |
| 105 | - return self::is_logging_active( $this->product ); | |
| 106 | - } | |
| 107 | - | |
| 108 | - /** | |
| 109 | - * Check if the logging consent is granted for a product. | |
| 110 | - * | |
| 111 | - * Shared with the other telemetry modules (e.g. the crash reporter) so the | |
| 112 | - * consent semantics live in a single place. | |
| 113 | - * | |
| 114 | - * @param \ThemeisleSDK\Product $product Product to check. | |
| 115 | - * | |
| 116 | - * @return bool Is logging active for the product? | |
| 117 | - */ | |
| 118 | - public static function is_logging_active( $product ) { | |
| 119 | - if ( apply_filters( 'themeisle_sdk_disable_telemetry', false ) ) { | |
| 120 | - return false; | |
| 121 | - } | |
| 122 | 102 | $default = 'no'; |
| 123 | 103 | |
| 124 | - if ( ! $product->is_wordpress_available() ) { | |
| 104 | + if ( ! $this->product->is_wordpress_available() ) { | |
| 125 | 105 | $default = 'yes'; |
| 126 | 106 | } else { |
| 127 | - $all_products = Loader::get_products(); | |
| 128 | - foreach ( $all_products as $registered_product ) { | |
| 129 | - if ( $registered_product->requires_license() ) { | |
| 107 | + $pro_slug = $this->product->get_pro_slug(); | |
| 108 | + | |
| 109 | + if ( ! empty( $pro_slug ) ) { | |
| 110 | + $all_products = Loader::get_products(); | |
| 111 | + if ( isset( $all_products[ $pro_slug ] ) ) { | |
| 130 | 112 | $default = 'yes'; |
| 131 | - break; | |
| 132 | 113 | } |
| 133 | 114 | } |
| 134 | 115 | } |
| 135 | 116 | |
| 117 | + return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); | |
| 118 | + } | |
| 136 | 119 | |
| 137 | - return ( get_option( $product->get_key() . '_logger_flag', $default ) === 'yes' ); | |
| 138 | - } | |
| 139 | 120 | /** |
| 140 | 121 | * Add notification to queue. |
| 141 | 122 | * |
| 142 | 123 | * @param array $all_notifications Previous notification. |
| @@ -144,9 +125,9 @@ | ||
| 144 | 125 | * @return array All notifications. |
| 145 | 126 | */ |
| 146 | 127 | public function add_notification( $all_notifications ) { |
| 147 | 128 | |
| 148 | - $message = apply_filters( $this->product->get_key() . '_logger_heading', Loader::$labels['logger']['notice'] ); | |
| 129 | + $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.' ); | |
| 149 | 130 | |
| 150 | 131 | $message = str_replace( |
| 151 | 132 | array( '{product}' ), |
| 152 | 133 | $this->product->get_friendly_name(), |
| @@ -151,10 +132,10 @@ | ||
| 151 | 132 | array( '{product}' ), |
| 152 | 133 | $this->product->get_friendly_name(), |
| 153 | 134 | $message |
| 154 | 135 | ); |
| 155 | - $button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', Loader::$labels['logger']['cta_y'] ); | |
| 156 | - $button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', Loader::$labels['logger']['cta_n'] ); | |
| 136 | + $button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', 'Sure, I would love to help.' ); | |
| 137 | + $button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', 'No, thanks.' ); | |
| 157 | 138 | |
| 158 | 139 | $all_notifications[] = [ |
| 159 | 140 | 'id' => $this->product->get_key() . '_logger_flag', |
| 160 | 141 | 'message' => $message, |
| @@ -191,17 +172,16 @@ | ||
| 191 | 172 | 'method' => 'POST', |
| 192 | 173 | 'timeout' => 3, |
| 193 | 174 | 'redirection' => 5, |
| 194 | 175 | 'body' => array( |
| 195 | - 'site' => get_site_url(), | |
| 196 | - 'slug' => $this->product->get_slug(), | |
| 197 | - 'version' => $this->product->get_version(), | |
| 198 | - 'wp_version' => $wp_version, | |
| 199 | - 'install_time' => $this->product->get_install_time(), | |
| 200 | - 'locale' => get_locale(), | |
| 201 | - 'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), | |
| 202 | - 'environment' => $environment, | |
| 203 | - 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), | |
| 176 | + 'site' => get_site_url(), | |
| 177 | + 'slug' => $this->product->get_slug(), | |
| 178 | + 'version' => $this->product->get_version(), | |
| 179 | + 'wp_version' => $wp_version, | |
| 180 | + 'locale' => get_locale(), | |
| 181 | + 'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), | |
| 182 | + 'environment' => $environment, | |
| 183 | + 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), | |
| 204 | 184 | ), |
| 205 | 185 | ) |
| 206 | 186 | ); |
| 207 | 187 | } |
| @@ -207,9 +187,9 @@ | ||
| 207 | 187 | } |
| 208 | 188 | |
| 209 | 189 | /** |
| 210 | 190 | * Load telemetry. |
| 211 | - * | |
| 191 | + * | |
| 212 | 192 | * @return void |
| 213 | 193 | */ |
| 214 | 194 | public function load_telemetry() { |
| 215 | 195 | // See which products have telemetry enabled. |
| @@ -217,37 +197,37 @@ | ||
| 217 | 197 | $products_with_telemetry = array(); |
| 218 | 198 | $all_products = Loader::get_products(); |
| 219 | 199 | $all_products[ $this->product->get_slug() ] = $this->product; // Add current product to the list of products to check for telemetry. |
| 220 | 200 | |
| 221 | - // Register telemetry params for eligible products. | |
| 222 | 201 | foreach ( $all_products as $product_slug => $product ) { |
| 223 | - | |
| 224 | - // Ignore PRO products. | |
| 202 | + | |
| 203 | + // Ignore pro products. | |
| 225 | 204 | if ( false !== strstr( $product_slug, 'pro' ) ) { |
| 226 | 205 | continue; |
| 227 | 206 | } |
| 228 | 207 | |
| 229 | - $pro_slug = $product->get_pro_slug(); | |
| 230 | - $logger_key = $product->get_key() . '_logger_flag'; | |
| 208 | + $default = 'no'; | |
| 231 | 209 | |
| 232 | - // 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. | |
| 233 | - if ( ! $product->is_wordpress_available() || ( ! empty( $pro_slug ) && isset( $all_products[ $pro_slug ] ) ) ) { | |
| 234 | - $logger_flag = get_option( $logger_key ); | |
| 210 | + if ( ! $product->is_wordpress_available() ) { | |
| 211 | + $default = 'yes'; | |
| 212 | + } else { | |
| 213 | + $pro_slug = $product->get_pro_slug(); | |
| 235 | 214 | |
| 236 | - if ( false === $logger_flag ) { | |
| 237 | - update_option( $logger_key, 'yes' ); | |
| 215 | + if ( ! empty( $pro_slug ) && isset( $all_products[ $pro_slug ] ) ) { | |
| 216 | + $default = 'yes'; | |
| 238 | 217 | } |
| 239 | 218 | } |
| 240 | 219 | |
| 241 | - if ( 'yes' === get_option( $product->get_key() . '_logger_flag', 'no' ) ) { | |
| 220 | + if ( 'yes' === get_option( $product->get_key() . '_logger_flag', $default ) ) { | |
| 242 | 221 | |
| 243 | 222 | $main_slug = explode( '-', $product_slug ); |
| 244 | 223 | $main_slug = $main_slug[0]; |
| 224 | + $pro_slug = $product->get_pro_slug(); | |
| 245 | 225 | $track_hash = Licenser::create_license_hash( str_replace( '-', '_', ! empty( $pro_slug ) ? $pro_slug : $product_slug ) ); |
| 246 | 226 | |
| 247 | 227 | // Check if product was already tracked. |
| 248 | 228 | $active_telemetry = false; |
| 249 | - foreach ( $products_with_telemetry as $product_with_telemetry ) { | |
| 229 | + foreach ( $products_with_telemetry as &$product_with_telemetry ) { | |
| 250 | 230 | if ( $product_with_telemetry['slug'] === $main_slug ) { |
| 251 | 231 | $active_telemetry = true; |
| 252 | 232 | break; |
| 253 | 233 | } |
| @@ -255,9 +235,9 @@ | ||
| 255 | 235 | |
| 256 | 236 | if ( $active_telemetry ) { |
| 257 | 237 | continue; |
| 258 | 238 | } |
| 259 | - | |
| 239 | + | |
| 260 | 240 | $products_with_telemetry[] = array( |
| 261 | 241 | 'slug' => $main_slug, |
| 262 | 242 | 'trackHash' => $track_hash ? $track_hash : 'free', |
| 263 | 243 | 'consent' => true, |
| @@ -270,21 +250,32 @@ | ||
| 270 | 250 | if ( 0 === count( $products_with_telemetry ) ) { |
| 271 | 251 | return; |
| 272 | 252 | } |
| 273 | 253 | |
| 274 | - $tracking_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'tracking' ); | |
| 275 | - if ( ! empty( $tracking_handler ) ) { | |
| 276 | - do_action( 'themeisle_sdk_dependency_enqueue_script', 'tracking' ); | |
| 277 | - wp_localize_script( | |
| 278 | - $tracking_handler, | |
| 279 | - 'tiTelemetry', | |
| 280 | - array( | |
| 281 | - 'products' => $products_with_telemetry, | |
| 282 | - 'endpoint' => self::TELEMETRY_ENDPOINT, | |
| 283 | - ) | |
| 284 | - ); | |
| 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 | |
| 285 | 276 | } |
| 286 | - } catch ( \Exception $e ) { | |
| 277 | + } catch ( \Error $e ) { | |
| 287 | 278 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { |
| 288 | 279 | error_log( $e->getMessage() ); // phpcs:ignore |
| 289 | 280 | } |
| 290 | 281 | } finally { |