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