Google Analytics - Version 2.0.2

Version Description

  • Fixed issues related to older versions of PHP
  • Fixed terms of service notice
  • Added better support for HTTP proxy, thanks @usrlocaldick for the suggestion
  • Added better support when WP_PLUGIN_DIR are already set, thanks @heiglandreas for the tip
  • Added support for PHP version 5.2.17
Download this release

Release Info

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

Code changes from version 2.0.1 to 2.0.2

LICENSE CHANGED
File without changes
class/Ga_Admin.php CHANGED
@@ -1,559 +1,649 @@
1
  <?php
2
 
3
- class Ga_Admin {
4
-
5
- const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
6
-
7
- const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
8
-
9
- const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
10
-
11
- const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
12
-
13
- const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
14
-
15
- const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
16
-
17
- const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
18
-
19
- const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
20
-
21
- const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
22
-
23
- const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
24
-
25
- const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
26
-
27
- const MIN_WP_VERSION = '3.8';
28
-
29
- /**
30
- * Instantiate API client.
31
- *
32
- * @return Ga_Lib_Api_Client|null
33
- */
34
- public static function api_client() {
35
- $instance = Ga_Lib_Api_Client::get_instance();
36
- $token = Ga_Helper::get_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
37
- try {
38
- if ( ! empty( $token ) ) {
39
- $token = json_decode( $token, true );
40
- $instance->set_access_token( $token );
41
- }
42
- } catch ( Exception $e ) {
43
- Ga_Helper::ga_oauth_notice( $e->getMessage() );
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() ) );
55
- add_option( self::GA_SHARETHIS_TERMS_OPTION_NAME, false );
56
- add_option( self::GA_HIDE_TERMS_OPTION_NAME, false );
57
- add_option( self::GA_VERSION_OPTION_NAME );
58
- add_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
59
- add_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
60
- add_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
61
- add_option( self::GA_SELECTED_ACCOUNT );
62
- add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
63
- add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
64
- }
65
-
66
- /*
67
- * Deletes plugin's options during plugin activation process.
68
- */
69
- public static function deactivate_googleanalytics() {
70
- delete_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
71
- delete_option( self::GA_EXCLUDE_ROLES_OPTION_NAME );
72
- delete_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
73
- delete_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
74
- delete_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
75
- delete_option( self::GA_SELECTED_ACCOUNT );
76
- delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
77
- delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
78
- }
79
-
80
- /**
81
- * Deletes plugin's options during plugin uninstallation process.
82
- */
83
- public static function uninstall_googleanalytics() {
84
- delete_option( self::GA_SHARETHIS_TERMS_OPTION_NAME );
85
- delete_option( self::GA_HIDE_TERMS_OPTION_NAME );
86
- delete_option( self::GA_VERSION_OPTION_NAME );
87
- }
88
-
89
- /**
90
- * Do actions during plugin load.
91
- */
92
- public static function loaded_googleanalytics() {
93
- self::update_googleanalytics();
94
- }
95
-
96
- /**
97
- * Update hook fires when plugin is being loaded.
98
- */
99
- public static function update_googleanalytics() {
100
-
101
- $version = get_option( self::GA_VERSION_OPTION_NAME );
102
- $installed_version = get_option( self::GA_VERSION_OPTION_NAME, '1.0.7' );
103
- $old_property_value = Ga_Helper::get_option( 'web_property_id' );
104
- if ( version_compare( $installed_version, GOOGLEANALYTICS_VERSION, 'eq' ) ) {
105
- return;
106
- }
107
- if ( empty( $old_property_value ) && empty( $version ) ) {
108
- update_option( self::GA_SHARETHIS_TERMS_OPTION_NAME, true );
109
- }
110
-
111
- if ( version_compare( $installed_version, GOOGLEANALYTICS_VERSION, 'lt' ) ) {
112
-
113
- if ( ! empty( $old_property_value ) ) {
114
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME, $old_property_value );
115
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, 1 );
116
- delete_option( 'web_property_id' );
117
- }
118
- }
119
-
120
- update_option( self::GA_VERSION_OPTION_NAME, GOOGLEANALYTICS_VERSION );
121
- }
122
-
123
-
124
- public static function preupdate_exclude_roles( $new_value, $old_value ) {
125
- if ( !Ga_Helper::are_terms_accepted()){
126
- return '';
127
- }
128
- return wp_json_encode( $new_value );
129
- }
130
-
131
- /**
132
- * Pre-update hook for preparing JSON structure.
133
- *
134
- * @param $new_value
135
- * @param $old_value
136
- *
137
- * @return mixed
138
- */
139
- public static function preupdate_selected_account( $new_value, $old_value ) {
140
- $data = explode( "_", $new_value );
141
- if ( ! empty( $data[1] ) ) {
142
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[1] );
143
- }
144
-
145
- return wp_json_encode( $data );
146
- }
147
-
148
- /**
149
- * Registers plugin's settings.
150
- */
151
- public static function admin_init_googleanalytics() {
152
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_OPTION_NAME );
153
- register_setting( GA_NAME, self::GA_EXCLUDE_ROLES_OPTION_NAME );
154
- register_setting( GA_NAME, self::GA_SELECTED_ACCOUNT );
155
- register_setting( GA_NAME, self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
156
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
157
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
158
- add_filter( 'pre_update_option_' . Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles', 1, 2 );
159
- add_filter( 'pre_update_option_' . Ga_Admin::GA_SELECTED_ACCOUNT, 'Ga_Admin::preupdate_selected_account', 1, 2 );
160
- }
161
-
162
- public static function admin_menu_googleanalytics() {
163
- if ( current_user_can( 'manage_options' ) ) {
164
- add_menu_page( 'Google Analytics', 'Google Analytics', 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics', 'dashicons-chart-line', 1000 );
165
- add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Dashboard' ), 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics' );
166
- add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Settings' ), 'manage_options', 'googleanalytics/settings', 'Ga_Admin::options_page_googleanalytics' );
167
- }
168
- }
169
-
170
- public static function update_terms() {
171
- if ( !empty( $_GET['accept-terms'] ) && ( 'Y' === $_GET['accept-terms'] ) ) {
172
- update_option( self::GA_SHARETHIS_TERMS_OPTION_NAME, true );
173
- }
174
- }
175
- /**
176
- * Prepares and displays plugin's stats page.
177
- */
178
- public static function statistics_page_googleanalytics() {
179
-
180
- if ( ! Ga_Helper::is_wp_version_valid() ) {
181
- return false;
182
- }
183
- self::update_terms();
184
- $data = self::get_stats_page();
185
- Ga_View::load( 'statistics', array(
186
- 'data' => $data
187
- ) );
188
- }
189
-
190
- /**
191
- * Prepares and displays plugin's settings page.
192
- */
193
- public static function options_page_googleanalytics() {
194
-
195
- if ( ! Ga_Helper::is_wp_version_valid() ) {
196
- return false;
197
- }
198
- self::update_terms();
199
-
200
- /**
201
- * Keeps data to be extracted as variables in the view.
202
- *
203
- * @var array $data
204
- */
205
- $data = array();
206
-
207
- $data[ self::GA_WEB_PROPERTY_ID_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
208
- $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
209
- $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
210
-
211
- $roles = Ga_Helper::get_user_roles();
212
- $saved = json_decode( get_option( self::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
213
-
214
- $tmp = array();
215
- if ( ! empty( $roles ) ) {
216
- foreach ( $roles as $role ) {
217
- $role_id = Ga_Helper::prepare_role_id( $role );
218
- $tmp[] = array(
219
- 'name' => $role,
220
- 'id' => $role_id,
221
- 'checked' => ( ! empty( $saved[ $role_id ] ) && $saved[ $role_id ] === 'on' )
222
- );
223
- }
224
- }
225
- $data['roles'] = $tmp;
226
-
227
- if ( Ga_Helper::is_authorized() ) {
228
- $data['ga_accounts_selector'] = self::get_accounts_selector();
229
- } else {
230
- $data['popup_url'] = self::get_auth_popup_url();
231
- }
232
-
233
- Ga_View::load( 'page', array(
234
- 'data' => $data
235
- ) );
236
- }
237
-
238
- /**
239
- * Prepares and returns a plugin's URL to be opened in a popup window
240
- * during Google authentication process.
241
- *
242
- * @return mixed
243
- */
244
- public static function get_auth_popup_url() {
245
- return admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL . '&ga_action=ga_auth' );
246
- }
247
-
248
- /**
249
- * Prepares and returns Google Account's dropdown code.
250
- *
251
- * @return string
252
- */
253
- public static function get_accounts_selector() {
254
- $selected = Ga_Helper::get_selected_account_data();
255
-
256
- return Ga_View::load( 'ga_accounts_selector', array(
257
- 'selector' => json_decode( get_option( self::GA_ACCOUNT_DATA_OPTION_NAME ), true ),
258
- 'selected' => $selected ? implode( "_", $selected ) : null,
259
- 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled()
260
- ), true );
261
- }
262
-
263
- /**
264
- * Adds Bootstrap scripts.
265
- */
266
- public static function enqueue_bootstrap() {
267
- wp_register_script( GA_NAME . '-bootstrap-js', GA_PLUGIN_URL . '/js/bootstrap.min.js', array(
268
- 'jquery'
269
- ) );
270
- wp_register_style( GA_NAME . '-bootstrap-css', GA_PLUGIN_URL . '/css/bootstrap.min.css', false, null, 'all' );
271
- wp_enqueue_script( GA_NAME . '-bootstrap-js' );
272
- wp_enqueue_style( GA_NAME . '-bootstrap-css' );
273
- }
274
-
275
- /**
276
- * Adds JS scripts for the settings page.
277
- */
278
- public static function enqueue_ga_scripts() {
279
- wp_register_script( GA_NAME . '-page-js', GA_PLUGIN_URL . '/js/' . GA_NAME . '_page.js', array(
280
- 'jquery'
281
- ) );
282
- wp_enqueue_script( GA_NAME . '-page-js' );
283
- }
284
-
285
- /**
286
- * Adds CSS plugin's scripts.
287
- */
288
- public static function enqueue_ga_css() {
289
- wp_register_style( GA_NAME . '-css', GA_PLUGIN_URL . '/css/' . GA_NAME . '.css', false, null, 'all' );
290
- wp_enqueue_style( GA_NAME . '-css' );
291
- }
292
-
293
- /**
294
- * Enqueues dashboard JS scripts.
295
- */
296
- private static function enqueue_dashboard_scripts() {
297
- wp_register_script( GA_NAME . '-dashboard-js', GA_PLUGIN_URL . '/js/' . GA_NAME . '_dashboard.js', array(
298
- 'jquery'
299
- ) );
300
- wp_enqueue_script( GA_NAME . '-dashboard-js' );
301
- }
302
-
303
- /**
304
- * Enqueues plugin's JS and CSS scripts.
305
- */
306
- public static function enqueue_scripts() {
307
- if ( Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page() ) {
308
- wp_register_script( GA_NAME . '-js', GA_PLUGIN_URL . '/js/' . GA_NAME . '.js', array(
309
- 'jquery'
310
- ) );
311
- wp_enqueue_script( GA_NAME . '-js' );
312
-
313
- wp_register_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', null, null, false );
314
- wp_enqueue_script( 'googlecharts' );
315
-
316
- self::enqueue_ga_css();
317
- }
318
-
319
- if ( Ga_Helper::is_dashboard_page() ) {
320
- self::enqueue_dashboard_scripts();
321
- }
322
-
323
- if ( Ga_Helper::is_plugin_page() ) {
324
- self::enqueue_bootstrap();
325
- self::enqueue_ga_scripts();
326
- }
327
- }
328
-
329
- /**
330
- * Prepares plugin's statistics page and return HTML code.
331
- *
332
- * @return string HTML code
333
- */
334
- public static function get_stats_page() {
335
- $selected = Ga_Helper::get_selected_account_data( true );
336
-
337
- $query_params = Ga_Stats::get_query( 'main_chart', $selected['view_id'] );
338
- $stats_data = self::api_client()->call( 'ga_api_data', array(
339
-
340
- $query_params
341
- ) );
342
-
343
- $boxes_data = self::api_client()->call( 'ga_api_data', array(
344
-
345
- Ga_Stats::get_query( 'boxes', $selected['view_id'] )
346
- ) );
347
- $sources_data = self::api_client()->call( 'ga_api_data', array(
348
-
349
- Ga_Stats::get_query( 'sources', $selected['view_id'] )
350
- ) );
351
- $chart = ! empty( $stats_data ) ? Ga_Stats::get_chart( $stats_data->getData() ) : array();
352
- $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_boxes( $boxes_data->getData() ) : array();
353
- $last_chart_date = ! empty( $chart ) ? $chart['date'] : strtotime( 'now' );
354
- unset( $chart['date'] );
355
- $labels = array(
356
- 'thisWeek' => date( 'M d, Y', strtotime( '-6 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
357
- 'lastWeek' => date( 'M d, Y', strtotime( '-13 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', strtotime( '-7 day', $last_chart_date ) )
358
- );
359
- $sources = ! empty( $sources_data ) ? Ga_Stats::get_sources( $sources_data->getData() ) : array();
360
-
361
- return Ga_Helper::get_chart_page( 'stats', array(
362
-
363
- 'chart' => $chart,
364
- 'boxes' => $boxes,
365
- 'labels' => $labels,
366
- 'sources' => $sources
367
- ) );
368
- }
369
-
370
- /**
371
- * Shows plugin's notice on the admin area.
372
- */
373
- public static function admin_notice_googleanalytics() {
374
-
375
- 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 ) ) ) {
376
- if ( !empty( $_GET['accept-terms'] ) && ( 'Y' === $_GET['accept-terms'] ) ) {
377
- return;
378
- }
379
- $url = home_url( $_SERVER['REQUEST_URI'] . '&accept-terms=Y' );
380
- Ga_View::load( 'ga_notice', array(
381
-
382
- 'url' => $url
383
- ) );
384
- }
385
-
386
- if ( ! Ga_Helper::is_wp_version_valid() ) {
387
- Ga_View::load( 'ga_oauth_notice', array(
388
- 'msg' => _( 'Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION )
389
- ) );
390
- }
391
- }
392
-
393
- /**
394
- * Hides plugin's notice
395
- */
396
- public static function admin_notice_hide_googleanalytics() {
397
- update_option( self::GA_HIDE_TERMS_OPTION_NAME, true );
398
- }
399
-
400
- /**
401
- * Adds GA dashboard widget only for administrators.
402
- */
403
- public static function add_dashboard_device_widget() {
404
- if ( Ga_Helper::is_administrator() ) {
405
- wp_add_dashboard_widget( 'ga_dashboard_widget', __( 'Google Analytics Dashboard' ), 'Ga_Helper::add_ga_dashboard_widget' );
406
- }
407
- }
408
-
409
- /**
410
- * Adds plugin's actions
411
- */
412
- public static function add_actions() {
413
- add_action( 'admin_init', 'Ga_Admin::admin_init_googleanalytics' );
414
- add_action( 'admin_menu', 'Ga_Admin::admin_menu_googleanalytics' );
415
- add_action( 'admin_enqueue_scripts', 'Ga_Admin::enqueue_scripts' );
416
- add_action( 'wp_dashboard_setup', 'Ga_Admin::add_dashboard_device_widget' );
417
- add_action( 'wp_ajax_ga_ajax_data_change', 'Ga_Admin::ga_ajax_data_change' );
418
- add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics' );
419
-
420
- if ( ! get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && ! get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) {
421
- add_action( 'wp_ajax_googleanalytics_hide_terms', 'Ga_Admin::admin_notice_hide_googleanalytics' );
422
- }
423
- }
424
-
425
- /**
426
- * Adds plugin's filters
427
- */
428
- public static function add_filters() {
429
- add_filter( 'plugin_action_links', 'Ga_Admin::ga_action_links', 10, 5 );
430
- }
431
-
432
- /**
433
- * Adds new action links on the plugin list.
434
- *
435
- * @param $actions
436
- * @param $plugin_file
437
- *
438
- * @return mixed
439
- */
440
- public static function ga_action_links( $actions, $plugin_file ) {
441
-
442
- if ( basename( $plugin_file ) == GA_NAME . '.php' ) {
443
- array_unshift( $actions, '<a href="' . esc_url( get_admin_url( null, Ga_Helper::GA_SETTINGS_PAGE_URL ) ) . '">' . _( 'Settings' ) . '</a>' );
444
- }
445
-
446
- return $actions;
447
- }
448
-
449
- public static function init_oauth() {
450
- // $code = ! empty( $_GET['code'] ) ? $_GET['code'] : null;
451
- $code = Ga_Helper::get_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
452
-
453
- if ( ! Ga_Helper::is_authorized() && $code ) {
454
- Ga_Helper::update_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME, "" );
455
-
456
- // Get access token
457
- $response = self::api_client()->call( 'ga_auth_get_access_token', $code );
458
-
459
- self::save_access_token( $response );
460
- self::api_client()->set_access_token( $response->getData() ); // sprawdzic
461
-
462
- // Get accounts data
463
- $account_summaries = self::api_client()->call( 'ga_api_account_summaries' );
464
- self::save_ga_account_summaries( $account_summaries->getData() );
465
-
466
- wp_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL ) );
467
- }
468
- }
469
-
470
- public static function handle_actions() {
471
- $action = ! empty( $_GET['ga_action'] ) ? $_GET['ga_action'] : null;
472
-
473
- if ( $action ) {
474
- $class = __CLASS__;
475
- if ( is_callable( array(
476
-
477
- $class,
478
- $action
479
- ) ) ) {
480
- $class::$action();
481
- }
482
- }
483
- }
484
-
485
- public static function ga_auth() {
486
- if ( Ga_Helper::are_terms_accepted() ) {
487
- header( 'Location:' . self::api_client()->create_auth_url() );
488
- } else {
489
- wp_die( Ga_Helper::ga_oauth_notice( __( 'Please accept the terms to use this feature' ) ) );
490
- }
491
- }
492
-
493
- /**
494
- * Save access token.
495
- *
496
- * @param Ga_Lib_Api_Response $response
497
- *
498
- * @return boolean
499
- */
500
- public static function save_access_token( $response ) {
501
- $access_token = $response->getData();
502
- $access_token['created'] = time();
503
-
504
- return update_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode( $access_token ) );
505
- }
506
-
507
- /**
508
- * Saves Google Analytics account data.
509
- *
510
- * @param $data
511
- *
512
- * @return array
513
- */
514
- public static function save_ga_account_summaries( $data ) {
515
- $return = array();
516
- if ( ! empty( $data['items'] ) ) {
517
- foreach ( $data['items'] as $item ) {
518
- $tmp = array();
519
- $tmp['id'] = $item['id'];
520
- $tmp['name'] = $item['name'];
521
- if ( is_array( $item['webProperties'] ) ) {
522
- foreach ( $item['webProperties'] as $property ) {
523
- $profiles = array();
524
- if ( is_array( $property['profiles'] ) ) {
525
- foreach ( $property['profiles'] as $profile ) {
526
- $profiles[] = array(
527
-
528
- 'id' => $profile['id'],
529
- 'name' => $profile['name']
530
- );
531
- }
532
- }
533
-
534
- $tmp['webProperties'][] = array(
535
-
536
- 'webPropertyId' => $property['id'],
537
- 'name' => $property['name'],
538
- 'profiles' => $profiles
539
- );
540
- }
541
- }
542
-
543
- $return[] = $tmp;
544
- }
545
-
546
- update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode( $return ) );
547
- update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, "" );
548
- }
549
-
550
- return $return;
551
- }
552
-
553
- public static function ga_ajax_data_change() {
554
- $date_range = ! empty( $_POST['date_range'] ) ? $_POST['date_range'] : null;
555
- $metric = ! empty( $_POST['metric'] ) ? $_POST['metric'] : null;
556
- echo Ga_Helper::get_ga_dashboard_widget_data_json( $date_range, $metric, false, true );
557
- wp_die();
558
- }
559
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
+ class Ga_Admin
4
+ {
5
+
6
+ const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
7
+
8
+ const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
9
+
10
+ const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
11
+
12
+ const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
13
+
14
+ const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
15
+
16
+ const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
17
+
18
+ const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
19
+
20
+ const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
21
+
22
+ const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
23
+
24
+ const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
25
+
26
+ const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
27
+
28
+ const MIN_WP_VERSION = '3.8';
29
+
30
+ const NOTICE_SUCCESS = 'success';
31
+
32
+ const NOTICE_WARNING = 'warning';
33
+
34
+ const NOTICE_ERROR = 'error';
35
+
36
+ /**
37
+ * Instantiate API client.
38
+ *
39
+ * @return Ga_Lib_Api_Client|null
40
+ */
41
+ public static function api_client()
42
+ {
43
+ $instance = Ga_Lib_Api_Client::get_instance();
44
+ $token = Ga_Helper::get_option(self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME);
45
+ try {
46
+ if ( ! empty($token)) {
47
+ $token = json_decode($token, true);
48
+ $instance->set_access_token($token);
49
+ }
50
+ } catch (Exception $e) {
51
+ Ga_Helper::ga_oauth_notice($e->getMessage());
52
+ }
53
+
54
+ return $instance;
55
+ }
56
+
57
+ /*
58
+ * Initializes plugin's options during plugin activation process.
59
+ */
60
+ public static function activate_googleanalytics()
61
+ {
62
+ add_option(self::GA_WEB_PROPERTY_ID_OPTION_NAME, Ga_Helper::GA_DEFAULT_WEB_ID);
63
+ add_option(self::GA_EXCLUDE_ROLES_OPTION_NAME, wp_json_encode(array()));
64
+ add_option(self::GA_SHARETHIS_TERMS_OPTION_NAME, false);
65
+ add_option(self::GA_HIDE_TERMS_OPTION_NAME, false);
66
+ add_option(self::GA_VERSION_OPTION_NAME);
67
+ add_option(self::GA_OAUTH_AUTH_CODE_OPTION_NAME);
68
+ add_option(self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME);
69
+ add_option(self::GA_ACCOUNT_DATA_OPTION_NAME);
70
+ add_option(self::GA_SELECTED_ACCOUNT);
71
+ add_option(self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME);
72
+ add_option(self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME);
73
+ }
74
+
75
+ /*
76
+ * Deletes plugin's options during plugin activation process.
77
+ */
78
+ public static function deactivate_googleanalytics()
79
+ {
80
+ delete_option(self::GA_WEB_PROPERTY_ID_OPTION_NAME);
81
+ delete_option(self::GA_EXCLUDE_ROLES_OPTION_NAME);
82
+ delete_option(self::GA_OAUTH_AUTH_CODE_OPTION_NAME);
83
+ delete_option(self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME);
84
+ delete_option(self::GA_ACCOUNT_DATA_OPTION_NAME);
85
+ delete_option(self::GA_SELECTED_ACCOUNT);
86
+ delete_option(self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME);
87
+ delete_option(self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME);
88
+ }
89
+
90
+ /**
91
+ * Deletes plugin's options during plugin uninstallation process.
92
+ */
93
+ public static function uninstall_googleanalytics()
94
+ {
95
+ delete_option(self::GA_SHARETHIS_TERMS_OPTION_NAME);
96
+ delete_option(self::GA_HIDE_TERMS_OPTION_NAME);
97
+ delete_option(self::GA_VERSION_OPTION_NAME);
98
+ }
99
+
100
+ /**
101
+ * Do actions during plugin load.
102
+ */
103
+ public static function loaded_googleanalytics()
104
+ {
105
+ self::update_googleanalytics();
106
+ }
107
+
108
+ /**
109
+ * Update hook fires when plugin is being loaded.
110
+ */
111
+ public static function update_googleanalytics()
112
+ {
113
+
114
+ $version = get_option(self::GA_VERSION_OPTION_NAME);
115
+ $installed_version = get_option(self::GA_VERSION_OPTION_NAME, '1.0.7');
116
+ $old_property_value = Ga_Helper::get_option('web_property_id');
117
+ if (version_compare($installed_version, GOOGLEANALYTICS_VERSION, 'eq')) {
118
+ return;
119
+ }
120
+ if (empty($old_property_value) && empty($version)) {
121
+ update_option(self::GA_SHARETHIS_TERMS_OPTION_NAME, true);
122
+ }
123
+
124
+ if (version_compare($installed_version, GOOGLEANALYTICS_VERSION, 'lt')) {
125
+
126
+ if ( ! empty($old_property_value)) {
127
+ Ga_Helper::update_option(self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME, $old_property_value);
128
+ Ga_Helper::update_option(self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, 1);
129
+ delete_option('web_property_id');
130
+ }
131
+ }
132
+
133
+ update_option(self::GA_VERSION_OPTION_NAME, GOOGLEANALYTICS_VERSION);
134
+ }
135
+
136
+
137
+ public static function preupdate_exclude_roles($new_value, $old_value)
138
+ {
139
+ if ( ! Ga_Helper::are_terms_accepted()) {
140
+ return '';
141
+ }
142
+
143
+ return wp_json_encode($new_value);
144
+ }
145
+
146
+ /**
147
+ * Pre-update hook for preparing JSON structure.
148
+ *
149
+ * @param $new_value
150
+ * @param $old_value
151
+ *
152
+ * @return mixed
153
+ */
154
+ public static function preupdate_selected_account($new_value, $old_value)
155
+ {
156
+ if ( ! empty($new_value)) {
157
+ $data = explode("_", $new_value);
158
+
159
+ if ( ! empty($data[1])) {
160
+ Ga_Helper::update_option(self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[1]);
161
+ }
162
+ }
163
+
164
+ return wp_json_encode($data);
165
+ }
166
+
167
+ /**
168
+ * Registers plugin's settings.
169
+ */
170
+ public static function admin_init_googleanalytics()
171
+ {
172
+ register_setting(GA_NAME, self::GA_WEB_PROPERTY_ID_OPTION_NAME);
173
+ register_setting(GA_NAME, self::GA_EXCLUDE_ROLES_OPTION_NAME);
174
+ register_setting(GA_NAME, self::GA_SELECTED_ACCOUNT);
175
+ register_setting(GA_NAME, self::GA_OAUTH_AUTH_CODE_OPTION_NAME);
176
+ register_setting(GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME);
177
+ register_setting(GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME);
178
+ add_filter('pre_update_option_' . Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles',
179
+ 1, 2);
180
+ add_filter('pre_update_option_' . Ga_Admin::GA_SELECTED_ACCOUNT, 'Ga_Admin::preupdate_selected_account', 1, 2);
181
+ }
182
+
183
+ /**
184
+ * Builds plugin's menu structure.
185
+ */
186
+ public static function admin_menu_googleanalytics()
187
+ {
188
+ if (current_user_can('manage_options')) {
189
+ add_menu_page('Google Analytics', 'Google Analytics', 'manage_options', 'googleanalytics',
190
+ 'Ga_Admin::statistics_page_googleanalytics', 'dashicons-chart-line', 1000);
191
+ add_submenu_page('googleanalytics', 'Google Analytics', __('Dashboard'), 'manage_options',
192
+ 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics');
193
+ add_submenu_page('googleanalytics', 'Google Analytics', __('Settings'), 'manage_options',
194
+ 'googleanalytics/settings', 'Ga_Admin::options_page_googleanalytics');
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Sets accept terms option to TRUE.
200
+ */
201
+ public static function update_terms()
202
+ {
203
+ update_option(self::GA_SHARETHIS_TERMS_OPTION_NAME, true);
204
+ wp_redirect(admin_url(Ga_Helper::GA_SETTINGS_PAGE_URL));
205
+ }
206
+
207
+ /**
208
+ * Prepares and displays plugin's stats page.
209
+ */
210
+ public static function statistics_page_googleanalytics()
211
+ {
212
+
213
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid()) {
214
+ return false;
215
+ }
216
+
217
+ $data = self::get_stats_page();
218
+ Ga_View::load('statistics', array(
219
+ 'data' => $data
220
+ ));
221
+ }
222
+
223
+ /**
224
+ * Prepares and displays plugin's settings page.
225
+ */
226
+ public static function options_page_googleanalytics()
227
+ {
228
+
229
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid()) {
230
+ return false;
231
+ }
232
+
233
+ /**
234
+ * Keeps data to be extracted as variables in the view.
235
+ *
236
+ * @var array $data
237
+ */
238
+ $data = array();
239
+
240
+ $data[self::GA_WEB_PROPERTY_ID_OPTION_NAME] = get_option(self::GA_WEB_PROPERTY_ID_OPTION_NAME);
241
+ $data[self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME] = get_option(self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME);
242
+ $data[self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME] = get_option(self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME);
243
+
244
+ $roles = Ga_Helper::get_user_roles();
245
+ $saved = json_decode(get_option(self::GA_EXCLUDE_ROLES_OPTION_NAME), true);
246
+
247
+ $tmp = array();
248
+ if ( ! empty($roles)) {
249
+ foreach ($roles as $role) {
250
+ $role_id = Ga_Helper::prepare_role_id($role);
251
+ $tmp[] = array(
252
+ 'name' => $role,
253
+ 'id' => $role_id,
254
+ 'checked' => ( ! empty($saved[$role_id]) && $saved[$role_id] === 'on')
255
+ );
256
+ }
257
+ }
258
+ $data['roles'] = $tmp;
259
+
260
+ if (Ga_Helper::is_authorized()) {
261
+ $data['ga_accounts_selector'] = self::get_accounts_selector();
262
+ } else {
263
+ $data['popup_url'] = self::get_auth_popup_url();
264
+ }
265
+ if ( ! empty($_GET['err'])) {
266
+ switch ($_GET['err']) {
267
+ case 1:
268
+ $data['error_message'] = Ga_Helper::ga_oauth_notice('There was a problem with Google Oauth2 authentication process.');
269
+ break;
270
+ }
271
+ }
272
+ Ga_View::load('page', array(
273
+ 'data' => $data
274
+ ));
275
+ }
276
+
277
+ /**
278
+ * Prepares and returns a plugin's URL to be opened in a popup window
279
+ * during Google authentication process.
280
+ *
281
+ * @return mixed
282
+ */
283
+ public static function get_auth_popup_url()
284
+ {
285
+ return admin_url(Ga_Helper::GA_SETTINGS_PAGE_URL . '&ga_action=ga_auth');
286
+ }
287
+
288
+ /**
289
+ * Prepares and returns Google Account's dropdown code.
290
+ *
291
+ * @return string
292
+ */
293
+ public static function get_accounts_selector()
294
+ {
295
+ $selected = Ga_Helper::get_selected_account_data();
296
+
297
+ return Ga_View::load('ga_accounts_selector', array(
298
+ 'selector' => json_decode(get_option(self::GA_ACCOUNT_DATA_OPTION_NAME), true),
299
+ 'selected' => $selected ? implode("_", $selected) : null,
300
+ 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled()
301
+ ), true);
302
+ }
303
+
304
+ /**
305
+ * Adds Bootstrap scripts.
306
+ */
307
+ public static function enqueue_bootstrap()
308
+ {
309
+ wp_register_script(GA_NAME . '-bootstrap-js', GA_PLUGIN_URL . '/js/bootstrap.min.js', array(
310
+ 'jquery'
311
+ ));
312
+ wp_register_style(GA_NAME . '-bootstrap-css', GA_PLUGIN_URL . '/css/bootstrap.min.css', false, null, 'all');
313
+ wp_enqueue_script(GA_NAME . '-bootstrap-js');
314
+ wp_enqueue_style(GA_NAME . '-bootstrap-css');
315
+ }
316
+
317
+ /**
318
+ * Adds JS scripts for the settings page.
319
+ */
320
+ public static function enqueue_ga_scripts()
321
+ {
322
+ wp_register_script(GA_NAME . '-page-js', GA_PLUGIN_URL . '/js/' . GA_NAME . '_page.js', array(
323
+ 'jquery'
324
+ ));
325
+ wp_enqueue_script(GA_NAME . '-page-js');
326
+ }
327
+
328
+ /**
329
+ * Adds CSS plugin's scripts.
330
+ */
331
+ public static function enqueue_ga_css()
332
+ {
333
+ wp_register_style(GA_NAME . '-css', GA_PLUGIN_URL . '/css/' . GA_NAME . '.css', false, null, 'all');
334
+ wp_enqueue_style(GA_NAME . '-css');
335
+ }
336
+
337
+ /**
338
+ * Enqueues dashboard JS scripts.
339
+ */
340
+ private static function enqueue_dashboard_scripts()
341
+ {
342
+ wp_register_script(GA_NAME . '-dashboard-js', GA_PLUGIN_URL . '/js/' . GA_NAME . '_dashboard.js', array(
343
+ 'jquery'
344
+ ));
345
+ wp_enqueue_script(GA_NAME . '-dashboard-js');
346
+ }
347
+
348
+ /**
349
+ * Enqueues plugin's JS and CSS scripts.
350
+ */
351
+ public static function enqueue_scripts()
352
+ {
353
+ if (Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page()) {
354
+ wp_register_script(GA_NAME . '-js', GA_PLUGIN_URL . '/js/' . GA_NAME . '.js', array(
355
+ 'jquery'
356
+ ));
357
+ wp_enqueue_script(GA_NAME . '-js');
358
+
359
+ wp_register_script('googlecharts', 'https://www.gstatic.com/charts/loader.js', null, null, false);
360
+ wp_enqueue_script('googlecharts');
361
+
362
+ self::enqueue_ga_css();
363
+ }
364
+
365
+ if (Ga_Helper::is_dashboard_page()) {
366
+ self::enqueue_dashboard_scripts();
367
+ }
368
+
369
+ if (Ga_Helper::is_plugin_page()) {
370
+ self::enqueue_bootstrap();
371
+ self::enqueue_ga_scripts();
372
+ }
373
+ }
374
+
375
+ /**
376
+ * Prepares plugin's statistics page and return HTML code.
377
+ *
378
+ * @return string HTML code
379
+ */
380
+ public static function get_stats_page()
381
+ {
382
+ $chart = null;
383
+ $boxes = null;
384
+ $labels = null;
385
+ $sources = null;
386
+ if (Ga_Helper::is_authorized() && Ga_Helper::is_account_selected()) {
387
+ $selected = Ga_Helper::get_selected_account_data(true);
388
+
389
+ $query_params = Ga_Stats::get_query('main_chart', $selected['view_id']);
390
+ $stats_data = self::api_client()->call('ga_api_data', array(
391
+
392
+ $query_params
393
+ ));
394
+
395
+ $boxes_data = self::api_client()->call('ga_api_data', array(
396
+
397
+ Ga_Stats::get_query('boxes', $selected['view_id'])
398
+ ));
399
+ $sources_data = self::api_client()->call('ga_api_data', array(
400
+
401
+ Ga_Stats::get_query('sources', $selected['view_id'])
402
+ ));
403
+ $chart = ! empty($stats_data) ? Ga_Stats::get_chart($stats_data->getData()) : array();
404
+ $boxes = ! empty($boxes_data) ? Ga_Stats::get_boxes($boxes_data->getData()) : array();
405
+ $last_chart_date = ! empty($chart) ? $chart['date'] : strtotime('now');
406
+ unset($chart['date']);
407
+ $labels = array(
408
+ 'thisWeek' => date('M d, Y', strtotime('-6 day', $last_chart_date)) . ' - ' . date('M d, Y',
409
+ $last_chart_date),
410
+ 'lastWeek' => date('M d, Y', strtotime('-13 day', $last_chart_date)) . ' - ' . date('M d, Y',
411
+ strtotime('-7 day', $last_chart_date))
412
+ );
413
+ $sources = ! empty($sources_data) ? Ga_Stats::get_sources($sources_data->getData()) : array();
414
+ }
415
+
416
+ return Ga_Helper::get_chart_page('stats', array(
417
+
418
+ 'chart' => $chart,
419
+ 'boxes' => $boxes,
420
+ 'labels' => $labels,
421
+ 'sources' => $sources
422
+ ));
423
+ }
424
+
425
+ /**
426
+ * Shows plugin's notice on the admin area.
427
+ */
428
+ public static function admin_notice_googleanalytics()
429
+ {
430
+ 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))) {
431
+ $current_url = Ga_Helper::get_current_url();
432
+ $url = (strstr($current_url,
433
+ '?') ? $current_url . '&' : $current_url . '?') . http_build_query(array('ga_action' => 'update_terms'));
434
+ Ga_View::load('ga_notice', array(
435
+ 'url' => $url
436
+ ));
437
+ }
438
+
439
+ if ( ! empty($_GET['settings-updated'])) {
440
+ echo Ga_Helper::ga_wp_notice(_('Settings saved'), self::NOTICE_SUCCESS);
441
+ }
442
+ }
443
+
444
+ /**
445
+ * Prepare required PHP version warning.
446
+ * @return string
447
+ */
448
+ public static function admin_notice_googleanalytics_php_version()
449
+ {
450
+ 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),
451
+ self::NOTICE_ERROR);
452
+ }
453
+
454
+ /**
455
+ * Prepare required WP version warning
456
+ * @return string
457
+ */
458
+ public static function admin_notice_googleanalytics_wp_version()
459
+ {
460
+ echo Ga_Helper::ga_wp_notice(_('Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION),
461
+ self::NOTICE_ERROR);
462
+ }
463
+
464
+ /**
465
+ * Hides plugin's notice
466
+ */
467
+ public static function admin_notice_hide_googleanalytics()
468
+ {
469
+ update_option(self::GA_HIDE_TERMS_OPTION_NAME, true);
470
+ }
471
+
472
+ /**
473
+ * Adds GA dashboard widget only for administrators.
474
+ */
475
+ public static function add_dashboard_device_widget()
476
+ {
477
+ if (Ga_Helper::is_administrator()) {
478
+ wp_add_dashboard_widget('ga_dashboard_widget', __('Google Analytics Dashboard'),
479
+ 'Ga_Helper::add_ga_dashboard_widget');
480
+ }
481
+ }
482
+
483
+ /**
484
+ * Adds plugin's actions
485
+ */
486
+ public static function add_actions()
487
+ {
488
+ add_action('admin_init', 'Ga_Admin::admin_init_googleanalytics');
489
+ add_action('admin_menu', 'Ga_Admin::admin_menu_googleanalytics');
490
+ add_action('admin_enqueue_scripts', 'Ga_Admin::enqueue_scripts');
491
+ add_action('wp_dashboard_setup', 'Ga_Admin::add_dashboard_device_widget');
492
+ add_action('wp_ajax_ga_ajax_data_change', 'Ga_Admin::ga_ajax_data_change');
493
+ add_action('admin_notices', 'Ga_Admin::admin_notice_googleanalytics');
494
+
495
+ if ( ! get_option(self::GA_SHARETHIS_TERMS_OPTION_NAME) && ! get_option(self::GA_HIDE_TERMS_OPTION_NAME)) {
496
+ add_action('wp_ajax_googleanalytics_hide_terms', 'Ga_Admin::admin_notice_hide_googleanalytics');
497
+ }
498
+ }
499
+
500
+ /**
501
+ * Adds plugin's filters
502
+ */
503
+ public static function add_filters()
504
+ {
505
+ add_filter('plugin_action_links', 'Ga_Admin::ga_action_links', 10, 5);
506
+ }
507
+
508
+ /**
509
+ * Adds new action links on the plugin list.
510
+ *
511
+ * @param $actions
512
+ * @param $plugin_file
513
+ *
514
+ * @return mixed
515
+ */
516
+ public static function ga_action_links($actions, $plugin_file)
517
+ {
518
+
519
+ if (basename($plugin_file) == GA_NAME . '.php') {
520
+ array_unshift($actions, '<a href="' . esc_url(get_admin_url(null,
521
+ Ga_Helper::GA_SETTINGS_PAGE_URL)) . '">' . _('Settings') . '</a>');
522
+ }
523
+
524
+ return $actions;
525
+ }
526
+
527
+ public static function init_oauth()
528
+ {
529
+ // $code = ! empty( $_GET['code'] ) ? $_GET['code'] : null;
530
+ $code = Ga_Helper::get_option(self::GA_OAUTH_AUTH_CODE_OPTION_NAME);
531
+
532
+ if ( ! Ga_Helper::is_authorized() && $code) {
533
+ Ga_Helper::update_option(self::GA_OAUTH_AUTH_CODE_OPTION_NAME, "");
534
+
535
+ // Get access token
536
+ $response = self::api_client()->call('ga_auth_get_access_token', $code);
537
+ $param = '';
538
+ if ( ! self::save_access_token($response)) {
539
+ $param = '&err=1';
540
+ }
541
+ self::api_client()->set_access_token($response->getData());
542
+
543
+ // Get accounts data
544
+ $account_summaries = self::api_client()->call('ga_api_account_summaries');
545
+ self::save_ga_account_summaries($account_summaries->getData());
546
+
547
+ wp_redirect(admin_url(Ga_Helper::GA_SETTINGS_PAGE_URL . $param));
548
+ }
549
+ }
550
+
551
+ public static function handle_actions()
552
+ {
553
+ $action = ! empty($_GET['ga_action']) ? $_GET['ga_action'] : null;
554
+
555
+ if ($action) {
556
+ $class = __CLASS__;
557
+ if (is_callable(array(
558
+
559
+ $class,
560
+ $action
561
+ ))) {
562
+ call_user_func($class . '::' . $action);
563
+ }
564
+ }
565
+ }
566
+
567
+ public static function ga_auth()
568
+ {
569
+ if (Ga_Helper::are_terms_accepted()) {
570
+ header('Location:' . self::api_client()->create_auth_url());
571
+ } else {
572
+ wp_die(Ga_Helper::ga_oauth_notice(__('Please accept the terms to use this feature')));
573
+ }
574
+ }
575
+
576
+ /**
577
+ * Save access token.
578
+ *
579
+ * @param Ga_Lib_Api_Response $response
580
+ *
581
+ * @return boolean
582
+ */
583
+ public static function save_access_token($response)
584
+ {
585
+ $access_token = $response->getData();
586
+ if ( ! empty($access_token)) {
587
+ $access_token['created'] = time();
588
+ } else {
589
+ return false;
590
+ }
591
+
592
+ return update_option(self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode($access_token));
593
+ }
594
+
595
+ /**
596
+ * Saves Google Analytics account data.
597
+ *
598
+ * @param $data
599
+ *
600
+ * @return array
601
+ */
602
+ public static function save_ga_account_summaries($data)
603
+ {
604
+ $return = array();
605
+ if ( ! empty($data['items'])) {
606
+ foreach ($data['items'] as $item) {
607
+ $tmp = array();
608
+ $tmp['id'] = $item['id'];
609
+ $tmp['name'] = $item['name'];
610
+ if (is_array($item['webProperties'])) {
611
+ foreach ($item['webProperties'] as $property) {
612
+ $profiles = array();
613
+ if (is_array($property['profiles'])) {
614
+ foreach ($property['profiles'] as $profile) {
615
+ $profiles[] = array(
616
+
617
+ 'id' => $profile['id'],
618
+ 'name' => $profile['name']
619
+ );
620
+ }
621
+ }
622
+
623
+ $tmp['webProperties'][] = array(
624
+
625
+ 'webPropertyId' => $property['id'],
626
+ 'name' => $property['name'],
627
+ 'profiles' => $profiles
628
+ );
629
+ }
630
+ }
631
+
632
+ $return[] = $tmp;
633
+ }
634
+
635
+ update_option(self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode($return));
636
+ update_option(self::GA_WEB_PROPERTY_ID_OPTION_NAME, "");
637
+ }
638
+
639
+ return $return;
640
+ }
641
+
642
+ public static function ga_ajax_data_change()
643
+ {
644
+ $date_range = ! empty($_POST['date_range']) ? $_POST['date_range'] : null;
645
+ $metric = ! empty($_POST['metric']) ? $_POST['metric'] : null;
646
+ echo Ga_Helper::get_ga_dashboard_widget_data_json($date_range, $metric, false, true);
647
+ wp_die();
648
+ }
649
+ }
class/Ga_Helper.php CHANGED
@@ -1,399 +1,488 @@
1
  <?php
