repository = $repository; $this->oauthManager = $oauthManager; $this->stateManager = $stateManager; $this->sheetsClient = $sheetsClient; } /** * Process adding a new named connection, redirecting to Google Consent. * * @return void */ public function handleAddConnection(): void { if (!isset($_POST['oneclickformsync_add_connection'])) { return; } // Verify CSRF. check_admin_referer('oneclickformsync_add_connection_action'); if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have permission to perform this action.', '1-click-form-sync'), 403); } $name = sanitize_text_field($_POST['connection_name'] ?? ''); if ($name === '') { $name = __('Google Connection', '1-click-form-sync'); } $isProxy = !empty($_POST['oneclickformsync_connect_proxy']); if (!$isProxy) { $clientId = get_option(SettingsController::CLIENT_ID_OPTION); $clientSecret = get_option(SettingsController::CLIENT_SECRET_OPTION); if (!$clientId || !$clientSecret) { wp_safe_redirect( add_query_arg( ['oneclickformsync_error' => 'missing_credentials'], menu_page_url('1-click-form-sync-connections', false) ) ); exit; } } else { $clientId = ''; $clientSecret = ''; } $connectionId = function_exists('wp_generate_uuid4') ? wp_generate_uuid4() : uniqid('ocfs_', true); // Setup temporary connection entity. $connection = new Connection([ 'id' => $connectionId, 'name' => $name, 'client_id' => $clientId, 'client_secret' => $clientSecret, 'owner_id' => get_current_user_id(), 'status' => 'pending_auth', ]); $this->repository->save($connection); // Generate state token and forward to Google OAuth or Proxy. $state = $this->stateManager->generateState($connectionId); if ($isProxy) { $authUrl = $this->oauthManager->getProxyAuthorizationUrl($state); } else { $authUrl = $this->oauthManager->getAuthorizationUrl($clientId, $state); } wp_redirect($authUrl); exit; } /** * Handle connection deletion / revocation. * * @return void */ public function handleDeleteConnection(): void { if (!isset($_GET['oneclickformsync_delete_connection'])) { return; } $id = sanitize_text_field($_GET['oneclickformsync_delete_connection']); check_admin_referer('oneclickformsync_delete_connection_action_' . $id); if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have permission to perform this action.', '1-click-form-sync'), 403); } // Clear transient cache for this token. delete_transient('oneclickformsync_access_token_' . $id); // Delete from database. $this->repository->delete($id); wp_safe_redirect( add_query_arg( ['oneclickformsync_deleted' => '1'], menu_page_url('1-click-form-sync-connections', false) ) ); exit; } /** * Test connection credentials. * * @param string $connectionId * @return bool */ public function testConnection(string $connectionId): bool { return $this->sheetsClient->testConnection($connectionId); } }