[wp-trac] [WordPress Trac] #43941: Add default value to register meta
WordPress Trac
noreply at wordpress.org
Tue Jun 26 23:40:11 UTC 2018
#43941: Add default value to register meta
----------------------------------------+---------------------------
Reporter: spacedmonkey | Owner: spacedmonkey
Type: enhancement | Status: assigned
Priority: normal | Milestone: 5.0
Component: Options, Meta APIs | Version: 4.6
Severity: normal | Resolution:
Keywords: has-patch needs-unit-tests | Focuses: rest-api
----------------------------------------+---------------------------
Comment (by flixos90):
Replying to [comment:6 spacedmonkey]:
> I am not sure I like this idea. I personally hate conditional filters in
core, it is means that developers have to route through code and fine out
when filters are fired. This is confusing. Firing both filters here does
the same thing functionally, not sure why we need to change it.
We decided earlier for #38323 that only the subtype-specific filters
should fire if used, with the other ones only executed as fallback. This
is the case for both sanitize and auth filters, and it should be the same
here.
> So my original patch did this, but I couldn't get it working without
with either,
> - Using an an anonymous function, which are not supported in PHP 5.2
> - Using `create_function`, which is not supported in later versions of
PHP.
> - Create two new functions, that are single use. This feels a little
pointless and weird. Might just be me. Am I missing something here?
I agree that this would be far easier if we had anonymous functions, but
it's also okay to introduce a one-off function for that. Please check how
this is handled for options (see `filter_default_option()`).
> > get_metadata_default() as well as the two filters need to be aware of
the value of $single, in order to allow developers full flexibility in
handling this. The default value may need to be different, depending on
how a meta value is requested.
>
> How do you intend this works? So in register_meta, should it have
`default_single` and `default_multi`. If so, if `default_multi` isn't set,
does it fallback to `default_single`.This doesn't feel right. I personally
think of defaults as defaulting a single value. If anything, I wouldn't
support defaulting the multiple values.
I absolutely agree we should not have two default values. However I think
we need to think of the filters and the registered default value a bit
more separately. The filters themselves need to be aware of how the meta
value is requested. However, when registering, there should only be a
single `default` value. For example, if a meta key is registered with
`single => false` and `default => array( 'my_value, 'my_value_2' )` and
someone calls `get_metadata()` with `$single` set to true, the default
filter callback would need to handle this, and only return the first value
from the array (which here would be `my_value`). It is unfortunate that we
still have this `$single` parameter although it could now automatically be
handled. But since it is there, we definitely need to ensure the returned
default value takes this into account.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/43941#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list