PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.5
Pods – Custom Content Types and Fields v2.8.23.5
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / Wisdom_Tracker.php
pods / src / Pods Last commit date
API 2 days ago Admin 2 days ago Blocks 2 days ago CLI 2 days ago Data 2 days ago Integrations 2 days ago REST 2 days ago Theme 2 days ago Whatsit 2 days ago Integration.php 2 days ago Permissions.php 2 days ago Service_Provider.php 2 days ago Static_Cache.php 2 days ago Whatsit.php 2 days ago Wisdom_Tracker.php 2 days ago
Wisdom_Tracker.php
848 lines
1 <?php
2
3 namespace Pods;
4
5 /**
6 * Wisdom Tracker class.
7 *
8 * @link https://wisdomplugin.com/
9 *
10 * @since 2.8.0
11 */
12 class Wisdom_Tracker {
13
14 private $wisdom_version = '1.2.5';
15 private $home_url = '';
16 private $plugin_file = '';
17 private $plugin_name = '';
18 private $options = [];
19 private $require_optin = true;
20 private $include_goodbye_form = true;
21 private $marketing = false;
22 private $collect_email = false;
23 private $what_am_i = 'plugin';
24 private $theme_allows_tracking = 0;
25
26 /**
27 * Class constructor
28 *
29 * @param $_home_url The URL to the site we're sending data to
30 * @param $_plugin_slug The slug for this plugin (SKC modification for Pods).
31 * @param $_plugin_file The file path for this plugin
32 * @param $_options Plugin options to track
33 * @param $_require_optin Whether user opt-in is required (always required on WordPress.org)
34 * @param $_include_goodbye_form Whether to include a form when the user deactivates
35 * @param $_marketing Marketing method:
36 * 0: Don't collect email addresses
37 * 1: Request permission same time as tracking opt-in
38 * 2: Request permission after opt-in
39 */
40 public function __construct(
41 $_plugin_file, $_plugin_slug, $_home_url, $_options, $_require_optin = true, $_include_goodbye_form = true, $_marketing = false
42 ) {
43 $this->plugin_file = $_plugin_file;
44 $this->home_url = trailingslashit( $_home_url );
45
46 // If the filename is 'functions' then we're tracking a theme
47 if ( basename( $this->plugin_file, '.php' ) != 'functions' ) {
48 $this->plugin_name = basename( $this->plugin_file, '.php' );
49
50 // SKC modification for Pods.
51 if ( ! empty( $_plugin_slug ) ) {
52 $this->plugin_name = $_plugin_slug;
53 }
54 } else {
55 $this->what_am_i = 'theme';
56 $theme = wp_get_theme();
57 if ( $theme->Name ) {
58 $this->plugin_name = sanitize_text_field( $theme->Name );
59 }
60 }
61
62 $this->options = $_options;
63 $this->require_optin = $_require_optin;
64 $this->include_goodbye_form = $_include_goodbye_form;
65 $this->marketing = $_marketing;
66
67 // Only use this on switching theme
68 $this->theme_allows_tracking = get_theme_mod( 'wisdom-allow-tracking', 0 );
69
70 // Schedule / deschedule tracking when activated / deactivated
71 if ( $this->what_am_i == 'theme' ) {
72 // Need to think about scheduling for sites that have already activated the theme
73 add_action( 'after_switch_theme', [ $this, 'schedule_tracking' ] );
74 add_action( 'switch_theme', [ $this, 'deactivate_this_plugin' ] );
75 } else {
76 register_activation_hook( $this->plugin_file, [ $this, 'schedule_tracking' ] );
77 register_deactivation_hook( $this->plugin_file, [ $this, 'deactivate_this_plugin' ] );
78 }
79
80 // Get it going
81 $this->init();
82 }
83
84 public function init() {
85 // Check marketing
86 if ( $this->marketing == 3 ) {
87 $this->set_can_collect_email( true, $this->plugin_name );
88 }
89
90 // Check whether opt-in is required
91 // If not, then tracking is allowed
92 if ( ! $this->require_optin ) {
93 $this->set_can_collect_email( true, $this->plugin_name );
94 $this->set_is_tracking_allowed( true );
95 $this->update_block_notice();
96 $this->do_tracking();
97 }
98
99 // Hook our do_tracking function to the weekly action
100 add_filter( 'cron_schedules', [ $this, 'schedule_weekly_event' ] );
101 // It's called weekly, but in fact it could be daily, weekly or monthly
102 add_action( 'put_do_weekly_action', [ $this, 'do_tracking' ] );
103
104 // Use this action for local testing
105 // add_action( 'admin_init', array( $this, 'do_tracking' ) );
106
107 // Display the admin notice on activation
108 add_action( 'admin_init', [ $this, 'set_notification_time' ] );
109 add_action( 'admin_notices', [ $this, 'optin_notice' ] );
110 }
111
112 /**
113 * When the plugin is activated
114 * Create scheduled event
115 * And check if tracking is enabled - perhaps the plugin has been reactivated
116 *
117 * @since 1.0.0
118 */
119 public function schedule_tracking() {
120 if ( ! wp_next_scheduled( 'put_do_weekly_action' ) ) {
121 $schedule = $this->get_schedule();
122 wp_schedule_event( time(), $schedule, 'put_do_weekly_action' );
123 }
124 $this->do_tracking( true );
125 }
126
127 /**
128 * Create weekly schedule
129 *
130 * @since 1.2.3
131 */
132 public function schedule_weekly_event( $schedules ) {
133 $schedules['weekly'] = [
134 'interval' => 604800,
135 'display' => __( 'Once Weekly' ),
136 ];
137 $schedules['monthly'] = [
138 'interval' => 2635200,
139 'display' => __( 'Once Monthly' ),
140 ];
141
142 return $schedules;
143 }
144
145 /**
146 * Get how frequently data is tracked back
147 *
148 * @since 1.2.3
149 */
150 public function get_schedule() {
151 // Could be daily, weekly or monthly
152 $schedule = apply_filters( 'wisdom_filter_schedule_' . $this->plugin_name, 'monthly' );
153
154 return $schedule;
155 }
156
157 /**
158 * This is our function to get everything going
159 * Check that user has opted in
160 * Collect data
161 * Then send it back
162 *
163 * @since 1.0.0
164 *
165 * @param $force Force tracking if it's not time
166 */
167 public function do_tracking( $force = false ) {
168 // If the home site hasn't been defined, we just drop out. Nothing much we can do.
169 if ( ! $this->home_url ) {
170 return;
171 }
172
173 // Check to see if the user has opted in to tracking
174 $allow_tracking = $this->get_is_tracking_allowed();
175 if ( ! $allow_tracking ) {
176 return;
177 }
178
179 // Check to see if it's time to track
180 $track_time = $this->get_is_time_to_track();
181 if ( ! $track_time && ! $force ) {
182 return;
183 }
184
185 $this->set_admin_email();
186
187 // Get our data
188 $body = $this->get_data();
189
190 // Send the data
191 $this->send_data( $body );
192 }
193
194 /**
195 * We hook this to admin_init when the user accepts tracking
196 * Ensures the email address is available
197 *
198 * @since 1.2.1
199 */
200 public function force_tracking() {
201 $this->do_tracking( true ); // Run this straightaway
202 }
203
204 /**
205 * Send the data to the home site
206 *
207 * @since 1.0.0
208 */
209 public function send_data( $body ) {
210 $request = wp_remote_post( esc_url( $this->home_url . '?usage_tracker=hello' ), [
211 'method' => 'POST',
212 'timeout' => 20,
213 'redirection' => 5,
214 'httpversion' => '1.1',
215 'blocking' => true,
216 'body' => $body,
217 'user-agent' => 'PUT/1.0.0; ' . home_url(),
218 ] );
219
220 $this->set_track_time();
221
222 if ( is_wp_error( $request ) ) {
223 return $request;
224 }
225 }
226
227 /**
228 * Here we collect most of the data
229 *
230 * @since 1.0.0
231 */
232 public function get_data() {
233 // Use this to pass error messages back if necessary
234 $body['message'] = '';
235
236 // Use this array to send data back
237 $body = [
238 'plugin_slug' => sanitize_text_field( $this->plugin_name ),
239 'url' => home_url(),
240 'site_name' => get_bloginfo( 'name' ),
241 'site_version' => get_bloginfo( 'version' ),
242 'site_language' => get_bloginfo( 'language' ),
243 'charset' => get_bloginfo( 'charset' ),
244 'wisdom_version' => $this->wisdom_version,
245 'php_version' => phpversion(),
246 'multisite' => is_multisite(),
247 'file_location' => __FILE__,
248 'product_type' => esc_html( $this->what_am_i ),
249 ];
250
251 // Collect the email if the correct option has been set
252 if ( $this->get_can_collect_email() ) {
253 $body['email'] = $this->get_admin_email();
254 }
255 $body['marketing_method'] = $this->marketing;
256
257 $body['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
258
259 // Extra PHP fields
260 $body['memory_limit'] = ini_get( 'memory_limit' );
261 $body['upload_max_size'] = ini_get( 'upload_max_size' );
262 $body['post_max_size'] = ini_get( 'post_max_size' );
263 $body['upload_max_filesize'] = ini_get( 'upload_max_filesize' );
264 $body['max_execution_time'] = ini_get( 'max_execution_time' );
265 $body['max_input_time'] = ini_get( 'max_input_time' );
266
267 // Retrieve current plugin information
268 if ( ! function_exists( 'get_plugins' ) ) {
269 include ABSPATH . '/wp-admin/includes/plugin.php';
270 }
271
272 $plugins = array_keys( get_plugins() );
273 $active_plugins = get_option( 'active_plugins', [] );
274
275 foreach ( $plugins as $key => $plugin ) {
276 if ( in_array( $plugin, $active_plugins ) ) {
277 // Remove active plugins from list so we can show active and inactive separately
278 unset( $plugins[ $key ] );
279 }
280 }
281
282 $body['active_plugins'] = $active_plugins;
283 $body['inactive_plugins'] = $plugins;
284
285 // Check text direction
286 $body['text_direction'] = 'LTR';
287 if ( function_exists( 'is_rtl' ) ) {
288 if ( is_rtl() ) {
289 $body['text_direction'] = 'RTL';
290 }
291 } else {
292 $body['text_direction'] = 'not set';
293 }
294
295 /**
296 * Get our plugin data
297 * Currently we grab plugin name and version
298 * Or, return a message if the plugin data is not available
299 *
300 * @since 1.0.0
301 */
302 $plugin = $this->plugin_data();
303 $body['status'] = 'Active'; // Never translated
304 if ( empty( $plugin ) ) {
305 // We can't find the plugin data
306 // Send a message back to our home site
307 $body['message'] .= __( 'We can\'t detect any product information. This is most probably because you have not included the code snippet.', 'pods' );
308 $body['status'] = 'Data not found'; // Never translated
309 } else {
310 if ( isset( $plugin['Name'] ) ) {
311 $body['plugin'] = sanitize_text_field( $plugin['Name'] );
312 }
313 if ( isset( $plugin['Version'] ) ) {
314 $body['version'] = sanitize_text_field( $plugin['Version'] );
315 }
316 }
317
318 /**
319 * Get our plugin options
320 *
321 * @since 1.0.0
322 */
323 $options = $this->options;
324 $plugin_options = [];
325 if ( ! empty( $options ) && is_array( $options ) ) {
326 foreach ( $options as $option ) {
327 $fields = get_option( $option );
328 // Check for permission to send this option
329 if ( isset( $fields['wisdom_registered_setting'] ) ) {
330 foreach ( $fields as $key => $value ) {
331 $plugin_options[ $key ] = $value;
332 }
333 }
334 }
335 }
336 $body['plugin_options'] = $this->options; // Returns array
337 $body['plugin_options_fields'] = $plugin_options; // Returns object
338
339 /**
340 * Get our theme data
341 * Currently we grab theme name and version
342 *
343 * @since 1.0.0
344 */
345 $theme = wp_get_theme();
346 if ( $theme->Name ) {
347 $body['theme'] = sanitize_text_field( $theme->Name );
348 }
349 if ( $theme->Version ) {
350 $body['theme_version'] = sanitize_text_field( $theme->Version );
351 }
352 if ( $theme->Template ) {
353 $body['theme_parent'] = sanitize_text_field( $theme->Template );
354 }
355
356 // Return the data
357 return $body;
358 }
359
360 /**
361 * Return plugin data
362 *
363 * @since 1.0.0
364 */
365 public function plugin_data() {
366 // Being cautious here
367 if ( ! function_exists( 'get_plugin_data' ) ) {
368 include ABSPATH . '/wp-admin/includes/plugin.php';
369 }
370 // Retrieve current plugin information
371 $plugin = get_plugin_data( $this->plugin_file );
372
373 return $plugin;
374 }
375
376 /**
377 * Deactivating plugin
378 *
379 * @since 1.0.0
380 */
381 public function deactivate_this_plugin() {
382 // Check to see if the user has opted in to tracking
383 if ( $this->what_am_i == 'theme' ) {
384 $allow_tracking = $this->theme_allows_tracking;
385 } else {
386 $allow_tracking = $this->get_is_tracking_allowed();
387 }
388
389 if ( ! $allow_tracking ) {
390 return;
391 }
392
393 $body = $this->get_data();
394 $body['status'] = 'Deactivated'; // Never translated
395 $body['deactivated_date'] = time();
396
397 // Add deactivation form data
398 if ( false !== get_option( 'wisdom_deactivation_reason_' . $this->plugin_name ) ) {
399 $body['deactivation_reason'] = get_option( 'wisdom_deactivation_reason_' . $this->plugin_name );
400 }
401 if ( false !== get_option( 'wisdom_deactivation_details_' . $this->plugin_name ) ) {
402 $body['deactivation_details'] = get_option( 'wisdom_deactivation_details_' . $this->plugin_name );
403 }
404
405 $this->send_data( $body );
406 // Clear scheduled update
407 wp_clear_scheduled_hook( 'put_do_weekly_action' );
408
409 // Clear the wisdom_last_track_time value for this plugin
410 // @since 1.2.2
411 $track_time = get_option( 'wisdom_last_track_time' );
412 if ( isset( $track_time[ $this->plugin_name ] ) ) {
413 unset( $track_time[ $this->plugin_name ] );
414 }
415 update_option( 'wisdom_last_track_time', $track_time );
416 }
417
418 /**
419 * Is tracking allowed?
420 *
421 * @since 1.0.0
422 */
423 public function get_is_tracking_allowed() {
424 // First, check if the user has changed their mind and opted out of tracking
425 if ( $this->has_user_opted_out() ) {
426 $this->set_is_tracking_allowed( false, $this->plugin_name );
427
428 // SKC modification for Pods.
429 $this->set_can_collect_email( false, $this->plugin_name );
430
431 return false;
432 }
433
434 if ( $this->what_am_i == 'theme' ) {
435 $mod = get_theme_mod( 'wisdom-allow-tracking', 0 );
436 if ( $mod ) {
437 return true;
438 }
439 } else {
440 // The wisdom_allow_tracking option is an array of plugins that are being tracked
441 $allow_tracking = get_option( 'wisdom_allow_tracking' );
442 // If this plugin is in the array, then tracking is allowed
443 if ( isset( $allow_tracking[ $this->plugin_name ] ) ) {
444 return true;
445 }
446 }
447
448 return false;
449 }
450
451 /**
452 * Set if tracking is allowed
453 * Option is an array of all plugins with tracking permitted
454 * More than one plugin may be using the tracker
455 *
456 * @since 1.0.0
457 *
458 * @param $is_allowed Boolean true if tracking is allowed, false if not
459 */
460 public function set_is_tracking_allowed( $is_allowed, $plugin = null ) {
461 if ( empty( $plugin ) ) {
462 $plugin = $this->plugin_name;
463 }
464
465 // The wisdom_allow_tracking option is an array of plugins that are being tracked
466 $allow_tracking = get_option( 'wisdom_allow_tracking' );
467
468 // If the user has decided to opt out
469 if ( $this->has_user_opted_out() ) {
470 if ( $this->what_am_i == 'theme' ) {
471 set_theme_mod( 'wisdom-allow-tracking', 0 );
472 } else {
473 if ( isset( $allow_tracking[ $plugin ] ) ) {
474 unset( $allow_tracking[ $plugin ] );
475 }
476 }
477 } elseif ( $is_allowed || ! $this->require_optin ) {
478 // If the user has agreed to allow tracking or if opt-in is not required
479
480 if ( $this->what_am_i == 'theme' ) {
481 set_theme_mod( 'wisdom-allow-tracking', 1 );
482 } else {
483 if ( empty( $allow_tracking ) || ! is_array( $allow_tracking ) ) {
484 // If nothing exists in the option yet, start a new array with the plugin name
485 $allow_tracking = [ $plugin => $plugin ];
486 } else {
487 // Else add the plugin name to the array
488 $allow_tracking[ $plugin ] = $plugin;
489 }
490 }
491 } else {
492 if ( $this->what_am_i == 'theme' ) {
493 set_theme_mod( 'wisdom-allow-tracking', 0 );
494 } else {
495 if ( isset( $allow_tracking[ $plugin ] ) ) {
496 unset( $allow_tracking[ $plugin ] );
497 }
498 }
499 }
500
501 update_option( 'wisdom_allow_tracking', $allow_tracking );
502 }
503
504 /**
505 * Has the user opted out of allowing tracking?
506 * Note that themes are opt in / plugins are opt out
507 *
508 * @since 1.1.0
509 * @return Boolean
510 */
511 public function has_user_opted_out() {
512 // Different opt-out methods for plugins and themes
513 if ( $this->what_am_i == 'theme' ) {
514 // Look for the theme mod
515 $mod = get_theme_mod( 'wisdom-allow-tracking', 0 );
516 if ( false === $mod ) {
517 // If the theme mod is not set, then return true - the user has opted out
518 return true;
519 }
520 } else {
521 // Iterate through the options that are being tracked looking for wisdom_opt_out setting
522 if ( ! empty( $this->options ) ) {
523 foreach ( $this->options as $option_name ) {
524 // Check each option
525 $options = get_option( $option_name );
526 // If we find the setting, return true
527 if ( ! empty( $options['wisdom_opt_out'] ) ) {
528 return true;
529 }
530 }
531 }
532 }
533
534 return false;
535 }
536
537 /**
538 * Check if it's time to track
539 *
540 * @since 1.1.1
541 */
542 public function get_is_time_to_track() {
543 // Let's see if we're due to track this plugin yet
544 $track_times = get_option( 'wisdom_last_track_time', [] );
545 if ( ! isset( $track_times[ $this->plugin_name ] ) ) {
546 // If we haven't set a time for this plugin yet, then we must track it
547 return true;
548 } else {
549 // If the time is set, let's get our schedule and check if it's time to track
550 $schedule = $this->get_schedule();
551 if ( $schedule == 'daily' ) {
552 $period = 'day';
553 } elseif ( $schedule == 'weekly' ) {
554 $period = 'week';
555 } else {
556 $period = 'month';
557 }
558 if ( $track_times[ $this->plugin_name ] < strtotime( '-1 ' . $period ) ) {
559 return true;
560 }
561 }
562
563 return false;
564 }
565
566 /**
567 * Record the time we send tracking data
568 *
569 * @since 1.1.1
570 */
571 public function set_track_time() {
572 // We've tracked, so record the time
573 $track_times = get_option( 'wisdom_last_track_time', [] );
574 // Set different times according to plugin, in case we are tracking multiple plugins
575 $track_times[ $this->plugin_name ] = time();
576 update_option( 'wisdom_last_track_time', $track_times );
577 }
578
579 /**
580 * Set the time when we can display the opt-in notification
581 * Will display now unless filtered
582 *
583 * @since 1.2.4
584 */
585 public function set_notification_time() {
586 $notification_times = get_option( 'wisdom_notification_times', [] );
587 // Set different times according to plugin, in case we are tracking multiple plugins
588 if ( ! isset( $notification_times[ $this->plugin_name ] ) ) {
589 $delay_notification = apply_filters( 'wisdom_delay_notification_' . $this->plugin_name, 0 );
590 // We can delay the notification time
591 $notification_time = time() + absint( $delay_notification );
592 $notification_times[ $this->plugin_name ] = $notification_time;
593 update_option( 'wisdom_notification_times', $notification_times );
594 }
595 }
596
597 /**
598 * Get whether it's time to display the notification
599 *
600 * @since 1.2.4
601 * @return Boolean
602 */
603 public function get_is_notification_time() {
604 $notification_times = get_option( 'wisdom_notification_times', [] );
605 $time = time();
606 // Set different times according to plugin, in case we are tracking multiple plugins
607 if ( isset( $notification_times[ $this->plugin_name ] ) ) {
608 $notification_time = $notification_times[ $this->plugin_name ];
609 if ( $time >= $notification_time ) {
610 return true;
611 }
612 }
613
614 return false;
615 }
616
617 /**
618 * Set if we should block the opt-in notice for this plugin
619 * Option is an array of all plugins that have received a response from the user
620 *
621 * @since 1.0.0
622 */
623 public function update_block_notice( $plugin = null ) {
624 if ( empty( $plugin ) ) {
625 $plugin = $this->plugin_name;
626 }
627 $block_notice = get_option( 'wisdom_block_notice' );
628 if ( empty( $block_notice ) || ! is_array( $block_notice ) ) {
629 // If nothing exists in the option yet, start a new array with the plugin name
630 $block_notice = [ $plugin => $plugin ];
631 } else {
632 // Else add the plugin name to the array
633 $block_notice[ $plugin ] = $plugin;
634 }
635 update_option( 'wisdom_block_notice', $block_notice );
636 }
637
638 /**
639 * Can we collect the email address?
640 *
641 * @since 1.0.0
642 */
643 public function get_can_collect_email() {
644 // The wisdom_collect_email option is an array of plugins that are being tracked
645 $collect_email = get_option( 'wisdom_collect_email' );
646 // If this plugin is in the array, then we can collect the email address
647 if ( isset( $collect_email[ $this->plugin_name ] ) ) {
648 return true;
649 }
650
651 return false;
652 }
653
654 /**
655 * Set if user has allowed us to collect their email address
656 * Option is an array of all plugins with email collection permitted
657 * More than one plugin may be using the tracker
658 *
659 * @since 1.0.0
660 *
661 * @param $can_collect Boolean true if collection is allowed, false if not
662 */
663 public function set_can_collect_email( $can_collect, $plugin = null ) {
664 if ( empty( $plugin ) ) {
665 $plugin = $this->plugin_name;
666 }
667 // The wisdom_collect_email option is an array of plugins that are being tracked
668 $collect_email = get_option( 'wisdom_collect_email' );
669 // If the user has agreed to allow tracking or if opt-in is not required
670 if ( $can_collect ) {
671 if ( empty( $collect_email ) || ! is_array( $collect_email ) ) {
672 // If nothing exists in the option yet, start a new array with the plugin name
673 $collect_email = [ $plugin => $plugin ];
674 } else {
675 // Else add the plugin name to the array
676 $collect_email[ $plugin ] = $plugin;
677 }
678 } else {
679 if ( isset( $collect_email[ $plugin ] ) ) {
680 unset( $collect_email[ $plugin ] );
681 }
682 }
683 update_option( 'wisdom_collect_email', $collect_email );
684 }
685
686 /**
687 * Get the correct email address to use
688 *
689 * @since 1.1.2
690 * @return Email address
691 */
692 public function get_admin_email() {
693 // The wisdom_collect_email option is an array of plugins that are being tracked
694 $email = get_option( 'wisdom_admin_emails' );
695 // If this plugin is in the array, then we can collect the email address
696 if ( isset( $email[ $this->plugin_name ] ) ) {
697 return $email[ $this->plugin_name ];
698 }
699
700 return false;
701 }
702
703 /**
704 * Set the correct email address to use
705 * There might be more than one admin on the site
706 * So we only use the first admin's email address
707 *
708 * @since 1.1.2
709 *
710 * @param $plugin Plugin name to set email address for
711 * @param $email Email address to set
712 */
713 public function set_admin_email( $email = null, $plugin = null ) {
714 if ( empty( $plugin ) ) {
715 $plugin = $this->plugin_name;
716 }
717 // If no email address passed, try to get the current user's email
718 if ( empty( $email ) ) {
719 // Have to check that current user object is available
720 if ( function_exists( 'wp_get_current_user' ) ) {
721 $current_user = wp_get_current_user();
722 $email = $current_user->user_email;
723 }
724 }
725 // The wisdom_admin_emails option is an array of admin email addresses
726 $admin_emails = get_option( 'wisdom_admin_emails' );
727 if ( empty( $admin_emails ) || ! is_array( $admin_emails ) ) {
728 // If nothing exists in the option yet, start a new array with the plugin name
729 $admin_emails = [ $plugin => sanitize_email( $email ) ];
730 } elseif ( empty( $admin_emails[ $plugin ] ) ) {
731 // Else add the email address to the array, if not already set
732 $admin_emails[ $plugin ] = sanitize_email( $email );
733 }
734 update_option( 'wisdom_admin_emails', $admin_emails );
735 }
736
737 /**
738 * Display the admin notice to users to allow them to opt in
739 *
740 * @since 1.0.0
741 */
742 public function optin_notice() {
743 // Check for plugin args
744 if (
745 ! empty( $_GET['wisdom_plugin'] )
746 && ! empty( $_GET['wisdom_plugin_action'] )
747 && ! empty( $_GET['wisdom_plugin_nonce'] )
748 && wp_verify_nonce( $_GET['wisdom_plugin_nonce'], 'wisdom_plugin_action' )
749 ) {
750 $plugin = sanitize_text_field( $_GET['wisdom_plugin'] );
751 $action = sanitize_text_field( $_GET['wisdom_plugin_action'] );
752 if ( $action === 'yes' ) {
753 $this->set_is_tracking_allowed( true, $plugin );
754 // Run this straightaway
755 add_action( 'admin_init', [ $this, 'force_tracking' ] );
756 } else {
757 $this->set_is_tracking_allowed( false, $plugin );
758 }
759 $this->update_block_notice( $plugin );
760 }
761
762 // Is it time to display the notification?
763 $is_time = $this->get_is_notification_time();
764 if ( ! $is_time ) {
765 return false;
766 }
767
768 // Check whether to block the notice, e.g. because we're in a local environment
769 // wisdom_block_notice works the same as wisdom_allow_tracking, an array of plugin names
770 $block_notice = get_option( 'wisdom_block_notice' );
771
772 if ( isset( $block_notice[ $this->plugin_name ] ) ) {
773 return;
774 }
775
776 if ( ! current_user_can( 'manage_options' ) ) {
777 return;
778 }
779
780 // @credit EDD
781 // Don't bother asking user to opt in if they're in local dev
782 $is_local = false;
783 if ( stristr( network_site_url( '/' ), '.dev' ) !== false || stristr( network_site_url( '/' ), 'localhost' ) !== false || stristr( network_site_url( '/' ), ':8888' ) !== false ) {
784 $is_local = true;
785 }
786 $is_local = apply_filters( 'wisdom_is_local_' . $this->plugin_name, $is_local );
787 if ( $is_local ) {
788 $this->update_block_notice();
789
790 // SKC modification for Pods.
791 if ( $this->marketing ) {
792 $this->set_can_collect_email( false );
793 }
794 } else {
795 // Display the notice requesting permission to track
796 // Retrieve current plugin information
797 $plugin = $this->plugin_data();
798 $plugin_name = $plugin['Name'];
799
800 $nonce = wp_create_nonce( 'wisdom_plugin_action' );
801
802 // Args to add to query if user opts in to tracking
803 $yes_args = [
804 'wisdom_plugin' => $this->plugin_name,
805 'wisdom_plugin_action' => 'yes',
806 'wisdom_plugin_nonce' => $nonce,
807 ];
808
809 // Decide how to request permission to collect email addresses
810 if ( (int) $this->marketing === 1 ) {
811 // Option 1 combines permissions to track and collect email
812 $yes_args['wisdom_marketing_optin'] = 'yes';
813 } elseif ( (int) $this->marketing === 2 ) {
814 // Option 2 enables a second notice that fires after the user opts in to tracking
815 $yes_args['wisdom_marketing'] = 'yes';
816 }
817 $url_yes = add_query_arg( $yes_args );
818 $url_no = add_query_arg( [
819 'wisdom_plugin' => $this->plugin_name,
820 'wisdom_plugin_action' => 'no',
821 'wisdom_plugin_nonce' => $nonce,
822 ] );
823
824 // Decide on notice text
825 if ( $this->marketing != 1 ) {
826 // Standard notice text
827 $notice_text = sprintf( __( 'Thank you for installing our %1$s. We would like to track its usage on your site. We don\'t record any sensitive data, only information regarding the WordPress environment and %1$s settings, which we will use to help us make improvements to the %1$s. Tracking is completely optional.', 'pods' ), $this->what_am_i );
828 } else {
829 // If we have option 1 for marketing, we include reference to sending product information here
830 $notice_text = sprintf( __( 'Thank you for installing our %1$s. We\'d like your permission to track its usage on your site and subscribe you to our newsletter. We won\'t record any sensitive data, only information regarding the WordPress environment and %1$s settings, which we will use to help us make improvements to the %1$s. Tracking is completely optional.', 'pods' ), $this->what_am_i );
831 }
832 // And we allow you to filter the text anyway
833 $notice_text = apply_filters( 'wisdom_notice_text_' . esc_attr( $this->plugin_name ), $notice_text ); ?>
834
835 <div class="notice notice-info updated put-dismiss-notice">
836 <p><?php echo '<strong>' . esc_html( $plugin_name ) . '</strong>'; ?></p>
837 <p><?php echo esc_html( $notice_text ); ?></p>
838 <p>
839 <a href="<?php echo esc_url( $url_yes ); ?>" class="button-secondary"><?php _e( 'Allow', 'pods' ); ?></a>
840 <a href="<?php echo esc_url( $url_no ); ?>" class="button-secondary"><?php _e( 'Do Not Allow', 'pods' ); ?></a>
841 </p>
842 </div>
843 <?php
844 }
845 }
846
847 }
848