[wp-trac] [WordPress Trac] #42094: REST API: extend _fields parameter to selectively include nested fields in response JSON
WordPress Trac
noreply at wordpress.org
Tue May 21 15:12:40 UTC 2019
#42094: REST API: extend _fields parameter to selectively include nested fields in
response JSON
-------------------------------------------------+-------------------------
Reporter: kadamwhite | Owner: kadamwhite
Type: enhancement | Status: assigned
Priority: normal | Milestone: 5.3
Component: REST API | Version: 4.9
Severity: normal | Resolution:
Keywords: has-patch dev-feedback needs- | Focuses:
testing has-unit-tests |
-------------------------------------------------+-------------------------
Changes (by kadamwhite):
* keywords: needs-refresh => has-patch dev-feedback needs-testing has-
unit-tests
Comment:
I have uploaded a patch with a proposed solution to this problem. It works
in three parts:
- The initial generation of the accepted fields list is modified to be
`nested.field`-aware,
- The posts controller is modified to use a smarter helper method to
determine if a specific property should be included, rather than a simple
`in_array` call, and
- The filter which strips invalid properties out of the response is
modified to do a recursive check against a tree generated from the
`_fields` input string. (Thank you @rmccue for helping with the code
here.)
I would love to get some critique on this patch.
My biggest dissatisfaction with it is the code organization: the recursive
key merge method feels like the simplest solution to this problem, but
making that a `rest_`-prefixed global function feels inappropriate.
However, introducing an arbitrary `wp_` helper method also feels like an
overstep. I'd welcome input, @danielbachhuber @desrosj @timothybjacobs
One additional unit test I'd like to add would be a check to ensure the
content filters are not triggered if `content.rendered` is not requested
in the fields list.
Once we agree on the mechanics of the patch, I can update the remaining
controllers to use this new method when determining what fields to
compute.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/42094#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list