@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 | |
18 | -if (!class_exists('MonsterInsights_GA_Lib_Client')) { |
|
18 | +if ( ! class_exists('MonsterInsights_GA_Lib_Client')) { |
|
19 | 19 | require_once dirname(__FILE__) . '/../autoload.php'; |
20 | 20 | } |
21 | 21 | |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | */ |
73 | 73 | public function log($level, $message, array $context = array()) |
74 | 74 | { |
75 | - if (!$this->shouldHandle($level)) { |
|
75 | + if ( ! $this->shouldHandle($level)) { |
|
76 | 76 | return false; |
77 | 77 | } |
78 | 78 |
@@ -15,7 +15,7 @@ |
||
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 | |
18 | -if (!class_exists('MonsterInsights_GA_Lib_Client')) { |
|
18 | +if ( ! class_exists('MonsterInsights_GA_Lib_Client')) { |
|
19 | 19 | require_once dirname(__FILE__) . '/../autoload.php'; |
20 | 20 | } |
21 | 21 |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 | |
18 | -if (!class_exists('MonsterInsights_GA_Lib_Client')) { |
|
18 | +if ( ! class_exists('MonsterInsights_GA_Lib_Client')) { |
|
19 | 19 | require_once dirname(__FILE__) . '/../autoload.php'; |
20 | 20 | } |
21 | 21 | |
@@ -56,14 +56,14 @@ discard block |
||
56 | 56 | parent::__construct($client); |
57 | 57 | |
58 | 58 | $file = $client->getClassConfig('MonsterInsights_GA_Lib_Logger_File', 'file'); |
59 | - if (!is_string($file) && !is_resource($file)) { |
|
59 | + if ( ! is_string($file) && ! is_resource($file)) { |
|
60 | 60 | throw new MonsterInsights_GA_Lib_Logger_Exception( |
61 | 61 | 'File logger requires a filename or a valid file pointer' |
62 | 62 | ); |
63 | 63 | } |
64 | 64 | |
65 | 65 | $mode = $client->getClassConfig('MonsterInsights_GA_Lib_Logger_File', 'mode'); |
66 | - if (!$mode) { |
|
66 | + if ( ! $mode) { |
|
67 | 67 | $this->mode = $mode; |
68 | 68 | } |
69 | 69 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | { |
79 | 79 | if (is_string($this->file)) { |
80 | 80 | $this->open(); |
81 | - } elseif (!is_resource($this->file)) { |
|
81 | + } elseif ( ! is_resource($this->file)) { |
|
82 | 82 | throw new MonsterInsights_GA_Lib_Logger_Exception('File pointer is no longer available'); |
83 | 83 | } |
84 | 84 | |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | |
107 | 107 | $old = set_error_handler(array($this, 'trapError')); |
108 | 108 | |
109 | - $needsChmod = !file_exists($this->file); |
|
109 | + $needsChmod = ! file_exists($this->file); |
|
110 | 110 | $fh = fopen($this->file, 'a'); |
111 | 111 | |
112 | 112 | restore_error_handler(); |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 | |
18 | -if (!class_exists('MonsterInsights_GA_Lib_Client')) { |
|
18 | +if ( ! class_exists('MonsterInsights_GA_Lib_Client')) { |
|
19 | 19 | require_once dirname(__FILE__) . '/../autoload.php'; |
20 | 20 | } |
21 | 21 | |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | */ |
268 | 268 | public function log($level, $message, array $context = array()) |
269 | 269 | { |
270 | - if (!$this->shouldHandle($level)) { |
|
270 | + if ( ! $this->shouldHandle($level)) { |
|
271 | 271 | return false; |
272 | 272 | } |
273 | 273 | |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | { |
295 | 295 | $template = $this->logFormat; |
296 | 296 | |
297 | - if (!$variables['context']) { |
|
297 | + if ( ! $variables['context']) { |
|
298 | 298 | $template = str_replace('%context%', '', $template); |
299 | 299 | unset($variables['context']); |
300 | 300 | } else { |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | } |
303 | 303 | |
304 | 304 | foreach ($variables as $key => $value) { |
305 | - if (strpos($template, '%'. $key .'%') !== false) { |
|
305 | + if (strpos($template, '%' . $key . '%') !== false) { |
|
306 | 306 | $template = str_replace( |
307 | 307 | '%' . $key . '%', |
308 | 308 | $this->export($value), |
@@ -321,12 +321,12 @@ discard block |
||
321 | 321 | */ |
322 | 322 | protected function reverseJsonInContext(array &$context) |
323 | 323 | { |
324 | - if (!$context) { |
|
324 | + if ( ! $context) { |
|
325 | 325 | return; |
326 | 326 | } |
327 | 327 | |
328 | 328 | foreach ($context as $key => $val) { |
329 | - if (!$val || !is_string($val) || !($val[0] == '{' || $val[0] == '[')) { |
|
329 | + if ( ! $val || ! is_string($val) || ! ($val[0] == '{' || $val[0] == '[')) { |
|
330 | 330 | continue; |
331 | 331 | } |
332 | 332 |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | */ |
49 | 49 | private $reserved = array( |
50 | 50 | "=", ",", "!", "@", "|", ":", "/", "?", "#", |
51 | - "[", "]",'$', "&", "'", "(", ")", "*", "+", ";" |
|
51 | + "[", "]", '$', "&", "'", "(", ")", "*", "+", ";" |
|
52 | 52 | ); |
53 | 53 | private $reservedEncoded = array( |
54 | 54 | "%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F", |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | case "segments": |
108 | 108 | // Slash separated data. Bare values only. |
109 | 109 | $prefix = "/"; |
110 | - $data =$this->replaceVars($data, $parameters, "/"); |
|
110 | + $data = $this->replaceVars($data, $parameters, "/"); |
|
111 | 111 | break; |
112 | 112 | case "dotprefix": |
113 | 113 | // Dot separated data. Bare values only. |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | |
265 | 265 | // If we do not need to include the key name, we just return the raw |
266 | 266 | // value. |
267 | - if (!$combine || $skip_final_combine) { |
|
267 | + if ( ! $combine || $skip_final_combine) { |
|
268 | 268 | return $value; |
269 | 269 | } |
270 | 270 |
@@ -544,7 +544,7 @@ |
||
544 | 544 | */ |
545 | 545 | public function getScopes() |
546 | 546 | { |
547 | - return $this->requestedScopes; |
|
547 | + return $this->requestedScopes; |
|
548 | 548 | } |
549 | 549 | |
550 | 550 | /** |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 | |
18 | -if (!class_exists('MonsterInsights_GA_Lib_Client')) { |
|
18 | +if ( ! class_exists('MonsterInsights_GA_Lib_Client')) { |
|
19 | 19 | require_once dirname(__FILE__) . '/autoload.php'; |
20 | 20 | } |
21 | 21 | |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | { |
77 | 77 | if (is_string($config) && strlen($config)) { |
78 | 78 | $config = new MonsterInsights_GA_Lib_Config($config); |
79 | - } else if ( !($config instanceof MonsterInsights_GA_Lib_Config)) { |
|
79 | + } else if ( ! ($config instanceof MonsterInsights_GA_Lib_Config)) { |
|
80 | 80 | $config = new MonsterInsights_GA_Lib_Config(); |
81 | 81 | |
82 | 82 | if ($this->isAppEngine()) { |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | |
93 | 93 | if ($config->getIoClass() == MonsterInsights_GA_Lib_Config::USE_AUTO_IO_SELECTION) { |
94 | 94 | if (function_exists('curl_version') && function_exists('curl_exec') |
95 | - && !$this->isAppEngine()) { |
|
95 | + && ! $this->isAppEngine()) { |
|
96 | 96 | $config->setIoClass("MonsterInsights_GA_Lib_IO_Curl"); |
97 | 97 | } else { |
98 | 98 | $config->setIoClass("MonsterInsights_GA_Lib_IO_Stream"); |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | { |
168 | 168 | $data = json_decode($json); |
169 | 169 | $key = isset($data->installed) ? 'installed' : 'web'; |
170 | - if (!isset($data->$key)) { |
|
170 | + if ( ! isset($data->$key)) { |
|
171 | 171 | throw new MonsterInsights_GA_Lib_Exception("Invalid client secret JSON file."); |
172 | 172 | } |
173 | 173 | $this->setClientId($data->$key->client_id); |
@@ -528,7 +528,7 @@ discard block |
||
528 | 528 | */ |
529 | 529 | public function addScope($scope_or_scopes) |
530 | 530 | { |
531 | - if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) { |
|
531 | + if (is_string($scope_or_scopes) && ! in_array($scope_or_scopes, $this->requestedScopes)) { |
|
532 | 532 | $this->requestedScopes[] = $scope_or_scopes; |
533 | 533 | } else if (is_array($scope_or_scopes)) { |
534 | 534 | foreach ($scope_or_scopes as $scope) { |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | . " " . self::USER_AGENT_SUFFIX |
587 | 587 | . $this->getLibraryVersion() |
588 | 588 | ); |
589 | - if (!$this->getClassConfig("MonsterInsights_GA_Lib_Http_Request", "disable_gzip")) { |
|
589 | + if ( ! $this->getClassConfig("MonsterInsights_GA_Lib_Http_Request", "disable_gzip")) { |
|
590 | 590 | $request->enableGzip(); |
591 | 591 | } |
592 | 592 | $request->maybeMoveParametersToBody(); |
@@ -612,7 +612,7 @@ discard block |
||
612 | 612 | */ |
613 | 613 | public function getAuth() |
614 | 614 | { |
615 | - if (!isset($this->auth)) { |
|
615 | + if ( ! isset($this->auth)) { |
|
616 | 616 | $class = $this->config->getAuthClass(); |
617 | 617 | $this->auth = new $class($this); |
618 | 618 | } |
@@ -624,7 +624,7 @@ discard block |
||
624 | 624 | */ |
625 | 625 | public function getIo() |
626 | 626 | { |
627 | - if (!isset($this->io)) { |
|
627 | + if ( ! isset($this->io)) { |
|
628 | 628 | $class = $this->config->getIoClass(); |
629 | 629 | $this->io = new $class($this); |
630 | 630 | } |
@@ -636,7 +636,7 @@ discard block |
||
636 | 636 | */ |
637 | 637 | public function getCache() |
638 | 638 | { |
639 | - if (!isset($this->cache)) { |
|
639 | + if ( ! isset($this->cache)) { |
|
640 | 640 | $class = $this->config->getCacheClass(); |
641 | 641 | $this->cache = new $class($this); |
642 | 642 | } |
@@ -648,7 +648,7 @@ discard block |
||
648 | 648 | */ |
649 | 649 | public function getLogger() |
650 | 650 | { |
651 | - if (!isset($this->logger)) { |
|
651 | + if ( ! isset($this->logger)) { |
|
652 | 652 | $class = $this->config->getLoggerClass(); |
653 | 653 | $this->logger = new $class($this); |
654 | 654 | } |
@@ -663,7 +663,7 @@ discard block |
||
663 | 663 | */ |
664 | 664 | public function getClassConfig($class, $key = null) |
665 | 665 | { |
666 | - if (!is_string($class)) { |
|
666 | + if ( ! is_string($class)) { |
|
667 | 667 | $class = get_class($class); |
668 | 668 | } |
669 | 669 | return $this->config->getClassConfig($class, $key); |
@@ -680,7 +680,7 @@ discard block |
||
680 | 680 | */ |
681 | 681 | public function setClassConfig($class, $config, $value = null) |
682 | 682 | { |
683 | - if (!is_string($class)) { |
|
683 | + if ( ! is_string($class)) { |
|
684 | 684 | $class = get_class($class); |
685 | 685 | } |
686 | 686 | $this->config->setClassConfig($class, $config, $value); |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 | |
18 | -if (!class_exists('MonsterInsights_GA_Lib_Client')) { |
|
18 | +if ( ! class_exists('MonsterInsights_GA_Lib_Client')) { |
|
19 | 19 | require_once dirname(__FILE__) . '/../autoload.php'; |
20 | 20 | } |
21 | 21 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | // Creates a new signer from a .p12 file. |
35 | 35 | public function __construct($p12, $password) |
36 | 36 | { |
37 | - if (!function_exists('openssl_x509_read')) { |
|
37 | + if ( ! function_exists('openssl_x509_read')) { |
|
38 | 38 | throw new MonsterInsights_GA_Lib_Exception( |
39 | 39 | 'The Google PHP API library needs the openssl PHP extension' |
40 | 40 | ); |
@@ -44,14 +44,14 @@ discard block |
||
44 | 44 | // format. Different versions of openssl support different p12 formats |
45 | 45 | // and the key from google wasn't being accepted by the version available |
46 | 46 | // at the time. |
47 | - if (!$password && strpos($p12, "-----BEGIN RSA PRIVATE KEY-----") !== false) { |
|
47 | + if ( ! $password && strpos($p12, "-----BEGIN RSA PRIVATE KEY-----") !== false) { |
|
48 | 48 | $this->privateKey = openssl_pkey_get_private($p12); |
49 | 49 | } elseif ($password === 'notasecret' && strpos($p12, "-----BEGIN PRIVATE KEY-----") !== false) { |
50 | 50 | $this->privateKey = openssl_pkey_get_private($p12); |
51 | 51 | } else { |
52 | 52 | // This throws on error |
53 | 53 | $certs = array(); |
54 | - if (!openssl_pkcs12_read($p12, $certs, $password)) { |
|
54 | + if ( ! openssl_pkcs12_read($p12, $certs, $password)) { |
|
55 | 55 | throw new MonsterInsights_GA_Lib_Auth_Exception( |
56 | 56 | "Unable to parse the p12 file. " . |
57 | 57 | "Is this a .p12 file? Is the password correct? OpenSSL error: " . |
@@ -60,13 +60,13 @@ discard block |
||
60 | 60 | } |
61 | 61 | // TODO(beaton): is this part of the contract for the openssl_pkcs12_read |
62 | 62 | // method? What happens if there are multiple private keys? Do we care? |
63 | - if (!array_key_exists("pkey", $certs) || !$certs["pkey"]) { |
|
63 | + if ( ! array_key_exists("pkey", $certs) || ! $certs["pkey"]) { |
|
64 | 64 | throw new MonsterInsights_GA_Lib_Auth_Exception("No private key found in p12 file."); |
65 | 65 | } |
66 | 66 | $this->privateKey = openssl_pkey_get_private($certs['pkey']); |
67 | 67 | } |
68 | 68 | |
69 | - if (!$this->privateKey) { |
|
69 | + if ( ! $this->privateKey) { |
|
70 | 70 | throw new MonsterInsights_GA_Lib_Auth_Exception("Unable to load private key"); |
71 | 71 | } |
72 | 72 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | ); |
87 | 87 | } |
88 | 88 | $hash = defined("OPENSSL_ALGO_SHA256") ? OPENSSL_ALGO_SHA256 : "sha256"; |
89 | - if (!openssl_sign($data, $signature, $this->privateKey, $hash)) { |
|
89 | + if ( ! openssl_sign($data, $signature, $this->privateKey, $hash)) { |
|
90 | 90 | throw new MonsterInsights_GA_Lib_Auth_Exception("Unable to sign data"); |
91 | 91 | } |
92 | 92 | return $signature; |
@@ -33,12 +33,12 @@ |
||
33 | 33 | function monsterinsights_renamed_google_api_php_client_autoload($className) |
34 | 34 | { |
35 | 35 | $classPath = explode('_', $className); |
36 | - if ( empty( $classPath[0] ) || empty( $classPath[1] ) || empty( $classPath[2] ) || |
|
36 | + if (empty($classPath[0]) || empty($classPath[1]) || empty($classPath[2]) || |
|
37 | 37 | $classPath[0] != 'MonsterInsights' && $classPath[1] != 'GA' && $classPath[2] != 'Lib') { |
38 | 38 | return; |
39 | 39 | } |
40 | - unset( $classPath[0] ); |
|
41 | - unset( $classPath[1] ); |
|
40 | + unset($classPath[0]); |
|
41 | + unset($classPath[1]); |
|
42 | 42 | $classPath[2] = 'Google'; |
43 | 43 | // Drop 'MonsterInsights_GA_Lib', and maximum class file path depth in this project is 3. |
44 | 44 | $classPath = array_slice($classPath, 1, 2); |
@@ -4,345 +4,345 @@ |
||
4 | 4 | */ |
5 | 5 | // Exit if accessed directly. |
6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
7 | - exit; |
|
7 | + exit; |
|
8 | 8 | } |
9 | 9 | class MonsterInsights_GA_Client extends MonsterInsights_GA_Lib_Client { |
10 | 10 | |
11 | - /** |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - protected $option_refresh_token; |
|
15 | - |
|
16 | - /** |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $option_access_token; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $http_response_code; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - public $client; // used for reporting purposes |
|
30 | - |
|
31 | - /** |
|
32 | - * Initialize the config and refresh the token |
|
33 | - * |
|
34 | - * @param array $config |
|
35 | - * @param string $option_prefix |
|
36 | - */ |
|
37 | - public function __construct( $google_settings, $client_name ) { |
|
38 | - |
|
39 | - // Initialize the config to set all properties properly. |
|
40 | - $config = $this->init_config( $google_settings ); |
|
41 | - |
|
42 | - parent::__construct( $config ); |
|
43 | - |
|
44 | - $this->client = new MonsterInsights_GA_Lib_Client( $config ); |
|
45 | - |
|
46 | - if ( ! empty( $google_settings['scopes'] ) ) { |
|
47 | - $this->setScopes( $google_settings['scopes'] ); |
|
48 | - } |
|
49 | - |
|
50 | - $this->setAccessType( 'offline' ); |
|
51 | - |
|
52 | - // Client name will be `pro`, `lite`, or `support` |
|
53 | - $this->option_refresh_token = 'monsterinsights_' . $client_name . '_refresh_token'; |
|
54 | - $this->option_access_token = 'monsterinsights_' . $client_name . '_access_token'; |
|
55 | - |
|
56 | - // Let's get an access token if we've got a refresh token. |
|
57 | - $this->refresh_tokens(); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Authenticate the client. If $authorization_code is empty it will lead the user through the validation process of |
|
62 | - * Google. If set it will be get the access token for current session and save the refresh_token for future use |
|
63 | - * |
|
64 | - * @param mixed $authorization_code |
|
65 | - * |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function authenticate_client( $authorization_code = null ) { |
|
69 | - static $has_retried; |
|
70 | - |
|
71 | - // Authenticate client. |
|
72 | - try { |
|
73 | - $this->authenticate( $authorization_code ); |
|
74 | - |
|
75 | - // Get access response. |
|
76 | - $response = $this->getAccessToken(); |
|
77 | - |
|
78 | - // Check if there is a response body. |
|
79 | - if ( ! empty( $response ) ) { |
|
80 | - $response = json_decode( $response ); |
|
81 | - |
|
82 | - if ( is_object( $response ) ) { |
|
83 | - // Save the refresh token. |
|
84 | - $this->save_refresh_token( $response->refresh_token ); |
|
85 | - $this->save_initial_access_token( $response ); |
|
86 | - return true; |
|
87 | - } |
|
88 | - } |
|
89 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
90 | - // If there aren't any attempts before, try again and set attempts on true, to prevent further attempts. |
|
91 | - if ( empty( $has_retried ) ) { |
|
92 | - $has_retried = true; |
|
93 | - |
|
94 | - return $this->authenticate_client( $authorization_code ); |
|
95 | - } |
|
96 | - //error_log( $exception ); // @todo proper logging and handling of already used oauth token |
|
97 | - } |
|
98 | - |
|
99 | - return false; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Doing a request to the API |
|
104 | - * |
|
105 | - * @param string $target_request_url |
|
106 | - * @param bool $decode_response |
|
107 | - * @param string $request_method |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - public function do_request( $target_request_url, $decode_response = false, $request_method = 'GET', $body = array() ) { |
|
112 | - // Get response. |
|
113 | - $request = new MonsterInsights_GA_Lib_Http_Request( $target_request_url, $request_method ); |
|
114 | - if ( ! empty( $body ) ) { |
|
115 | - $request->setPostBody( $body ); // used exclusively for auth profiles |
|
116 | - } |
|
117 | - |
|
118 | - $response = $this->getAuth()->authenticatedRequest( $request ); |
|
119 | - |
|
120 | - // Storing the response code. |
|
121 | - $this->http_response_code = $response->getResponseHttpCode(); |
|
122 | - |
|
123 | - if ( $decode_response ) { |
|
124 | - return $this->decode_response( $response ); |
|
125 | - } |
|
126 | - |
|
127 | - return $response; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Decode the JSON response |
|
132 | - * |
|
133 | - * @param object $response |
|
134 | - * @param int $accepted_response_code |
|
135 | - * |
|
136 | - * @return mixed |
|
137 | - */ |
|
138 | - public function decode_response( $response, $accepted_response_code = 200 ) { |
|
139 | - if ( $accepted_response_code === $response->getResponseHttpCode() ) { |
|
140 | - return json_decode( $response->getResponseBody() ); |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Getting the response code, saved from latest request to Google |
|
146 | - * |
|
147 | - * @return mixed |
|
148 | - */ |
|
149 | - public function get_http_response_code() { |
|
150 | - return $this->http_response_code; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Clears the options and revokes the token |
|
155 | - */ |
|
156 | - public function clear_data() { |
|
157 | - $this->revokeToken(); |
|
158 | - delete_option( $this->option_access_token ); |
|
159 | - delete_option( $this->option_refresh_token ); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Moves test options to live |
|
164 | - */ |
|
165 | - public function move_test_to_live() { |
|
166 | - $new_option_access_token = str_replace( 'test_', '', $this->option_access_token ); |
|
167 | - $new_option_refresh_token = str_replace( 'test_', '', $this->option_refresh_token ); |
|
168 | - |
|
169 | - update_option( $new_option_access_token, get_option( $this->option_access_token, '' ) ); |
|
170 | - update_option( $new_option_refresh_token, get_option( $this->option_refresh_token, '' ) ); |
|
171 | - |
|
172 | - delete_option( $this->option_access_token ); |
|
173 | - delete_option( $this->option_refresh_token ); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Check if user is authenticated |
|
178 | - * |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function is_authenticated() { |
|
182 | - $has_refresh_token = ( $this->get_refresh_token() !== '' ); |
|
183 | - $access_token_expired = $this->access_token_expired(); |
|
184 | - |
|
185 | - return $has_refresh_token && ! $access_token_expired; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Initialize the config, will merge given config with default config to be sure all settings are available |
|
190 | - * |
|
191 | - * @param array $settings |
|
192 | - */ |
|
193 | - protected function init_config( array $google_config ) { |
|
194 | - |
|
195 | - // Load MI io |
|
196 | - require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-io.php'; |
|
197 | - |
|
198 | - // Load MI cache |
|
199 | - require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-cache.php'; |
|
200 | - |
|
201 | - // Load MI logger |
|
202 | - require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-logger.php'; |
|
203 | - |
|
204 | - $config = new MonsterInsights_GA_Lib_Config(); |
|
205 | - |
|
206 | - $config->setIoClass( 'MonsterInsights_GA_IO' ); |
|
207 | - $config->setCacheClass( 'MonsterInsights_GA_Cache' ); |
|
208 | - $config->setLoggerClass( 'MonsterInsights_GA_Logger' ); |
|
209 | - |
|
210 | - if ( ! empty( $google_config['application_name'] ) ) { |
|
211 | - $config->setApplicationName( $google_config['application_name'] ); |
|
212 | - } |
|
213 | - |
|
214 | - if ( ! empty( $google_config['client_id'] ) ) { |
|
215 | - $config->setClientId( $google_config['client_id'] ); |
|
216 | - } |
|
217 | - |
|
218 | - if ( ! empty( $google_config['client_secret'] ) ) { |
|
219 | - $config->setClientSecret( $google_config['client_secret'] ); |
|
220 | - } |
|
221 | - |
|
222 | - if ( ! empty( $google_config['redirect_uri'] ) ) { |
|
223 | - $config->setRedirectUri( $google_config['redirect_uri'] ); |
|
224 | - } |
|
225 | - |
|
226 | - return $config; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Refreshing the tokens |
|
231 | - */ |
|
232 | - protected function refresh_tokens() { |
|
233 | - if ( ( $refresh_token = $this->get_refresh_token() ) !== '' && $this->access_token_expired() ) { |
|
234 | - try { |
|
235 | - // Refresh the token. |
|
236 | - $this->refreshToken( $refresh_token ); |
|
237 | - |
|
238 | - $response = $this->getAuth()->getAccessToken(); |
|
239 | - $response = json_decode( $response ); |
|
240 | - |
|
241 | - // Check response and if there is an access_token. |
|
242 | - if ( ! empty( $response ) && ! empty ( $response->access_token ) ) { |
|
243 | - $this->save_access_token( $response ); |
|
244 | - } |
|
245 | - } |
|
246 | - catch ( Exception $e ) { |
|
247 | - return false; |
|
248 | - } |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Save the refresh token |
|
254 | - * |
|
255 | - * @param string $refresh_token |
|
256 | - */ |
|
257 | - protected function save_refresh_token( $refresh_token ) { |
|
258 | - update_option( $this->option_refresh_token, trim( $refresh_token ) ); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Return refresh token |
|
263 | - * |
|
264 | - * @return string |
|
265 | - */ |
|
266 | - protected function get_refresh_token() { |
|
267 | - return get_option( $this->option_refresh_token, '' ); |
|
268 | - } |
|
269 | - |
|
270 | - |
|
271 | - /** |
|
272 | - * Saving the access token as an option for further use till it expires. |
|
273 | - * |
|
274 | - * @param array $response |
|
275 | - */ |
|
276 | - protected function save_initial_access_token( $response ) { |
|
277 | - update_option( |
|
278 | - $this->option_access_token, |
|
279 | - array( |
|
280 | - 'refresh_token' => $response->refresh_token, |
|
281 | - 'access_token' => $response->access_token, |
|
282 | - 'expires' => current_time( 'timestamp' ) + $response->expires_in, |
|
283 | - 'expires_in' => $response->expires_in, |
|
284 | - 'created' => $response->created, |
|
285 | - ) |
|
286 | - ); |
|
287 | - |
|
288 | - try { |
|
289 | - $this->setAccessToken( json_encode( $response ) ); |
|
290 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
291 | - |
|
292 | - } |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Saving the access token as an option for further use till it expires. |
|
297 | - * |
|
298 | - * @param array $response |
|
299 | - */ |
|
300 | - protected function save_access_token( $response ) { |
|
301 | - update_option( |
|
302 | - $this->option_access_token, |
|
303 | - array( |
|
304 | - 'refresh_token' => $this->get_refresh_token(), |
|
305 | - 'access_token' => $response->access_token, |
|
306 | - 'expires' => current_time( 'timestamp' ) + $response->expires_in, |
|
307 | - 'expires_in' => $response->expires_in, |
|
308 | - 'created' => $response->created, |
|
309 | - ) |
|
310 | - ); |
|
311 | - |
|
312 | - try { |
|
313 | - $this->setAccessToken( json_encode( $response ) ); |
|
314 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
315 | - |
|
316 | - } |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Check if current access token is expired. |
|
321 | - * |
|
322 | - * @return bool |
|
323 | - */ |
|
324 | - private function access_token_expired() { |
|
325 | - $access_token = $this->get_access_token(); |
|
326 | - |
|
327 | - if ( empty( $access_token ) || empty( $access_token['expires'] ) || current_time( 'timestamp' ) >= $access_token['expires'] ) { |
|
328 | - return true; |
|
329 | - } |
|
330 | - |
|
331 | - try { |
|
332 | - $this->setAccessToken( json_encode( $access_token ) ); |
|
333 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
334 | - return true; |
|
335 | - } |
|
336 | - return false; |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Getting the current access token from the options |
|
341 | - * |
|
342 | - * @return mixed |
|
343 | - */ |
|
344 | - public function get_access_token() { |
|
345 | - return get_option( $this->option_access_token, array( 'access_token' => false, 'expires' => 0 ) ); |
|
346 | - } |
|
11 | + /** |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + protected $option_refresh_token; |
|
15 | + |
|
16 | + /** |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $option_access_token; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $http_response_code; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + public $client; // used for reporting purposes |
|
30 | + |
|
31 | + /** |
|
32 | + * Initialize the config and refresh the token |
|
33 | + * |
|
34 | + * @param array $config |
|
35 | + * @param string $option_prefix |
|
36 | + */ |
|
37 | + public function __construct( $google_settings, $client_name ) { |
|
38 | + |
|
39 | + // Initialize the config to set all properties properly. |
|
40 | + $config = $this->init_config( $google_settings ); |
|
41 | + |
|
42 | + parent::__construct( $config ); |
|
43 | + |
|
44 | + $this->client = new MonsterInsights_GA_Lib_Client( $config ); |
|
45 | + |
|
46 | + if ( ! empty( $google_settings['scopes'] ) ) { |
|
47 | + $this->setScopes( $google_settings['scopes'] ); |
|
48 | + } |
|
49 | + |
|
50 | + $this->setAccessType( 'offline' ); |
|
51 | + |
|
52 | + // Client name will be `pro`, `lite`, or `support` |
|
53 | + $this->option_refresh_token = 'monsterinsights_' . $client_name . '_refresh_token'; |
|
54 | + $this->option_access_token = 'monsterinsights_' . $client_name . '_access_token'; |
|
55 | + |
|
56 | + // Let's get an access token if we've got a refresh token. |
|
57 | + $this->refresh_tokens(); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Authenticate the client. If $authorization_code is empty it will lead the user through the validation process of |
|
62 | + * Google. If set it will be get the access token for current session and save the refresh_token for future use |
|
63 | + * |
|
64 | + * @param mixed $authorization_code |
|
65 | + * |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function authenticate_client( $authorization_code = null ) { |
|
69 | + static $has_retried; |
|
70 | + |
|
71 | + // Authenticate client. |
|
72 | + try { |
|
73 | + $this->authenticate( $authorization_code ); |
|
74 | + |
|
75 | + // Get access response. |
|
76 | + $response = $this->getAccessToken(); |
|
77 | + |
|
78 | + // Check if there is a response body. |
|
79 | + if ( ! empty( $response ) ) { |
|
80 | + $response = json_decode( $response ); |
|
81 | + |
|
82 | + if ( is_object( $response ) ) { |
|
83 | + // Save the refresh token. |
|
84 | + $this->save_refresh_token( $response->refresh_token ); |
|
85 | + $this->save_initial_access_token( $response ); |
|
86 | + return true; |
|
87 | + } |
|
88 | + } |
|
89 | + } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
90 | + // If there aren't any attempts before, try again and set attempts on true, to prevent further attempts. |
|
91 | + if ( empty( $has_retried ) ) { |
|
92 | + $has_retried = true; |
|
93 | + |
|
94 | + return $this->authenticate_client( $authorization_code ); |
|
95 | + } |
|
96 | + //error_log( $exception ); // @todo proper logging and handling of already used oauth token |
|
97 | + } |
|
98 | + |
|
99 | + return false; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Doing a request to the API |
|
104 | + * |
|
105 | + * @param string $target_request_url |
|
106 | + * @param bool $decode_response |
|
107 | + * @param string $request_method |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + public function do_request( $target_request_url, $decode_response = false, $request_method = 'GET', $body = array() ) { |
|
112 | + // Get response. |
|
113 | + $request = new MonsterInsights_GA_Lib_Http_Request( $target_request_url, $request_method ); |
|
114 | + if ( ! empty( $body ) ) { |
|
115 | + $request->setPostBody( $body ); // used exclusively for auth profiles |
|
116 | + } |
|
117 | + |
|
118 | + $response = $this->getAuth()->authenticatedRequest( $request ); |
|
119 | + |
|
120 | + // Storing the response code. |
|
121 | + $this->http_response_code = $response->getResponseHttpCode(); |
|
122 | + |
|
123 | + if ( $decode_response ) { |
|
124 | + return $this->decode_response( $response ); |
|
125 | + } |
|
126 | + |
|
127 | + return $response; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Decode the JSON response |
|
132 | + * |
|
133 | + * @param object $response |
|
134 | + * @param int $accepted_response_code |
|
135 | + * |
|
136 | + * @return mixed |
|
137 | + */ |
|
138 | + public function decode_response( $response, $accepted_response_code = 200 ) { |
|
139 | + if ( $accepted_response_code === $response->getResponseHttpCode() ) { |
|
140 | + return json_decode( $response->getResponseBody() ); |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Getting the response code, saved from latest request to Google |
|
146 | + * |
|
147 | + * @return mixed |
|
148 | + */ |
|
149 | + public function get_http_response_code() { |
|
150 | + return $this->http_response_code; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Clears the options and revokes the token |
|
155 | + */ |
|
156 | + public function clear_data() { |
|
157 | + $this->revokeToken(); |
|
158 | + delete_option( $this->option_access_token ); |
|
159 | + delete_option( $this->option_refresh_token ); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Moves test options to live |
|
164 | + */ |
|
165 | + public function move_test_to_live() { |
|
166 | + $new_option_access_token = str_replace( 'test_', '', $this->option_access_token ); |
|
167 | + $new_option_refresh_token = str_replace( 'test_', '', $this->option_refresh_token ); |
|
168 | + |
|
169 | + update_option( $new_option_access_token, get_option( $this->option_access_token, '' ) ); |
|
170 | + update_option( $new_option_refresh_token, get_option( $this->option_refresh_token, '' ) ); |
|
171 | + |
|
172 | + delete_option( $this->option_access_token ); |
|
173 | + delete_option( $this->option_refresh_token ); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Check if user is authenticated |
|
178 | + * |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function is_authenticated() { |
|
182 | + $has_refresh_token = ( $this->get_refresh_token() !== '' ); |
|
183 | + $access_token_expired = $this->access_token_expired(); |
|
184 | + |
|
185 | + return $has_refresh_token && ! $access_token_expired; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Initialize the config, will merge given config with default config to be sure all settings are available |
|
190 | + * |
|
191 | + * @param array $settings |
|
192 | + */ |
|
193 | + protected function init_config( array $google_config ) { |
|
194 | + |
|
195 | + // Load MI io |
|
196 | + require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-io.php'; |
|
197 | + |
|
198 | + // Load MI cache |
|
199 | + require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-cache.php'; |
|
200 | + |
|
201 | + // Load MI logger |
|
202 | + require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-logger.php'; |
|
203 | + |
|
204 | + $config = new MonsterInsights_GA_Lib_Config(); |
|
205 | + |
|
206 | + $config->setIoClass( 'MonsterInsights_GA_IO' ); |
|
207 | + $config->setCacheClass( 'MonsterInsights_GA_Cache' ); |
|
208 | + $config->setLoggerClass( 'MonsterInsights_GA_Logger' ); |
|
209 | + |
|
210 | + if ( ! empty( $google_config['application_name'] ) ) { |
|
211 | + $config->setApplicationName( $google_config['application_name'] ); |
|
212 | + } |
|
213 | + |
|
214 | + if ( ! empty( $google_config['client_id'] ) ) { |
|
215 | + $config->setClientId( $google_config['client_id'] ); |
|
216 | + } |
|
217 | + |
|
218 | + if ( ! empty( $google_config['client_secret'] ) ) { |
|
219 | + $config->setClientSecret( $google_config['client_secret'] ); |
|
220 | + } |
|
221 | + |
|
222 | + if ( ! empty( $google_config['redirect_uri'] ) ) { |
|
223 | + $config->setRedirectUri( $google_config['redirect_uri'] ); |
|
224 | + } |
|
225 | + |
|
226 | + return $config; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Refreshing the tokens |
|
231 | + */ |
|
232 | + protected function refresh_tokens() { |
|
233 | + if ( ( $refresh_token = $this->get_refresh_token() ) !== '' && $this->access_token_expired() ) { |
|
234 | + try { |
|
235 | + // Refresh the token. |
|
236 | + $this->refreshToken( $refresh_token ); |
|
237 | + |
|
238 | + $response = $this->getAuth()->getAccessToken(); |
|
239 | + $response = json_decode( $response ); |
|
240 | + |
|
241 | + // Check response and if there is an access_token. |
|
242 | + if ( ! empty( $response ) && ! empty ( $response->access_token ) ) { |
|
243 | + $this->save_access_token( $response ); |
|
244 | + } |
|
245 | + } |
|
246 | + catch ( Exception $e ) { |
|
247 | + return false; |
|
248 | + } |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Save the refresh token |
|
254 | + * |
|
255 | + * @param string $refresh_token |
|
256 | + */ |
|
257 | + protected function save_refresh_token( $refresh_token ) { |
|
258 | + update_option( $this->option_refresh_token, trim( $refresh_token ) ); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Return refresh token |
|
263 | + * |
|
264 | + * @return string |
|
265 | + */ |
|
266 | + protected function get_refresh_token() { |
|
267 | + return get_option( $this->option_refresh_token, '' ); |
|
268 | + } |
|
269 | + |
|
270 | + |
|
271 | + /** |
|
272 | + * Saving the access token as an option for further use till it expires. |
|
273 | + * |
|
274 | + * @param array $response |
|
275 | + */ |
|
276 | + protected function save_initial_access_token( $response ) { |
|
277 | + update_option( |
|
278 | + $this->option_access_token, |
|
279 | + array( |
|
280 | + 'refresh_token' => $response->refresh_token, |
|
281 | + 'access_token' => $response->access_token, |
|
282 | + 'expires' => current_time( 'timestamp' ) + $response->expires_in, |
|
283 | + 'expires_in' => $response->expires_in, |
|
284 | + 'created' => $response->created, |
|
285 | + ) |
|
286 | + ); |
|
287 | + |
|
288 | + try { |
|
289 | + $this->setAccessToken( json_encode( $response ) ); |
|
290 | + } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
291 | + |
|
292 | + } |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Saving the access token as an option for further use till it expires. |
|
297 | + * |
|
298 | + * @param array $response |
|
299 | + */ |
|
300 | + protected function save_access_token( $response ) { |
|
301 | + update_option( |
|
302 | + $this->option_access_token, |
|
303 | + array( |
|
304 | + 'refresh_token' => $this->get_refresh_token(), |
|
305 | + 'access_token' => $response->access_token, |
|
306 | + 'expires' => current_time( 'timestamp' ) + $response->expires_in, |
|
307 | + 'expires_in' => $response->expires_in, |
|
308 | + 'created' => $response->created, |
|
309 | + ) |
|
310 | + ); |
|
311 | + |
|
312 | + try { |
|
313 | + $this->setAccessToken( json_encode( $response ) ); |
|
314 | + } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
315 | + |
|
316 | + } |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Check if current access token is expired. |
|
321 | + * |
|
322 | + * @return bool |
|
323 | + */ |
|
324 | + private function access_token_expired() { |
|
325 | + $access_token = $this->get_access_token(); |
|
326 | + |
|
327 | + if ( empty( $access_token ) || empty( $access_token['expires'] ) || current_time( 'timestamp' ) >= $access_token['expires'] ) { |
|
328 | + return true; |
|
329 | + } |
|
330 | + |
|
331 | + try { |
|
332 | + $this->setAccessToken( json_encode( $access_token ) ); |
|
333 | + } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
334 | + return true; |
|
335 | + } |
|
336 | + return false; |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Getting the current access token from the options |
|
341 | + * |
|
342 | + * @return mixed |
|
343 | + */ |
|
344 | + public function get_access_token() { |
|
345 | + return get_option( $this->option_access_token, array( 'access_token' => false, 'expires' => 0 ) ); |
|
346 | + } |
|
347 | 347 | |
348 | 348 | } |
349 | 349 | \ No newline at end of file |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class MonsterInsights_GA_Client |
4 | 4 | */ |
5 | 5 | // Exit if accessed directly. |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if ( ! defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | class MonsterInsights_GA_Client extends MonsterInsights_GA_Lib_Client { |
@@ -34,20 +34,20 @@ discard block |
||
34 | 34 | * @param array $config |
35 | 35 | * @param string $option_prefix |
36 | 36 | */ |
37 | - public function __construct( $google_settings, $client_name ) { |
|
37 | + public function __construct($google_settings, $client_name) { |
|
38 | 38 | |
39 | 39 | // Initialize the config to set all properties properly. |
40 | - $config = $this->init_config( $google_settings ); |
|
40 | + $config = $this->init_config($google_settings); |
|
41 | 41 | |
42 | - parent::__construct( $config ); |
|
42 | + parent::__construct($config); |
|
43 | 43 | |
44 | - $this->client = new MonsterInsights_GA_Lib_Client( $config ); |
|
44 | + $this->client = new MonsterInsights_GA_Lib_Client($config); |
|
45 | 45 | |
46 | - if ( ! empty( $google_settings['scopes'] ) ) { |
|
47 | - $this->setScopes( $google_settings['scopes'] ); |
|
46 | + if ( ! empty($google_settings['scopes'])) { |
|
47 | + $this->setScopes($google_settings['scopes']); |
|
48 | 48 | } |
49 | 49 | |
50 | - $this->setAccessType( 'offline' ); |
|
50 | + $this->setAccessType('offline'); |
|
51 | 51 | |
52 | 52 | // Client name will be `pro`, `lite`, or `support` |
53 | 53 | $this->option_refresh_token = 'monsterinsights_' . $client_name . '_refresh_token'; |
@@ -65,33 +65,33 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @return bool |
67 | 67 | */ |
68 | - public function authenticate_client( $authorization_code = null ) { |
|
68 | + public function authenticate_client($authorization_code = null) { |
|
69 | 69 | static $has_retried; |
70 | 70 | |
71 | 71 | // Authenticate client. |
72 | 72 | try { |
73 | - $this->authenticate( $authorization_code ); |
|
73 | + $this->authenticate($authorization_code); |
|
74 | 74 | |
75 | 75 | // Get access response. |
76 | 76 | $response = $this->getAccessToken(); |
77 | 77 | |
78 | 78 | // Check if there is a response body. |
79 | - if ( ! empty( $response ) ) { |
|
80 | - $response = json_decode( $response ); |
|
79 | + if ( ! empty($response)) { |
|
80 | + $response = json_decode($response); |
|
81 | 81 | |
82 | - if ( is_object( $response ) ) { |
|
82 | + if (is_object($response)) { |
|
83 | 83 | // Save the refresh token. |
84 | - $this->save_refresh_token( $response->refresh_token ); |
|
85 | - $this->save_initial_access_token( $response ); |
|
84 | + $this->save_refresh_token($response->refresh_token); |
|
85 | + $this->save_initial_access_token($response); |
|
86 | 86 | return true; |
87 | 87 | } |
88 | 88 | } |
89 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
89 | + } catch (MonsterInsights_GA_Lib_Auth_Exception $exception) { |
|
90 | 90 | // If there aren't any attempts before, try again and set attempts on true, to prevent further attempts. |
91 | - if ( empty( $has_retried ) ) { |
|
91 | + if (empty($has_retried)) { |
|
92 | 92 | $has_retried = true; |
93 | 93 | |
94 | - return $this->authenticate_client( $authorization_code ); |
|
94 | + return $this->authenticate_client($authorization_code); |
|
95 | 95 | } |
96 | 96 | //error_log( $exception ); // @todo proper logging and handling of already used oauth token |
97 | 97 | } |
@@ -108,20 +108,20 @@ discard block |
||
108 | 108 | * |
109 | 109 | * @return array |
110 | 110 | */ |
111 | - public function do_request( $target_request_url, $decode_response = false, $request_method = 'GET', $body = array() ) { |
|
111 | + public function do_request($target_request_url, $decode_response = false, $request_method = 'GET', $body = array()) { |
|
112 | 112 | // Get response. |
113 | - $request = new MonsterInsights_GA_Lib_Http_Request( $target_request_url, $request_method ); |
|
114 | - if ( ! empty( $body ) ) { |
|
115 | - $request->setPostBody( $body ); // used exclusively for auth profiles |
|
113 | + $request = new MonsterInsights_GA_Lib_Http_Request($target_request_url, $request_method); |
|
114 | + if ( ! empty($body)) { |
|
115 | + $request->setPostBody($body); // used exclusively for auth profiles |
|
116 | 116 | } |
117 | 117 | |
118 | - $response = $this->getAuth()->authenticatedRequest( $request ); |
|
118 | + $response = $this->getAuth()->authenticatedRequest($request); |
|
119 | 119 | |
120 | 120 | // Storing the response code. |
121 | 121 | $this->http_response_code = $response->getResponseHttpCode(); |
122 | 122 | |
123 | - if ( $decode_response ) { |
|
124 | - return $this->decode_response( $response ); |
|
123 | + if ($decode_response) { |
|
124 | + return $this->decode_response($response); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | return $response; |
@@ -135,9 +135,9 @@ discard block |
||
135 | 135 | * |
136 | 136 | * @return mixed |
137 | 137 | */ |
138 | - public function decode_response( $response, $accepted_response_code = 200 ) { |
|
139 | - if ( $accepted_response_code === $response->getResponseHttpCode() ) { |
|
140 | - return json_decode( $response->getResponseBody() ); |
|
138 | + public function decode_response($response, $accepted_response_code = 200) { |
|
139 | + if ($accepted_response_code === $response->getResponseHttpCode()) { |
|
140 | + return json_decode($response->getResponseBody()); |
|
141 | 141 | } |
142 | 142 | } |
143 | 143 | |
@@ -155,22 +155,22 @@ discard block |
||
155 | 155 | */ |
156 | 156 | public function clear_data() { |
157 | 157 | $this->revokeToken(); |
158 | - delete_option( $this->option_access_token ); |
|
159 | - delete_option( $this->option_refresh_token ); |
|
158 | + delete_option($this->option_access_token); |
|
159 | + delete_option($this->option_refresh_token); |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
163 | 163 | * Moves test options to live |
164 | 164 | */ |
165 | 165 | public function move_test_to_live() { |
166 | - $new_option_access_token = str_replace( 'test_', '', $this->option_access_token ); |
|
167 | - $new_option_refresh_token = str_replace( 'test_', '', $this->option_refresh_token ); |
|
166 | + $new_option_access_token = str_replace('test_', '', $this->option_access_token); |
|
167 | + $new_option_refresh_token = str_replace('test_', '', $this->option_refresh_token); |
|
168 | 168 | |
169 | - update_option( $new_option_access_token, get_option( $this->option_access_token, '' ) ); |
|
170 | - update_option( $new_option_refresh_token, get_option( $this->option_refresh_token, '' ) ); |
|
169 | + update_option($new_option_access_token, get_option($this->option_access_token, '')); |
|
170 | + update_option($new_option_refresh_token, get_option($this->option_refresh_token, '')); |
|
171 | 171 | |
172 | - delete_option( $this->option_access_token ); |
|
173 | - delete_option( $this->option_refresh_token ); |
|
172 | + delete_option($this->option_access_token); |
|
173 | + delete_option($this->option_refresh_token); |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | /** |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | * @return bool |
180 | 180 | */ |
181 | 181 | public function is_authenticated() { |
182 | - $has_refresh_token = ( $this->get_refresh_token() !== '' ); |
|
182 | + $has_refresh_token = ($this->get_refresh_token() !== ''); |
|
183 | 183 | $access_token_expired = $this->access_token_expired(); |
184 | 184 | |
185 | 185 | return $has_refresh_token && ! $access_token_expired; |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | * |
191 | 191 | * @param array $settings |
192 | 192 | */ |
193 | - protected function init_config( array $google_config ) { |
|
193 | + protected function init_config(array $google_config) { |
|
194 | 194 | |
195 | 195 | // Load MI io |
196 | 196 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-io.php'; |
@@ -203,24 +203,24 @@ discard block |
||
203 | 203 | |
204 | 204 | $config = new MonsterInsights_GA_Lib_Config(); |
205 | 205 | |
206 | - $config->setIoClass( 'MonsterInsights_GA_IO' ); |
|
207 | - $config->setCacheClass( 'MonsterInsights_GA_Cache' ); |
|
208 | - $config->setLoggerClass( 'MonsterInsights_GA_Logger' ); |
|
206 | + $config->setIoClass('MonsterInsights_GA_IO'); |
|
207 | + $config->setCacheClass('MonsterInsights_GA_Cache'); |
|
208 | + $config->setLoggerClass('MonsterInsights_GA_Logger'); |
|
209 | 209 | |
210 | - if ( ! empty( $google_config['application_name'] ) ) { |
|
211 | - $config->setApplicationName( $google_config['application_name'] ); |
|
210 | + if ( ! empty($google_config['application_name'])) { |
|
211 | + $config->setApplicationName($google_config['application_name']); |
|
212 | 212 | } |
213 | 213 | |
214 | - if ( ! empty( $google_config['client_id'] ) ) { |
|
215 | - $config->setClientId( $google_config['client_id'] ); |
|
214 | + if ( ! empty($google_config['client_id'])) { |
|
215 | + $config->setClientId($google_config['client_id']); |
|
216 | 216 | } |
217 | 217 | |
218 | - if ( ! empty( $google_config['client_secret'] ) ) { |
|
219 | - $config->setClientSecret( $google_config['client_secret'] ); |
|
218 | + if ( ! empty($google_config['client_secret'])) { |
|
219 | + $config->setClientSecret($google_config['client_secret']); |
|
220 | 220 | } |
221 | 221 | |
222 | - if ( ! empty( $google_config['redirect_uri'] ) ) { |
|
223 | - $config->setRedirectUri( $google_config['redirect_uri'] ); |
|
222 | + if ( ! empty($google_config['redirect_uri'])) { |
|
223 | + $config->setRedirectUri($google_config['redirect_uri']); |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | return $config; |
@@ -230,20 +230,20 @@ discard block |
||
230 | 230 | * Refreshing the tokens |
231 | 231 | */ |
232 | 232 | protected function refresh_tokens() { |
233 | - if ( ( $refresh_token = $this->get_refresh_token() ) !== '' && $this->access_token_expired() ) { |
|
233 | + if (($refresh_token = $this->get_refresh_token()) !== '' && $this->access_token_expired()) { |
|
234 | 234 | try { |
235 | 235 | // Refresh the token. |
236 | - $this->refreshToken( $refresh_token ); |
|
236 | + $this->refreshToken($refresh_token); |
|
237 | 237 | |
238 | 238 | $response = $this->getAuth()->getAccessToken(); |
239 | - $response = json_decode( $response ); |
|
239 | + $response = json_decode($response); |
|
240 | 240 | |
241 | 241 | // Check response and if there is an access_token. |
242 | - if ( ! empty( $response ) && ! empty ( $response->access_token ) ) { |
|
243 | - $this->save_access_token( $response ); |
|
242 | + if ( ! empty($response) && ! empty ($response->access_token)) { |
|
243 | + $this->save_access_token($response); |
|
244 | 244 | } |
245 | 245 | } |
246 | - catch ( Exception $e ) { |
|
246 | + catch (Exception $e) { |
|
247 | 247 | return false; |
248 | 248 | } |
249 | 249 | } |
@@ -254,8 +254,8 @@ discard block |
||
254 | 254 | * |
255 | 255 | * @param string $refresh_token |
256 | 256 | */ |
257 | - protected function save_refresh_token( $refresh_token ) { |
|
258 | - update_option( $this->option_refresh_token, trim( $refresh_token ) ); |
|
257 | + protected function save_refresh_token($refresh_token) { |
|
258 | + update_option($this->option_refresh_token, trim($refresh_token)); |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | /** |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | * @return string |
265 | 265 | */ |
266 | 266 | protected function get_refresh_token() { |
267 | - return get_option( $this->option_refresh_token, '' ); |
|
267 | + return get_option($this->option_refresh_token, ''); |
|
268 | 268 | } |
269 | 269 | |
270 | 270 | |
@@ -273,21 +273,21 @@ discard block |
||
273 | 273 | * |
274 | 274 | * @param array $response |
275 | 275 | */ |
276 | - protected function save_initial_access_token( $response ) { |
|
276 | + protected function save_initial_access_token($response) { |
|
277 | 277 | update_option( |
278 | 278 | $this->option_access_token, |
279 | 279 | array( |
280 | 280 | 'refresh_token' => $response->refresh_token, |
281 | 281 | 'access_token' => $response->access_token, |
282 | - 'expires' => current_time( 'timestamp' ) + $response->expires_in, |
|
282 | + 'expires' => current_time('timestamp') + $response->expires_in, |
|
283 | 283 | 'expires_in' => $response->expires_in, |
284 | 284 | 'created' => $response->created, |
285 | 285 | ) |
286 | 286 | ); |
287 | 287 | |
288 | 288 | try { |
289 | - $this->setAccessToken( json_encode( $response ) ); |
|
290 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
289 | + $this->setAccessToken(json_encode($response)); |
|
290 | + } catch (MonsterInsights_GA_Lib_Auth_Exception $exception) { |
|
291 | 291 | |
292 | 292 | } |
293 | 293 | } |
@@ -297,21 +297,21 @@ discard block |
||
297 | 297 | * |
298 | 298 | * @param array $response |
299 | 299 | */ |
300 | - protected function save_access_token( $response ) { |
|
300 | + protected function save_access_token($response) { |
|
301 | 301 | update_option( |
302 | 302 | $this->option_access_token, |
303 | 303 | array( |
304 | 304 | 'refresh_token' => $this->get_refresh_token(), |
305 | 305 | 'access_token' => $response->access_token, |
306 | - 'expires' => current_time( 'timestamp' ) + $response->expires_in, |
|
306 | + 'expires' => current_time('timestamp') + $response->expires_in, |
|
307 | 307 | 'expires_in' => $response->expires_in, |
308 | 308 | 'created' => $response->created, |
309 | 309 | ) |
310 | 310 | ); |
311 | 311 | |
312 | 312 | try { |
313 | - $this->setAccessToken( json_encode( $response ) ); |
|
314 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
313 | + $this->setAccessToken(json_encode($response)); |
|
314 | + } catch (MonsterInsights_GA_Lib_Auth_Exception $exception) { |
|
315 | 315 | |
316 | 316 | } |
317 | 317 | } |
@@ -324,13 +324,13 @@ discard block |
||
324 | 324 | private function access_token_expired() { |
325 | 325 | $access_token = $this->get_access_token(); |
326 | 326 | |
327 | - if ( empty( $access_token ) || empty( $access_token['expires'] ) || current_time( 'timestamp' ) >= $access_token['expires'] ) { |
|
327 | + if (empty($access_token) || empty($access_token['expires']) || current_time('timestamp') >= $access_token['expires']) { |
|
328 | 328 | return true; |
329 | 329 | } |
330 | 330 | |
331 | 331 | try { |
332 | - $this->setAccessToken( json_encode( $access_token ) ); |
|
333 | - } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { |
|
332 | + $this->setAccessToken(json_encode($access_token)); |
|
333 | + } catch (MonsterInsights_GA_Lib_Auth_Exception $exception) { |
|
334 | 334 | return true; |
335 | 335 | } |
336 | 336 | return false; |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | * @return mixed |
343 | 343 | */ |
344 | 344 | public function get_access_token() { |
345 | - return get_option( $this->option_access_token, array( 'access_token' => false, 'expires' => 0 ) ); |
|
345 | + return get_option($this->option_access_token, array('access_token' => false, 'expires' => 0)); |
|
346 | 346 | } |
347 | 347 | |
348 | 348 | } |
349 | 349 | \ No newline at end of file |
@@ -242,8 +242,7 @@ |
||
242 | 242 | if ( ! empty( $response ) && ! empty ( $response->access_token ) ) { |
243 | 243 | $this->save_access_token( $response ); |
244 | 244 | } |
245 | - } |
|
246 | - catch ( Exception $e ) { |
|
245 | + } catch ( Exception $e ) { |
|
247 | 246 | return false; |
248 | 247 | } |
249 | 248 | } |