/wp-content/plugins/google-analytics-for-wordpress/includes/admin/reports/abstract-report.php

https://bitbucket.org/carloskikea/helpet · PHP · 375 lines · 287 code · 51 blank · 37 comment · 78 complexity · d4412a2d76602d56e3dafc762a676057 MD5 · raw file

  1. <?php
  2. /**
  3. * Report Abstract
  4. *
  5. * Ensures all of the reports have a uniform class with helper functions.
  6. *
  7. * @since 6.0.0
  8. *
  9. * @package MonsterInsights
  10. * @subpackage Reports
  11. * @author Chris Christoff
  12. */
  13. // Exit if accessed directly
  14. if ( ! defined( 'ABSPATH' ) ) {
  15. exit;
  16. }
  17. class MonsterInsights_Report {
  18. public $title;
  19. public $class;
  20. public $name;
  21. public $version = '1.0.0';
  22. /**
  23. * Primary class constructor.
  24. *
  25. * @access public
  26. * @since 6.0.0
  27. */
  28. public function __construct() {
  29. add_filter( 'monsterinsights_reports_abstract_get_data_pre_cache', array( $this, 'requirements' ), 10 , 3 );
  30. }
  31. // Let's get the HTML to output for a particular report. This is not the AJAX endpoint. Args can hold things (generally start/end date range)
  32. protected function get_report_html( $args = array() ) {
  33. /* Defined in the report class */
  34. // For ajax, args start, end, and data will be set with the data to use. Else call $this->get_data( array( 'default' => true ) )
  35. return '';
  36. }
  37. public function additional_data(){
  38. return array();
  39. }
  40. public function requirements( $error = false, $args = array(), $name = '' ) {
  41. return $error;
  42. }
  43. public function show_report( $args = array() ) {
  44. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) {
  45. return monsterinsights_get_message( 'error', esc_html__( 'Access denied' , 'google-analytics-for-wordpress' ) );
  46. }
  47. if ( monsterinsights_get_option( 'dashboard_disabled', false ) ) {
  48. if ( current_user_can( 'monsterinsights_save_settings' ) ) {
  49. $url = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_settings' ) : admin_url( 'admin.php?page=monsterinsights_settings' );
  50. return monsterinsights_get_message( 'error',
  51. sprintf(
  52. esc_html__( 'Please %1$senable the dashboard%2$s to see report data.', 'google-analytics-for-wordpress' ),
  53. '<a href="' . $url . '">',
  54. '</a>'
  55. )
  56. );
  57. } else {
  58. return monsterinsights_get_message( 'error', esc_html__( 'The dashboard is disabled.', 'google-analytics-for-wordpress' ) );
  59. }
  60. }
  61. if ( monsterinsights_is_pro_version() ){
  62. if ( ! MonsterInsights()->license->has_license() ) {
  63. $url = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_settings' ) : admin_url( 'admin.php?page=monsterinsights_settings' );
  64. return monsterinsights_get_message( 'error', esc_html__( 'You do not have an active license. Please %1$scheck your license configuration.%2$s', 'google-analytics-for-wordpress' ),'<a href="' . $url . '">','</a>' );
  65. } else if ( MonsterInsights()->license->license_has_error() ) {
  66. return monsterinsights_get_message( 'error', $this->get_license_error() );
  67. }
  68. }
  69. if ( ! ( MonsterInsights()->auth->is_authed() || MonsterInsights()->auth->is_network_authed() ) ) {
  70. if ( current_user_can( 'monsterinsights_save_settings' ) ) {
  71. $url = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_settings' ) : admin_url( 'admin.php?page=monsterinsights_settings' );
  72. return monsterinsights_get_message( 'error',
  73. sprintf(
  74. esc_html__( 'Please %1$sauthenticate %2$swith Google Analytics to allow the plugin to fetch data.', 'google-analytics-for-wordpress' ),
  75. '<a href="' . $url . '">',
  76. '</a>'
  77. )
  78. );
  79. } else {
  80. return monsterinsights_get_message( 'error', esc_html__( 'The Google oAuth authentication needs to be re-authenticated to view data.', 'google-analytics-for-wordpress' ) );
  81. }
  82. }
  83. if ( ! MonsterInsights()->license->license_can( $this->level ) ) {
  84. return $this->get_upsell_notice();
  85. }
  86. $error = $this->requirements( false, array(), $this->name );
  87. if ( ! empty( $error ) ) {
  88. return monsterinsights_get_message( 'error', $error );
  89. }
  90. if ( ! empty( $args['error'] ) ) {
  91. return monsterinsights_get_message( 'error', $args['error'] );
  92. }
  93. if ( empty( $args['data' ] ) || ! is_array( $args['data' ] ) ) {
  94. if ( monsterinsights_is_pro_version() ) {
  95. return '';
  96. } else {
  97. // Try to get default data.
  98. $args = $this->get_data( array( 'default' => true ) );
  99. if ( empty( $args['data'] ) || is_array( $args['data' ] ) ) {
  100. return monsterinsights_get_message( 'error', __( 'No data found', 'google-analytics-for-wordpress' ) );
  101. }
  102. if ( ! empty( $args['error'] ) ) {
  103. return monsterinsights_get_message( 'error', $data['error'] );
  104. }
  105. }
  106. }
  107. return $this->get_report_html( $args['data'] );
  108. }
  109. // Deletes the report data from the cache
  110. public function delete_cache( $where = 'site' ) {
  111. if ( $where === 'site' || $where === 'both' ) {
  112. delete_option( 'monsterinsights_report_data_' . $this->name );
  113. }
  114. if ( $where === 'network' || $where === 'both' ) {
  115. delete_option( 'monsterinsights_network_report_data_' . $this->name );
  116. }
  117. }
  118. // Get report data
  119. public function get_data( $args = array() ) {
  120. if ( ! empty( $args['default'] ) ) {
  121. $args['start'] = $this->default_start_date();
  122. $args['end'] = $this->default_end_date();
  123. }
  124. $start = ! empty( $args['start'] ) && $this->is_valid_date( $args['start'] ) ? $args['start'] : '';
  125. $end = ! empty( $args['end'] ) && $this->is_valid_date( $args['end'] ) ? $args['end'] : '';
  126. if ( ! MonsterInsights()->license->license_can( $this->level ) ) {
  127. return array(
  128. 'success' => true,
  129. 'upgrade' => true,
  130. 'data' => array(),
  131. );
  132. }
  133. if ( ! $this->is_valid_date_range( $start, $end ) ) {
  134. return array(
  135. 'success' => false,
  136. 'error' => __( 'Invalid date range.', 'google-analytics-for-wordpress' ),
  137. 'data' => array(),
  138. );
  139. }
  140. if ( ( $start !== $this->default_start_date() || $end !== $this->default_end_date() ) && ! monsterinsights_is_pro_version() ) {
  141. return array(
  142. 'success' => false,
  143. 'error' => __( 'Please upgrade to MonsterInsights Pro to use custom date ranges.', 'google-analytics-for-wordpress' ),
  144. 'data' => array(),
  145. );
  146. }
  147. $error = apply_filters( 'monsterinsights_reports_abstract_get_data_pre_cache', false, $args, $this->name );
  148. if ( $error ) {
  149. return array(
  150. 'success' => false,
  151. 'error' => $error,
  152. 'data' => array(),
  153. );
  154. }
  155. $check_cache = ( $start === $this->default_start_date() && $end === $this->default_end_date() );
  156. $site_auth = MonsterInsights()->auth->get_viewname();
  157. $ms_auth = is_multisite() && MonsterInsights()->auth->get_network_viewname();
  158. $transient = 'monsterinsights_report_' . $this->name . '_' . $start . '_' . $end;
  159. // Set to same time as MI cache. MI caches same day to 15 and others to 1 day, so there's no point pinging MI before then.
  160. $expiration = $end === date( 'Y-m-d' ) ? 15 * MINUTE_IN_SECONDS : DAY_IN_SECONDS;
  161. // Default date range, check
  162. if ( $site_auth || $ms_auth ) {
  163. // Single site or MS with auth at subsite
  164. $option_name = $site_auth ? 'monsterinsights_report_data_' . $this->name : 'monsterinsights_network_report_data_' . $this->name;
  165. $p = $site_auth ? MonsterInsights()->auth->get_viewid() : MonsterInsights()->auth->get_network_viewid();
  166. $data = array();
  167. if ( $check_cache ) {
  168. $data = ! $site_auth && $ms_auth ? get_site_option( $option_name, array() ) : get_option( $option_name, array() );
  169. } else {
  170. $data = ! $site_auth && $ms_auth ? get_site_transient( $transient ) : get_transient( $transient );
  171. }
  172. if ( ! empty( $data ) &&
  173. ! empty( $data['expires'] ) &&
  174. $data['expires'] >= time() &&
  175. ! empty( $data['data'] ) &&
  176. ! empty( $data['p'] ) &&
  177. $data['p'] === $p
  178. ) {
  179. return array(
  180. 'success' => true,
  181. 'data' => $data['data'],
  182. );
  183. }
  184. // Nothing in cache, either not saved before, expired or mismatch. Let's grab from API
  185. $api_options = array( 'start' => $start, 'end' => $end);
  186. if ( ! $site_auth && $ms_auth ) {
  187. $api_options['network'] = true;
  188. }
  189. $api = new MonsterInsights_API_Request( 'analytics/reports/' . $this->name . '/', $api_options, 'GET' );
  190. $additional_data = $this->additional_data();
  191. if ( ! empty( $additional_data ) ) {
  192. $api->set_additional_data( $additional_data );
  193. }
  194. $ret = $api->request();
  195. //echo print_r( $ret['data']);wp_die();
  196. if ( is_wp_error( $ret ) ) {
  197. return array(
  198. 'success' => false,
  199. 'error' => $ret->get_error_message(),
  200. 'data' => array(),
  201. );
  202. } else {
  203. // Success
  204. $data = array(
  205. 'expires' => $expiration,
  206. 'p' => $p,
  207. 'data' => $ret['data'],
  208. );
  209. if ( $check_cache ) {
  210. ! $site_auth && $ms_auth ? update_site_option( $option_name, $data ) : update_option( $option_name, $data );
  211. } else {
  212. ! $site_auth && $ms_auth ? set_site_transient( $option_name, $data, $expiration ) : set_transient( $option_name, $data, $expiration );
  213. }
  214. return array(
  215. 'success' => true,
  216. 'data' => $ret['data'],
  217. );
  218. }
  219. } else {
  220. return array(
  221. 'success' => false,
  222. 'error' => __( 'You must authenticate with MonsterInsights to use reports.', 'google-analytics-for-wordpress' ),
  223. 'data' => array(),
  224. );
  225. }
  226. }
  227. public function default_start_date() {
  228. return date( 'Y-m-d', strtotime( '-30 days' ) );
  229. }
  230. public function default_end_date() {
  231. return date( 'Y-m-d', strtotime( '-1 day' ) );
  232. }
  233. // Checks to see if date range is valid. Should be 30-yesterday always for lite & any valid date range to today for Pro.
  234. public function is_valid_date_range( $start, $end ) {
  235. $start = strtotime( $start );
  236. $end = strtotime( $end );
  237. if ( $start > strtotime( 'now' ) || $end > strtotime( 'now' ) || $start < strtotime( '01 January 2005' ) || $end < strtotime( '01 January 2005' ) ) {
  238. return false;
  239. }
  240. // return false if the start date is after the end date
  241. return ( $start > $end ) ? false : true;
  242. }
  243. // Is a valid date value
  244. public function is_valid_date( $date = '' ) {
  245. $d = MonsterInsightsDateTime::createFromFormat( 'Y-m-d', $date );
  246. return $d && $d->format('Y-m-d') === $date;
  247. }
  248. /**
  249. * Do not use the functions below this. They are unused and are just here so people
  250. * with out of date MonsterInsights addons won't get fatal errors.
  251. */
  252. protected function get_api_max_limit() {return 300;}
  253. protected function get_date_range() {return array();}
  254. public function get_upsell_notice() {
  255. $has_level = MonsterInsights()->license->get_license_type();
  256. $has_level = $has_level ? $has_level : 'lite';
  257. $message = sprintf( __( 'You currently have a %s level license, but this report requires at least a %s level license to view the %s. Please upgrade to view this report.', 'google-analytics-for-wordpress' ), $has_level, $this->level, $this->title );
  258. ob_start();?>
  259. <div class="monsterinsights-upsell-report-container monsterinsights-upsell-report-<?php echo $this->name;?>-bg">
  260. <div class="monsterinsights-upsell-container">
  261. <div class="row justify-content-center">
  262. <div class="col-lg-10 col-lg-offset-1 align-self-center">
  263. <div class="monsterinsights-upsell-card">
  264. <img class="monsterinsights-upgrade-mascot" src="<?php echo trailingslashit( MONSTERINSIGHTS_PLUGIN_URL );?>assets/css/images/mascot.png" srcset="<?php echo trailingslashit( MONSTERINSIGHTS_PLUGIN_URL );?>assets/css/images/mascot@2x.png 2x" alt="">
  265. <div class="monsterinsights-upsell-card-card-content">
  266. <span class="monsterinsights-upsell-card-title"><?php esc_html_e( 'Ready to Get Analytics Super-Powers?', 'google-analytics-for-wordpress' );?></span>
  267. <p class="monsterinsights-upsell-card-subtitle"><strong><?php esc_html_e( '(And Crush Your Competition?)', 'google-analytics-for-wordpress' );?></strong></p> &nbsp;
  268. <?php if ( monsterinsights_is_pro_version() ) { ?>
  269. <p ><?php echo sprintf( esc_html__( "Hey there! It looks like you've got the %s license installed on your site.
  270. That's awesome! %s",'google-analytics-for-wordpress'), $has_level, '<span class="dashicons dashicons-smiley"></span>' ); ?></p>
  271. &nbsp;
  272. <p><?php echo sprintf( esc_html__( "Do you you want to access to %s reporting right now%s in your WordPress Dashboard? That comes with %s level%s of our paid packages. You'll need to upgrade your license to get instant access.",'google-analytics-for-wordpress'), '<strong>' . $this->title, '</strong>','<a href="https://monsterinsights.com/my-account/">' . $this->level,'</a>' ); ?></p>
  273. &nbsp;<p><?php echo sprintf( esc_html__( "It's easy! To upgrade, navigate to %sMy Account%s on MonsterInsights.com, go to the licenses tab, and click upgrade. We also have a %sstep by step guide%s with pictures of this process.",'google-analytics-for-wordpress'), '<a href="https://monsterinsights.com/my-account/?utm_source=wpdashboard&utm_campaign=reportupsellpro"><strong>','</strong></a>', '<a href="https://www.monsterinsights.com/docs/upgrade-monsterinsights-license/" style="text-decoration:underline !important">', '</a>' ); ?></p>
  274. &nbsp;<p><?php esc_html_e( "If you have any questions, don't hesitate to reach out. We're here to help.", 'google-analytics-for-wordpress');?></p>
  275. <?php } else { ?>
  276. <p><?php echo sprintf( esc_html__( "Hey there! %s It looks like you've got the free version of MonsterInsights installed on your site.
  277. That's awesome!",'google-analytics-for-wordpress'), '<span class="dashicons dashicons-smiley"></span>' ); ?></p>
  278. &nbsp;
  279. <p><?php echo sprintf( esc_html__( "Do you you want to access to %s reporting right now%s in your WordPress Dashboard? That comes with %s level%s of our paid packages. To get instant access, you'll want to buy a MonsterInsights license, which also gives you access to powerful addons, expanded reporting (including the ability to use custom date ranges), comprehensive tracking features (like UserID tracking) and access to our world-class support team.",'google-analytics-for-wordpress'), '<strong>' . $this->title, '</strong>','<a href="https://monsterinsights.com/lite/">' . $this->level,'</a>' ); ?></p>
  280. &nbsp;<p><?php echo sprintf( esc_html__( "Upgrading is easy! To upgrade, navigate to %sour pricing page%s, purchase the required license, and then follow the %sinstructions in the email receipt%s to upgrade. It only takes a few minutes to unlock the most powerful, yet easy to use analytics tracking system for WordPress.",'google-analytics-for-wordpress'), '<a href="https://monsterinsights.com/lite/?utm_source=wpdashboard&utm_campaign=reportupselllite"><strong>', '</strong></a>','<a style="text-decoration:underline !important" href="https://www.monsterinsights.com/docs/go-lite-pro/">', '</a>' ); ?></p>
  281. &nbsp;<p><?php esc_html_e( "If you have any questions, don't hesitate to reach out. We're here to help.", 'google-analytics-for-wordpress');?></p>
  282. <?php } ?>
  283. </div>
  284. <div class="monsterinsights-upsell-card-action">
  285. <?php if ( monsterinsights_is_pro_version() ) { ?>
  286. <a href="https://monsterinsights.com/my-account/?utm_source=wpdashboard&utm_campaign=reportupsellpro" class="monsterinsights-upsell-card-button"><?php esc_html_e( 'Upgrade Now', 'google-analytics-for-wordpress' ); ?></a>
  287. <?php } else { ?>
  288. <a href="https://monsterinsights.com/lite/?utm_source=wpdashboard&utm_campaign=reportupselllite" class="monsterinsights-upsell-card-button"><?php esc_html_e( 'Get MonsterInsights Pro', 'google-analytics-for-wordpress' ); ?></a>
  289. <?php } ?>
  290. </div>
  291. </div>
  292. </div>
  293. </div>
  294. </div>
  295. </div>
  296. </div>
  297. <?php
  298. return ob_get_clean();
  299. }
  300. function get_ga_report_range( $data = array() ) {
  301. if ( empty( $data['reportcurrentrange'] ) || empty( $data['reportcurrentrange']['startDate'] ) || empty( $data['reportcurrentrange']['endDate'] ) ) {
  302. return '';
  303. } else {
  304. if ( ! empty( $data['reportprevrange'] ) && ! empty( $data['reportprevrange']['startDate'] ) && ! empty( $data['reportprevrange']['endDate'] ) ) {
  305. return '%3F_u.date00%3D' . str_replace( '-', '', $data['reportcurrentrange']['startDate'] ) .'%26_u.date01%3D' . str_replace( '-', '', $data['reportcurrentrange']['endDate'] ) . '%26_u.date10%3D' . str_replace( '-', '', $data['reportprevrange']['startDate'] ) .'%26_u.date11%3D' . str_replace( '-', '', $data['reportprevrange']['endDate'] ) . '/';
  306. } else {
  307. return '%3F_u.date00%3D' . str_replace( '-', '', $data['reportcurrentrange']['startDate'] ) .'%26_u.date01%3D' . str_replace( '-', '', $data['reportcurrentrange']['endDate'] ) . '/';
  308. }
  309. }
  310. }
  311. }
  312. if ( ! class_exists( 'MonsterInsightsDateTime' ) ) {
  313. class MonsterInsightsDateTime extends DateTime {
  314. public static function createFromFormat( $format, $time, $timezone = null ) {
  315. if ( ! $timezone ) {
  316. $timezone = new DateTimeZone( date_default_timezone_get() );
  317. }
  318. if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
  319. return parent::createFromFormat( $format, $time, $timezone );
  320. }
  321. return new DateTime( date( $format, strtotime( $time ) ), $timezone );
  322. }
  323. }
  324. }