Google Analytics - Version 2.3.7

Version Description

  • Fix property creation structure.
  • Remove terms blocker.
Download this release

Release Info

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

Code changes from version 2.3.6 to 2.3.7

class/Ga_Admin.php CHANGED
@@ -1,759 +1,781 @@
1
- <?php
2
-
3
- class Ga_Admin {
4
-
5
- const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
6
- const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
7
- const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
8
- const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
9
- const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
10
- const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
11
- const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
12
- const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
13
- const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
14
- const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
15
- const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
16
- const GA_SHARETHIS_PROPERTY_ID = 'googleanalytics_sherethis_property_id';
17
- const GA_SHARETHIS_PROPERTY_SECRET = 'googleanalytics_sherethis_property_secret';
18
- const GA_SHARETHIS_VERIFICATION_RESULT = 'googleanalytics_sherethis_verification_result';
19
- const MIN_WP_VERSION = '3.8';
20
- const GA_SHARETHIS_API_ALIAS = 'sharethis';
21
- const GA_DISABLE_ALL_FEATURES = 'googleanalytics_disable_all_features';
22
- const GA_HEARTBEAT_API_CACHE_UPDATE = false;
23
-
24
- /**
25
- * Instantiate API client.
26
- *
27
- * @return Ga_Lib_Google_Api_Client|null
28
- */
29
- public static function api_client( $type = '' ) {
30
- if ( self::GA_SHARETHIS_API_ALIAS === $type ) {
31
- $instance = Ga_Lib_Sharethis_Api_Client::get_instance();
32
- } else {
33
- $instance = Ga_Lib_Google_Api_Client::get_instance();
34
- $token = Ga_Helper::get_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
35
- try {
36
- if ( ! empty( $token ) ) {
37
- $token = json_decode( $token, true );
38
- $instance->set_access_token( $token );
39
- }
40
- } catch ( Exception $e ) {
41
- Ga_Helper::ga_oauth_notice( $e->getMessage() );
42
- }
43
- }
44
-
45
- return $instance;
46
- }
47
-
48
- /**
49
- * Initializes plugin's options during plugin activation process.
50
- */
51
- public static function activate_googleanalytics() {
52
- add_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, Ga_Helper::GA_DEFAULT_WEB_ID );
53
- add_option( self::GA_EXCLUDE_ROLES_OPTION_NAME, wp_json_encode( array() ) );
54
- add_option( self::GA_SHARETHIS_TERMS_OPTION_NAME, false );
55
- add_option( self::GA_HIDE_TERMS_OPTION_NAME, false );
56
- add_option( self::GA_VERSION_OPTION_NAME );
57
- add_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
58
- add_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
59
- add_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
60
- add_option( self::GA_SELECTED_ACCOUNT );
61
- add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
62
- add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
63
- add_option( self::GA_DISABLE_ALL_FEATURES );
64
- Ga_Cache::add_cache_options();
65
- }
66
-
67
- /**
68
- * Deletes plugin's options during plugin activation process.
69
- */
70
- public static function deactivate_googleanalytics() {
71
- delete_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
72
- delete_option( self::GA_EXCLUDE_ROLES_OPTION_NAME );
73
- delete_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
74
- delete_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
75
- delete_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
76
- delete_option( self::GA_SELECTED_ACCOUNT );
77
- delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
78
- delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
79
- delete_option( self::GA_DISABLE_ALL_FEATURES );
80
- delete_option( Ga_SupportLogger::LOG_OPTION );
81
- Ga_Cache::delete_cache_options();
82
- }
83
-
84
- /**
85
- * Deletes plugin's options during plugin uninstallation process.
86
- */
87
- public static function uninstall_googleanalytics() {
88
- delete_option( self::GA_SHARETHIS_TERMS_OPTION_NAME );
89
- delete_option( self::GA_HIDE_TERMS_OPTION_NAME );
90
- delete_option( self::GA_VERSION_OPTION_NAME );
91
- delete_option( self::GA_SHARETHIS_PROPERTY_ID );
92
- delete_option( self::GA_SHARETHIS_PROPERTY_SECRET );
93
- delete_option( self::GA_SHARETHIS_VERIFICATION_RESULT );
94
- }
95
-
96
- /**
97
- * Do actions during plugin load.
98
- */
99
- public static function loaded_googleanalytics() {
100
- self::update_googleanalytics();
101
- }
102
-
103
- /**
104
- * Update hook fires when plugin is being loaded.
105
- */
106
- public static function update_googleanalytics() {
107
- $version = get_option( self::GA_VERSION_OPTION_NAME );
108
- $installed_version = get_option( self::GA_VERSION_OPTION_NAME, '1.0.7' );
109
- $old_property_value = Ga_Helper::get_option( 'web_property_id' );
110
-
111
- if ( version_compare( $installed_version, GOOGLEANALYTICS_VERSION, 'eq' ) ) {
112
- return;
113
- }
114
- if ( empty( $old_property_value ) && empty( $version ) ) {
115
- update_option( self::GA_SHARETHIS_TERMS_OPTION_NAME, true );
116
- }
117
-
118
- if ( version_compare( $installed_version, GOOGLEANALYTICS_VERSION, 'lt' ) ) {
119
-
120
- if ( ! empty( $old_property_value ) ) {
121
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME, $old_property_value );
122
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, 1 );
123
- delete_option( 'web_property_id' );
124
- }
125
- }
126
-
127
- update_option( self::GA_VERSION_OPTION_NAME, GOOGLEANALYTICS_VERSION );
128
- }
129
-
130
- public static function preupdate_exclude_roles( $new_value, $old_value ) {
131
- if ( ! Ga_Helper::are_features_enabled() ) {
132
- return '';
133
- }
134
-
135
- return wp_json_encode( $new_value );
136
- }
137
-
138
- /**
139
- * Pre-update hook for preparing JSON structure.
140
- *
141
- * @param $new_value
142
- * @param $old_value
143
- *
144
- * @return mixed
145
- */
146
- public static function preupdate_selected_account( $new_value, $old_value ) {
147
- $data = null;
148
- if ( ! empty( $new_value ) ) {
149
- $data = explode( '_', $new_value );
150
-
151
- if ( ! empty( $data[1] ) ) {
152
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[1] );
153
- }
154
- }
155
-
156
- return wp_json_encode( $data );
157
- }
158
-
159
- public static function preupdate_disable_all_features( $new_value, $old_value ) {
160
- if ( 'on' === $old_value ) {
161
- Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false );
162
- }
163
-
164
- return $new_value;
165
- }
166
-
167
- public static function preupdate_optimize_code( $new_value, $old_value ) {
168
- if ( ! empty( $new_value ) ) {
169
- $new_value = sanitize_text_field( wp_unslash( $new_value ) );
170
- }
171
-
172
- return $new_value;
173
- }
174
-
175
- public static function preupdate_ip_anonymization( $new_value, $old_value ) {
176
- return $new_value;
177
- }
178
-
179
- /**
180
- * Registers plugin's settings.
181
- */
182
- public static function admin_init_googleanalytics() {
183
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_OPTION_NAME );
184
- register_setting( GA_NAME, self::GA_EXCLUDE_ROLES_OPTION_NAME );
185
- register_setting( GA_NAME, self::GA_SELECTED_ACCOUNT );
186
- register_setting( GA_NAME, self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
187
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
188
- register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
189
- register_setting( GA_NAME, self::GA_DISABLE_ALL_FEATURES );
190
- register_setting( GA_NAME, 'googleanalytics_optimize_code' );
191
- register_setting( GA_NAME, 'googleanalytics_ip_anonymization' );
192
- add_filter( 'pre_update_option_' . self::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles', 1, 2 );
193
- add_filter( 'pre_update_option_' . self::GA_SELECTED_ACCOUNT, 'GA_Admin::preupdate_selected_account', 1, 2 );
194
- add_filter( 'pre_update_option_googleanalytics_optimize_code', 'Ga_Admin::preupdate_optimize_code', 1, 2 );
195
- add_filter( 'pre_update_option_googleanalytics_ip_anonymization', 'Ga_Admin::preupdate_ip_anonymization', 1, 2 );
196
- }
197
-
198
- /**
199
- * Builds plugin's menu structure.
200
- */
201
- public static function admin_menu_googleanalytics() {
202
- if ( current_user_can( 'manage_options' ) ) {
203
- add_menu_page( 'Google Analytics', 'Google Analytics', 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics', 'dashicons-chart-line', 1000 );
204
- add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Dashboard' ), 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics' );
205
- add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Settings' ), 'manage_options', 'googleanalytics/settings', 'Ga_Admin::options_page_googleanalytics' );
206
- }
207
- }
208
-
209
- /**
210
- * Prepares and displays plugin's stats page.
211
- */
212
- public static function statistics_page_googleanalytics() {
213
-
214
- if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
215
- return false;
216
- }
217
-
218
- $data = self::get_stats_page();
219
- Ga_View_Core::load(
220
- 'statistics',
221
- array(
222
- 'data' => $data,
223
- )
224
- );
225
-
226
- if ( Ga_Cache::is_data_cache_outdated( '', Ga_Helper::get_account_id() ) ) {
227
- self::api_client()->add_own_error( '1', __( 'Saved data is shown, it will be refreshed soon' ), 'Ga_Data_Outdated_Exception' );
228
- }
229
-
230
- self::display_api_errors();
231
- }
232
-
233
- /**
234
- * Prepares and displays plugin's settings page.
235
- */
236
- public static function options_page_googleanalytics() {
237
-
238
- if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
239
- return false;
240
- }
241
- if ( Ga_Helper::are_features_enabled() && Ga_Helper::is_curl_disabled() ) {
242
- echo wp_kses_post( Ga_Helper::ga_wp_notice( __( 'Looks like cURL is not configured on your server. In order to authenticate your Google Analytics account and display statistics, cURL is required. Please contact your server administrator to enable it, or manually enter your Tracking ID.' ), 'warning' ) );
243
- }
244
- /**
245
- * Keeps data to be extracted as variables in the view.
246
- *
247
- * @var array $data
248
- */
249
- $data = array();
250
-
251
- $data[ self::GA_WEB_PROPERTY_ID_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
252
- $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
253
- $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
254
- $data[ self::GA_DISABLE_ALL_FEATURES ] = get_option( self::GA_DISABLE_ALL_FEATURES );
255
-
256
- $roles = Ga_Helper::get_user_roles();
257
- $saved = json_decode( get_option( self::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
258
-
259
- $tmp = array();
260
- if ( ! empty( $roles ) ) {
261
- foreach ( $roles as $role ) {
262
- $role_id = Ga_Helper::prepare_role_id( $role );
263
- $tmp[] = array(
264
- 'name' => $role,
265
- 'id' => $role_id,
266
- 'checked' => ( ! empty( $saved[ $role_id ] ) && 'on' === $saved[ $role_id ] ),
267
- );
268
- }
269
- }
270
- $data['roles'] = $tmp;
271
-
272
- if ( Ga_Helper::is_authorized() ) {
273
- $data['ga_accounts_selector'] = self::get_accounts_selector();
274
- $data['auth_button'] = self::get_auth_button( 'reauth' );
275
- } else {
276
- $data['popup_url'] = self::get_auth_popup_url();
277
- $data['auth_button'] = self::get_auth_button( 'auth' );
278
- }
279
- $data['debug_modal'] = self::get_debug_modal();
280
- $data['debug_info'] = Ga_SupportLogger::$debug_info;
281
-
282
- if ( ! empty( $_GET['err'] ) ) { // WPCS: CSRF ok.
283
- switch ( $_GET['err'] ) { // WPCS: CSRF ok.
284
- case 1:
285
- $data['error_message'] = Ga_Helper::ga_oauth_notice( 'There was a problem with Google Oauth2 authentication process. Please verify your site has a valid SSL Certificate in place and is using the HTTPS protocol.' );
286
- break;
287
- case 2:
288
- $data['error_message'] = Ga_Helper::ga_wp_notice( 'Authentication code is incorrect.', 'error', true );
289
- break;
290
- }
291
- }
292
- Ga_View_Core::load(
293
- 'page',
294
- array(
295
- 'data' => $data,
296
- 'tooltip' => Ga_Helper::get_tooltip(),
297
- )
298
- );
299
-
300
- self::display_api_errors();
301
- }
302
-
303
- /**
304
- * Prepares and returns a plugin's URL to be opened in a popup window
305
- * during Google authentication process.
306
- *
307
- * @return mixed
308
- */
309
- public static function get_auth_popup_url() {
310
- return admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_auth' ) ) );
311
- }
312
-
313
- /**
314
- * Prepares and returns Google Account's dropdown code.
315
- *
316
- * @return string
317
- */
318
- public static function get_accounts_selector() {
319
- $selected = Ga_Helper::get_selected_account_data();
320
- $selector = json_decode( get_option( self::GA_ACCOUNT_DATA_OPTION_NAME ), true );
321
- if ( ! Ga_Helper::is_code_manually_enabled() && empty( $selector ) ) {
322
- echo wp_kses_post( Ga_Helper::ga_wp_notice( "Hi there! It seems like we weren't able to locate a Google Analytics account attached to your email account. Can you please register for Google Analytics and then deactivate and reactivate the plugin?", 'warning' ) );
323
- }
324
-
325
- return Ga_View_Core::load(
326
- 'ga_accounts_selector',
327
- array(
328
- 'selector' => $selector,
329
- 'selected' => $selected ? implode( '_', $selected ) : null,
330
- 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled() || Ga_Helper::is_all_feature_disabled(),
331
- ),
332
- true
333
- );
334
- }
335
-
336
- /**
337
- * Adds JS scripts for the settings page.
338
- */
339
- public static function enqueue_ga_scripts() {
340
- wp_register_script(
341
- GA_NAME . '-page-js',
342
- Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_page.js',
343
- [ 'jquery' ],
344
- GOOGLEANALYTICS_VERSION,
345
- false
346
- );
347
- wp_enqueue_script( GA_NAME . '-page-js' );
348
- }
349
-
350
- /**
351
- * Adds CSS plugin's scripts.
352
- */
353
- public static function enqueue_ga_css() {
354
- wp_register_style( GA_NAME . '-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/' . GA_NAME . '.css', false, GOOGLEANALYTICS_VERSION, 'all' );
355
- wp_register_style( GA_NAME . '-additional-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_additional.css', false, GOOGLEANALYTICS_VERSION, 'all' );
356
- wp_enqueue_style( GA_NAME . '-css' );
357
- wp_enqueue_style( GA_NAME . '-additional-css' );
358
- if ( Ga_Helper::is_wp_old() ) {
359
- wp_register_style( GA_NAME . '-old-wp-support-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_old_wp_support.css', false, GOOGLEANALYTICS_VERSION, 'all' );
360
- wp_enqueue_style( GA_NAME . '-old-wp-support-css' );
361
- }
362
- wp_register_style( GA_NAME . '-modal-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_modal.css', false, GOOGLEANALYTICS_VERSION, 'all' );
363
- wp_enqueue_style( GA_NAME . '-modal-css' );
364
- }
365
-
366
- /**
367
- * Enqueues dashboard JS scripts.
368
- */
369
- private static function enqueue_dashboard_scripts() {
370
- wp_register_script(
371
- GA_NAME . '-dashboard-js',
372
- Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_dashboard.js',
373
- [ 'jquery' ],
374
- GOOGLEANALYTICS_VERSION,
375
- false
376
- );
377
- wp_enqueue_script( GA_NAME . '-dashboard-js' );
378
- }
379
-
380
- /**
381
- * Enqueues plugin's JS and CSS scripts.
382
- */
383
- public static function enqueue_scripts() {
384
- if ( Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page() ) {
385
- wp_register_script(
386
- GA_NAME . '-js',
387
- Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '.js',
388
- [ 'jquery' ],
389
- GOOGLEANALYTICS_VERSION,
390
- false
391
- );
392
- wp_enqueue_script( GA_NAME . '-js' );
393
-
394
- wp_register_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', null, 1, false );
395
- wp_enqueue_script( 'googlecharts' );
396
-
397
- self::enqueue_ga_css();
398
- }
399
-
400
- if ( Ga_Helper::is_dashboard_page() ) {
401
- self::enqueue_dashboard_scripts();
402
- }
403
-
404
- if ( Ga_Helper::is_plugin_page() ) {
405
- self::enqueue_ga_scripts();
406
- }
407
- }
408
-
409
- /**
410
- * Prepares plugin's statistics page and return HTML code.
411
- *
412
- * @return string HTML code
413
- */
414
- public static function get_stats_page() {
415
- $chart = null;
416
- $boxes = null;
417
- $labels = null;
418
- $sources = null;
419
- if ( Ga_Helper::is_authorized() && Ga_Helper::is_account_selected() && ! Ga_Helper::is_all_feature_disabled() ) {
420
- list( $chart, $boxes, $labels, $sources ) = self::generate_stats_data();
421
- }
422
-
423
- return Ga_Helper::get_chart_page(
424
- 'stats',
425
- array(
426
- 'chart' => $chart,
427
- 'boxes' => $boxes,
428
- 'labels' => $labels,
429
- 'sources' => $sources,
430
- )
431
- );
432
- }
433
-
434
- /**
435
- * Shows plugin's notice on the admin area.
436
- */
437
- public static function admin_notice_googleanalytics() {
438
- 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 ) ) ) {
439
- $current_url = Ga_Helper::get_current_url();
440
- $url = ( strstr( $current_url, '?' ) ? $current_url . '&' : $current_url . '?' ) . http_build_query( array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_update_terms' ) );
441
- Ga_View_Core::load( 'ga_notice', [ 'url' => $url ] );
442
- }
443
-
444
- if ( Ga_Helper::get_option( self::GA_DISABLE_ALL_FEATURES ) ) {
445
- echo wp_kses_post(
446
- Ga_Helper::ga_wp_notice(
447
- __( 'You have disabled all extra features, click here to enable Dashboards, Viral Alerts and Google API.' ),
448
- 'warning',
449
- false,
450
- array(
451
- 'url' => admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features' ) ) ),
452
- 'label' => __( 'Enable' ),
453
- )
454
- )
455
- );
456
- }
457
- }
458
-
459
- /**
460
- * Prepare required PHP version warning.
461
- * @return string
462
- */
463
- public static function admin_notice_googleanalytics_php_version() {
464
- echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Cannot use Google Analytics plugin. PHP version ' . phpversion() . ' is to low. Required PHP version: ' . Ga_Helper::PHP_VERSION_REQUIRED, 'error' ) );
465
- }
466
-
467
- /**
468
- * Prepare required WP version warning
469
- * @return string
470
- */
471
- public static function admin_notice_googleanalytics_wp_version() {
472
- echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION, 'error' ) );
473
- }
474
-
475
- /**
476
- * Hides plugin's notice
477
- */
478
- public static function admin_notice_hide_googleanalytics() {
479
- update_option( self::GA_HIDE_TERMS_OPTION_NAME, true );
480
- }
481
-
482
- /**
483
- * Adds GA dashboard widget only for administrators.
484
- */
485
- public static function add_dashboard_device_widget() {
486
- if ( Ga_Helper::is_administrator() ) {
487
- wp_add_dashboard_widget( 'ga_dashboard_widget', __( 'Google Analytics Dashboard' ), 'Ga_Helper::add_ga_dashboard_widget' );
488
- }
489
- }
490
-
491
- /**
492
- * Adds plugin's actions
493
- */
494
- public static function add_actions() {
495
- add_action( 'admin_init', 'Ga_Admin::admin_init_googleanalytics' );
496
- add_action( 'admin_menu', 'Ga_Admin::admin_menu_googleanalytics' );
497
- add_action( 'admin_enqueue_scripts', 'Ga_Admin::enqueue_scripts' );
498
- add_action( 'wp_dashboard_setup', 'Ga_Admin::add_dashboard_device_widget' );
499
- add_action( 'wp_ajax_ga_ajax_data_change', 'Ga_Admin::ga_ajax_data_change' );
500
- add_action( 'wp_ajax_ga_ajax_hide_review', 'Ga_Admin::ga_ajax_hide_review' );
501
- add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics' );
502
- add_action( 'heartbeat_tick', 'Ga_Admin::run_heartbeat_jobs' );
503
- add_action( 'wp_ajax_googleanalytics_send_debug_email', 'Ga_SupportLogger::send_email' );
504
- if ( ! get_option( self::GA_SHARETHIS_TERMS_OPTION_NAME ) && ! get_option( self::GA_HIDE_TERMS_OPTION_NAME ) ) {
505
- add_action( 'wp_ajax_googleanalytics_hide_terms', 'Ga_Admin::admin_notice_hide_googleanalytics' );
506
- }
507
- }
508
-
509
- /**
510
- * Runs jobs
511
- *
512
- * @param $response
513
- * @param $screen_id
514
- */
515
- public static function run_heartbeat_jobs( $response, $screen_id = '' ) {
516
-
517
- if ( self::GA_HEARTBEAT_API_CACHE_UPDATE ) {
518
- // Disable cache for ajax request
519
- self::api_client()->set_disable_cache( true );
520
-
521
- // Try to regenerate cache if needed
522
- self::generate_stats_data();
523
- }
524
- }
525
-
526
- /**
527
- * Adds plugin's filters
528
- */
529
- public static function add_filters() {
530
- add_filter( 'plugin_action_links', 'Ga_Admin::ga_action_links', 10, 5 );
531
- }
532
-
533
- /**
534
- * Adds new action links on the plugin list.
535
- *
536
- * @param $actions
537
- * @param $plugin_file
538
- *
539
- * @return mixed
540
- */
541
- public static function ga_action_links( $actions, $plugin_file ) {
542
-
543
- if ( basename( $plugin_file ) === GA_NAME . '.php' ) {
544
- array_unshift( $actions, '<a href="' . esc_url( get_admin_url( null, Ga_Helper::GA_SETTINGS_PAGE_URL ) ) . '">' . __( 'Settings' ) . '</a>' );
545
- }
546
-
547
- return $actions;
548
- }
549
-
550
- public static function init_oauth() {
551
-
552
- $code = Ga_Helper::get_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
553
-
554
- if ( ! empty( $code ) ) {
555
- Ga_Helper::update_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME, '' );
556
-
557
- // Get access token
558
- $response = self::api_client()->call( 'ga_auth_get_access_token', $code );
559
- if ( empty( $response ) ) {
560
- return false;
561
- }
562
- $param = '';
563
- if ( ! self::save_access_token( $response ) ) {
564
- $param = '&err=1';
565
- $errors = self::api_client()->get_errors();
566
- if ( ! empty( $errors ) ) {
567
- foreach ( $errors as $error ) {
568
- if ( 'invalid_grant' === $error['message'] ) {
569
- $param = '&err=2';
570
- }
571
- }
572
- }
573
- } else {
574
- self::api_client()->set_access_token( $response->getData() );
575
- // Get accounts data
576
- $account_summaries = self::api_client()->call( 'ga_api_account_summaries' );
577
- self::save_ga_account_summaries( $account_summaries->getData() );
578
- update_option( self::GA_SELECTED_ACCOUNT, '' );
579
- }
580
-
581
- wp_safe_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL . $param ) );
582
- }
583
- }
584
-
585
- /**
586
- * Save access token.
587
- *
588
- * @param Ga_Lib_Api_Response $response
589
- *
590
- * @return boolean
591
- */
592
- public static function save_access_token( $response, $refresh_token = '' ) {
593
- $access_token = $response->getData();
594
- if ( ! empty( $access_token ) ) {
595
- $access_token['created'] = time();
596
- } else {
597
- return false;
598
- }
599
-
600
- if ( ! empty( $refresh_token ) ) {
601
- $access_token['refresh_token'] = $refresh_token;
602
- }
603
-
604
- return update_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode( $access_token ) );
605
- }
606
-
607
- /**
608
- * Saves Google Analytics account data.
609
- *
610
- * @param $data
611
- *
612
- * @return array
613
- */
614
- public static function save_ga_account_summaries( $data ) {
615
- $return = array();
616
- if ( ! empty( $data['items'] ) ) {
617
- foreach ( $data['items'] as $item ) {
618
- $tmp = array();
619
- $tmp['id'] = $item['id'];
620
- $tmp['name'] = $item['name'];
621
- if ( is_array( $item['webProperties'] ) ) {
622
- foreach ( $item['webProperties'] as $property ) {
623
- $profiles = array();
624
- if ( is_array( $property['profiles'] ) ) {
625
- foreach ( $property['profiles'] as $profile ) {
626
- $profiles[] = array(
627
- 'id' => $profile['id'],
628
- 'name' => $profile['name'],
629
- );
630
- }
631
- }
632
-
633
- $tmp['webProperties'][] = array(
634
- 'internalWebPropertyId' => $property['internalWebPropertyId'],
635
- 'webPropertyId' => $property['id'],
636
- 'name' => $property['name'],
637
- 'profiles' => $profiles,
638
- );
639
- }
640
- }
641
-
642
- $return[] = $tmp;
643
- }
644
-
645
- update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode( $return ) );
646
- } else {
647
- update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, '' );
648
- }
649
- update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, '' );
650
-
651
- return $return;
652
- }
653
-
654
- /**
655
- * Handle AJAX data for the GA dashboard widget.
656
- */
657
- public static function ga_ajax_data_change() {
658
- if ( Ga_Admin_Controller::validate_ajax_data_change_post( $_POST ) ) {
659
- $date_range = ! empty( $_POST['date_range'] ) ? $_POST['date_range'] : null; // WPCS: CSRF ok.
660
- $metric = ! empty( $_POST['metric'] ) ? $_POST['metric'] : null; // WPCS: CSRF ok.
661
- echo wp_kses_post( Ga_Helper::get_ga_dashboard_widget_data_json( $date_range, $metric, false, true ) );
662
- } else {
663
- echo wp_json_encode( array( 'error' => __( 'Invalid request.' ) ) );
664
- }
665
-
666
- wp_die();
667
- }
668
-
669
- /**
670
- * Displays API error messages.
671
- */
672
- public static function display_api_errors( $alias = '' ) {
673
- $errors = self::api_client( $alias )->get_errors();
674
- if ( ! empty( $errors ) ) {
675
- foreach ( $errors as $error ) {
676
- echo wp_kses_post( Ga_Notice::get_message( $error ) );
677
- }
678
- }
679
- }
680
-
681
- /**
682
- * Gets dashboard data.
683
- *
684
- * @return array
685
- */
686
- public static function generate_stats_data() {
687
- $selected = Ga_Helper::get_selected_account_data( true );
688
-
689
- $query_params = isset( $_GET['th'] ) ? Ga_Stats::get_query( 'main_chart', $selected['view_id'], '30daysAgo' ) : Ga_Stats::get_query( 'main_chart', $selected['view_id'] );
690
-
691
- $stats_data = self::api_client()->call(
692
- 'ga_api_data',
693
- [ $query_params ]
694
- );
695
-
696
- $boxes_data = self::api_client()->call(
697
- 'ga_api_data',
698
- [ Ga_Stats::get_query( 'boxes', $selected['view_id'] ) ]
699
- );
700
- $sources_data = self::api_client()->call(
701
- 'ga_api_data',
702
- [ Ga_Stats::get_query( 'sources', $selected['view_id'] ) ]
703
- );
704
- $chart = ! empty( $stats_data ) ? Ga_Stats::get_chart( $stats_data->getData() ) : array();
705
- $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_boxes( $boxes_data->getData() ) : array();
706
- $last_chart_date = ! empty( $chart ) ? $chart['date'] : strtotime( 'now' );
707
- unset( $chart['date'] );
708
- $labels = array(
709
- 'thisWeek' => date( 'M d, Y', strtotime( '-6 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
710
- 'thisMonth' => date( 'M d, Y', strtotime( '-29 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
711
- );
712
- $sources = ! empty( $sources_data ) ? Ga_Stats::get_sources( $sources_data->getData() ) : array();
713
-
714
- return array( $chart, $boxes, $labels, $sources );
715
- }
716
-
717
- /**
718
- * Returns auth or re-auth button
719
- *
720
- * @return string
721
- */
722
- public static function get_auth_button( $type ) {
723
-
724
- return Ga_View_Core::load(
725
- 'ga_auth_button',
726
- [
727
- 'label' => 'auth' === $type ? 'Authenticate with Google' : 'Re-authenticate with Google',
728
- 'type' => $type,
729
- 'url' => self::get_auth_popup_url(),
730
- 'manually_id' => get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ),
731
- ],
732
- true
733
- );
734
- }
735
-
736
- /**
737
- * Returns debug modal
738
- *
739
- * @return string
740
- */
741
- public static function get_debug_modal() {
742
-
743
- return Ga_View_Core::load(
744
- 'ga_debug_modal',
745
- [ 'debug_info' => Ga_SupportLogger::$debug_info, 'debug_help_message' => Ga_SupportLogger::$debug_help_message ],
746
- true
747
- );
748
- }
749
-
750
- public static function ga_ajax_hide_review( $post ) {
751
- $error = 0;
752
-
753
- if ( Ga_Controller_Core::verify_nonce( 'ga_ajax_data_change' ) ) {
754
- update_option('googleanalytics-hide-review', true);
755
- }
756
-
757
- wp_send_json_success('hidden');
758
- }
759
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Ga_Admin {
4
+
5
+ const GA_WEB_PROPERTY_ID_OPTION_NAME = 'googleanalytics_web_property_id';
6
+ const GA_EXCLUDE_ROLES_OPTION_NAME = 'googleanalytics_exclude_roles';
7
+ const GA_SHARETHIS_TERMS_OPTION_NAME = 'googleanalytics_sharethis_terms';
8
+ const GA_HIDE_TERMS_OPTION_NAME = 'googleanalytics_hide_terms';
9
+ const GA_VERSION_OPTION_NAME = 'googleanalytics_version';
10
+ const GA_SELECTED_ACCOUNT = 'googleanalytics_selected_account';
11
+ const GA_OAUTH_AUTH_CODE_OPTION_NAME = 'googleanalytics_oauth_auth_code';
12
+ const GA_OAUTH_AUTH_TOKEN_OPTION_NAME = 'googleanalytics_oauth_auth_token';
13
+ const GA_ACCOUNT_DATA_OPTION_NAME = 'googleanalytics_account_data';
14
+ const GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME = 'googleanalytics_web_property_id_manually';
15
+ const GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME = 'googleanalytics_web_property_id_manually_value';
16
+ const GA_SHARETHIS_PROPERTY_ID = 'googleanalytics_sherethis_property_id';
17
+ const GA_SHARETHIS_PROPERTY_SECRET = 'googleanalytics_sherethis_property_secret';
18
+ const GA_SHARETHIS_VERIFICATION_RESULT = 'googleanalytics_sherethis_verification_result';
19
+ const MIN_WP_VERSION = '3.8';
20
+ const GA_SHARETHIS_API_ALIAS = 'sharethis';
21
+ const GA_DISABLE_ALL_FEATURES = 'googleanalytics_disable_all_features';
22
+ const GA_HEARTBEAT_API_CACHE_UPDATE = false;
23
+
24
+ /**
25
+ * Instantiate API client.
26
+ *
27
+ * @return Ga_Lib_Google_Api_Client|null
28
+ */
29
+ public static function api_client( $type = '' ) {
30
+ if ( self::GA_SHARETHIS_API_ALIAS === $type ) {
31
+ $instance = Ga_Lib_Sharethis_Api_Client::get_instance();
32
+ } else {
33
+ $instance = Ga_Lib_Google_Api_Client::get_instance();
34
+ $token = Ga_Helper::get_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
35
+ try {
36
+ if ( ! empty( $token ) ) {
37
+ $token = json_decode( $token, true );
38
+ $instance->set_access_token( $token );
39
+ }
40
+ } catch ( Exception $e ) {
41
+ Ga_Helper::ga_oauth_notice( $e->getMessage() );
42
+ }
43
+ }
44
+
45
+ return $instance;
46
+ }
47
+
48
+ /**
49
+ * Initializes plugin's options during plugin activation process.
50
+ */
51
+ public static function activate_googleanalytics() {
52
+ add_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, Ga_Helper::GA_DEFAULT_WEB_ID );
53
+ add_option( self::GA_EXCLUDE_ROLES_OPTION_NAME, wp_json_encode( array() ) );
54
+ add_option( self::GA_SHARETHIS_TERMS_OPTION_NAME, false );
55
+ add_option( self::GA_HIDE_TERMS_OPTION_NAME, false );
56
+ add_option( self::GA_VERSION_OPTION_NAME );
57
+ add_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
58
+ add_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
59
+ add_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
60
+ add_option( self::GA_SELECTED_ACCOUNT );
61
+ add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
62
+ add_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
63
+ add_option( self::GA_DISABLE_ALL_FEATURES );
64
+ Ga_Cache::add_cache_options();
65
+ }
66
+
67
+ /**
68
+ * Deletes plugin's options during plugin activation process.
69
+ */
70
+ public static function deactivate_googleanalytics() {
71
+ delete_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
72
+ delete_option( self::GA_EXCLUDE_ROLES_OPTION_NAME );
73
+ delete_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
74
+ delete_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME );
75
+ delete_option( self::GA_ACCOUNT_DATA_OPTION_NAME );
76
+ delete_option( self::GA_SELECTED_ACCOUNT );
77
+ delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
78
+ delete_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
79
+ delete_option( self::GA_DISABLE_ALL_FEATURES );
80
+ delete_option( Ga_SupportLogger::LOG_OPTION );
81
+ Ga_Cache::delete_cache_options();
82
+ }
83
+
84
+ /**
85
+ * Deletes plugin's options during plugin uninstallation process.
86
+ */
87
+ public static function uninstall_googleanalytics() {
88
+ delete_option( self::GA_SHARETHIS_TERMS_OPTION_NAME );
89
+ delete_option( self::GA_HIDE_TERMS_OPTION_NAME );
90
+ delete_option( self::GA_VERSION_OPTION_NAME );
91
+ delete_option( self::GA_SHARETHIS_PROPERTY_ID );
92
+ delete_option( self::GA_SHARETHIS_PROPERTY_SECRET );
93
+ delete_option( self::GA_SHARETHIS_VERIFICATION_RESULT );
94
+ }
95
+
96
+ /**
97
+ * Do actions during plugin load.
98
+ */
99
+ public static function loaded_googleanalytics() {
100
+ self::update_googleanalytics();
101
+ }
102
+
103
+ /**
104
+ * Update hook fires when plugin is being loaded.
105
+ */
106
+ public static function update_googleanalytics() {
107
+ $version = get_option( self::GA_VERSION_OPTION_NAME );
108
+ $installed_version = get_option( self::GA_VERSION_OPTION_NAME, '2.3.7' );
109
+ $old_property_value = Ga_Helper::get_option( 'web_property_id' );
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
+ public static function preupdate_exclude_roles( $new_value, $old_value ) {
124
+ if ( ! Ga_Helper::are_features_enabled() ) {
125
+ return '';
126
+ }
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 = null;
141
+ if ( ! empty( $new_value ) ) {
142
+ $data = explode( '_', $new_value );
143
+
144
+ if ( ! empty( $data[1] ) ) {
145
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, $data[1] );
146
+ }
147
+ }
148
+
149
+ return wp_json_encode( $data );
150
+ }
151
+
152
+ public static function preupdate_disable_all_features( $new_value, $old_value ) {
153
+ if ( 'on' === $old_value ) {
154
+ Ga_Helper::update_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false );
155
+ }
156
+
157
+ return $new_value;
158
+ }
159
+
160
+ public static function preupdate_optimize_code( $new_value, $old_value ) {
161
+ if ( ! empty( $new_value ) ) {
162
+ $new_value = sanitize_text_field( wp_unslash( $new_value ) );
163
+ }
164
+
165
+ return $new_value;
166
+ }
167
+
168
+ public static function preupdate_ip_anonymization( $new_value, $old_value ) {
169
+ return $new_value;
170
+ }
171
+
172
+ /**
173
+ * Registers plugin's settings.
174
+ */
175
+ public static function admin_init_googleanalytics() {
176
+ register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_OPTION_NAME );
177
+ register_setting( GA_NAME, self::GA_EXCLUDE_ROLES_OPTION_NAME );
178
+ register_setting( GA_NAME, self::GA_SELECTED_ACCOUNT );
179
+ register_setting( GA_NAME, self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
180
+ register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
181
+ register_setting( GA_NAME, self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
182
+ register_setting( GA_NAME, self::GA_DISABLE_ALL_FEATURES );
183
+ register_setting( GA_NAME, 'googleanalytics_optimize_code' );
184
+ register_setting( GA_NAME, 'googleanalytics_ip_anonymization' );
185
+ add_filter( 'pre_update_option_' . self::GA_EXCLUDE_ROLES_OPTION_NAME, 'Ga_Admin::preupdate_exclude_roles', 1, 2 );
186
+ add_filter( 'pre_update_option_' . self::GA_SELECTED_ACCOUNT, 'GA_Admin::preupdate_selected_account', 1, 2 );
187
+ add_filter( 'pre_update_option_googleanalytics_optimize_code', 'Ga_Admin::preupdate_optimize_code', 1, 2 );
188
+ add_filter( 'pre_update_option_googleanalytics_ip_anonymization', 'Ga_Admin::preupdate_ip_anonymization', 1, 2 );
189
+ }
190
+
191
+ /**
192
+ * Builds plugin's menu structure.
193
+ */
194
+ public static function admin_menu_googleanalytics() {
195
+ if ( current_user_can( 'manage_options' ) ) {
196
+ add_menu_page( 'Google Analytics', 'Google Analytics', 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics', 'dashicons-chart-line', 1000 );
197
+ add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Dashboard' ), 'manage_options', 'googleanalytics', 'Ga_Admin::statistics_page_googleanalytics' );
198
+ add_submenu_page( 'googleanalytics', 'Google Analytics', __( 'Settings' ), 'manage_options', 'googleanalytics/settings', 'Ga_Admin::options_page_googleanalytics' );
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Prepares and displays plugin's stats page.
204
+ */
205
+ public static function statistics_page_googleanalytics() {
206
+
207
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
208
+ return false;
209
+ }
210
+
211
+ $data = self::get_stats_page();
212
+ Ga_View_Core::load(
213
+ 'statistics',
214
+ array(
215
+ 'data' => $data,
216
+ )
217
+ );
218
+
219
+ if ( Ga_Cache::is_data_cache_outdated( '', Ga_Helper::get_account_id() ) ) {
220
+ self::api_client()->add_own_error( '1', __( 'Saved data is shown, it will be refreshed soon' ), 'Ga_Data_Outdated_Exception' );
221
+ }
222
+
223
+ self::display_api_errors();
224
+ }
225
+
226
+ /**
227
+ * Prepares and displays plugin's settings page.
228
+ */
229
+ public static function options_page_googleanalytics() {
230
+
231
+ if ( ! Ga_Helper::is_wp_version_valid() || ! Ga_Helper::is_php_version_valid() ) {
232
+ return false;
233
+ }
234
+ if ( Ga_Helper::are_features_enabled() && Ga_Helper::is_curl_disabled() ) {
235
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( __( 'Looks like cURL is not configured on your server. In order to authenticate your Google Analytics account and display statistics, cURL is required. Please contact your server administrator to enable it, or manually enter your Tracking ID.' ), 'warning' ) );
236
+ }
237
+ /**
238
+ * Keeps data to be extracted as variables in the view.
239
+ *
240
+ * @var array $data
241
+ */
242
+ $data = array();
243
+
244
+ $data[ self::GA_WEB_PROPERTY_ID_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME );
245
+ $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_VALUE_OPTION_NAME );
246
+ $data[ self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ] = get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
247
+ $data[ self::GA_DISABLE_ALL_FEATURES ] = get_option( self::GA_DISABLE_ALL_FEATURES );
248
+
249
+ $roles = Ga_Helper::get_user_roles();
250
+ $saved = json_decode( get_option( self::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
251
+
252
+ $tmp = array();
253
+ if ( ! empty( $roles ) ) {
254
+ foreach ( $roles as $role ) {
255
+ $role_id = Ga_Helper::prepare_role_id( $role );
256
+ $tmp[] = array(
257
+ 'name' => $role,
258
+ 'id' => $role_id,
259
+ 'checked' => ( ! empty( $saved[ $role_id ] ) && 'on' === $saved[ $role_id ] ),
260
+ );
261
+ }
262
+ }
263
+ $data['roles'] = $tmp;
264
+
265
+ if ( Ga_Helper::is_authorized() ) {
266
+ $data['ga_accounts_selector'] = self::get_accounts_selector();
267
+ $data['auth_button'] = self::get_auth_button( 'reauth' );
268
+ } else {
269
+ $data['popup_url'] = self::get_auth_popup_url();
270
+ $data['auth_button'] = self::get_auth_button( 'auth' );
271
+ }
272
+ $data['debug_modal'] = self::get_debug_modal();
273
+ $data['debug_info'] = Ga_SupportLogger::$debug_info;
274
+
275
+ if ( ! empty( $_GET['err'] ) ) { // WPCS: CSRF ok.
276
+ switch ( $_GET['err'] ) { // WPCS: CSRF ok.
277
+ case 1:
278
+ $data['error_message'] = Ga_Helper::ga_oauth_notice( 'There was a problem with Google Oauth2 authentication process. Please verify your site has a valid SSL Certificate in place and is using the HTTPS protocol.' );
279
+ break;
280
+ case 2:
281
+ $data['error_message'] = Ga_Helper::ga_wp_notice( 'Authentication code is incorrect.', 'error', true );
282
+ break;
283
+ }
284
+ }
285
+ Ga_View_Core::load(
286
+ 'page',
287
+ array(
288
+ 'data' => $data,
289
+ 'tooltip' => Ga_Helper::get_tooltip(),
290
+ )
291
+ );
292
+
293
+ self::display_api_errors();
294
+ }
295
+
296
+ /**
297
+ * Prepares and returns a plugin's URL to be opened in a popup window
298
+ * during Google authentication process.
299
+ *
300
+ * @return mixed
301
+ */
302
+ public static function get_auth_popup_url() {
303
+ return admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_auth' ) ) );
304
+ }
305
+
306
+ /**
307
+ * Prepares and returns Google Account's dropdown code.
308
+ *
309
+ * @return string
310
+ */
311
+ public static function get_accounts_selector() {
312
+ $selected = Ga_Helper::get_selected_account_data();
313
+ $selector = json_decode( get_option( self::GA_ACCOUNT_DATA_OPTION_NAME ), true );
314
+ if ( ! Ga_Helper::is_code_manually_enabled() && empty( $selector ) ) {
315
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( "Hi there! It seems like we weren't able to locate a Google Analytics account attached to your email account. Can you please register for Google Analytics and then deactivate and reactivate the plugin?", 'warning' ) );
316
+ }
317
+
318
+ return Ga_View_Core::load(
319
+ 'ga_accounts_selector',
320
+ array(
321
+ 'selector' => $selector,
322
+ 'selected' => $selected ? implode( '_', $selected ) : null,
323
+ 'add_manually_enabled' => Ga_Helper::is_code_manually_enabled() || Ga_Helper::is_all_feature_disabled(),
324
+ ),
325
+ true
326
+ );
327
+ }
328
+
329
+ /**
330
+ * Adds JS scripts for the settings page.
331
+ */
332
+ public static function enqueue_ga_scripts() {
333
+ wp_register_script(
334
+ GA_NAME . '-page-js',
335
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_page.js',
336
+ [ 'jquery' ],
337
+ GOOGLEANALYTICS_VERSION,
338
+ false
339
+ );
340
+ wp_enqueue_script( GA_NAME . '-page-js' );
341
+ }
342
+
343
+ /**
344
+ * Adds CSS plugin's scripts.
345
+ */
346
+ public static function enqueue_ga_css() {
347
+ wp_register_style( GA_NAME . '-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/' . GA_NAME . '.css', false, GOOGLEANALYTICS_VERSION, 'all' );
348
+ wp_register_style( GA_NAME . '-additional-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_additional.css', false, GOOGLEANALYTICS_VERSION, 'all' );
349
+ wp_enqueue_style( GA_NAME . '-css' );
350
+ wp_enqueue_style( GA_NAME . '-additional-css' );
351
+ if ( Ga_Helper::is_wp_old() ) {
352
+ wp_register_style( GA_NAME . '-old-wp-support-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_old_wp_support.css', false, GOOGLEANALYTICS_VERSION, 'all' );
353
+ wp_enqueue_style( GA_NAME . '-old-wp-support-css' );
354
+ }
355
+ wp_register_style( GA_NAME . '-modal-css', Ga_Helper::get_plugin_url_with_correct_protocol() . '/css/ga_modal.css', false, GOOGLEANALYTICS_VERSION, 'all' );
356
+ wp_enqueue_style( GA_NAME . '-modal-css' );
357
+ }
358
+
359
+ /**
360
+ * Enqueues dashboard JS scripts.
361
+ */
362
+ private static function enqueue_dashboard_scripts() {
363
+ wp_register_script(
364
+ GA_NAME . '-dashboard-js',
365
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '_dashboard.js',
366
+ [ 'jquery' ],
367
+ GOOGLEANALYTICS_VERSION,
368
+ false
369
+ );
370
+ wp_enqueue_script( GA_NAME . '-dashboard-js' );
371
+ }
372
+
373
+ /**
374
+ * Enqueues plugin's JS and CSS scripts.
375
+ */
376
+ public static function enqueue_scripts() {
377
+ $domain = str_replace('http://','', str_replace('https://', '', str_replace( '/wp-admin/', '', admin_url() )));
378
+ $st_prop = get_option(self::GA_SHARETHIS_PROPERTY_ID);
379
+ $st_secret = get_option(self::GA_SHARETHIS_PROPERTY_SECRET);
380
+
381
+ if ( Ga_Helper::is_dashboard_page() || Ga_Helper::is_plugin_page() ) {
382
+ wp_register_script(
383
+ GA_NAME . '-js',
384
+ Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/' . GA_NAME . '.js',
385
+ [ 'jquery' ],
386
+ GOOGLEANALYTICS_VERSION,
387
+ false
388
+ );
389
+ wp_enqueue_script( GA_NAME . '-js' );
390
+
391
+ wp_register_script( 'googlecharts', 'https://www.gstatic.com/charts/loader.js', null, 1, false );
392
+ wp_enqueue_script( 'googlecharts' );
393
+
394
+ if ( empty($st_prop) || empty($st_secret) ) {
395
+ wp_register_script( 'googlecreateprop', Ga_Helper::get_plugin_url_with_correct_protocol() . '/js/googleanalytics_createprop.js', ['jquery', 'wp-util'], time(), false );
396
+ wp_enqueue_script('googlecreateprop');
397
+ wp_add_inline_script('googlecreateprop', '
398
+ var gaNonce = "' . wp_create_nonce('googleanalyticsnonce') . '";
399
+ var gasiteURL = "' . $domain . '";
400
+ var gaAdminEmail = "' . get_option('admin_email') . '";'
401
+ );
402
+ }
403
+
404
+ self::enqueue_ga_css();
405
+ }
406
+
407
+ if ( Ga_Helper::is_dashboard_page() ) {
408
+ self::enqueue_dashboard_scripts();
409
+ }
410
+
411
+ if ( Ga_Helper::is_plugin_page() ) {
412
+ self::enqueue_ga_scripts();
413
+ }
414
+ }
415
+
416
+ /**
417
+ * Prepares plugin's statistics page and return HTML code.
418
+ *
419
+ * @return string HTML code
420
+ */
421
+ public static function get_stats_page() {
422
+ $chart = null;
423
+ $boxes = null;
424
+ $labels = null;
425
+ $sources = null;
426
+ if ( Ga_Helper::is_authorized() && Ga_Helper::is_account_selected() && ! Ga_Helper::is_all_feature_disabled() ) {
427
+ list( $chart, $boxes, $labels, $sources ) = self::generate_stats_data();
428
+ }
429
+
430
+ return Ga_Helper::get_chart_page(
431
+ 'stats',
432
+ array(
433
+ 'chart' => $chart,
434
+ 'boxes' => $boxes,
435
+ 'labels' => $labels,
436
+ 'sources' => $sources,
437
+ )
438
+ );
439
+ }
440
+
441
+ /**
442
+ * Shows plugin's notice on the admin area.
443
+ */
444
+ public static function admin_notice_googleanalytics() {
445
+ 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 ) ) ) {
446
+ $current_url = Ga_Helper::get_current_url();
447
+ $url = ( strstr( $current_url, '?' ) ? $current_url . '&' : $current_url . '?' ) . http_build_query( array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_update_terms' ) );
448
+ Ga_View_Core::load( 'ga_notice', [ 'url' => $url ] );
449
+ }
450
+
451
+ if ( Ga_Helper::get_option( self::GA_DISABLE_ALL_FEATURES ) ) {
452
+ echo wp_kses_post(
453
+ Ga_Helper::ga_wp_notice(
454
+ __( 'You have disabled all extra features, click here to enable Dashboards, Viral Alerts and Google API.' ),
455
+ 'warning',
456
+ false,
457
+ array(
458
+ 'url' => admin_url( Ga_Helper::create_url( Ga_Helper::GA_SETTINGS_PAGE_URL, array( Ga_Controller_Core::ACTION_PARAM_NAME => 'ga_action_enable_all_features' ) ) ),
459
+ 'label' => __( 'Enable' ),
460
+ )
461
+ )
462
+ );
463
+ }
464
+ }
465
+
466
+ /**
467
+ * Prepare required PHP version warning.
468
+ * @return string
469
+ */
470
+ public static function admin_notice_googleanalytics_php_version() {
471
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Cannot use Google Analytics plugin. PHP version ' . phpversion() . ' is to low. Required PHP version: ' . Ga_Helper::PHP_VERSION_REQUIRED, 'error' ) );
472
+ }
473
+
474
+ /**
475
+ * Prepare required WP version warning
476
+ * @return string
477
+ */
478
+ public static function admin_notice_googleanalytics_wp_version() {
479
+ echo wp_kses_post( Ga_Helper::ga_wp_notice( 'Google Analytics plugin requires at least WordPress version ' . self::MIN_WP_VERSION, 'error' ) );
480
+ }
481
+
482
+ /**
483
+ * Hides plugin's notice
484
+ */
485
+ public static function admin_notice_hide_googleanalytics() {
486
+ update_option( self::GA_HIDE_TERMS_OPTION_NAME, true );
487
+ }
488
+
489
+ /**
490
+ * Adds GA dashboard widget only for administrators.
491
+ */
492
+ public static function add_dashboard_device_widget() {
493
+ if ( Ga_Helper::is_administrator() ) {
494
+ wp_add_dashboard_widget( 'ga_dashboard_widget', __( 'Google Analytics Dashboard' ), 'Ga_Helper::add_ga_dashboard_widget' );
495
+ }
496
+ }
497
+
498
+ /**
499
+ * Adds plugin's actions
500
+ */
501
+ public static function add_actions() {
502
+ add_action( 'admin_init', 'Ga_Admin::admin_init_googleanalytics' );
503
+ add_action( 'admin_menu', 'Ga_Admin::admin_menu_googleanalytics' );
504
+ add_action( 'admin_enqueue_scripts', 'Ga_Admin::enqueue_scripts' );
505
+ add_action( 'wp_dashboard_setup', 'Ga_Admin::add_dashboard_device_widget' );
506
+ add_action( 'wp_ajax_ga_ajax_data_change', 'Ga_Admin::ga_ajax_data_change' );
507
+ add_action( 'wp_ajax_ga_ajax_hide_review', 'Ga_Admin::ga_ajax_hide_review' );
508
+ add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics' );
509
+ add_action( 'heartbeat_tick', 'Ga_Admin::run_heartbeat_jobs' );
510
+ add_action( 'wp_ajax_googleanalytics_send_debug_email', 'Ga_SupportLogger::send_email' );
511
+ add_action( 'wp_ajax_set_ga_credentials', 'Ga_Admin::createGAProperty' );
512
+ }
513
+
514
+ /**
515
+ * Runs jobs
516
+ *
517
+ * @param $response
518
+ * @param $screen_id
519
+ */
520
+ public static function run_heartbeat_jobs( $response, $screen_id = '' ) {
521
+
522
+ if ( self::GA_HEARTBEAT_API_CACHE_UPDATE ) {
523
+ // Disable cache for ajax request
524
+ self::api_client()->set_disable_cache( true );
525
+
526
+ // Try to regenerate cache if needed
527
+ self::generate_stats_data();
528
+ }
529
+ }
530
+
531
+ /**
532
+ * Adds plugin's filters
533
+ */
534
+ public static function add_filters() {
535
+ add_filter( 'plugin_action_links', 'Ga_Admin::ga_action_links', 10, 5 );
536
+ }
537
+
538
+ /**
539
+ * Adds new action links on the plugin list.
540
+ *
541
+ * @param $actions
542
+ * @param $plugin_file
543
+ *
544
+ * @return mixed
545
+ */
546
+ public static function ga_action_links( $actions, $plugin_file ) {
547
+
548
+ if ( basename( $plugin_file ) === GA_NAME . '.php' ) {
549
+ array_unshift( $actions, '<a href="' . esc_url( get_admin_url( null, Ga_Helper::GA_SETTINGS_PAGE_URL ) ) . '">' . __( 'Settings' ) . '</a>' );
550
+ }
551
+
552
+ return $actions;
553
+ }
554
+
555
+ public static function init_oauth() {
556
+
557
+ $code = Ga_Helper::get_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME );
558
+
559
+ if ( ! empty( $code ) ) {
560
+ Ga_Helper::update_option( self::GA_OAUTH_AUTH_CODE_OPTION_NAME, '' );
561
+
562
+ // Get access token
563
+ $response = self::api_client()->call( 'ga_auth_get_access_token', $code );
564
+ if ( empty( $response ) ) {
565
+ return false;
566
+ }
567
+ $param = '';
568
+ if ( ! self::save_access_token( $response ) ) {
569
+ $param = '&err=1';
570
+ $errors = self::api_client()->get_errors();
571
+ if ( ! empty( $errors ) ) {
572
+ foreach ( $errors as $error ) {
573
+ if ( 'invalid_grant' === $error['message'] ) {
574
+ $param = '&err=2';
575
+ }
576
+ }
577
+ }
578
+ } else {
579
+ self::api_client()->set_access_token( $response->getData() );
580
+ // Get accounts data
581
+ $account_summaries = self::api_client()->call( 'ga_api_account_summaries' );
582
+ self::save_ga_account_summaries( $account_summaries->getData() );
583
+ update_option( self::GA_SELECTED_ACCOUNT, '' );
584
+ }
585
+
586
+ wp_safe_redirect( admin_url( Ga_Helper::GA_SETTINGS_PAGE_URL . $param ) );
587
+ }
588
+ }
589
+
590
+ /**
591
+ * Save access token.
592
+ *
593
+ * @param Ga_Lib_Api_Response $response
594
+ *
595
+ * @return boolean
596
+ */
597
+ public static function save_access_token( $response, $refresh_token = '' ) {
598
+ $access_token = $response->getData();
599
+ if ( ! empty( $access_token ) ) {
600
+ $access_token['created'] = time();
601
+ } else {
602
+ return false;
603
+ }
604
+
605
+ if ( ! empty( $refresh_token ) ) {
606
+ $access_token['refresh_token'] = $refresh_token;
607
+ }
608
+
609
+ return update_option( self::GA_OAUTH_AUTH_TOKEN_OPTION_NAME, wp_json_encode( $access_token ) );
610
+ }
611
+
612
+ /**
613
+ * Saves Google Analytics account data.
614
+ *
615
+ * @param $data
616
+ *
617
+ * @return array
618
+ */
619
+ public static function save_ga_account_summaries( $data ) {
620
+ $return = array();
621
+ if ( ! empty( $data['items'] ) ) {
622
+ foreach ( $data['items'] as $item ) {
623
+ $tmp = array();
624
+ $tmp['id'] = $item['id'];
625
+ $tmp['name'] = $item['name'];
626
+ if ( is_array( $item['webProperties'] ) ) {
627
+ foreach ( $item['webProperties'] as $property ) {
628
+ $profiles = array();
629
+ if ( is_array( $property['profiles'] ) ) {
630
+ foreach ( $property['profiles'] as $profile ) {
631
+ $profiles[] = array(
632
+ 'id' => $profile['id'],
633
+ 'name' => $profile['name'],
634
+ );
635
+ }
636
+ }
637
+
638
+ $tmp['webProperties'][] = array(
639
+ 'internalWebPropertyId' => $property['internalWebPropertyId'],
640
+ 'webPropertyId' => $property['id'],
641
+ 'name' => $property['name'],
642
+ 'profiles' => $profiles,
643
+ );
644
+ }
645
+ }
646
+
647
+ $return[] = $tmp;
648
+ }
649
+
650
+ update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, wp_json_encode( $return ) );
651
+ } else {
652
+ update_option( self::GA_ACCOUNT_DATA_OPTION_NAME, '' );
653
+ }
654
+ update_option( self::GA_WEB_PROPERTY_ID_OPTION_NAME, '' );
655
+
656
+ return $return;
657
+ }
658
+
659
+ /**
660
+ * Handle AJAX data for the GA dashboard widget.
661
+ */
662
+ public static function ga_ajax_data_change() {
663
+ if ( Ga_Admin_Controller::validate_ajax_data_change_post( $_POST ) ) {
664
+ $date_range = ! empty( $_POST['date_range'] ) ? $_POST['date_range'] : null; // WPCS: CSRF ok.
665
+ $metric = ! empty( $_POST['metric'] ) ? $_POST['metric'] : null; // WPCS: CSRF ok.
666
+ echo wp_kses_post( Ga_Helper::get_ga_dashboard_widget_data_json( $date_range, $metric, false, true ) );
667
+ } else {
668
+ echo wp_json_encode( array( 'error' => __( 'Invalid request.' ) ) );
669
+ }
670
+
671
+ wp_die();
672
+ }
673
+
674
+ /**
675
+ * Displays API error messages.
676
+ */
677
+ public static function display_api_errors( $alias = '' ) {
678
+ $errors = self::api_client( $alias )->get_errors();
679
+ if ( ! empty( $errors ) ) {
680
+ foreach ( $errors as $error ) {
681
+ echo wp_kses_post( Ga_Notice::get_message( $error ) );
682
+ }
683
+ }
684
+ }
685
+
686
+ /**
687
+ * Gets dashboard data.
688
+ *
689
+ * @return array
690
+ */
691
+ public static function generate_stats_data() {
692
+ $selected = Ga_Helper::get_selected_account_data( true );
693
+
694
+ $query_params = isset( $_GET['th'] ) ? Ga_Stats::get_query( 'main_chart', $selected['view_id'], '30daysAgo' ) : Ga_Stats::get_query( 'main_chart', $selected['view_id'] );
695
+
696
+ $stats_data = self::api_client()->call(
697
+ 'ga_api_data',
698
+ [ $query_params ]
699
+ );
700
+
701
+ $boxes_data = self::api_client()->call(
702
+ 'ga_api_data',
703
+ [ Ga_Stats::get_query( 'boxes', $selected['view_id'] ) ]
704
+ );
705
+ $sources_data = self::api_client()->call(
706
+ 'ga_api_data',
707
+ [ Ga_Stats::get_query( 'sources', $selected['view_id'] ) ]
708
+ );
709
+ $chart = ! empty( $stats_data ) ? Ga_Stats::get_chart( $stats_data->getData() ) : array();
710
+ $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_boxes( $boxes_data->getData() ) : array();
711
+ $last_chart_date = ! empty( $chart ) ? $chart['date'] : strtotime( 'now' );
712
+ unset( $chart['date'] );
713
+ $labels = array(
714
+ 'thisWeek' => date( 'M d, Y', strtotime( '-6 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
715
+ 'thisMonth' => date( 'M d, Y', strtotime( '-29 day', $last_chart_date ) ) . ' - ' . date( 'M d, Y', $last_chart_date ),
716
+ );
717
+ $sources = ! empty( $sources_data ) ? Ga_Stats::get_sources( $sources_data->getData() ) : array();
718
+
719
+ return array( $chart, $boxes, $labels, $sources );
720
+ }
721
+
722
+ /**
723
+ * Returns auth or re-auth button
724
+ *
725
+ * @return string
726
+ */
727
+ public static function get_auth_button( $type ) {
728
+
729
+ return Ga_View_Core::load(
730
+ 'ga_auth_button',
731
+ [
732
+ 'label' => 'auth' === $type ? 'Authenticate with Google' : 'Re-authenticate with Google',
733
+ 'type' => $type,
734
+ 'url' => self::get_auth_popup_url(),
735
+ 'manually_id' => get_option( self::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME ),
736
+ ],
737
+ true
738
+ );
739
+ }
740
+
741
+ /**
742
+ * Returns debug modal
743
+ *
744
+ * @return string
745
+ */
746
+ public static function get_debug_modal() {
747
+
748
+ return Ga_View_Core::load(
749
+ 'ga_debug_modal',
750
+ [ 'debug_info' => Ga_SupportLogger::$debug_info, 'debug_help_message' => Ga_SupportLogger::$debug_help_message ],
751
+ true
752
+ );
753
+ }
754
+
755
+ public static function ga_ajax_hide_review( $post ) {
756
+ $error = 0;
757
+
758
+ if ( Ga_Controller_Core::verify_nonce( 'ga_ajax_data_change' ) ) {
759
+ update_option('googleanalytics-hide-review', true);
760
+ }
761
+
762
+ wp_send_json_success('hidden');
763
+ }
764
+
765
+ /**
766
+ * New property creation method.
767
+ */
768
+ public static function createGAProperty() {
769
+ check_ajax_referer( 'googleanalyticsnonce', 'nonce' );
770
+
771
+ if (! isset($_POST['propid'], $_POST['secret'])) { // WPCS: input var ok.
772
+ wp_send_json_error( 'Set credentials failed.' );
773
+ }
774
+
775
+ $secret = sanitize_text_field( wp_unslash( $_POST['secret'] ) ); // WPCS: input var ok.
776
+ $propid = sanitize_text_field( wp_unslash( $_POST['propid'] ) ); // WPCS: input var ok.
777
+
778
+ update_option(self::GA_SHARETHIS_PROPERTY_ID, $propid);
779
+ update_option(self::GA_SHARETHIS_PROPERTY_SECRET, $secret);
780
+ }
781
+ }
class/Ga_Helper.php CHANGED
@@ -1,587 +1,587 @@
1
- <?php
2
-
3
- class Ga_Helper {
4
-
5
- const ROLE_ID_PREFIX = 'role-id-';
6
- const GA_DEFAULT_WEB_ID = 'UA-0000000-0';
7
- const GA_STATISTICS_PAGE_URL = 'admin.php?page=googleanalytics';
8
- const GA_SETTINGS_PAGE_URL = 'admin.php?page=googleanalytics/settings';
9
- const DASHBOARD_PAGE_NAME = 'dashboard';
10
- const PHP_VERSION_REQUIRED = '5.2.17';
11
- const GA_WP_MODERN_VERSION = '4.1';
12
- const GA_TOOLTIP_TERMS_NOT_ACCEPTED = 'Please accept the terms to use this feature.';
13
- const GA_TOOLTIP_FEATURES_DISABLED = 'Click the Enable button at the top to start using this feature.';
14
- const GA_DEBUG_MODE = false;
15
-
16
- /**
17
- * Init plugin actions.
18
- *
19
- */
20
- public static function init() {
21
-
22
- // Displays errors related to required PHP version
23
- if ( ! self::is_php_version_valid() ) {
24
- add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_php_version' );
25
-
26
- return false;
27
- }
28
-
29
- // Displays errors related to required WP version
30
- if ( ! self::is_wp_version_valid() ) {
31
- add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_wp_version' );
32
-
33
- return false;
34
- }
35
-
36
- if ( ! is_admin() ) {
37
- Ga_Frontend::add_actions();
38
-
39
- $frontend_controller = new Ga_Frontend_Controller();
40
- $frontend_controller->handle_actions();
41
- }
42
-
43
- if ( is_admin() ) {
44
- Ga_Admin::add_filters();
45
- Ga_Admin::add_actions();
46
- Ga_Admin::init_oauth();
47
-
48
-
49
- $admin_controller = new Ga_Admin_Controller();
50
- $admin_controller->handle_actions();
51
- }
52
- }
53
-
54
- /**
55
- * Checks if current page is a WordPress dashboard.
56
- * @return int
57
- */
58
- public static function is_plugin_page() {
59
- $site = get_current_screen();
60
-
61
- return preg_match( '/' . GA_NAME . '/i', $site->base ) || preg_match( '/' . GA_NAME . '/i', $_SERVER['REQUEST_URI'] );
62
- }
63
-
64
- /**
65
- * Checks if current page is a WordPress dashboard.
66
- * @return number
67
- */
68
- public static function is_dashboard_page() {
69
- $site = get_current_screen();
70
-
71
- return preg_match( '/' . self::DASHBOARD_PAGE_NAME . '/', $site->base );
72
- }
73
-
74
- /**
75
- * Check whether the plugin is configured.
76
- *
77
- * @param String $web_id
78
- *
79
- * @return boolean
80
- */
81
- public static function is_configured( $web_id ) {
82
- return ( self::GA_DEFAULT_WEB_ID !== $web_id ) && ! empty( $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
- global $wp_roles;
92
- if ( ! isset( $wp_roles ) ) {
93
- $wp_roles = new WP_Roles();
94
- }
95
-
96
- return $wp_roles->get_names();
97
- }
98
-
99
- /**
100
- * Prepare a role ID.
101
- *
102
- * The role ID is derived from the role's name and will be used
103
- * in its setting name in the additional settings.
104
- *
105
- * @param string $role_name Role name
106
- *
107
- * @return string
108
- */
109
- public static function prepare_role_id( $role_name ) {
110
- return self::ROLE_ID_PREFIX . strtolower( preg_replace( '/[\W]/', '-', before_last_bar( $role_name ) ) );
111
- }
112
-
113
- /**
114
- * Prepares role id.
115
- *
116
- * @param $v
117
- * @param $k
118
- */
119
- public static function prepare_role( &$v, $k ) {
120
- $v = self::prepare_role_id( $v );
121
- }
122
-
123
- /**
124
- * Checks whether user role is excluded from adding UA code.
125
- *
126
- * @return boolean
127
- */
128
- public static function can_add_ga_code() {
129
- $current_user = wp_get_current_user();
130
- $user_roles = !empty( $current_user->roles ) ? $current_user->roles : array();
131
- $exclude_roles = json_decode( get_option( Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
132
-
133
- array_walk( $user_roles, 'Ga_Helper::prepare_role' );
134
-
135
- $return = true;
136
- foreach ( $user_roles as $role ) {
137
- if ( !empty( $exclude_roles[ $role ] ) ) {
138
- $return = false;
139
- break;
140
- }
141
- }
142
-
143
- return $return;
144
- }
145
-
146
- /**
147
- * Adds ga dashboard widget HTML code for a WordPress
148
- * Dashboard widget hook.
149
- */
150
- public static function add_ga_dashboard_widget() {
151
- echo self::get_ga_dashboard_widget( null, false, false, true );
152
- }
153
-
154
- /**
155
- * Generates dashboard widget HTML code.
156
- *
157
- * @param string $date_range Google Analytics specific date range string.
158
- * @param boolean $text_mode
159
- * @param boolean $ajax
160
- *
161
- * @return null | string HTML dashboard widget code.
162
- */
163
- public static function get_ga_dashboard_widget( $date_range = null, $text_mode = false, $ajax = false, $trigger_request = false ) {
164
- if ( empty( $date_range ) ) {
165
- $date_range = '30daysAgo';
166
- }
167
-
168
- if ( !$trigger_request ) {
169
- // Get chart and boxes data
170
- $data = self::get_dashboard_widget_data( $date_range );
171
-
172
- if ( $text_mode ) {
173
- return self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
174
- 'chart' => $data[ 'chart' ],
175
- 'boxes' => $data[ 'boxes' ]
176
- ) );
177
- } else {
178
- echo self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
179
- 'chart' => $data[ 'chart' ],
180
- 'boxes' => $data[ 'boxes' ],
181
- 'more_details_url' => admin_url( self::GA_STATISTICS_PAGE_URL ),
182
- 'ga_nonce' => wp_create_nonce( 'ga_ajax_data_change' ),
183
- 'ga_nonce_name' => Ga_Admin_Controller::GA_NONCE_FIELD_NAME
184
- ) );
185
- }
186
- } else {
187
- echo self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
188
- 'chart' => array(),
189
- 'boxes' => Ga_Stats::get_empty_boxes_structure(),
190
- 'more_details_url' => admin_url( self::GA_STATISTICS_PAGE_URL ),
191
- 'show_trigger_button' => true,
192
- 'ga_nonce' => wp_create_nonce( 'ga_ajax_data_change' ),
193
- 'ga_nonce_name' => Ga_Admin_Controller::GA_NONCE_FIELD_NAME
194
- ) );
195
- }
196
-
197
- return null;
198
- }
199
-
200
- /**
201
- * Generates JSON data string for AJAX calls.
202
- *
203
- * @param string $date_range
204
- * @param string $metric
205
- * @param boolean $text_mode
206
- * @param boolean $ajax
207
- *
208
- * @return string|false Returns JSON data string
209
- */
210
- public static function get_ga_dashboard_widget_data_json(
211
- $date_range = null, $metric = null, $text_mode = false, $ajax = false
212
- ) {
213
- if ( empty( $date_range ) ) {
214
- $date_range = '30daysAgo';
215
- }
216
-
217
- if ( empty( $metric ) ) {
218
- $metric = 'pageviews';
219
- }
220
-
221
- $data = self::get_dashboard_widget_data( $date_range, $metric );
222
-
223
- return wp_json_encode( $data );
224
- }
225
-
226
- /**
227
- * Gets dashboard widget data.
228
- *
229
- * @param date_range
230
- * @param metric
231
- *
232
- * @return array Return chart and boxes data
233
- */
234
- private static function get_dashboard_widget_data( $date_range, $metric = null ) {
235
- $selected = self::get_selected_account_data( true );
236
- if ( self::is_authorized() && self::is_account_selected() ) {
237
- $query_params = Ga_Stats::get_query( 'main_chart', $selected[ 'view_id' ], $date_range, $metric );
238
- $stats_data = Ga_Admin::api_client()->call( 'ga_api_data', array(
239
- $query_params
240
- ) );
241
-
242
- $boxes_query = Ga_Stats::get_query( 'dashboard_boxes', $selected[ 'view_id' ], $date_range );
243
- $boxes_data = Ga_Admin::api_client()->call( 'ga_api_data', array(
244
- $boxes_query
245
- ) );
246
- }
247
- $chart = !empty( $stats_data ) ? Ga_Stats::get_dashboard_chart( $stats_data->getData() ) : array();
248
- $boxes = !empty( $boxes_data ) ? Ga_Stats::get_dashboard_boxes_data( $boxes_data->getData() ) : array();
249
-
250
- return array(
251
- 'chart' => $chart,
252
- 'boxes' => $boxes
253
- );
254
- }
255
-
256
- public static function is_account_selected() {
257
- return self::get_selected_account_data();
258
- }
259
-
260
- /**
261
- * Returns HTML code of the chart page or a notice.
262
- *
263
- * @param chart
264
- *
265
- * @return string Returns HTML code
266
- */
267
- public static function get_chart_page( $view, $params ) {
268
-
269
- $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 ) );
270
-
271
- if ( self::is_authorized() && !self::is_code_manually_enabled() && !self::is_all_feature_disabled() ) {
272
- if ( self::is_account_selected() ) {
273
- if ( $params ) {
274
- return Ga_View_Core::load( $view, $params, true );
275
- } else {
276
- return self::ga_oauth_notice( sprintf( 'Please configure your <a href="%s">Google Analytics settings</a>.', admin_url( self::GA_SETTINGS_PAGE_URL ) ) );
277
- }
278
- } else {
279
- return self::ga_oauth_notice( $message );
280
- }
281
- } else {
282
- return self::ga_oauth_notice( $message );
283
- }
284
- }
285
-
286
- /**
287
- * Checks whether users is authorized with Google.
288
- *
289
- * @return boolean
290
- */
291
- public static function is_authorized() {
292
- return Ga_Admin::api_client()->get_instance()->is_authorized();
293
- }
294
-
295
- /**
296
- * Wrapper for WordPress method get_option
297
- *
298
- * @param string $name Option name
299
- *
300
- * @return NULL|mixed|boolean
301
- */
302
- public static function get_option( $name ) {
303
- $opt = get_option( $name );
304
-
305
- return !empty( $opt ) ? $opt : null;
306
- }
307
-
308
- /**
309
- * Wrapper for WordPress method update_option
310
- *
311
- * @param string $name
312
- * @param mixed $value
313
- *
314
- * @return NULL|boolean
315
- */
316
- public static function update_option( $name, $value ) {
317
- $opt = update_option( $name, $value );
318
-
319
- return !empty( $opt ) ? $opt : null;
320
- }
321
-
322
- /**
323
- * Loads ga notice HTML code with given message included.
324
- *
325
- * @param string $message
326
- * $param bool $cannot_activate Whether the plugin cannot be activated
327
- *
328
- * @return string
329
- */
330
- public static function ga_oauth_notice( $message ) {
331
- return Ga_View_Core::load( 'ga_oauth_notice', array(
332
- 'msg' => $message
333
- ), true );
334
- }
335
-
336
- /**
337
- * Displays notice following the WP style.
338
- *
339
- * @param $message
340
- * @param string $type
341
- * @param $is_dismissable
342
- * @param $action
343
- *
344
- * @return string
345
- */
346
- public static function ga_wp_notice( $message, $type = '', $is_dismissable = false, $action = array() ) {
347
- return Ga_View_Core::load( 'ga_wp_notice', array(
348
- 'type' => empty( $type ) ? Ga_Admin::NOTICE_WARNING : $type,
349
- 'msg' => $message,
350
- 'is_dismissable' => $is_dismissable,
351
- 'action' => $action
352
- ), true );
353
- }
354
-
355
- /**
356
- * Gets data according to selected GA account.
357
- *
358
- * @param boolean $assoc
359
- *
360
- * @return mixed
361
- */
362
- public static function get_selected_account_data( $assoc = false ) {
363
- $data = json_decode( self::get_option( Ga_Admin::GA_SELECTED_ACCOUNT ) );
364
- $data = (!empty( $data ) && count( $data ) == 3 ) ? $data : false;
365
-
366
- if ( $data ) {
367
- if ( $assoc ) {
368
- return array(
369
- 'account_id' => $data[ 0 ],
370
- 'web_property_id' => $data[ 1 ],
371
- 'view_id' => $data[ 2 ]
372
- );
373
- } else {
374
- return $data;
375
- }
376
- }
377
-
378
- return false;
379
- }
380
-
381
- /**
382
- * Chekcs whether option for manually UA-code
383
- * @return NULL|mixed|boolean
384
- */
385
- public static function is_code_manually_enabled() {
386
- return Ga_Helper::get_option( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
387
- }
388
-
389
- /**
390
- * Adds percent sign to the given text.
391
- *
392
- * @param $text
393
- *
394
- * @return string
395
- */
396
- public static function format_percent( $text ) {
397
- $text = self::add_plus( $text );
398
-
399
- return $text . '%';
400
- }
401
-
402
- /**
403
- * Adds plus sign before number.
404
- *
405
- * @param $number
406
- *
407
- * @return string
408
- */
409
- public static function add_plus( $number ) {
410
- if ( $number > 0 ) {
411
- return '+' . $number;
412
- }
413
-
414
- return $number;
415
- }
416
-
417
- /**
418
- * Check whether current user has administrator privileges.
419
- *
420
- * @return bool
421
- */
422
- public static function is_administrator() {
423
- if ( current_user_can( 'administrator' ) ) {
424
- return true;
425
- }
426
-
427
- return false;
428
- }
429
-
430
- public static function is_wp_version_valid() {
431
- $wp_version = get_bloginfo( 'version' );
432
-
433
- return version_compare( $wp_version, Ga_Admin::MIN_WP_VERSION, 'ge' );
434
- }
435
-
436
- /**
437
- * Check if terms are accepted
438
- *
439
- * @return bool
440
- */
441
- public static function are_terms_accepted() {
442
- return self::get_option( Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME );
443
- }
444
-
445
- /**
446
- * Check if sharethis scripts enabled
447
- *
448
- * @return bool
449
- */
450
- public static function is_sharethis_included() {
451
- return GA_SHARETHIS_SCRIPTS_INCLUDED;
452
- }
453
-
454
- /**
455
- * @return mixed
456
- */
457
- public static function is_php_version_valid() {
458
- $p = '#(\.0+)+($|-)#';
459
- $ver1 = preg_replace( $p, '', phpversion() );
460
- $ver2 = preg_replace( $p, '', self::PHP_VERSION_REQUIRED );
461
- $operator = 'ge';
462
- $compare = isset( $operator ) ?
463
- version_compare( $ver1, $ver2, $operator ) :
464
- version_compare( $ver1, $ver2 );
465
-
466
- return $compare;
467
- }
468
-
469
- public static function get_current_url() {
470
- return $_SERVER[ 'REQUEST_URI' ];
471
- }
472
-
473
- public static function create_url( $url, $data = array() ) {
474
- return !empty( $data ) ? ( strstr( $url, '?' ) ? ( $url . '&' ) : ( $url . '?' ) ) . http_build_query( $data ) : $url;
475
- }
476
-
477
- public static function handle_url_message( $data ) {
478
- if ( !empty( $_GET[ 'ga_msg' ] ) ) {
479
- $invite_result = json_decode( base64_decode( $_GET[ 'ga_msg' ] ), true );
480
- if ( !empty( $invite_result[ 'status' ] ) && !empty( $invite_result[ 'message' ] ) ) {
481
- $data[ 'ga_msg' ] = Ga_Helper::ga_wp_notice( $invite_result[ 'message' ], $invite_result[ 'status' ], true );
482
- }
483
- }
484
-
485
- return $data;
486
- }
487
-
488
- public static function get_url_message_text() {
489
- $ga_msg = '';
490
- $invite_result = json_decode( base64_decode( $_GET[ 'ga_msg' ] ), true );
491
- if ( !empty( $invite_result[ 'status' ] ) && !empty( $invite_result[ 'message' ] ) ) {
492
- $ga_msg = $invite_result[ 'message' ];
493
- }
494
-
495
- return $ga_msg;
496
- }
497
-
498
- /**
499
- * Create base64 url message
500
- *
501
- * @param $msg
502
- * @param $status
503
- *
504
- * @return string
505
- */
506
- public static function create_url_msg( $msg, $status ) {
507
- $msg = array( 'status' => $status, 'message' => $msg );
508
-
509
- return base64_encode( json_encode( $msg ) );
510
- }
511
-
512
- public static function is_all_feature_disabled() {
513
- return self::get_option( Ga_Admin::GA_DISABLE_ALL_FEATURES );
514
- }
515
-
516
- public static function are_features_enabled() {
517
- return self::are_terms_accepted() && !self::is_all_feature_disabled();
518
- }
519
-
520
- public static function are_sharethis_properties_verified() {
521
- return ( get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT ) && self::are_sharethis_properties_set() );
522
- }
523
-
524
- public static function are_sharethis_properties_ready_to_verify() {
525
- return ( self::are_sharethis_properties_set() && !get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT ) );
526
- }
527
-
528
- public static function are_sharethis_properties_set() {
529
- return ( get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ) && get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET ) );
530
- }
531
-
532
- public static function should_create_sharethis_property() {
533
- return ( self::are_features_enabled() && !self::are_sharethis_properties_set() );
534
- }
535
-
536
- public static function should_verify_sharethis_installation() {
537
- return ( self::are_features_enabled() && self::are_sharethis_properties_ready_to_verify() );
538
- }
539
-
540
- public static function get_tooltip() {
541
- if ( !self::are_terms_accepted() ) {
542
- return self::GA_TOOLTIP_TERMS_NOT_ACCEPTED;
543
- } else if ( !self::are_features_enabled() ) {
544
- return self::GA_TOOLTIP_FEATURES_DISABLED;
545
- } else {
546
- return '';
547
- }
548
- }
549
-
550
- public static function is_wp_old() {
551
- return version_compare( get_bloginfo( 'version' ), self::GA_WP_MODERN_VERSION, 'lt' );
552
- }
553
-
554
- public static function should_load_ga_javascript( $web_property_id ) {
555
- return ( self::is_configured( $web_property_id ) && ( self::can_add_ga_code() || self::is_all_feature_disabled() ) );
556
- }
557
-
558
- /**
559
- * @return string
560
- */
561
- public static function get_account_id() {
562
- $account_id = json_decode( Ga_Helper::get_option( Ga_Admin::GA_SELECTED_ACCOUNT ) );
563
-
564
- return ! empty( $account_id[0] ) ? $account_id[0] : '';
565
- }
566
-
567
- public static function is_curl_disabled(){
568
- return ! function_exists( 'curl_version' );
569
- }
570
-
571
-
572
- public static function get_plugin_url_with_correct_protocol() {
573
- $url = parse_url( GA_PLUGIN_URL );
574
- return ( ( is_ssl() ) ? 'https://' : 'http://' ) . $url['host'] . $url['path'];
575
- }
576
-
577
- public static function get_code_manually_label_classes() {
578
- $classes = '';
579
- if ( ! self::are_features_enabled() ){
580
- $classes = 'label-grey ga-tooltip';
581
- }
582
- else if( self::is_account_selected() ) {
583
- $classes = 'label-grey';
584
- }
585
- return $classes;
586
- }
587
- }
1
+ <?php
2
+
3
+ class Ga_Helper {
4
+
5
+ const ROLE_ID_PREFIX = 'role-id-';
6
+ const GA_DEFAULT_WEB_ID = 'UA-0000000-0';
7
+ const GA_STATISTICS_PAGE_URL = 'admin.php?page=googleanalytics';
8
+ const GA_SETTINGS_PAGE_URL = 'admin.php?page=googleanalytics/settings';
9
+ const DASHBOARD_PAGE_NAME = 'dashboard';
10
+ const PHP_VERSION_REQUIRED = '5.2.17';
11
+ const GA_WP_MODERN_VERSION = '4.1';
12
+ const GA_TOOLTIP_TERMS_NOT_ACCEPTED = 'Please accept the terms to use this feature.';
13
+ const GA_TOOLTIP_FEATURES_DISABLED = 'Click the Enable button at the top to start using this feature.';
14
+ const GA_DEBUG_MODE = false;
15
+
16
+ /**
17
+ * Init plugin actions.
18
+ *
19
+ */
20
+ public static function init() {
21
+
22
+ // Displays errors related to required PHP version
23
+ if ( ! self::is_php_version_valid() ) {
24
+ add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_php_version' );
25
+
26
+ return false;
27
+ }
28
+
29
+ // Displays errors related to required WP version
30
+ if ( ! self::is_wp_version_valid() ) {
31
+ add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_wp_version' );
32
+
33
+ return false;
34
+ }
35
+
36
+ if ( ! is_admin() ) {
37
+ Ga_Frontend::add_actions();
38
+
39
+ $frontend_controller = new Ga_Frontend_Controller();
40
+ $frontend_controller->handle_actions();
41
+ }
42
+
43
+ if ( is_admin() ) {
44
+ Ga_Admin::add_filters();
45
+ Ga_Admin::add_actions();
46
+ Ga_Admin::init_oauth();
47
+
48
+
49
+ $admin_controller = new Ga_Admin_Controller();
50
+ $admin_controller->handle_actions();
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Checks if current page is a WordPress dashboard.
56
+ * @return int
57
+ */
58
+ public static function is_plugin_page() {
59
+ $site = get_current_screen();
60
+
61
+ return preg_match( '/' . GA_NAME . '/i', $site->base ) || preg_match( '/' . GA_NAME . '/i', $_SERVER['REQUEST_URI'] );
62
+ }
63
+
64
+ /**
65
+ * Checks if current page is a WordPress dashboard.
66
+ * @return number
67
+ */
68
+ public static function is_dashboard_page() {
69
+ $site = get_current_screen();
70
+
71
+ return preg_match( '/' . self::DASHBOARD_PAGE_NAME . '/', $site->base );
72
+ }
73
+
74
+ /**
75
+ * Check whether the plugin is configured.
76
+ *
77
+ * @param String $web_id
78
+ *
79
+ * @return boolean
80
+ */
81
+ public static function is_configured( $web_id ) {
82
+ return ( self::GA_DEFAULT_WEB_ID !== $web_id ) && ! empty( $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
+ global $wp_roles;
92
+ if ( ! isset( $wp_roles ) ) {
93
+ $wp_roles = new WP_Roles();
94
+ }
95
+
96
+ return $wp_roles->get_names();
97
+ }
98
+
99
+ /**
100
+ * Prepare a role ID.
101
+ *
102
+ * The role ID is derived from the role's name and will be used
103
+ * in its setting name in the additional settings.
104
+ *
105
+ * @param string $role_name Role name
106
+ *
107
+ * @return string
108
+ */
109
+ public static function prepare_role_id( $role_name ) {
110
+ return self::ROLE_ID_PREFIX . strtolower( preg_replace( '/[\W]/', '-', before_last_bar( $role_name ) ) );
111
+ }
112
+
113
+ /**
114
+ * Prepares role id.
115
+ *
116
+ * @param $v
117
+ * @param $k
118
+ */
119
+ public static function prepare_role( &$v, $k ) {
120
+ $v = self::prepare_role_id( $v );
121
+ }
122
+
123
+ /**
124
+ * Checks whether user role is excluded from adding UA code.
125
+ *
126
+ * @return boolean
127
+ */
128
+ public static function can_add_ga_code() {
129
+ $current_user = wp_get_current_user();
130
+ $user_roles = !empty( $current_user->roles ) ? $current_user->roles : array();
131
+ $exclude_roles = json_decode( get_option( Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
132
+
133
+ array_walk( $user_roles, 'Ga_Helper::prepare_role' );
134
+
135
+ $return = true;
136
+ foreach ( $user_roles as $role ) {
137
+ if ( !empty( $exclude_roles[ $role ] ) ) {
138
+ $return = false;
139
+ break;
140
+ }
141
+ }
142
+
143
+ return $return;
144
+ }
145
+
146
+ /**
147
+ * Adds ga dashboard widget HTML code for a WordPress
148
+ * Dashboard widget hook.
149
+ */
150
+ public static function add_ga_dashboard_widget() {
151
+ echo self::get_ga_dashboard_widget( null, false, false, true );
152
+ }
153
+
154
+ /**
155
+ * Generates dashboard widget HTML code.
156
+ *
157
+ * @param string $date_range Google Analytics specific date range string.
158
+ * @param boolean $text_mode
159
+ * @param boolean $ajax
160
+ *
161
+ * @return null | string HTML dashboard widget code.
162
+ */
163
+ public static function get_ga_dashboard_widget( $date_range = null, $text_mode = false, $ajax = false, $trigger_request = false ) {
164
+ if ( empty( $date_range ) ) {
165
+ $date_range = '30daysAgo';
166
+ }
167
+
168
+ if ( !$trigger_request ) {
169
+ // Get chart and boxes data
170
+ $data = self::get_dashboard_widget_data( $date_range );
171
+
172
+ if ( $text_mode ) {
173
+ return self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
174
+ 'chart' => $data[ 'chart' ],
175
+ 'boxes' => $data[ 'boxes' ]
176
+ ) );
177
+ } else {
178
+ echo self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
179
+ 'chart' => $data[ 'chart' ],
180
+ 'boxes' => $data[ 'boxes' ],
181
+ 'more_details_url' => admin_url( self::GA_STATISTICS_PAGE_URL ),
182
+ 'ga_nonce' => wp_create_nonce( 'ga_ajax_data_change' ),
183
+ 'ga_nonce_name' => Ga_Admin_Controller::GA_NONCE_FIELD_NAME
184
+ ) );
185
+ }
186
+ } else {
187
+ echo self::get_chart_page( 'ga_dashboard_widget' . ( $ajax ? "_ajax" : "" ), array(
188
+ 'chart' => array(),
189
+ 'boxes' => Ga_Stats::get_empty_boxes_structure(),
190
+ 'more_details_url' => admin_url( self::GA_STATISTICS_PAGE_URL ),
191
+ 'show_trigger_button' => true,
192
+ 'ga_nonce' => wp_create_nonce( 'ga_ajax_data_change' ),
193
+ 'ga_nonce_name' => Ga_Admin_Controller::GA_NONCE_FIELD_NAME
194
+ ) );
195
+ }
196
+
197
+ return null;
198
+ }
199
+
200
+ /**
201
+ * Generates JSON data string for AJAX calls.
202
+ *
203
+ * @param string $date_range
204
+ * @param string $metric
205
+ * @param boolean $text_mode
206
+ * @param boolean $ajax
207
+ *
208
+ * @return string|false Returns JSON data string
209
+ */
210
+ public static function get_ga_dashboard_widget_data_json(
211
+ $date_range = null, $metric = null, $text_mode = false, $ajax = false
212
+ ) {
213
+ if ( empty( $date_range ) ) {
214
+ $date_range = '30daysAgo';
215
+ }
216
+
217
+ if ( empty( $metric ) ) {
218
+ $metric = 'pageviews';
219
+ }
220
+
221
+ $data = self::get_dashboard_widget_data( $date_range, $metric );
222
+
223
+ return wp_json_encode( $data );
224
+ }
225
+
226
+ /**
227
+ * Gets dashboard widget data.
228
+ *
229
+ * @param date_range
230
+ * @param metric
231
+ *
232
+ * @return array Return chart and boxes data
233
+ */
234
+ private static function get_dashboard_widget_data( $date_range, $metric = null ) {
235
+ $selected = self::get_selected_account_data( true );
236
+ if ( self::is_authorized() && self::is_account_selected() ) {
237
+ $query_params = Ga_Stats::get_query( 'main_chart', $selected[ 'view_id' ], $date_range, $metric );
238
+ $stats_data = Ga_Admin::api_client()->call( 'ga_api_data', array(
239
+ $query_params
240
+ ) );
241
+
242
+ $boxes_query = Ga_Stats::get_query( 'dashboard_boxes', $selected[ 'view_id' ], $date_range );
243
+ $boxes_data = Ga_Admin::api_client()->call( 'ga_api_data', array(
244
+ $boxes_query
245
+ ) );
246
+ }
247
+ $chart = !empty( $stats_data ) ? Ga_Stats::get_dashboard_chart( $stats_data->getData() ) : array();
248
+ $boxes = !empty( $boxes_data ) ? Ga_Stats::get_dashboard_boxes_data( $boxes_data->getData() ) : array();
249
+
250
+ return array(
251
+ 'chart' => $chart,
252
+ 'boxes' => $boxes
253
+ );
254
+ }
255
+
256
+ public static function is_account_selected() {
257
+ return self::get_selected_account_data();
258
+ }
259
+
260
+ /**
261
+ * Returns HTML code of the chart page or a notice.
262
+ *
263
+ * @param chart
264
+ *
265
+ * @return string Returns HTML code
266
+ */
267
+ public static function get_chart_page( $view, $params ) {
268
+
269
+ $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 ) );
270
+
271
+ if ( self::is_authorized() && !self::is_code_manually_enabled() && !self::is_all_feature_disabled() ) {
272
+ if ( self::is_account_selected() ) {
273
+ if ( $params ) {
274
+ return Ga_View_Core::load( $view, $params, true );
275
+ } else {
276
+ return self::ga_oauth_notice( sprintf( 'Please configure your <a href="%s">Google Analytics settings</a>.', admin_url( self::GA_SETTINGS_PAGE_URL ) ) );
277
+ }
278
+ } else {
279
+ return self::ga_oauth_notice( $message );
280
+ }
281
+ } else {
282
+ return self::ga_oauth_notice( $message );
283
+ }
284
+ }
285
+
286
+ /**
287
+ * Checks whether users is authorized with Google.
288
+ *
289
+ * @return boolean
290
+ */
291
+ public static function is_authorized() {
292
+ return Ga_Admin::api_client()->get_instance()->is_authorized();
293
+ }
294
+
295
+ /**
296
+ * Wrapper for WordPress method get_option
297
+ *
298
+ * @param string $name Option name
299
+ *
300
+ * @return NULL|mixed|boolean
301
+ */
302
+ public static function get_option( $name ) {
303
+ $opt = get_option( $name );
304
+
305
+ return !empty( $opt ) ? $opt : null;
306
+ }
307
+
308
+ /**
309
+ * Wrapper for WordPress method update_option
310
+ *
311
+ * @param string $name
312
+ * @param mixed $value
313
+ *
314
+ * @return NULL|boolean
315
+ */
316
+ public static function update_option( $name, $value ) {
317
+ $opt = update_option( $name, $value );
318
+
319
+ return !empty( $opt ) ? $opt : null;
320
+ }
321
+
322
+ /**
323
+ * Loads ga notice HTML code with given message included.
324
+ *
325
+ * @param string $message
326
+ * $param bool $cannot_activate Whether the plugin cannot be activated
327
+ *
328
+ * @return string
329
+ */
330
+ public static function ga_oauth_notice( $message ) {
331
+ return Ga_View_Core::load( 'ga_oauth_notice', array(
332
+ 'msg' => $message
333
+ ), true );
334
+ }
335
+
336
+ /**
337
+ * Displays notice following the WP style.
338
+ *
339
+ * @param $message
340
+ * @param string $type
341
+ * @param $is_dismissable
342
+ * @param $action
343
+ *
344
+ * @return string
345
+ */
346
+ public static function ga_wp_notice( $message, $type = '', $is_dismissable = false, $action = array() ) {
347
+ return Ga_View_Core::load( 'ga_wp_notice', array(
348
+ 'type' => empty( $type ) ? Ga_Admin::NOTICE_WARNING : $type,
349
+ 'msg' => $message,
350
+ 'is_dismissable' => $is_dismissable,
351
+ 'action' => $action
352
+ ), true );
353
+ }
354
+
355
+ /**
356
+ * Gets data according to selected GA account.
357
+ *
358
+ * @param boolean $assoc
359
+ *
360
+ * @return mixed
361
+ */
362
+ public static function get_selected_account_data( $assoc = false ) {
363
+ $data = json_decode( self::get_option( Ga_Admin::GA_SELECTED_ACCOUNT ) );
364
+ $data = (!empty( $data ) && count( $data ) == 3 ) ? $data : false;
365
+
366
+ if ( $data ) {
367
+ if ( $assoc ) {
368
+ return array(
369
+ 'account_id' => $data[ 0 ],
370
+ 'web_property_id' => $data[ 1 ],
371
+ 'view_id' => $data[ 2 ]
372
+ );
373
+ } else {
374
+ return $data;
375
+ }
376
+ }
377
+
378
+ return false;
379
+ }
380
+
381
+ /**
382
+ * Chekcs whether option for manually UA-code
383
+ * @return NULL|mixed|boolean
384
+ */
385
+ public static function is_code_manually_enabled() {
386
+ return Ga_Helper::get_option( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME );
387
+ }
388
+
389
+ /**
390
+ * Adds percent sign to the given text.
391
+ *
392
+ * @param $text
393
+ *
394
+ * @return string
395
+ */
396
+ public static function format_percent( $text ) {
397
+ $text = self::add_plus( $text );
398
+
399
+ return $text . '%';
400
+ }
401
+
402
+ /**
403
+ * Adds plus sign before number.
404
+ *
405
+ * @param $number
406
+ *
407
+ * @return string
408
+ */
409
+ public static function add_plus( $number ) {
410
+ if ( $number > 0 ) {
411
+ return '+' . $number;
412
+ }
413
+
414
+ return $number;
415
+ }
416
+
417
+ /**
418
+ * Check whether current user has administrator privileges.
419
+ *
420
+ * @return bool
421
+ */
422
+ public static function is_administrator() {
423
+ if ( current_user_can( 'administrator' ) ) {
424
+ return true;
425
+ }
426
+
427
+ return false;
428
+ }
429
+
430
+ public static function is_wp_version_valid() {
431
+ $wp_version = get_bloginfo( 'version' );
432
+
433
+ return version_compare( $wp_version, Ga_Admin::MIN_WP_VERSION, 'ge' );
434
+ }
435
+
436
+ /**
437
+ * Check if terms are accepted
438
+ *
439
+ * @return bool
440
+ */
441
+ public static function are_terms_accepted() {
442
+ return true;
443
+ }
444
+
445
+ /**
446
+ * Check if sharethis scripts enabled
447
+ *
448
+ * @return bool
449
+ */
450
+ public static function is_sharethis_included() {
451
+ return GA_SHARETHIS_SCRIPTS_INCLUDED;
452
+ }
453
+
454
+ /**
455
+ * @return mixed
456
+ */
457
+ public static function is_php_version_valid() {
458
+ $p = '#(\.0+)+($|-)#';
459
+ $ver1 = preg_replace( $p, '', phpversion() );
460
+ $ver2 = preg_replace( $p, '', self::PHP_VERSION_REQUIRED );
461
+ $operator = 'ge';
462
+ $compare = isset( $operator ) ?
463
+ version_compare( $ver1, $ver2, $operator ) :
464
+ version_compare( $ver1, $ver2 );
465
+
466
+ return $compare;
467
+ }
468
+
469
+ public static function get_current_url() {
470
+ return $_SERVER[ 'REQUEST_URI' ];
471
+ }
472
+
473
+ public static function create_url( $url, $data = array() ) {
474
+ return !empty( $data ) ? ( strstr( $url, '?' ) ? ( $url . '&' ) : ( $url . '?' ) ) . http_build_query( $data ) : $url;
475
+ }
476
+
477
+ public static function handle_url_message( $data ) {
478
+ if ( !empty( $_GET[ 'ga_msg' ] ) ) {
479
+ $invite_result = json_decode( base64_decode( $_GET[ 'ga_msg' ] ), true );
480
+ if ( !empty( $invite_result[ 'status' ] ) && !empty( $invite_result[ 'message' ] ) ) {
481
+ $data[ 'ga_msg' ] = Ga_Helper::ga_wp_notice( $invite_result[ 'message' ], $invite_result[ 'status' ], true );
482
+ }
483
+ }
484
+
485
+ return $data;
486
+ }
487
+
488
+ public static function get_url_message_text() {
489
+ $ga_msg = '';
490
+ $invite_result = json_decode( base64_decode( $_GET[ 'ga_msg' ] ), true );
491
+ if ( !empty( $invite_result[ 'status' ] ) && !empty( $invite_result[ 'message' ] ) ) {
492
+ $ga_msg = $invite_result[ 'message' ];
493
+ }
494
+
495
+ return $ga_msg;
496
+ }
497
+
498
+ /**
499
+ * Create base64 url message
500
+ *
501
+ * @param $msg
502
+ * @param $status
503
+ *
504
+ * @return string
505
+ */
506
+ public static function create_url_msg( $msg, $status ) {
507
+ $msg = array( 'status' => $status, 'message' => $msg );
508
+
509
+ return base64_encode( json_encode( $msg ) );
510
+ }
511
+
512
+ public static function is_all_feature_disabled() {
513
+ return self::get_option( Ga_Admin::GA_DISABLE_ALL_FEATURES );
514
+ }
515
+
516
+ public static function are_features_enabled() {
517
+ return self::are_terms_accepted() && !self::is_all_feature_disabled();
518
+ }
519
+
520
+ public static function are_sharethis_properties_verified() {
521
+ return ( get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT ) && self::are_sharethis_properties_set() );
522
+ }
523
+
524
+ public static function are_sharethis_properties_ready_to_verify() {
525
+ return ( self::are_sharethis_properties_set() && !get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT ) );
526
+ }
527
+
528
+ public static function are_sharethis_properties_set() {
529
+ return ( get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ) && get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET ) );
530
+ }
531
+
532
+ public static function should_create_sharethis_property() {
533
+ return ( self::are_features_enabled() && !self::are_sharethis_properties_set() );
534
+ }
535
+
536
+ public static function should_verify_sharethis_installation() {
537
+ return ( self::are_features_enabled() && self::are_sharethis_properties_ready_to_verify() );
538
+ }
539
+
540
+ public static function get_tooltip() {
541
+ if ( !self::are_terms_accepted() ) {
542
+ return self::GA_TOOLTIP_TERMS_NOT_ACCEPTED;
543
+ } else if ( !self::are_features_enabled() ) {
544
+ return self::GA_TOOLTIP_FEATURES_DISABLED;
545
+ } else {
546
+ return '';
547
+ }
548
+ }
549
+
550
+ public static function is_wp_old() {
551
+ return version_compare( get_bloginfo( 'version' ), self::GA_WP_MODERN_VERSION, 'lt' );
552
+ }
553
+
554
+ public static function should_load_ga_javascript( $web_property_id ) {
555
+ return ( self::is_configured( $web_property_id ) && ( self::can_add_ga_code() || self::is_all_feature_disabled() ) );
556
+ }
557
+
558
+ /**
559
+ * @return string
560
+ */
561
+ public static function get_account_id() {
562
+ $account_id = json_decode( Ga_Helper::get_option( Ga_Admin::GA_SELECTED_ACCOUNT ) );
563
+
564
+ return ! empty( $account_id[0] ) ? $account_id[0] : '';
565
+ }
566
+
567
+ public static function is_curl_disabled(){
568
+ return ! function_exists( 'curl_version' );
569
+ }
570
+
571
+
572
+ public static function get_plugin_url_with_correct_protocol() {
573
+ $url = parse_url( GA_PLUGIN_URL );
574
+ return ( ( is_ssl() ) ? 'https://' : 'http://' ) . $url['host'] . $url['path'];
575
+ }
576
+
577
+ public static function get_code_manually_label_classes() {
578
+ $classes = '';
579
+ if ( ! self::are_features_enabled() ){
580
+ $classes = 'label-grey ga-tooltip';
581
+ }
582
+ else if( self::is_account_selected() ) {
583
+ $classes = 'label-grey';
584
+ }
585
+ return $classes;
586
+ }
587
+ }
class/Ga_Sharethis.php CHANGED
@@ -8,6 +8,7 @@
8
  * @author wle@adips.com
