Skip to content

Commit fdbe530

Browse files
committed
Media: Rename the original attachment to the edit root.
`wp_get_original_attachment_id()` sat next to `wp_get_original_image_path()` and `wp_get_original_image_url()`, which describe the unscaled upload of the same attachment rather than the attachment a chain of edits started from. The names now say which one they mean: - `wp_get_original_attachment_id()` -> `wp_get_edit_root_attachment_id()` - `_wp_delete_original_attachment_id()` -> `_wp_delete_edit_root_attachment_id()` - `_wp_attachment_original_id` postmeta -> `_wp_attachment_edit_root_id` - `original_attachment` REST field -> `edit_root` - `wp:original-attachment` link relation -> `wp:edit-root` No change in behaviour. See #65987.
1 parent 65af124 commit fdbe530

4 files changed

Lines changed: 73 additions & 73 deletions

File tree

‎src/wp-includes/default-filters.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@
696696
add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' );
697697
add_action( 'plugins_loaded', '_wp_add_additional_image_sizes', 0 );
698698
add_filter( 'plupload_default_settings', 'wp_show_heic_upload_error' );
699-
add_action( 'delete_attachment', '_wp_delete_original_attachment_id' );
699+
add_action( 'delete_attachment', '_wp_delete_edit_root_attachment_id' );
700700

701701
// Client-side media processing.
702702
add_action( 'admin_init', 'wp_set_client_side_media_processing_flag' );

‎src/wp-includes/post.php‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8827,14 +8827,14 @@ function wp_get_original_image_url( $attachment_id ) {
88278827
}
88288828

