REST API: Correctly map meta keys to field names.

This accidentally assumed $name was the same as $meta_key, which ruined the whole point of $name.

Props tharsheblows, joehoyle.
Fixes #38786.

Built from https://develop.svn.wordpress.org/trunk@39328


git-svn-id: http://core.svn.wordpress.org/trunk@39268 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ryan McCue 2016-11-21 05:41:30 +00:00
parent 4e05ff6a11
commit ca9f71e9b2
2 changed files with 29 additions and 24 deletions

View File

@ -65,8 +65,9 @@ abstract class WP_REST_Meta_Fields {
$fields = $this->get_registered_fields(); $fields = $this->get_registered_fields();
$response = array(); $response = array();
foreach ( $fields as $name => $args ) { foreach ( $fields as $meta_key => $args ) {
$all_values = get_metadata( $this->get_meta_type(), $object_id, $name, false ); $name = $args['name'];
$all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false );
if ( $args['single'] ) { if ( $args['single'] ) {
if ( empty( $all_values ) ) { if ( empty( $all_values ) ) {
$value = $args['schema']['default']; $value = $args['schema']['default'];
@ -122,7 +123,8 @@ abstract class WP_REST_Meta_Fields {
*/ */
public function update_value( $request, $object_id ) { public function update_value( $request, $object_id ) {
$fields = $this->get_registered_fields(); $fields = $this->get_registered_fields();
foreach ( $fields as $name => $args ) { foreach ( $fields as $meta_key => $args ) {
$name = $args['name'];
if ( ! array_key_exists( $name, $request ) ) { if ( ! array_key_exists( $name, $request ) ) {
continue; continue;
} }
@ -132,7 +134,7 @@ abstract class WP_REST_Meta_Fields {
* from the database and then relying on the default value. * from the database and then relying on the default value.
*/ */
if ( is_null( $request[ $name ] ) ) { if ( is_null( $request[ $name ] ) ) {
$result = $this->delete_meta_value( $object_id, $name ); $result = $this->delete_meta_value( $object_id, $meta_key, $name );
if ( is_wp_error( $result ) ) { if ( is_wp_error( $result ) ) {
return $result; return $result;
} }
@ -148,9 +150,9 @@ abstract class WP_REST_Meta_Fields {
$value = rest_sanitize_value_from_schema( $request[ $name ], $args['schema'] ); $value = rest_sanitize_value_from_schema( $request[ $name ], $args['schema'] );
if ( $args['single'] ) { if ( $args['single'] ) {
$result = $this->update_meta_value( $object_id, $name, $value ); $result = $this->update_meta_value( $object_id, $meta_key, $name, $value );
} else { } else {
$result = $this->update_multi_meta_value( $object_id, $name, $value ); $result = $this->update_multi_meta_value( $object_id, $meta_key, $name, $value );
} }
if ( is_wp_error( $result ) ) { if ( is_wp_error( $result ) ) {
@ -168,12 +170,13 @@ abstract class WP_REST_Meta_Fields {
* @access protected * @access protected
* *
* @param int $object_id Object ID the field belongs to. * @param int $object_id Object ID the field belongs to.
* @param string $name Key for the field. * @param string $meta_key Key for the field.
* @param string $name Name for the field that is exposed in the REST API.
* @return bool|WP_Error True if meta field is deleted, WP_Error otherwise. * @return bool|WP_Error True if meta field is deleted, WP_Error otherwise.
*/ */
protected function delete_meta_value( $object_id, $name ) { protected function delete_meta_value( $object_id, $meta_key, $name ) {
$meta_type = $this->get_meta_type(); $meta_type = $this->get_meta_type();
if ( ! current_user_can( "delete_{$meta_type}_meta", $object_id, $name ) ) { if ( ! current_user_can( "delete_{$meta_type}_meta", $object_id, $meta_key ) ) {
return new WP_Error( return new WP_Error(
'rest_cannot_delete', 'rest_cannot_delete',
/* translators: %s: custom field key */ /* translators: %s: custom field key */
@ -182,7 +185,7 @@ abstract class WP_REST_Meta_Fields {
); );
} }
if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $name ) ) ) { if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $meta_key ) ) ) {
return new WP_Error( return new WP_Error(
'rest_meta_database_error', 'rest_meta_database_error',
__( 'Could not delete meta value from database.' ), __( 'Could not delete meta value from database.' ),
@ -202,13 +205,14 @@ abstract class WP_REST_Meta_Fields {
* @access protected * @access protected
* *
* @param int $object_id Object ID to update. * @param int $object_id Object ID to update.
* @param string $name Key for the custom field. * @param string $meta_key Key for the custom field.
* @param string $name Name for the field that is exposed in the REST API.
* @param array $values List of values to update to. * @param array $values List of values to update to.
* @return bool|WP_Error True if meta fields are updated, WP_Error otherwise. * @return bool|WP_Error True if meta fields are updated, WP_Error otherwise.
*/ */
protected function update_multi_meta_value( $object_id, $name, $values ) { protected function update_multi_meta_value( $object_id, $meta_key, $name, $values ) {
$meta_type = $this->get_meta_type(); $meta_type = $this->get_meta_type();
if ( ! current_user_can( "edit_{$meta_type}_meta", $object_id, $name ) ) { if ( ! current_user_can( "edit_{$meta_type}_meta", $object_id, $meta_key ) ) {
return new WP_Error( return new WP_Error(
'rest_cannot_update', 'rest_cannot_update',
/* translators: %s: custom field key */ /* translators: %s: custom field key */
@ -217,7 +221,7 @@ abstract class WP_REST_Meta_Fields {
); );
} }
$current = get_metadata( $meta_type, $object_id, $name, false ); $current = get_metadata( $meta_type, $object_id, $meta_key, false );
$to_remove = $current; $to_remove = $current;
$to_add = $values; $to_add = $values;
@ -244,7 +248,7 @@ abstract class WP_REST_Meta_Fields {
$to_remove = array_unique( $to_remove ); $to_remove = array_unique( $to_remove );
foreach ( $to_remove as $value ) { foreach ( $to_remove as $value ) {
if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $name ), wp_slash( $value ) ) ) { if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
return new WP_Error( return new WP_Error(
'rest_meta_database_error', 'rest_meta_database_error',
__( 'Could not update meta value in database.' ), __( 'Could not update meta value in database.' ),
@ -254,7 +258,7 @@ abstract class WP_REST_Meta_Fields {
} }
foreach ( $to_add as $value ) { foreach ( $to_add as $value ) {
if ( ! add_metadata( $meta_type, $object_id, wp_slash( $name ), wp_slash( $value ) ) ) { if ( ! add_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
return new WP_Error( return new WP_Error(
'rest_meta_database_error', 'rest_meta_database_error',
__( 'Could not update meta value in database.' ), __( 'Could not update meta value in database.' ),
@ -273,13 +277,14 @@ abstract class WP_REST_Meta_Fields {
* @access protected * @access protected
* *
* @param int $object_id Object ID to update. * @param int $object_id Object ID to update.
* @param string $name Key for the custom field. * @param string $meta_key Key for the custom field.
* @param string $name Name for the field that is exposed in the REST API.
* @param mixed $value Updated value. * @param mixed $value Updated value.
* @return bool|WP_Error True if the meta field was updated, WP_Error otherwise. * @return bool|WP_Error True if the meta field was updated, WP_Error otherwise.
*/ */
protected function update_meta_value( $object_id, $name, $value ) { protected function update_meta_value( $object_id, $meta_key, $name, $value ) {
$meta_type = $this->get_meta_type(); $meta_type = $this->get_meta_type();
if ( ! current_user_can( "edit_{$meta_type}_meta", $object_id, $name ) ) { if ( ! current_user_can( "edit_{$meta_type}_meta", $object_id, $meta_key ) ) {
return new WP_Error( return new WP_Error(
'rest_cannot_update', 'rest_cannot_update',
/* translators: %s: custom field key */ /* translators: %s: custom field key */
@ -288,7 +293,7 @@ abstract class WP_REST_Meta_Fields {
); );
} }
$meta_key = wp_slash( $name ); $meta_key = wp_slash( $meta_key );
$meta_value = wp_slash( $value ); $meta_value = wp_slash( $value );
// Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
@ -364,7 +369,7 @@ abstract class WP_REST_Meta_Fields {
$rest_args['schema']['type'] = 'array'; $rest_args['schema']['type'] = 'array';
} }
$registered[ $rest_args['name'] ] = $rest_args; $registered[ $name ] = $rest_args;
} }
return $registered; return $registered;
@ -388,8 +393,8 @@ abstract class WP_REST_Meta_Fields {
'properties' => array(), 'properties' => array(),
); );
foreach ( $fields as $key => $args ) { foreach ( $fields as $args ) {
$schema['properties'][ $key ] = $args['schema']; $schema['properties'][ $args['name'] ] = $args['schema'];
} }
return $schema; return $schema;

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-beta4-39327'; $wp_version = '4.7-beta4-39328';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.