| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yatra\Core\Admin; |
| 4 |
|
| 5 |
|
| 6 |
use Yatra\Core\API\TrackerAPI; |
| 7 |
|
| 8 |
/** |
| 9 |
* Agent Usage tracking |
| 10 |
* |
| 11 |
* @return void |
| 12 |
* @since 2.1.12 |
| 13 |
*/ |
| 14 |
class Tracking |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* @access private |
| 19 |
*/ |
| 20 |
private $slug = ''; |
| 21 |
private $secret_opt_key = ''; |
| 22 |
private $last_send_opt_key = ''; |
| 23 |
private $hide_notice_opt_key = ''; |
| 24 |
private $data; |
| 25 |
private $api_url = ''; |
| 26 |
private $remote_url = ''; |
| 27 |
private $version = ''; |
| 28 |
private $is_test_environment = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* Get things going |
| 32 |
* allow_tracking is set from $this->get_opt_data() |
| 33 |
* Other from opt key |
| 34 |
* |
| 35 |
* @return void |
| 36 |
* @since 2.1.12 |
| 37 |
*/ |
| 38 |
public function __construct() |
| 39 |
{ |
| 40 |
/*Changed with the plugin*/ |
| 41 |
$this->remote_url = trailingslashit('https://tracking.mantrabrain.com/wp-json/mantrabrain/usage-tracking/v1/'); |
| 42 |
if ($this->is_test_environment) { |
| 43 |
$this->remote_url = trailingslashit('http://localhost/wp-json/mantrabrain/usage-tracking/v1/'); |
| 44 |
} |
| 45 |
$this->slug = 'yatra'; |
| 46 |
$this->version = '1.0.0'; |
| 47 |
/*Changed with the plugin end*/ |
| 48 |
|
| 49 |
$this->secret_opt_key = 'agent_secret_key'; |
| 50 |
$this->last_send_opt_key = 'agent_last_send'; |
| 51 |
$this->hide_notice_opt_key = 'agent_hide_notice'; |
| 52 |
|
| 53 |
$this->init(); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Set up WordPress hooks. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
* @since 2.1.12 |
| 62 |
*/ |
| 63 |
public function init() |
| 64 |
{ |
| 65 |
|
| 66 |
add_action('init', array($this, 'schedule_send')); |
| 67 |
add_action('yatra_admin_settings_sanitize_option_yatra_allow_tracking', array($this, 'after_tracking_enable'), 10, 3); |
| 68 |
add_action('admin_init', array($this, 'show_tracking_notice')); |
| 69 |
add_action('admin_notices', array($this, 'admin_notice')); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Update secret keyy |
| 74 |
* |
| 75 |
* @return void |
| 76 |
* @since 2.1.12 |
| 77 |
*/ |
| 78 |
private function update_secret_key($secret_key) |
| 79 |
{ |
| 80 |
$this->update_opt_data($this->secret_opt_key, sanitize_text_field($secret_key)); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Authorize this site to send data to engine. |
| 85 |
* get secret key from engine |
| 86 |
* run on agent activation. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
* @since 2.1.12 |
| 90 |
*/ |
| 91 |
public function handshake_and_request_api() |
| 92 |
{ |
| 93 |
$this->request_api(true); //handshake |
| 94 |
$this->request_api(); // For Request |
| 95 |
|
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Default Options |
| 101 |
* |
| 102 |
* @param null |
| 103 |
* @return array |
| 104 |
* |
| 105 |
* @since 2.1.12 |
| 106 |
*/ |
| 107 |
private function default_options() |
| 108 |
{ |
| 109 |
return array( |
| 110 |
'allow_tracking' => false, |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get option. |
| 116 |
* |
| 117 |
* @return array |
| 118 |
* @since 2.1.12 |
| 119 |
*/ |
| 120 |
private function get_opt_data($key = '') |
| 121 |
{ |
| 122 |
$helper_options = json_decode(get_option('yatra_helper_options'), true); |
| 123 |
|
| 124 |
$helper_default_options = $this->default_options(); |
| 125 |
if (!empty($key)) { |
| 126 |
if (isset($helper_options[$key])) { |
| 127 |
return $helper_options[$key]; |
| 128 |
} |
| 129 |
return $helper_default_options[$key] ?? ''; |
| 130 |
} else { |
| 131 |
if (!is_array($helper_options)) { |
| 132 |
$helper_options = array(); |
| 133 |
} |
| 134 |
return array_merge($helper_default_options, $helper_options); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Update options. |
| 140 |
* |
| 141 |
* @return array |
| 142 |
* @since 2.1.12 |
| 143 |
*/ |
| 144 |
private function update_opt_data($key, $val) |
| 145 |
{ |
| 146 |
$helper_options = json_decode(get_option('yatra_helper_options'), true); |
| 147 |
if (!is_array($helper_options)) { |
| 148 |
$helper_options = array(); |
| 149 |
} |
| 150 |
$helper_options[$key] = $val; |
| 151 |
|
| 152 |
if ($key === 'allow_tracking') { |
| 153 |
$value = (boolean)$val ? 'yes' : 'no'; |
| 154 |
delete_option('yatra_allow_tracking'); |
| 155 |
add_option('yatra_allow_tracking', $value); |
| 156 |
} |
| 157 |
update_option( |
| 158 |
'yatra_helper_options', |
| 159 |
wp_json_encode($helper_options) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
private function get_data_items() |
| 164 |
{ |
| 165 |
|
| 166 |
$theme_data = wp_get_theme(); |
| 167 |
$theme = $theme_data->Name . ' ' . $theme_data->Version; |
| 168 |
$checkout_page = yatra_get_checkout_page(); |
| 169 |
$date = (absint($checkout_page) > 0) |
| 170 |
? get_post_field('post_date', $checkout_page) |
| 171 |
: 'not set'; |
| 172 |
$server = isset($_SERVER['SERVER_SOFTWARE']) |
| 173 |
? $_SERVER['SERVER_SOFTWARE'] |
| 174 |
: ''; |
| 175 |
|
| 176 |
$installed_date = date('Y-m-d H:i:s', get_option('yatra_install_date', strtotime($date))); |
| 177 |
// Setup data. |
| 178 |
$data = array( |
| 179 |
'php_version' => phpversion(), |
| 180 |
'yatra_version' => YATRA_VERSION, |
| 181 |
'wp_version' => get_bloginfo('version'), |
| 182 |
'server' => $server, |
| 183 |
'install_date' => $installed_date, |
| 184 |
'multisite' => is_multisite(), |
| 185 |
'url' => home_url(), |
| 186 |
'theme' => $theme, |
| 187 |
'email' => get_bloginfo('admin_email') |
| 188 |
); |
| 189 |
|
| 190 |
// Retrieve current plugin information. |
| 191 |
if (!function_exists('get_plugins')) { |
| 192 |
include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 193 |
} |
| 194 |
|
| 195 |
// Get plugins |
| 196 |
$plugins = array_keys(get_plugins()); |
| 197 |
$active_plugins = get_option('active_plugins', array()); |
| 198 |
|
| 199 |
// Remove active plugins from list so we can show active and inactive separately. |
| 200 |
foreach ($plugins as $key => $plugin) { |
| 201 |
if (in_array($plugin, $active_plugins, true)) { |
| 202 |
unset($plugins[$key]); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
$data['active_plugins'] = $active_plugins; |
| 207 |
$data['inactive_plugins'] = $plugins; |
| 208 |
$data['active_gateways'] = yatra_get_active_payment_gateways(); |
| 209 |
$data['tours'] = wp_count_posts('tour')->publish; |
| 210 |
$data['locale'] = get_locale(); |
| 211 |
return $data; |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Gather data to send to engine. |
| 217 |
* |
| 218 |
* @return array |
| 219 |
* @since 2.1.12 |
| 220 |
*/ |
| 221 |
private function get_data() |
| 222 |
{ |
| 223 |
$data = array(); |
| 224 |
$data['data'] = $this->get_data_items(); |
| 225 |
$data['admin_email'] = get_bloginfo('admin_email'); |
| 226 |
$user = get_user_by('email', $data['admin_email']); |
| 227 |
$data['nicename'] = $user->data->user_nicename; |
| 228 |
$data['site_url'] = get_bloginfo('url'); |
| 229 |
$data['version'] = get_bloginfo('version'); |
| 230 |
$data['sender'] = $this->slug; |
| 231 |
|
| 232 |
return $data; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Setup the data |
| 237 |
* |
| 238 |
* @access private |
| 239 |
* |
| 240 |
* @return void |
| 241 |
* @since 2.1.12 |
| 242 |
*/ |
| 243 |
private function setup_data() |
| 244 |
{ |
| 245 |
$data = array(); |
| 246 |
|
| 247 |
$data['secret_key'] = $this->get_opt_data($this->secret_opt_key); |
| 248 |
$data['validate_callback'] = rest_url(YATRA_REST_GENERAL_NAMESPACE . '/track'); |
| 249 |
if ($this->is_test_environment) { |
| 250 |
$data['validate_callback'] = ('http://localhost/wp-json/' . YATRA_REST_GENERAL_NAMESPACE . '/track'); |
| 251 |
} |
| 252 |
$data['agent_data'] = maybe_serialize($this->get_data()); |
| 253 |
$this->data = $data; |
| 254 |
} |
| 255 |
|
| 256 |
public function is_last_send_pass() |
| 257 |
{ |
| 258 |
$last_send = $this->get_last_send(); |
| 259 |
if (is_numeric($last_send) && $last_send !== '' && $last_send > strtotime('-1 week') && !$this->is_test_environment) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Send the data to the Engine server |
| 267 |
* |
| 268 |
* @access public |
| 269 |
* |
| 270 |
* @param bool $do_handshake If it is just handshake to get secret key. |
| 271 |
* |
| 272 |
* @return void|mixed |
| 273 |
* @since 2.1.12 |
| 274 |
*/ |
| 275 |
public function request_api($do_handshake = false) |
| 276 |
{ |
| 277 |
|
| 278 |
if (!$this->get_opt_data('allow_tracking')) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
/*Send a maximum of once per week*/ |
| 283 |
if (!$this->is_last_send_pass()) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
if ($do_handshake) { |
| 289 |
|
| 290 |
$this->api_url = $this->remote_url . 'handshake'; |
| 291 |
|
| 292 |
} else { |
| 293 |
|
| 294 |
$this->api_url = $this->remote_url . 'process'; |
| 295 |
|
| 296 |
$this->update_last_send(); |
| 297 |
} |
| 298 |
|
| 299 |
$this->setup_data(); |
| 300 |
|
| 301 |
$response_array = $this->send_request($this->api_url, $this->data); |
| 302 |
|
| 303 |
|
| 304 |
$has_secret_key = isset($response_array['secret_key']); |
| 305 |
|
| 306 |
if ($has_secret_key) { |
| 307 |
$this->update_secret_key($response_array['secret_key']); |
| 308 |
|
| 309 |
} |
| 310 |
return $response_array; |
| 311 |
} |
| 312 |
|
| 313 |
public function send_request($url, $data) |
| 314 |
{ |
| 315 |
|
| 316 |
$headers = array( |
| 317 |
'user-agent' => 'Yatra/' . YATRA_VERSION . '; ' . get_bloginfo('url'), |
| 318 |
); |
| 319 |
|
| 320 |
$response = wp_remote_post( |
| 321 |
$url, |
| 322 |
array( |
| 323 |
'method' => 'POST', |
| 324 |
'timeout' => 45, |
| 325 |
'redirection' => 5, |
| 326 |
'httpversion' => '1.0', |
| 327 |
'blocking' => true, |
| 328 |
'headers' => $headers, |
| 329 |
'body' => $data |
| 330 |
) |
| 331 |
); |
| 332 |
|
| 333 |
return json_decode(wp_remote_retrieve_body($response), true); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* While user allow the tracking |
| 338 |
* |
| 339 |
* @return array |
| 340 |
* @since 2.1.12 |
| 341 |
*/ |
| 342 |
public function after_tracking_enable($value, $option, $raw) |
| 343 |
{ |
| 344 |
if ($value != 'yes') { |
| 345 |
|
| 346 |
$this->update_opt_data('allow_tracking', false); |
| 347 |
|
| 348 |
} else { |
| 349 |
|
| 350 |
$this->update_opt_data('allow_tracking', true); |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
if ($this->get_opt_data('allow_tracking')) { |
| 355 |
|
| 356 |
$this->handshake_and_request_api(); |
| 357 |
} |
| 358 |
|
| 359 |
return $value; |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* When saving hide tracking notice. |
| 365 |
* |
| 366 |
* @return void |
| 367 |
*/ |
| 368 |
public function show_tracking_notice() |
| 369 |
{ |
| 370 |
|
| 371 |
// listen for our activate button to be clicked |
| 372 |
if (!isset($_GET[esc_attr($this->slug) . '_tracking'])) { |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
if (!current_user_can('manage_options')) { |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
/*Security check*/ |
| 381 |
check_admin_referer($this->slug); |
| 382 |
|
| 383 |
if (1 == $_GET[esc_attr($this->slug) . '_tracking']) { |
| 384 |
$this->update_opt_data('allow_tracking', true); |
| 385 |
$this->handshake_and_request_api(); |
| 386 |
} else { |
| 387 |
$this->update_opt_data('allow_tracking', false); |
| 388 |
} |
| 389 |
|
| 390 |
$this->update_hide_tracking_notice(true); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Schedule a weekly tracking |
| 395 |
* |
| 396 |
* @return array|false|string |
| 397 |
* @since 2.1.12 |
| 398 |
*/ |
| 399 |
public function schedule_send() |
| 400 |
{ |
| 401 |
|
| 402 |
if (!wp_doing_cron()) { |
| 403 |
return; |
| 404 |
} |
| 405 |
if (!$this->get_opt_data('allow_tracking')) { |
| 406 |
return; |
| 407 |
} |
| 408 |
add_action('yatra_weekly_scheduled_events', array($this, 'handshake_and_request_api')); |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Update last send |
| 414 |
* |
| 415 |
* @return void |
| 416 |
* @since 2.1.12 |
| 417 |
*/ |
| 418 |
public function update_last_send() |
| 419 |
{ |
| 420 |
|
| 421 |
$this->update_opt_data($this->last_send_opt_key, time()); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Get last send |
| 426 |
* |
| 427 |
* @return string |
| 428 |
* @since 2.1.12 |
| 429 |
*/ |
| 430 |
public function get_last_send() |
| 431 |
{ |
| 432 |
return $this->get_opt_data($this->last_send_opt_key); |
| 433 |
|
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Update hide notice |
| 438 |
* |
| 439 |
* @return void |
| 440 |
* @since 2.1.12 |
| 441 |
*/ |
| 442 |
public function update_hide_tracking_notice($val = false) |
| 443 |
{ |
| 444 |
$this->update_opt_data($this->hide_notice_opt_key, $val); |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Get hide notice |
| 449 |
* |
| 450 |
* @return boolean |
| 451 |
* @since 2.1.12 |
| 452 |
*/ |
| 453 |
public function get_hide_tracking_notice() |
| 454 |
{ |
| 455 |
return $this->get_opt_data($this->hide_notice_opt_key); |
| 456 |
|
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Check if we can show tracking notice to user. |
| 461 |
* |
| 462 |
* @return boolean |
| 463 |
* @since 2.1.12 |
| 464 |
*/ |
| 465 |
public function can_show_notice() |
| 466 |
{ |
| 467 |
|
| 468 |
if (get_option('yatra_install_date', 0) > strtotime('-3 day')) { |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
if ($this->get_hide_tracking_notice()) { |
| 474 |
return false; |
| 475 |
} |
| 476 |
if ($this->get_opt_data('allow_tracking')) { |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
if (!current_user_can('manage_yatra')) { |
| 481 |
return false; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
return true; |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Get current admin page URL. |
| 491 |
* |
| 492 |
* Returns an empty string if it cannot generate a URL. |
| 493 |
* |
| 494 |
* @return string |
| 495 |
* @since 2.1.12 |
| 496 |
*/ |
| 497 |
private function get_current_admin_url() |
| 498 |
{ |
| 499 |
$uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : ''; |
| 500 |
$uri = preg_replace('|^.*/wp-admin/|i', '', $uri); |
| 501 |
|
| 502 |
if (!$uri) { |
| 503 |
return ''; |
| 504 |
} |
| 505 |
|
| 506 |
return remove_query_arg(array('_wpnonce'), admin_url($uri)); |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* Display the admin notice to users. |
| 511 |
* |
| 512 |
* @return void |
| 513 |
* @since 2.1.12 |
| 514 |
*/ |
| 515 |
public function admin_notice() |
| 516 |
{ |
| 517 |
if (!$this->can_show_notice()) { |
| 518 |
return; |
| 519 |
} |
| 520 |
$allow_url = wp_nonce_url( |
| 521 |
add_query_arg( |
| 522 |
array( |
| 523 |
esc_attr($this->slug) . '_tracking' => 1, |
| 524 |
), |
| 525 |
$this->get_current_admin_url() |
| 526 |
), |
| 527 |
$this->slug |
| 528 |
); |
| 529 |
$not_allow_url = wp_nonce_url( |
| 530 |
add_query_arg( |
| 531 |
array( |
| 532 |
esc_attr($this->slug) . '_tracking' => 0, |
| 533 |
), |
| 534 |
$this->get_current_admin_url() |
| 535 |
), |
| 536 |
$this->slug |
| 537 |
); |
| 538 |
ob_start(); |
| 539 |
?> |
| 540 |
<p> |
| 541 |
<strong>Help us to improve Yatra!</strong></p> |
| 542 |
<p>Allow Yatra to anonymously track how this plugin is used and help us to make the plugin better. </p> |
| 543 |
<p>No sensitive data is tracked.</p> |
| 544 |
<p><a href="<?php echo esc_attr($allow_url) ?>" class="button-secondary">Allow</a> <a |
| 545 |
href="<?php echo esc_attr($not_allow_url) ?>" class="button-secondary">Do not allow</a></p> |
| 546 |
|
| 547 |
<?php |
| 548 |
|
| 549 |
$notice_html = ob_get_clean(); |
| 550 |
|
| 551 |
Notices::info( |
| 552 |
$notice_html, |
| 553 |
[ |
| 554 |
'dismiss' => Notices::DISMISS_NONE, |
| 555 |
'slug' => 'usage_tracking', |
| 556 |
'autop' => true, |
| 557 |
'class' => 'yatra-usage-tracking-notice', |
| 558 |
] |
| 559 |
); |
| 560 |
} |
| 561 |
} |
| 562 |
|