admin-templates
4 years ago
base
4 years ago
controls
4 years ago
editor-templates
4 years ago
elements
4 years ago
interfaces
6 years ago
libraries
5 years ago
managers
4 years ago
settings
4 years ago
template-library
4 years ago
widgets
4 years ago
api.php
4 years ago
autoloader.php
4 years ago
beta-testers.php
6 years ago
compatibility.php
4 years ago
conditions.php
6 years ago
db.php
5 years ago
embed.php
4 years ago
fonts.php
4 years ago
frontend.php
4 years ago
heartbeat.php
7 years ago
maintenance-mode.php
4 years ago
maintenance.php
4 years ago
plugin.php
4 years ago
preview.php
4 years ago
rollback.php
4 years ago
shapes.php
6 years ago
stylesheet.php
4 years ago
tracker.php
4 years ago
user.php
4 years ago
utils.php
4 years ago
tracker.php
607 lines
| 1 | <?php |
| 2 | namespace Elementor; |
| 3 | |
| 4 | use Elementor\Core\Experiments\Experiments_Reporter; |
| 5 | use Elementor\Modules\System_Info\Module as System_Info_Module; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly. |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Elementor tracker. |
| 13 | * |
| 14 | * Elementor tracker handler class is responsible for sending non-sensitive plugin |
| 15 | * data to Elementor servers for users that actively allowed data tracking. |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | class Tracker { |
| 20 | |
| 21 | /** |
| 22 | * API URL. |
| 23 | * |
| 24 | * Holds the URL of the Tracker API. |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | * @access private |
| 28 | * |
| 29 | * @var string API URL. |
| 30 | */ |
| 31 | private static $_api_url = 'https://my.elementor.com/api/v1/tracker/'; |
| 32 | |
| 33 | private static $notice_shown = false; |
| 34 | |
| 35 | /** |
| 36 | * Init. |
| 37 | * |
| 38 | * Initialize Elementor tracker. |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | * @access public |
| 42 | * @static |
| 43 | */ |
| 44 | public static function init() { |
| 45 | add_action( 'elementor/tracker/send_event', [ __CLASS__, 'send_tracking_data' ] ); |
| 46 | add_action( 'admin_init', [ __CLASS__, 'handle_tracker_actions' ] ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Check for settings opt-in. |
| 51 | * |
| 52 | * Checks whether the site admin has opted-in for data tracking, or not. |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | * @access public |
| 56 | * @static |
| 57 | * |
| 58 | * @param string $new_value Allowed tracking value. |
| 59 | * |
| 60 | * @return string Return `yes` if tracking allowed, `no` otherwise. |
| 61 | */ |
| 62 | public static function check_for_settings_optin( $new_value ) { |
| 63 | $old_value = get_option( 'elementor_allow_tracking', 'no' ); |
| 64 | if ( $old_value !== $new_value && 'yes' === $new_value ) { |
| 65 | Plugin::$instance->custom_tasks->add_tasks_requested_to_run( [ |
| 66 | 'opt_in_recalculate_usage', |
| 67 | 'opt_in_send_tracking_data', |
| 68 | ] ); |
| 69 | } |
| 70 | |
| 71 | if ( empty( $new_value ) ) { |
| 72 | $new_value = 'no'; |
| 73 | } |
| 74 | |
| 75 | return $new_value; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Send tracking data. |
| 80 | * |
| 81 | * Decide whether to send tracking data, or not. |
| 82 | * |
| 83 | * @since 1.0.0 |
| 84 | * @access public |
| 85 | * @static |
| 86 | * |
| 87 | * @param bool $override |
| 88 | */ |
| 89 | public static function send_tracking_data( $override = false ) { |
| 90 | // Don't trigger this on AJAX Requests. |
| 91 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if ( ! self::is_allow_track() ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $last_send = self::get_last_send_time(); |
| 100 | |
| 101 | /** |
| 102 | * Tracker override send. |
| 103 | * |
| 104 | * Filters whether to override sending tracking data or not. |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | * |
| 108 | * @param bool $override Whether to override default setting or not. |
| 109 | */ |
| 110 | $override = apply_filters( 'elementor/tracker/send_override', $override ); |
| 111 | |
| 112 | if ( ! $override ) { |
| 113 | $last_send_interval = strtotime( '-1 week' ); |
| 114 | |
| 115 | /** |
| 116 | * Tracker last send interval. |
| 117 | * |
| 118 | * Filters the interval of between two tracking requests. |
| 119 | * |
| 120 | * @since 1.0.0 |
| 121 | * |
| 122 | * @param int $last_send_interval A date/time string. Default is `strtotime( '-1 week' )`. |
| 123 | */ |
| 124 | $last_send_interval = apply_filters( 'elementor/tracker/last_send_interval', $last_send_interval ); |
| 125 | |
| 126 | // Send a maximum of once per week by default. |
| 127 | if ( $last_send && $last_send > $last_send_interval ) { |
| 128 | return; |
| 129 | } |
| 130 | } else { |
| 131 | // Make sure there is at least a 1 hour delay between override sends, we dont want duplicate calls due to double clicking links. |
| 132 | if ( $last_send && $last_send > strtotime( '-1 hours' ) ) { |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Update time first before sending to ensure it is set. |
| 138 | update_option( 'elementor_tracker_last_send', time() ); |
| 139 | |
| 140 | $params = self::get_tracking_data( empty( $last_send ) ); |
| 141 | |
| 142 | add_filter( 'https_ssl_verify', '__return_false' ); |
| 143 | |
| 144 | wp_safe_remote_post( |
| 145 | self::$_api_url, |
| 146 | [ |
| 147 | 'timeout' => 25, |
| 148 | 'blocking' => false, |
| 149 | // 'sslverify' => false, |
| 150 | 'body' => [ |
| 151 | 'data' => wp_json_encode( $params ), |
| 152 | ], |
| 153 | ] |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Is allow track. |
| 159 | * |
| 160 | * Checks whether the site admin has opted-in for data tracking, or not. |
| 161 | * |
| 162 | * @since 1.0.0 |
| 163 | * @access public |
| 164 | * @static |
| 165 | */ |
| 166 | public static function is_allow_track() { |
| 167 | return 'yes' === get_option( 'elementor_allow_tracking', 'no' ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Handle tracker actions. |
| 172 | * |
| 173 | * Check if the user opted-in or opted-out and update the database. |
| 174 | * |
| 175 | * Fired by `admin_init` action. |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | * @access public |
| 179 | * @static |
| 180 | */ |
| 181 | public static function handle_tracker_actions() { |
| 182 | if ( ! isset( $_GET['elementor_tracker'] ) ) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if ( 'opt_into' === $_GET['elementor_tracker'] ) { |
| 187 | check_admin_referer( 'opt_into' ); |
| 188 | |
| 189 | self::set_opt_in( true ); |
| 190 | } |
| 191 | |
| 192 | if ( 'opt_out' === $_GET['elementor_tracker'] ) { |
| 193 | check_admin_referer( 'opt_out' ); |
| 194 | |
| 195 | self::set_opt_in( false ); |
| 196 | } |
| 197 | |
| 198 | wp_redirect( remove_query_arg( 'elementor_tracker' ) ); |
| 199 | exit; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @since 2.2.0 |
| 204 | * @access public |
| 205 | * @static |
| 206 | */ |
| 207 | public static function is_notice_shown() { |
| 208 | return self::$notice_shown; |
| 209 | } |
| 210 | |
| 211 | public static function set_opt_in( $value ) { |
| 212 | if ( $value ) { |
| 213 | update_option( 'elementor_allow_tracking', 'yes' ); |
| 214 | self::send_tracking_data( true ); |
| 215 | } else { |
| 216 | update_option( 'elementor_allow_tracking', 'no' ); |
| 217 | update_option( 'elementor_tracker_notice', '1' ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get system reports data. |
| 223 | * |
| 224 | * Retrieve the data from system reports. |
| 225 | * |
| 226 | * @since 2.0.0 |
| 227 | * @access private |
| 228 | * @static |
| 229 | * |
| 230 | * @return array The data from system reports. |
| 231 | */ |
| 232 | private static function get_system_reports_data() { |
| 233 | $reports = Plugin::$instance->system_info->load_reports( System_Info_Module::get_allowed_reports() ); |
| 234 | |
| 235 | $system_reports = []; |
| 236 | foreach ( $reports as $report_key => $report_details ) { |
| 237 | $system_reports[ $report_key ] = []; |
| 238 | foreach ( $report_details['report']->get_report() as $sub_report_key => $sub_report_details ) { |
| 239 | $system_reports[ $report_key ][ $sub_report_key ] = $sub_report_details['value']; |
| 240 | } |
| 241 | } |
| 242 | return $system_reports; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get last send time. |
| 247 | * |
| 248 | * Retrieve the last time tracking data was sent. |
| 249 | * |
| 250 | * @since 2.0.0 |
| 251 | * @access private |
| 252 | * @static |
| 253 | * |
| 254 | * @return int|false The last time tracking data was sent, or false if |
| 255 | * tracking data never sent. |
| 256 | */ |
| 257 | private static function get_last_send_time() { |
| 258 | $last_send_time = get_option( 'elementor_tracker_last_send', false ); |
| 259 | |
| 260 | /** |
| 261 | * Tracker last send time. |
| 262 | * |
| 263 | * Filters the last time tracking data was sent. |
| 264 | * |
| 265 | * @since 1.0.0 |
| 266 | * |
| 267 | * @param int|false $last_send_time The last time tracking data was sent, |
| 268 | * or false if tracking data never sent. |
| 269 | */ |
| 270 | $last_send_time = apply_filters( 'elementor/tracker/last_send_time', $last_send_time ); |
| 271 | |
| 272 | return $last_send_time; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get non elementor post usages. |
| 277 | * |
| 278 | * Retrieve the number of posts that not using elementor. |
| 279 | |
| 280 | * @return array The number of posts using not used by Elementor grouped by post types |
| 281 | * and post status. |
| 282 | */ |
| 283 | public static function get_non_elementor_posts_usage() { |
| 284 | global $wpdb; |
| 285 | |
| 286 | $usage = []; |
| 287 | |
| 288 | $results = $wpdb->get_results( |
| 289 | "SELECT `post_type`, `post_status`, COUNT(`ID`) `hits` |
| 290 | FROM {$wpdb->posts} `p` |
| 291 | LEFT JOIN {$wpdb->postmeta} `pm` ON(`p`.`ID` = `pm`.`post_id` AND `meta_key` = '_elementor_edit_mode' ) |
| 292 | WHERE `post_type` != 'elementor_library' AND `meta_value` IS NULL |
| 293 | GROUP BY `post_type`, `post_status`;" |
| 294 | ); |
| 295 | |
| 296 | if ( $results ) { |
| 297 | foreach ( $results as $result ) { |
| 298 | $usage[ $result->post_type ][ $result->post_status ] = $result->hits; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return $usage; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Get posts usage. |
| 307 | * |
| 308 | * Retrieve the number of posts using Elementor. |
| 309 | * |
| 310 | * @since 2.0.0 |
| 311 | * @access public |
| 312 | * @static |
| 313 | * |
| 314 | * @return array The number of posts using Elementor grouped by post types |
| 315 | * and post status. |
| 316 | */ |
| 317 | public static function get_posts_usage() { |
| 318 | global $wpdb; |
| 319 | |
| 320 | $usage = []; |
| 321 | |
| 322 | $results = $wpdb->get_results( |
| 323 | "SELECT `post_type`, `post_status`, COUNT(`ID`) `hits` |
| 324 | FROM {$wpdb->posts} `p` |
| 325 | LEFT JOIN {$wpdb->postmeta} `pm` ON(`p`.`ID` = `pm`.`post_id`) |
| 326 | WHERE `post_type` != 'elementor_library' |
| 327 | AND `meta_key` = '_elementor_edit_mode' AND `meta_value` = 'builder' |
| 328 | GROUP BY `post_type`, `post_status`;" |
| 329 | ); |
| 330 | |
| 331 | if ( $results ) { |
| 332 | foreach ( $results as $result ) { |
| 333 | $usage[ $result->post_type ][ $result->post_status ] = (int) $result->hits; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return $usage; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Get library usage. |
| 342 | * |
| 343 | * Retrieve the number of Elementor library items saved. |
| 344 | * |
| 345 | * @since 2.0.0 |
| 346 | * @access public |
| 347 | * @static |
| 348 | * |
| 349 | * @return array The number of Elementor library items grouped by post types |
| 350 | * and meta value. |
| 351 | */ |
| 352 | public static function get_library_usage() { |
| 353 | global $wpdb; |
| 354 | |
| 355 | $usage = []; |
| 356 | |
| 357 | $results = $wpdb->get_results( |
| 358 | "SELECT `meta_value`, COUNT(`ID`) `hits` |
| 359 | FROM {$wpdb->posts} `p` |
| 360 | LEFT JOIN {$wpdb->postmeta} `pm` ON(`p`.`ID` = `pm`.`post_id`) |
| 361 | WHERE `post_type` = 'elementor_library' |
| 362 | AND `meta_key` = '_elementor_template_type' |
| 363 | GROUP BY `post_type`, `meta_value`;" |
| 364 | ); |
| 365 | |
| 366 | if ( $results ) { |
| 367 | foreach ( $results as $result ) { |
| 368 | $usage[ $result->meta_value ] = $result->hits; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return $usage; |
| 373 | |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Get usage of general settings. |
| 378 | * 'Elementor->Settings->General'. |
| 379 | * |
| 380 | * @return array |
| 381 | */ |
| 382 | public static function get_settings_general_usage() { |
| 383 | return self::get_tracking_data_from_settings( 'general' ); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Get usage of advanced settings. |
| 388 | * 'Elementor->Settings->Advanced'. |
| 389 | * |
| 390 | * @return array |
| 391 | */ |
| 392 | public static function get_settings_advanced_usage() { |
| 393 | return self::get_tracking_data_from_settings( 'advanced' ); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Get usage of experiments settings. |
| 398 | * |
| 399 | * 'Elementor->Settings->Experiments'. |
| 400 | * |
| 401 | * @return array |
| 402 | */ |
| 403 | public static function get_settings_experiments_usage() { |
| 404 | $system_info = Plugin::$instance->system_info; |
| 405 | |
| 406 | /** |
| 407 | * @var $experiments_report Experiments_Reporter |
| 408 | */ |
| 409 | $experiments_report = $system_info->create_reporter( [ |
| 410 | 'class_name' => Experiments_Reporter::class, |
| 411 | ] ); |
| 412 | |
| 413 | return $experiments_report->get_experiments()['value']; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Get usage of general tools. |
| 418 | * 'Elementor->Tools->General'. |
| 419 | * |
| 420 | * @return array |
| 421 | */ |
| 422 | public static function get_tools_general_usage() { |
| 423 | return self::get_tracking_data_from_tools( 'general' ); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Get usage of 'version control' tools. |
| 428 | * 'Elementor->Tools->Version Control'. |
| 429 | * |
| 430 | * @return array |
| 431 | */ |
| 432 | public static function get_tools_version_control_usage() { |
| 433 | return self::get_tracking_data_from_tools( 'versions' ); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Get usage of 'maintenance' tools. |
| 438 | * 'Elementor->Tools->Maintenance'. |
| 439 | * |
| 440 | * @return array |
| 441 | */ |
| 442 | public static function get_tools_maintenance_usage() { |
| 443 | return self::get_tracking_data_from_tools( 'maintenance_mode' ); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Get library usage extend. |
| 448 | * |
| 449 | * Retrieve the number of Elementor library items saved. |
| 450 | * |
| 451 | * @return array The number of Elementor library items grouped by post types, post status |
| 452 | * and meta value. |
| 453 | */ |
| 454 | public static function get_library_usage_extend() { |
| 455 | global $wpdb; |
| 456 | |
| 457 | $usage = []; |
| 458 | |
| 459 | $results = $wpdb->get_results( |
| 460 | "SELECT `meta_value`, COUNT(`ID`) `hits`, `post_status` |
| 461 | FROM {$wpdb->posts} `p` |
| 462 | LEFT JOIN {$wpdb->postmeta} `pm` ON(`p`.`ID` = `pm`.`post_id`) |
| 463 | WHERE `post_type` = 'elementor_library' |
| 464 | AND `meta_key` = '_elementor_template_type' |
| 465 | GROUP BY `post_type`, `meta_value`, `post_status`;" |
| 466 | ); |
| 467 | |
| 468 | if ( $results ) { |
| 469 | foreach ( $results as $result ) { |
| 470 | if ( empty( $usage[ $result->meta_value ] ) ) { |
| 471 | $usage[ $result->meta_value ] = []; |
| 472 | } |
| 473 | |
| 474 | if ( empty( $usage[ $result->meta_value ][ $result->post_status ] ) ) { |
| 475 | $usage[ $result->meta_value ][ $result->post_status ] = 0; |
| 476 | } |
| 477 | |
| 478 | $usage[ $result->meta_value ][ $result->post_status ] += $result->hits; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return $usage; |
| 483 | |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Get the tracking data |
| 488 | * |
| 489 | * Retrieve tracking data and apply filter |
| 490 | * |
| 491 | * @access public |
| 492 | * @static |
| 493 | * |
| 494 | * @param bool $is_first_time |
| 495 | * |
| 496 | * @return array |
| 497 | */ |
| 498 | public static function get_tracking_data( $is_first_time = false ) { |
| 499 | $params = [ |
| 500 | 'system' => self::get_system_reports_data(), |
| 501 | 'site_lang' => get_bloginfo( 'language' ), |
| 502 | 'email' => get_option( 'admin_email' ), |
| 503 | 'usages' => [ |
| 504 | 'posts' => self::get_posts_usage(), |
| 505 | 'non-elementor-posts' => self::get_non_elementor_posts_usage(), |
| 506 | 'library' => self::get_library_usage(), |
| 507 | 'settings' => [ |
| 508 | 'general' => self::get_settings_general_usage(), |
| 509 | 'advanced' => self::get_settings_advanced_usage(), |
| 510 | 'experiments' => self::get_settings_experiments_usage(), |
| 511 | ], |
| 512 | 'tools' => [ |
| 513 | 'general' => self::get_tools_general_usage(), |
| 514 | 'version' => self::get_tools_version_control_usage(), |
| 515 | 'maintenance' => self::get_tools_maintenance_usage(), |
| 516 | ], |
| 517 | 'library-details' => self::get_library_usage_extend(), |
| 518 | ], |
| 519 | 'is_first_time' => $is_first_time, |
| 520 | 'install_time' => Plugin::instance()->get_install_time(), |
| 521 | ]; |
| 522 | |
| 523 | /** |
| 524 | * Tracker send tracking data params. |
| 525 | * |
| 526 | * Filters the data parameters when sending tracking request. |
| 527 | * |
| 528 | * @param array $params Variable to encode as JSON. |
| 529 | * |
| 530 | * @since 1.0.0 |
| 531 | * |
| 532 | */ |
| 533 | $params = apply_filters( 'elementor/tracker/send_tracking_data_params', $params ); |
| 534 | |
| 535 | return $params; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * @param string $tab_name |
| 540 | * @return array |
| 541 | */ |
| 542 | private static function get_tracking_data_from_settings( $tab_name ) { |
| 543 | return self::get_tracking_data_from_settings_page( |
| 544 | Plugin::$instance->settings->get_tabs(), |
| 545 | $tab_name |
| 546 | ); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * @param string $tab_name |
| 551 | * @return array |
| 552 | */ |
| 553 | private static function get_tracking_data_from_tools( $tab_name ) { |
| 554 | return self::get_tracking_data_from_settings_page( |
| 555 | Plugin::$instance->tools->get_tabs(), |
| 556 | $tab_name |
| 557 | ); |
| 558 | } |
| 559 | |
| 560 | private static function get_tracking_data_from_settings_page( $tabs, $tab_name ) { |
| 561 | $result = []; |
| 562 | |
| 563 | if ( empty( $tabs[ $tab_name ] ) ) { |
| 564 | return $result; |
| 565 | } |
| 566 | |
| 567 | $tab = $tabs[ $tab_name ]; |
| 568 | |
| 569 | foreach ( $tab['sections'] as $section_name => $section ) { |
| 570 | foreach ( $section['fields'] as $field_name => $field ) { |
| 571 | // Skips fields with '_' prefix. |
| 572 | if ( '_' === $field_name[0] ) { |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | $default_value = null; |
| 577 | $args = $field['field_args']; |
| 578 | switch ( $args['type'] ) { |
| 579 | case 'checkbox': |
| 580 | $default_value = $args['value']; |
| 581 | break; |
| 582 | |
| 583 | case 'select': |
| 584 | case 'checkbox_list_cpt': |
| 585 | $default_value = $args['std']; |
| 586 | break; |
| 587 | |
| 588 | case 'checkbox_list_roles': |
| 589 | $default_value = null; |
| 590 | break; |
| 591 | |
| 592 | // 'raw_html' is used as action and not as data. |
| 593 | case 'raw_html': |
| 594 | continue 2; // Skip fields loop. |
| 595 | |
| 596 | default: |
| 597 | trigger_error( 'Invalid type: \'' . $args['type'] . '\'' ); // phpcs:ignore |
| 598 | } |
| 599 | |
| 600 | $result[ $field_name ] = get_option( 'elementor_' . $field_name, $default_value ); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | return $result; |
| 605 | } |
| 606 | } |
| 607 |