PSR
2 weeks ago
assets
2 weeks ago
data_provider
2 weeks ago
persistence
2 weeks ago
sender
2 weeks ago
views
2 weeks ago
class-wpdesk-tracker-factory-prefixed.php
2 weeks ago
class-wpdesk-tracker-interface.php
2 weeks ago
class-wpdesk-tracker.php
2 weeks ago
class-wpdesk-tracker.php
517 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor; |
| 4 | |
| 5 | /** |
| 6 | * WP Desk Tracker |
| 7 | * |
| 8 | * @class WPDESK_Tracker |
| 9 | * @version 1.3.2 |
| 10 | * @package WPDESK/Helper |
| 11 | * @category Class |
| 12 | * @author WP Desk |
| 13 | */ |
| 14 | if (!\defined('ABSPATH')) { |
| 15 | exit; |
| 16 | } |
| 17 | if (!\class_exists('FSVendor\WPDesk_Tracker')) { |
| 18 | /** |
| 19 | * Class WPDESK_Tracker |
| 20 | */ |
| 21 | class WPDesk_Tracker implements \WPDesk_Tracker_Interface |
| 22 | { |
| 23 | public const ADMIN_HANDLER = 'wpdesk-helper-tracker'; |
| 24 | public const ADMIN_LOCALIZE = 'wpdesk_tracker_localize'; |
| 25 | public const ADMIN_NOTICE_HANDLER = 'wpdesk-helper-tracker-notice'; |
| 26 | public const ADMIN_NOTICE_LOCALIZE = 'wpdesk_tracker_notice_localize'; |
| 27 | public const ADMIN_PLUGINS_HANDLER = 'wpdesk-helper-plugins-notice'; |
| 28 | public const ADMIN_PLUGINS_LOCALIZE = 'wpdesk_tracker_plugins_localize'; |
| 29 | public const AJAX_ACTION = 'wpdesk_tracker_deactivation_handler'; |
| 30 | public const AJAX_NONCE = 'nonce_wpdesk_tracker_deactivation_handler'; |
| 31 | public const AJAX_NOTICE_ACTION = 'wpdesk_tracker_notice_handler'; |
| 32 | public const WPDESK_TRACKER_NOTICE = 'wpdesk-tracker-notice'; |
| 33 | public const WPDESK_TRACKER_DEACTIVATION = 'wpdesk-tracker-deactivation'; |
| 34 | private const WPDESK_TRACKER_ACTION = 'wpdesk_tracker_action'; |
| 35 | private const WPDESK_TRACKER_NONCE = 'nonce'; |
| 36 | /** |
| 37 | * @var string |
| 38 | */ |
| 39 | private $script_version = '11'; |
| 40 | /** |
| 41 | * @var string |
| 42 | */ |
| 43 | private $plugin_basename = ''; |
| 44 | /** |
| 45 | * @var string |
| 46 | */ |
| 47 | private $message_version = '4'; |
| 48 | /** |
| 49 | * @var WPDesk_Tracker_Data_Provider[] |
| 50 | */ |
| 51 | private $providers = []; |
| 52 | /** |
| 53 | * @var WPDesk_Tracker_Sender |
| 54 | */ |
| 55 | private $sender; |
| 56 | /** |
| 57 | * @deprecated do not use. This is for backward compatibility only. |
| 58 | */ |
| 59 | public static function init($foo = null) |
| 60 | { |
| 61 | } |
| 62 | public function __construct($plugin_basename, \WPDesk_Tracker_Sender $sender) |
| 63 | { |
| 64 | $this->plugin_basename = $plugin_basename; |
| 65 | $this->set_sender($sender); |
| 66 | } |
| 67 | /** |
| 68 | * Setter for object that sends data. |
| 69 | * |
| 70 | * @param WPDesk_Tracker_Sender $sender Object that can send payloads. |
| 71 | */ |
| 72 | public function set_sender(\WPDesk_Tracker_Sender $sender) |
| 73 | { |
| 74 | $this->sender = $sender; |
| 75 | } |
| 76 | /** |
| 77 | * Hook into cron event. |
| 78 | */ |
| 79 | public function init_hooks() |
| 80 | { |
| 81 | \add_action('admin_init', [$this, 'init_schedule']); |
| 82 | \add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts'], 100); |
| 83 | \add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_notice_scripts'], 100); |
| 84 | \add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_plugins_scripts'], 100); |
| 85 | \add_action('wpdesk_tracker_send_event', [$this, 'send_tracking_data']); |
| 86 | \add_action('admin_menu', [$this, 'admin_menu']); |
| 87 | \add_action('admin_init', [$this, 'admin_init']); |
| 88 | \add_action('admin_notices', [$this, 'admin_notices']); |
| 89 | \add_action('wp_ajax_wpdesk_tracker_notice_handler', [$this, 'wp_ajax_wpdesk_tracker_notice_handler']); |
| 90 | \add_action('wp_ajax_wpdesk_tracker_deactivation_handler', [$this, 'wp_ajax_wpdesk_tracker_deactivation_handler']); |
| 91 | \add_action('update_option_wpdesk_helper_options', [$this, 'update_option_wpdesk_helper_options'], 10, 3); |
| 92 | \add_filter('option_wpdesk_helper_options', [$this, 'option_wpdesk_helper_options'], 10, 2); |
| 93 | \add_filter('default_option_wpdesk_helper_options', [$this, 'default_option_wpdesk_helper_options'], 10, 3); |
| 94 | \add_filter('wpdesk_tracker_data', [$this, 'wpdesk_tracker_message_version']); |
| 95 | \add_action('admin_bar_menu', [$this, 'admin_bar_menu'], 999); |
| 96 | } |
| 97 | public function add_data_provider(\WPDesk_Tracker_Data_Provider $provider) |
| 98 | { |
| 99 | $this->providers[] = $provider; |
| 100 | } |
| 101 | /** |
| 102 | * @param WP_Admin_Bar $wp_admin_bar |
| 103 | */ |
| 104 | public function admin_bar_menu($wp_admin_bar) |
| 105 | { |
| 106 | if (\apply_filters('wpdesk_tracker_use_testdata', \false) && \apply_filters('wpdesk_tracker_show_testdata', \false)) { |
| 107 | $args = ['id' => 'my_page', 'title' => 'WP Desk Test!', 'meta' => ['class' => 'wpdesk-tracker-test']]; |
| 108 | $wp_admin_bar->add_node($args); |
| 109 | } |
| 110 | } |
| 111 | public function init_schedule() |
| 112 | { |
| 113 | $options = \get_option('wpdesk_helper_options'); |
| 114 | if (!\is_array($options)) { |
| 115 | $options = []; |
| 116 | } |
| 117 | if (empty($options['wpdesk_tracker_agree'])) { |
| 118 | $options['wpdesk_tracker_agree'] = '0'; |
| 119 | } |
| 120 | $wpdesk_tracker_agree = $options['wpdesk_tracker_agree']; |
| 121 | $wp_next_scheduled = \wp_next_scheduled('wpdesk_tracker_send_event'); |
| 122 | if ($wpdesk_tracker_agree == '1' && !$wp_next_scheduled) { |
| 123 | \wp_schedule_event(\time(), 'daily', 'wpdesk_tracker_send_event'); |
| 124 | } |
| 125 | if ($wpdesk_tracker_agree == '0' && $wp_next_scheduled) { |
| 126 | \wp_clear_scheduled_hook('wpdesk_tracker_send_event'); |
| 127 | } |
| 128 | } |
| 129 | /** |
| 130 | * @return bool |
| 131 | */ |
| 132 | private function should_enable_wpdesk_tracker() |
| 133 | { |
| 134 | return \apply_filters('wpdesk_tracker_enabled', \true); |
| 135 | } |
| 136 | public function admin_enqueue_plugins_scripts(): void |
| 137 | { |
| 138 | global $pagenow; |
| 139 | if ('plugins.php' === $pagenow && !\is_network_admin() && $this->should_enable_wpdesk_tracker() && !\apply_filters('wpdesk_tracker_do_not_ask', \false)) { |
| 140 | \wp_register_script(self::ADMIN_PLUGINS_HANDLER, \plugin_dir_url(__FILE__) . 'assets/js/admin_plugins.js', [], $this->script_version); |
| 141 | \wp_enqueue_script(self::ADMIN_PLUGINS_HANDLER); |
| 142 | $plugins = ['wpdesk-helper/wpdesk-helper.php' => 'wpdesk-helper/wpdesk-helper.php']; |
| 143 | $plugins = \apply_filters('wpdesk_track_plugin_deactivation', $plugins); |
| 144 | \wp_localize_script(self::ADMIN_PLUGINS_HANDLER, self::ADMIN_PLUGINS_LOCALIZE, ['plugins' => \wp_json_encode($plugins), 'base_url' => \esc_url_raw(\wp_nonce_url(\admin_url("admin.php?page=wpdesk_tracker_deactivate"), self::WPDESK_TRACKER_ACTION, self::WPDESK_TRACKER_NONCE))]); |
| 145 | } |
| 146 | } |
| 147 | public function admin_enqueue_scripts(): void |
| 148 | { |
| 149 | $screen = \get_current_screen(); |
| 150 | if ($screen->id === 'admin_page_wpdesk_tracker' || $screen->id === 'admin_page_wpdesk_tracker_deactivate') { |
| 151 | \wp_register_style(self::ADMIN_HANDLER, \plugin_dir_url(__FILE__) . 'assets/css/tracker.css', [], $this->script_version, 'all'); |
| 152 | \wp_enqueue_style(self::ADMIN_HANDLER); |
| 153 | \wp_register_script(self::ADMIN_HANDLER, \plugin_dir_url(__FILE__) . 'assets/js/admin.js', [], $this->script_version); |
| 154 | \wp_enqueue_script(self::ADMIN_HANDLER); |
| 155 | $plugin = ''; |
| 156 | $plugin_name = ''; |
| 157 | if (\current_user_can('activate_plugins') && \false !== \check_ajax_referer(self::WPDESK_TRACKER_ACTION, self::WPDESK_TRACKER_NONCE, \false)) { |
| 158 | $plugin = \sanitize_text_field(\wp_unslash($_GET['plugin'] ?? '')); |
| 159 | $active_plugins = \get_plugins(); |
| 160 | $plugin_name = $active_plugins[$plugin]['Name'] ?? ''; |
| 161 | } |
| 162 | $deactivation_plugins = ['wpdesk-helper/wpdesk-helper.php' => 'wpdesk-helper/wpdesk-helper.php']; |
| 163 | $deactivation_plugins = \apply_filters('wpdesk_track_plugin_deactivation', $deactivation_plugins); |
| 164 | \wp_localize_script(self::ADMIN_HANDLER, self::ADMIN_LOCALIZE, ['action' => \esc_attr(self::AJAX_ACTION), 'nonce' => \wp_create_nonce(self::AJAX_NONCE), 'ajax_url' => \esc_url_raw(\admin_url('admin-ajax.php')), 'tracker_page' => \esc_url_raw(\str_replace("&", "&", \admin_url(\wp_nonce_url("plugins.php?action=deactivate&plugin=" . $plugin . "&plugin_status=all&", "deactivate-plugin_" . $plugin)))), 'plugin' => \esc_attr($plugin), 'plugin_name' => \esc_attr($plugin_name), 'submit_txt' => \esc_html__("Submit & Deactivate", "flexible-shipping"), 'deactivation_plugins' => \wp_json_encode($deactivation_plugins)]); |
| 165 | } |
| 166 | } |
| 167 | private function is_notices_enabled(): bool |
| 168 | { |
| 169 | if (!$this->should_enable_wpdesk_tracker()) { |
| 170 | return \false; |
| 171 | } |
| 172 | if (!\current_user_can('activate_plugins')) { |
| 173 | return \false; |
| 174 | } |
| 175 | if (\get_option('wpdesk_tracker_notice', '0') != 'dismiss_all') { |
| 176 | $options = \get_option('wpdesk_helper_options', []); |
| 177 | if (!\is_array($options)) { |
| 178 | $options = []; |
| 179 | } |
| 180 | if (empty($options['wpdesk_tracker_agree']) || $options['wpdesk_tracker_agree'] == '0') { |
| 181 | if ($this->can_display_notice()) { |
| 182 | return \true; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | return \false; |
| 187 | } |
| 188 | public function admin_enqueue_notice_scripts(): void |
| 189 | { |
| 190 | if ($this->is_notices_enabled()) { |
| 191 | \wp_register_script(self::ADMIN_NOTICE_HANDLER, \plugin_dir_url(__FILE__) . 'assets/js/admin_notices.js', [], $this->script_version); |
| 192 | \wp_enqueue_script(self::ADMIN_NOTICE_HANDLER); |
| 193 | \wp_localize_script(self::ADMIN_NOTICE_HANDLER, self::ADMIN_NOTICE_LOCALIZE, ['action' => \esc_attr(self::AJAX_NOTICE_ACTION), 'nonce' => \wp_create_nonce(self::WPDESK_TRACKER_NOTICE), 'ajax_url' => \esc_url_raw(\admin_url('admin-ajax.php')), 'plugin' => \esc_attr($this->plugin_basename)]); |
| 194 | } |
| 195 | } |
| 196 | public function admin_menu() |
| 197 | { |
| 198 | \add_submenu_page('wpdesk_tracker', 'WP Desk Tracker', 'WP Desk Tracker', 'manage_options', 'wpdesk_tracker', [$this, 'wpdesk_tracker_page']); |
| 199 | \add_submenu_page('wpdesk_tracker', 'Deactivate plugin', 'Deactivate plugin', 'manage_options', 'wpdesk_tracker_deactivate', [$this, 'wpdesk_tracker_deactivate']); |
| 200 | } |
| 201 | public function wp_ajax_wpdesk_tracker_deactivation_handler() |
| 202 | { |
| 203 | \check_ajax_referer(self::WPDESK_TRACKER_DEACTIVATION, 'security'); |
| 204 | if (!\current_user_can('activate_plugins')) { |
| 205 | die; |
| 206 | } |
| 207 | $this->send_deactivation_data(); |
| 208 | } |
| 209 | public function wp_ajax_wpdesk_tracker_notice_handler() |
| 210 | { |
| 211 | \check_ajax_referer(self::WPDESK_TRACKER_NOTICE, 'security'); |
| 212 | if (!\current_user_can('activate_plugins')) { |
| 213 | die; |
| 214 | } |
| 215 | $option = \get_option('wpdesk_helper_options'); |
| 216 | if (!$option) { |
| 217 | \add_option('wpdesk_helper_options', []); |
| 218 | } |
| 219 | $type = ''; |
| 220 | if (isset($_REQUEST['type'])) { |
| 221 | $type = \sanitize_key($_REQUEST['type']); |
| 222 | } |
| 223 | if ($type === 'allow') { |
| 224 | $options = \get_option('wpdesk_helper_options', []); |
| 225 | if (!\is_array($options)) { |
| 226 | $options = []; |
| 227 | } |
| 228 | \update_option('wpdesk_helper_options', $options); |
| 229 | \delete_option('wpdesk_tracker_notice'); |
| 230 | $options['wpdesk_tracker_agree'] = '1'; |
| 231 | \update_option('wpdesk_helper_options', $options); |
| 232 | } |
| 233 | if ($type === 'dismiss') { |
| 234 | $options = \get_option('wpdesk_helper_options', []); |
| 235 | if (!\is_array($options)) { |
| 236 | $options = []; |
| 237 | } |
| 238 | \update_option('wpdesk_tracker_notice', 'dismiss_all'); |
| 239 | $options['wpdesk_tracker_agree'] = '0'; |
| 240 | \update_option('wpdesk_helper_options', $options); |
| 241 | } |
| 242 | } |
| 243 | public function update_option_wpdesk_helper_options($old_value, $value, $option) |
| 244 | { |
| 245 | if (empty($old_value)) { |
| 246 | $old_value = ['wpdesk_tracker_agree' => '-1']; |
| 247 | } |
| 248 | if (!isset($old_value['wpdesk_tracker_agree'])) { |
| 249 | $old_value['wpdesk_tracker_agree'] = '-1'; |
| 250 | } |
| 251 | if (empty($value)) { |
| 252 | $value = ['wpdesk_tracker_agree' => '-1']; |
| 253 | } |
| 254 | if (!isset($value['wpdesk_tracker_agree'])) { |
| 255 | $value['wpdesk_tracker_agree'] = '-1'; |
| 256 | } |
| 257 | if ($old_value['wpdesk_tracker_agree'] != '1') { |
| 258 | if ($value['wpdesk_tracker_agree'] == '1') { |
| 259 | $this->send_tracking_data(\true, 'agree', $this->read_context_from_request()); |
| 260 | } |
| 261 | } |
| 262 | if ($old_value['wpdesk_tracker_agree'] != '0') { |
| 263 | if ($value['wpdesk_tracker_agree'] == '0') { |
| 264 | $this->send_tracking_data(\true, 'no', $this->read_context_from_request()); |
| 265 | \update_option('wpdesk_tracker_notice', 'dismiss_all'); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | public function option_wpdesk_helper_options($value, $option) |
| 270 | { |
| 271 | if (\apply_filters('wpdesk_tracker_do_not_ask', \false)) { |
| 272 | if (!\is_array($value)) { |
| 273 | $value = []; |
| 274 | } |
| 275 | $value['wpdesk_tracker_agree'] = 1; |
| 276 | } |
| 277 | return $value; |
| 278 | } |
| 279 | public function default_option_wpdesk_helper_options($default, $option = null, $passed_default = null) |
| 280 | { |
| 281 | if (\apply_filters('wpdesk_tracker_do_not_ask', \false)) { |
| 282 | $default = []; |
| 283 | $default['wpdesk_tracker_agree'] = 1; |
| 284 | } |
| 285 | return $default; |
| 286 | } |
| 287 | public function admin_notices() |
| 288 | { |
| 289 | if ($this->is_notices_enabled()) { |
| 290 | $user = \wp_get_current_user(); |
| 291 | $username = $user->first_name ? $user->first_name : $user->user_login; |
| 292 | $terms_url = \get_locale() === 'pl_PL' ? 'https://www.wpdesk.pl/dane-uzytkowania/' : 'https://www.wpdesk.net/usage-tracking/'; |
| 293 | include 'views/tracker-notice.php'; |
| 294 | } |
| 295 | if (isset($_GET['wpdesk_tracker_opt_out']) && \current_user_can('activate_plugins') && $this->should_enable_wpdesk_tracker()) { |
| 296 | $options = \get_option('wpdesk_helper_options', []); |
| 297 | if (!\is_array($options)) { |
| 298 | $options = []; |
| 299 | } |
| 300 | \delete_option('wpdesk_tracker_notice'); |
| 301 | $options['wpdesk_tracker_agree'] = '0'; |
| 302 | \update_option('wpdesk_helper_options', $options); |
| 303 | include 'views/tracker-opt-out-notice.php'; |
| 304 | } |
| 305 | } |
| 306 | public function wpdesk_tracker_page() |
| 307 | { |
| 308 | $nonce = \wp_create_nonce(self::WPDESK_TRACKER_ACTION); |
| 309 | $user = \wp_get_current_user(); |
| 310 | $username = $user->first_name ? $user->first_name : $user->user_login; |
| 311 | $allow_url = \admin_url('admin.php?page=wpdesk_tracker'); |
| 312 | $allow_url = \add_query_arg('plugin', \sanitize_text_field(\wp_unslash($_GET['plugin'] ?? '')), $allow_url); |
| 313 | $allow_url = \add_query_arg(self::WPDESK_TRACKER_NONCE, $nonce, $allow_url); |
| 314 | $skip_url = $allow_url; |
| 315 | $allow_url = \add_query_arg('allow', '1', $allow_url); |
| 316 | $skip_url = \add_query_arg('allow', '0', $skip_url); |
| 317 | $terms_url = \get_locale() === 'pl_PL' ? 'https://www.wpdesk.pl/dane-uzytkowania/' : 'https://www.wpdesk.net/usage-tracking/'; |
| 318 | include 'views/tracker-connect.php'; |
| 319 | } |
| 320 | public function wpdesk_tracker_deactivate() |
| 321 | { |
| 322 | if (!\current_user_can('activate_plugins') || \false === \check_ajax_referer(self::WPDESK_TRACKER_ACTION, self::WPDESK_TRACKER_NONCE)) { |
| 323 | die; |
| 324 | } |
| 325 | if (!$this->applies_to_current_plugin()) { |
| 326 | return; |
| 327 | } |
| 328 | $user = \wp_get_current_user(); |
| 329 | $username = $user->first_name; |
| 330 | $plugin = \sanitize_text_field(\wp_unslash($_GET['plugin'] ?? '')); |
| 331 | $active_plugins = \get_plugins(); |
| 332 | $plugin_name = $active_plugins[$plugin]['Name']; |
| 333 | include 'views/tracker-deactivate.php'; |
| 334 | } |
| 335 | public function admin_init() |
| 336 | { |
| 337 | if (isset($_GET['page']) && $_GET['page'] === 'wpdesk_tracker') { |
| 338 | if (isset($_GET['plugin']) && isset($_GET['allow'])) { |
| 339 | if (!\current_user_can('activate_plugins') || \false === \check_ajax_referer(self::WPDESK_TRACKER_ACTION, self::WPDESK_TRACKER_NONCE)) { |
| 340 | die; |
| 341 | } |
| 342 | $options = \get_option('wpdesk_helper_options', []); |
| 343 | if (!\is_array($options)) { |
| 344 | $options = []; |
| 345 | } |
| 346 | if ($_GET['allow'] == '0') { |
| 347 | \remove_action('update_option_wpdesk_helper_options', [$this, 'update_option_wpdesk_helper_options'], 10, 3); |
| 348 | unset($options['wpdesk_tracker_agree']); |
| 349 | \update_option('wpdesk_helper_options', $options); |
| 350 | \add_action('update_option_wpdesk_helper_options', [$this, 'update_option_wpdesk_helper_options'], 10, 3); |
| 351 | $options['wpdesk_tracker_agree'] = '0'; |
| 352 | \update_option('wpdesk_helper_options', $options); |
| 353 | \update_option('wpdesk_tracker_notice', '1'); |
| 354 | } else { |
| 355 | \remove_action('update_option_wpdesk_helper_options', [$this, 'update_option_wpdesk_helper_options'], 10, 3); |
| 356 | unset($options['wpdesk_tracker_agree']); |
| 357 | \update_option('wpdesk_helper_options', $options); |
| 358 | \add_action('update_option_wpdesk_helper_options', [$this, 'update_option_wpdesk_helper_options'], 10, 3); |
| 359 | \delete_option('wpdesk_tracker_notice'); |
| 360 | \update_option('wpdesk_tracker_agree', '1'); |
| 361 | $options['wpdesk_tracker_agree'] = '1'; |
| 362 | \update_option('wpdesk_helper_options', $options); |
| 363 | } |
| 364 | \wp_safe_redirect(\admin_url('plugins.php')); |
| 365 | exit; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | public function wpdesk_tracker_message_version($data) |
| 370 | { |
| 371 | $data['message_version'] = $this->message_version; |
| 372 | return $data; |
| 373 | } |
| 374 | public function send_deactivation_data() |
| 375 | { |
| 376 | if (!isset($_REQUEST['plugin'], $_REQUEST['plugin_name'], $_REQUEST['reason'])) { |
| 377 | return; |
| 378 | } |
| 379 | $params = []; |
| 380 | $params['click_action'] = 'plugin_deactivation'; |
| 381 | $params['plugin'] = \sanitize_text_field(\wp_unslash($_REQUEST['plugin'])); |
| 382 | $params['plugin_name'] = \sanitize_text_field(\wp_unslash($_REQUEST['plugin_name'])); |
| 383 | $params['reason'] = \sanitize_text_field(\wp_unslash($_REQUEST['reason'])); |
| 384 | if (!empty($_REQUEST['additional_info'])) { |
| 385 | $params['additional_info'] = \sanitize_text_field(\wp_unslash($_REQUEST['additional_info'])); |
| 386 | } |
| 387 | $this->send_payload_to_wpdesk(\apply_filters('wpdesk_tracker_deactivation_data', $params)); |
| 388 | } |
| 389 | /** |
| 390 | * Decide whether to send tracking data or not. |
| 391 | * |
| 392 | * @param boolean $override |
| 393 | */ |
| 394 | public function send_tracking_data($override = \false, $click_action = null, array $additional_data = []) |
| 395 | { |
| 396 | $options = \get_option('wpdesk_helper_options', []); |
| 397 | if (empty($options)) { |
| 398 | $options = []; |
| 399 | } |
| 400 | if (empty($options['wpdesk_tracker_agree'])) { |
| 401 | $options['wpdesk_tracker_agree'] = '0'; |
| 402 | } |
| 403 | if (empty($click_action) && $options['wpdesk_tracker_agree'] == '0') { |
| 404 | return; |
| 405 | } |
| 406 | if (!$this->should_enable_wpdesk_tracker()) { |
| 407 | return; |
| 408 | } |
| 409 | if (!\apply_filters('wpdesk_tracker_send_override', $override)) { |
| 410 | // Send a maximum of once per week by default. |
| 411 | $last_send = $this->get_last_send_time(); |
| 412 | if ($last_send && $last_send > \apply_filters('wpdesk_tracker_last_send_interval', \strtotime('-1 week'))) { |
| 413 | return; |
| 414 | } |
| 415 | } else { |
| 416 | // Make sure there is at least a 1 hour delay between override sends, we dont want duplicate calls due to double clicking links. |
| 417 | $last_send = $this->get_last_send_time(); |
| 418 | if (empty($click_action) && $last_send && $last_send > \strtotime('-1 hours')) { |
| 419 | return; |
| 420 | } |
| 421 | } |
| 422 | // Update time first before sending to ensure it is set. |
| 423 | \update_option('wpdesk_tracker_last_send', \time()); |
| 424 | if (empty($click_action) || $click_action === 'agree') { |
| 425 | $params = $this->get_tracking_data(); |
| 426 | if (isset($params['active_plugins'])) { |
| 427 | foreach ($params['active_plugins'] as $plugin => $plugin_data) { |
| 428 | $option_name = 'activation_plugin_' . $plugin; |
| 429 | $activation_date = \get_option($option_name, ''); |
| 430 | if ($activation_date != '') { |
| 431 | $params['active_plugins'][$plugin]['activation_date'] = $activation_date; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | if (!empty($click_action)) { |
| 436 | $params['click_action'] = 'agree'; |
| 437 | } |
| 438 | } else { |
| 439 | $params = ['click_action' => 'no']; |
| 440 | $params['url'] = \home_url(); |
| 441 | } |
| 442 | $params['localhost'] = 'no'; |
| 443 | if (!empty($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR'] === '127.0.0.1') { |
| 444 | $params['localhost'] = 'yes'; |
| 445 | } |
| 446 | $this->send_payload_to_wpdesk(\array_merge($params, $additional_data)); |
| 447 | } |
| 448 | /** |
| 449 | * Sends payload to WPDesk servers. |
| 450 | * |
| 451 | * @param array $payload Payload to sent. |
| 452 | * |
| 453 | * @return bool If sending was successfull. |
| 454 | */ |
| 455 | private function send_payload_to_wpdesk(array $payload) |
| 456 | { |
| 457 | try { |
| 458 | $this->sender->send_payload($payload); |
| 459 | return \true; |
| 460 | } catch (WPDesk_Tracker_Sender_Exception_WpError $e) { |
| 461 | return \false; |
| 462 | } |
| 463 | } |
| 464 | /** |
| 465 | * Get the last time tracking data was sent. |
| 466 | * |
| 467 | * @return int|bool |
| 468 | */ |
| 469 | private function get_last_send_time() |
| 470 | { |
| 471 | return \apply_filters('wpdesk_tracker_last_send_time', \get_option('wpdesk_tracker_last_send', \false)); |
| 472 | } |
| 473 | /** |
| 474 | * @return array |
| 475 | */ |
| 476 | private function get_data_from_providers() |
| 477 | { |
| 478 | $data = []; |
| 479 | if (!empty($this->providers)) { |
| 480 | foreach ($this->providers as $provider) { |
| 481 | $data = \array_merge($data, $provider->get_data()); |
| 482 | } |
| 483 | } |
| 484 | return $data; |
| 485 | } |
| 486 | /** |
| 487 | * Get all the tracking data. |
| 488 | * |
| 489 | * @return array |
| 490 | */ |
| 491 | private function get_tracking_data() |
| 492 | { |
| 493 | $data = $this->get_data_from_providers(); |
| 494 | return \apply_filters('wpdesk_tracker_data', $data); |
| 495 | } |
| 496 | private function read_context_from_request(): array |
| 497 | { |
| 498 | if (isset($_REQUEST['ctx'], $_REQUEST['plugin'])) { |
| 499 | return ['source' => ['plugin' => \sanitize_key($_REQUEST['plugin']), 'ctx' => \sanitize_key($_REQUEST['ctx'])]]; |
| 500 | } |
| 501 | return []; |
| 502 | } |
| 503 | private function can_display_notice(): bool |
| 504 | { |
| 505 | if (\has_filter('wpdesk_tracker_notice_screens') === \true) { |
| 506 | $screen = \get_current_screen(); |
| 507 | return \in_array($screen->id, \apply_filters('wpdesk_tracker_notice_screens', []), \true); |
| 508 | } |
| 509 | return \true; |
| 510 | } |
| 511 | private function applies_to_current_plugin(): bool |
| 512 | { |
| 513 | return \strpos(\sanitize_text_field(\wp_unslash($_GET['plugin'] ?? '')), $this->plugin_basename); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 |