88298829
/**
8830-
* Retrieves the ID of the attachment an edited image originally came from.
8830+
* Retrieves the edit root of an attachment: the attachment its chain of edits started from.
88318831
*
88328832
* Editing an image through the `wp/v2/media/<id>/edit` REST endpoint does not change the
88338833
* image that was edited. It saves the result as a brand new attachment, so a site can end
88348834
* up with a chain of attachments: an upload, a crop of it, a crop of that crop, and so on.
88358835
*
88368836
* Every attachment created that way stores the ID of the attachment at the top of its chain,
8837-
* so this function can find the original in one lookup no matter how long the chain is.
8837+
* so this function can find the edit root in one lookup no matter how long the chain is.
88388838
*
88398839
* Attachments that were uploaded rather than created by editing have no chain of their own,
88408840
* and this returns the ID that was passed in. To tell the two cases apart, compare the
@@ -8846,41 +8846,41 @@ function wp_get_original_image_url( $attachment_id ) {
88468846
* @return int ID of the attachment the chain of edits started from, or `$attachment_id`
88478847
* if none is recorded.
88488848
*/
8849-
function wp_get_original_attachment_id( $attachment_id ) {
8850-
$original_id = (int) get_post_meta( $attachment_id, '_wp_attachment_original_id', true );
8849+
function wp_get_edit_root_attachment_id( $attachment_id ) {
8850+
$edit_root_id = (int) get_post_meta( $attachment_id, '_wp_attachment_edit_root_id', true );
88518851

8852-
return $original_id > 0 ? $original_id : (int) $attachment_id;
8852+
return $edit_root_id > 0 ? $edit_root_id : (int) $attachment_id;
88538853
}
88548854

88558855
/**
8856-
* Clears the recorded original attachment ID from any attachment pointing at a deleted one.
8856+
* Clears the recorded edit root ID from any attachment pointing at a deleted one.
88578857
*
88588858
* Without this, attachments created by editing the deleted image would keep pointing at an
88598859
* ID that no longer exists, and could later point at an unrelated attachment if WordPress
88608860
* reuses that ID.
88618861
*
88628862
* This only runs when an attachment is deleted for good. On sites where media goes to the
8863-
* trash first, attachments keep pointing at the trashed original until the trash is emptied.
8863+
* trash first, attachments keep pointing at the trashed edit root until the trash is emptied.
88648864
*
88658865
* @since 7.2.0
88668866
*
88678867
* @access private
88688868
*
88698869
* @param int $post_id Attachment ID being deleted.
88708870
*/
8871-
function _wp_delete_original_attachment_id( $post_id ) {
8871+
function _wp_delete_edit_root_attachment_id( $post_id ) {
88728872
$post_id = (int) $post_id;
88738873

88748874
if ( $post_id <= 0 ) {
88758875
return;
88768876
}
88778877

88788878
/*
8879-
* Deletes the meta from every attachment recording this ID as its original. The meta key
8879+
* Deletes the meta from every attachment recording this ID as its edit root. The meta key
88808880
* is indexed, so this only scans the rows for attachments created by editing an image,
88818881
* and it avoids searching the serialized attachment metadata for the ID.
88828882
*/
8883-
delete_metadata( 'post', 0, '_wp_attachment_original_id', $post_id, true );
8883+
delete_metadata( 'post', 0, '_wp_attachment_edit_root_id', $post_id, true );
88848884
}
88858885

88868886
/**

‎src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,15 +1357,15 @@ public function edit_media_item( $request ) {
13571357
);
13581358

13591359
/*
1360-
* Record the attachment this chain of edits started from, so the original can be
1360+
* Record the attachment this chain of edits started from, so the edit root can be
13611361
* found in one lookup from any image later in the chain. The new attachment inherits
1362-
* the original recorded on the image being edited, or that image itself when it was
1362+
* the edit root recorded on the image being edited, or that image itself when it was
13631363
* uploaded rather than edited.
13641364
*/
13651365
update_post_meta(
13661366
$new_attachment_id,
1367-
'_wp_attachment_original_id',
1368-
wp_get_original_attachment_id( $attachment_id )
1367+
'_wp_attachment_edit_root_id',
1368+
wp_get_edit_root_attachment_id( $attachment_id )
13691369
);
13701370

13711371
/**
@@ -1526,10 +1526,10 @@ public function prepare_item_for_response( $item, $request ) {
15261526
* Edit context only, since only editors need it.
15271527
* Not validated: deleting an attachment clears it from images edited from it.
15281528
*/
1529-
if ( in_array( 'original_attachment', $fields, true ) && 'edit' === $request['context'] ) {
1530-
$original_id = wp_get_original_attachment_id( $post->ID );
1529+
if ( in_array( 'edit_root', $fields, true ) && 'edit' === $request['context'] ) {
1530+
$edit_root_id = wp_get_edit_root_attachment_id( $post->ID );
15311531

1532-
$data['original_attachment'] = $original_id !== (int) $post->ID ? $original_id : 0;
1532+
$data['edit_root'] = $edit_root_id !== (int) $post->ID ? $edit_root_id : 0;
15331533
}
15341534

15351535
if ( in_array( 'source_url', $fields, true ) ) {
@@ -1691,15 +1691,15 @@ public function prepare_item_for_response( $item, $request ) {
16911691
}
16921692

16931693
/*
1694-
* Let clients fetch the original attachment in the same request with `_embed`,
1694+
* Let clients fetch the edit root in the same request with `_embed`,
16951695
* the way `featured_media` is paired with its own link. Added here rather than in
16961696
* `prepare_links()` because that method cannot see the request, and this belongs
16971697
* in the `edit` context only, alongside the field itself.
16981698
*/
1699-
if ( ! empty( $data['original_attachment'] ) ) {
1699+
if ( ! empty( $data['edit_root'] ) ) {
17001700
$response->add_link(
1701-
'https://api.w.org/original-attachment',
1702-
rest_url( rest_get_route_for_post( $data['original_attachment'] ) ),
1701+
'https://api.w.org/edit-root',
1702+
rest_url( rest_get_route_for_post( $data['edit_root'] ) ),
17031703
array( 'embeddable' => true )
17041704
);
17051705
}
@@ -1842,7 +1842,7 @@ public function get_item_schema() {
18421842
'context' => array( 'view', 'edit' ),
18431843
);
18441844

1845-
$schema['properties']['original_attachment'] = array(
1845+
$schema['properties']['edit_root'] = array(
18461846
'description' => __( 'The ID of the attachment this attachment\'s chain of edits started from, or 0 if none is recorded.' ),
18471847
'type' => 'integer',
18481848
'context' => array( 'edit' ),

0 commit comments

Comments
 (0)