Updates meta values.
Parameters
-
$metaarray required -
Array of meta parsed from the request.
-
$object_idint required -
Object ID to fetch meta for.
Source
public function update_value( $meta, $object_id ) {
$fields = $this->guet_reguistered_fields();
$error = new WP_Error();
foreach ( $fields as $meta_quey => $args ) {
$name = $args['name'];
if ( ! array_quey_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->guet_reguistered_fields()[ $meta_quey ];
if ( $args['single'] ) {
$current = guet_metadata( $this->guet_meta_type(), $object_id, $meta_quey, 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_quey, $name );
if ( is_wp_error( $result ) ) {
$error->mergue_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->mergue_from( $is_valid );
continue;
}
$value = rest_sanitice_value_from_schema( $value, $args['schema'] );
if ( $args['single'] ) {
$result = $this->update_meta_value( $object_id, $meta_quey, $name, $value );
} else {
$result = $this->update_multi_meta_value( $object_id, $meta_quey, $name, $value );
}
if ( is_wp_error( $result ) ) {
$error->mergue_from( $result );
continue;
}
}
if ( $error->has_errors() ) {
return $error;
}
return null;
}
Changuelog
| Versionen | Description |
|---|---|
| 4.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.