2
 
3
- class Ga_Helper {
4
-
5
- const ROLE_ID_PREFIX = "role-id-";
6
-
7
- const GA_DEFAULT_WEB_ID = "UA-0000000-0";
8
-
9
- const GA_STATISTICS_PAGE_URL = "admin.php?page=googleanalytics";
10
-
11
- const GA_SETTINGS_PAGE_URL = "admin.php?page=googleanalytics/settings";
12
-
13
- const DASHBOARD_PAGE_NAME = "dashboard";
14
-
15
- /**
16
- * Init plugin actions.
17
- *
18
- */
19
- public static function init() {
20
- if ( ! is_admin() ) {
21
- Ga_Frontend::add_actions();
22
- }
23
-
24
- if ( is_admin() ) {
25
- Ga_Admin::add_filters();
26
- Ga_Admin::add_actions();
27
- Ga_Admin::init_oauth();
28
- Ga_Admin::handle_actions();
29
- }
30
- }
31
-
32
- /**
33
- * Checks if current page is a WordPress dashboard.
34
- * @return int
35
- */
36
- public static function is_plugin_page() {
37
- $site = get_current_screen();
38
-
39
- return preg_match( '/' . GA_NAME . '/', $site->base );
40
- }
41
-
42
- /**
43
- * Checks if current page is a WordPress dashboard.
44
- * @return number
45
- */
46
- public static function is_dashboard_page() {
47
- $site = get_current_screen();
48
-
49
- return preg_match( '/' . self::DASHBOARD_PAGE_NAME . '/', $site->base );
50
- }
51
-
52
- /**
53
- * Check whether the plugin is configured.
54
- *
55
- * @param String $web_id
56
- *
57
- * @return boolean
58
- */
59
- public static function is_configured( $web_id ) {
60
- return $web_id !== self::GA_DEFAULT_WEB_ID;
61
- }
62
-
63
- /**
64
- * Prepare an array of current site's user roles
65
- *
66
- * return array
67
- */
68
- public static function get_user_roles() {
69
- global $wp_roles;
70
- if ( ! isset( $wp_roles ) ) {
71
- $wp_roles = new WP_Roles();
72
- }
73
-
74
- return $wp_roles->get_names();
75
- }
76
-
77
- /**
78
- * Prepare a role ID.
79
- *
80
- * The role ID is derived from the role's name and will be used
81
- * in its setting name in the additional settings.
82
- *
83
- * @param string $role_name Role name
84
- *
85
- * @return string
86
- */
87
- public static function prepare_role_id( $role_name ) {
88
- return self::ROLE_ID_PREFIX . strtolower( preg_replace( '/[\W]/', '-', before_last_bar( $role_name ) ) );
89
- }
90
-
91
- /**
92
- * Prepares role id.
93
- *
94
- * @param $v
95
- * @param $k
96
- */
97
- public static function prepare_role( &$v, $k ) {
98
- $v = self::prepare_role_id( $v );
99
- }
100
-
101
- /**
102
- * Checks whether user role is excluded from adding UA code.
103
- *
104
- * @return boolean
105
- */
106
- public static function can_add_ga_code() {
107
- $current_user = wp_get_current_user();
108
- $user_roles = ! empty( $current_user->roles ) ? $current_user->roles : array();
109
- $exclude_roles = json_decode( get_option( Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
110
-
111
- array_walk( $user_roles, 'Ga_Helper::prepare_role' );
112
-
113
- $return = true;
114
- foreach ( $user_roles as $role ) {
115
- if ( ! empty( $exclude_roles[ $role ] ) ) {
116
- $return = false;
117
- break;
118
- }
119
- }
120
-
121
- return $return;
122
- }
123
-
124
- /**
125
- * Adds ga dashboard widget HTML code for a WordPress
126
- * Dashboard widget hook.
127
- */
128
- public static function add_ga_dashboard_widget() {
129
- echo self::get_ga_dashboard_widget();
130
- }
131
-
132
- /**
133
- * Generates dashboard widget HTML code.
134
- *
135
- * @param string $date_range Google Analytics specific date range string.
136
- * @param boolean $text_mode
137
- * @param boolean $ajax
138
- *
139
- * @return null | string HTML dashboard widget code.
140
- */
141
- public static function get_ga_dashboard_widget( $date_range = null, $text_mode = false, $ajax = false ) {
142
- if ( empty( $date_range ) ) {
143
- $date_range = '30daysAgo';
144
- }
145
-
146
- // Get chart and boxes data
147
- $data = self::get_dashboard_widget_data( $date_range );
148
-
149
- if ( $text_mode ) {
150
- return self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
151
- 'chart' => $data['chart'],
152
- 'boxes' => $data['boxes']
153
- ) );
154
- } else {
155
- echo self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
156
- 'chart' => $data['chart'],
157
- 'boxes' => $data['boxes'],
158
- 'more_details_url' => admin_url( self::GA_STATISTICS_PAGE_URL )
159
- ) );
160
- }
161
-
162
- return null;
163
- }
164
-
165
- /**
166
- * Generates JSON data string for AJAX calls.
167
- *
168
- * @param string $date_range
169
- * @param string $metric
170
- * @param boolean $text_mode
171
- * @param boolean $ajax
172
- *
173
- * @return string|false Returns JSON data string
174
- */
175
- public static function get_ga_dashboard_widget_data_json( $date_range = null, $metric = null, $text_mode = false, $ajax = false ) {
176
- if ( empty( $date_range ) ) {
177
- $date_range = '30daysAgo';
178
- }
179
-
180
- if ( empty( $metric ) ) {
181
- $metric = 'pageviews';
182
- }
183
-
184
- $data = self::get_dashboard_widget_data( $date_range, $metric );
185
-
186
- return wp_json_encode( $data );
187
- }
188
-
189
- /**
190
- * Gets dashboard widget data.
191
- *
192
- * @param date_range
193
- * @param metric
194
- *
195
- * @return array Return chart and boxes data
196
- */
197
- private static function get_dashboard_widget_data( $date_range, $metric = null ) {
198
- $selected = self::get_selected_account_data( true );
199
-
200
- $query_params = Ga_Stats::get_query( 'main_chart', $selected['view_id'], $date_range, $metric );
201
- $stats_data = Ga_Admin::api_client()->call( 'ga_api_data', array(
202
-
203
- $query_params
204
- ) );
205
-
206
- $boxes_query = Ga_Stats::get_query( 'dashboard_boxes', $selected['view_id'], $date_range );
207
- $boxes_data = Ga_Admin::api_client()->call( 'ga_api_data', array(
208
-
209
- $boxes_query
210
- ) );
211
-
212
- $chart = ! empty( $stats_data ) ? Ga_Stats::get_dashboard_chart( $stats_data->getData() ) : array();
213
- $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_dashboard_boxes_data( $boxes_data->getData() ) : array();
214
-
215
- return array(
216
-
217
- 'chart' => $chart,
218
- 'boxes' => $boxes
219
- );
220
- }
221
-
222
- public static function is_account_selected() {
223
- return self::get_selected_account_data();
224
- }
225
-
226
- /**
227
- * Returns HTML code of the chart page or a notice.
228
- *
229
- * @param chart
230
- *
231
- * @return string Returns HTML code
232
- */
233
- public static function get_chart_page( $view, $params ) {
234
-
235
- $message = sprintf( __( 'Statistics can only be seen after you authenticate with your Google account on the <a href="%s">Settings page</a>.' ), admin_url( self::GA_SETTINGS_PAGE_URL ) );
236
-
237
- if ( self::is_authorized() && ! self::is_code_manually_enabled() ) {
238
- if ( self::is_account_selected() ) {
239
- if ( $params ) {
240
- return Ga_View::load( $view, $params, true );
241
- } else {
242
- return self::ga_oauth_notice( sprintf( 'Please configure your <a href="%s">Google Analytics settings</a>.', admin_url( self::GA_SETTINGS_PAGE_URL ) ) );
243
- }
244
- } else {
245
- return self::ga_oauth_notice( $message );
246
- }
247
- } else {
248
- return self::ga_oauth_notice( $message );
249
- }
250
- }
251
-
252
- /**
253
- * Checks whether users is authorized with Google.
254
- *
255
- * @return boolean
256
- */
257
- public static function is_authorized() {
258
- return Ga_Admin::api_client()->get_instance()->is_authorized();
259
- }
260
-
261
- /**
262
- * Wrapper for WordPress method get_option
263
- *
264
- * @param string $name Option name
265
- *
266
- * @return NULL|mixed|boolean
267
- */
268
- public static function get_option( $name ) {
269
- $opt = get_option( $name );
270
-
271
- return ! empty( $opt ) ? $opt : null;
272
- }
273
-
274
- /**
275
- * Wrapper for WordPress method update_option
276
- *
277
- * @param string $name
278
- * @param mixed $value
279
- *
280
- * @return NULL|boolean
281
- */
282
- public static function update_option( $name, $value ) {
283
- $opt = update_option( $name, $value );
284
-
285
- return ! empty( $opt ) ? $opt : null;
286
- }
287
-
288
- /**
289
- * Loads ga notice HTML code with gicen message included.
290
- *
291
- * @param string $message
292
- *
293
- * @return string
294
- */
295
- public static function ga_oauth_notice( $message ) {
296
- return Ga_View::load( 'ga_oauth_notice', array(
297
-
298
- 'msg' => $message
299
- ), true );
300
- }
301
-
302
- /**
303
- * Gets data according to selected GA account.
304
- *
305
- * @param boolean $assoc
306
- *
307
- * @return mixed
308
- */
309
- public static function get_selected_account_data( $assoc = false ) {
310
- $data = json_decode( self::get_option( Ga_Admin::GA_SELECTED_ACCOUNT ) );
311
- $data = ( ! empty( $data ) && count( $data ) == 3 ) ? $data : false;
312
-
313
- if ( $data ) {
314
- if ( $assoc ) {
315
- return array(
316
-
317
- 'account_id' => $data[0],
318
- 'web_property_id' => $data[1],
319
- 'view_id' => $data[2]
320
- );
321
- } else {
322
- return $data;
323
- }
324
- }
325
-
326
- return false;
327
- }
328
-
329
- /**
330
- * Chekcs whether option for manually UA-code
331
- * @return NULL|mixed|boolean
332
- */
333
- public static function is_code_manually_enabled() {
334
- return Ga_Helper::get_option( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
335
- }
336
-
337
- /**
338
- * Adds percent sign to the given text.
339
- *
340
- * @param $text
341
- *
342
- * @return string
343
- */
344
- public static function format_percent( $text ) {
345
- $text = self::add_plus( $text );
346
- return $text . '%';
347
- }
348
-
349
- /**
350
- * Adds plus sign before number.
351
- *
352
- * @param $number
353
- *
354
- * @return string
355
- */
356
- public static function add_plus( $number ) {
357
- if ( $number > 0 ){
358
- return '+' . $number;
359
- }
360
- return $number;
361
- }
362
-
363
- /**
364
- * Check whether current user has administrator privileges.
365
- *
366
- * @return bool
367
- */
368
- public static function is_administrator() {
369
- if ( current_user_can( 'administrator' ) ) {
370
- return true;
371
- }
372
-
373
- return false;
374
- }
375
-
376
- public static function is_wp_version_valid() {
377
- $wp_version = get_bloginfo( 'version' );
378
-
379
- return version_compare( $wp_version, Ga_Admin::MIN_WP_VERSION, 'ge' );
380
- }
381
-
382
- /**
383
- * Check if terms are accepted
384
- *
385
- * @return bool
386
- */
387
- public static function are_terms_accepted() {
388
- return self::get_option( Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME );
389
- }
390
-
391
- /**
392
- * Check if sharethis scripts enabled
393
- *
394
- * @return bool
395
- */
396
- public static function is_sharethis_included() {
397
- return GA_SHARETHIS_SCRIPTS_INCLUDED;
398
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  }
1
  <?php
2
 
3
+ class Ga_Helper
4
+ {
5
+
6
+ const ROLE_ID_PREFIX = "role-id-";
7
+
8
+ const GA_DEFAULT_WEB_ID = "UA-0000000-0";
9
+
10
+ const GA_STATISTICS_PAGE_URL = "admin.php?page=googleanalytics";
11
+
12
+ const GA_SETTINGS_PAGE_URL = "admin.php?page=googleanalytics/settings";
13
+
14
+ const DASHBOARD_PAGE_NAME = "dashboard";
15
+
16
+ const PHP_VERSION_REQUIRED = "5.2.17";
17
+
18
+ /**
19
+ * Init plugin actions.
20
+ *
21
+ */
22
+ public static function init()
23
+ {
24
+
25
+ // Displays errors related to required PHP version
26
+ if ( ! self::is_php_version_valid()) {
27
+ add_action('admin_notices', 'Ga_Admin::admin_notice_googleanalytics_php_version');
28
+
29
+ return false;
30
+ }
31
+
32
+ // Displays errors related to required WP version
33
+ if ( ! self::is_wp_version_valid()) {
34
+ add_action('admin_notices', 'Ga_Admin::admin_notice_googleanalytics_wp_version');
35
+
36
+ return false;
37
+ }
38
+
39
+ if ( ! is_admin()) {
40
+ Ga_Frontend::add_actions();
41
+ }
42
+
43
+ if (is_admin()) {
44
+ Ga_Admin::add_filters();
45
+ Ga_Admin::add_actions();
46
+ Ga_Admin::init_oauth();
47
+ Ga_Admin::handle_actions();
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Checks if current page is a WordPress dashboard.
53
+ * @return int
54
+ */
55
+ public static function is_plugin_page()
56
+ {
57
+ $site = get_current_screen();
58
+
59
+ return preg_match('/' . GA_NAME . '/', $site->base);
60
+ }
61
+
62
+ /**
63
+ * Checks if current page is a WordPress dashboard.
64
+ * @return number
65
+ */
66
+ public static function is_dashboard_page()
67
+ {
68
+ $site = get_current_screen();
69
+
70
+ return preg_match('/' . self::DASHBOARD_PAGE_NAME . '/', $site->base);
71
+ }
72
+
73
+ /**
74
+ * Check whether the plugin is configured.
75
+ *
76
+ * @param String $web_id
77
+ *
78
+ * @return boolean
79
+ */
80
+ public static function is_configured($web_id)
81
+ {
82
+ return $web_id !== self::GA_DEFAULT_WEB_ID;
83
+ }
84
+
85
+ /**
86
+ * Prepare an array of current site's user roles
87
+ *
88
+ * return array
89
+ */
90
+ public static function get_user_roles()
91
+ {
92
+ global $wp_roles;
93
+ if ( ! isset($wp_roles)) {
94
+ $wp_roles = new WP_Roles();
95
+ }
96
+
97
+ return $wp_roles->get_names();
98
+ }
99
+
100
+ /**
101
+ * Prepare a role ID.
102
+ *
103
+ * The role ID is derived from the role's name and will be used
104
+ * in its setting name in the additional settings.
105
+ *
106
+ * @param string $role_name Role name
107
+ *
108
+ * @return string
109
+ */
110
+ public static function prepare_role_id($role_name)
111
+ {
112
+ return self::ROLE_ID_PREFIX . strtolower(preg_replace('/[\W]/', '-', before_last_bar($role_name)));
113
+ }
114
+
115
+ /**
116
+ * Prepares role id.
117
+ *
118
+ * @param $v
119
+ * @param $k
120
+ */
121
+ public static function prepare_role(&$v, $k)
122
+ {
123
+ $v = self::prepare_role_id($v);
124
+ }
125
+
126
+ /**
127
+ * Checks whether user role is excluded from adding UA code.
128
+ *
129
+ * @return boolean
130
+ */
131
+ public static function can_add_ga_code()
132
+ {
133
+ $current_user = wp_get_current_user();
134
+ $user_roles = ! empty($current_user->roles) ? $current_user->roles : array();
135
+ $exclude_roles = json_decode(get_option(Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME), true);
136
+
137
+ array_walk($user_roles, 'Ga_Helper::prepare_role');
138
+
139
+ $return = true;
140
+ foreach ($user_roles as $role) {
141
+ if ( ! empty($exclude_roles[$role])) {
142
+ $return = false;
143
+ break;
144
+ }
145
+ }
146
+
147
+ return $return;
148
+ }
149
+
150
+ /**
151
+ * Adds ga dashboard widget HTML code for a WordPress
152
+ * Dashboard widget hook.
153
+ */
154
+ public static function add_ga_dashboard_widget()
155
+ {
156
+ echo self::get_ga_dashboard_widget();
157
+ }
158
+
159
+ /**
160
+ * Generates dashboard widget HTML code.
161
+ *
162
+ * @param string $date_range Google Analytics specific date range string.
163
+ * @param boolean $text_mode
164
+ * @param boolean $ajax
165
+ *
166
+ * @return null | string HTML dashboard widget code.
167
+ */
168
+ public static function get_ga_dashboard_widget($date_range = null, $text_mode = false, $ajax = false)
169
+ {
170
+ if (empty($date_range)) {
171
+ $date_range = '30daysAgo';
172
+ }
173
+
174
+ // Get chart and boxes data
175
+ $data = self::get_dashboard_widget_data($date_range);
176
+
177
+ if ($text_mode) {
178
+ return self::get_chart_page('ga_dashboard_widget' . ($ajax ? "_ajax" : ""), array(
179
+ 'chart' => $data['chart'],
180
+ 'boxes' => $data['boxes']
181
+ ));
182
+ } else {
183
+ echo self::get_chart_page('ga_dashboard_widget' . ($ajax ? "_ajax" : ""), array(
184
+ 'chart' => $data['chart'],
185
+ 'boxes' => $data['boxes'],
186
+ 'more_details_url' => admin_url(self::GA_STATISTICS_PAGE_URL)
187
+ ));
188
+ }
189
+
190
+ return null;
191
+ }
192
+
193
+ /**
194
+ * Generates JSON data string for AJAX calls.
195
+ *
196
+ * @param string $date_range
197
+ * @param string $metric
198
+ * @param boolean $text_mode
199
+ * @param boolean $ajax
200
+ *
201
+ * @return string|false Returns JSON data string
202
+ */
203
+ public static function get_ga_dashboard_widget_data_json(
204
+ $date_range = null,
205
+ $metric = null,
206
+ $text_mode = false,
207
+ $ajax = false
208
+ ) {
209
+ if (empty($date_range)) {
210
+ $date_range = '30daysAgo';
211
+ }
212
+
213
+ if (empty($metric)) {
214
+ $metric = 'pageviews';
215
+ }
216
+
217
+ $data = self::get_dashboard_widget_data($date_range, $metric);
218
+
219
+ return wp_json_encode($data);
220
+ }
221
+
222
+ /**
223
+ * Gets dashboard widget data.
224
+ *
225
+ * @param date_range
226
+ * @param metric
227
+ *
228
+ * @return array Return chart and boxes data
229
+ */
230
+ private static function get_dashboard_widget_data($date_range, $metric = null)
231
+ {
232
+ $selected = self::get_selected_account_data(true);
233
+
234
+ $query_params = Ga_Stats::get_query('main_chart', $selected['view_id'], $date_range, $metric);
235
+ $stats_data = Ga_Admin::api_client()->call('ga_api_data', array(
236
+
237
+ $query_params
238
+ ));
239
+
240
+ $boxes_query = Ga_Stats::get_query('dashboard_boxes', $selected['view_id'], $date_range);
241
+ $boxes_data = Ga_Admin::api_client()->call('ga_api_data', array(
242
+
243
+ $boxes_query
244
+ ));
245
+
246
+ $chart = ! empty($stats_data) ? Ga_Stats::get_dashboard_chart($stats_data->getData()) : array();
247
+ $boxes = ! empty($boxes_data) ? Ga_Stats::get_dashboard_boxes_data($boxes_data->getData()) : array();
248
+
249
+ return array(
250
+
251
+ 'chart' => $chart,
252
+ 'boxes' => $boxes
253
+ );
254
+ }
255
+
256
+ public static function is_account_selected()
257
+ {
258
+ return self::get_selected_account_data();
259
+ }
260
+
261
+ /**
262
+ * Returns HTML code of the chart page or a notice.
263
+ *
264
+ * @param chart
265
+ *
266
+ * @return string Returns HTML code
267
+ */
268
+ public static function get_chart_page($view, $params)
269
+ {
270
+
271
+ $message = sprintf(__('Statistics can only be seen after you authenticate with your Google account on the <a href="%s">Settings page</a>.'),
272
+ admin_url(self::GA_SETTINGS_PAGE_URL));
273
+
274
+ if (self::is_authorized() && ! self::is_code_manually_enabled()) {
275
+ if (self::is_account_selected()) {
276
+ if ($params) {
277
+ return Ga_View::load($view, $params, true);
278
+ } else {
279
+ return self::ga_oauth_notice(sprintf('Please configure your <a href="%s">Google Analytics settings</a>.',
280
+ admin_url(self::GA_SETTINGS_PAGE_URL)));
281
+ }
282
+ } else {
283
+ return self::ga_oauth_notice($message);
284
+ }
285
+ } else {
286
+ return self::ga_oauth_notice($message);
287
+ }
288
+ }
289
+
290
+ /**
291
+ * Checks whether users is authorized with Google.
292
+ *
293
+ * @return boolean
294
+ */
295
+ public static function is_authorized()
296
+ {
297
+ return Ga_Admin::api_client()->get_instance()->is_authorized();
298
+ }
299
+
300
+ /**
301
+ * Wrapper for WordPress method get_option
302
+ *
303
+ * @param string $name Option name
304
+ *
305
+ * @return NULL|mixed|boolean
306
+ */
307
+ public static function get_option($name)
308
+ {
309
+ $opt = get_option($name);
310
+
311
+ return ! empty($opt) ? $opt : null;
312
+ }
313
+
314
+ /**
315
+ * Wrapper for WordPress method update_option
316
+ *
317
+ * @param string $name
318
+ * @param mixed $value
319
+ *
320
+ * @return NULL|boolean
321
+ */
322
+ public static function update_option($name, $value)
323
+ {
324
+ $opt = update_option($name, $value);
325
+
326
+ return ! empty($opt) ? $opt : null;
327
+ }
328
+
329
+ /**
330
+ * Loads ga notice HTML code with given message included.
331
+ *
332
+ * @param string $message
333
+ * $param bool $cannot_activate Whether the plugin cannot be activated
334
+ *
335
+ * @return string
336
+ */
337
+ public static function ga_oauth_notice($message)
338
+ {
339
+ return Ga_View::load('ga_oauth_notice', array(
340
+ 'msg' => $message
341
+ ), true);
342
+ }
343
+
344
+ /**
345
+ * Displays notice following the WP style.
346
+ *
347
+ * @param $message
348
+ * @param string $type
349
+ *
350
+ * @return string
351
+ */
352
+ public static function ga_wp_notice($message, $type = '')
353
+ {
354
+ return Ga_View::load('ga_wp_notice', array(
355
+ 'type' => empty($type) ? Ga_Admin::NOTICE_WARNING : $type,
356
+ 'msg' => $message
357
+ ), true);
358
+ }
359
+
360
+ /**
361
+ * Gets data according to selected GA account.
362
+ *
363
+ * @param boolean $assoc
364
+ *
365
+ * @return mixed
366
+ */
367
+ public static function get_selected_account_data($assoc = false)
368
+ {
369
+ $data = json_decode(self::get_option(Ga_Admin::GA_SELECTED_ACCOUNT));
370
+ $data = ( ! empty($data) && count($data) == 3) ? $data : false;
371
+
372
+ if ($data) {
373
+ if ($assoc) {
374
+ return array(
375
+
376
+ 'account_id' => $data[0],
377
+ 'web_property_id' => $data[1],
378
+ 'view_id' => $data[2]
379
+ );
380
+ } else {
381
+ return $data;
382
+ }
383
+ }
384
+
385
+ return false;
386
+ }
387
+
388
+ /**
389
+ * Chekcs whether option for manually UA-code
390
+ * @return NULL|mixed|boolean
391
+ */
392
+ public static function is_code_manually_enabled()
393
+ {
394
+ return Ga_Helper::get_option(Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME);
395
+ }
396
+
397
+ /**
398
+ * Adds percent sign to the given text.
399
+ *
400
+ * @param $text
401
+ *
402
+ * @return string
403
+ */
404
+ public static function format_percent($text)
405
+ {
406
+ $text = self::add_plus($text);
407
+
408
+ return $text . '%';
409
+ }
410
+
411
+ /**
412
+ * Adds plus sign before number.
413
+ *
414
+ * @param $number
415
+ *
416
+ * @return string
417
+ */
418
+ public static function add_plus($number)
419
+ {
420
+ if ($number > 0) {
421
+ return '+' . $number;
422
+ }
423
+
424
+ return $number;
425
+ }
426
+
427
+ /**
428
+ * Check whether current user has administrator privileges.
429
+ *
430
+ * @return bool
431
+ */
432
+ public static function is_administrator()
433
+ {
434
+ if (current_user_can('administrator')) {
435
+ return true;
436
+ }
437
+
438
+ return false;
439
+ }
440
+
441
+ public static function is_wp_version_valid()
442
+ {
443
+ $wp_version = get_bloginfo('version');
444
+
445
+ return version_compare($wp_version, Ga_Admin::MIN_WP_VERSION, 'ge');
446
+ }
447
+
448
+ /**
449
+ * Check if terms are accepted
450
+ *
451
+ * @return bool
452
+ */
453
+ public static function are_terms_accepted()
454
+ {
455
+ return self::get_option(Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME);
456
+ }
457
+
458
+ /**
459
+ * Check if sharethis scripts enabled
460
+ *
461
+ * @return bool
462
+ */
463
+ public static function is_sharethis_included()
464
+ {
465
+ return GA_SHARETHIS_SCRIPTS_INCLUDED;
466
+ }
467
+
468
+ /**
469
+ * @return mixed
470
+ */
471
+ public static function is_php_version_valid()
472
+ {
473
+ $p = '#(\.0+)+($|-)#';
474
+ $ver1 = preg_replace($p, '', phpversion());
475
+ $ver2 = preg_replace($p, '', self::PHP_VERSION_REQUIRED);
476
+ $operator = 'ge';
477
+ $compare = isset($operator) ?
478
+ version_compare($ver1, $ver2, $operator) :
479
+ version_compare($ver1, $ver2);
480
+
481
+ return $compare;
482
+ }
483
+
484
+ public static function get_current_url()
485
+ {
486
+ return $_SERVER['REQUEST_URI'];
487
+ }
488
  }
class/Ga_Stats.php CHANGED
@@ -444,15 +444,15 @@ class Ga_Stats {
444
  /**
445
  * Get chart from response data
446
  *
447
- * @param array $data Analytics response data
448
  *
449
  * @return array chart data
450
  */
451
- public static function get_chart( $data ) {
452
  $chart_data = array();
453
- if ( ! empty( $data ) ) {
454
- $data = $data['reports'][0]['data'];
455
- $rows = $data['rows'];
456
  if ( ! empty( $rows ) ) {
457
  foreach ( $rows as $key => $row ) {
458
  if ( $key < 7 ) {
@@ -473,17 +473,16 @@ class Ga_Stats {
473
  /**
474
  * Get dasboard chart from response data
475
  *
476
- * @param array $data Analytics response data
477
  *
478
  * @return array dashboard chart data
479
  */
480
- public static function get_dashboard_chart( $data ) {
481
  $chart_data = array();
482
- if ( ! empty( $data ) ) {
483
- $data = $data['reports'][0]['data'];
484
- $rows = $data['rows'];
485
-
486
- if ( $rows ) {
487
  foreach ( $rows as $row ) {
488
  $chart_data[] = array(
489
  'day' => date( 'M j', strtotime( $row['dimensions'][0] ) ),
@@ -531,12 +530,12 @@ class Ga_Stats {
531
  $boxes_data['Users']['current'] = $total['values'][0];
532
  $boxes_data['Pageviews']['current'] = $total['values'][1];
533
  $boxes_data['PageviewsPerSession']['current'] = $total['values'][2];
534
- $boxes_data['BounceRate']['current'] = round( $total['values'][3], 2 ) . '%';
535
  } else {
536
  $boxes_data['Users']['previous'] = $total['values'][0];
537
  $boxes_data['Pageviews']['previous'] = $total['values'][1];
538
  $boxes_data['PageviewsPerSession']['previous'] = $total['values'][2];
539
- $boxes_data['BounceRate']['previous'] = round( $total['values'][3], 2 ) . '%';
540
  }
541
  }
542
 
@@ -558,6 +557,9 @@ class Ga_Stats {
558
  $boxes_data['Pageviews']['diff'] = ( $boxes_data['Pageviews']['previous'] > 0 ) ? round( ( $boxes_data['Pageviews']['current'] - $boxes_data['Pageviews']['previous'] ) / $boxes_data['Pageviews']['previous'] * 100, 2 ) : 100;
559
  $boxes_data['PageviewsPerSession']['diff'] = ( $boxes_data['PageviewsPerSession']['previous'] > 0 ) ? round( ( $boxes_data['PageviewsPerSession']['current'] - $boxes_data['PageviewsPerSession']['previous'] ) / $boxes_data['PageviewsPerSession']['previous'] * 100, 2 ) : 100;
560
  $boxes_data['BounceRate']['diff'] = ( $boxes_data['BounceRate']['previous'] > 0 ) ? round( ( $boxes_data['BounceRate']['current'] - $boxes_data['BounceRate']['previous'] ) / $boxes_data['BounceRate']['previous'] * 100, 2 ) : 100;
 
 
 
561
  $boxes_data['BounceRate']['diff'] = ( $boxes_data['BounceRate']['previous'] == 0 && $boxes_data['BounceRate']['current'] == 0) ? 0 : $boxes_data['BounceRate']['diff'];
562
  $boxes_data['Users']['label'] = 'Users';
563
  $boxes_data['Pageviews']['label'] = 'Pageviews';
@@ -618,8 +620,8 @@ class Ga_Stats {
618
  $report_data = self::get_report_data( $report );
619
  $rows = self::get_rows( $report_data );
620
  $totals = self::get_totals( $report_data );
 
621
  if ( ! empty( $totals ) ) {
622
- $totalCount = array();
623
  foreach ( $totals as $key => $total ) {
624
  $totalCount = $total['values'][0];
625
  }
444
  /**
445
  * Get chart from response data
446
  *
447
+ * @param array $response_data Analytics response data
448
  *
449
  * @return array chart data
450
  */
451
+ public static function get_chart( $response_data ) {
452
  $chart_data = array();
453
+ if ( ! empty( $response_data ) ) {
454
+ $data = ( !empty( $response_data['reports'] ) && !empty( $response_data['reports'][0] ) && !empty( $response_data['reports'][0]['data'] ) ) ? $response_data['reports'][0]['data'] : array();
455
+ $rows = ( !empty( $data['rows'] ) ) ? $data['rows'] : array();
456
  if ( ! empty( $rows ) ) {
457
  foreach ( $rows as $key => $row ) {
458
  if ( $key < 7 ) {
473
  /**
474
  * Get dasboard chart from response data
475
  *
476
+ * @param array $response_data Analytics response data
477
  *
478
  * @return array dashboard chart data
479
  */
480
+ public static function get_dashboard_chart( $response_data ) {
481
  $chart_data = array();
482
+ if ( ! empty( $response_data ) ) {
483
+ $data = ( !empty( $response_data['reports'] ) && !empty( $response_data['reports'][0] ) && !empty( $response_data['reports'][0]['data'] ) ) ? $response_data['reports'][0]['data'] : array();
484
+ $rows = ( !empty( $data['rows'] ) ) ? $data['rows'] : array();
485
+ if ( ! empty( $rows ) ) {
 
486
  foreach ( $rows as $row ) {
487
  $chart_data[] = array(
488
  'day' => date( 'M j', strtotime( $row['dimensions'][0] ) ),
530
  $boxes_data['Users']['current'] = $total['values'][0];
531
  $boxes_data['Pageviews']['current'] = $total['values'][1];
532
  $boxes_data['PageviewsPerSession']['current'] = $total['values'][2];
533
+ $boxes_data['BounceRate']['current'] = round( $total['values'][3], 2 );
534
  } else {
535
  $boxes_data['Users']['previous'] = $total['values'][0];
536
  $boxes_data['Pageviews']['previous'] = $total['values'][1];
537
  $boxes_data['PageviewsPerSession']['previous'] = $total['values'][2];
538
+ $boxes_data['BounceRate']['previous'] = round( $total['values'][3], 2 );
539
  }
540
  }
541
 
557
  $boxes_data['Pageviews']['diff'] = ( $boxes_data['Pageviews']['previous'] > 0 ) ? round( ( $boxes_data['Pageviews']['current'] - $boxes_data['Pageviews']['previous'] ) / $boxes_data['Pageviews']['previous'] * 100, 2 ) : 100;
558
  $boxes_data['PageviewsPerSession']['diff'] = ( $boxes_data['PageviewsPerSession']['previous'] > 0 ) ? round( ( $boxes_data['PageviewsPerSession']['current'] - $boxes_data['PageviewsPerSession']['previous'] ) / $boxes_data['PageviewsPerSession']['previous'] * 100, 2 ) : 100;
559
  $boxes_data['BounceRate']['diff'] = ( $boxes_data['BounceRate']['previous'] > 0 ) ? round( ( $boxes_data['BounceRate']['current'] - $boxes_data['BounceRate']['previous'] ) / $boxes_data['BounceRate']['previous'] * 100, 2 ) : 100;
560
+ $boxes_data['Users']['diff'] = ( $boxes_data['Users']['previous'] == 0 && $boxes_data['Users']['current'] == 0) ? 0 : $boxes_data['Users']['diff'];
561
+ $boxes_data['Pageviews']['diff'] = ( $boxes_data['Pageviews']['previous'] == 0 && $boxes_data['Pageviews']['current'] == 0) ? 0 : $boxes_data['Pageviews']['diff'];
562
+ $boxes_data['PageviewsPerSession']['diff'] = ( $boxes_data['PageviewsPerSession']['previous'] == 0 && $boxes_data['PageviewsPerSession']['current'] == 0) ? 0 : $boxes_data['PageviewsPerSession']['diff'];
563
  $boxes_data['BounceRate']['diff'] = ( $boxes_data['BounceRate']['previous'] == 0 && $boxes_data['BounceRate']['current'] == 0) ? 0 : $boxes_data['BounceRate']['diff'];
564
  $boxes_data['Users']['label'] = 'Users';
565
  $boxes_data['Pageviews']['label'] = 'Pageviews';
620
  $report_data = self::get_report_data( $report );
621
  $rows = self::get_rows( $report_data );
622
  $totals = self::get_totals( $report_data );
623
+ $totalCount = array();
624
  if ( ! empty( $totals ) ) {
 
625
  foreach ( $totals as $key => $total ) {
626
  $totalCount = $total['values'][0];
627
  }
googleanalytics.php CHANGED
@@ -4,7 +4,7 @@
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.0.1
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
@@ -24,10 +24,10 @@ if ( ! defined( 'GA_NAME' ) ) {
24
  define( 'GA_NAME', 'googleanalytics' );
25
  }
26
  if ( ! defined( 'GA_PLUGIN_DIR' ) ) {
27
- define( 'GA_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins/' . GA_NAME );
28
  }
29
  if ( ! defined( 'GA_PLUGIN_URL' ) ) {
30
- define( 'GA_PLUGIN_URL', WP_CONTENT_URL . '/plugins/' . GA_NAME );
31
  }
32
  if ( ! defined( 'GA_MAIN_FILE_PATH' ) ) {
33
  define( 'GA_MAIN_FILE_PATH', __FILE__ );
@@ -35,7 +35,7 @@ if ( ! defined( 'GA_MAIN_FILE_PATH' ) ) {
35
  if ( ! defined( 'GA_SHARETHIS_SCRIPTS_INCLUDED' ) ) {
36
  define( 'GA_SHARETHIS_SCRIPTS_INCLUDED', 0 );
37
  }
38
- define( 'GOOGLEANALYTICS_VERSION', '2.0.1' );
39
  include_once GA_PLUGIN_DIR . '/overwrite/ga_overwrite.php';
40
  include_once GA_PLUGIN_DIR . '/class/Ga_Autoloader.php';
41
  Ga_Autoloader::register();
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.0.2
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
24
  define( 'GA_NAME', 'googleanalytics' );
25
  }
26
  if ( ! defined( 'GA_PLUGIN_DIR' ) ) {
27
+ define( 'GA_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . GA_NAME );
28
  }
29
  if ( ! defined( 'GA_PLUGIN_URL' ) ) {
30
+ define( 'GA_PLUGIN_URL', WP_PLUGIN_URL . '/' . GA_NAME );
31
  }
32
  if ( ! defined( 'GA_MAIN_FILE_PATH' ) ) {
33
  define( 'GA_MAIN_FILE_PATH', __FILE__ );
35
  if ( ! defined( 'GA_SHARETHIS_SCRIPTS_INCLUDED' ) ) {
36
  define( 'GA_SHARETHIS_SCRIPTS_INCLUDED', 0 );
37
  }
38
+ define( 'GOOGLEANALYTICS_VERSION', '2.0.2' );
39
  include_once GA_PLUGIN_DIR . '/overwrite/ga_overwrite.php';
40
  include_once GA_PLUGIN_DIR . '/class/Ga_Autoloader.php';
41
  Ga_Autoloader::register();
lib/Ga_Lib_Api_Request.php CHANGED
@@ -1,112 +1,131 @@
1
  <?php
2
 
3
- class Ga_Lib_Api_Request {
4
-
5
- const HEADER_CONTENT_TYPE = "application/x-www-form-urlencoded";
6
-
7
- const HEADER_CONTENT_TYPE_JSON = "Content-type: application/json";
8
-
9
- const HEADER_ACCEPT = "Accept: application/json, text/javascript, */*; q=0.01";
10
-
11
- const TIMEOUT = 30;
12
-
13
- const USER_AGENT = 'googleanalytics-wordpress-plugin';
14
-
15
- private $headers = array();
16
-
17
- function __construct() {
18
- }
19
-
20
- /**
21
- * Returns API client instance.
22
- *
23
- * @return Ga_Lib_Api_Request|null
24
- */
25
- public static function get_instance() {
26
- static $instance = null;
27
- if ( $instance === null ) {
28
- $instance = new Ga_Lib_Api_Request();
29
- }
30
-
31
- return $instance;
32
- }
33
-
34
- /**
35
- * Sets request headers.
36
- *
37
- * @param $headers
38
- */
39
- public function set_request_headers( $headers ) {
40
- if ( is_array( $headers ) ) {
41
- $this->headers = array_merge( $this->headers, $headers );
42
- } else {
43
- $this->headers[] = $headers;
44
- }
45
- }
46
-
47
- /**
48
- * Perform HTTP request.
49
- *
50
- * @param string $url URL address
51
- * @param string $rawPostBody
52
- *
53
- * @return string Response
54
- * @throws Exception
55
- */
56
- public function make_request( $url, $rawPostBody = null, $json = false ) {
57
- if ( ! function_exists( 'curl_init' ) ) {
58
- throw new Exception( 'cURL functions are not available' );
59
- }
60
-
61
- // Set default headers
62
- $this->set_request_headers( array(
63
- ( $json ? self::HEADER_CONTENT_TYPE_JSON : self::HEADER_CONTENT_TYPE ),
64
- self::HEADER_ACCEPT,
65
- 'Expect: 200-OK'
66
- ) );
67
-
68
- $ch = curl_init( $url );
69
- $headers = $this->headers;
70
- curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
71
-
72
- $curl_timeout = self::TIMEOUT;
73
- $php_execution_time = ini_get( 'max_execution_time' );
74
- if ( ! empty( $php_execution_time ) && is_numeric( $php_execution_time ) ) {
75
- if ( $php_execution_time < 36 && $php_execution_time > 9 ) {
76
- $curl_timeout = $php_execution_time - 5;
77
- } elseif ( $php_execution_time < 10 ) {
78
- $curl_timeout = 5;
79
- }
80
- }
81
-
82
- curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout );
83
- curl_setopt( $ch, CURLOPT_TIMEOUT, $curl_timeout );
84
- curl_setopt( $ch, CURLOPT_HEADER, true );
85
- curl_setopt( $ch, CURLINFO_HEADER_OUT, true );
86
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
87
- curl_setopt( $ch, CURLOPT_USERAGENT, self::USER_AGENT );
88
-
89
- // POST body
90
- if ( ! empty( $rawPostBody ) ) {
91
- curl_setopt( $ch, CURLOPT_POST, true );
92
- curl_setopt( $ch, CURLOPT_POSTFIELDS, $rawPostBody );
93
- }
94
-
95
- // Execute request
96
- $response = curl_exec( $ch );
97
-
98
- if ( $error = curl_error( $ch ) ) {
99
- $errno = curl_errno( $ch );
100
- curl_close( $ch );
101
- throw new Exception( $error . ' (' . $errno . ')' );
102
- } else {
103
-
104
- $headerSize = curl_getinfo( $ch, CURLINFO_HEADER_SIZE );
105
- $header = substr( $response, 0, $headerSize );
106
- $body = substr( $response, $headerSize, strlen( $response ) );
107
- curl_close( $ch );
108
-
109
- return array( $header, $body );
110
- }
111
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  }
1
  <?php
2
 
3
+ class Ga_Lib_Api_Request
4
+ {
5
+
6
+ const HEADER_CONTENT_TYPE = "application/x-www-form-urlencoded";
7
+
8
+ const HEADER_CONTENT_TYPE_JSON = "Content-type: application/json";
9
+
10
+ const HEADER_ACCEPT = "Accept: application/json, text/javascript, */*; q=0.01";
11
+
12
+ const TIMEOUT = 30;
13
+
14
+ const USER_AGENT = 'googleanalytics-wordpress-plugin';
15
+
16
+ private $headers = array();
17
+
18
+ function __construct()
19
+ {
20
+ }
21
+
22
+ /**
23
+ * Returns API client instance.
24
+ *
25
+ * @return Ga_Lib_Api_Request|null
26
+ */
27
+ public static function get_instance()
28
+ {
29
+ static $instance = null;
30
+ if ($instance === null) {
31
+ $instance = new Ga_Lib_Api_Request();
32
+ }
33
+
34
+ return $instance;
35
+ }
36
+
37
+ /**
38
+ * Sets request headers.
39
+ *
40
+ * @param $headers
41
+ */
42
+ public function set_request_headers($headers)
43
+ {
44
+ if (is_array($headers)) {
45
+ $this->headers = array_merge($this->headers, $headers);
46
+ } else {
47
+ $this->headers[] = $headers;
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Perform HTTP request.
53
+ *
54
+ * @param string $url URL address
55
+ * @param string $rawPostBody
56
+ *
57
+ * @return string Response
58
+ * @throws Exception
59
+ */
60
+ public function make_request($url, $rawPostBody = null, $json = false)
61
+ {
62
+ if ( ! function_exists('curl_init')) {
63
+ throw new Exception('cURL functions are not available');
64
+ }
65
+
66
+ // Set default headers
67
+ $this->set_request_headers(array(
68
+ ($json ? self::HEADER_CONTENT_TYPE_JSON : self::HEADER_CONTENT_TYPE),
69
+ self::HEADER_ACCEPT
70
+ ));
71
+
72
+ $ch = curl_init($url);
73
+ $headers = $this->headers;
74
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
75
+
76
+ $curl_timeout = self::TIMEOUT;
77
+ $php_execution_time = ini_get('max_execution_time');
78
+ if ( ! empty($php_execution_time) && is_numeric($php_execution_time)) {
79
+ if ($php_execution_time < 36 && $php_execution_time > 9) {
80
+ $curl_timeout = $php_execution_time - 5;
81
+ } elseif ($php_execution_time < 10) {
82
+ $curl_timeout = 5;
83
+ }
84
+ }
85
+
86
+ // Set the proxy configuration. The user can provide this in wp-config.php
87
+ if (defined('WP_PROXY_HOST')) {
88
+ curl_setopt($ch, CURLOPT_PROXY, WP_PROXY_HOST);
89
+ }
90
+ if (defined('WP_PROXY_PORT')) {
91
+ curl_setopt($ch, CURLOPT_PROXYPORT, WP_PROXY_PORT);
92
+ }
93
+ if (defined('WP_PROXY_USERNAME')) {
94
+ $auth = WP_PROXY_USERNAME;
95
+ if (defined('WP_PROXY_PASSWORD')) {
96
+ $auth .= ':' . WP_PROXY_PASSWORD;
97
+ }
98
+ curl_setopt($ch, CURLOPT_PROXYUSERPWD, $auth);
99
+ }
100
+
101
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $curl_timeout);
102
+ curl_setopt($ch, CURLOPT_TIMEOUT, $curl_timeout);
103
+ curl_setopt($ch, CURLOPT_HEADER, true);
104
+ curl_setopt($ch, CURLINFO_HEADER_OUT, true);
105
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
106
+ curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
107
+
108
+
109
+ // POST body
110
+ if ( ! empty($rawPostBody)) {
111
+ curl_setopt($ch, CURLOPT_POST, true);
112
+ curl_setopt($ch, CURLOPT_POSTFIELDS, ($json ? $rawPostBody : http_build_query($rawPostBody)));
113
+ }
114
+
115
+ // Execute request
116
+ $response = curl_exec($ch);
117
+
118
+ if ($error = curl_error($ch)) {
119
+ $errno = curl_errno($ch);
120
+ curl_close($ch);
121
+ throw new Exception($error . ' (' . $errno . ')');
122
+ } else {
123
+ $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
124
+ $header = substr($response, 0, $headerSize);
125
+ $body = substr($response, $headerSize, strlen($response));
126
+ curl_close($ch);
127
+
128
+ return array($header, $body);
129
+ }
130
+ }
131
  }
overwrite/ga_overwrite.php CHANGED
File without changes
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: ShareThis
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.7.0
6
- Stable tag: trunk
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
@@ -46,16 +46,16 @@ By downloading and installing this plugin you are agreeing to the <a href="http:
46
  5. View different time ranges and key metrics in the Wordpress Google Analytics widget
47
 
48
  == Changelog ==
49
-
 
 
 
 
 
 
 
50
  = 2.0.1 =
51
- * Completely redesigned with new features!
52
- * Updated with the latest Google Analytics code
53
- * No need to find your GA property ID and copy it over, just sign in with Google and choose your site
54
- * See analytics right inside the plugin, the past 7 days vs your previous 7 days
55
- * Shows pageviews, users, pages per session and bounce rate + top 5 traffic referrals
56
- * Wordpress Dashboard widget for 7, 30 or 90 days graph and top site usage stats
57
- * Disable tracking for logged in users like admins or editors for more reliable analytics
58
- * Support for the old PHP versions
59
 
60
  = 2.0.0 =
61
  * Completely redesigned with new features!
2
  Contributors: ShareThis
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.7
6
+ Stable tag: 2.0.2
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
46
  5. View different time ranges and key metrics in the Wordpress Google Analytics widget
47
 
48
  == Changelog ==
49
+
50
+ = 2.0.2 =
51
+ * Fixed issues related to older versions of PHP
52
+ * Fixed terms of service notice
53
+ * Added better support for HTTP proxy, thanks @usrlocaldick for the suggestion
54
+ * Added better support when WP_PLUGIN_DIR are already set, thanks @heiglandreas for the tip
55
+ * Added support for PHP version 5.2.17
56
+
57
  = 2.0.1 =
58
+ * Fix for old versions of PHP
 
 
 
 
 
 
 
59
 
60
  = 2.0.0 =
61
  * Completely redesigned with new features!
screenshot-3.png CHANGED
File without changes
screenshot-4.png CHANGED
File without changes
screenshot-5.png CHANGED
File without changes
view/ga_oauth_notice.php CHANGED
@@ -1,5 +1,3 @@
1
- <div class="wrap ga-notice">
2
- <div class="alert alert-warning">
3
- <?php echo $msg; ?>
4
- </div>
5
- </div>
1
+ <div class="alert alert-warning">
2
+ <?php echo $msg; ?>
3
+ </div>
 
 
view/ga_wp_notice.php ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ <div class="notice notice-<?php echo $type; ?>">
2
+ <?php echo $msg; ?>
3
+ </div>
view/page.php CHANGED
@@ -4,12 +4,12 @@
4
  <div class="modal-header">
5
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
6
  aria-hidden="true">&times;</span></button>
7
- <h4 class="modal-title"><?php _e( 'Please paste the access code obtained from Google below:' ) ?></h4>
8
  </div>
9
  <div class="modal-body">
10
- <label for="ga_access_code"><strong><?php _e( 'Access Code' ); ?></strong>:</label>
11
  &nbsp;<input id="ga_access_code_tmp" type="text" style="width: 350px"
12
- placeholder="<?php _e( 'Paste your access code here' ) ?>"/>
13
  <div class="ga-loader-wrapper">
14
  <div class="ga-loader"></div>
15
  </div>
@@ -18,135 +18,142 @@
18
  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
19
  <button type="button" class="btn btn-primary"
20
  id="ga_save_access_code"
21
- onclick="ga_popup.saveAccessCode(event)"><?php _e( 'Save Changes' ); ?></button>
22
  </div>
23
  </div><!-- /.modal-content -->
24
  </div><!-- /.modal-dialog -->
25
  </div><!-- /.modal -->
26
 
27
  <div class="wrap ga-wrap">
28
- <h2>Google Analytics - <?php _e( 'Settings' ); ?></h2>
29
  <div class="ga_container">
 
 
 
30
  <form id="ga_form" method="post" action="options.php">
31
- <?php settings_fields( 'googleanalytics' ); ?>
32
  <input id="ga_access_code" type="hidden"
33
- name="<?php echo esc_attr( Ga_Admin::GA_OAUTH_AUTH_CODE_OPTION_NAME ); ?>" value=""/>
34
- <table class="form-table">
35
- <tr valign="top">
36
- <?php if ( ! empty( $data['popup_url'] ) ): ?>
37
- <th scope="row">
38
- <label <?php echo ( ! Ga_Helper::are_terms_accepted() ) ? 'class="label-grey ga-tooltip"' : '' ?>><?php echo _e( 'Google Profile' ) ?>
39
- :
40
- <span class="ga-tooltiptext ga-tt-abs"><?php _e( 'Please accept the terms to use this feature' ); ?></span>
41
- </label>
42
- </th>
43
- <td <?php echo ( ! Ga_Helper::are_terms_accepted() ) ? 'class="ga-tooltip"' : ''; ?>>
44
- <button id="ga_authorize_with_google_button" class="btn btn-primary"
45
- <?php if ( Ga_Helper::are_terms_accepted() ) : ?>
46
- onclick="ga_popup.authorize(event, '<?php echo esc_attr( $data['popup_url'] ); ?>')"
47
- <?php endif; ?>
48
- <?php echo( ( esc_attr( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] ) || ! Ga_Helper::are_terms_accepted() ) ? 'disabled="disabled"' : '' ); ?>
49
- ><?php _e( 'Authenticate
50
- with Google' ) ?>
51
- </button>
52
- <span class="ga-tooltiptext"><?php _e( 'Please accept the terms to use this feature' ); ?></span>
53
- <?php if ( ! empty( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] ) ): ?>
54
- <div class="ga_warning">
55
- <strong><?php _e( 'Notice' ) ?></strong>:&nbsp;<?php _e( 'Please uncheck the "Manually enter Tracking ID" option to authenticate and view statistics.' ); ?>
56
- </div>
57
- <?php endif; ?>
58
- </td>
59
- <?php endif; ?>
60
 
61
- <?php if ( ! empty( $data['ga_accounts_selector'] ) ): ?>
62
- <th scope="row"><?php echo _e( 'Google Analytics Account' ) ?>:</th>
63
- <td><?php echo $data['ga_accounts_selector']; ?></td>
64
- <?php endif; ?>
65
 
66
- </tr>
67
 
68
- <tr valign="top">
69
 
70
- <th scope="row">
71
- <div class="checkbox">
72
- <label class="ga_checkbox_label <?php echo ( ! Ga_Helper::are_terms_accepted() ) ? 'label-grey ga-tooltip' : '' ?>"
73
- for="ga_enter_code_manually"> <input
74
- <?php if ( Ga_Helper::are_terms_accepted() ) : ?>
75
- onclick="ga_events.click( this, ga_events.codeManuallyCallback( <?php echo Ga_Helper::are_terms_accepted() ? 1 : 0; ?> ) )"
76
- <?php endif; ?>
77
- type="checkbox"
78
- <?php echo ( ! Ga_Helper::are_terms_accepted() ) ? 'disabled="disabled"' : ''; ?>
79
- name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ); ?>"
80
- id="ga_enter_code_manually"
81
- value="1"
82
- <?php echo( ( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] || ! Ga_Helper::are_terms_accepted() ) ? 'checked="checked"' : '' ); ?>/>&nbsp;
83
- <?php _e( 'Manually enter Tracking ID' ) ?>
84
- <span class="ga-tooltiptext ga-tt-abs"><?php _e( 'Please accept the terms to use this feature' ); ?></span>
85
- </label>
86
- </div>
87
- </th>
88
- <td></td>
89
- </tr>
90
- <tr valign="top"
91
- id="ga_manually_wrapper" <?php echo( ( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] || ! Ga_Helper::are_terms_accepted() ) ? '' : 'style="display: none"' ); ?> >
 
 
 
 
 
92
 
93
- <th scope="row"><?php _e( 'Tracking ID' ) ?>:</th>
94
- <td>
95
- <input type="text"
96
- name="<?php echo esc_attr( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ); ?>"
97
- value="<?php echo esc_attr( $data[ Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] ); ?>"
98
- id="ga_manually_input"/>&nbsp;
99
- <div class="ga_warning">
100
- <strong><?php _e( 'Warning' ); ?></strong>:&nbsp;<?php _e( 'If you enter your Tracking ID manually, Analytics statistics will not be shown.' ); ?>
101
- <br>
102
- <?php _e( 'We strongly recommend to authenticate with Google using the button above.' ); ?>
103
- </div>
104
- </td>
105
 
106
- </tr>
107
 
108
- <tr valign="top">
109
- <th scope="row">
110
- <label <?php echo ( ! Ga_Helper::are_terms_accepted() ) ? 'class="label-grey ga-tooltip"' : '' ?>><?php _e( 'Exclude Tracking for Roles' ) ?>
111
- :
112
- <span class="ga-tooltiptext ga-tt-abs"><?php _e( 'Please accept the terms to use this feature' ); ?></span>
113
- </label>
114
- </th>
115
- <td>
116
 
117
 
118
- <?php
119
- if ( ! empty( $data['roles'] ) ) {
120
- $roles = $data['roles'];
121
- foreach ( $roles as $role ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
- ?>
124
- <div class="checkbox">
125
- <label class="ga_checkbox_label <?php echo ( ! Ga_Helper::are_terms_accepted() ) ? 'label-grey ga-tooltip' : ''; ?>"
126
- for="checkbox_<?php echo $role['id']; ?>">
127
- <input id="checkbox_<?php echo $role['id']; ?>" type="checkbox"
128
- <?php echo ( ! Ga_Helper::are_terms_accepted() ) ? 'disabled="disabled"' : ''; ?>
129
- name="<?php echo esc_attr( Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME . "[" . $role['id'] . "]" ); ?>"
130
- id="<?php echo esc_attr( $role['id'] ); ?>"
131
- <?php echo esc_attr( ( $role['checked'] ? 'checked="checked"' : '' ) ); ?> />&nbsp;
132
- <?php echo esc_html( $role['name'] ); ?>
133
- <span class="ga-tooltiptext"><?php _e( 'Please accept the terms to use this feature' ); ?></span>
134
- </label>
135
- </div>
136
- <?php
137
- }
138
- }
139
- ?>
140
 
141
- </td>
142
- </tr>
143
 
144
- </table>
145
-
146
- <p class="submit">
147
- <input type="submit" class="button-primary"
148
- value="<?php _e( 'Save Changes' ) ?>"/>
149
- </p>
150
  </form>
151
  </div>
152
  </div>
4
  <div class="modal-header">
5
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
6
  aria-hidden="true">&times;</span></button>
7
+ <h4 class="modal-title"><?php _e('Please paste the access code obtained from Google below:') ?></h4>
8
  </div>
9
  <div class="modal-body">
10
+ <label for="ga_access_code"><strong><?php _e('Access Code'); ?></strong>:</label>
11
  &nbsp;<input id="ga_access_code_tmp" type="text" style="width: 350px"
12
+ placeholder="<?php _e('Paste your access code here') ?>"/>
13
  <div class="ga-loader-wrapper">
14
  <div class="ga-loader"></div>
15
  </div>
18
  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
19
  <button type="button" class="btn btn-primary"
20
  id="ga_save_access_code"
21
+ onclick="ga_popup.saveAccessCode( event )"><?php _e('Save Changes'); ?></button>
22
  </div>
23
  </div><!-- /.modal-content -->
24
  </div><!-- /.modal-dialog -->
25
  </div><!-- /.modal -->
26
 
27
  <div class="wrap ga-wrap">
28
+ <h2>Google Analytics - <?php _e('Settings'); ?></h2>
29
  <div class="ga_container">
30
+ <?php if ( ! empty($data['error_message'])) : ?>
31
+ <?php echo $data['error_message']; ?>
32
+ <?php endif; ?>
33
  <form id="ga_form" method="post" action="options.php">
34
+ <?php settings_fields('googleanalytics'); ?>
35
  <input id="ga_access_code" type="hidden"
36
+ name="<?php echo esc_attr(Ga_Admin::GA_OAUTH_AUTH_CODE_OPTION_NAME); ?>" value=""/>
37
+ <table class="form-table">
38
+ <tr valign="top">
39
+ <?php if ( ! empty($data['popup_url'])): ?>
40
+ <th scope="row">
41
+ <label <?php echo ( ! Ga_Helper::are_terms_accepted()) ? 'class="label-grey ga-tooltip"' : '' ?>><?php echo _e('Google Profile') ?>
42
+ :
43
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e('Please accept the terms to use this feature'); ?></span>
44
+ </label>
45
+ </th>
46
+ <td <?php echo ( ! Ga_Helper::are_terms_accepted()) ? 'class="ga-tooltip"' : ''; ?>>
47
+ <button id="ga_authorize_with_google_button" class="btn btn-primary"
48
+ <?php if (Ga_Helper::are_terms_accepted()) : ?>
49
+ onclick="ga_popup.authorize( event, '<?php echo esc_attr($data['popup_url']); ?>' )"
50
+ <?php endif; ?>
51
+ <?php echo((esc_attr($data[Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME]) || ! Ga_Helper::are_terms_accepted()) ? 'disabled="disabled"' : ''); ?>
52
+ ><?php _e('Authenticate
53
+ with Google') ?>
54
+ </button>
55
+ <span class="ga-tooltiptext"><?php _e('Please accept the terms to use this feature'); ?></span>
56
+ <?php if ( ! empty($data[Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME])): ?>
57
+ <div class="ga_warning">
58
+ <strong><?php _e('Notice') ?></strong>:&nbsp;<?php _e('Please uncheck the "Manually enter Tracking ID" option to authenticate and view statistics.'); ?>
59
+ </div>
60
+ <?php endif; ?>
61
+ </td>
62
+ <?php endif; ?>
63
 
64
+ <?php if ( ! empty($data['ga_accounts_selector'])): ?>
65
+ <th scope="row"><?php echo _e('Google Analytics Account') ?>:</th>
66
+ <td><?php echo $data['ga_accounts_selector']; ?></td>
67
+ <?php endif; ?>
68
 
69
+ </tr>
70
 
71
+ <tr valign="top">
72
 
73
+ <th scope="row">
74
+ <div class="checkbox">
75
+ <label class="ga_checkbox_label <?php echo ( ! Ga_Helper::are_terms_accepted()) ? 'label-grey ga-tooltip' : '' ?>"
76
+ for="ga_enter_code_manually"> <input
77
+ <?php if (Ga_Helper::are_terms_accepted()) : ?>
78
+ onclick="ga_events.click( this, ga_events.codeManuallyCallback( <?php echo Ga_Helper::are_terms_accepted() ? 1 : 0; ?> ) )"
79
+ <?php endif; ?>
80
+ type="checkbox"
81
+ <?php echo ( ! Ga_Helper::are_terms_accepted()) ? 'disabled="disabled"' : ''; ?>
82
+ name="<?php echo esc_attr(Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME); ?>"
83
+ id="ga_enter_code_manually"
84
+ value="1"
85
+ <?php echo(($data[Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME] || ! Ga_Helper::are_terms_accepted()) ? 'checked="checked"' : ''); ?>/>&nbsp;
86
+ <?php _e('Manually enter Tracking ID') ?>
87
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e('Please accept the terms to use this feature'); ?></span>
88
+ </label>
89
+ <?php if ( ! Ga_Helper::are_terms_accepted()) : ?>
90
+ <input id="ga_access_code" type="hidden"
91
+ name="<?php echo esc_attr(Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME); ?>"
92
+ value="1"/>
93
+ <?php endif; ?>
94
+ </div>
95
+ </th>
96
+ <td></td>
97
+ </tr>
98
+ <tr valign="top"
99
+ id="ga_manually_wrapper" <?php echo(($data[Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME] || ! Ga_Helper::are_terms_accepted()) ? '' : 'style="display: none"'); ?> >
100
 
101
+ <th scope="row"><?php _e('Tracking ID') ?>:</th>
102
+ <td>
103
+ <input type="text"
104
+ name="<?php echo esc_attr(Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME); ?>"
105
+ value="<?php echo esc_attr($data[Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME]); ?>"
106
+ id="ga_manually_input"/>&nbsp;
107
+ <div class="ga_warning">
108
+ <strong><?php _e('Warning'); ?></strong>:&nbsp;<?php _e('If you enter your Tracking ID manually, Analytics statistics will not be shown.'); ?>
109
+ <br>
110
+ <?php _e('We strongly recommend to authenticate with Google using the button above.'); ?>
111
+ </div>
112
+ </td>
113
 
114
+ </tr>
115
 
116
+ <tr valign="top">
117
+ <th scope="row">
118
+ <label <?php echo ( ! Ga_Helper::are_terms_accepted()) ? 'class="label-grey ga-tooltip"' : '' ?>><?php _e('Exclude Tracking for Roles') ?>
119
+ :
120
+ <span class="ga-tooltiptext ga-tt-abs"><?php _e('Please accept the terms to use this feature'); ?></span>
121
+ </label>
122
+ </th>
123
+ <td>
124
 
125
 
126
+ <?php
127
+ if ( ! empty($data['roles'])) {
128
+ $roles = $data['roles'];
129
+ foreach ($roles as $role) {
130
+ ?>
131
+ <div class="checkbox">
132
+ <label class="ga_checkbox_label <?php echo ( ! Ga_Helper::are_terms_accepted()) ? 'label-grey ga-tooltip' : ''; ?>"
133
+ for="checkbox_<?php echo $role['id']; ?>">
134
+ <input id="checkbox_<?php echo $role['id']; ?>" type="checkbox"
135
+ <?php echo ( ! Ga_Helper::are_terms_accepted()) ? 'disabled="disabled"' : ''; ?>
136
+ name="<?php echo esc_attr(Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME . "[" . $role['id'] . "]"); ?>"
137
+ id="<?php echo esc_attr($role['id']); ?>"
138
+ <?php echo esc_attr(($role['checked'] ? 'checked="checked"' : '')); ?> />&nbsp;
139
+ <?php echo esc_html($role['name']); ?>
140
+ <span class="ga-tooltiptext"><?php _e('Please accept the terms to use this feature'); ?></span>
141
+ </label>
142
+ </div>
143
+ <?php
144
+ }
145
+ }
146
+ ?>
147
 
148
+ </td>
149
+ </tr>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
+ </table>
 
152
 
153
+ <p class="submit">
154
+ <input type="submit" class="button-primary"
155
+ value="<?php _e('Save Changes') ?>"/>
156
+ </p>
 
 
157
  </form>
158
  </div>
159
  </div>
view/statistics.php CHANGED
File without changes