PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.11
Timetics – Appointment Booking Calendar & Scheduling v1.0.11
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.11, at core/integrations/google/client.php

224 lines 4.8 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() {
91 $auth_url = add_query_arg(
92 [
93 'client_id' => $this->client_id,
94 'scope' => urlencode_deep( $this->auth_scope ),
95 'redirect_uri' => $this->redirect_uri,
96 'response_type' => 'code',
97 'access_type' => 'offline',
98
99 ], self::TIMETICS_AUTH_URL
100 );
101
102 return $auth_url;
103 }
104
105 /**
106 * Set google authentication configuration
107 *
108 * @return void
109 */
110 public function set_auth_config( $args = [] ) {
111 $defaults = [
112 'client_id' => '',
113 'client_secrete' => '',
114 ];
115
116 $args = wp_parse_args( $args, $defaults );
117
118 $this->client_id = $args['client_id'];
119 $this->client_secrete = $args['client_secrete'];
120 }
121
122 /**
123 * Set authentication scope
124 *
125 * @return void
126 */
127 public function add_scope( $scope ) {
128 $this->auth_scope = $scope;
129 }
130
131 /**
132 * Set redirect uri
133 *
134 * @param string $uri Redirect uri
135 *
136 * @return void
137 */
138 public function set_redirect_uri( $uri ) {
139 $this->redirect_uri = $uri;
140 }
141
142 /**
143 * Fetch access token
144 *
145 * @param string $code
146 *
147 * @return void
148 */
149 public function fetch_access_token_with_auth_code( $code ) {
150 if ( strlen( $code ) === 0 ) {
151 throw new InvalidArgumentException( 'Invalid code' );
152 }
153
154 $args = [
155 'client_id' => $this->client_id,
156 'client_secret' => $this->client_secrete,
157 'code' => $code,
158 'redirect_uri' => $this->redirect_uri,
159 'grant_type' => 'authorization_code',
160 ];
161
162 $response = wp_remote_post( self::TIMETICS_TOKEN_URI, [ 'body' => $args ] );
163
164 if ( ! is_wp_error( $response ) ) {
165 $data = json_decode( wp_remote_retrieve_body( $response ), true );
166
167 return $data;
168 }
169
170 return $response;
171 }
172
173 /**
174 * Fetch access token by using refresh token
175 *
176 * @return void
177 */
178 public function fetch_access_token_with_refresh_token( $refresh_token ) {
179 $args = [
180 'client_id' => $this->client_id,
181 'client_secret' => $this->client_secrete,
182 'refresh_token' => $refresh_token,
183 'redirect_uri' => $this->redirect_uri,
184 'grant_type' => 'refresh_token',
185 ];
186
187 $response = wp_remote_post( self::TIMETICS_TOKEN_URI, [ 'body' => $args ] );
188
189 if ( ! is_wp_error( $response ) ) {
190 $data = json_decode( wp_remote_retrieve_body( $response ), true );
191
192 return $data;
193 }
194
195 return $response;
196 }
197
198 /**
199 * Revoke authorization
200 *
201 * @param string $token
202 *
203 * @return bool
204 */
205 public function revoke( $token ) {
206 $response = wp_remote_post(
207 self::TIMETICS_REVOKE_URI, [
208 'headers' => [
209 'content-type' => 'application/x-www-form-urlencoded',
210 ],
211 'body' => build_query(
212 [
213 'token' => $token,
214 ]
215 ),
216 ]
217 );
218
219 $status_code = wp_remote_retrieve_response_code( $response );
220
221 return $status_code === 200;
222 }
223 }
224