[wp-trac] [WordPress Trac] #38323: Reconsider $object_subtype handling in `register_meta()`

WordPress Trac noreply at wordpress.org
Thu Dec 29 19:54:24 UTC 2016


#38323: Reconsider $object_subtype handling in `register_meta()`
-------------------------------------------------+-------------------------
 Reporter:  flixos90                             |       Owner:
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
Component:  Options, Meta APIs                   |  Review
 Severity:  normal                               |     Version:
 Keywords:  has-patch needs-unit-tests 2nd-      |  Resolution:
  opinion                                        |     Focuses:
-------------------------------------------------+-------------------------

Comment (by tharsheblows):

 Your description was good, it was me! I misread it and conflated it with
 something else; it was totally down to my confusion. Thanks for being nice
 about it. :)

 Just to make sure I've got it correct this time: the issues here arise if
 people accidentally register the same meta key, ie don't use unique meta
 key names. That makes sense.

 >When retrieving all registered metadata for an object, we need to look at
 both meta registered for its general type and meta registered for its
 subtype. In the above patch the latter takes precedence in case there are
 meta keys registered

 This also makes sense to me, especially because someone using a non-unique
 meta key name is, I'd guess, more likely to use the easiest form of
 `register_meta()` and register it for the object type without a subtype.

 Unregistering object subtypes has the potential to be confusing,
 `unregister_meta_key()` won't work quite as expected if someone tries to
 unregister a meta key for an individual object subtype and the meta key is
 also registered for the object type eg
 {{{#!php
  register_meta( 'post', 'key', array( 'show_in_rest' => true ) );
  register_meta( 'post', 'key', array( 'show_in_rest' => true,
 'object_subtype' => 'cpt' ) );
  unregister_meta( 'post', 'key', 'cpt' );
 }}}
 will still allow the meta key "key" to show up in the REST API for posts
 which have a cpt custom post type.

 Also if someone uses a non-unique meta key that someone else has used and
 registers it with `'show_in_rest'=>true` then whatever it's duplicated
 will show up in the REST API response in whichever object subtype it's
 used.

 To mitigate both of those, it would be nice to be able to explicitly
 whitelist meta keys for a given object subtype. Either a filter on eg
 `get_registered_fields()` or add it maybe like
 `add_post_type_support('cpt', 'custom-fields', array('key', 'key1',
 'key2')` or when the post type is registered. It would be nice if
 `$wp_meta_keys` could be a reliable whitelist but I don't think it can for
 subtypes. (Is that right?)

 I think the change in the structure of `$wp_meta_keys` could be an issue
 but that's only because it's there so people might be using it as they do.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/38323#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list