<!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>[45807] trunk: REST API: Support 'object' and 'array' types in register_meta() schemas.</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/45807">45807</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/45807","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-08-15 17:16:21 +0000 (Thu, 15 Aug 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 'object' and 'array' types in register_meta() schemas.
Extends meta registration to support complex schema values, mirroring the functionality in the settings controller.
Error when trying to modify a meta key containing schema-nonconformant data.
Props @TimothyBlynJacobs, @birgire, @mnelson4, @flixos90.
Fixes <a href="https://core.trac.wordpress.org/ticket/43392">#43392</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapifieldsclasswprestmetafieldsphp">trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunktestsphpunitincludesclassbasicobjectphp">trunk/tests/phpunit/includes/class-basic-object.php</a></li>
<li><a href="#trunktestsphpunitincludesfunctionsphp">trunk/tests/phpunit/includes/functions.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestpostmetafieldsphp">trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapifieldsclasswprestmetafieldsphp"></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/fields/class-wp-rest-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php 2019-08-15 13:05:39 UTC (rev 45806)
+++ trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php 2019-08-15 17:16:21 UTC (rev 45807)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -143,6 +143,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * from the database and then relying on the default value.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_null( $meta[ $name ] ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $args = $this->get_registered_fields()[ $meta_key ];
+
+ if ( $args['single'] ) {
+ $current = get_metadata( $this->get_meta_type(), $object_id, $meta_key, true );
+
+ if ( is_wp_error( rest_validate_value_from_schema( $current, $args['schema'] ) ) ) {
+ return new WP_Error(
+ 'rest_invalid_stored_value',
+ /* translators: %s: custom field key */
+ sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ),
+ array( 'status' => 500 )
+ );
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $result = $this->delete_meta_value( $object_id, $meta_key, $name );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $result ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $result;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -150,13 +165,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</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">- $is_valid = rest_validate_value_from_schema( $meta[ $name ], $args['schema'], 'meta.' . $name );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $value = $meta[ $name ];
+
+ if ( ! $args['single'] && is_array( $value ) && count( array_filter( $value, 'is_null' ) ) ) {
+ return new WP_Error(
+ 'rest_invalid_stored_value',
+ /* translators: %s: custom field key */
+ sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ),
+ array( 'status' => 500 )
+ );
+ }
+
+ $is_valid = rest_validate_value_from_schema( $value, $args['schema'], 'meta.' . $name );
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $is_valid ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $is_valid->add_data( array( 'status' => 400 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> return $is_valid;
</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">- $value = rest_sanitize_value_from_schema( $meta[ $name ], $args['schema'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $value = rest_sanitize_value_from_schema( $value, $args['schema'] );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $args['single'] ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $result = $this->update_meta_value( $object_id, $meta_key, $name, $value );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -260,8 +286,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $to_add[ $add_key ] );
</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">- // `delete_metadata` removes _all_ instances of the value, so only call once.
- $to_remove = array_unique( $to_remove );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // `delete_metadata` removes _all_ instances of the value, so only call once. Otherwise,
+ // `delete_metadata` will return false for subsequent calls of the same value.
+ // Use serialization to produce a predictable string that can be used by array_unique.
+ $to_remove = array_map( 'maybe_unserialize', array_unique( array_map( 'maybe_serialize', $to_remove ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $to_remove as $value ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -393,15 +421,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $type = ! empty( $rest_args['type'] ) ? $rest_args['type'] : null;
</span><span class="cx" style="display: block; padding: 0 10px"> $type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type;
</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, array( 'string', 'boolean', 'integer', 'number' ) ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( null === $rest_args['schema']['default'] ) {
+ $rest_args['schema']['default'] = $this->get_default_for_type( $type );
+ }
+
+ $rest_args['schema'] = $this->default_additional_properties_to_false( $rest_args['schema'] );
+
+ if ( ! in_array( $type, array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ) ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> continue;
</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"> if ( empty( $rest_args['single'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $rest_args['schema']['items'] = array(
- 'type' => $rest_args['type'],
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $rest_args['schema'] = array(
+ 'type' => 'array',
+ 'items' => $rest_args['schema'],
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $rest_args['schema']['type'] = 'array';
</del><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"> $registered[ $name ] = $rest_args;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -452,34 +486,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return mixed Value prepared for output. If a non-JsonSerializable object, null.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function prepare_value( $value, $request, $args ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $type = $args['schema']['type'];
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // For multi-value fields, check the item type instead.
- if ( 'array' === $type && ! empty( $args['schema']['items']['type'] ) ) {
- $type = $args['schema']['items']['type'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $args['single'] ) {
+ $schema = $args['schema'];
+ } else {
+ $schema = $args['schema']['items'];
</ins><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">- switch ( $type ) {
- case 'string':
- $value = (string) $value;
- break;
- case 'integer':
- $value = (int) $value;
- break;
- case 'number':
- $value = (float) $value;
- break;
- case 'boolean':
- $value = (bool) $value;
- break;
- }
-
- // Don't allow objects to be output.
- if ( is_object( $value ) && ! ( $value instanceof JsonSerializable ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return null;
</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">- return $value;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return rest_sanitize_value_from_schema( $value, $schema );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -499,4 +517,63 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $value;
</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 add additionalProperties = false to all objects in a schema if no additionalProperties setting
+ * is specified.
+ *
+ * This is needed to restrict properties of objects in meta values to only
+ * registered items, as the REST API will allow additional properties by
+ * default.
+ *
+ * @since 5.3.0
+ *
+ * @param array $schema The schema array.
+ * @return array
+ */
+ protected function default_additional_properties_to_false( $schema ) {
+ switch ( $schema['type'] ) {
+ case 'object':
+ foreach ( $schema['properties'] as $key => $child_schema ) {
+ $schema['properties'][ $key ] = $this->default_additional_properties_to_false( $child_schema );
+ }
+
+ if ( ! isset( $schema['additionalProperties'] ) ) {
+ $schema['additionalProperties'] = false;
+ }
+ break;
+ case 'array':
+ $schema['items'] = $this->default_additional_properties_to_false( $schema['items'] );
+ break;
+ }
+
+ return $schema;
+ }
+
+ /**
+ * Gets the default value for a schema type.
+ *
+ * @since 5.3.0
+ *
+ * @param string $type
+ * @return mixed
+ */
+ protected function get_default_for_type( $type ) {
+ switch ( $type ) {
+ case 'string':
+ return '';
+ case 'boolean':
+ return false;
+ case 'integer':
+ return 0;
+ case 'number':
+ return 0.0;
+ case 'array':
+ return array();
+ case 'object':
+ return array();
+ default:
+ return null;
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</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-08-15 13:05:39 UTC (rev 45806)
+++ trunk/src/wp-includes/rest-api.php 2019-08-15 17:16:21 UTC (rev 45807)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1159,6 +1159,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $value instanceof stdClass ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $value = (array) $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ if ( $value instanceof JsonSerializable ) {
+ $value = $value->jsonSerialize();
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_array( $value ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: 1: parameter, 2: type name */
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1170,8 +1175,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $is_valid ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return $is_valid;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( isset( $args['additionalProperties'] ) && false === $args['additionalProperties'] ) {
- return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( isset( $args['additionalProperties'] ) ) {
+ if ( false === $args['additionalProperties'] ) {
+ return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s is not a valid property of Object.' ), $property ) );
+ }
+
+ if ( is_array( $args['additionalProperties'] ) ) {
+ $is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
+ if ( is_wp_error( $is_valid ) ) {
+ return $is_valid;
+ }
+ }
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1298,6 +1312,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $value instanceof stdClass ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $value = (array) $value;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ if ( $value instanceof JsonSerializable ) {
+ $value = $value->jsonSerialize();
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_array( $value ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1305,8 +1324,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $value as $property => $v ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $args['properties'][ $property ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- } elseif ( isset( $args['additionalProperties'] ) && false === $args['additionalProperties'] ) {
- unset( $value[ $property ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ } elseif ( isset( $args['additionalProperties'] ) ) {
+ if ( false === $args['additionalProperties'] ) {
+ unset( $value[ $property ] );
+ } elseif ( is_array( $args['additionalProperties'] ) ) {
+ $value[ $property ] = rest_sanitize_value_from_schema( $v, $args['additionalProperties'] );
+ }
</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="trunktestsphpunitincludesclassbasicobjectphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/class-basic-object.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/class-basic-object.php 2019-08-15 13:05:39 UTC (rev 45806)
+++ trunk/tests/phpunit/includes/class-basic-object.php 2019-08-15 17:16:21 UTC (rev 45807)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,8 +7,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.7.0
</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">-trigger_error( __FILE__ . ' is deprecated since version 5.0.0 with no alternative available.' );
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Class used to test accessing methods and properties
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunktestsphpunitincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/functions.php 2019-08-15 13:05:39 UTC (rev 45806)
+++ trunk/tests/phpunit/includes/functions.php 2019-08-15 17:16:21 UTC (rev 45807)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,4 +1,5 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require_once __DIR__ . '/class-basic-object.php';
</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"> * Retrieves PHPUnit runner version.
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestpostmetafieldsphp"></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-post-meta-fields.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php 2019-08-15 13:05:39 UTC (rev 45806)
+++ trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php 2019-08-15 17:16:21 UTC (rev 45807)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1176,7 +1176,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">- * @ticket 38323
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 38323
</ins><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_get_subtype_meta_value
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_get_subtype_meta_value( $post_type, $meta_key, $single, $in_post_type ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1228,7 +1228,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">- * @ticket 38323
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 38323
</ins><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_set_subtype_meta_value
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_set_subtype_meta_value( $post_type, $meta_key, $single, $in_post_type, $can_write ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1256,6 +1256,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $can_write ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 403, $response->get_status() );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEmpty( get_post_meta( $post_id, $meta_key, $single ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px"> return;
</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">@@ -1298,7 +1299,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">- * @ticket 42069
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 42069
</ins><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_update_value_return_success_with_same_value
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1348,6 +1349,778 @@
</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 43392
+ */
+ public function test_object_single() {
+ $this->grant_write_permission();
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => true,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => array(
+ 'project' => 'WordPress',
+ ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'object', $data['meta'] );
+ $this->assertArrayHasKey( 'project', $data['meta']['object'] );
+ $this->assertEquals( 'WordPress', $data['meta']['object']['project'] );
+
+ $meta = get_post_meta( self::$post_id, 'object', true );
+ $this->assertArrayHasKey( 'project', $meta );
+ $this->assertEquals( 'WordPress', $meta['project'] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_object_multiple() {
+ $this->grant_write_permission();
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => false,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => array(
+ array(
+ 'project' => 'WordPress',
+ ),
+ array(
+ 'project' => 'bbPress',
+ ),
+ ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'object', $data['meta'] );
+ $this->assertCount( 2, $data['meta']['object'] );
+
+ $this->assertArrayHasKey( 'project', $data['meta']['object'][0] );
+ $this->assertEquals( 'WordPress', $data['meta']['object'][0]['project'] );
+
+ $this->assertArrayHasKey( 'project', $data['meta']['object'][1] );
+ $this->assertEquals( 'bbPress', $data['meta']['object'][1]['project'] );
+
+ $meta = get_post_meta( self::$post_id, 'object' );
+
+ $this->assertCount( 2, $meta );
+
+ $this->assertArrayHasKey( 'project', $meta[0] );
+ $this->assertEquals( 'WordPress', $meta[0]['project'] );
+
+ $this->assertArrayHasKey( 'project', $meta[1] );
+ $this->assertEquals( 'bbPress', $meta[1]['project'] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_array_single() {
+ $this->grant_write_permission();
+
+ register_post_meta(
+ 'post',
+ 'list',
+ array(
+ 'single' => true,
+ 'type' => 'array',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'array',
+ 'items' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'list' => array( 'WordPress', 'bbPress' ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'list', $data['meta'] );
+ $this->assertEquals( array( 'WordPress', 'bbPress' ), $data['meta']['list'] );
+
+ $meta = get_post_meta( self::$post_id, 'list', true );
+ $this->assertEquals( array( 'WordPress', 'bbPress' ), $meta );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_array_of_objects_multiple() {
+ $this->grant_write_permission();
+
+ register_post_meta(
+ 'post',
+ 'list_of_objects',
+ array(
+ 'single' => false,
+ 'type' => 'array',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'array',
+ 'items' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'version' => array(
+ 'type' => 'string',
+ ),
+ 'artist' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'list_of_objects' => array(
+ // Meta 1
+ array(
+ array(
+ 'version' => '5.2',
+ 'artist' => 'Jaco',
+ ),
+ array(
+ 'version' => '5.1',
+ 'artist' => 'Betty',
+ ),
+ ),
+ // Meta 2
+ array(
+ array(
+ 'version' => '4.9',
+ 'artist' => 'Tipton',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'list_of_objects', $data['meta'] );
+ $this->assertCount( 2, $data['meta']['list_of_objects'] );
+
+ $this->assertEquals(
+ array(
+ array(
+ 'version' => '5.2',
+ 'artist' => 'Jaco',
+ ),
+ array(
+ 'version' => '5.1',
+ 'artist' => 'Betty',
+ ),
+ ),
+ $data['meta']['list_of_objects'][0]
+ );
+
+ $this->assertEquals(
+ array(
+ array(
+ 'version' => '4.9',
+ 'artist' => 'Tipton',
+ ),
+ ),
+ $data['meta']['list_of_objects'][1]
+ );
+
+ $meta = get_post_meta( self::$post_id, 'list_of_objects' );
+
+ $this->assertCount( 2, $meta );
+
+ $this->assertEquals(
+ array(
+ array(
+ 'version' => '5.2',
+ 'artist' => 'Jaco',
+ ),
+ array(
+ 'version' => '5.1',
+ 'artist' => 'Betty',
+ ),
+ ),
+ $meta[0]
+ );
+
+ $this->assertEquals(
+ array(
+ array(
+ 'version' => '4.9',
+ 'artist' => 'Tipton',
+ ),
+ ),
+ $meta[1]
+ );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_php_objects_returned_as_null() {
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => true,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $basic = new Basic_Object();
+ $basic->project = 'WordPress';
+ update_post_meta( self::$post_id, 'object', $basic );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'object', $data['meta'] );
+ $this->assertNull( $data['meta']['object'] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_php_objects_returned_as_null_multiple() {
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => false,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $basic = new Basic_Object();
+ $basic->project = 'WordPress';
+ add_post_meta( self::$post_id, 'object', array( 'project' => 'bbPress' ) );
+ add_post_meta( self::$post_id, 'object', $basic );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'object', $data['meta'] );
+ $this->assertCount( 2, $data['meta']['object'] );
+ $this->assertEquals( array( 'project' => 'bbPress' ), $data['meta']['object'][0] );
+ $this->assertNull( $data['meta']['object'][1] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_php_jsonserializable_object_returns_value() {
+ require_once __DIR__ . '/../../includes/class-jsonserializable-object.php';
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => true,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ update_post_meta( self::$post_id, 'object', new JsonSerializable_Object( array( 'project' => 'WordPress' ) ) );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertArrayHasKey( 'object', $data['meta'] );
+ $this->assertEquals( array( 'project' => 'WordPress' ), $data['meta']['object'] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_updating_meta_to_null_for_key_with_existing_php_object_does_not_delete_meta_value() {
+ $this->grant_write_permission();
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => true,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $basic = new Basic_Object();
+ $basic->project = 'WordPress';
+ update_post_meta( self::$post_id, 'object', $basic );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => null,
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 500, $response->get_status() );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_updating_non_single_meta_to_null_for_key_with_existing_php_object_does_not_set_meta_value_to_null() {
+ $this->grant_write_permission();
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => false,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $basic = new Basic_Object();
+ $basic->project = 'WordPress';
+ add_post_meta( self::$post_id, 'object', array( 'project' => 'bbPress' ) );
+ add_post_meta( self::$post_id, 'object', $basic );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => array(
+ array( 'project' => 'BuddyPress' ),
+ null,
+ ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 500, $response->get_status() );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_object_rejects_additional_properties_by_default() {
+ $this->grant_write_permission();
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => true,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => array(
+ 'project' => 'BuddyPress',
+ 'awesomeness' => 100,
+ ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 400, $response->get_status() );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_object_allows_additional_properties_if_explicitly_set() {
+ $this->grant_write_permission();
+
+ $value = array(
+ 'project' => 'BuddyPress',
+ 'awesomeness' => 100,
+ );
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => true,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'additionalProperties' => true,
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => $value,
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+ $this->assertEquals( $value, $response->get_data()['meta']['object'] );
+
+ $this->assertEquals( $value, get_post_meta( self::$post_id, 'object', true ) );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_object_allows_additional_properties_and_uses_its_schema() {
+ $this->grant_write_permission();
+
+ $value = array(
+ 'project' => 'BuddyPress',
+ 'awesomeness' => 'fabulous',
+ );
+
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => true,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'additionalProperties' => array(
+ 'type' => 'number',
+ ),
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => $value,
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 400, $response->get_status() );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_invalid_meta_value_are_set_to_null_in_response() {
+ register_post_meta(
+ 'post',
+ 'email',
+ array(
+ 'single' => true,
+ 'type' => 'string',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'string',
+ 'format' => 'email',
+ ),
+ ),
+ )
+ );
+
+ update_post_meta( self::$post_id, 'email', 'invalid_meta_value' );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertNull( $response->get_data()['meta']['email'] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable() {
+ register_post_meta(
+ 'post',
+ 'boolean',
+ array(
+ 'single' => true,
+ 'show_in_rest' => true,
+ 'type' => 'boolean',
+ )
+ );
+
+ update_post_meta( self::$post_id, 'boolean', 'true' );
+
+ $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertTrue( $response->get_data()['meta']['boolean'] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_update_multi_meta_value_object() {
+ register_post_meta(
+ 'post',
+ 'object',
+ array(
+ 'single' => false,
+ 'type' => 'object',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'project' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ ),
+ )
+ );
+
+ add_post_meta(
+ self::$post_id,
+ 'object',
+ array(
+ 'project' => 'WordPress',
+ )
+ );
+ add_post_meta(
+ self::$post_id,
+ 'object',
+ array(
+ 'project' => 'bbPress',
+ )
+ );
+
+ $this->grant_write_permission();
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'object' => array(
+ array( 'project' => 'WordPress' ),
+ array( 'project' => 'BuddyPress' ),
+ ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $this->assertArrayHasKey( 'object', $data['meta'] );
+
+ $this->assertCount( 2, $data['meta']['object'] );
+ $this->assertEquals( array( 'project' => 'WordPress' ), $data['meta']['object'][0] );
+ $this->assertEquals( array( 'project' => 'BuddyPress' ), $data['meta']['object'][1] );
+
+ $meta = get_post_meta( self::$post_id, 'object' );
+ $this->assertCount( 2, $meta );
+ $this->assertEquals( array( 'project' => 'WordPress' ), $meta[0] );
+ $this->assertEquals( array( 'project' => 'BuddyPress' ), $meta[1] );
+ }
+
+ /**
+ * @ticket 43392
+ */
+ public function test_update_multi_meta_value_array() {
+ register_post_meta(
+ 'post',
+ 'list',
+ array(
+ 'type' => 'array',
+ 'show_in_rest' => array(
+ 'schema' => array(
+ 'type' => 'array',
+ 'items' => array(
+ 'type' => 'string',
+ ),
+ ),
+ ),
+ )
+ );
+
+ add_post_meta( self::$post_id, 'list', array( 'WordPress', 'bbPress' ) );
+ add_post_meta( self::$post_id, 'list', array( 'WordCamp' ) );
+
+ $this->grant_write_permission();
+
+ $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
+ $request->set_body_params(
+ array(
+ 'meta' => array(
+ 'list' => array(
+ array( 'WordPress', 'bbPress' ),
+ array( 'BuddyPress' ),
+ ),
+ ),
+ )
+ );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertEquals( 200, $response->get_status() );
+
+ $data = $response->get_data();
+ $this->assertArrayHasKey( 'list', $data['meta'] );
+
+ $this->assertCount( 2, $data['meta']['list'] );
+ $this->assertEquals( array( 'WordPress', 'bbPress' ), $data['meta']['list'][0] );
+ $this->assertEquals( array( 'BuddyPress' ), $data['meta']['list'][1] );
+
+ $meta = get_post_meta( self::$post_id, 'list' );
+ $this->assertCount( 2, $meta );
+ $this->assertEquals( array( 'WordPress', 'bbPress' ), $meta[0] );
+ $this->assertEquals( array( 'BuddyPress' ), $meta[1] );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Internal function used to disable an insert query which
</span><span class="cx" style="display: block; padding: 0 10px"> * will trigger a wpdb error for testing purposes.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span></span></pre>
</div>
</div>
</body>
</html>