<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[46184] trunk: REST API: Support dot.nested hierarchical properties in _fields query parameter.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/46184">46184</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/46184","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>kadamwhite</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-09-19 14:04:51 +0000 (Thu, 19 Sep 2019)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Support dot.nested hierarchical properties in _fields query parameter.
Enable clients to opt-in to receipt of one or more specific sub-properties within a response, and not other sub-properties.
Skip potentially expensive filtering and processing for post resources which were explicitly not requested.
Props kadamwhite, TimothyBlynJacobs, dlh.
Fixes <a href="https://core.trac.wordpress.org/ticket/42094">#42094</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestcontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestcontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpostscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapiphp">trunk/tests/phpunit/tests/rest-api.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiendpointsclasswprestcontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php 2019-09-19 12:01:03 UTC (rev 46183)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php 2019-09-19 14:04:51 UTC (rev 46184)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -562,7 +562,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( in_array( 'id', $fields, true ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $requested_fields[] = 'id';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return array_intersect( $fields, $requested_fields );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Return the list of all requested fields which appear in the schema.
+ return array_reduce(
+ $requested_fields,
+ function( $response_fields, $field ) use ( $fields ) {
+ if ( in_array( $field, $fields, true ) ) {
+ $response_fields[] = $field;
+ return $response_fields;
+ }
+ // Check for nested fields if $field is not a direct match.
+ $nested_fields = explode( '.', $field );
+ // A nested field is included so long as its top-level property is
+ // present in the schema.
+ if ( in_array( $nested_fields[0], $fields, true ) ) {
+ $response_fields[] = $field;
+ }
+ return $response_fields;
+ },
+ array()
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2019-09-19 12:01:03 UTC (rev 46183)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php 2019-09-19 14:04:51 UTC (rev 46184)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1439,15 +1439,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Base fields for every post.
</span><span class="cx" style="display: block; padding: 0 10px"> $data = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'id', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'id', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['id'] = $post->ID;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'date', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'date', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'date_gmt', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'date_gmt', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // For drafts, `post_date_gmt` may not be set, indicating that the
</span><span class="cx" style="display: block; padding: 0 10px"> // date of the draft should be updated each time it is saved (see
</span><span class="cx" style="display: block; padding: 0 10px"> // #38883). In this case, shim the value based on the `post_date`
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1460,7 +1460,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $data['date_gmt'] = $this->prepare_date_response( $post_date_gmt );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'guid', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'guid', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['guid'] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-includes/post-template.php */
</span><span class="cx" style="display: block; padding: 0 10px"> 'rendered' => apply_filters( 'get_the_guid', $post->guid, $post->ID ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1468,11 +1468,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'modified', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'modified', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'modified_gmt', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'modified_gmt', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> // For drafts, `post_modified_gmt` may not be set (see
</span><span class="cx" style="display: block; padding: 0 10px"> // `post_date_gmt` comments above). In this case, shim the value
</span><span class="cx" style="display: block; padding: 0 10px"> // based on the `post_modified` field with the site's timezone
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1485,33 +1485,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $data['modified_gmt'] = $this->prepare_date_response( $post_modified_gmt );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'password', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'password', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['password'] = $post->post_password;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'slug', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'slug', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['slug'] = $post->post_name;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'status', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'status', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['status'] = $post->post_status;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'type', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'type', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['type'] = $post->post_type;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'link', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'link', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['link'] = get_permalink( $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'title', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'title', $fields ) ) {
+ $data['title'] = array();
+ }
+ if ( rest_is_field_included( 'title.raw', $fields ) ) {
+ $data['title']['raw'] = $post->post_title;
+ }
+ if ( rest_is_field_included( 'title.rendered', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $data['title'] = array(
- 'raw' => $post->post_title,
- 'rendered' => get_the_title( $post->ID ),
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $data['title']['rendered'] = get_the_title( $post->ID );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1525,17 +1528,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $has_password_filter = true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'content', $fields, true ) ) {
- $data['content'] = array(
- 'raw' => $post->post_content,
- /** This filter is documented in wp-includes/post-template.php */
- 'rendered' => post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content ),
- 'protected' => (bool) $post->post_password,
- 'block_version' => block_version( $post->post_content ),
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'content', $fields ) ) {
+ $data['content'] = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'content.raw', $fields ) ) {
+ $data['content']['raw'] = $post->post_content;
+ }
+ if ( rest_is_field_included( 'content.rendered', $fields ) ) {
+ /** This filter is documented in wp-includes/post-template.php */
+ $data['content']['rendered'] = post_password_required( $post ) ? '' : apply_filters( 'the_content', $post->post_content );
+ }
+ if ( rest_is_field_included( 'content.protected', $fields ) ) {
+ $data['content']['protected'] = (bool) $post->post_password;
+ }
+ if ( rest_is_field_included( 'content.block_version', $fields ) ) {
+ $data['content']['block_version'] = block_version( $post->post_content );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'excerpt', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'excerpt', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> /** This filter is documented in wp-includes/post-template.php */
</span><span class="cx" style="display: block; padding: 0 10px"> $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $data['excerpt'] = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1550,35 +1560,35 @@
</span><span class="cx" style="display: block; padding: 0 10px"> remove_filter( 'post_password_required', '__return_false' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'author', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'author', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['author'] = (int) $post->post_author;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'featured_media', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'featured_media', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'parent', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'parent', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['parent'] = (int) $post->post_parent;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'menu_order', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'menu_order', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['menu_order'] = (int) $post->menu_order;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'comment_status', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'comment_status', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['comment_status'] = $post->comment_status;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'ping_status', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'ping_status', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['ping_status'] = $post->ping_status;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'sticky', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'sticky', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['sticky'] = is_sticky( $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'template', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'template', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $template = get_page_template_slug( $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $template ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $data['template'] = $template;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1587,7 +1597,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'format', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'format', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['format'] = get_post_format( $post->ID );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Fill in blank post format.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1596,7 +1606,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( 'meta', $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( 'meta', $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $data['meta'] = $this->meta->get_value( $post->ID, $request );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1605,7 +1615,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $taxonomies as $taxonomy ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( in_array( $base, $fields, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( rest_is_field_included( $base, $fields ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $terms = get_the_terms( $post, $taxonomy->name );
</span><span class="cx" style="display: block; padding: 0 10px"> $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1613,8 +1623,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $post_type_obj = get_post_type_object( $post->post_type );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $permalink_template_requested = in_array( 'permalink_template', $fields, true );
- $generated_slug_requested = in_array( 'generated_slug', $fields, true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $permalink_template_requested = rest_is_field_included( 'permalink_template', $fields );
+ $generated_slug_requested = rest_is_field_included( 'generated_slug', $fields );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $permalink_template_requested || $generated_slug_requested ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! function_exists( 'get_sample_permalink' ) ) {
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php 2019-09-19 12:01:03 UTC (rev 46183)
+++ trunk/src/wp-includes/rest-api.php 2019-09-19 14:04:51 UTC (rev 46184)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -697,6 +697,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Recursively computes the intersection of arrays using keys for comparison.
+ *
+ * @param array $array1 The array with master keys to check.
+ * @param array $array2 An array to compare keys against.
+ *
+ * @return array An associative array containing all the entries of array1 which have keys that are present in all arguments.
+ */
+function _rest_array_intersect_key_recursive( $array1, $array2 ) {
+ $array1 = array_intersect_key( $array1, $array2 );
+ foreach ( $array1 as $key => $value ) {
+ if ( is_array( $value ) && is_array( $array2[ $key ] ) ) {
+ $array1[ $key ] = _rest_array_intersect_key_recursive( $value, $array2[ $key ] );
+ }
+ }
+ return $array1;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Filter the API response to include only a white-listed set of response object fields.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.8.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -723,15 +741,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Trim off outside whitespace from the comma delimited list.
</span><span class="cx" style="display: block; padding: 0 10px"> $fields = array_map( 'trim', $fields );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $fields_as_keyed = array_combine( $fields, array_fill( 0, count( $fields ), true ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Create nested array of accepted field hierarchy.
+ $fields_as_keyed = array();
+ foreach ( $fields as $field ) {
+ $parts = explode( '.', $field );
+ $ref = &$fields_as_keyed;
+ while ( count( $parts ) > 1 ) {
+ $next = array_shift( $parts );
+ $ref[ $next ] = array();
+ $ref = &$ref[ $next ];
+ }
+ $last = array_shift( $parts );
+ $ref[ $last ] = true;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( wp_is_numeric_array( $data ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $new_data = array();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $data as $item ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $new_data[] = array_intersect_key( $item, $fields_as_keyed );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $new_data[] = _rest_array_intersect_key_recursive( $item, $fields_as_keyed );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $new_data = array_intersect_key( $data, $fields_as_keyed );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $new_data = _rest_array_intersect_key_recursive( $data, $fields_as_keyed );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $response->set_data( $new_data );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -740,6 +770,41 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Given an array of fields to include in a response, some of which may be
+ * `nested.fields`, determine whether the provided field should be included
+ * in the response body.
+ *
+ * If a parent field is passed in, the presence of any nested field within
+ * that parent will cause the method to return `true`. For example "title"
+ * will return true if any of `title`, `title.raw` or `title.rendered` is
+ * provided.
+ *
+ * @since 5.3.0
+ *
+ * @param string $field A field to test for inclusion in the response body.
+ * @param array $fields An array of string fields supported by the endpoint.
+ * @return bool Whether to include the field or not.
+ */
+function rest_is_field_included( $field, $fields ) {
+ if ( in_array( $field, $fields, true ) ) {
+ return true;
+ }
+ foreach ( $fields as $accepted_field ) {
+ // Check to see if $field is the parent of any item in $fields.
+ // A field "parent" should be accepted if "parent.child" is accepted.
+ if ( strpos( $accepted_field, "$field." ) === 0 ) {
+ return true;
+ }
+ // Conversely, if "parent" is accepted, all "parent.child" fields should
+ // also be accepted.
+ if ( strpos( $field, "$accepted_field." ) === 0 ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Adds the REST API URL to the WP RSD endpoint.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestcontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-controller.php 2019-09-19 12:01:03 UTC (rev 46183)
+++ trunk/tests/phpunit/tests/rest-api/rest-controller.php 2019-09-19 14:04:51 UTC (rev 46184)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -232,7 +232,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public function data_get_fields_for_response() {
</span><span class="cx" style="display: block; padding: 0 10px"> return array(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'somestring,someinteger',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'somestring,someinteger,someinvalidkey',
</ins><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'somestring',
</span><span class="cx" style="display: block; padding: 0 10px"> 'someinteger',
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestpostscontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php 2019-09-19 12:01:03 UTC (rev 46183)
+++ trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php 2019-09-19 14:04:51 UTC (rev 46184)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1689,6 +1689,76 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * @ticket 42094
+ */
+ public function test_prepare_item_filters_content_when_needed() {
+ $filter_count = 0;
+ $filter_content = function() use ( &$filter_count ) {
+ $filter_count++;
+ return '<p>Filtered content.</p>';
+ };
+ add_filter( 'the_content', $filter_content );
+
+ wp_set_current_user( self::$editor_id );
+ $endpoint = new WP_REST_Posts_Controller( 'post' );
+ $request = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+
+ $request->set_param( 'context', 'edit' );
+ $request->set_param( '_fields', 'content.rendered' );
+
+ $post = get_post( self::$post_id );
+ $response = $endpoint->prepare_item_for_response( $post, $request );
+
+ remove_filter( 'the_content', $filter_content );
+
+ $this->assertEquals(
+ array(
+ 'id' => self::$post_id,
+ 'content' => array(
+ 'rendered' => '<p>Filtered content.</p>',
+ ),
+ ),
+ $response->get_data()
+ );
+ $this->assertSame( 1, $filter_count );
+ }
+
+ /**
+ * @ticket 42094
+ */
+ public function test_prepare_item_skips_content_filter_if_not_needed() {
+ $filter_count = 0;
+ $filter_content = function() use ( &$filter_count ) {
+ $filter_count++;
+ return '<p>Filtered content.</p>';
+ };
+ add_filter( 'the_content', $filter_content );
+
+ wp_set_current_user( self::$editor_id );
+ $endpoint = new WP_REST_Posts_Controller( 'post' );
+ $request = new WP_REST_REQUEST( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+
+ $request->set_param( 'context', 'edit' );
+ $request->set_param( '_fields', 'content.raw' );
+
+ $post = get_post( self::$post_id );
+ $response = $endpoint->prepare_item_for_response( $post, $request );
+
+ remove_filter( 'the_content', $filter_content );
+
+ $this->assertEquals(
+ array(
+ 'id' => $post->ID,
+ 'content' => array(
+ 'raw' => $post->post_content,
+ ),
+ ),
+ $response->get_data()
+ );
+ $this->assertSame( 0, $filter_count );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> public function test_create_item() {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_set_current_user( self::$editor_id );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api.php 2019-09-19 12:01:03 UTC (rev 46183)
+++ trunk/tests/phpunit/tests/rest-api.php 2019-09-19 14:04:51 UTC (rev 46184)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -520,6 +520,71 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Ensure that nested fields may be whitelisted with request['_fields'].
+ *
+ * @ticket 42094
+ */
+ public function test_rest_filter_response_fields_nested_field_filter() {
+ $response = new WP_REST_Response();
+
+ $response->set_data(
+ array(
+ 'a' => 0,
+ 'b' => array(
+ '1' => 1,
+ '2' => 2,
+ ),
+ 'c' => 3,
+ 'd' => array(
+ '4' => 4,
+ '5' => 5,
+ ),
+ )
+ );
+ $request = array(
+ '_fields' => 'b.1,c,d.5',
+ );
+
+ $response = rest_filter_response_fields( $response, null, $request );
+ $this->assertEquals(
+ array(
+ 'b' => array(
+ '1' => 1,
+ ),
+ 'c' => 3,
+ 'd' => array(
+ '5' => 5,
+ ),
+ ),
+ $response->get_data()
+ );
+ }
+
+ /**
+ * @ticket 42094
+ */
+ public function test_rest_is_field_included() {
+ $fields = array(
+ 'id',
+ 'title',
+ 'content.raw',
+ 'custom.property',
+ );
+
+ $this->assertTrue( rest_is_field_included( 'id', $fields ) );
+ $this->assertTrue( rest_is_field_included( 'title', $fields ) );
+ $this->assertTrue( rest_is_field_included( 'title.raw', $fields ) );
+ $this->assertTrue( rest_is_field_included( 'title.rendered', $fields ) );
+ $this->assertTrue( rest_is_field_included( 'content', $fields ) );
+ $this->assertTrue( rest_is_field_included( 'content.raw', $fields ) );
+ $this->assertTrue( rest_is_field_included( 'custom.property', $fields ) );
+ $this->assertFalse( rest_is_field_included( 'content.rendered', $fields ) );
+ $this->assertFalse( rest_is_field_included( 'type', $fields ) );
+ $this->assertFalse( rest_is_field_included( 'meta', $fields ) );
+ $this->assertFalse( rest_is_field_included( 'meta.value', $fields ) );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * The get_rest_url function should return a URL consistently terminated with a "/",
</span><span class="cx" style="display: block; padding: 0 10px"> * whether the blog is configured with pretty permalink support or not.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre>
</div>
</div>
</body>
</html>