| 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 |
$redirect_url = admin_url('admin.php?page=timetics#/my-profile'); |
| 54 |
|
| 55 |
if ( current_user_can('manage_options') ) { |
| 56 |
$redirect_url = admin_url('admin.php?page=timetics#/settings'); |
| 57 |
} |
| 58 |
|
| 59 |
wp_redirect( $redirect_url ); |
| 60 |
exit; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Authentication for google |
| 65 |
* |
| 66 |
* @param string $code |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function google_auth( $code = '' ) { |
| 71 |
$client = timetics_get_google_client(); |
| 72 |
|
| 73 |
// If no code is provided, redirect to Google's authorization page |
| 74 |
if ( empty($code) ) { |
| 75 |
$client->add_scope( Calendar::scope() ); |
| 76 |
$auth_url = $client->get_auth_url(); |
| 77 |
wp_redirect($auth_url); |
| 78 |
exit; |
| 79 |
} |
| 80 |
|
| 81 |
try { |
| 82 |
$client->add_scope( Calendar::scope() ); |
| 83 |
$data = $client->fetch_access_token_with_auth_code( $code ); |
| 84 |
$data['code'] = $code; |
| 85 |
|
| 86 |
timetics_update_google_auth( get_current_user_id(), $data ); |
| 87 |
|
| 88 |
wp_redirect(admin_url('admin.php?page=timetics#/settings')); |
| 89 |
exit; |
| 90 |
} catch (\Exception $e) { |
| 91 |
$error_message = sprintf( |
| 92 |
'<p>%s</p><p><a href="%s" class="button button-primary">Go Back</a></p>', |
| 93 |
esc_html( $e->getMessage() ), |
| 94 |
esc_url( admin_url('admin.php?page=timetics#/settings') ) |
| 95 |
); |
| 96 |
|
| 97 |
wp_die($error_message, esc_html__('Google Auth Error', 'timetics'), array('back_link' => true)); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Authentication for zoom |
| 103 |
* |
| 104 |
* @param string $code |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function zoom_auth( $code = '' ) { |
| 109 |
// Override this function to authenticate zoom |
| 110 |
} |
| 111 |
} |
| 112 |
|