Skip to content

Add image editing functionality to REST API for media attachments - #78458

Closed
the-hercules wants to merge 1 commit into
WordPress:trunkfrom
the-hercules:feature/78077
Closed

the-hercules wants to merge 1 commit into
WordPress:trunkfrom
the-hercules:feature/78077

Conversation

@the-hercules

Copy link
Copy Markdown

What?

Closes

Closes #78077

What?

Add lineage tracking for cropped media created through the Gutenberg/REST image edit flow by storing _source_attachment_id on newly created derivative attachments.

Why?

When Gutenberg crops an image, it creates a brand new attachment instead of updating the existing one. While the generated attachment metadata already stores the immediate source in parent_image, there is no stable root/original lineage reference across crop-of-crop chains.

This makes it difficult to:

  • identify all derivatives of an original image
  • distinguish the root original from intermediate crops
  • support future restore/grouping workflows cleanly

This PR adds a minimal backward-compatible MVP for derivative lineage tracking using attachment post meta.

How?

This PR adds a compatibility helper, wp_get_original_attachment_id(), that returns the root/original attachment ID for an attachment derivative tree, or the attachment’s own ID when no lineage meta exists.

It also overrides the Gutenberg REST media edit flow in Gutenberg_REST_Attachments_Controller::edit_media_item() so that when a new cropped attachment is created, it stores _source_attachment_id on the new attachment. The stored value is always the root/original attachment ID, not the immediate parent crop.

This keeps the two lineage concepts separate:

  • _source_attachment_id stores the root/original attachment ID
  • parent_image['attachment_id'] in _wp_attachment_metadata continues to store the immediate source attachment ID

The change is limited to newly created cropped attachments and does not backfill or migrate existing media.

Testing Instructions

  1. Activate the Gutenberg plugin in your local environment.
  2. Open the Site Editor or Post Editor.
  3. Insert an Image block.
  4. Upload a new image and note its attachment ID.
  5. Use the block toolbar crop tool to crop the image and save the change.
  6. Identify the new cropped attachment ID in the database.
  7. Confirm the cropped attachment has a _source_attachment_id row in wp_postmeta pointing to the original attachment ID.
  8. Confirm the cropped attachment’s _wp_attachment_metadata still contains parent_image['attachment_id'] pointing to the immediate source attachment.
  9. Crop the newly cropped image again to create a second derivative.
  10. Confirm the second derivative’s _source_attachment_id still points to the original root attachment ID.
  11. Confirm the second derivative’s parent_image['attachment_id'] points to the first cropped attachment, not the original.
  12. Upload a separate image without cropping it.
  13. Confirm that normal uploads do not get a _source_attachment_id meta entry.

@github-actions

Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Required label: Any label starting with [Type].
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: the-hercules <thehercules@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label May 20, 2026
@github-actions

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @the-hercules! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@ramonjd

ramonjd commented Aug 19, 2026

Copy link
Copy Markdown
Member

Superseded by #81803

@ramonjd ramonjd closed this Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Media: Tracking the relationship between original images and their cropped derivatives

2 participants