Google Analytics - Version 2.2.5

Version Description

  • WP ver 5+ compatibility tests.
  • Code quality clean up.
  • Fix setting save.
Download this release

Release Info

Developer ShareThis
Plugin Icon wp plugin Google Analytics
Version 2.2.5
Comparing to
See all releases

Code changes from version 2.1.5 to 2.2.5

class/Ga_Admin.php CHANGED
@@ -2,28 +2,25 @@
2
 
3
  class Ga_Admin {
4
 
5
- const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
6
- const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
7
- const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
8
- const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
9
- const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
10
- const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
11
- const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
12
- const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
13
- const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
14
- const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
15
- const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
16
- const GA_SHARETHIS_PROPERTY_ID = 'googleanalytics_sherethis_property_id';
17
- const GA_SHARETHIS_PROPERTY_SECRET = 'googleanalytics_sherethis_property_secret';
18
- const GA_SHARETHIS_VERIFICATION_RESULT = 'googleanalytics_sherethis_verification_result';
19
- const MIN_WP_VERSION = '3.8';
20
- const GA_SHARETHIS_TRENDING_CONTENT_PLUGIN_VERSION = '2.1';
21
- const NOTICE_SUCCESS = 'success';
22
- const NOTICE_WARNING = 'warning';
23
- const NOTICE_ERROR = 'error';
24
- const GA_SHARETHIS_API_ALIAS = 'sharethis';
25
- const GA_DISABLE_ALL_FEATURES = 'googleanalytics_disable_all_features';
26
- const GA_HEARTBEAT_API_CACHE_UPDATE = false;
27
 
28
  /**
29
  * Instantiate API client.
@@ -31,13 +28,13 @@ class Ga_Admin {
31
  * @return Ga_Lib_Google_Api_Client|null
32
  */
33
  public static function api_client( $type = '' ) {
34
- if ( $type === self::GA_SHARETHIS_API_ALIAS ) {
35
  $instance = Ga_Lib_Sharethis_Api_Client::get_instance();
36
  } else {
37
- $instance = Ga_Lib_Google_Api_Client::get_instance();
38
- $token = Ga_Helper::get_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
39
  try {
40
- if ( !empty( $token ) ) {
41
  $token = json_decode( $token, true );
42
  $instance->set_access_token( $token );
43
  }
@@ -45,13 +42,13 @@ class Ga_Admin {
45
  Ga_Helper::ga_oauth_notice( $e->getMessage() );
46
  }
47
  }
 
48
  return $instance;
49
  }
50
 
51
- /*
52
  * Initializes plugin's options during plugin activation process.
53
  */
54
-
55
  public static function activate_googleanalytics() {
56
  add_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, Ga_Helper::GA_DEFAULT_WEB_ID );
57
  add_option( self::GA_EXCLUDE_ROLES_OPTION_NAME, wp_json_encode( array() ) );
@@ -68,10 +65,9 @@ class Ga_Admin {
68
  Ga_Cache::add_cache_options();
69
  }
70
 
71
- /*
72
  * Deletes plugin's options during plugin activation process.
73
  */
74
-
75
  public static function deactivate_googleanalytics() {
76
  delete_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
77
  delete_option( self::GA_EXCLUDE_ROLES_OPTION_NAME );
@@ -150,11 +146,11 @@ class Ga_Admin {
150
  */
151
  public static function preupdate_selected_account( $new_value, $old_value ) {
152
  $data = null;
153
- if ( !empty( $new_value ) ) {
154
- $data = explode( "_", $new_value );
155
 
156
- if ( !empty( $data[ 1 ] ) ) {
157
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[ 1 ] );
158
  }
159
  }
160
 
@@ -162,8 +158,8 @@ class Ga_Admin {
162
  }
163
 
164
  public static function preupdate_disable_all_features( $new_value, $old_value ) {
165
- if ( $old_value == 'on' ) {
166
- Ga_Helper::update_option( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false );
167
  }
168
 
169
  return $new_value;
@@ -194,11 +190,10 @@ class Ga_Admin {
194
  register_setting( GA_NAME, self::GA_DISABLE_ALL_FEATURES );
195
  register_setting( GA_NAME, 'googleanalytics_optimize_code' );
196
  register_setting( GA_NAME, 'googleanalytics_ip_anonymization' );
197
- add_filter( 'pre_update_option_' . Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles', 1, 2 );
198
- add_filter( 'pre_update_option_' . Ga_Admin::GA_SELECTED_ACCOUNT, 'Ga_Admin::preupdate_selected_account', 1, 2 );
199
  add_filter( 'pre_update_option_googleanalytics_optimize_code', 'Ga_Admin::preupdate_optimize_code', 1, 2 );
200
  add_filter( 'pre_update_option_googleanalytics_ip_anonymization', 'Ga_Admin::preupdate_ip_anonymization', 1, 2 );
201
- // add_filter( 'pre_update_option_' . Ga_Admin::GA_DISABLE_ALL_FEATURES, 'Ga_Admin::preupdate_disable_all_features', 1, 2 );
202
  }
203
 
204
  /**
@@ -218,26 +213,29 @@ class Ga_Admin {
218
  */
219
  public static function trending_page_googleanalytics() {
220
 
221
- if ( !Ga_Helper::is_wp_version_valid() || !Ga_Helper::is_php_version_valid() ) {
222
  return false;
223
  }
224
  $data = Ga_Sharethis::create_sharethis_options( self::api_client( self::GA_SHARETHIS_API_ALIAS ) );
225
 
226
  Ga_Sharethis::sharethis_installation_verification( self::api_client( self::GA_SHARETHIS_API_ALIAS ) );
227
 
228
- $alerts = Ga_Sharethis::load_sharethis_trending_alerts( self::api_client( self::GA_SHARETHIS_API_ALIAS ) );
229
  // Handle invitation result
230
- $data = Ga_Helper::handle_url_message( $data );
231
  if ( Ga_Helper::is_wp_old() ) {
232
  self::display_api_errors( self::GA_SHARETHIS_API_ALIAS );
233
  }
234
- Ga_View_Core::load( 'trending', array(
235
- 'data' => $data,
236
- 'alerts' => $alerts,
237
- 'tooltip' => Ga_Helper::get_tooltip(),
238
- 'errors' => self::api_client( self::GA_SHARETHIS_API_ALIAS )->get_errors()
239
- ) );
240
- if ( !Ga_Helper::is_wp_old() ) {
 
 
 
241
  self::display_api_errors( self::GA_SHARETHIS_API_ALIAS );
242
  }
243
  }
@@ -247,17 +245,20 @@ class Ga_Admin {
247
  */
248
  public static function statistics_page_googleanalytics() {
249
 
250
- if ( !Ga_Helper::is_wp_version_valid() || !Ga_Helper::is_php_version_valid() ) {
251
  return false;
252
  }
253
 
254
  $data = self::get_stats_page();
255
- Ga_View_Core::load( 'statistics', array(
256
- 'data' => $data
257
- ) );
 
 
 
258
 
259
  if ( Ga_Cache::is_data_cache_outdated( '', Ga_Helper::get_account_id() ) ) {
260
- self::api_client()->add_own_error( '1', _( 'Saved data is shown, it will be refreshed soon' ), 'Ga_Data_Outdated_Exception' );
261
  }
262
 
263
  self::display_api_errors();
@@ -268,12 +269,12 @@ class Ga_Admin {
268
  */
269
  public static function options_page_googleanalytics() {
270
 
271
- if ( !Ga_Helper::is_wp_version_valid() || !Ga_Helper::is_php_version_valid() ) {
272
  return false;
273
  }
274
- if ( Ga_Helper::are_features_enabled() && Ga_Helper::is_curl_disabled() ) {
275
- echo Ga_Helper::ga_wp_notice( _( 'Looks like cURL is not configured on your server. In order to authenticate your Google Analytics account and display statistics, cURL is required. Please contact your server administrator to enable it, or manually enter your Tracking ID.' ), self::NOTICE_WARNING );
276
- }
277
  /**
278
  * Keeps data to be extracted as variables in the view.
279
  *
@@ -281,49 +282,52 @@ class Ga_Admin {
281
  */
282
  $data = array();
283
 
284
- $data[ self::GA_WEB_PROPERTY_ID_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
285
  $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
286
- $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
287
- $data[ self::GA_DISABLE_ALL_FEATURES ] = get_option( self::GA_DISABLE_ALL_FEATURES );
288
 
289
- $roles = Ga_Helper::get_user_roles();
290
- $saved = json_decode( get_option( self::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
291
 
292
  $tmp = array();
293
- if ( !empty( $roles ) ) {
294
  foreach ( $roles as $role ) {
295
  $role_id = Ga_Helper::prepare_role_id( $role );
296
- $tmp[] = array(
297
- 'name' => $role,
298
- 'id' => $role_id,
299
- 'checked' => (!empty( $saved[ $role_id ] ) && $saved[ $role_id ] === 'on' )
300
  );
301
  }
302
  }
303
- $data[ 'roles' ] = $tmp;
304
 
305
  if ( Ga_Helper::is_authorized() ) {
306
- $data[ 'ga_accounts_selector' ] = self::get_accounts_selector();
307
- $data[ 'auth_button' ] = self::get_auth_button( 'reauth' );
308
  } else {
309
- $data[ 'popup_url' ] = self::get_auth_popup_url();
310
- $data[ 'auth_button' ] = self::get_auth_button( 'auth' );
311
  }
312
  $data['debug_modal'] = self::get_debug_modal();
313
- if ( !empty( $_GET[ 'err' ] ) ) {
314
- switch ( $_GET[ 'err' ] ) {
315
  case 1:
316
- $data[ 'error_message' ] = Ga_Helper::ga_oauth_notice( 'There was a problem with Google Oauth2 authentication process. Please verify your site has a valid SSL Certificate in place and is using the HTTPS protocol.' );
317
  break;
318
  case 2:
319
- $data[ 'error_message' ] = Ga_Helper::ga_wp_notice( 'Authentication code is incorrect.', 'error', true );
320
  break;
321
  }
322
  }
323
- Ga_View_Core::load( 'page', array(
324
- 'data' => $data,
325
- 'tooltip' => Ga_Helper::get_tooltip()
326
- ) );
 
 
 
327
 
328
  self::display_api_errors();
329
  }
@@ -345,24 +349,33 @@ class Ga_Admin {
345
  */
346
  public static function get_accounts_selector() {
347
  $selected = Ga_Helper::get_selected_account_data();
348
- $selector = json_decode( get_option( self::GA_ACCOUNT_DATA_OPTION_NAME ), true );
349
- if ( !Ga_Helper::is_code_manually_enabled() && empty( $selector ) ) {
350
- echo Ga_Helper::ga_wp_notice( "Hi there! It seems like we weren't able to locate a Google Analytics account attached to your email account. Can you please register for Google Analytics and then deactivate and reactivate the plugin?", self::NOTICE_WARNING );
351
- }
352
- return Ga_View_Core::load( 'ga_accounts_selector', array(
353
- 'selector' => $selector,
354
- 'selected' => $selected ? implode( "_", $selected ) : null,
355
- 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled() || Ga_Helper::is_all_feature_disabled()
356
- ), true );
 
 
 
 
 
357
  }
358
 
359
  /**
360
  * Adds JS scripts for the settings page.
361
  */
362
  public static function enqueue_ga_scripts() {
363
- wp_register_script( GA_NAME . '-page-js', Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_page.js', array(
364
- 'jquery'
365
- ), GOOGLEANALYTICS_VERSION );
 
 
 
 
366
  wp_enqueue_script( GA_NAME . '-page-js' );
367
  }
368
 
@@ -371,7 +384,7 @@ class Ga_Admin {
371
  */
372
  public static function enqueue_ga_css() {
373
  wp_register_style( GA_NAME . '-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/' . GA_NAME . '.css', false, GOOGLEANALYTICS_VERSION, 'all' );
374
- wp_register_style( GA_NAME . '-additional-css', Ga_Helper::get_plugin_url_with_correct_protocol(). '/css/ga_additional.css', false, GOOGLEANALYTICS_VERSION, 'all' );
375
  wp_enqueue_style( GA_NAME . '-css' );
376
  wp_enqueue_style( GA_NAME . '-additional-css' );
377
  if ( Ga_Helper::is_wp_old() ) {
@@ -386,9 +399,13 @@ class Ga_Admin {
386
  * Enqueues dashboard JS scripts.
387
  */
388
  private static function enqueue_dashboard_scripts() {
389
- wp_register_script( GA_NAME . '-dashboard-js', Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_dashboard.js', array(
390
- 'jquery'
391
- ), GOOGLEANALYTICS_VERSION );
 
 
 
 
392
  wp_enqueue_script( GA_NAME . '-dashboard-js' );
393
  }
394
 
@@ -397,12 +414,16 @@ class Ga_Admin {
397
  */
398
  public static function enqueue_scripts() {
399
  if ( Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page() ) {
400
- wp_register_script( GA_NAME . '-js', Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '.js', array(
401
- 'jquery'
402
- ), GOOGLEANALYTICS_VERSION );
 
 
 
 
403
  wp_enqueue_script( GA_NAME . '-js' );
404
 
405
- wp_register_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', null, null, false );
406
  wp_enqueue_script( 'googlecharts' );
407
 
408
  self::enqueue_ga_css();
@@ -423,43 +444,51 @@ class Ga_Admin {
423
  * @return string HTML code
424
  */
425
  public static function get_stats_page() {
426
- $chart = null;
427
- $boxes = null;
428
- $labels = null;
429
  $sources = null;
430
- if ( Ga_Helper::is_authorized() && Ga_Helper::is_account_selected() && !Ga_Helper::is_all_feature_disabled() ) {
431
  list( $chart, $boxes, $labels, $sources ) = self::generate_stats_data();
432
  }
433
 
434
- return Ga_Helper::get_chart_page( 'stats', array(
435
- 'chart' => $chart,
436
- 'boxes' => $boxes,
437
- 'labels' => $labels,
438
- 'sources' => $sources
439
- ) );
 
 
 
440
  }
441
 
442
  /**
443
  * Shows plugin's notice on the admin area.
444
  */
445
  public static function admin_notice_googleanalytics() {
446
- if ( (!get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && Ga_Helper::is_plugin_page() ) || (!get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && !get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) ) {
447
  $current_url = Ga_Helper::get_current_url();
448
- $url = ( strstr( $current_url, '?' ) ? $current_url . '&' : $current_url . '?' ) . http_build_query( array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_update_terms' ) );
449
- Ga_View_Core::load( 'ga_notice', array(
450
- 'url' => $url
451
- ) );
452
  }
453
 
454
- if ( !empty( $_GET[ 'settings-updated' ] ) && Ga_Helper::is_plugin_page() ) {
455
- echo Ga_Helper::ga_wp_notice( _( 'Settings saved' ), self::NOTICE_SUCCESS );
456
  }
457
 
458
  if ( Ga_Helper::get_option( self::GA_DISABLE_ALL_FEATURES ) ) {
459
- echo Ga_Helper::ga_wp_notice( _( 'You have disabled all extra features, click here to enable Dashboards, Viral Alerts and Google API.' ), self::NOTICE_WARNING, false, array(
460
- 'url' => admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features' ) ) ),
461
- 'label' => _( 'Enable' )
462
- ) );
 
 
 
 
 
 
 
463
  }
464
  }
465
 
@@ -468,7 +497,7 @@ class Ga_Admin {
468
  * @return string
469
  */
470
  public static function admin_notice_googleanalytics_php_version() {
471
- echo Ga_Helper::ga_wp_notice( _( 'Cannot use Google Analytics plugin. PHP version ' . phpversion() . ' is to low. Required PHP version: ' . Ga_Helper::PHP_VERSION_REQUIRED ), self::NOTICE_ERROR );
472
  }
473
 
474
  /**
@@ -476,7 +505,7 @@ class Ga_Admin {
476
  * @return string
477
  */
478
  public static function admin_notice_googleanalytics_wp_version() {
479
- echo Ga_Helper::ga_wp_notice( _( 'Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION ), self::NOTICE_ERROR );
480
  }
481
 
482
  /**
@@ -507,26 +536,27 @@ class Ga_Admin {
507
  add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics' );
508
  add_action( 'heartbeat_tick', 'Ga_Admin::run_heartbeat_jobs' );
509
  add_action( 'wp_ajax_googleanalytics_send_debug_email', 'Ga_SupportLogger::send_email' );
510
- if ( !get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && !get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) {
511
  add_action( 'wp_ajax_googleanalytics_hide_terms', 'Ga_Admin::admin_notice_hide_googleanalytics' );
512
  }
513
  }
514
 
515
  /**
516
  * Runs jobs
 
517
  * @param $response
518
  * @param $screen_id
519
  */
520
  public static function run_heartbeat_jobs( $response, $screen_id = '' ) {
521
 
522
- if ( Ga_Admin::GA_HEARTBEAT_API_CACHE_UPDATE ) {
523
  // Disable cache for ajax request
524
  self::api_client()->set_disable_cache( true );
525
 
526
  // Try to regenerate cache if needed
527
- self::generate_stats_data();
528
- }
529
  }
 
530
 
531
  /**
532
  * Adds plugin's filters
@@ -545,8 +575,8 @@ class Ga_Admin {
545
  */
546
  public static function ga_action_links( $actions, $plugin_file ) {
547
 
548
- if ( basename( $plugin_file ) == GA_NAME . '.php' ) {
549
- array_unshift( $actions, '<a href="' . esc_url( get_admin_url( null, Ga_Helper::GA_SETTINGS_PAGE_URL ) ) . '">' . _( 'Settings' ) . '</a>' );
550
  }
551
 
552
  return $actions;
@@ -556,8 +586,8 @@ class Ga_Admin {
556
 
557
  $code = Ga_Helper::get_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
558
 
559
- if ( !empty( $code ) ) {
560
- Ga_Helper::update_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME, "" );
561
 
562
  // Get access token
563
  $response = self::api_client()->call( 'ga_auth_get_access_token', $code );
@@ -565,12 +595,12 @@ class Ga_Admin {
565
  return false;
566
  }
567
  $param = '';
568
- if ( !self::save_access_token( $response ) ) {
569
- $param = '&err=1';
570
  $errors = self::api_client()->get_errors();
571
- if ( !empty( $errors ) ){
572
  foreach ( $errors as $error ) {
573
- if ( $error[ 'message' ] == 'invalid_grant' ){
574
  $param = '&err=2';
575
  }
576
  }
@@ -580,10 +610,10 @@ class Ga_Admin {
580
  // Get accounts data
581
  $account_summaries = self::api_client()->call( 'ga_api_account_summaries' );
582
  self::save_ga_account_summaries( $account_summaries->getData() );
583
- update_option( self::GA_SELECTED_ACCOUNT, "" );
584
  }
585
 
586
- wp_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL . $param ) );
587
  }
588
  }
589
 
@@ -596,14 +626,14 @@ class Ga_Admin {
596
  */
597
  public static function save_access_token( $response, $refresh_token = '' ) {
598
  $access_token = $response->getData();
599
- if ( !empty( $access_token ) ) {
600
- $access_token[ 'created' ] = time();
601
  } else {
602
  return false;
603
  }
604
 
605
- if ( !empty( $refresh_token ) ) {
606
- $access_token[ 'refresh_token' ] = $refresh_token;
607
  }
608
 
609
  return update_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode( $access_token ) );
@@ -618,27 +648,27 @@ class Ga_Admin {
618
  */
619
  public static function save_ga_account_summaries( $data ) {
620
  $return = array();
621
- if ( !empty( $data[ 'items' ] ) ) {
622
- foreach ( $data[ 'items' ] as $item ) {
623
- $tmp = array();
624
- $tmp[ 'id' ] = $item[ 'id' ];
625
- $tmp[ 'name' ] = $item[ 'name' ];
626
- if ( is_array( $item[ 'webProperties' ] ) ) {
627
- foreach ( $item[ 'webProperties' ] as $property ) {
628
  $profiles = array();
629
- if ( is_array( $property[ 'profiles' ] ) ) {
630
- foreach ( $property[ 'profiles' ] as $profile ) {
631
  $profiles[] = array(
632
- 'id' => $profile[ 'id' ],
633
- 'name' => $profile[ 'name' ]
634
  );
635
  }
636
  }
637
 
638
- $tmp[ 'webProperties' ][] = array(
639
- 'webPropertyId' => $property[ 'id' ],
640
- 'name' => $property[ 'name' ],
641
- 'profiles' => $profiles
642
  );
643
  }
644
  }
@@ -647,11 +677,11 @@ class Ga_Admin {
647
  }
648
 
649
  update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode( $return ) );
 
 
650
  }
651
- else{
652
- update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, "" );
653
- }
654
- update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, "" );
655
  return $return;
656
  }
657
 
@@ -660,23 +690,24 @@ class Ga_Admin {
660
  */
661
  public static function ga_ajax_data_change() {
662
  if ( Ga_Admin_Controller::validate_ajax_data_change_post( $_POST ) ) {
663
- $date_range = !empty( $_POST[ 'date_range' ] ) ? $_POST[ 'date_range' ] : null;
664
- $metric = !empty( $_POST[ 'metric' ] ) ? $_POST[ 'metric' ] : null;
665
- echo Ga_Helper::get_ga_dashboard_widget_data_json( $date_range, $metric, false, true );
666
  } else {
667
- echo wp_json_encode( array( 'error' => _( 'Invalid request.' ) ) );
668
  }
 
669
  wp_die();
670
  }
671
-
672
  /**
673
  * Displays API error messages.
674
  */
675
  public static function display_api_errors( $alias = '' ) {
676
  $errors = self::api_client( $alias )->get_errors();
677
- if ( !empty( $errors ) ) {
678
  foreach ( $errors as $error ) {
679
- echo Ga_Notice::get_message( $error );
680
  }
681
  }
682
  }
@@ -689,29 +720,33 @@ class Ga_Admin {
689
  public static function generate_stats_data() {
690
  $selected = Ga_Helper::get_selected_account_data( true );
691
 
692
- $query_params = Ga_Stats::get_query( 'main_chart', $selected[ 'view_id' ] );
693
- $stats_data = self::api_client()->call( 'ga_api_data', array(
694
- $query_params
695
- ) );
696
-
697
- $boxes_data = self::api_client()->call( 'ga_api_data', array(
698
- Ga_Stats::get_query( 'boxes', $selected[ 'view_id' ] )
699
- ) );
700
- $sources_data = self::api_client()->call( 'ga_api_data', array(
701
- Ga_Stats::get_query( 'sources', $selected[ 'view_id' ] )
702
- ) );
703
- $chart = !empty( $stats_data ) ? Ga_Stats::get_chart( $stats_data->getData() ) : array();
704
- $boxes = !empty( $boxes_data ) ? Ga_Stats::get_boxes( $boxes_data->getData() ) : array();
705
- $last_chart_date = !empty( $chart ) ? $chart[ 'date' ] : strtotime( 'now' );
706
- unset( $chart[ 'date' ] );
707
- $labels = array(
708
- 'thisWeek' => date( 'M d, Y', strtotime( '-6 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
709
- 'lastWeek' => date( 'M d, Y', strtotime( '-13 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', strtotime( '-7 day', $last_chart_date ) )
710
  );
711
- $sources = !empty( $sources_data ) ? Ga_Stats::get_sources( $sources_data->getData() ) : array();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
712
 
713
  return array( $chart, $boxes, $labels, $sources );
714
  }
 
715
  /**
716
  * Returns auth or re-auth button
717
  *
@@ -719,12 +754,16 @@ class Ga_Admin {
719
  */
720
  public static function get_auth_button( $type ) {
721
 
722
- return Ga_View_Core::load( 'ga_auth_button', array(
723
- 'label' => ( $type == 'auth' ) ? 'Authenticate with Google' : 'Re-authenticate with Google',
724
- 'type' => $type,
725
- 'url' => self::get_auth_popup_url(),
726
- 'manually_id' => get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME )
727
- ), true );
 
 
 
 
728
  }
729
 
730
  /**
@@ -732,10 +771,12 @@ class Ga_Admin {
732
  *
733
  * @return string
734
  */
735
- public static function get_debug_modal( ) {
736
 
737
- return Ga_View_Core::load( 'ga_debug_modal', array(
738
- 'debug_info' => Ga_SupportLogger::$debug_info
739
- ), true );
 
 
740
  }
741
  }
2
 
3
  class Ga_Admin {
4
 
5
+ const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
6
+ const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
7
+ const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
8
+ const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
9
+ const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
10
+ const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
11
+ const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
12
+ const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
13
+ const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
14
+ const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
15
+ const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
16
+ const GA_SHARETHIS_PROPERTY_ID = 'googleanalytics_sherethis_property_id';
17
+ const GA_SHARETHIS_PROPERTY_SECRET = 'googleanalytics_sherethis_property_secret';
18
+ const GA_SHARETHIS_VERIFICATION_RESULT = 'googleanalytics_sherethis_verification_result';
19
+ const MIN_WP_VERSION = '3.8';
20
+ const GA_SHARETHIS_TRENDING_CONTENT_PLUGIN_VERSION = '2.1';
21
+ const GA_SHARETHIS_API_ALIAS = 'sharethis';
22
+ const GA_DISABLE_ALL_FEATURES = 'googleanalytics_disable_all_features';
23
+ const GA_HEARTBEAT_API_CACHE_UPDATE = false;
 
 
 
24
 
25
  /**
26
  * Instantiate API client.
28
  * @return Ga_Lib_Google_Api_Client|null
29
  */
30
  public static function api_client( $type = '' ) {
31
+ if ( self::GA_SHARETHIS_API_ALIAS === $type ) {
32
  $instance = Ga_Lib_Sharethis_Api_Client::get_instance();
33
  } else {
34
+ $instance = Ga_Lib_Google_Api_Client::get_instance();
35
+ $token = Ga_Helper::get_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
36
  try {
37
+ if ( ! empty( $token ) ) {
38
  $token = json_decode( $token, true );
39
  $instance->set_access_token( $token );
40
  }
42
  Ga_Helper::ga_oauth_notice( $e->getMessage() );
43
  }
44
  }
45
+
46
  return $instance;
47
  }
48
 
49
+ /**
50
  * Initializes plugin's options during plugin activation process.
51
  */
 
52
  public static function activate_googleanalytics() {
53
  add_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, Ga_Helper::GA_DEFAULT_WEB_ID );
54
  add_option( self::GA_EXCLUDE_ROLES_OPTION_NAME, wp_json_encode( array() ) );
65
  Ga_Cache::add_cache_options();
66
  }
67
 
68
+ /**
69
  * Deletes plugin's options during plugin activation process.
70
  */
 
71
  public static function deactivate_googleanalytics() {
72
  delete_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
73
  delete_option( self::GA_EXCLUDE_ROLES_OPTION_NAME );
146
  */
147
  public static function preupdate_selected_account( $new_value, $old_value ) {
148
  $data = null;
149
+ if ( ! empty( $new_value ) ) {
150
+ $data = explode( '_', $new_value );
151
 
152
+ if ( ! empty( $data[1] ) ) {
153
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[1] );
154
  }
155
  }
156
 
158
  }
159
 
160
  public static function preupdate_disable_all_features( $new_value, $old_value ) {
161
+ if ( 'on' === $old_value ) {
162
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false );
163
  }
164
 
165
  return $new_value;
190
  register_setting( GA_NAME, self::GA_DISABLE_ALL_FEATURES );
191
  register_setting( GA_NAME, 'googleanalytics_optimize_code' );
192
  register_setting( GA_NAME, 'googleanalytics_ip_anonymization' );
193
+ add_filter( 'pre_update_option_' . self::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles', 1, 2 );
194
+ add_filter( 'pre_update_option_' . self::GA_SELECTED_ACCOUNT, 'GA_Admin::preupdate_selected_account', 1, 2 );
195
  add_filter( 'pre_update_option_googleanalytics_optimize_code', 'Ga_Admin::preupdate_optimize_code', 1, 2 );
196
  add_filter( 'pre_update_option_googleanalytics_ip_anonymization', 'Ga_Admin::preupdate_ip_anonymization', 1, 2 );
 
197
  }
198
 
199
  /**
213
  */
214
  public static function trending_page_googleanalytics() {
215
 
216
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
217
  return false;
218
  }
219
  $data = Ga_Sharethis::create_sharethis_options( self::api_client( self::GA_SHARETHIS_API_ALIAS ) );
220
 
221
  Ga_Sharethis::sharethis_installation_verification( self::api_client( self::GA_SHARETHIS_API_ALIAS ) );
222
 
223
+ $alerts = Ga_Sharethis::load_sharethis_trending_alerts( self::api_client( self::GA_SHARETHIS_API_ALIAS ) );
224
  // Handle invitation result
225
+ $data = Ga_Helper::handle_url_message( $data );
226
  if ( Ga_Helper::is_wp_old() ) {
227
  self::display_api_errors( self::GA_SHARETHIS_API_ALIAS );
228
  }
229
+ Ga_View_Core::load(
230
+ 'trending',
231
+ array(
232
+ 'data' => $data,
233
+ 'alerts' => $alerts,
234
+ 'tooltip' => Ga_Helper::get_tooltip(),
235
+ 'errors' => self::api_client( self::GA_SHARETHIS_API_ALIAS )->get_errors(),
236
+ )
237
+ );
238
+ if ( ! Ga_Helper::is_wp_old() ) {
239
  self::display_api_errors( self::GA_SHARETHIS_API_ALIAS );
240
  }
241
  }
245
  */
246
  public static function statistics_page_googleanalytics() {
247
 
248
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
249
  return false;
250
  }
251
 
252
  $data = self::get_stats_page();
253
+ Ga_View_Core::load(
254
+ 'statistics',
255
+ array(
256
+ 'data' => $data,
257
+ )
258
+ );
259
 
260
  if ( Ga_Cache::is_data_cache_outdated( '', Ga_Helper::get_account_id() ) ) {
261
+ self::api_client()->add_own_error( '1', __( 'Saved data is shown, it will be refreshed soon' ), 'Ga_Data_Outdated_Exception' );
262
  }
263
 
264
  self::display_api_errors();
269
  */
270
  public static function options_page_googleanalytics() {
271
 
272
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
273
  return false;
274
  }
275
+ if ( Ga_Helper::are_features_enabled() && Ga_Helper::is_curl_disabled() ) {
276
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( __( 'Looks like cURL is not configured on your server. In order to authenticate your Google Analytics account and display statistics, cURL is required. Please contact your server administrator to enable it, or manually enter your Tracking ID.' ), 'warning' ) );
277
+ }
278
  /**
279
  * Keeps data to be extracted as variables in the view.
280
  *
282
  */
283
  $data = array();
284
 
285
+ $data[ self::GA_WEB_PROPERTY_ID_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
286
  $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
287
+ $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
288
+ $data[ self::GA_DISABLE_ALL_FEATURES ] = get_option( self::GA_DISABLE_ALL_FEATURES );
289
 
290
+ $roles = Ga_Helper::get_user_roles();
291
+ $saved = json_decode( get_option( self::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
292
 
293
  $tmp = array();
294
+ if ( ! empty( $roles ) ) {
295
  foreach ( $roles as $role ) {
296
  $role_id = Ga_Helper::prepare_role_id( $role );
297
+ $tmp[] = array(
298
+ 'name' => $role,
299
+ 'id' => $role_id,
300
+ 'checked' => ( ! empty( $saved[ $role_id ] ) && 'on' === $saved[ $role_id ] ),
301
  );
302
  }
303
  }
304
+ $data['roles'] = $tmp;
305
 
306
  if ( Ga_Helper::is_authorized() ) {
307
+ $data['ga_accounts_selector'] = self::get_accounts_selector();
308
+ $data['auth_button'] = self::get_auth_button( 'reauth' );
309
  } else {
310
+ $data['popup_url'] = self::get_auth_popup_url();
311
+ $data['auth_button'] = self::get_auth_button( 'auth' );
312
  }
313
  $data['debug_modal'] = self::get_debug_modal();
314
+ if ( ! empty( $_GET['err'] ) ) { // WPCS: CSRF ok.
315
+ switch ( $_GET['err'] ) { // WPCS: CSRF ok.
316
  case 1:
317
+ $data['error_message'] = Ga_Helper::ga_oauth_notice( 'There was a problem with Google Oauth2 authentication process. Please verify your site has a valid SSL Certificate in place and is using the HTTPS protocol.' );
318
  break;
319
  case 2:
320
+ $data['error_message'] = Ga_Helper::ga_wp_notice( 'Authentication code is incorrect.', 'error', true );
321
  break;
322
  }
323
  }
324
+ Ga_View_Core::load(
325
+ 'page',
326
+ array(
327
+ 'data' => $data,
328
+ 'tooltip' => Ga_Helper::get_tooltip(),
329
+ )
330
+ );
331
 
332
  self::display_api_errors();
333
  }
349
  */
350
  public static function get_accounts_selector() {
351
  $selected = Ga_Helper::get_selected_account_data();
352
+ $selector = json_decode( get_option( self::GA_ACCOUNT_DATA_OPTION_NAME ), true );
353
+ if ( ! Ga_Helper::is_code_manually_enabled() && empty( $selector ) ) {
354
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( "Hi there! It seems like we weren't able to locate a Google Analytics account attached to your email account. Can you please register for Google Analytics and then deactivate and reactivate the plugin?", 'warning' ) );
355
+ }
356
+
357
+ return Ga_View_Core::load(
358
+ 'ga_accounts_selector',
359
+ array(
360
+ 'selector' => $selector,
361
+ 'selected' => $selected ? implode( '_', $selected ) : null,
362
+ 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled() || Ga_Helper::is_all_feature_disabled(),
363
+ ),
364
+ true
365
+ );
366
  }
367
 
368
  /**
369
  * Adds JS scripts for the settings page.
370
  */
371
  public static function enqueue_ga_scripts() {
372
+ wp_register_script(
373
+ GA_NAME . '-page-js',
374
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_page.js',
375
+ [ 'jquery' ],
376
+ GOOGLEANALYTICS_VERSION,
377
+ false
378
+ );
379
  wp_enqueue_script( GA_NAME . '-page-js' );
380
  }
381
 
384
  */
385
  public static function enqueue_ga_css() {
386
  wp_register_style( GA_NAME . '-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/' . GA_NAME . '.css', false, GOOGLEANALYTICS_VERSION, 'all' );
387
+ wp_register_style( GA_NAME . '-additional-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_additional.css', false, GOOGLEANALYTICS_VERSION, 'all' );
388
  wp_enqueue_style( GA_NAME . '-css' );
389
  wp_enqueue_style( GA_NAME . '-additional-css' );
390
  if ( Ga_Helper::is_wp_old() ) {
399
  * Enqueues dashboard JS scripts.
400
  */
401
  private static function enqueue_dashboard_scripts() {
402
+ wp_register_script(
403
+ GA_NAME . '-dashboard-js',
404
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_dashboard.js',
405
+ [ 'jquery' ],
406
+ GOOGLEANALYTICS_VERSION,
407
+ false
408
+ );
409
  wp_enqueue_script( GA_NAME . '-dashboard-js' );
410
  }
411
 
414
  */
415
  public static function enqueue_scripts() {
416
  if ( Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page() ) {
417
+ wp_register_script(
418
+ GA_NAME . '-js',
419
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '.js',
420
+ [ 'jquery' ],
421
+ GOOGLEANALYTICS_VERSION,
422
+ false
423
+ );
424
  wp_enqueue_script( GA_NAME . '-js' );
425
 
426
+ wp_register_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', null, 1, false );
427
  wp_enqueue_script( 'googlecharts' );
428
 
429
  self::enqueue_ga_css();
444
  * @return string HTML code
445
  */
446
  public static function get_stats_page() {
447
+ $chart = null;
448
+ $boxes = null;
449
+ $labels = null;
450
  $sources = null;
451
+ if ( Ga_Helper::is_authorized() && Ga_Helper::is_account_selected() && ! Ga_Helper::is_all_feature_disabled() ) {
452
  list( $chart, $boxes, $labels, $sources ) = self::generate_stats_data();
453
  }
454
 
455
+ return Ga_Helper::get_chart_page(
456
+ 'stats',
457
+ array(
458
+ 'chart' => $chart,
459
+ 'boxes' => $boxes,
460
+ 'labels' => $labels,
461
+ 'sources' => $sources,
462
+ )
463
+ );
464
  }
465
 
466
  /**
467
  * Shows plugin's notice on the admin area.
468
  */
469
  public static function admin_notice_googleanalytics() {
470
+ if ( ( ! get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && Ga_Helper::is_plugin_page() ) || ( ! get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && ! get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) ) {
471
  $current_url = Ga_Helper::get_current_url();
472
+ $url = ( strstr( $current_url, '?' ) ? $current_url . '&' : $current_url . '?' ) . http_build_query( array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_update_terms' ) );
473
+ Ga_View_Core::load( 'ga_notice', [ 'url' => $url ] );
 
 
474
  }
475
 
476
+ if ( ! empty( $_GET['settings-updated'] ) && Ga_Helper::is_plugin_page() ) { // WPCS: CSRF ok.
477
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( __( 'Settings saved' ), 'success' ) );
478
  }
479
 
480
  if ( Ga_Helper::get_option( self::GA_DISABLE_ALL_FEATURES ) ) {
481
+ echo wp_kses_post(
482
+ Ga_Helper::ga_wp_notice(
483
+ __( 'You have disabled all extra features, click here to enable Dashboards, Viral Alerts and Google API.' ),
484
+ 'warning',
485
+ false,
486
+ array(
487
+ 'url' => admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features' ) ) ),
488
+ 'label' => __( 'Enable' ),
489
+ )
490
+ )
491
+ );
492
  }
493
  }
494
 
497
  * @return string
498
  */
499
  public static function admin_notice_googleanalytics_php_version() {
500
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Cannot use Google Analytics plugin. PHP version ' . phpversion() . ' is to low. Required PHP version: ' . Ga_Helper::PHP_VERSION_REQUIRED, 'error' ) );
501
  }
502
 
503
  /**
505
  * @return string
506
  */
507
  public static function admin_notice_googleanalytics_wp_version() {
508
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION, 'error' ) );
509
  }
510
 
511
  /**
536
  add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics' );
537
  add_action( 'heartbeat_tick', 'Ga_Admin::run_heartbeat_jobs' );
538
  add_action( 'wp_ajax_googleanalytics_send_debug_email', 'Ga_SupportLogger::send_email' );
539
+ if ( ! get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && ! get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) {
540
  add_action( 'wp_ajax_googleanalytics_hide_terms', 'Ga_Admin::admin_notice_hide_googleanalytics' );
541
  }
542
  }
543
 
544
  /**
545
  * Runs jobs
546
+ *
547
  * @param $response
548
  * @param $screen_id
549
  */
550
  public static function run_heartbeat_jobs( $response, $screen_id = '' ) {
551
 
552
+ if ( self::GA_HEARTBEAT_API_CACHE_UPDATE ) {
553
  // Disable cache for ajax request
554
  self::api_client()->set_disable_cache( true );
555
 
556
  // Try to regenerate cache if needed
557
+ self::generate_stats_data();
 
558
  }
559
+ }
560
 
561
  /**
562
  * Adds plugin's filters
575
  */
576
  public static function ga_action_links( $actions, $plugin_file ) {
577
 
578
+ if ( basename( $plugin_file ) === GA_NAME . '.php' ) {
579
+ array_unshift( $actions, '<a href="' . esc_url( get_admin_url( null, Ga_Helper::GA_SETTINGS_PAGE_URL ) ) . '">' . __( 'Settings' ) . '</a>' );
580
  }
581
 
582
  return $actions;
586
 
587
  $code = Ga_Helper::get_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
588
 
589
+ if ( ! empty( $code ) ) {
590
+ Ga_Helper::update_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME, '' );
591
 
592
  // Get access token
593
  $response = self::api_client()->call( 'ga_auth_get_access_token', $code );
595
  return false;
596
  }
597
  $param = '';
598
+ if ( ! self::save_access_token( $response ) ) {
599
+ $param = '&err=1';
600
  $errors = self::api_client()->get_errors();
601
+ if ( ! empty( $errors ) ) {
602
  foreach ( $errors as $error ) {
603
+ if ( 'invalid_grant' === $error['message'] ) {
604
  $param = '&err=2';
605
  }
606
  }
610
  // Get accounts data
611
  $account_summaries = self::api_client()->call( 'ga_api_account_summaries' );
612
  self::save_ga_account_summaries( $account_summaries->getData() );
613
+ update_option( self::GA_SELECTED_ACCOUNT, '' );
614
  }
615
 
616
+ wp_safe_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL . $param ) );
617
  }
618
  }
619
 
626
  */
627
  public static function save_access_token( $response, $refresh_token = '' ) {
628
  $access_token = $response->getData();
629
+ if ( ! empty( $access_token ) ) {
630
+ $access_token['created'] = time();
631
  } else {
632
  return false;
633
  }
634
 
635
+ if ( ! empty( $refresh_token ) ) {
636
+ $access_token['refresh_token'] = $refresh_token;
637
  }
638
 
639
  return update_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode( $access_token ) );
648
  */
649
  public static function save_ga_account_summaries( $data ) {
650
  $return = array();
651
+ if ( ! empty( $data['items'] ) ) {
652
+ foreach ( $data['items'] as $item ) {
653
+ $tmp = array();
654
+ $tmp['id'] = $item['id'];
655
+ $tmp['name'] = $item['name'];
656
+ if ( is_array( $item['webProperties'] ) ) {
657
+ foreach ( $item['webProperties'] as $property ) {
658
  $profiles = array();
659
+ if ( is_array( $property['profiles'] ) ) {
660
+ foreach ( $property['profiles'] as $profile ) {
661
  $profiles[] = array(
662
+ 'id' => $profile['id'],
663
+ 'name' => $profile['name'],
664
  );
665
  }
666
  }
667
 
668
+ $tmp['webProperties'][] = array(
669
+ 'webPropertyId' => $property['id'],
670
+ 'name' => $property['name'],
671
+ 'profiles' => $profiles,
672
  );
673
  }
674
  }
677
  }
678
 
679
  update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode( $return ) );
680
+ } else {
681
+ update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, '' );
682
  }
683
+ update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, '' );
684
+
 
 
685
  return $return;
686
  }
687
 
690
  */
691
  public static function ga_ajax_data_change() {
692
  if ( Ga_Admin_Controller::validate_ajax_data_change_post( $_POST ) ) {
693
+ $date_range = ! empty( $_POST['date_range'] ) ? $_POST['date_range'] : null; // WPCS: CSRF ok.
694
+ $metric = ! empty( $_POST['metric'] ) ? $_POST['metric'] : null; // WPCS: CSRF ok.
695
+ echo wp_kses_post( Ga_Helper::get_ga_dashboard_widget_data_json( $date_range, $metric, false, true ) );
696
  } else {
697
+ echo wp_json_encode( array( 'error' => __( 'Invalid request.' ) ) );
698
  }
699
+
700
  wp_die();
701
  }
702
+
703
  /**
704
  * Displays API error messages.
705
  */
706
  public static function display_api_errors( $alias = '' ) {
707
  $errors = self::api_client( $alias )->get_errors();
708
+ if ( ! empty( $errors ) ) {
709
  foreach ( $errors as $error ) {
710
+ echo wp_kses_post( Ga_Notice::get_message( $error ) );
711
  }
712
  }
713
  }
720
  public static function generate_stats_data() {
721
  $selected = Ga_Helper::get_selected_account_data( true );
722
 
723
+ $query_params = Ga_Stats::get_query( 'main_chart', $selected['view_id'] );
724
+ $stats_data = self::api_client()->call(
725
+ 'ga_api_data',
726
+ [ $query_params ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727
  );
728
+
729
+ $boxes_data = self::api_client()->call(
730
+ 'ga_api_data',
731
+ [ Ga_Stats::get_query( 'boxes', $selected['view_id'] ) ]
732
+ );
733
+ $sources_data = self::api_client()->call(
734
+ 'ga_api_data',
735
+ [ Ga_Stats::get_query( 'sources', $selected['view_id'] ) ]
736
+ );
737
+ $chart = ! empty( $stats_data ) ? Ga_Stats::get_chart( $stats_data->getData() ) : array();
738
+ $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_boxes( $boxes_data->getData() ) : array();
739
+ $last_chart_date = ! empty( $chart ) ? $chart['date'] : strtotime( 'now' );
740
+ unset( $chart['date'] );
741
+ $labels = array(
742
+ 'thisWeek' => date( 'M d, Y', strtotime( '-6 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
743
+ 'lastWeek' => date( 'M d, Y', strtotime( '-13 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', strtotime( '-7 day', $last_chart_date ) ),
744
+ );
745
+ $sources = ! empty( $sources_data ) ? Ga_Stats::get_sources( $sources_data->getData() ) : array();
746
 
747
  return array( $chart, $boxes, $labels, $sources );
748
  }
749
+
750
  /**
751
  * Returns auth or re-auth button
752
  *
754
  */
755
  public static function get_auth_button( $type ) {
756
 
757
+ return Ga_View_Core::load(
758
+ 'ga_auth_button',
759
+ [
760
+ 'label' => 'auth' === $type ? 'Authenticate with Google' : 'Re-authenticate with Google',
761
+ 'type' => $type,
762
+ 'url' => self::get_auth_popup_url(),
763
+ 'manually_id' => get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ),
764
+ ],
765
+ true
766
+ );
767
  }
768
 
769
  /**
771
  *
772
  * @return string
773
  */
774
+ public static function get_debug_modal() {
775
 
776
+ return Ga_View_Core::load(
777
+ 'ga_debug_modal',
778
+ [ 'debug_info' => Ga_SupportLogger::$debug_info ],
779
+ true
780
+ );
781
  }
782
  }
class/Ga_Autoloader.php CHANGED
@@ -6,7 +6,7 @@ class Ga_Autoloader {
6
  * Registers clas loader.
7
  */
8
  public static function register() {
9
- spl_autoload_register( "Ga_Autoloader::loader" );
10
  }
11
 
12
  /**
6
  * Registers clas loader.
7
  */
8
  public static function register() {
9
+ spl_autoload_register( 'Ga_Autoloader::loader' );
10
  }
11
 
12
  /**
class/Ga_Helper.php CHANGED
@@ -2,17 +2,17 @@
2
 
3
  class Ga_Helper {
4
 
5
- const ROLE_ID_PREFIX = "role-id-";
6
- const GA_DEFAULT_WEB_ID = "UA-0000000-0";
7
- const GA_STATISTICS_PAGE_URL = "admin.php?page=googleanalytics";
8
- const GA_SETTINGS_PAGE_URL = "admin.php?page=googleanalytics/settings";
9
- const GA_TRENDING_PAGE_URL = 'admin.php?page=googleanalytics/trending';
10
- const DASHBOARD_PAGE_NAME = "dashboard";
11
- const PHP_VERSION_REQUIRED = "5.2.17";
12
- const GA_WP_MODERN_VERSION = "4.1";
13
- const GA_TOOLTIP_TERMS_NOT_ACCEPTED = 'Please accept the terms to use this feature.';
14
- const GA_TOOLTIP_FEATURES_DISABLED = 'Click the Enable button at the top to start using this feature.';
15
- const GA_DEBUG_MODE = false;
16
 
17
  /**
18
  * Init plugin actions.
@@ -21,20 +21,20 @@ class Ga_Helper {
21
  public static function init() {
22
 
23
  // Displays errors related to required PHP version
24
- if ( !self::is_php_version_valid() ) {
25
  add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_php_version' );
26
 
27
  return false;
28
  }
29
 
30
  // Displays errors related to required WP version
31
- if ( !self::is_wp_version_valid() ) {
32
  add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_wp_version' );
33
 
34
  return false;
35
  }
36
 
37
- if ( !is_admin() ) {
38
  Ga_Frontend::add_actions();
39
 
40
  $frontend_controller = new Ga_Frontend_Controller();
@@ -61,7 +61,7 @@ class Ga_Helper {
61
  public static function is_plugin_page() {
62
  $site = get_current_screen();
63
 
64
- return preg_match( '/' . GA_NAME . '/i', $site->base ) || preg_match( '/' . GA_NAME . '/i', $_SERVER[ 'REQUEST_URI' ] );
65
  }
66
 
67
  /**
@@ -79,9 +79,9 @@ class Ga_Helper {
79
  * @return number
80
  */
81
  public static function is_trending_page() {
82
- $site_uri = urldecode( basename( $_SERVER[ 'REQUEST_URI' ] ) );
83
 
84
- return preg_match( '/' . preg_quote( self::GA_TRENDING_PAGE_URL, '/' ) . '/', $site_uri, $matches ) == 1;
85
  }
86
 
87
  /**
@@ -92,7 +92,7 @@ class Ga_Helper {
92
  * @return boolean
93
  */
94
  public static function is_configured( $web_id ) {
95
- return ( $web_id !== self::GA_DEFAULT_WEB_ID ) && !empty( $web_id );
96
  }
97
 
98
  /**
@@ -102,7 +102,7 @@ class Ga_Helper {
102
  */
103
  public static function get_user_roles() {
104
  global $wp_roles;
105
- if ( !isset( $wp_roles ) ) {
106
  $wp_roles = new WP_Roles();
107
  }
108
 
@@ -585,10 +585,10 @@ class Ga_Helper {
585
 
586
  return ! empty( $account_id[0] ) ? $account_id[0] : '';
587
  }
588
-
589
- public static function is_curl_disabled(){
590
- return ! function_exists( 'curl_version' );
591
- }
592
 
593
 
594
  public static function get_plugin_url_with_correct_protocol() {
@@ -597,9 +597,9 @@ class Ga_Helper {
597
  }
598
 
599
  public static function get_code_manually_label_classes() {
600
- $classes = '';
601
- if ( ! self::are_features_enabled() ){
602
- $classes = 'label-grey ga-tooltip';
603
  }
604
  else if( self::is_account_selected() ) {
605
  $classes = 'label-grey';
2
 
3
  class Ga_Helper {
4
 
5
+ const ROLE_ID_PREFIX = 'role-id-';
6
+ const GA_DEFAULT_WEB_ID = 'UA-0000000-0';
7
+ const GA_STATISTICS_PAGE_URL = 'admin.php?page=googleanalytics';
8
+ const GA_SETTINGS_PAGE_URL = 'admin.php?page=googleanalytics/settings';
9
+ const GA_TRENDING_PAGE_URL = 'admin.php?page=googleanalytics/trending';
10
+ const DASHBOARD_PAGE_NAME = 'dashboard';
11
+ const PHP_VERSION_REQUIRED = '5.2.17';
12
+ const GA_WP_MODERN_VERSION = '4.1';
13
+ const GA_TOOLTIP_TERMS_NOT_ACCEPTED = 'Please accept the terms to use this feature.';
14
+ const GA_TOOLTIP_FEATURES_DISABLED = 'Click the Enable button at the top to start using this feature.';
15
+ const GA_DEBUG_MODE = false;
16
 
17
  /**
18
  * Init plugin actions.
21
  public static function init() {
22
 
23
  // Displays errors related to required PHP version
24
+ if ( ! self::is_php_version_valid() ) {
25
  add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_php_version' );
26
 
27
  return false;
28
  }
29
 
30
  // Displays errors related to required WP version
31
+ if ( ! self::is_wp_version_valid() ) {
32
  add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_wp_version' );
33
 
34
  return false;
35
  }
36
 
37
+ if ( ! is_admin() ) {
38
  Ga_Frontend::add_actions();
39
 
40
  $frontend_controller = new Ga_Frontend_Controller();
61
  public static function is_plugin_page() {
62
  $site = get_current_screen();
63
 
64
+ return preg_match( '/' . GA_NAME . '/i', $site->base ) || preg_match( '/' . GA_NAME . '/i', $_SERVER['REQUEST_URI'] );
65
  }
66
 
67
  /**
79
  * @return number
80
  */
81
  public static function is_trending_page() {
82
+ $site_uri = urldecode( basename( $_SERVER['REQUEST_URI'] ) );
83
 
84
+ return preg_match( '/' . preg_quote( self::GA_TRENDING_PAGE_URL, '/' ) . '/', $site_uri, $matches ) === 1;
85
  }
86
 
87
  /**
92
  * @return boolean
93
  */
94
  public static function is_configured( $web_id ) {
95
+ return ( self::GA_DEFAULT_WEB_ID !== $web_id ) && ! empty( $web_id );
96
  }
97
 
98
  /**
102
  */
103
  public static function get_user_roles() {
104
  global $wp_roles;
105
+ if ( ! isset( $wp_roles ) ) {
106
  $wp_roles = new WP_Roles();
107
  }
108
 
585
 
586
  return ! empty( $account_id[0] ) ? $account_id[0] : '';
587
  }
588
+
589
+ public static function is_curl_disabled(){
590
+ return ! function_exists( 'curl_version' );
591
+ }
592
 
593
 
594
  public static function get_plugin_url_with_correct_protocol() {
597
  }
598
 
599
  public static function get_code_manually_label_classes() {
600
+ $classes = '';
601
+ if ( ! self::are_features_enabled() ){
602
+ $classes = 'label-grey ga-tooltip';
603
  }
604
  else if( self::is_account_selected() ) {
605
  $classes = 'label-grey';
class/Ga_Notice.php CHANGED
@@ -14,19 +14,19 @@ class Ga_Notice {
14
  $message = '';
15
 
16
  if ( Ga_Helper::GA_DEBUG_MODE ) {
17
- $message = Ga_Helper::ga_wp_notice( (!empty( $error[ 'class' ] ) ? _( '[' . $error[ 'class' ] . ']' ) : '' ) . ' ' . $error[ 'message' ], Ga_Admin::NOTICE_ERROR );
18
  } elseif ( $error[ 'class' ] == 'Ga_Lib_Google_Api_Client_AuthCode_Exception' ) {
19
- $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], Ga_Admin::NOTICE_ERROR );
20
  } elseif ( $error[ 'class' ] == 'Ga_Lib_Sharethis_Api_Client_InvalidDomain_Exception' ) {
21
- $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], Ga_Admin::NOTICE_ERROR );
22
  } elseif ( $error[ 'class' ] == 'Ga_Lib_Sharethis_Api_Client_Invite_Exception' ) {
23
- $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], Ga_Admin::NOTICE_ERROR );
24
  } elseif ( in_array( $error[ 'class' ], array( 'Ga_Lib_Sharethis_Api_Client_Verify_Exception', 'Ga_Lib_Sharethis_Api_Client_Alerts_Exception' ) ) ) {
25
- $message = Ga_Helper::ga_wp_notice( Ga_Sharethis::GA_SHARETHIS_ALERTS_ERROR, Ga_Admin::NOTICE_ERROR );
26
  } elseif ( $error[ 'class' ] == 'Ga_Data_Outdated_Exception' ) {
27
- $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], Ga_Admin::NOTICE_WARNING );
28
  } else {
29
- $message = Ga_Helper::ga_wp_notice( _( 'There are temporary connection issues, please try again later or go to Google Analytics website to see the dashboards' ), Ga_Admin::NOTICE_ERROR );
30
  }
31
 
32
  return $message;
14
  $message = '';
15
 
16
  if ( Ga_Helper::GA_DEBUG_MODE ) {
17
+ $message = Ga_Helper::ga_wp_notice( (!empty( $error[ 'class' ] ) ? _( '[' . $error[ 'class' ] . ']' ) : '' ) . ' ' . $error[ 'message' ], 'error' );
18
  } elseif ( $error[ 'class' ] == 'Ga_Lib_Google_Api_Client_AuthCode_Exception' ) {
19
+ $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], 'error' );
20
  } elseif ( $error[ 'class' ] == 'Ga_Lib_Sharethis_Api_Client_InvalidDomain_Exception' ) {
21
+ $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], 'error' );
22
  } elseif ( $error[ 'class' ] == 'Ga_Lib_Sharethis_Api_Client_Invite_Exception' ) {
23
+ $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], 'error' );
24
  } elseif ( in_array( $error[ 'class' ], array( 'Ga_Lib_Sharethis_Api_Client_Verify_Exception', 'Ga_Lib_Sharethis_Api_Client_Alerts_Exception' ) ) ) {
25
+ $message = Ga_Helper::ga_wp_notice( Ga_Sharethis::GA_SHARETHIS_ALERTS_ERROR, 'error' );
26
  } elseif ( $error[ 'class' ] == 'Ga_Data_Outdated_Exception' ) {
27
+ $message = Ga_Helper::ga_wp_notice( $error[ 'message' ], 'warning' );
28
  } else {
29
+ $message = Ga_Helper::ga_wp_notice( _( 'There are temporary connection issues, please try again later or go to Google Analytics website to see the dashboards' ), 'error' );
30
  }
31
 
32
  return $message;
googleanalytics.php CHANGED
@@ -3,11 +3,12 @@
3
  /*
4
  * Plugin Name: Google Analytics
5
  * Plugin URI: http://wordpress.org/extend/plugins/googleanalytics/
6
- * Description: Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
7
- * Version: 2.1.5
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
 
11
  if ( !defined( 'WP_CONTENT_URL' ) ) {
12
  define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
13
  }
3
  /*
4
  * Plugin Name: Google Analytics
5
  * Plugin URI: http://wordpress.org/extend/plugins/googleanalytics/
6
+ * Description: Use Google Analytics on your WordPress site without touching any code, and view visitor reports right in your WordPress admin dashboard!
7
+ * Version: 2.2.5
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
11
+
12
  if ( !defined( 'WP_CONTENT_URL' ) ) {
13
  define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
14
  }
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: sharethis, scottstorebloom
3
  Tags: analytics, dashboard, google, google analytics, google analytics plugin, javascript, marketing, pageviews, statistics, stats, tracking, visits, web stats, widget, analytics dashboard, google analytics dashboard, google analytics widget, google analytics dashboard
4
  Requires at least: 3.8
5
- Tested up to: 4.9.8
6
- Stable tag: 2.1.5
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
@@ -74,6 +74,11 @@ We are always happy to help.
74
 
75
  == Changelog ==
76
 
 
 
 
 
 
77
  = 2.1.5 =
78
  * Added IP Anonymization Option.
79
  * Added Google Optimization field.
2
  Contributors: sharethis, scottstorebloom
3
  Tags: analytics, dashboard, google, google analytics, google analytics plugin, javascript, marketing, pageviews, statistics, stats, tracking, visits, web stats, widget, analytics dashboard, google analytics dashboard, google analytics widget, google analytics dashboard
4
  Requires at least: 3.8
5
+ Tested up to: 5.2.2
6
+ Stable tag: 2.2.5
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
74
 
75
  == Changelog ==
76
 
77
+ = 2.2.5 =
78
+ * WP ver 5+ compatibility tests.
79
+ * Code quality clean up.
80
+ * Fix setting save.
81
+
82
  = 2.1.5 =
83
  * Added IP Anonymization Option.
84
  * Added Google Optimization field.