WordPress.org

Make WordPress Core

Opened 22 months ago

Last modified 21 months ago

#26235 new enhancement

Add action when _fix_attachment_links fires

Reported by: dllh Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.7
Component: Administration Keywords: has-patch
Focuses: Cc:

Description

There are sometimes circumstances under which it would be useful to fire an action when _fix_attachment_links() fires, since it does a wp_update_post() right after another wp_update_post() has run. For example, say you want to run an action on save_post exactly once per post update. If a given post has attachments that need fixing, the save_post action will fire twice.

The addition of an action within _fix_attachment_links() would allow plugins to know that the call to wp_update_post() was a second call and to remove any actions that shouldn't fire a second time.

A real life scenario: My site hooks onto publish_post and initiates some async processes (email subscription notifications, for example). When a post needs its attachments updated, these processes execute twice, causing fun things like duplicate emails. So I need a way to make sure the events that fire on publish_post fire exactly once even if the update itself executes more than once. I thought adding an action might be the proper way forward. Patch attached.

A sample use case:

function skip_async_publish_post_if_fixing_attachment_links() {
    remove_action( 'publish_post', 'queue_publish_post', 10, 2 );
}
add_action( '_fix_attachment_links', 'skip_async_publish_post_if_fixing_attachment_links' );

Attachments (2)

fix-attachment-links.patch (403 bytes) - added by dllh 22 months ago.
fix-attachment-links.2.patch (461 bytes) - added by dllh 21 months ago.
A new approach, addition/subtraction of filters

Download all attachments as: .zip

Change History (6)

comment:1 @SergeyBiryukov21 months ago

So I need a way to make sure the events that fire on publish_post fire exactly once even if the update itself executes more than once.

You can just call remove_action() in your current function attached to publish_post. This use case doesn't require a new hook.

comment:2 @SergeyBiryukov21 months ago

  • Version changed from trunk to 3.7

comment:3 @dllh21 months ago

I tried this and you're right that it works well for one-off publishing of posts, but it'll fail for things like imports and batch updates. For that matter, I think my fix would fail in that case as well, so I'll need to think this through some more.

comment:4 @dllh21 months ago

I've come up with another approach. It requires the addition and removal of a filter, which I suspect still won't fly, but nothing ventured nothing gained. It may be better to name the filter more generically (something like "doing_secondary_wp_update_post" so that it could be used more readily in other contexts as well.

Just want to restate that it's not my intention to modify core just to meet my use case. I can always hack my local core if I really need to (though would prefer not to). It just seems plausible that there might be other use cases for which something along these lines would be useful as well.

@dllh21 months ago

A new approach, addition/subtraction of filters

Note: See TracTickets for help on using tickets.