WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#8490 closed defect (bug) (fixed)

Media Library - Attached/Unattached logic is flawed

Reported by: hailin Owned by:
Milestone: 2.7 Priority: high
Severity: major Version:
Component: General Keywords:
Focuses: Cc:

Description

In 2.7 Media Library, the tabs Attached, Unattched logic has an issue , and can cause user confusions and wrong output.

Current ‘Attached’ logic:

From Write post panel, user clicks upload media, the the url will contain post_id, which is used to set post_parent for the uploaded attachement.
Ex: wp-admin/post.php?action=edit&post=8
Then the uploaded image’s post_parent field is set to 8, and as long as post_parent is >=1, the attachment is regarded as “Attached”. Otherwise, it’s “Not attached”.

When an image is added via “Media=>Add New”, then its post_parent is not set, thus the image will be “Unattached”. No subsequent operation will alter its’ post_parent, even when user inserts the image into a post.

So if users always upload images/videos via Media=>Add New, then insert them into posts later, those images/videos will always be classified as “Unattached”, this can cause confusion as users may intend to delete those “unattached, thus unused” things.

I think a better logic for “attached/unattached” would be actually scanning posts contents for inclusion. Basing this logic on a particular operation flow is problematic and it’s difficult to guarantee the correctness.

Attachments (1)

8490.patch (1.0 KB) - added by azaozz 6 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 @hailin6 years ago

Scanning posts contents to determine "attached/unattached" is going to be very expensive, plus it may not always work because the link or shortcode inserted may not have definitive correlation to the original media.

For example:
when an image (such as flower.jpg) is inserted, the inserted link will contain flower.jpg. However, if an video is inserted, it may only contain a guid. Currently video does not apply to wporg yet, but it shows the point.

An alternative is to use periodic process to scan and produce the mapping relationship. But again, it may not be accurate due the above point.

So I think we should remove the functionality before we figure out a clear and fast solution.

comment:2 @westi6 years ago

I disagree..

The bug here is that when an attachment that has been uploaded via Media>Add New is attached to a post it should have it's post parent set correctly.

comment:3 @azaozz6 years ago

Perhaps the right thing to do would be to attach the image on inserting it into a post if it's not attached.

comment:4 @azaozz6 years ago

Westi, exactly, responded before seeing your message.

comment:5 follow-up: @hailin6 years ago

The tricky part is that user may or may not click "insert into post" button.
User can just copy and paste the "file url" into Write post box.

Plus, the same image URL can be inserted or pasted into multiple posts, then the
post_parent notion becomes ambiguous. Imagine when we need to build more logic
on that 1-1 assumption...

A fairly large percentage of existing images and other types of media become "orphans" after switch to this logic. It's not easy to rectify this situation.

comment:6 @hailin6 years ago

Also, suppose we update post_parent when user does click "insert into post". When the same image (flower.jpg) is inserted into multiple posts, (ex: post1, post2). But we can only keep one post_parent for flower.jpg attachment, so let's say we end up recording 2. Then after some time, post2 is deleted, so we reset post_parent of flower.jpg to 0, making it Unattached. But, at this point, flower.jpg is still attached to post1.

comment:7 in reply to: ↑ 5 @azaozz6 years ago

Replying to hailin:

The tricky part is that user may or may not click "insert into post" button.
User can just copy and paste the "file url" into Write post box.

Yes, but very few users would open the uploader popup and copy/paste the url instead of just clicking Insert into post.

Plus, the same image URL can be inserted or pasted into multiple posts, then the
post_parent notion becomes ambiguous. Imagine when we need to build more logic
on that 1-1 assumption...

The attachment would be "attached" on insert only if it's an orphan.

For 2.8 we will have to rethink the attachment/parent concept. Perhaps unattached media can be allowed or extra info added as post meta. With the current system, there would always be "lost" attachments when the parent post is deleted.

@azaozz6 years ago

comment:8 @azaozz6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [10062]) Attach orphan attachments when inserting into a post, fixes #8490

Note: See TracTickets for help on using tickets.