=== Pagehaven – Orphan Finder & Link Inserter ===
Contributors: corvet
Tags: internal-linking, internal-links, orphan-pages, seo, links
Requires at least: 5.8
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 0.8.2
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Find orphan articles, build a ready prompt for any AI, then preview, apply and undo internal link insertions. No cloud, no subscription.

== Description ==

Pagehaven is an internal linking workflow for sites that write with AI or an editor, and want full control over every link.

1. **Scan** the site. The plugin counts inbound internal links for every article and lists the orphans (no inbound links) and the weak articles (one inbound link). The scan runs in small steps, so it is safe on slow hosting.
2. **Export** the article list (JSON or CSV): URL, category, Yoast focus keyword, word count, H2 headings, internal links in and out. For the donor articles you choose, export the full text too.
3. **Ask any AI** (ChatGPT, Claude, Gemini or a person) for link edits. Pick an orphan and the plugin builds a ready prompt with the best-matching donor articles and their text, so there is nothing to attach. For bigger batches, use the export files and the general prompt instead.
4. **Apply** the edits: upload the JSON file or paste the AI's reply (code fences and chatter around the JSON are ignored). The plugin shows a preview of every insertion, you tick what to apply, and every change can be undone later from the history.

**Why it is different**

* Bring your own AI. Nothing is sent anywhere, and there are no credits or subscriptions.
* Works in any language: the plugin does not analyse language, your AI writes the sentence in the language of the donor article.
* Nothing changes until you approve it in the preview.
* The inserted sentence is saved in the history and can be removed with one click. Each change also creates a normal post revision.
* Only simple HTML is allowed in an inserted sentence: a link, bold and italic.

**What the preview checks**

* The donor article exists and you may edit it.
* The words the sentence goes after are found, and only once.
* The sentence is not placed inside an existing link.
* The article does not already contain this link.

**Safe by design**

* Administrator-only screens, nonces on every action, capability checks per article on every edit.
* The uploaded file is read from the temporary upload location and is not stored on your server.
* No external requests, no tracking.

= Edits file format =

`
{
  "edits": [
    {
      "post_id": 123,
      "insert_after": "exact words from the donor article",
      "text_html": "See also our guide to <a href=\"https://example.com/target/\">the target topic</a>.",
      "note": "optional"
    }
  ]
}
`

Up to 200 edits and 1 MB per file.

== Frequently Asked Questions ==

= Does it send my content to an AI service? =

No. The plugin only creates files you download and reads files you upload. You choose where to use them.

= What does the built prompt contain? =

The title and URL of the orphan article, and the text of up to three donor articles from the whole site that mention the same names as the orphan (for example place names from its title and focus keyword; common words such as "road" or "trip" are ignored). Articles that already link to the orphan are skipped. You copy the prompt yourself; the plugin sends nothing.

= Does it need Yoast SEO? =

No. If Yoast SEO is active, the focus keyword is added to the export.

= Will a link stay if I deactivate the plugin? =

Yes. Links are written into the article content like any link you add by hand.

= How do I undo a change? =

Open the history below the upload form and press Undo. If the article was edited afterwards and the sentence can no longer be found exactly once, restore the previous version from the post revisions.

= Which content does it cover? =

Published posts and pages.

== Screenshots ==

1. Scan result: orphans, weak articles and progress.
2. Export of the article list or the full text of chosen donors.
3. Prompt for your AI and the edits file format.
4. Preview of insertions before anything is applied.
5. History with one-click undo.

== For developers ==

The plugin has a few hooks that add-ons can use.

* `pagehaven_max_edits` (filter): number of edits read from one file. Default 200.
* `pagehaven_prompt_donors` (filter): number of donor articles in a built prompt. Default 3.
* `pagehaven_prompt_pool` (filter): how many recent posts are searched for donors. Default 500.
* `pagehaven_prompt_donor_chars` (filter): characters of each donor's text in a built prompt. Default 6000.
* `pagehaven_parsed_edits` (filter): the edits read from the uploaded file.
* `pagehaven_evaluate_edit` (filter): the check result for one edit.
* `pagehaven_export_data` (filter): the export data before it is sent.
* `pagehaven_edit_applied` (action): after an edit was applied. Arguments: donor post ID, edit, target info.
* `pagehaven_edit_undone` (action): after an edit was undone. Arguments: donor post ID, history entry.
* `pagehaven_after_cards` (action): print your own card below the built-in ones.

== Changelog ==

= 0.8.2 =
* New: "Build prompt" for an orphan. A self-contained prompt with the best-matching donor articles and their text, nothing to attach.
* New: paste the AI's JSON reply into a box instead of saving a file. Code fences and text around the JSON are ignored.

= 0.7.2 =
* First public release.
* Prompt and file format for your AI on the apply screen, "Copy IDs" for orphans.
* Preview, history and one-click undo for every inserted sentence.
* Hooks for add-ons.

== Upgrade Notice ==

= 0.8.2 =
Adds a ready prompt per orphan and a paste box for the AI's reply.

= 0.7.2 =
First public release.
