← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Logger.php
+47
-46
3.10.11
→
3.11.14
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,28 @@ | ||
| 98 | 101 | * |
| 99 | 102 | * @return bool Is logger active? |
| 100 | 103 | */ |
| 101 | 104 | private function is_logger_active() { |
| 105 | + if ( apply_filters( 'themeisle_sdk_disable_telemetry', false ) ) { | |
| 106 | + return false; | |
| 107 | + } | |
| 102 | 108 | $default = 'no'; |
| 103 | 109 | |
| 104 | 110 | if ( ! $this->product->is_wordpress_available() ) { |
| 105 | 111 | $default = 'yes'; |
| 106 | 112 | } 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 ] ) ) { | |
| 113 | + $all_products = Loader::get_products(); | |
| 114 | + foreach ( $all_products as $product ) { | |
| 115 | + if ( $product->requires_license() ) { | |
| 112 | 116 | $default = 'yes'; |
| 117 | + break; | |
| 113 | 118 | } |
| 114 | 119 | } |
| 115 | 120 | } |
| 116 | 121 | |
| 122 | + | |
| 117 | 123 | return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); |
| 118 | 124 | } |
| 119 | - | |
| 120 | 125 | /** |
| 121 | 126 | * Add notification to queue. |
| 122 | 127 | * |
| 123 | 128 | * @param array $all_notifications Previous notification. |
| @@ -125,9 +130,9 @@ | ||
| 125 | 130 | * @return array All notifications. |
| 126 | 131 | */ |
| 127 | 132 | public function add_notification( $all_notifications ) { |
| 128 | 133 | |
| 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.' ); | |
| 134 | + $message = apply_filters( $this->product->get_key() . '_logger_heading', Loader::$labels['logger']['notice'] ); | |
| 130 | 135 | |
| 131 | 136 | $message = str_replace( |
| 132 | 137 | array( '{product}' ), |
| 133 | 138 | $this->product->get_friendly_name(), |
| @@ -132,10 +137,10 @@ | ||
| 132 | 137 | array( '{product}' ), |
| 133 | 138 | $this->product->get_friendly_name(), |
| 134 | 139 | $message |
| 135 | 140 | ); |
| 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.' ); | |
| 141 | + $button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', Loader::$labels['logger']['cta_y'] ); | |
| 142 | + $button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', Loader::$labels['logger']['cta_n'] ); | |
| 138 | 143 | |
| 139 | 144 | $all_notifications[] = [ |
| 140 | 145 | 'id' => $this->product->get_key() . '_logger_flag', |
| 141 | 146 | 'message' => $message, |
| @@ -172,16 +177,17 @@ | ||
| 172 | 177 | 'method' => 'POST', |
| 173 | 178 | 'timeout' => 3, |
| 174 | 179 | 'redirection' => 5, |
| 175 | 180 | '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', '' ), | |
| 181 | + 'site' => get_site_url(), | |
| 182 | + 'slug' => $this->product->get_slug(), | |
| 183 | + 'version' => $this->product->get_version(), | |
| 184 | + 'wp_version' => $wp_version, | |
| 185 | + 'install_time' => $this->product->get_install_time(), | |
| 186 | + 'locale' => get_locale(), | |
| 187 | + 'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), | |
| 188 | + 'environment' => $environment, | |
| 189 | + 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), | |
| 184 | 190 | ), |
| 185 | 191 | ) |
| 186 | 192 | ); |
| 187 | 193 | } |
| @@ -187,9 +193,9 @@ | ||
| 187 | 193 | } |
| 188 | 194 | |
| 189 | 195 | /** |
| 190 | 196 | * Load telemetry. |
| 191 | - * | |
| 197 | + * | |
| 192 | 198 | * @return void |
| 193 | 199 | */ |
| 194 | 200 | public function load_telemetry() { |
| 195 | 201 | // See which products have telemetry enabled. |
| @@ -197,37 +203,37 @@ | ||
| 197 | 203 | $products_with_telemetry = array(); |
| 198 | 204 | $all_products = Loader::get_products(); |
| 199 | 205 | $all_products[ $this->product->get_slug() ] = $this->product; // Add current product to the list of products to check for telemetry. |
| 200 | 206 | |
| 207 | + // Register telemetry params for eligible products. | |
| 201 | 208 | foreach ( $all_products as $product_slug => $product ) { |
| 202 | - | |
| 203 | - // Ignore pro products. | |
| 209 | + | |
| 210 | + // Ignore PRO products. | |
| 204 | 211 | if ( false !== strstr( $product_slug, 'pro' ) ) { |
| 205 | 212 | continue; |
| 206 | 213 | } |
| 207 | 214 | |
| 208 | - $default = 'no'; | |
| 215 | + $pro_slug = $product->get_pro_slug(); | |
| 216 | + $logger_key = $product->get_key() . '_logger_flag'; | |
| 209 | 217 | |
| 210 | - if ( ! $product->is_wordpress_available() ) { | |
| 211 | - $default = 'yes'; | |
| 212 | - } else { | |
| 213 | - $pro_slug = $product->get_pro_slug(); | |
| 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 ); | |
| 214 | 221 | |
| 215 | - if ( ! empty( $pro_slug ) && isset( $all_products[ $pro_slug ] ) ) { | |
| 216 | - $default = 'yes'; | |
| 222 | + if ( false === $logger_flag ) { | |
| 223 | + update_option( $logger_key, 'yes' ); | |
| 217 | 224 | } |
| 218 | 225 | } |
| 219 | 226 | |
| 220 | - if ( 'yes' === get_option( $product->get_key() . '_logger_flag', $default ) ) { | |
| 227 | + if ( 'yes' === get_option( $product->get_key() . '_logger_flag', 'no' ) ) { | |
| 221 | 228 | |
| 222 | 229 | $main_slug = explode( '-', $product_slug ); |
| 223 | 230 | $main_slug = $main_slug[0]; |
| 224 | - $pro_slug = $product->get_pro_slug(); | |
| 225 | 231 | $track_hash = Licenser::create_license_hash( str_replace( '-', '_', ! empty( $pro_slug ) ? $pro_slug : $product_slug ) ); |
| 226 | 232 | |
| 227 | 233 | // Check if product was already tracked. |
| 228 | 234 | $active_telemetry = false; |
| 229 | - foreach ( $products_with_telemetry as &$product_with_telemetry ) { | |
| 235 | + foreach ( $products_with_telemetry as $product_with_telemetry ) { | |
| 230 | 236 | if ( $product_with_telemetry['slug'] === $main_slug ) { |
| 231 | 237 | $active_telemetry = true; |
| 232 | 238 | break; |
| 233 | 239 | } |
| @@ -235,9 +241,9 @@ | ||
| 235 | 241 | |
| 236 | 242 | if ( $active_telemetry ) { |
| 237 | 243 | continue; |
| 238 | 244 | } |
| 239 | - | |
| 245 | + | |
| 240 | 246 | $products_with_telemetry[] = array( |
| 241 | 247 | 'slug' => $main_slug, |
| 242 | 248 | 'trackHash' => $track_hash ? $track_hash : 'free', |
| 243 | 249 | 'consent' => true, |
| @@ -250,9 +256,8 @@ | ||
| 250 | 256 | if ( 0 === count( $products_with_telemetry ) ) { |
| 251 | 257 | return; |
| 252 | 258 | } |
| 253 | 259 | |
| 254 | - | |
| 255 | 260 | $tracking_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'tracking' ); |
| 256 | 261 | if ( ! empty( $tracking_handler ) ) { |
| 257 | 262 | do_action( 'themeisle_sdk_dependency_enqueue_script', 'tracking' ); |
| 258 | 263 | wp_localize_script( |
| @@ -264,12 +269,8 @@ | ||
| 264 | 269 | ) |
| 265 | 270 | ); |
| 266 | 271 | } |
| 267 | 272 | } 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 | 273 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { |
| 273 | 274 | error_log( $e->getMessage() ); // phpcs:ignore |
| 274 | 275 | } |
| 275 | 276 | } finally { |