MonsterInsights_GA_Client

The Google Analytics for WordPress by MonsterInsights MonsterInsights GA Client class.

Defined (1)

The class is defined in the following location(s).

/assets/lib/google/class-ga-client.php  
  1. class MonsterInsights_GA_Client extends MonsterInsights_GA_Lib_Client { 
  2.  
  3. /** 
  4. * @var string 
  5. */ 
  6. protected $option_refresh_token; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. protected $option_access_token; 
  12.  
  13. /** 
  14. * @var string 
  15. */ 
  16. protected $http_response_code; 
  17.  
  18. /** 
  19. * @var string 
  20. */ 
  21. public $client; // used for reporting purposes 
  22.  
  23. /** 
  24. * Initialize the config and refresh the token 
  25. * @param array $config 
  26. * @param string $option_prefix 
  27. */ 
  28. public function __construct( $google_settings, $client_name ) { 
  29.  
  30. // Initialize the config to set all properties properly. 
  31. $config = $this->init_config( $google_settings ); 
  32.  
  33. parent::__construct( $config ); 
  34.  
  35. $this->client = new MonsterInsights_GA_Lib_Client( $config ); 
  36.  
  37. if ( ! empty( $google_settings['scopes'] ) ) { 
  38. $this->setScopes( $google_settings['scopes'] ); 
  39.  
  40. $this->setAccessType( 'offline' ); 
  41.  
  42. // Client name will be `pro`, `lite`, or `support` 
  43. $this->option_refresh_token = 'monsterinsights_' . $client_name . '_refresh_token'; 
  44. $this->option_access_token = 'monsterinsights_' . $client_name . '_access_token'; 
  45.  
  46. // Let's get an access token if we've got a refresh token. 
  47. $this->refresh_tokens(); 
  48.  
  49. /** 
  50. * Authenticate the client. If $authorization_code is empty it will lead the user through the validation process of 
  51. * Google. If set it will be get the access token for current session and save the refresh_token for future use 
  52. * @param mixed $authorization_code 
  53. * @return bool 
  54. */ 
  55. public function authenticate_client( $authorization_code = null ) { 
  56. static $has_retried; 
  57.  
  58. // Authenticate client. 
  59. try { 
  60. $this->authenticate( $authorization_code ); 
  61.  
  62. // Get access response. 
  63. $response = $this->getAccessToken(); 
  64.  
  65. // Check if there is a response body. 
  66. if ( ! empty( $response ) ) { 
  67. $response = json_decode( $response ); 
  68.  
  69. if ( is_object( $response ) ) { 
  70. // Save the refresh token. 
  71. $this->save_refresh_token( $response->refresh_token ); 
  72. $this->save_initial_access_token( $response ); 
  73. return true; 
  74. } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { 
  75. // If there aren't any attempts before, try again and set attempts on true, to prevent further attempts. 
  76. if ( empty( $has_retried ) ) { 
  77. $has_retried = true; 
  78.  
  79. return $this->authenticate_client( $authorization_code ); 
  80. //error_log( $exception ); // @todo proper logging and handling of already used oauth token 
  81.  
  82. return false; 
  83.  
  84. /** 
  85. * Doing a request to the API 
  86. * @param string $target_request_url 
  87. * @param bool $decode_response 
  88. * @param string $request_method 
  89. * @return array 
  90. */ 
  91. public function do_request( $target_request_url, $decode_response = false, $request_method = 'GET', $body = array() ) { 
  92. // Get response. 
  93. $request = new MonsterInsights_GA_Lib_Http_Request( $target_request_url, $request_method ); 
  94. if ( ! empty( $body ) ) { 
  95. $request->setPostBody( $body ); // used exclusively for auth profiles 
  96.  
  97. $response = $this->getAuth()->authenticatedRequest( $request ); 
  98.  
  99. // Storing the response code. 
  100. $this->http_response_code = $response->getResponseHttpCode(); 
  101.  
  102. if ( $decode_response ) { 
  103. return $this->decode_response( $response ); 
  104.  
  105. return $response; 
  106.  
  107. /** 
  108. * Decode the JSON response 
  109. * @param object $response 
  110. * @param int $accepted_response_code 
  111. * @return mixed 
  112. */ 
  113. public function decode_response( $response, $accepted_response_code = 200 ) { 
  114. if ( $accepted_response_code === $response->getResponseHttpCode() ) { 
  115. return json_decode( $response->getResponseBody() ); 
  116.  
  117. /** 
  118. * Getting the response code, saved from latest request to Google 
  119. * @return mixed 
  120. */ 
  121. public function get_http_response_code() { 
  122. return $this->http_response_code; 
  123.  
  124. /** 
  125. * Clears the options and revokes the token 
  126. */ 
  127. public function clear_data() { 
  128. $this->revokeToken(); 
  129. delete_option( $this->option_access_token ); 
  130. delete_option( $this->option_refresh_token ); 
  131.  
  132. /** 
  133. * Moves test options to live 
  134. */ 
  135. public function move_test_to_live() { 
  136. $new_option_access_token = str_replace( 'test_', '', $this->option_access_token ); 
  137. $new_option_refresh_token = str_replace( 'test_', '', $this->option_refresh_token ); 
  138.  
  139. update_option( $new_option_access_token, get_option( $this->option_access_token, '' ) ); 
  140. update_option( $new_option_refresh_token, get_option( $this->option_refresh_token, '' ) ); 
  141.  
  142. delete_option( $this->option_access_token ); 
  143. delete_option( $this->option_refresh_token ); 
  144.  
  145. /** 
  146. * Check if user is authenticated 
  147. * @return bool 
  148. */ 
  149. public function is_authenticated() { 
  150. $has_refresh_token = ( $this->get_refresh_token() !== '' ); 
  151. $access_token_expired = $this->access_token_expired(); 
  152.  
  153. return $has_refresh_token && ! $access_token_expired; 
  154.  
  155. /** 
  156. * Initialize the config, will merge given config with default config to be sure all settings are available 
  157. * @param array $settings 
  158. */ 
  159. protected function init_config( array $google_config ) { 
  160.  
  161. // Load MI io 
  162. require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-io.php'; 
  163.  
  164. // Load MI cache 
  165. require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-cache.php'; 
  166.  
  167. // Load MI logger 
  168. require_once MONSTERINSIGHTS_PLUGIN_DIR . 'assets/lib/google/class-ga-logger.php'; 
  169.  
  170. $config = new MonsterInsights_GA_Lib_Config(); 
  171.  
  172. $config->setIoClass( 'MonsterInsights_GA_IO' ); 
  173. $config->setCacheClass( 'MonsterInsights_GA_Cache' ); 
  174. $config->setLoggerClass( 'MonsterInsights_GA_Logger' ); 
  175.  
  176. if ( ! empty( $google_config['application_name'] ) ) { 
  177. $config->setApplicationName( $google_config['application_name'] ); 
  178.  
  179. if ( ! empty( $google_config['client_id'] ) ) { 
  180. $config->setClientId( $google_config['client_id'] ); 
  181.  
  182. if ( ! empty( $google_config['client_secret'] ) ) { 
  183. $config->setClientSecret( $google_config['client_secret'] ); 
  184.  
  185. if ( ! empty( $google_config['redirect_uri'] ) ) { 
  186. $config->setRedirectUri( $google_config['redirect_uri'] ); 
  187.  
  188. return $config; 
  189.  
  190. /** 
  191. * Refreshing the tokens 
  192. */ 
  193. protected function refresh_tokens() { 
  194. if ( ( $refresh_token = $this->get_refresh_token() ) !== '' && $this->access_token_expired() ) { 
  195. try { 
  196. // Refresh the token. 
  197. $this->refreshToken( $refresh_token ); 
  198.  
  199. $response = $this->getAuth()->getAccessToken(); 
  200. $response = json_decode( $response ); 
  201.  
  202. // Check response and if there is an access_token. 
  203. if ( ! empty( $response ) && ! empty ( $response->access_token ) ) { 
  204. $this->save_access_token( $response ); 
  205. catch ( Exception $e ) { 
  206. return false; 
  207.  
  208. /** 
  209. * Save the refresh token 
  210. * @param string $refresh_token 
  211. */ 
  212. protected function save_refresh_token( $refresh_token ) { 
  213. update_option( $this->option_refresh_token, trim( $refresh_token ) ); 
  214.  
  215. /** 
  216. * Return refresh token 
  217. * @return string 
  218. */ 
  219. protected function get_refresh_token() { 
  220. return get_option( $this->option_refresh_token, '' ); 
  221.  
  222.  
  223. /** 
  224. * Saving the access token as an option for further use till it expires. 
  225. * @param array $response 
  226. */ 
  227. protected function save_initial_access_token( $response ) { 
  228. update_option( 
  229. $this->option_access_token,  
  230. array( 
  231. 'refresh_token' => $response->refresh_token,  
  232. 'access_token' => $response->access_token,  
  233. 'expires' => current_time( 'timestamp' ) + $response->expires_in,  
  234. 'expires_in' => $response->expires_in,  
  235. 'created' => $response->created,  
  236. ); 
  237.  
  238. try { 
  239. $this->setAccessToken( json_encode( $response ) ); 
  240. } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { 
  241.  
  242.  
  243. /** 
  244. * Saving the access token as an option for further use till it expires. 
  245. * @param array $response 
  246. */ 
  247. protected function save_access_token( $response ) { 
  248. update_option( 
  249. $this->option_access_token,  
  250. array( 
  251. 'refresh_token' => $this->get_refresh_token(),  
  252. 'access_token' => $response->access_token,  
  253. 'expires' => current_time( 'timestamp' ) + $response->expires_in,  
  254. 'expires_in' => $response->expires_in,  
  255. 'created' => $response->created,  
  256. ); 
  257.  
  258. try { 
  259. $this->setAccessToken( json_encode( $response ) ); 
  260. } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { 
  261.  
  262.  
  263. /** 
  264. * Check if current access token is expired. 
  265. * @return bool 
  266. */ 
  267. private function access_token_expired() { 
  268. $access_token = $this->get_access_token(); 
  269.  
  270. if ( empty( $access_token ) || empty( $access_token['expires'] ) || current_time( 'timestamp' ) >= $access_token['expires'] ) { 
  271. return true; 
  272.  
  273. try { 
  274. $this->setAccessToken( json_encode( $access_token ) ); 
  275. } catch ( MonsterInsights_GA_Lib_Auth_Exception $exception ) { 
  276. return true; 
  277. return false; 
  278.  
  279. /** 
  280. * Getting the current access token from the options 
  281. * @return mixed 
  282. */ 
  283. public function get_access_token() { 
  284. return get_option( $this->option_access_token, array( 'access_token' => false, 'expires' => 0 ) ); 
  285.