Options, Meta APIs: Pass $unique to the add_{$meta_type}_meta and added_{$meta_type}_meta actions. - #12768
Conversation
…ed_{$meta_type}_meta actions.
The $unique flag passed to add_metadata() was not exposed to the
add_{$meta_type}_meta and added_{$meta_type}_meta action hooks, so
callbacks could not tell whether the metadata being added was meant to
be unique for the object. Pass it as an additional argument, matching
the add_{$meta_type}_metadata filter which already receives it.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
This PR updates the core Metadata API so that the $unique flag passed to add_metadata() is forwarded to the dynamic action hooks fired before/after insertion, aligning the action signatures with the existing add_{$meta_type}_metadata filter.
Changes:
- Pass
$uniqueas an additional argument toadd_{$meta_type}_meta(4th arg) andadded_{$meta_type}_meta(5th arg). - Update the corresponding hook docblocks to document the new argument and
@sincetag. - Add PHPUnit coverage verifying both actions receive the correct
$uniquevalue fortrueandfalse.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/wp-includes/meta.php |
Forwards $unique into the relevant add_*_meta/added_*_meta actions and updates hook documentation. |
tests/phpunit/tests/meta/addMetadata.php |
Adds unit tests ensuring the action callbacks receive the $unique flag when opting into the new accepted-args count. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * @since 2.9.0 | ||
| * @since 7.2.0 The `$unique` parameter was added. | ||
| * | ||
| * @param int $mid The meta ID after successful update. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…n addition, not an update.
Trac ticket: https://core.trac.wordpress.org/ticket/39706
What
Passes the
$uniqueflag given toadd_metadata()through to theadd_{$meta_type}_metaandadded_{$meta_type}_metaaction hooks, as an additional (4th respectively 5th) argument.Why
Callbacks on these hooks can currently see what metadata is being added, but not whether the caller requested it to be unique for the object, even though that flag changes the semantics of the operation. The related
add_{$meta_type}_metadatafilter already receives$unique; this brings the actions in line with it.The change is backward compatible: existing callbacks declared their accepted argument count against the old signature and continue to receive exactly the arguments they did before. Only callbacks that opt in to the additional argument receive it.
Testing
New unit tests in
tests/phpunit/tests/meta/addMetadata.phpassert that both actions receive the$uniquevalue for bothtrueandfalse, using callbacks that opt in to the new argument count.metagroup: 460 tests, 1222 assertions, passing.Notes
@since 7.2.0was used because trunk is currently in the 7.1 beta cycle; happy to adjust.$delete_allto thedelete_{$meta_type}_metahooks. That is left out here to keep this PR scoped to the ticket summary, and could be a follow-up.