reports.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Reports class.
  4. *
  5. * @since 6.0.0
  6. *
  7. * @package MonsterInsights
  8. * @subpackage Reports
  9. * @author Chris Christoff
  10. */
  11. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. function monsterinsights_reports_page_body_class( $classes ) {
  16. if ( ! empty( $_REQUEST['page'] ) && $_REQUEST['page'] === 'monsterinsights_reports' ) {
  17. $classes .= ' monsterinsights-reporting-page ';
  18. }
  19. return $classes;
  20. }
  21. add_filter( 'admin_body_class', 'monsterinsights_reports_page_body_class' );
  22. /**
  23. * Callback for getting all of the reports tabs for MonsterInsights.
  24. *
  25. * @since 6.0.0
  26. * @access public
  27. *
  28. * @return array Array of tab information.
  29. */
  30. function monsterinsights_get_reports() {
  31. /**
  32. * Developer Alert:
  33. *
  34. * Per the README, this is considered an internal hook and should
  35. * not be used by other developers. This hook's behavior may be modified
  36. * or the hook may be removed at any time, without warning.
  37. */
  38. $reports = apply_filters( 'monsterinsights_get_reports', array() );
  39. return $reports;
  40. }
  41. /**
  42. * Callback to output the MonsterInsights reports page.
  43. *
  44. * @since 6.0.0
  45. * @access public
  46. *
  47. * @return void
  48. */
  49. function monsterinsights_reports_page() {
  50. /**
  51. * Developer Alert:
  52. *
  53. * Per the README, this is considered an internal hook and should
  54. * not be used by other developers. This hook's behavior may be modified
  55. * or the hook may be removed at any time, without warning.
  56. */
  57. do_action( 'monsterinsights_head' );
  58. echo monsterinsights_ublock_notice(); ?>
  59. <!-- Tabs -->
  60. <h1 id="monsterinsights-reports-page-main-nav" class="monsterinsights-main-nav-container monsterinsights-nav-container" data-container="#monsterinsights-reports-pages" data-update-hashbang="1">
  61. <?php
  62. $i = 0;
  63. ?>
  64. <?php
  65. foreach ( (array) MonsterInsights()->reporting->reports as $report ) {
  66. ?>
  67. <a class="monsterinsights-main-nav-item monsterinsights-nav-item " href="#monsterinsights-main-tab-<?php echo esc_attr( $report->name ); ?>" title="<?php echo esc_attr( $report->title ); ?>"><?php echo esc_html( $report->title ); ?></a>
  68. <?php
  69. $i++;
  70. }
  71. ?>
  72. </h1>
  73. <div id="monsterinsights-reports" class="wrap monsterinsights-bootstrap-container">
  74. <div class="monsterinsights-clear">
  75. <!-- Tab Panels -->
  76. <div id="monsterinsights-reports-pages" class="monsterinsights-main-nav-tabs monsterinsights-nav-tabs" data-navigation="#monsterinsights-reports-page-main-nav">
  77. <?php
  78. $i = 0;
  79. foreach ( (array) MonsterInsights()->reporting->reports as $report ) {
  80. ?>
  81. <div id="monsterinsights-main-tab-<?php echo esc_attr( $report->name ); ?>" class="monsterinsights-main-nav-tab monsterinsights-nav-tab">
  82. <div class="monsterinsights-reports-action-bar monsterinsights-clear">
  83. <div class="monsterinsights-reports-action-bar-title">
  84. <?php echo esc_html( sprintf( __( '%s Report', 'google-analytics-for-wordpress' ), $report->title ) ); ?>
  85. </div>
  86. <div class="monsterinsights-reports-action-bar-actions"><?php
  87. /**
  88. * Developer Alert:
  89. *
  90. * Per the README, this is considered an internal hook and should
  91. * not be used by other developers. This hook's behavior may be modified
  92. * or the hook may be removed at any time, without warning.
  93. */
  94. do_action( 'monsterinsights_tab_reports_actions' );
  95. ?>
  96. </div>
  97. </div>
  98. <div class="monsterinsights-reports-wrap container-fluid">
  99. <?php
  100. /**
  101. * Developer Alert:
  102. *
  103. * Per the README, this is considered an internal hook and should
  104. * not be used by other developers. This hook's behavior may be modified
  105. * or the hook may be removed at any time, without warning.
  106. */
  107. ?>
  108. <?php do_action( 'monsterinsights_tab_reports_notices' ); ?>
  109. <?php //echo $report->show_report(); @todo we don't need to init output them. ?>
  110. </div>
  111. </div>
  112. <?php
  113. $i++;
  114. }
  115. ?>
  116. </div>
  117. </div>
  118. </div>
  119. <?php
  120. }
  121. function monsterinsights_refresh_reports_data() {
  122. check_ajax_referer( 'mi-admin-nonce', 'security' );
  123. // Get variables
  124. $start = ! empty( $_REQUEST['start'] ) ? $_REQUEST['start'] : '';
  125. $end = ! empty( $_REQUEST['end'] ) ? $_REQUEST['end'] : '';
  126. $name = ! empty( $_REQUEST['report'] ) ? $_REQUEST['report'] : '';
  127. $isnetwork = ! empty( $_REQUEST['isnetwork'] ) ? $_REQUEST['isnetwork'] : '';
  128. // Current user can authenticate
  129. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) {
  130. wp_send_json_error( array( 'message' => __( "You don't have permission to view MonsterInsights reports.", 'google-analytics-for-wordpress' ) ) );
  131. }
  132. if ( ! empty( $_REQUEST['isnetwork'] ) && $_REQUEST['isnetwork'] ) {
  133. define( 'WP_NETWORK_ADMIN', true );
  134. }
  135. // Only for Pro users, require a license key to be entered first so we can link to things.
  136. if ( monsterinsights_is_pro_version() ) {
  137. if ( ! MonsterInsights()->license->is_site_licensed() && ! MonsterInsights()->license->is_network_licensed() ) {
  138. wp_send_json_error( array( 'message' => __( "You can't view MonsterInsights reports because you are not licensed.", 'google-analytics-for-wordpress' ) ) );
  139. } else if ( MonsterInsights()->license->is_site_licensed() && ! MonsterInsights()->license->site_license_has_error() ) {
  140. // good to go: site licensed
  141. } else if ( MonsterInsights()->license->is_network_licensed() && ! MonsterInsights()->license->network_license_has_error() ) {
  142. // good to go: network licensed
  143. } else {
  144. wp_send_json_error( array( 'message' => __( "You can't view MonsterInsights reports due to license key errors.", 'google-analytics-for-wordpress' ) ) );
  145. }
  146. }
  147. // we do not have a current auth
  148. $site_auth = MonsterInsights()->auth->get_viewname();
  149. $ms_auth = is_multisite() && MonsterInsights()->auth->get_network_viewname();
  150. if ( ! $site_auth && ! $ms_auth ) {
  151. wp_send_json_error( array( 'message' => __( "You must authenticate with MonsterInsights before you can view reports.", 'google-analytics-for-wordpress' ) ) );
  152. }
  153. if ( empty( $name ) ) {
  154. wp_send_json_error( array( 'message' => __( "Unknown report. Try refreshing and retrying. Contact support if this issue persists.", 'google-analytics-for-wordpress' ) ) );
  155. }
  156. $report = MonsterInsights()->reporting->get_report( $name );
  157. if ( empty( $report ) ) {
  158. wp_send_json_error( array( 'message' => __( "Unknown report. Try refreshing and retrying. Contact support if this issue persists.", 'google-analytics-for-wordpress' ) ) );
  159. }
  160. $args = array( 'start' => $start, 'end' => $end );
  161. if ( $isnetwork ) {
  162. $args['network'] = true;
  163. }
  164. $data = $report->get_data( $args );
  165. if ( ! empty( $data['success'] ) ) {
  166. $data = $report->show_report(
  167. array( 'start' => $start,
  168. 'end' => $end,
  169. 'data' => $data['data'],
  170. 'success' => true
  171. )
  172. );
  173. wp_send_json_success( array( 'html' => $data ) );
  174. } else {
  175. wp_send_json_error( array( 'message' => $data['error'], 'data' => $data['data'] ) );
  176. }
  177. }
  178. add_action( 'wp_ajax_monsterinsights_refresh_reports', 'monsterinsights_refresh_reports_data' );