← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Logger.php
+60
-45
3.10.13
→
4.0.8
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 | - $this->setup_notification(); | |
| 59 | - $this->setup_actions(); | |
| 58 | + add_action( 'wp_loaded', array( $this, 'setup_actions' ) ); | |
| 59 | + add_action( 'admin_init', array( $this, 'setup_notification' ) ); | |
| 60 | 60 | return $this; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
| @@ -63,14 +63,12 @@ | ||
| 63 | 63 | /** |
| 64 | 64 | * Setup notification on admin. |
| 65 | 65 | */ |
| 66 | 66 | public function setup_notification() { |
| 67 | - if ( ! $this->product->is_wordpress_available() ) { | |
| 67 | + if ( $this->is_logger_active() ) { | |
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | - | |
| 71 | 70 | add_filter( 'themeisle_sdk_registered_notifications', [ $this, 'add_notification' ] ); |
| 72 | - | |
| 73 | 71 | } |
| 74 | 72 | |
| 75 | 73 | /** |
| 76 | 74 | * Setup tracking actions. |
| @@ -78,15 +76,20 @@ | ||
| 78 | 76 | public function setup_actions() { |
| 79 | 77 | if ( ! $this->is_logger_active() ) { |
| 80 | 78 | return; |
| 81 | 79 | } |
| 80 | + add_action( | |
| 81 | + 'admin_enqueue_scripts', | |
| 82 | + function() { | |
| 83 | + if ( ! apply_filters( 'themeisle_sdk_enable_telemetry', false ) ) { | |
| 84 | + return; | |
| 85 | + } | |
| 82 | 86 | |
| 83 | - $can_load_telemetry = apply_filters( 'themeisle_sdk_enable_telemetry', false ); | |
| 87 | + $this->load_telemetry(); | |
| 88 | + }, | |
| 89 | + PHP_INT_MAX | |
| 90 | + ); | |
| 84 | 91 | |
| 85 | - if ( $can_load_telemetry ) { | |
| 86 | - add_action( 'admin_enqueue_scripts', array( $this, 'load_telemetry' ) ); | |
| 87 | - } | |
| 88 | - | |
| 89 | 92 | $action_key = $this->product->get_key() . '_log_activity'; |
| 90 | 93 | if ( ! wp_next_scheduled( $action_key ) ) { |
| 91 | 94 | wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); |
| 92 | 95 | } |
| @@ -98,26 +101,42 @@ | ||
| 98 | 101 | * |
| 99 | 102 | * @return bool Is logger active? |
| 100 | 103 | */ |
| 101 | 104 | 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 | + } | |
| 102 | 122 | $default = 'no'; |
| 103 | 123 | |
| 104 | - if ( ! $this->product->is_wordpress_available() ) { | |
| 124 | + if ( ! $product->is_wordpress_available() ) { | |
| 105 | 125 | $default = 'yes'; |
| 106 | 126 | } else { |
| 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 ] ) ) { | |
| 127 | + $all_products = Loader::get_products(); | |
| 128 | + foreach ( $all_products as $registered_product ) { | |
| 129 | + if ( $registered_product->requires_license() ) { | |
| 112 | 130 | $default = 'yes'; |
| 131 | + break; | |
| 113 | 132 | } |
| 114 | 133 | } |
| 115 | 134 | } |
| 116 | 135 | |
| 117 | - return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); | |
| 136 | + | |
| 137 | + return ( get_option( $product->get_key() . '_logger_flag', $default ) === 'yes' ); | |
| 118 | 138 | } |
| 119 | - | |
| 120 | 139 | /** |
| 121 | 140 | * Add notification to queue. |
| 122 | 141 | * |
| 123 | 142 | * @param array $all_notifications Previous notification. |
| @@ -172,16 +191,17 @@ | ||
| 172 | 191 | 'method' => 'POST', |
| 173 | 192 | 'timeout' => 3, |
| 174 | 193 | 'redirection' => 5, |
| 175 | 194 | 'body' => array( |
| 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', '' ), | |
| 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', '' ), | |
| 184 | 204 | ), |
| 185 | 205 | ) |
| 186 | 206 | ); |
| 187 | 207 | } |
| @@ -187,9 +207,9 @@ | ||
| 187 | 207 | } |
| 188 | 208 | |
| 189 | 209 | /** |
| 190 | 210 | * Load telemetry. |
| 191 | - * | |
| 211 | + * | |
| 192 | 212 | * @return void |
| 193 | 213 | */ |
| 194 | 214 | public function load_telemetry() { |
| 195 | 215 | // See which products have telemetry enabled. |
| @@ -197,37 +217,37 @@ | ||
| 197 | 217 | $products_with_telemetry = array(); |
| 198 | 218 | $all_products = Loader::get_products(); |
| 199 | 219 | $all_products[ $this->product->get_slug() ] = $this->product; // Add current product to the list of products to check for telemetry. |
| 200 | 220 | |
| 221 | + // Register telemetry params for eligible products. | |
| 201 | 222 | foreach ( $all_products as $product_slug => $product ) { |
| 202 | - | |
| 203 | - // Ignore pro products. | |
| 223 | + | |
| 224 | + // Ignore PRO products. | |
| 204 | 225 | if ( false !== strstr( $product_slug, 'pro' ) ) { |
| 205 | 226 | continue; |
| 206 | 227 | } |
| 207 | 228 | |
| 208 | - $default = 'no'; | |
| 229 | + $pro_slug = $product->get_pro_slug(); | |
| 230 | + $logger_key = $product->get_key() . '_logger_flag'; | |
| 209 | 231 | |
| 210 | - if ( ! $product->is_wordpress_available() ) { | |
| 211 | - $default = 'yes'; | |
| 212 | - } else { | |
| 213 | - $pro_slug = $product->get_pro_slug(); | |
| 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 ); | |
| 214 | 235 | |
| 215 | - if ( ! empty( $pro_slug ) && isset( $all_products[ $pro_slug ] ) ) { | |
| 216 | - $default = 'yes'; | |
| 236 | + if ( false === $logger_flag ) { | |
| 237 | + update_option( $logger_key, 'yes' ); | |
| 217 | 238 | } |
| 218 | 239 | } |
| 219 | 240 | |
| 220 | - if ( 'yes' === get_option( $product->get_key() . '_logger_flag', $default ) ) { | |
| 241 | + if ( 'yes' === get_option( $product->get_key() . '_logger_flag', 'no' ) ) { | |
| 221 | 242 | |
| 222 | 243 | $main_slug = explode( '-', $product_slug ); |
| 223 | 244 | $main_slug = $main_slug[0]; |
| 224 | - $pro_slug = $product->get_pro_slug(); | |
| 225 | 245 | $track_hash = Licenser::create_license_hash( str_replace( '-', '_', ! empty( $pro_slug ) ? $pro_slug : $product_slug ) ); |
| 226 | 246 | |
| 227 | 247 | // Check if product was already tracked. |
| 228 | 248 | $active_telemetry = false; |
| 229 | - foreach ( $products_with_telemetry as &$product_with_telemetry ) { | |
| 249 | + foreach ( $products_with_telemetry as $product_with_telemetry ) { | |
| 230 | 250 | if ( $product_with_telemetry['slug'] === $main_slug ) { |
| 231 | 251 | $active_telemetry = true; |
| 232 | 252 | break; |
| 233 | 253 | } |
| @@ -235,9 +255,9 @@ | ||
| 235 | 255 | |
| 236 | 256 | if ( $active_telemetry ) { |
| 237 | 257 | continue; |
| 238 | 258 | } |
| 239 | - | |
| 259 | + | |
| 240 | 260 | $products_with_telemetry[] = array( |
| 241 | 261 | 'slug' => $main_slug, |
| 242 | 262 | 'trackHash' => $track_hash ? $track_hash : 'free', |
| 243 | 263 | 'consent' => true, |
| @@ -250,9 +270,8 @@ | ||
| 250 | 270 | if ( 0 === count( $products_with_telemetry ) ) { |
| 251 | 271 | return; |
| 252 | 272 | } |
| 253 | 273 | |
| 254 | - | |
| 255 | 274 | $tracking_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'tracking' ); |
| 256 | 275 | if ( ! empty( $tracking_handler ) ) { |
| 257 | 276 | do_action( 'themeisle_sdk_dependency_enqueue_script', 'tracking' ); |
| 258 | 277 | wp_localize_script( |
| @@ -264,12 +283,8 @@ | ||
| 264 | 283 | ) |
| 265 | 284 | ); |
| 266 | 285 | } |
| 267 | 286 | } catch ( \Exception $e ) { |
| 268 | - if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { | |
| 269 | - error_log( $e->getMessage() ); // phpcs:ignore | |
| 270 | - } | |
| 271 | - } catch ( \Error $e ) { | |
| 272 | 287 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { |
| 273 | 288 | error_log( $e->getMessage() ); // phpcs:ignore |
| 274 | 289 | } |
| 275 | 290 | } finally { |