Media Library - Attached/Unattached logic is flawed

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)

Change History (9)

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.

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.

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.