| 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 |
$this->setup_notification(); |
| 59 |
$this->setup_actions(); |
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Setup notification on admin. |
| 65 |
*/ |
| 66 |
public function setup_notification() { |
| 67 |
if ( ! $this->product->is_wordpress_available() ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
add_filter( 'themeisle_sdk_registered_notifications', [ $this, 'add_notification' ] ); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Setup tracking actions. |
| 77 |
*/ |
| 78 |
public function setup_actions() { |
| 79 |
if ( ! $this->is_logger_active() ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$can_load_telemetry = apply_filters( 'themeisle_sdk_enable_telemetry', false ); |
| 84 |
|
| 85 |
if ( $can_load_telemetry ) { |
| 86 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_telemetry' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
$action_key = $this->product->get_key() . '_log_activity'; |
| 90 |
if ( ! wp_next_scheduled( $action_key ) ) { |
| 91 |
wp_schedule_single_event( time() + ( wp_rand( 1, 24 ) * 3600 ), $action_key ); |
| 92 |
} |
| 93 |
add_action( $action_key, array( $this, 'send_log' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Check if the logger is active. |
| 98 |
* |
| 99 |
* @return bool Is logger active? |
| 100 |
*/ |
| 101 |
private function is_logger_active() { |
| 102 |
$default = 'no'; |
| 103 |
|
| 104 |
if ( ! $this->product->is_wordpress_available() ) { |
| 105 |
$default = 'yes'; |
| 106 |
} 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 ] ) ) { |
| 112 |
$default = 'yes'; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return ( get_option( $this->product->get_key() . '_logger_flag', $default ) === 'yes' ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Add notification to queue. |
| 122 |
* |
| 123 |
* @param array $all_notifications Previous notification. |
| 124 |
* |
| 125 |
* @return array All notifications. |
| 126 |
*/ |
| 127 |
public function add_notification( $all_notifications ) { |
| 128 |
|
| 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.' ); |
| 130 |
|
| 131 |
$message = str_replace( |
| 132 |
array( '{product}' ), |
| 133 |
$this->product->get_friendly_name(), |
| 134 |
$message |
| 135 |
); |
| 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.' ); |
| 138 |
|
| 139 |
$all_notifications[] = [ |
| 140 |
'id' => $this->product->get_key() . '_logger_flag', |
| 141 |
'message' => $message, |
| 142 |
'ctas' => [ |
| 143 |
'confirm' => [ |
| 144 |
'link' => '#', |
| 145 |
'text' => $button_submit, |
| 146 |
], |
| 147 |
'cancel' => [ |
| 148 |
'link' => '#', |
| 149 |
'text' => $button_cancel, |
| 150 |
], |
| 151 |
], |
| 152 |
]; |
| 153 |
|
| 154 |
return $all_notifications; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Send the statistics to the api endpoint. |
| 159 |
*/ |
| 160 |
public function send_log() { |
| 161 |
$environment = array(); |
| 162 |
$theme = wp_get_theme(); |
| 163 |
$environment['theme'] = array(); |
| 164 |
$environment['theme']['name'] = $theme->get( 'Name' ); |
| 165 |
$environment['theme']['author'] = $theme->get( 'Author' ); |
| 166 |
$environment['theme']['parent'] = $theme->parent() !== false ? $theme->parent()->get( 'Name' ) : $theme->get( 'Name' ); |
| 167 |
$environment['plugins'] = get_option( 'active_plugins' ); |
| 168 |
global $wp_version; |
| 169 |
wp_remote_post( |
| 170 |
self::TRACKING_ENDPOINT, |
| 171 |
array( |
| 172 |
'method' => 'POST', |
| 173 |
'timeout' => 3, |
| 174 |
'redirection' => 5, |
| 175 |
'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', '' ), |
| 184 |
), |
| 185 |
) |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Load telemetry. |
| 191 |
* |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
public function load_telemetry() { |
| 195 |
// See which products have telemetry enabled. |
| 196 |
try { |
| 197 |
$products_with_telemetry = array(); |
| 198 |
$all_products = Loader::get_products(); |
| 199 |
$all_products[ $this->product->get_slug() ] = $this->product; // Add current product to the list of products to check for telemetry. |
| 200 |
|
| 201 |
foreach ( $all_products as $product_slug => $product ) { |
| 202 |
|
| 203 |
// Ignore pro products. |
| 204 |
if ( false !== strstr( $product_slug, 'pro' ) ) { |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
$default = 'no'; |
| 209 |
|
| 210 |
if ( ! $product->is_wordpress_available() ) { |
| 211 |
$default = 'yes'; |
| 212 |
} else { |
| 213 |
$pro_slug = $product->get_pro_slug(); |
| 214 |
|
| 215 |
if ( ! empty( $pro_slug ) && isset( $all_products[ $pro_slug ] ) ) { |
| 216 |
$default = 'yes'; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if ( 'yes' === get_option( $product->get_key() . '_logger_flag', $default ) ) { |
| 221 |
|
| 222 |
$main_slug = explode( '-', $product_slug ); |
| 223 |
$main_slug = $main_slug[0]; |
| 224 |
$pro_slug = $product->get_pro_slug(); |
| 225 |
$track_hash = Licenser::create_license_hash( str_replace( '-', '_', ! empty( $pro_slug ) ? $pro_slug : $product_slug ) ); |
| 226 |
|
| 227 |
// Check if product was already tracked. |
| 228 |
$active_telemetry = false; |
| 229 |
foreach ( $products_with_telemetry as &$product_with_telemetry ) { |
| 230 |
if ( $product_with_telemetry['slug'] === $main_slug ) { |
| 231 |
$active_telemetry = true; |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if ( $active_telemetry ) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
$products_with_telemetry[] = array( |
| 241 |
'slug' => $main_slug, |
| 242 |
'trackHash' => $track_hash ? $track_hash : 'free', |
| 243 |
'consent' => true, |
| 244 |
); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
$products_with_telemetry = apply_filters( 'themeisle_sdk_telemetry_products', $products_with_telemetry ); |
| 249 |
|
| 250 |
if ( 0 === count( $products_with_telemetry ) ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
$tracking_handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'tracking' ); |
| 256 |
if ( ! empty( $tracking_handler ) ) { |
| 257 |
do_action( 'themeisle_sdk_dependency_enqueue_script', 'tracking' ); |
| 258 |
wp_localize_script( |
| 259 |
$tracking_handler, |
| 260 |
'tiTelemetry', |
| 261 |
array( |
| 262 |
'products' => $products_with_telemetry, |
| 263 |
'endpoint' => self::TELEMETRY_ENDPOINT, |
| 264 |
) |
| 265 |
); |
| 266 |
} |
| 267 |
} 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 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { |
| 273 |
error_log( $e->getMessage() ); // phpcs:ignore |
| 274 |
} |
| 275 |
} finally { |
| 276 |
return; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|