[wp-trac] [WordPress Trac] #43392: Support associative array type in register_meta()

WordPress Trac noreply at wordpress.org
Thu Aug 16 17:28:50 UTC 2018


#43392: Support associative array type in register_meta()
--------------------------------+-----------------------------
 Reporter:  diegoliv            |       Owner:  (none)
     Type:  enhancement         |      Status:  new
 Priority:  normal              |   Milestone:  Future Release
Component:  Options, Meta APIs  |     Version:  4.9.4
 Severity:  normal              |  Resolution:
 Keywords:                      |     Focuses:  rest-api
--------------------------------+-----------------------------
Changes (by flixos90):

 * keywords:  close =>
 * milestone:  Awaiting Review => Future Release


Comment:

 I think we need to differentiate here a bit more: The way we use
 associative arrays is typically no different from using objects. They have
 properties and values. This becomes more obvious when looking at
 JavaScript where associative arrays do not exist.

 The REST API already supports both objects and arrays in the settings
 controller. It doesn't actually make it easy to use those, but you can
 without problems specify a schema for an object or array, and even nest
 it. There is no harm in doing the same for metadata, at least for objects
 (as in equal to associative arrays). Arrays might be a little more
 complicated to figure out with meta, due to the `$single` vs not-`$single`
 caveat that settings do not have.

 This is not unknown territory, the REST API already supports it elsewhere.
 Note that, when looking at JSON schema, the proper definition for an
 object would look like this:

 {{{
 {
     type: 'object',
     properties: {
         my_prop1: {
             type: 'string',
             enum: [ 'Hello', 'Bye' ]
         },
         my_prop2: {
             type: 'integer'
         }
     },
     default: {
         my_prop1: 'Hello',
         my_prop2: 1
     }
 }
 }}}

 For arrays, it would look like this:

 {{{
 {
     type: 'array',
     items: {
         type: 'string',
         enum: [ 'Hello', 'Bye' ]
     }
 }
 }}}

 Again, we need to be careful about arrays. If you specified the above
 schema for an array while setting `$single` to false, it would result in
 each meta value for the key being an array in the above format, so the
 full metadata returned would actually be an array of arrays.

 Regarding serialization, I consider this problematic for the above
 workaround, passing serialized or JSON-encoded strings to the API. Once we
 actually support proper objects and arrays, this will happen internally.
 Developers have been saving arrays and objects as both metadata and
 settings for years without significant issues.

 Let's think about this further, but I recommend drifting away from talking
 about associative arrays and rather talk about objects and "real" arrays,
 which have a clear differentiation and exist in that form in the majority
 of programming languages.

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


More information about the wp-trac mailing list