← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Logger.php
+26
-127
4.0.5
→
3.4.7
View file →
| @@ -28,14 +28,9 @@ | ||
| 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'; | |
| 36 | 32 | |
| 37 | - | |
| 38 | 33 | /** |
| 39 | 34 | * Check if we should load the module for this product. |
| 40 | 35 | * |
| 41 | 36 | * @param Product $product Product to load the module for. |
| @@ -42,8 +37,9 @@ | ||
| 42 | 37 | * |
| 43 | 38 | * @return bool Should we load ? |
| 44 | 39 | */ |
| 45 | 40 | public function can_load( $product ) { |
| 41 | + | |
| 46 | 42 | return apply_filters( $product->get_slug() . '_sdk_enable_logger', true ); |
| 47 | 43 | } |
| 48 | 44 | |
| 49 | 45 | /** |
| @@ -54,10 +50,11 @@ | ||
| 54 | 50 | * @return Logger Module object. |
| 55 | 51 | */ |
| 56 | 52 | public function load( $product ) { |
| 57 | 53 | $this->product = $product; |
| 58 | - add_action( 'wp_loaded', array( $this, 'setup_actions' ) ); | |
| 59 | - add_action( 'admin_init', array( $this, 'setup_notification' ) ); | |
| 54 | + $this->setup_notification(); | |
| 55 | + $this->setup_actions(); | |
| 56 | + | |
| 60 | 57 | return $this; |
| 61 | 58 | } |
| 62 | 59 | |
| 63 | 60 | /** |
| @@ -63,12 +60,14 @@ | ||
| 63 | 60 | /** |
| 64 | 61 | * Setup notification on admin. |
| 65 | 62 | */ |
| 66 | 63 | public function setup_notification() { |
| 67 | - if ( $this->is_logger_active() ) { | |
| 64 | + if ( ! $this->product->is_wordpress_available() ) { | |
| 68 | 65 | return; |
| 69 | 66 | } |
| 67 | + | |
| 70 | 68 | add_filter( 'themeisle_sdk_registered_notifications', [ $this, 'add_notification' ] ); |
| 69 | + | |
| 71 | 70 | } |
| 72 | 71 | |
| 73 | 72 | /** |
| 74 | 73 | * Setup tracking actions. |
| @@ -76,25 +75,14 @@ | ||
| 76 | 75 | public function setup_actions() { |
| 77 | 76 | if ( ! $this->is_logger_active() ) { |
| 78 | 77 | return; |
| 79 | 78 | } |
| 80 | - add_action( | |
| 81 | - 'admin_enqueue_scripts', | |
| 82 | - function() { | |
| 83 | - if ( ! apply_filters( 'themeisle_sdk_enable_telemetry', false ) ) { | |
| 84 | - return; | |
| 85 | - } | |
| 86 | - | |
| 87 | - $this->load_telemetry(); | |
| 88 | - }, | |
| 89 | - PHP_INT_MAX | |
| 90 | - ); | |
| 91 | - | |
| 92 | 79 | $action_key = $this->product->get_key() . '_log_activity'; |
| 93 | 80 | if ( ! wp_next_scheduled( $action_key ) ) { |
| 94 | - wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); | |
| 81 | + wp_schedule_single_event( time() + ( rand( 1, 24 ) * 3600 ), $action_key ); | |
| 95 | 82 | } |
| 96 | 83 | add_action( $action_key, array( $this, 'send_log' ) ); |
| 84 | + | |
| 97 | 85 | } |
| 98 | 86 | |
| 99 | 87 | /** |
| 100 | 88 | * Check if the logger is active. |
| @@ -101,28 +89,26 @@ | ||
| 101 | 89 | * |
| 102 | 90 | * @return bool Is logger active? |
| 103 | 91 | */ |
| 104 | 92 | private function is_logger_active() { |
| 105 | - if ( apply_filters( 'themeisle_sdk_disable_telemetry', false ) ) { | |
| 106 | - return false; | |
| 107 | - } | |
| 108 | 93 | $default = 'no'; |
| 109 | 94 | |
| 110 | 95 | if ( ! $this->product->is_wordpress_available() ) { |
| 111 | 96 | $default = 'yes'; |
| 112 | 97 | } else { |
| 113 | - $all_products = Loader::get_products(); | |
| 114 | - foreach ( $all_products as $product ) { | |
| 115 | - if ( $product->requires_license() ) { | |
| 98 | + $pro_slug = $this->product->get_pro_slug(); | |
| 99 | + | |
| 100 | + if ( ! empty( $pro_slug ) ) { | |
| 101 | + $all_products = Loader::get_products(); | |
| 102 | + if ( isset( $all_products[ $pro_slug ] ) ) { | |
| 116 | 103 | $default = 'yes'; |
| 117 | - break; | |
| 118 | 104 | } |
| 119 | 105 | } |
| 120 | 106 | } |
| 121 | 107 | |
| 122 | - | |
| 123 | 108 | return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); |
| 124 | 109 | } |
| 110 | + | |
| 125 | 111 | /** |
| 126 | 112 | * Add notification to queue. |
| 127 | 113 | * |
| 128 | 114 | * @param array $all_notifications Previous notification. |
| @@ -130,9 +116,9 @@ | ||
| 130 | 116 | * @return array All notifications. |
| 131 | 117 | */ |
| 132 | 118 | public function add_notification( $all_notifications ) { |
| 133 | 119 | |
| 134 | - $message = apply_filters( $this->product->get_key() . '_logger_heading', Loader::$labels['logger']['notice'] ); | |
| 120 | + $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 | 121 | |
| 136 | 122 | $message = str_replace( |
| 137 | 123 | array( '{product}' ), |
| 138 | 124 | $this->product->get_friendly_name(), |
| @@ -137,10 +123,10 @@ | ||
| 137 | 123 | array( '{product}' ), |
| 138 | 124 | $this->product->get_friendly_name(), |
| 139 | 125 | $message |
| 140 | 126 | ); |
| 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'] ); | |
| 127 | + $button_submit = apply_filters( $this->product->get_key() . '_logger_button_submit', 'Sure, I would love to help.' ); | |
| 128 | + $button_cancel = apply_filters( $this->product->get_key() . '_logger_button_cancel', 'No, thanks.' ); | |
| 143 | 129 | |
| 144 | 130 | $all_notifications[] = [ |
| 145 | 131 | 'id' => $this->product->get_key() . '_logger_flag', |
| 146 | 132 | 'message' => $message, |
| @@ -177,104 +163,17 @@ | ||
| 177 | 163 | 'method' => 'POST', |
| 178 | 164 | 'timeout' => 3, |
| 179 | 165 | 'redirection' => 5, |
| 180 | 166 | 'body' => array( |
| 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', '' ), | |
| 167 | + 'site' => get_site_url(), | |
| 168 | + 'slug' => $this->product->get_slug(), | |
| 169 | + 'version' => $this->product->get_version(), | |
| 170 | + 'wp_version' => $wp_version, | |
| 171 | + 'locale' => get_locale(), | |
| 172 | + 'data' => apply_filters( $this->product->get_key() . '_logger_data', array() ), | |
| 173 | + 'environment' => $environment, | |
| 174 | + 'license' => apply_filters( $this->product->get_key() . '_license_status', '' ), | |
| 190 | 175 | ), |
| 191 | 176 | ) |
| 192 | 177 | ); |
| 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 | - } | |
| 279 | 178 | } |
| 280 | 179 | } |