9
  * @version 1.0
10
  */
 
11
  class Ga_Sharethis {
12
 
13
  public static function get_body( $data ) {
@@ -21,23 +22,21 @@ class Ga_Sharethis {
21
  public static function create_sharethis_options( $api_client ) {
22
  $data = array();
23
  $parsed_url = parse_url( get_option( 'siteurl' ) );
24
- if ( Ga_Helper::should_create_sharethis_property() ) {
25
- $domain = $parsed_url['host'] . ( !empty( $parsed_url['path'] ) ? $parsed_url['path'] : '' );
26
- $query_params = array(
27
- 'domain' => $domain,
28
- 'is_wordpress' => true,
29
- 'onboarding_product' => 'ga',
30
- );
31
- $response = $api_client->call( 'ga_api_create_sharethis_property', array(
32
- $query_params
33
- ) );
34
- $sharethis_options = self::get_sharethis_options( $response );
35
- if ( !empty( $sharethis_options[ 'id' ] ) ) {
36
- add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID, $sharethis_options[ 'id' ] );
37
- }
38
- if ( !empty( $sharethis_options[ 'secret' ] ) ) {
39
- add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET, $sharethis_options[ 'secret' ] );
40
- }
41
  }
42
 
43
  return $data;
@@ -57,7 +56,7 @@ class Ga_Sharethis {
57
  }
58
  }
59
  } else {
60
- $options[ 'error' ] = self::GA_SHARETHIS_ALERTS_ERROR;
61
  }
