| @@ -49,9 +49,15 @@ | ||
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | do_action('timetics_integration_auth', $query_var, $code ); |
| 52 | 52 | |
| 53 | - wp_redirect( admin_url( 'admin.php?page=timetics#/my-profile') ); | |
| 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 ); | |
| 54 | 60 | exit; |
| 55 | 61 | } |
| 56 | 62 | |
| 57 | 63 | /** |
| @@ -63,13 +69,34 @@ | ||
| 63 | 69 | */ |
| 64 | 70 | public function google_auth( $code = '' ) { |
| 65 | 71 | $client = timetics_get_google_client(); |
| 66 | 72 | |
| 67 | - $client->add_scope( Calendar::scope() ); | |
| 68 | - $data = $client->fetch_access_token_with_auth_code( $code ); | |
| 69 | - $data['code'] = $code; | |
| 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 | + } | |
| 70 | 80 | |
| 71 | - timetics_update_google_auth( get_current_user_id(), $data ); | |
| 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 | + } | |
| 72 | 99 | } |
| 73 | 100 | |
| 74 | 101 | /** |
| 75 | 102 | * Authentication for zoom |