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

228 lines 5.0 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 array(
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 = array() ) {
111 $defaults = array(
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 = array(
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, array( 'body' => $args ) );
163
164 $status_code = wp_remote_retrieve_response_code( $response );
165
166 if ( 200 != $status_code ) {
167 return false;
168 }
169
170 $data = json_decode( wp_remote_retrieve_body( $response ), true );
171
172 return $data;
173 }
174
175 /**
176 * Fetch access token by using refresh token
177 *
178 * @return void
179 */
180 public function fetch_access_token_with_refresh_token( $refresh_token ) {
181 $args = array(
182 'client_id' => $this->client_id,
183 'client_secret' => $this->client_secrete,
184 'refresh_token' => $refresh_token,
185 'redirect_uri' => $this->redirect_uri,
186 'grant_type' => 'refresh_token',
187 );
188
189 $response = wp_remote_post( self::TIMETICS_TOKEN_URI, array( 'body' => $args ) );
190
191 $status_code = wp_remote_retrieve_response_code( $response );
192
193 if ( 200 != $status_code ) {
194 return false;
195 }
196
197 $data = json_decode( wp_remote_retrieve_body( $response ), true );
198
199 return $data;
200 }
201
202 /**
203 * Revoke authorization
204 *
205 * @param string $token
206 *
207 * @return bool
208 */
209 public function revoke( $token ) {
210 $response = wp_remote_post(
211 self::TIMETICS_REVOKE_URI, array(
212 'headers' => array(
213 'content-type' => 'application/x-www-form-urlencoded',
214 ),
215 'body' => build_query(
216 array(
217 'token' => $token,
218 )
219 ),
220 )
221 );
222
223 $status_code = wp_remote_retrieve_response_code( $response );
224
225 return $status_code === 200;
226 }
227 }
228