| 1 |
<?php |
| 2 |
/** |
| 3 |
* Authentication for integrations |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Integrations; |
| 9 |
|
| 10 |
use Timetics\Core\Integrations\Google\Service\Calendar; |
| 11 |
use Timetics\Utils\Singleton; |
| 12 |
|
| 13 |
/** |
| 14 |
* Auth Class |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
class Auth { |
| 19 |
use Singleton; |
| 20 |
|
| 21 |
/** |
| 22 |
* Initialize |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function init() { |
| 27 |
add_action( 'template_redirect', [ $this, 'authenticate' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Authenticate integration |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function authenticate() { |
| 36 |
$query_var = get_query_var( 'timetics-integration', false ); |
| 37 |
$user_id = get_current_user_id(); |
| 38 |
|
| 39 |
$code = isset( $_GET['code'] ) ? sanitize_text_field( $_GET['code'] ) : ''; |
| 40 |
|
| 41 |
if ( ! $query_var ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
switch ( $query_var ) { |
| 46 |
case 'google-auth': |
| 47 |
$this->google_auth( $code ); |
| 48 |
break; |
| 49 |
} |
| 50 |
|
| 51 |
do_action('timetics_integration_auth', $query_var, $code ); |
| 52 |
|
| 53 |
wp_redirect( admin_url( 'admin.php?page=timetics#/my-profile') ); |
| 54 |
exit; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Authentication for google |
| 59 |
* |
| 60 |
* @param string $code |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function google_auth( $code = '' ) { |
| 65 |
$client = timetics_get_google_client(); |
| 66 |
|
| 67 |
$client->add_scope( Calendar::scope() ); |
| 68 |
$data = $client->fetch_access_token_with_auth_code( $code ); |
| 69 |
$data['code'] = $code; |
| 70 |
|
| 71 |
timetics_update_google_auth( get_current_user_id(), $data ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Authentication for zoom |
| 76 |
* |
| 77 |
* @param string $code |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function zoom_auth( $code = '' ) { |
| 82 |
// Override this function to authenticate zoom |
| 83 |
} |
| 84 |
} |
| 85 |
|