# Recorded image link changes

Manage Optimization shows the locations KSF changed during the latest successful
optimization or restoration. It does not search for current image usage. References
by attachment ID, including featured images, are outside this report.

The container service `image-link-changes` implements
`Image_Link_Changes_Interface` and is injected into the optimizer, admin, and API.
Its report creation, lookup, normalization, and rendering methods are instance
methods, allowing tests to replace the reporting dependency.

The private attachment meta field `_ksf_image_link_changes` contains a version,
operation, report ID, UTC completion time, and changed row identities. Restore
replaces the optimization report and retains the new report after deleting the
recovery record. There is no operation history and no table schema migration.
Missing metadata is distinct from a recorded empty list; older optimizations are
not backfilled.

While an operation is pending, its recovery snapshot can temporarily contain
`_ksf_link_changes`. This checkpoints committed link changes so a restoration retry
does not lose locations changed by an earlier pass. The completed report is
published together with recovery-record activation or deletion in a transaction.
A failed finish preserves the previous completed report.

## Verification

Run the PHP suite and the installed-builder suite using the test configuration
documented in [readme.md](readme.md). The focused coverage is:

- `Unit/ImageLinkChangesTest.php`: exact changed rows, content, Elementor and
  GreenShift labels, nonmatching candidates, revisions, autosaves, trash,
  ID-only references, legacy images, restoration, permissions, combined counts,
  stale batch reports, failed report publication, and reporting substitution in
  each consumer through a cloned container.
- `Unit/ImageOptimizerLinkLifecycleTest.php`: failed activation preserves the
  previous report; failed journal deletion and a subsequent recovery retain all
  restored locations, even when the retry has no remaining links to change.
- `Integration/ImageOptimizerBuildersTest.php`: reports generated while changing
  real installed Elementor and GreenShift data.
- `JavaScript/image-optimization-batch.test.cjs`: expandable per-image reports,
  text-only rendering, a server-generated combined checklist, empty reports,
  retries, and checklist failures that preserve confirmed image outcomes.

For a manual check:

1. Reference an image in page content, Elementor data, GreenShift post CSS, and
   GreenShift global styles. Optimize it and open Manage Optimization. Check the
   titles, post types, field labels, and edit links. Global settings appear as
   site-wide items.
2. Add revisions and an autosave, and reference the image on a trashed page.
   Revisions resolve to their parent without duplicate counts. Autosaves and
   trash do not contribute to the checklist. A revision-only field is labeled.
3. Remove an optimized link from one page and add it to another, then restore.
   The restored locations replace the optimization locations and remain visible
   after refreshing the page.
4. Process several images sharing a page. Check each row's disclosure and the
   combined checklist. A shared page appears once, with its changed fields merged.
5. Repeat as a user who can manage the source images but cannot edit one of the
   pages. Its title, URL, ID, and fields must not appear in the response or markup;
   it contributes only to the other-item count, once in the combined report.
6. Optimize an image with only an ID-based reference. Check the explicit “No
   stored links were updated…” explanation. For an existing optimization without
   report metadata, check “Locations not recorded (optimized before KSF 1.12).”

Release close-out: once this feature ships, close `17tnjram7eb` with a link to the
replacement-location task. This implementation does not add a usage scanner,
Media Library column, filter, or backfill.