62
  return $options;
63
  }
8
  * @author wle@adips.com
9
  * @version 1.0
10
  */
11
+
12
  class Ga_Sharethis {
13
 
14
  public static function get_body( $data ) {
22
  public static function create_sharethis_options( $api_client ) {
23
  $data = array();
24
  $parsed_url = parse_url( get_option( 'siteurl' ) );
25
+ $domain = $parsed_url['host'] . ( !empty( $parsed_url['path'] ) ? $parsed_url['path'] : '' );
26
+ $query_params = array(
27
+ 'domain' => $domain,
28
+ 'is_wordpress' => true,
29
+ 'onboarding_product' => 'ga',
30
+ );
31
+ $response = $api_client->call( 'ga_api_create_sharethis_property', array(
32
+ $query_params
33
+ ) );
34
+ $sharethis_options = self::get_sharethis_options( $response );
35
+ if ( !empty( $sharethis_options[ 'id' ] ) ) {
36
+ add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID, $sharethis_options[ 'id' ] );
37
+ }
38
+ if ( !empty( $sharethis_options[ 'secret' ] ) ) {
39
+ add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET, $sharethis_options[ 'secret' ] );
 
 
40
  }
41
 
42
  return $data;
56
  }
57
  }
58
  } else {
59
+ $options[ 'error' ] = 'error';
60
  }
