# ContextLift — Planned Features

---

## 1. Broken Internal Link Management

**Status:** 🟡 Planned  
**Priority:** High  
**Type:** New Module

### Problem
When a page is deleted, trashed, or becomes unavailable, internal links pointing to it break silently. This hurts SEO and user experience.

### Implementation Plan

#### Layer 1: Real-Time Cleanup (Hook into Post Deletion)

Hook into WordPress post lifecycle:
```php
add_action('wp_trash_post', [$this, 'on_post_removed']);
add_action('before_delete_post', [$this, 'on_post_removed']);
```

**`on_post_removed($post_id)`** logic:
1. Get the URL of the deleted post
2. Query all published posts whose `post_content` contains that URL
3. For each affected post:
   - Strip the `<a>` tag but keep the anchor text as plain text
   - Log the change in `wp_contextlift_change_logs` (undoable)
   - Optionally: re-link to a similar page in the same topic cluster

#### Layer 2: Periodic Broken Link Scanner (Cron or Manual)

Scheduled scan (Pro) or manual button (Free):
1. Collect all internal `<a href>` links from every published post
2. Check if target URL resolves to a published post (`url_to_postid()` + `post_status === 'publish'`)
3. If target is trashed, draft, deleted, or returns 0 → flag as broken
4. Store results: `broken_url`, `found_in_post_id`, `anchor_text`, `detected_at`

#### Layer 3: Admin UI (Dashboard Tab)

A **"Broken Links"** tab showing:
- Post name, broken URL, anchor text
- Actions: **Remove** (strip link, keep text) · **Re-link** (pick replacement) · **Ignore**
- **Bulk Fix** button (Pro only)

### New Files Needed

| File | Purpose |
|------|---------|
| `includes/broken-links.php` | New class `ContextLift_Broken_Links` |
| Updates to `contextlift.php` | Hook registration |
| Updates to `admin-ui.php` | New "Broken Links" tab |
| Updates to `admin.js` | AJAX handlers for scan/fix |
| Updates to `cron.php` | Weekly scan event (Pro) |

### Key Functions

| Function | Purpose |
|----------|---------|
| `on_post_removed($post_id)` | Hook handler — scans & cleans links to deleted post |
| `scan_broken_links()` | Full site scan for broken internal links |
| `remove_broken_link($post_id, $url)` | Strips `<a>` tag, keeps anchor text |
| `relink_broken_link($post_id, $old_url, $new_url)` | Replaces target URL with new one |
| `ajax_scan_broken_links()` | AJAX endpoint for manual scan |
| `ajax_fix_broken_link()` | AJAX endpoint for fixing individual links |

### Free vs Pro

- **Free:** Manual scan button, remove broken links only
- **Pro:** Cron-based auto-scan, re-link suggestions, bulk fix
