get_plugin_settings(); $auth_options = get_option(self::ALPHAGF_TOKEN_DATA, []); if (empty($auth_options['refresh_token']) || empty($settings['client_id']) || empty($settings['client_secret'])) { return false; } $auth_url = 'https://oauth2.googleapis.com/token'; $args = [ 'client_id' => $settings['client_id'], 'client_secret' => $settings['client_secret'] ]; if ($grant_type === 'refresh_token') { $args['grant_type'] = 'refresh_token'; $args['refresh_token'] = $auth_options['refresh_token']; } $postargs = array( 'body' => $args, 'timeout' => 45, 'sslverify' => true, ); $response = wp_remote_post($auth_url, $postargs); // A network hiccup shouldn't mark the connection as broken; just fail this request. if (is_wp_error($response)) { return false; } $authObj = json_decode(wp_remote_retrieve_body($response), true); if (! empty($authObj['access_token'])) { $access = [ 'access_token' => $authObj['access_token'], 'refresh_token' => $auth_options['refresh_token'], 'expires_in' => time() + absint(rgar($authObj, 'expires_in', 3600)), 'update_from' => 'generate with refresh', ]; update_option(self::ALPHAGF_TOKEN_DATA, $access); update_option(self::ALPHAGF_GC_AUTH_VERIFIED, 1); return $authObj['access_token']; } update_option(self::ALPHAGF_GC_AUTH_VERIFIED, 0); return false; } /** * Exchanges an authorization code for an access token. * * @param string $code The authorization code from Google. * @return string|false The access token or false on failure. */ public static function authenticate($code) { // Fixed: Default value should be array [], not true $settings = ALPHAGF_Google_Calendar::get_instance()->get_plugin_settings(); if (empty($settings['client_id']) || empty($settings['client_secret']) || empty($code)) { return false; } $auth_url = 'https://oauth2.googleapis.com/token'; $client = [ 'client_id' => $settings['client_id'], 'client_secret' => $settings['client_secret'], 'redirect_uri' => $settings['redirect_uri'], 'grant_type' => 'authorization_code', 'code' => $code ]; $postargs = array( 'body' => $client, ); $response = wp_remote_post($auth_url, $postargs); if (is_wp_error($response)) { return false; } $response_code = wp_remote_retrieve_response_code($response); if ($response_code !== 200) { update_option(self::ALPHAGF_GC_AUTH_VERIFIED, 0); return false; } $authObj = json_decode(wp_remote_retrieve_body($response), true); $access_token = false; if (!empty($authObj['access_token'])) { $access = []; $access['access_token'] = $authObj['access_token']; $access['refresh_token'] = rgar($authObj, 'refresh_token'); $access['expires_in'] = time() + absint(rgar($authObj, 'expires_in', 3600)); $access_token = true; $access['update_from'] = 'authenticate'; update_option(self::ALPHAGF_TOKEN_DATA, $access); update_option(self::ALPHAGF_GC_AUTH_VERIFIED, 1); delete_transient(self::ALPHAGF_GC_CALENDER_LIST); self::get_auth_account_info($authObj['access_token']); } return $access_token; } public static function get_calendar_lists() { $access_token = self::get_access_token(); if (empty($access_token)) { return []; } $request_url = 'https://www.googleapis.com/calendar/v3/users/me/calendarList?minAccessRole=owner&fields=items(id,summary)'; $headers = [ 'content-type' => 'application/json', 'Authorization' => 'Bearer ' . $access_token, ]; $req_args = array( 'method' => 'GET', 'headers' => $headers, 'sslverify' => true, ); $response = wp_remote_get($request_url, $req_args); if (is_wp_error($response) || 200 !== (int) wp_remote_retrieve_response_code($response)) { return []; } $result = json_decode(wp_remote_retrieve_body($response), true); $items = is_array(rgar($result, 'items')) ? $result['items'] : []; // Cache for an hour so newly created calendars show up without reconnecting. set_transient(self::ALPHAGF_GC_CALENDER_LIST, $items, HOUR_IN_SECONDS); return $items; } /** * Create an event on the given calendar. * * @return array|WP_Error The created event, or an error describing why Google rejected it. */ public static function make_request($calendar_id, $body = array(), $method = 'POST', $response_code = 200) { $access_token = self::get_access_token(); if (empty($access_token)) { return new WP_Error('alphagf_gc_no_token', 'Unable to get a Google access token. Please reconnect your account.'); } $headers = [ 'content-type' => 'application/json', 'Authorization' => 'Bearer ' . $access_token, ]; $args = array( 'method' => $method, 'headers' => $headers, 'body' => wp_json_encode($body), 'sslverify' => true, 'timeout' => 30, ); $request_url = 'https://www.googleapis.com/calendar/v3/calendars/' . rawurlencode($calendar_id) . '/events'; $result = wp_remote_request($request_url, $args); if (is_wp_error($result)) { return $result; } $response = \GFCommon::maybe_decode_json(wp_remote_retrieve_body($result)); $code = (int) wp_remote_retrieve_response_code($result); if ($code !== $response_code) { $message = is_array($response) ? rgars($response, 'error/message') : ''; return new WP_Error($code, $message ? $message : sprintf('Unexpected response code %d from Google Calendar.', $code)); } return $response; } public static function get_auth_account_info($access_token) { $auth_account = get_option(self::ALPHAGF_GC_AUTH_ACCOUNT, []); if (!empty($auth_account)) { return $auth_account; } if (empty($access_token)) { return false; } $url = 'https://www.googleapis.com/oauth2/v3/userinfo'; $response = wp_remote_get( $url, [ 'headers' => [ 'Authorization' => 'Bearer ' . $access_token, 'Accept' => 'application/json', ] ] ); if (is_wp_error($response)) { return $response->get_error_message(); } $response_code = wp_remote_retrieve_response_code($response); if (200 !== $response_code) { return false; } $body = wp_remote_retrieve_body($response); $result = json_decode($body, true); $user_data = array( 'name' => isset($result['name']) ? sanitize_text_field($result['name']) : '', 'picture' => isset($result['picture']) ? esc_url_raw($result['picture']) : '', 'email' => isset($result['email']) ? sanitize_email($result['email']) : '', 'time' => time(), ); update_option(self::ALPHAGF_GC_AUTH_ACCOUNT, $user_data); return $user_data; } public static function revoke_token($access_token) { if (empty($access_token)) { return false; } $revoke_url = 'https://oauth2.googleapis.com/revoke'; $response = wp_remote_post($revoke_url, [ 'body' => [ 'token' => $access_token, ], 'timeout' => 15, ]); if (is_wp_error($response)) { return false; } $response_code = wp_remote_retrieve_response_code($response); if (200 === $response_code || 400 === $response_code) { // Remove the token from the WordPress database delete_option(self::ALPHAGF_TOKEN_DATA); delete_option(self::ALPHAGF_GC_AUTH_ACCOUNT); delete_option(self::ALPHAGF_GC_AUTH_VERIFIED); delete_option(self::ALPHAGF_GC_CALENDER_LIST); delete_transient(self::ALPHAGF_GC_CALENDER_LIST); $settings = get_option('gravityformsaddon_alphagf_google_calendar_settings'); if (is_array($settings)) { unset($settings['client_token']); update_option('gravityformsaddon_alphagf_google_calendar_settings', $settings); } return true; } return false; } }