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