Yoast_Api_Google_Client

Class Yoast_Api_Google_Client.

Defined (1)

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

/vendor/yoast/api-libs/class-api-google-client.php  
  1. class Yoast_Api_Google_Client extends Yoast_Google_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 $api_url; 
  17.  
  18. /** 
  19. * @var string 
  20. */ 
  21. protected $http_response_code; 
  22.  
  23. /** 
  24. * Initialize the config and refresh the token 
  25. * @param array $config 
  26. * @param string $option_prefix 
  27. * @param string $api_url 
  28. */ 
  29. public function __construct( $config, $option_prefix, $api_url = '' ) { 
  30.  
  31. parent::__construct(); 
  32.  
  33. $this->option_refresh_token = $option_prefix . '-refresh_token'; 
  34. $this->option_access_token = $option_prefix . '-access_token'; 
  35.  
  36. $this->api_url = $api_url; 
  37.  
  38. // Initialize the config to set all properties properly. 
  39. $this->init_config( $config ); 
  40.  
  41. // Let's get an access token if we've got a refresh token. 
  42. $this->refresh_tokens(); 
  43.  
  44. /** 
  45. * Authenticate the client. If $authorization_code is empty it will lead the user through the validation process of 
  46. * Google. If set it will be get the access token for current session and save the refresh_token for future use 
  47. * @param mixed $authorization_code 
  48. * @return bool 
  49. */ 
  50. public function authenticate_client( $authorization_code = null ) { 
  51. static $has_retried; 
  52.  
  53. // Authenticate client. 
  54. try { 
  55. $this->authenticate( $authorization_code ); 
  56.  
  57. // Get access response. 
  58. $response = $this->getAccessToken(); 
  59.  
  60. // Check if there is a response body. 
  61. if ( ! empty( $response ) ) { 
  62. $response = json_decode( $response ); 
  63.  
  64. if ( is_object( $response ) ) { 
  65. // Save the refresh token. 
  66. $this->save_refresh_token( $response->refresh_token ); 
  67.  
  68. return true; 
  69. } catch ( Yoast_Google_AuthException $exception ) { 
  70. // If there aren't any attempts before, try again and set attempts on true, to prevent further attempts. 
  71. if ( empty( $has_retried ) ) { 
  72. $has_retried = true; 
  73.  
  74. return $this->authenticate_client( $authorization_code ); 
  75.  
  76. return false; 
  77.  
  78. /** 
  79. * Doing a request to the API 
  80. * @param string $target_request_url 
  81. * @param bool $decode_response 
  82. * @param string $request_method 
  83. * @return array 
  84. */ 
  85. public function do_request( $target_request_url, $decode_response = false, $request_method = 'GET' ) { 
  86. // Get response. 
  87. $response = $this->getIo()->authenticatedRequest( 
  88. new Yoast_Google_HttpRequest( $this->api_url . $target_request_url, $request_method ) 
  89. ); 
  90.  
  91. // Storing the response code. 
  92. $this->http_response_code = $response->getResponseHttpCode(); 
  93.  
  94. if ( $decode_response ) { 
  95. return $this->decode_response( $response ); 
  96.  
  97. return $response; 
  98.  
  99. /** 
  100. * Decode the JSON response 
  101. * @param object $response 
  102. * @param int $accepted_response_code 
  103. * @return mixed 
  104. */ 
  105. public function decode_response( $response, $accepted_response_code = 200 ) { 
  106. if ( $accepted_response_code === $response->getResponseHttpCode() ) { 
  107. return json_decode( $response->getResponseBody() ); 
  108.  
  109. /** 
  110. * Getting the response code, saved from latest request to Google 
  111. * @return mixed 
  112. */ 
  113. public function get_http_response_code() { 
  114. return $this->http_response_code; 
  115.  
  116. /** 
  117. * Clears the options and revokes the token 
  118. */ 
  119. public function clear_data() { 
  120. $this->revokeToken(); 
  121.  
  122. delete_option( $this->option_access_token ); 
  123. delete_option( $this->option_refresh_token ); 
  124.  
  125. /** 
  126. * Check if user is authenticated 
  127. * @return bool 
  128. */ 
  129. public function is_authenticated() { 
  130. $has_refresh_token = ( $this->get_refresh_token() !== '' ); 
  131. $access_token_expired = $this->access_token_expired(); 
  132.  
  133. return $has_refresh_token && ! $access_token_expired; 
  134.  
  135. /** 
  136. * Initialize the config, will merge given config with default config to be sure all settings are available 
  137. * @param array $config 
  138. */ 
  139. protected function init_config( array $config ) { 
  140. if ( ! empty( $config['application_name'] ) ) { 
  141. $this->setApplicationName( $config['application_name'] ); 
  142.  
  143. if ( ! empty( $config['client_id'] ) ) { 
  144. $this->setClientId( $config['client_id'] ); 
  145.  
  146. if ( ! empty( $config['client_secret'] ) ) { 
  147. $this->setClientSecret( $config['client_secret'] ); 
  148.  
  149. // Set our settings. 
  150. $this->setRedirectUri( $config['redirect_uri'] ); 
  151. $this->setScopes( $config['scopes'] ); 
  152. $this->setAccessType( 'offline' ); 
  153.  
  154. /** 
  155. * Refreshing the tokens 
  156. */ 
  157. protected function refresh_tokens() { 
  158. if ( ( $refresh_token = $this->get_refresh_token() ) !== '' && $this->access_token_expired() ) { 
  159. try { 
  160. // Refresh the token. 
  161. $this->refreshToken( $refresh_token ); 
  162.  
  163. $response = $this->getAuth()->token; 
  164.  
  165. // Check response and if there is an access_token. 
  166. if ( ! empty( $response ) && ! empty ( $response['access_token'] ) ) { 
  167. $this->save_access_token( $response ); 
  168. catch ( Exception $e ) { 
  169. return false; 
  170.  
  171. /** 
  172. * Save the refresh token 
  173. * @param string $refresh_token 
  174. */ 
  175. protected function save_refresh_token( $refresh_token ) { 
  176. update_option( $this->option_refresh_token, trim( $refresh_token ) ); 
  177.  
  178. /** 
  179. * Return refresh token 
  180. * @return string 
  181. */ 
  182. protected function get_refresh_token() { 
  183. return get_option( $this->option_refresh_token, '' ); 
  184.  
  185. /** 
  186. * Saving the access token as an option for further use till it expires. 
  187. * @param array $response 
  188. */ 
  189. protected function save_access_token( $response ) { 
  190. update_option( 
  191. $this->option_access_token,  
  192. array( 
  193. 'refresh_token' => $this->get_refresh_token(),  
  194. 'access_token' => $response['access_token'],  
  195. 'expires' => current_time( 'timestamp' ) + $response['expires_in'],  
  196. 'expires_in' => $response['expires_in'],  
  197. 'created' => $response['created'],  
  198. ); 
  199.  
  200. $this->setAccessToken( json_encode( $response ) ); 
  201.  
  202. /** 
  203. * Check if current access token is expired. 
  204. * @return bool 
  205. */ 
  206. private function access_token_expired() { 
  207. $access_token = $this->get_access_token(); 
  208.  
  209. if ( current_time( 'timestamp' ) >= $access_token['expires'] ) { 
  210. return true; 
  211.  
  212. $this->setAccessToken( json_encode( $access_token ) ); 
  213.  
  214. /** 
  215. * Getting the current access token from the options 
  216. * @return mixed 
  217. */ 
  218. private function get_access_token() { 
  219. return get_option( $this->option_access_token, array( 'access_token' => false, 'expires' => 0 ) ); 
  220.