Updates meta values.
Parameters
$meta
arrayrequired- Array of meta parsed from the request.
$object_id
intrequired- Object ID to fetch meta for.
Source
public function update_value( $meta, $object_id ) {
$fields = $this->get_registered_fields();
$error = new WP_Error();
foreach ( $fields as $meta_key => $args ) {
$name = $args['name'];
if ( ! array_key_exists( $name, $meta ) ) {
continue;
}
$value = $meta[ $name ];
/*
* A null value means reset the field, which is essentially deleting it
* from the database and then relying on the default value.
*
* Non-single meta can also be removed by passing an empty array.
*/
if ( is_null( $value ) || ( array() === $value && ! $args['single'] ) ) {
$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'] ) ) ) {
$error->add(
'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 )
);
continue;
}
}
$result = $this->delete_meta_value( $object_id, $meta_key, $name );
if ( is_wp_error( $result ) ) {
$error->merge_from( $result );
}
continue;
}
if ( ! $args['single'] && is_array( $value ) && count( array_filter( $value, 'is_null' ) ) ) {
$error->add(
'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 )
);
continue;
}
$is_valid = rest_validate_value_from_schema( $value, $args['schema'], 'meta.' . $name );
if ( is_wp_error( $is_valid ) ) {
$is_valid->add_data( array( 'status' => 400 ) );
$error->merge_from( $is_valid );
continue;
}
$value = rest_sanitize_value_from_schema( $value, $args['schema'] );
if ( $args['single'] ) {
$result = $this->update_meta_value( $object_id, $meta_key, $name, $value );
} else {
$result = $this->update_multi_meta_value( $object_id, $meta_key, $name, $value );
}
if ( is_wp_error( $result ) ) {
$error->merge_from( $result );
continue;
}
}
if ( $error->has_errors() ) {
return $error;
}
return null;
}
Changelog
Version | Description |
---|---|
4.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.