PluginProbe ʕ •ᴥ•ʔ
Advanced Import / trunk
Advanced Import vtrunk
trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 2.0.0
advanced-import / admin / class-advanced-import-tracking.php
advanced-import / admin Last commit date
class-advanced-import-admin.php 3 weeks ago class-advanced-import-template.php 3 months ago class-advanced-import-tracking.php 3 weeks ago class-elementor-import.php 3 weeks ago class-reset.php 3 weeks ago index.php 5 years ago
class-advanced-import-tracking.php
526 lines
1 <?php
2 /**
3 * Tracking functions for reporting plugin usage to the Engine site for users that have opted in
4 *
5 * @package Advanced Import
6 * @subpackage Admin
7 * @since 1.3.6
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Agent Usage tracking
17 *
18 * @since 1.3.6
19 * @return void
20 */
21 class Advanced_Import_Tracking {
22
23 /**
24 * @access private
25 */
26 private $slug = '';
27 private $secret_opt_key = '';
28 private $last_send_opt_key = '';
29 private $hide_notice_opt_key = '';
30 private $agents_opt_key = '';
31 private $agent_active_opt_key = '';
32 private $data;
33 private $api_url = '';
34 private $remote_url = '';
35 private $version = '';
36
37 /**
38 * Get things going
39 * allow-tracking is set from $this->get_opt_data()
40 * Other from opt key
41 *
42 * @since 1.3.6
43 * @return void
44 */
45 public function __construct() {
46
47 /*Changed with the plugin*/
48 $this->remote_url = trailingslashit( 'https://tracking.acmeit.org' );
49 $this->slug = ADVANCED_IMPORT_PLUGIN_NAME;
50 $this->version = '1.0.0';
51 /*Changed with the plugin end*/
52
53 $this->secret_opt_key = 'agent_secret_key';
54 $this->last_send_opt_key = 'agent_last_send';
55 $this->hide_notice_opt_key = 'agent_hide_notice';
56 $this->agent_active_opt_key = 'is_active_this_track';
57
58 $this->agents_opt_key = ADVANCED_IMPORT_PLUGIN_NAME . '-agent-' . md5( $this->remote_url );/*unique per remote url*/
59
60 $this->init();
61
62 }
63
64 /**
65 * Set up WordPress hooks.
66 *
67 * @since 1.3.6
68 * @return void
69 */
70 public function init() {
71
72 add_action( 'init', array( $this, 'schedule_send' ) );
73 add_action( 'admin_init', array( $this, 'do_agents' ) );
74 add_action( 'admin_init', array( $this, 'do_show_tracking_notice' ) );
75 add_action( 'admin_notices', array( $this, 'admin_notice' ) );
76 }
77
78 /**
79 * Update agents.
80 * Run once in the lifetime.
81 *
82 * @since 1.3.6
83 * @return void
84 */
85 public function do_agents() {
86
87 $installed_agents = get_option( $this->agents_opt_key, array() );
88 if ( isset( $installed_agents[ $this->slug ] ) ) {
89 return;
90 }
91
92 $installed_agents[ $this->slug ] = $this->version;
93
94 $active_agent = $this->get_opt_data( $this->agent_active_opt_key, '' );
95
96 if ( ! $active_agent ) {
97 $active_agent = $this->slug;
98 } else {
99 if ( is_array( $installed_agents ) && ! empty( $installed_agents ) ) {
100 $highest_ver = $this->version;
101 foreach ( $installed_agents as $agent => $agent_ver ) {
102 if ( version_compare( $agent_ver, $highest_ver ) > 0 ) {
103 $highest_ver = $agent_ver;
104 $active_agent = $agent;
105 }
106 }
107 }
108 }
109
110 advanced_import_add_installed_time();
111
112 // register this agent locally.
113 $this->update_opt_data( $this->agent_active_opt_key, $active_agent );
114
115 // register agent data globally.
116 update_option( $this->agents_opt_key, $installed_agents );
117 }
118
119 /**
120 * Is this active agent
121 *
122 * @since 1.3.6
123 * @return boolean
124 */
125 private function is_active_agent() {
126 if ( $this->slug == $this->get_opt_data( $this->agent_active_opt_key ) ) {
127 return true;
128 }
129 return false;
130
131 }
132
133 /**
134 * Update secret keyy
135 *
136 * @since 1.3.6
137 * @return void
138 */
139 private function update_secret_key( $res ) {
140 // get secret key from engine.
141 $get_secret_key = json_decode( $res, true );
142 $secret_key = 'none';
143 if ( $get_secret_key && is_array( $get_secret_key ) && isset( $get_secret_key['secret_key'] ) ) {
144 $secret_key = $get_secret_key['secret_key'];
145 }
146 $this->update_opt_data( $this->secret_opt_key, sanitize_text_field( $secret_key ) );
147 }
148
149 /**
150 * Authorize this site to send data to engine.
151 * get secret key from engine
152 * run on agent activation.
153 *
154 * @since 1.3.6
155 * @return void
156 */
157 public function do_handshake() {
158 $secret_key = $this->get_opt_data( $this->secret_opt_key );
159
160 if ( ! empty( $secret_key ) ) {
161 // secret_key already exists.
162 // do nothing.
163 return;
164 }
165
166 // authenticate with engine.
167 $this->api_url = $this->remote_url . 'wp-json/acme-udp-admin/v1/handshake';
168
169 $get_secret_key = $this->do_post( true, true );
170
171 $this->update_secret_key( $get_secret_key );
172
173 }
174
175 /**
176 * Default Options
177 *
178 * @return array $advanced_import_default_options
179 *
180 * @since 1.3.6
181 */
182 private function default_options() {
183 return array(
184 'allow-tracking' => false,
185 );
186 }
187
188 /**
189 * Get option.
190 *
191 * @since 1.3.6
192 * @return array
193 */
194 private function get_opt_data( $key = '' ) {
195 $advanced_import_options = json_decode( get_option( 'advanced_import_settings_options' ), true );
196
197 $advanced_import_default_options = $this->default_options();
198 if ( ! empty( $key ) ) {
199 if ( isset( $advanced_import_options[ $key ] ) ) {
200 return $advanced_import_options[ $key ];
201 }
202 return isset( $advanced_import_default_options[ $key ] ) ? $advanced_import_default_options[ $key ] : '';
203 } else {
204 if ( ! is_array( $advanced_import_options ) ) {
205 $advanced_import_options = array();
206 }
207 return array_merge( $advanced_import_default_options, $advanced_import_options );
208 }
209 }
210
211 /**
212 * Update options.
213 *
214 * @since 1.3.6
215 * @return array
216 */
217 private function update_opt_data( $key, $val ) {
218 $helper_options = json_decode( get_option( 'advanced_import_settings_options' ), true );
219 if ( ! is_array( $helper_options ) ) {
220 $helper_options = array();
221 }
222 $helper_options[ $key ] = $val;
223 update_option(
224 'advanced_import_settings_options',
225 wp_json_encode( $helper_options )
226 );
227 }
228
229 /**
230 * Gather data to send to engine.
231 *
232 * @since 1.3.6
233 * @return array
234 */
235 private function get_data() {
236
237 if ( ! class_exists( 'WP_Debug_Data' ) ) {
238 include_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
239 }
240 $data = array();
241
242 if ( method_exists( 'WP_Debug_Data', 'debug_data' ) ) {
243 $data['data'] = WP_Debug_Data::debug_data();
244 } else {
245 $data['data'] = array();
246 }
247 $data['admin_email'] = get_bloginfo( 'admin_email' );
248 $user = get_user_by( 'email', $data['admin_email'] );
249 $data['nicename'] = $user->data->user_nicename;
250 $data['site_url'] = get_bloginfo( 'url' );
251 $data['version'] = get_bloginfo( 'version' );
252
253 $data['sender'] = $this->slug;
254
255 return $data;
256 }
257
258 /**
259 * Setup the data
260 *
261 * @access private
262 *
263 * @since 1.3.6
264 * @return void
265 */
266 private function setup_data() {
267 $data = array();
268 $data['agent_data'] = maybe_serialize( $this->get_data() );
269 $data['secret_key'] = $this->get_opt_data( $this->secret_opt_key );
270 $this->data = $data;
271 }
272
273 /**
274 * Send the data to the Engine server
275 *
276 * @access public
277 *
278 * @param bool $override If we should override the tracking setting.
279 * @param bool $is_handshake If it is just handshake to get secret key.
280 *
281 * @since 1.3.6
282 * @return bool
283 */
284 public function do_post( $override = false, $is_handshake = false ) {
285
286 if ( ! $this->get_opt_data( 'allow-tracking' ) && ! $override ) {
287 return false;
288 }
289
290 /*Send a maximum of once per week*/
291 $last_send = $this->get_last_send();
292 if ( is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) && ! $is_handshake ) {
293 return false;
294 }
295
296 /*if this agent is not active agent*/
297 if ( ! $this->is_active_agent() ) {
298 return false;
299 }
300
301 if ( ! $is_handshake ) {
302 $this->api_url = $this->remote_url . 'wp-json/acme-udp-admin/v1/process-data';
303 $this->update_last_send();
304 }
305
306 $this->setup_data();
307 $response = wp_safe_remote_post(
308 $this->api_url,
309 array(
310 'method' => 'POST',
311 'timeout' => 45,
312 'redirection' => 5,
313 'httpversion' => '1.0',
314 'blocking' => true,
315 'headers' => array(),
316 'body' => $this->data,
317 )
318 );
319
320 if ( $is_handshake ) {
321 return wp_remote_retrieve_body( $response );
322 } else {
323 $is_secret_key = json_decode( wp_remote_retrieve_body( $response ), true );
324 if ( is_array( $is_secret_key ) && isset( $is_secret_key['secret_key'] ) ) {
325 $this->update_secret_key( wp_remote_retrieve_body( $response ) );
326 }
327 }
328
329 }
330
331 /**
332 * When saving hide tracking notice.
333 *
334 * @return void
335 */
336 public function do_show_tracking_notice() {
337
338 $this->do_handshake();
339
340 // listen for our activate button to be clicked
341 if ( ! isset( $_GET[ esc_attr( $this->slug ) . '_tracking' ] ) ) {
342 return;
343 }
344
345 if ( ! current_user_can( 'manage_options' ) ) {
346 return;
347 }
348
349 /*Security check*/
350 check_admin_referer( $this->slug );
351
352 if ( 1 == $_GET[ esc_attr( $this->slug ) . '_tracking' ] ) {
353 $this->update_opt_data( 'allow-tracking', true );
354 $this->do_post( true );
355 } else {
356 $this->update_opt_data( 'allow-tracking', false );
357 }
358
359 $this->update_hide_tracking_notice( true );
360 }
361
362 /**
363 * Schedule a weekly tracking
364 *
365 * @since 1.3.6
366 * @return void
367 */
368 public function schedule_send() {
369 if ( wp_doing_cron() ) {
370 add_action( 'advanced_import_weekly_scheduled_events', array( $this, 'do_post' ) );
371 }
372 }
373
374 /**
375 * Update last send
376 *
377 * @since 1.3.6
378 * @return void
379 */
380 public function update_last_send() {
381
382 $this->update_opt_data( $this->last_send_opt_key, time() );
383 }
384
385 /**
386 * Get last send
387 *
388 * @since 1.3.6
389 * @return string
390 */
391 public function get_last_send() {
392 return $this->get_opt_data( $this->last_send_opt_key );
393
394 }
395
396 /**
397 * Update hide notice
398 *
399 * @since 1.3.6
400 * @return void
401 */
402 public function update_hide_tracking_notice( $val = false ) {
403 $this->update_opt_data( $this->hide_notice_opt_key, $val );
404 }
405
406 /**
407 * Get hide notice
408 *
409 * @since 1.3.6
410 * @return boolean
411 */
412 public function get_hide_tracking_notice() {
413 return $this->get_opt_data( $this->hide_notice_opt_key );
414
415 }
416
417 /**
418 * Check if we can show tracking notice to user.
419 *
420 * @since 1.3.6
421 * @return boolean
422 */
423 public function can_show_notice() {
424
425 if ( $this->get_opt_data( 'installed_time' ) > strtotime( '-3 day' ) ) {
426 return false;
427 }
428
429 if ( $this->get_hide_tracking_notice() ) {
430 return false;
431 }
432 if ( $this->get_opt_data( 'allow-tracking' ) ) {
433 return false;
434 }
435 if ( ! current_user_can( 'manage_options' ) ) {
436 return false;
437 }
438 /*if this agent is not active agent*/
439 if ( ! $this->is_active_agent() ) {
440 return false;
441 }
442 return true;
443
444 }
445
446 /**
447 * Get current admin page URL.
448 *
449 * Returns an empty string if it cannot generate a URL.
450 *
451 * @since 1.3.6
452 * @return string
453 */
454 private function get_current_admin_url() {
455 $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
456 $uri = preg_replace( '|^.*/wp-admin/|i', '', $uri );
457
458 if ( ! $uri ) {
459 return '';
460 }
461
462 return remove_query_arg( array( '_wpnonce' ), admin_url( $uri ) );
463 }
464
465 /**
466 * Display the admin notice to users.
467 *
468 * @return void
469 * @since 1.3.6
470 */
471 public function admin_notice( $is_help = false ) {
472
473 if ( ! $this->can_show_notice() ) {
474 return;
475 }
476 global $current_user;
477
478 $allow_url = wp_nonce_url(
479 add_query_arg(
480 array(
481 esc_attr( $this->slug ) . '_tracking' => 1,
482 ),
483 $this->get_current_admin_url()
484 ),
485 $this->slug
486 );
487 $not_allow_url = wp_nonce_url(
488 add_query_arg(
489 array(
490 esc_attr( $this->slug ) . '_tracking' => 0,
491 ),
492 $this->get_current_admin_url()
493 ),
494 $this->slug
495 );
496 ?>
497 <div class="<?php echo $is_help ? '' : 'notice updated '; ?><?php echo esc_attr( $this->slug ); ?>-track-notice">
498 <p style="float: left">
499 <?php
500 printf(
501 /* Translators: %1$s current user display name. */
502 esc_html__(
503 'Howdy, %1$s! Allow Advanced Import to anonymously track how this plugin is used and help us make the plugin better. No sensitive data is tracked.',
504 'advanced-import'
505 ),
506 '<strong>' . esc_html( $current_user->display_name ) . '</strong>'
507 );
508 ?>
509 </p>
510
511
512 <a href="<?php echo esc_url( $allow_url ); ?>" class="btn button-primary">
513 <span><?php esc_html_e( 'Allow', 'advanced-import' ); ?></span>
514 </a>
515 <a href="<?php echo esc_url( $not_allow_url ); ?>" class="btn button-link">
516 <span><?php esc_html_e( 'Do not allow', 'advanced-import' ); ?></span>
517 </a>
518
519 </div>
520
521 <?php
522 }
523 }
524 global $advanced_import_tracking;
525 $advanced_import_tracking = new Advanced_Import_Tracking();
526