/vendor/yoast/api-libs/class-api-google-client.php

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