61
  return $options;
62
  }
class/controller/Ga_Admin_Controller.php CHANGED
@@ -16,11 +16,7 @@ class Ga_Admin_Controller extends Ga_Controller_Core {
16
  * Redirects to Google oauth authentication endpoint.
17
  */
18
  public static function ga_action_auth() {
19
- if ( Ga_Helper::are_features_enabled() ) {
20
- header( 'Location:' . Ga_Admin::api_client()->create_auth_url() );
21
- } else {
22
- wp_die( Ga_Helper::ga_oauth_notice( __( 'Please accept the terms to use this feature' ) ) );
23
- }
24
  }
25
 
26
  /**
@@ -88,7 +84,7 @@ class Ga_Admin_Controller extends Ga_Controller_Core {
88
 
89
  wp_redirect( $url );
90
  }
91
-
92
  public static function validate_ajax_data_change_post( $post ) {
93
  $error = 0;
94
 
16
  * Redirects to Google oauth authentication endpoint.
17
  */
18
  public static function ga_action_auth() {
19
+ header( 'Location:' . Ga_Admin::api_client()->create_auth_url() );
 
 
 
 
20
  }
21
 
22
  /**
84
 
85
  wp_redirect( $url );
86
  }
87
+
88
  public static function validate_ajax_data_change_post( $post ) {
89
  $error = 0;
90
 
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.3.6
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
@@ -47,7 +47,7 @@ if ( !preg_match( '/(\/|\\\)' . GA_NAME . '(\/|\\\)/', realpath( __FILE__ ), $te
47
  die();
48
  }
49
 
50
- define( 'GOOGLEANALYTICS_VERSION', '2.1.3' );
51
  include_once GA_PLUGIN_DIR . '/overwrite/ga_overwrite.php';
52
  include_once GA_PLUGIN_DIR . '/class/Ga_Autoloader.php';
53
  include_once GA_PLUGIN_DIR . '/tools/class-support-logging.php';
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.3.7
8
  * Author: ShareThis
9
  * Author URI: http://sharethis.com
10
  */
47
  die();
48
  }
