# Peligent WP Plugin (WordPress AI Assistant)

## Overview
- Single-file PHP plugin: peligent-ai-assistant.php
- Current version: 1.6.17
- Requires WordPress 5.8+ and WooCommerce
- Listed on WordPress.org plugin directory

## Structure
- peligent-ai-assistant.php — main plugin file (all logic, single-file architecture)
- assets/ — plugin icons and screenshots for WordPress.org
- readme.txt — WordPress.org listing, changelog, and metadata
- deploy-to-wordpress-svn.sh — SVN deployment script
- CLAUDE.md — this file

## Versioning
- **Both versions must always match**: `readme.txt` Stable tag and plugin header `Version:`
- Bump both when releasing. Use semantic versioning (major.minor.patch).
- Add a changelog entry in `readme.txt` under `== Changelog ==` for every version bump.

## Deploy to WordPress.org
SVN credentials are stored in `~/.netrc` (machine: plugins.svn.wordpress.org, login: peligent).
SVN auth is automatic via .netrc — no manual password entry needed.

To deploy:
```bash
cd /home/sajad/workspace/peligent-wp-plugin
bash deploy-to-wordpress-svn.sh
```

The script:
1. Reads version from `readme.txt` Stable tag
2. Checks out WordPress SVN repo to /tmp/
3. Copies assets (icons, screenshots) to SVN assets/
4. Rsyncs plugin files to trunk/ (excludes .git, admin-ui, deploy script, wpai-embed.js)
5. Creates a version tag
6. Prompts for confirmation, then commits

**Note:** The script is interactive (has `read -p` prompts for confirmation and commit message).

## Architecture
- Single PHP class: `Peligent_For_WooCommerce_Plugin`
- Communicates with app.peligent.com API
- Two-token auth: `connect_token` (user-specific, ephemeral) + `site_token` (per-site, persistent)
- Provides embedded AI chat widget for WordPress admin
- WooCommerce integration for e-commerce features

## Settings Wizard (v1.6.5)
4-step wizard on the settings page:
1. **Connect** — Connect Secret + Application Password input
2. **Analyzing** — AI bot analyzes store (polls profile-status endpoint every 5s, 2min timeout)
3. **Review Profile** — Pre-filled fields with AI Suggested badges (language, tone, industry, products, expectations)
4. **Ready** — Success confirmation + Dashboard link

Key JS functions (global scope, no IIFE — required for inline onclick handlers):
- `showStep(n)` — navigate between wizard steps with fade-in animation
- `startPolling()` — polls AJAX endpoint for AI analysis results
- `showTimeout()` — shows Retry + Fill manually buttons on timeout
- `applyProfile(p)` — fills form fields from AI analysis data
- `showBadge(field)` / `hideBadge(field)` — toggle AI Suggested badges
- `checkConnectReady()` — pulses Connect button when both inputs are filled

## Key PHP Methods
- `render_admin_page()` — renders the settings wizard
- `handle_send_to_peligent()` — sends credentials to Peligent backend
- `handle_save_assistant_defaults()` — saves profile form to WP options
- `fetch_profile_status()` — calls backend profile-status endpoint (with connect_token + site_token)
- `ajax_poll_profile()` — WP AJAX handler for wizard polling (nonce-verified)

## Important Conventions
- All PHP output uses `esc_html__()`, `esc_attr()`, `esc_url()` for security
- AJAX handlers use `wp_verify_nonce()` for CSRF protection
- Inline JS uses `echo '...'` statements in PHP — be careful with quote escaping
- CSS is inline in a `<style>` block within `render_admin_page()`
- No external JS/CSS dependencies for the wizard (self-contained)
