About_us.php
5 days ago
Abstract_Migration.php
4 months ago
Announcements.php
4 months ago
Compatibilities.php
2 years ago
Crash_reporter.php
5 days ago
Dashboard_widget.php
5 days ago
Featured_plugins.php
5 days ago
Float_widget.php
2 years ago
Licenser.php
5 days ago
Logger.php
5 days ago
Migrator.php
5 days ago
Notification.php
3 years ago
Promotions.php
5 days ago
Recommendation.php
3 years ago
Review.php
1 year ago
Rollback.php
2 years ago
Script_loader.php
1 year ago
Translate.php
5 years ago
Translations.php
1 year ago
Uninstall_feedback.php
5 days ago
Welcome.php
2 years ago
Logger.php
295 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The logger model class for ThemeIsle SDK |
| 4 | * |
| 5 | * @package ThemeIsleSDK |
| 6 | * @subpackage Modules |
| 7 | * @copyright Copyright (c) 2017, Marius Cristea |
| 8 | * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace ThemeisleSDK\Modules; |
| 13 | |
| 14 | use ThemeisleSDK\Common\Abstract_Module; |
| 15 | use ThemeisleSDK\Loader; |
| 16 | use ThemeisleSDK\Product; |
| 17 | |
| 18 | // Exit if accessed directly. |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Logger module for ThemeIsle SDK. |
| 25 | */ |
| 26 | class Logger extends Abstract_Module { |
| 27 | /** |
| 28 | * Endpoint where to collect logs. |
| 29 | */ |
| 30 | const TRACKING_ENDPOINT = 'https://api.themeisle.com/tracking/log'; |
| 31 | |
| 32 | /** |
| 33 | * Endpoint where to collect telemetry. |
| 34 | */ |
| 35 | const TELEMETRY_ENDPOINT = 'https://api.themeisle.com/tracking/events'; |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Check if we should load the module for this product. |
| 40 | * |
| 41 | * @param Product $product Product to load the module for. |
| 42 | * |
| 43 | * @return bool Should we load ? |
| 44 | */ |
| 45 | public function can_load( $product ) { |
| 46 | return apply_filters( $product->get_slug() . '_sdk_enable_logger', true ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Load module logic. |
| 51 | * |
| 52 | * @param Product $product Product to load. |
| 53 | * |
| 54 | * @return Logger Module object. |
| 55 | */ |
| 56 | public function load( $product ) { |
| 57 | $this->product = $product; |
| 58 | add_action( 'wp_loaded', array( $this, 'setup_actions' ) ); |
| 59 | add_action( 'admin_init', array( $this, 'setup_notification' ) ); |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Setup notification on admin. |
| 65 | */ |
| 66 | public function setup_notification() { |
| 67 | if ( $this->is_logger_active() ) { |
| 68 | return; |
| 69 | } |
| 70 | add_filter( 'themeisle_sdk_registered_notifications', [ $this, 'add_notification' ] ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Setup tracking actions. |
| 75 | */ |
| 76 | public function setup_actions() { |
| 77 | if ( ! $this->is_logger_active() ) { |
| 78 | return; |
| 79 | } |
| 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 | $action_key = $this->product->get_key() . '_log_activity'; |
| 93 | if ( ! wp_next_scheduled( $action_key ) ) { |
| 94 | wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); |
| 95 | } |
| 96 | add_action( $action_key, array( $this, 'send_log' ) ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Check if the logger is active. |
| 101 | * |
| 102 | * @return bool Is logger active? |
| 103 | */ |
| 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 | } |
| 122 | $default = 'no'; |
| 123 | |
| 124 | if ( ! $product->is_wordpress_available() ) { |
| 125 | $default = 'yes'; |
| 126 | } else { |
| 127 | $all_products = Loader::get_products(); |
| 128 | foreach ( $all_products as $registered_product ) { |
| 129 | if ( $registered_product->requires_license() ) { |
| 130 | $default = 'yes'; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | return ( get_option( $product->get_key() . '_logger_flag', $default ) === 'yes' ); |
| 138 | } |
| 139 | /** |
| 140 | * Add notification to queue. |
| 141 | * |
| 142 | * @param array $all_notifications Previous notification. |
| 143 | * |
| 144 | * @return array All notifications. |
| 145 | */ |
| 146 | public function add_notification( $all_notifications ) { |
| 147 | |
| 148 | $message = apply_filters( $this->product->get_key() . '_logger_heading', Loader::$labels['logger']['notice'] ); |
| 149 | |
| 150 | $message = str_replace( |
| 151 | array( '{product}' ), |
| 152 | $this->product->get_friendly_name(), |
| 153 | $message |
| 154 | ); |
| 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'] ); |
| 157 | |
| 158 | $all_notifications[] = [ |
| 159 | 'id' => $this->product->get_key() . '_logger_flag', |
| 160 | 'message' => $message, |
| 161 | 'ctas' => [ |
| 162 | 'confirm' => [ |
| 163 | 'link' => '#', |
| 164 | 'text' => $button_submit, |
| 165 | ], |
| 166 | 'cancel' => [ |
| 167 | 'link' => '#', |
| 168 | 'text' => $button_cancel, |
| 169 | ], |
| 170 | ], |
| 171 | ]; |
| 172 | |
| 173 | return $all_notifications; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Send the statistics to the api endpoint. |
| 178 | */ |
| 179 | public function send_log() { |
| 180 | $environment = array(); |
| 181 | $theme = wp_get_theme(); |
| 182 | $environment['theme'] = array(); |
| 183 | $environment['theme']['name'] = $theme->get( 'Name' ); |
| 184 | $environment['theme']['author'] = $theme->get( 'Author' ); |
| 185 | $environment['theme']['parent'] = $theme->parent() !== false ? $theme->parent()->get( 'Name' ) : $theme->get( 'Name' ); |
| 186 | $environment['plugins'] = get_option( 'active_plugins' ); |
| 187 | global $wp_version; |
| 188 | wp_remote_post( |
| 189 | self::TRACKING_ENDPOINT, |
| 190 | array( |
| 191 | 'method' => 'POST', |
| 192 | 'timeout' => 3, |
| 193 | 'redirection' => 5, |
| 194 | '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', '' ), |
| 204 | ), |
| 205 | ) |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Load telemetry. |
| 211 | * |
| 212 | * @return void |
| 213 | */ |
| 214 | public function load_telemetry() { |
| 215 | // See which products have telemetry enabled. |
| 216 | try { |
| 217 | $products_with_telemetry = array(); |
| 218 | $all_products = Loader::get_products(); |
| 219 | $all_products[ $this->product->get_slug() ] = $this->product; // Add current product to the list of products to check for telemetry. |
| 220 | |
| 221 | // Register telemetry params for eligible products. |
| 222 | foreach ( $all_products as $product_slug => $product ) { |
| 223 | |
| 224 | // Ignore PRO products. |
| 225 | if ( false !== strstr( $product_slug, 'pro' ) ) { |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | $pro_slug = $product->get_pro_slug(); |
| 230 | $logger_key = $product->get_key() . '_logger_flag'; |
| 231 | |
| 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 ); |
| 235 | |
| 236 | if ( false === $logger_flag ) { |
| 237 | update_option( $logger_key, 'yes' ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if ( 'yes' === get_option( $product->get_key() . '_logger_flag', 'no' ) ) { |
| 242 | |
| 243 | $main_slug = explode( '-', $product_slug ); |
| 244 | $main_slug = $main_slug[0]; |
| 245 | $track_hash = Licenser::create_license_hash( str_replace( '-', '_', ! empty( $pro_slug ) ? $pro_slug : $product_slug ) ); |
| 246 | |
| 247 | // Check if product was already tracked. |
| 248 | $active_telemetry = false; |
| 249 | foreach ( $products_with_telemetry as $product_with_telemetry ) { |
| 250 | if ( $product_with_telemetry['slug'] === $main_slug ) { |
| 251 | $active_telemetry = true; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if ( $active_telemetry ) { |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | $products_with_telemetry[] = array( |
| 261 | 'slug' => $main_slug, |
| 262 | 'trackHash' => $track_hash ? $track_hash : 'free', |
| 263 | 'consent' => true, |
| 264 | ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | $products_with_telemetry = apply_filters( 'themeisle_sdk_telemetry_products', $products_with_telemetry ); |
| 269 | |
| 270 | if ( 0 === count( $products_with_telemetry ) ) { |
| 271 | return; |
| 272 | } |
| 273 | |
| 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 | ); |
| 285 | } |
| 286 | } catch ( \Exception $e ) { |
| 287 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { |
| 288 | error_log( $e->getMessage() ); // phpcs:ignore |
| 289 | } |
| 290 | } finally { |
| 291 | return; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 |