Ticket #3757 (closed defect (bug): fixed)

Opened 5 years ago

Last modified 2 years ago

Uploads to post-new.php don't get associated with the post

Reported by: JBoyd Owned by: mdawaffe
Priority: normal Milestone: 2.1.1
Component: Administration Version: 2.1
Severity: normal Keywords: images uploader write has-patch commit
Cc:

Description

If a post has already been saved, using the inline uploader works as expected: the uploaded file is associated with the post. (You can tell it has worked, because the file appears in the uploader's "Browse" tab when editing the post, that is, when viewing a post with /wp-admin/post.php, under the "Manage" tab.)

But using the inline uploader before it has been saved (that is, when editing it with /wp-admin/post-new.php, displayed under the "Write" tab), uploaded files DO NOT get associated with the post, although they do get uploaded. (There is no "Browse" tab in the uploader, just a "Browse All.")

This is unexpected behavior and a change from WP 2.0.x. It's a significant problem anytime you need uploads and posts to be associated with one another (as many image plugins do, for instance). Naturally, the work around is to save first before uploading, but even so simple a work-around as that shouldn't be necessary, imho.

Attachments

3757.diff Download (766 bytes) - added by mdawaffe 5 years ago.
3757b.diff Download (2.3 KB) - added by mdawaffe 5 years ago.

Change History

comment:1   andy5 years ago

The uploader uses a temporary negative post ID for attachments on posts that do not exist in the database. When it is first saved, any orphaned attachments are supposed to have their post_parent updated. I suspect the negative ID is being lost or ignored due to autosave.

  • Milestone changed from 2.3 to 2.2
  • Owner changed from anonymous to mdawaffe
  • Status changed from new to assigned

fix_attachment_links() broke in [4478].

Attached fixes this. Re-edit posts (not attachments) to fix the issue (it will look at the attachment links in that post and re-parent any orphan attachments it finds).

This is only a partial solution. I'll look into why the post_parent wasn't updated to begin with.

  • Milestone changed from 2.2 to 2.1.1
  • Keywords has-patch commit added

3757b.diff

  1. Regex fix from previous patch.
  2. Make sure we call relocate_children() at the appropriate times with the appropriate args.

mdawaffe, does this fix the issue or is this still only a partial fix?

Oops, forgot to say :)

Full fix (to the best of my soothsaying abilities).

The regex (first patch) definitely needs to go into 2.1.1 - it's just reverting back to the unbroken version.

The other piece can wait if you don't want big changes in the double dot.

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

(In [4884]) Make sure attachments uploaded on unsaved drafts get reunited with their parent post. Props mdawaffe. fixes #3757

  • Status changed from closed to reopened
  • Resolution fixed deleted

Reopening for 2.1.1

mdawaffe, What functional changes does the second part of the patch (the part not in the first patch) make? What bug will we have to endure in 2.1.1 if only the first patch goes in?

First Part = regex fix that fixes fix_attachment_link() which provides the the following functionality.

When a post is saved, WP looks through the post content to see if there are any attachment links that do not adhere to the permalink structure. If it finds any, it fixes the link and also re-parents them if the attachment is an orphan.

Second Part = logic fix that fixes the following bug. (To reproduce, must follow exact order.)

  1. Go to post-new.php.
  2. Write a post.
  3. Allow autosave to fire.
  4. Upload image. Do NOT send image to editor. Just upload it.
  5. Publish.
  6. Observe that uploaded image still has negative ID. (Easy to see since it is not available in that post's "Browse" tab, only in the "Browse All" Tab.)

If we include the regex fix in 2.1.1 but not the fix for the above bug, images uploaded via the above steps will have negative ID. Consequences:

  1. Not associated with post and no valid post_parent object.
  2. No permalink.
  3. ?attachment_id=xxx link will generate 404 to non-logged in users due to WP_Query oddity.

BUT, if such an orphaned image is Sent to Editor, fix_attachment_links() (which will now work), will re-parent the image and fix the above consequences for that image only.

Doesn't seem so bad. As I'm reading it, there should be no public link to the orphaned image (as it has never been linked to from within an entry), so consequence #3 should never be triggered. If that's correct, I'm okay with leaving the second part of the patch for 2.2. Let me know if that's correct.

That's correct. It will annoy authors looking for photos and potentially plugins that deal with attachments, but not visitors of the site.

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

(In [4889]) Fix fix_attachment_link() regex. Props mdawaffe. fixes #3757

Note: See TracTickets for help on using tickets.