ajax-actions.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_Frontend_Ajax' ) ) {
  13. final class GADWP_Frontend_Ajax {
  14. private $gadwp;
  15. public function __construct() {
  16. $this->gadwp = GADWP();
  17. if ( GADWP_Tools::check_roles( $this->gadwp->config->options['access_front'] ) && $this->gadwp->config->options['frontend_item_reports'] ) {
  18. // Item Reports action
  19. add_action( 'wp_ajax_gadwp_frontend_item_reports', array( $this, 'ajax_item_reports' ) );
  20. }
  21. // Frontend Widget actions
  22. add_action( 'wp_ajax_ajax_frontwidget_report', array( $this, 'ajax_frontend_widget' ) );
  23. add_action( 'wp_ajax_nopriv_ajax_frontwidget_report', array( $this, 'ajax_frontend_widget' ) );
  24. }
  25. /**
  26. * Ajax handler for Item Reports
  27. *
  28. * @return string|int
  29. */
  30. public function ajax_item_reports() {
  31. if ( ! isset( $_POST['gadwp_security_frontend_item_reports'] ) || ! wp_verify_nonce( $_POST['gadwp_security_frontend_item_reports'], 'gadwp_frontend_item_reports' ) ) {
  32. wp_die( - 30 );
  33. }
  34. $from = $_POST['from'];
  35. $to = $_POST['to'];
  36. $query = $_POST['query'];
  37. $uri = $_POST['filter'];
  38. if ( isset( $_POST['metric'] ) ) {
  39. $metric = $_POST['metric'];
  40. } else {
  41. $metric = 'pageviews';
  42. }
  43. $query = $_POST['query'];
  44. if ( ob_get_length() ) {
  45. ob_clean();
  46. }
  47. if ( ! GADWP_Tools::check_roles( $this->gadwp->config->options['access_front'] ) || 0 == $this->gadwp->config->options['frontend_item_reports'] ) {
  48. wp_die( - 31 );
  49. }
  50. if ( $this->gadwp->config->options['token'] && $this->gadwp->config->options['tableid_jail'] ) {
  51. if ( null === $this->gadwp->gapi_controller ) {
  52. $this->gadwp->gapi_controller = new GADWP_GAPI_Controller();
  53. }
  54. } else {
  55. wp_die( - 24 );
  56. }
  57. if ( $this->gadwp->config->options['tableid_jail'] ) {
  58. $projectId = $this->gadwp->config->options['tableid_jail'];
  59. } else {
  60. wp_die( - 26 );
  61. }
  62. $profile_info = GADWP_Tools::get_selected_profile( $this->gadwp->config->options['ga_profiles_list'], $projectId );
  63. if ( isset( $profile_info[4] ) ) {
  64. $this->gadwp->gapi_controller->timeshift = $profile_info[4];
  65. } else {
  66. $this->gadwp->gapi_controller->timeshift = (int) current_time( 'timestamp' ) - time();
  67. }
  68. $uri = '/' . ltrim( $uri, '/' );
  69. // allow URL correction before sending an API request
  70. $filter = apply_filters( 'gadwp_frontenditem_uri', $uri );
  71. $lastchar = substr( $filter, - 1 );
  72. if ( isset( $profile_info[6] ) && $profile_info[6] && '/' == $lastchar ) {
  73. $filter = $filter . $profile_info[6];
  74. }
  75. // Encode URL
  76. $filter = rawurlencode( rawurldecode( $filter ) );
  77. $queries = explode( ',', $query );
  78. $results = array();
  79. foreach ( $queries as $value ) {
  80. $results[] = $this->gadwp->gapi_controller->get( $projectId, $value, $from, $to, $filter, $metric );
  81. }
  82. wp_send_json( $results );
  83. }
  84. /**
  85. * Ajax handler for getting analytics data for frontend Widget
  86. *
  87. * @return string|int
  88. */
  89. public function ajax_frontend_widget() {
  90. if ( ! isset( $_POST['gadwp_number'] ) || ! isset( $_POST['gadwp_optionname'] ) || ! is_active_widget( false, false, 'gadwp-frontwidget-report' ) ) {
  91. wp_die( - 30 );
  92. }
  93. $widget_index = $_POST['gadwp_number'];
  94. $option_name = $_POST['gadwp_optionname'];
  95. $options = get_option( $option_name );
  96. if ( isset( $options[$widget_index] ) ) {
  97. $instance = $options[$widget_index];
  98. } else {
  99. wp_die( - 32 );
  100. }
  101. switch ( $instance['period'] ) { // make sure we have a valid request
  102. case '7daysAgo' :
  103. $period = '7daysAgo';
  104. break;
  105. case '14daysAgo' :
  106. $period = '14daysAgo';
  107. break;
  108. default :
  109. $period = '30daysAgo';
  110. break;
  111. }
  112. if ( ob_get_length() ) {
  113. ob_clean();
  114. }
  115. if ( $this->gadwp->config->options['token'] && $this->gadwp->config->options['tableid_jail'] ) {
  116. if ( null === $this->gadwp->gapi_controller ) {
  117. $this->gadwp->gapi_controller = new GADWP_GAPI_Controller();
  118. }
  119. } else {
  120. wp_die( - 24 );
  121. }
  122. $projectId = $this->gadwp->config->options['tableid_jail'];
  123. $profile_info = GADWP_Tools::get_selected_profile( $this->gadwp->config->options['ga_profiles_list'], $projectId );
  124. if ( isset( $profile_info[4] ) ) {
  125. $this->gadwp->gapi_controller->timeshift = $profile_info[4];
  126. } else {
  127. $this->gadwp->gapi_controller->timeshift = (int) current_time( 'timestamp' ) - time();
  128. }
  129. wp_send_json( $this->gadwp->gapi_controller->frontend_widget_stats( $projectId, $period, (int) $instance['anonim'] ) );
  130. }
  131. }
  132. }