Make WordPress Core

Opened 3 years ago

Last modified 3 weeks ago

#60038 reviewing enhancement

Pass original array of arguments to hooks that fire within `wp_insert_post()`

Reported by: johnbillion Owned by: johnbillion
Priority: normal Milestone: Future Release
Component: Posts, Post Types Version:
Severity: normal Keywords: good-first-bug has-patch changes-requested has-unit-tests
Cc: Focuses:

Description

The $postarr value passed to the wp_insert_post() function contains context that can be useful to callbacks attached to the actions that fire within this function, but this value isn't passed to any of them. Strangely enough it is passed to most of the filters there.

As an example, a plugin of mine needs to perform logic within a callback on the wp_insert_post action based on a mixture of the tax_input argument and a custom argument. The wp_insert_post_data filter needs to be used to access these values.

The $postarr value gets copied to $unsanitized_postarr. This is the value that should be passed to the actions because it contains the original input array, rather than $postarr which includes defaults and sanitised values. Or both should be passed, as per the filters in the function.

This affects the following actions:

  • pre_post_update
  • edit_attachment
  • attachment_updated
  • add_attachment
  • edit_post_{$post->post_type}
  • edit_post
  • post_updated
  • save_post_{$post->post_type}
  • save_post
  • wp_insert_post

And the following filters:

  • add_trashed_suffix_to_trashed_posts

Attachments (4)

60038.diff​ (5.9 KB ) - added by rizwan_47 3 years ago.
Added $postarr arg to the missing hooks and filter
60038.1.diff​ (17.6 KB ) - added by sukhendu2002 3 years ago.
60038.2.diff​ (10.0 KB ) - added by sukhendu2002 3 years ago.
Patch refreshed
60038.2.2.diff​ (4.9 KB ) - added by marukou1976 3 years ago.
convert to utf-8

Download all attachments as: .zip

Change History (17)

@rizwan_47
3 years ago

Added $postarr arg to the missing hooks and filter

This ticket was mentioned in ​PR #5747 on ​WordPress/wordpress-develop by ​mrhatia.


3 years ago
#1

  • Keywords has-patch added; needs-patch removed

There are some missing arguments in the core i added it and resolve following ticket

Trac ticket: https://core.trac.wordpress.org/ticket/60038

#2 @johnbillion
3 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone Awaiting Review → Future Release

Thanks for the patch @rizwan_47! But, it doesn't do what I mentioned in the description. The $unsanitized_postarr array needs to be passed, either on its own or along with $postarr. Would you like to take another look?

@sukhendu2002
3 years ago

Patch refreshed

@marukou1976
3 years ago

convert to utf-8

#4 @marukou1976
3 years ago

  • Keywords has-patch added; needs-patch removed

I'm re-uploading 60038.2.2diff as it was corrupted. Specifically, it was in UTF-16LE, so I have converted it to UTF-8. I have not made any changes to the source code.

The content itself includes $unsanitized_postarr as an argument, which I believe meets the requirements. I'm also developing a plugin, and I find this to be more convenient.

I've changed the status to has-patch.

Last edited 3 years ago by marukou1976 (previous) (diff)

#5 @johnbillion
3 years ago

  • Milestone Future Release → 6.6
  • Owner set to johnbillion
  • Status new → reviewing

#6 @oglekler
2 years ago

@johnbillion, we have 1 week before Beta 1. Can you review the patch, please? It looks simple enought, but I am wondering if actions inside wp_publish_post() function should have the same properties for consistency.

This ticket was mentioned in ​Slack in #core by oglekler. ​View the logs.


2 years ago

#8 @oglekler
2 years ago

@johnbillion we have just a few days before Beta 1, so, place move this forward or to the next milestone. Thank you!

#9 @oglekler
2 years ago

  • Keywords dev-feedback added
  • Milestone 6.6 → 6.7

We have 2 days before Beta 1, so I am moving it into the next milestone. 

And I hope to get an opinion about if actions inside wp_publish_post() function should have the same properties for consistency.

#10 @johnbillion
2 years ago

  • Keywords changes-requested added; dev-feedback removed

This patch needs more work because the affected filters and actions also fire in other places in core, not just within the wp_insert_post() function.

This ticket was mentioned in ​Slack in #core-test by ankit-k-gupta. ​View the logs.


2 years ago

​@khoipro commented on ​PR #5747:


2 years ago
#12

Could you @mrhatia finish a coding standard before reviewing this issue again?

#13 @davidbaumwald
2 years ago

  • Milestone 6.7 → Future Release

With 6.7 Beta 1 releasing in a few hours, this is being moved to Future Release given a lack of recent momentum towards a resolution.

@johnbillion If you or any other committer feels the remaining work can be resolved in time for Beta 1 or any other specific milestone and wishes to assume ownership during that cycle, feel free to update the milestone accordingly.

This ticket was mentioned in ​PR #14796 on ​WordPress/wordpress-develop by ​@irozum.


3 weeks ago
#14

  • Keywords has-unit-tests added

Passes the original array of arguments given to wp_insert_post() ($unsanitized_postarr) as a trailing argument to the actions and filters that fire within it, matching what wp_insert_post_data and wp_insert_attachment_data have received since 5.4.1. This lets callbacks see custom keys such as tax_input, meta_input or plugin-specific arguments that are otherwise lost.

Hooks updated inside wp_insert_post()

  • Filter: add_trashed_suffix_to_trashed_posts
  • Actions: pre_post_update, edit_attachment, attachment_updated, add_attachment, edit_post_{$post_type}, edit_post, post_updated, save_post_{$post_type}, save_post, wp_insert_post

Other places these hooks fire
As noted by @johnbillion on the ticket, some of these hooks also fire outside wp_insert_post(): in wp_publish_post(), WP_Customize_Manager::trash_changeset_post() and wp_update_comment_count_now(). Those call sites now pass an empty array(), so a callback registered with the extra argument never hits an ArgumentCountError. The docblocks say the array is empty when the hook fires outside of wp_insert_post().

Notes

  • Existing arguments are not reordered; the new argument is always last.
  • The @since version is set to 7.3.0, since 7.2 is past feature freeze. A committer may adjust it.
  • Third-party code that fires these hooks itself without the new argument would trigger an ArgumentCountError in callbacks that require it, the same trade-off as when $update was added to save_post in 3.7.0. A dev note may be worthwhile.
  • phpstan.neon.dist gets a narrowly scoped ignore: PHPStan conflates the edit_post_{$post_type} action with the unrelated edit_post_{$field} filter in sanitize_post_field() because both normalise to edit_post_{...}.
  • Props to rizwan_47, sukhendu2002, marukou1976 and mrhatia for the earlier patches on the ticket.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Implementation, tests, and PR description.

---

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with ​Claude Code

Note: See TracTickets for help on using tickets.