ajax-actions.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Author: ExactMetrics team
  4. * Author URI: https://exactmetrics.com
  5. * Copyright 2018 ExactMetrics team
  6. * License: GPLv2 or later
  7. * License URI: http://www.gnu.org/licenses/gpl-2.0.html
  8. */
  9. // Exit if accessed directly
  10. if ( ! defined( 'ABSPATH' ) )
  11. exit();
  12. if ( ! class_exists( 'GADWP_Backend_Ajax' ) ) {
  13. final class GADWP_Backend_Ajax {
  14. private $gadwp;
  15. public function __construct() {
  16. $this->gadwp = GADWP();
  17. if ( GADWP_Tools::check_roles( $this->gadwp->config->options['access_back'] ) && ( ( 1 == $this->gadwp->config->options['backend_item_reports'] ) || ( 1 == $this->gadwp->config->options['dashboard_widget'] ) ) ) {
  18. // Items action
  19. add_action( 'wp_ajax_gadwp_backend_item_reports', array( $this, 'ajax_item_reports' ) );
  20. }
  21. if ( current_user_can( 'manage_options' ) ) {
  22. // Admin Widget action
  23. add_action( 'wp_ajax_gadwp_dismiss_notices', array( $this, 'ajax_dismiss_notices' ) );
  24. }
  25. }
  26. /**
  27. * Ajax handler for Item Reports
  28. *
  29. * @return json|int
  30. */
  31. public function ajax_item_reports() {
  32. if ( ! isset( $_POST['gadwp_security_backend_item_reports'] ) || ! wp_verify_nonce( $_POST['gadwp_security_backend_item_reports'], 'gadwp_backend_item_reports' ) ) {
  33. wp_die( - 30 );
  34. }
  35. if ( isset( $_POST['projectId'] ) && $this->gadwp->config->options['switch_profile'] && 'false' !== $_POST['projectId'] ) {
  36. $projectId = $_POST['projectId'];
  37. } else {
  38. $projectId = false;
  39. }
  40. $from = $_POST['from'];
  41. $to = $_POST['to'];
  42. $query = $_POST['query'];
  43. if ( isset( $_POST['filter'] ) ) {
  44. $filter_id = $_POST['filter'];
  45. } else {
  46. $filter_id = false;
  47. }
  48. if ( isset( $_POST['metric'] ) ) {
  49. $metric = $_POST['metric'];
  50. } else {
  51. $metric = 'sessions';
  52. }
  53. if ( $filter_id && $metric == 'sessions' ) { // Sessions metric is not available for item reports
  54. $metric = 'pageviews';
  55. }
  56. if ( ob_get_length() ) {
  57. ob_clean();
  58. }
  59. if ( ! ( GADWP_Tools::check_roles( $this->gadwp->config->options['access_back'] ) && ( ( 1 == $this->gadwp->config->options['backend_item_reports'] ) || ( 1 == $this->gadwp->config->options['dashboard_widget'] ) ) ) ) {
  60. wp_die( - 31 );
  61. }
  62. if ( $this->gadwp->config->options['token'] && $this->gadwp->config->options['tableid_jail'] && $from && $to ) {
  63. if ( null === $this->gadwp->gapi_controller ) {
  64. $this->gadwp->gapi_controller = new GADWP_GAPI_Controller();
  65. }
  66. } else {
  67. wp_die( - 24 );
  68. }
  69. if ( false == $projectId ) {
  70. $projectId = $this->gadwp->config->options['tableid_jail'];
  71. }
  72. $profile_info = GADWP_Tools::get_selected_profile( $this->gadwp->config->options['ga_profiles_list'], $projectId );
  73. if ( isset( $profile_info[4] ) ) {
  74. $this->gadwp->gapi_controller->timeshift = $profile_info[4];
  75. } else {
  76. $this->gadwp->gapi_controller->timeshift = (int) current_time( 'timestamp' ) - time();
  77. }
  78. if ( $filter_id ) {
  79. $uri_parts = explode( '/', get_permalink( $filter_id ), 4 );
  80. if ( isset( $uri_parts[3] ) ) {
  81. $uri = '/' . $uri_parts[3];
  82. } else {
  83. wp_die( - 25 );
  84. }
  85. // allow URL correction before sending an API request
  86. $filter = apply_filters( 'gadwp_backenditem_uri', $uri, $filter_id );
  87. $lastchar = substr( $filter, - 1 );
  88. if ( isset( $profile_info[6] ) && $profile_info[6] && '/' == $lastchar ) {
  89. $filter = $filter . $profile_info[6];
  90. }
  91. // Encode URL
  92. $filter = rawurlencode( rawurldecode( $filter ) );
  93. } else {
  94. $filter = false;
  95. }
  96. $queries = explode( ',', $query );
  97. $results = array();
  98. foreach ( $queries as $value ) {
  99. $results[] = $this->gadwp->gapi_controller->get( $projectId, $value, $from, $to, $filter, $metric );
  100. }
  101. wp_send_json( $results );
  102. }
  103. /**
  104. * Ajax handler for dismissing Admin notices
  105. *
  106. * @return json|int
  107. */
  108. public function ajax_dismiss_notices() {
  109. if ( ! isset( $_POST['gadwp_security_dismiss_notices'] ) || ! wp_verify_nonce( $_POST['gadwp_security_dismiss_notices'], 'gadwp_dismiss_notices' ) ) {
  110. wp_die( - 30 );
  111. }
  112. if ( ! current_user_can( 'manage_options' ) ) {
  113. wp_die( - 31 );
  114. }
  115. delete_option( 'gadwp_got_updated' );
  116. wp_die();
  117. }
  118. }
  119. }