PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.57
Timetics – Appointment Booking Calendar & Scheduling v1.0.57
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / core / integrations / google / client.php

client.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.57, at core/integrations/google/client.php

229 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Google Calendar Auth Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Integrations\Google;
8
9 use Exception;
10 use InvalidArgumentException;
11
12 /**
13 * Class Auth
14 */
15 class Client {
16 const TIMETICS_REVOKE_URI = 'https://oauth2.googleapis.com/revoke';
17 const TIMETICS_TOKEN_URI = 'https://oauth2.googleapis.com/token';
18 const TIMETICS_AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth';
19 const API_BASE_PATH = 'https://www.googleapis.com';
20
21 /**
22 * Store google app client id
23 *
24 * @var string
25 */
26 private $client_id;
27
28 /**
29 * Store google app secrete
30 *
31 * @var string
32 */
33 private $client_secrete;
34
35 /**
36 * Store google app auth scope
37 *
38 * @var string
39 */
40 private $auth_scope;
41
42 /**
43 * Store google auth redirect uri
44 *
45 * @var string
46 */
47 private $redirect_uri;
48
49 /**
50 * Get app client id
51 *
52 * @return string
53 */
54 public function get_client_id() {
55 return $this->client_id;
56 }
57
58 /**
59 * Get app secrete
60 *
61 * @return string
62 */
63 public function get_client_secrete() {
64 return $this->client_secrete;
65 }
66
67 /**
68 * Get app auth scope
69 *
70 * @return string
71 */
72 public function get_auth_scope() {
73 return $this->auth_scope;
74 }
75
76 /**
77 * Get auth redirect uri
78 *
79 * @return string
80 */
81 public function get_redirect_uri() {
82 return $this->redirect_uri;
83 }
84
85 /**
86 * Get auth url
87 *
88 * @return string
89 */
90 public function get_auth_url( $state = '' ) {
91 $params = array(
92 'client_id' => $this->client_id,
93 'scope' => urlencode_deep( $this->auth_scope ),
94 'redirect_uri' => $this->redirect_uri,
95 'response_type' => 'code',
96 'access_type' => 'offline',
97 );
98
99 if ( ! empty( $state ) ) {
100 $params['state'] = $state;
101 }
102
103 return add_query_arg( $params, self::TIMETICS_AUTH_URL );
104 }
105
106 /**
107 * Set google authentication configuration
108 *
109 * @return void
110 */
111 public function set_auth_config( $args = array() ) {
112 $defaults = array(
113 'client_id' => '',
114 'client_secrete' => '',
115 );
116
117 $args = wp_parse_args( $args, $defaults );
118
119 $this->client_id = $args['client_id'];
120 $this->client_secrete = $args['client_secrete'];
121 }
122
123 /**
124 * Set authentication scope
125 *
126 * @return void
127 */
128 public function add_scope( $scope ) {
129 $this->auth_scope = $scope;
130 }
131
132 /**
133 * Set redirect uri
134 *
135 * @param string $uri Redirect uri
136 *
137 * @return void
138 */
139 public function set_redirect_uri( $uri ) {
140 $this->redirect_uri = $uri;
141 }
142
143 /**
144 * Fetch access token
145 *
146 * @param string $code
147 *
148 * @return void
149 */
150 public function fetch_access_token_with_auth_code( $code ) {
151 if ( strlen( $code ) === 0 ) {
152 throw new InvalidArgumentException( 'Invalid code' );
153 }
154
155 $args = array(
156 'client_id' => $this->client_id,
157 'client_secret' => $this->client_secrete,
158 'code' => $code,
159 'redirect_uri' => $this->redirect_uri,
160 'grant_type' => 'authorization_code',
161 );
162
163 $response = wp_remote_post( self::TIMETICS_TOKEN_URI, array( 'body' => $args ) );
164
165 $status_code = wp_remote_retrieve_response_code( $response );
166
167 if ( 200 != $status_code ) {
168 return false;
169 }
170
171 $data = json_decode( wp_remote_retrieve_body( $response ), true );
172
173 return $data;
174 }
175
176 /**
177 * Fetch access token by using refresh token
178 *
179 * @return void
180 */
181 public function fetch_access_token_with_refresh_token( $refresh_token ) {
182 $args = array(
183 'client_id' => $this->client_id,
184 'client_secret' => $this->client_secrete,
185 'refresh_token' => $refresh_token,
186 'redirect_uri' => $this->redirect_uri,
187 'grant_type' => 'refresh_token',
188 );
189
190 $response = wp_remote_post( self::TIMETICS_TOKEN_URI, array( 'body' => $args ) );
191
192 $status_code = wp_remote_retrieve_response_code( $response );
193
194 if ( 200 != $status_code ) {
195 return false;
196 }
197
198 $data = json_decode( wp_remote_retrieve_body( $response ), true );
199
200 return $data;
201 }
202
203 /**
204 * Revoke authorization
205 *
206 * @param string $token
207 *
208 * @return bool
209 */
210 public function revoke( $token ) {
211 $response = wp_remote_post(
212 self::TIMETICS_REVOKE_URI, array(
213 'headers' => array(
214 'content-type' => 'application/x-www-form-urlencoded',
215 ),
216 'body' => build_query(
217 array(
218 'token' => $token,
219 )
220 ),
221 )
222 );
223
224 $status_code = wp_remote_retrieve_response_code( $response );
225
226 return $status_code === 200;
227 }
228 }
229