# REST API Endpoints

All endpoints are registered under the `ajaxpress` namespace and require the `manage_options` capability.

**Base URL:** `/wp-json/ajaxpress/`  
**Authentication:** WordPress nonce via `X-WP-Nonce` header

---

## Settings

### Save Settings

```
POST /wp-json/ajaxpress/settings
```

Updates plugin settings. Accepts a JSON object where keys are setting names and values are the new values.

**Request Body:**

```json
{
  "enable_navigation": true,
  "enable_prefetch": false,
  "loader_type": "progressbar",
  "progressbar_color": "#3b82f6"
}
```

**Response:**

```json
{
  "success": true
}
```

Only settings that exist in `Options::get_default_settings()` are accepted. Unknown keys are ignored.

---

## License

### Get/Update License

```
GET  /wp-json/ajaxpress/license
POST /wp-json/ajaxpress/license
```

**GET** returns the current license state.

**POST** updates the license. On deactivation, premium settings are reset to defaults.

**Request Body (POST):**

```json
{
  "key": "XXXX-XXXX-XXXX-XXXX",
  "status": "active",
  "plan_name": "Pro",
  "plan_type": "lifetime",
  "expires_at": "2027-01-01"
}
```

**Response:**

```json
{
  "success": true,
  "license": { ... }
}
```

---

## Deactivation Feedback

### Save Feedback

```
POST /wp-json/ajaxpress/deactivate-feedback
```

Captures feedback when the user deactivates the plugin. Stored locally and sent to the remote feedback endpoint.

**Request Body:**

```json
{
  "reason": "temporary",
  "comment": "Switching themes, will re-enable later",
  "site_info": { ... },
  "server_info": { ... }
}
```

---

## Diagnostic Permission

### Save Permission

```
POST /wp-json/ajaxpress/diagnostic-permission
```

Stores whether the user has opted in or out of diagnostics.

**Request Body:**

```json
{
  "permission": "allowed"
}
```

Values: `allowed`, `denied`, or empty string.

---

## Tour

### Get/Update Tour State

```
GET  /wp-json/ajaxpress/tour
POST /wp-json/ajaxpress/tour
```

Manages the onboarding tour state per user (stored as user meta).

**POST Request Body:**

```json
{
  "completed": true,
  "mode": "quick",
  "dismissed_at": 1712956800
}
```

**GET Response:**

```json
{
  "completed": false,
  "mode": "extended",
  "dismissed_at": null
}
```

---

## Cloudflare

### Verify Token

```
POST /wp-json/ajaxpress/cf/verify-token
```

Verifies a Cloudflare API token, encrypts it, and stores it. Returns the list of accounts.

**Request Body:**

```json
{
  "token": "your-cloudflare-api-token"
}
```

**Response:**

```json
{
  "success": true,
  "accounts": [
    {
      "id": "abc123",
      "name": "My Account"
    }
  ]
}
```

### List Zones

```
GET /wp-json/ajaxpress/cf/zones?account_id=abc123
```

Lists available Cloudflare zones and identifies which one matches the current site domain.

**Response:**

```json
{
  "success": true,
  "zones": [
    {
      "id": "zone123",
      "name": "example.com",
      "status": "active"
    }
  ],
  "matched_zone": "zone123"
}
```

### Deploy Worker

```
POST /wp-json/ajaxpress/cf/deploy
```

Deploys the Cloudflare Worker and creates a route for the selected zone.

**Request Body:**

```json
{
  "zone_id": "zone123"
}
```

**Response:**

```json
{
  "success": true,
  "route_id": "route456",
  "route_pattern": "example.com/*"
}
```

### Get Status

```
GET /wp-json/ajaxpress/cf/status
```

Returns the current Cloudflare cache deployment status and validates the stored token.

**Response:**

```json
{
  "success": true,
  "enabled": true,
  "zone_id": "zone123",
  "zone_name": "example.com",
  "route_id": "route456",
  "route_pattern": "example.com/*",
  "auto_purge": true,
  "token_valid": true
}
```

### Teardown

```
POST /wp-json/ajaxpress/cf/teardown
```

Removes the deployed worker and route, clears all Cloudflare settings, and deletes the stored token.

**Response:**

```json
{
  "success": true
}
```

### Purge Specific URLs

```
POST /wp-json/ajaxpress/cf/purge
```

Purges specific URLs from the Cloudflare cache.

**Request Body:**

```json
{
  "urls": [
    "https://example.com/page-1/",
    "https://example.com/page-2/"
  ]
}
```

URLs are batched in groups of 30 per API call.

### Purge All

```
POST /wp-json/ajaxpress/cf/purge-all
```

Purges the entire cache for the connected zone.

**Response:**

```json
{
  "success": true
}
```
