PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.60
Timetics – Appointment Booking Calendar & Scheduling v1.0.60
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 / auth.php

auth.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.60, at core/integrations/auth.php

128 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Authentication for integrations
4 *
5 * @package Timetics
6 */
7
8 namespace Timetics\Core\Integrations;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use Timetics\Core\Integrations\Google\Service\Calendar;
13 use Timetics\Utils\Singleton;
14
15 /**
16 * Auth Class
17 *
18 * @since 1.0.0
19 */
20 class Auth {
21 use Singleton;
22
23 /**
24 * Initialize
25 *
26 * @return void
27 */
28 public function init() {
29 add_action( 'template_redirect', [ $this, 'authenticate' ] );
30 }
31
32 /**
33 * Authenticate integration
34 *
35 * @return void
36 */
37 public function authenticate() {
38 $query_var = get_query_var( 'timetics-integration', false );
39 $user_id = get_current_user_id();
40
41 $code = isset( $_GET['code'] ) ? sanitize_text_field( wp_unslash( $_GET['code'] ) ) : '';
42
43 // Verify nonce for security.
44 if ( empty( $code ) ) {
45 if ( ! isset( $_GET['timetics_auth_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['timetics_auth_nonce'] ) ), 'timetics_auth_action' ) ) {
46 return;
47 }
48 } else {
49 $state = isset( $_GET['state'] ) ? sanitize_text_field( wp_unslash( $_GET['state'] ) ) : '';
50 if ( ! wp_verify_nonce( $state, 'timetics_auth_callback' ) ) {
51 return;
52 }
53 }
54
55 if ( ! $query_var ) {
56 return;
57 }
58
59 switch ( $query_var ) {
60 case 'google-auth':
61 $this->google_auth( $code );
62 break;
63 }
64
65 do_action('timetics_integration_auth', $query_var, $code );
66
67 $redirect_url = admin_url('admin.php?page=timetics#/my-profile');
68
69 if ( current_user_can('manage_options') ) {
70 $redirect_url = admin_url('admin.php?page=timetics#/settings');
71 }
72
73 wp_safe_redirect( $redirect_url );
74 exit;
75 }
76
77 /**
78 * Authentication for google
79 *
80 * @param string $code
81 *
82 * @return void
83 */
84 public function google_auth( $code = '' ) {
85 $client = timetics_get_google_client();
86
87 // If no code is provided, redirect to Google's authorization page.
88 if ( empty( $code ) ) {
89 $state = wp_create_nonce( 'timetics_auth_callback' );
90 $client->add_scope( Calendar::scope() );
91 $auth_url = $client->get_auth_url( $state );
92 // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Redirecting to external Google OAuth URL.
93 wp_redirect( $auth_url );
94 exit;
95 }
96
97 try {
98 $client->add_scope( Calendar::scope() );
99 $data = $client->fetch_access_token_with_auth_code( $code );
100 $data['code'] = $code;
101
102 timetics_update_google_auth( get_current_user_id(), $data );
103
104 wp_safe_redirect(admin_url('admin.php?page=timetics#/settings'));
105 exit;
106 } catch (\Exception $e) {
107 $error_message = sprintf(
108 '<p>%s</p><p><a href="%s" class="button button-primary">Go Back</a></p>',
109 esc_html( $e->getMessage() ),
110 esc_url( admin_url('admin.php?page=timetics#/settings') )
111 );
112
113 wp_die( wp_kses_post( $error_message ), esc_html__('Google Auth Error', 'timetics'), array('back_link' => true));
114 }
115 }
116
117 /**
118 * Authentication for zoom
119 *
120 * @param string $code
121 *
122 * @return void
123 */
124 public function zoom_auth( $code = '' ) {
125 // Override this function to authenticate zoom
126 }
127 }
128