Make WordPress Core

Opened 10 years ago

Last modified 6 weeks ago

#39706 new enhancement

Add $unique param to add_{$meta_type}_meta actions

Reported by: barryceelen Owned by:
Priority: normal Milestone: Future Release
Component: Options, Meta APIs Version: 3.1
Severity: normal Keywords: good-first-bug has-patch has-unit-tests
Cc: Focuses:

Description

The hooks add_{$meta_type}_meta and similar provide context about $object_id, $meta_key, $_meta_value and such, but do not tell whether the meta data added or updated is supposed to be $unique.

Adding this information could be helpful. My use case for instance is propagating meta data across posts.

Change History (6)

#1 @barryceelen
10 years ago

In a similar fashion, the delete_{$meta_type}_meta could have the $delete_all param added.

#2 @swissspidy
2 months ago

  • Keywords good-first-bug needs-patch added
  • Milestone Awaiting Review → Future Release
  • Version → 3.1

#3 @brianboy
2 months ago

Working on this, first time contributer. Will open a PR

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


2 months ago
#4

  • Keywords has-patch has-unit-tests added; needs-patch removed

## What

Passes the $unique flag given to add_metadata() through to the add_{$meta_type}_meta and added_{$meta_type}_meta action 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}_metadata filter 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.php assert that both actions receive the $unique value for both true and false, using callbacks that opt in to the new argument count.

  • New tests: 2 tests, 4 assertions, passing.
  • Full meta group: 460 tests, 1222 assertions, passing.
  • PHPCS: no new issues on changed files.
  • PHPStan hook docblock verification: no errors.

## Notes

  • @since 7.2.0 was used because trunk is currently in the 7.1 beta cycle; happy to adjust.
  • Comment 1 on the ticket suggests similarly passing $delete_all to the delete_{$meta_type}_meta hooks. That is left out here to keep this PR scoped to the ticket summary, and could be a follow-up.

#5 @brianboy
2 months ago

I've opened a pull request for this: ​https://github.com/WordPress/wordpress-develop/pull/12768

It passes $unique to both the add_{$meta_type}_meta and added_{$meta_type}_meta actions as an additional argument, matching the add_{$meta_type}_metadata filter which already receives it, and includes unit tests covering both hooks.

The $delete_all suggestion from comment:1 is intentionally not included to keep this scoped to the ticket summary. Happy to tackle that as a follow-up.

Last edited 6 weeks ago by brianboy (previous) (diff)

#6 @brianboy
6 weeks ago

The $delete_all suggestion from comment:1 is now tracked in #65976, with a PR following the same approach as this one.

Note: See TracTickets for help on using tickets.