49
 
50
+ define( 'GOOGLEANALYTICS_VERSION', '2.3.7' );
51
  include_once GA_PLUGIN_DIR . '/overwrite/ga_overwrite.php';
52
  include_once GA_PLUGIN_DIR . '/class/Ga_Autoloader.php';
53
  include_once GA_PLUGIN_DIR . '/tools/class-support-logging.php';
js/googleanalytics_createprop.js ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (
2
+ function ( $, wp ) {
3
+ $( document ).ready( function () {
4
+ var theData = JSON.stringify( {
5
+ onboarding_product: 'ga',
6
+ domain: gasiteURL,
7
+ email: gaAdminEmail,
8
+ is_wordpress: true
9
+ } );
10
+
11
+ $.ajax( {
12
+ url: 'https://platform-api.sharethis.com/v1.0/property',
13
+ method: 'POST',
14
+ async: false,
15
+ contentType: 'application/json; charset=utf-8',
16
+ data: theData,
17
+ success: function ( result ) {
18
+ setGACredentials( result.secret, result._id );
19
+ }
20
+ } );
21
+ } );
22
+
23
+ /**
24
+ * WP Ajax call to set prop id/secret
25
+ */
26
+ function setGACredentials(secret, propid) {
27
+ wp.ajax.post( 'set_ga_credentials', {
28
+ secret: secret,
29
+ propid: propid,
30
+ nonce: gaNonce
31
+ } ).always( function( results ) {
32
+ });
33
+ }
34
+ }
35
+ )( window.jQuery, window.wp );
lib/Ga_Lib_Sharethis_Api_Client.php CHANGED
@@ -54,7 +54,7 @@ class Ga_Lib_Sharethis_Api_Client extends Ga_Lib_Api_Client {
54
  private function ga_api_create_sharethis_property( $query_params ) {
55
  $request = Ga_Lib_Api_Request::get_instance(self::USE_CACHE);
56
  try {
57
- $response = $request->make_request( $this->add_protocol( self::GA_SHARETHIS_ENDPOINT ), wp_json_encode( $query_params ), true );
58
  } catch ( Ga_Lib_Api_Request_Exception $e ) {
59
  throw new Ga_Lib_Sharethis_Api_Client_InvalidDomain_Exception( $e->getMessage() );
60
  }
54
  private function ga_api_create_sharethis_property( $query_params ) {
55
  $request = Ga_Lib_Api_Request::get_instance(self::USE_CACHE);
56
  try {
57
+ $response = $request->make_request( $this->add_protocol( self::GA_SHARETHIS_ENDPOINT ), wp_json_encode( $query_params ), true ); var_dump($response); exit;
58
  } catch ( Ga_Lib_Api_Request_Exception $e ) {
59
  throw new Ga_Lib_Sharethis_Api_Client_InvalidDomain_Exception( $e->getMessage() );
60
  }
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: sharethis, scottstorebloom
3
  Tags: analytics, google analytics, google analytics plugin, google analytics widget, google analytics dashboard
4
  Requires at least: 3.8
5
  Tested up to: 5.4.1
6
- Stable tag: 2.3.6
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
@@ -84,6 +84,10 @@ We are always happy to help.
84
 
85
  == Changelog ==
86
 
 
 
 
 
87
  = 2.3.6 =
88
  * Add onboarding product to property creation.
89
 
3
  Tags: analytics, google analytics, google analytics plugin, google analytics widget, google analytics dashboard
4
  Requires at least: 3.8
5
  Tested up to: 5.4.1
6
+ Stable tag: 2.3.7
7
 
8
  Use Google Analytics on your Wordpress site without touching any code, and view visitor reports right in your Wordpress admin dashboard!
9
 
84
 
85
  == Changelog ==
86
 
87
+ = 2.3.7 =
88
+ * Fix property creation structure.
89
+ * Remove terms blocker.
90
+
91
  = 2.3.6 =
92
  * Add onboarding product to property creation.
93