REST API: Check the results of get_metadata() in WP_REST_Meta_Fields methods.

This avoids PHP warnings in case the function returns boolean `false` instead of an array.

Props david.binda.
Fixes #53099.
Built from https://develop.svn.wordpress.org/trunk@50793


git-svn-id: http://core.svn.wordpress.org/trunk@50402 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-04-27 18:36:04 +00:00
parent cbfa060298
commit 8cd601c8ba
2 changed files with 12 additions and 4 deletions

View File

@ -94,8 +94,10 @@ abstract class WP_REST_Meta_Fields {
} else {
$value = array();
foreach ( $all_values as $row ) {
$value[] = $this->prepare_value_for_response( $row, $request, $args );
if ( is_array( $all_values ) ) {
foreach ( $all_values as $row ) {
$value[] = $this->prepare_value_for_response( $row, $request, $args );
}
}
}
@ -281,6 +283,10 @@ abstract class WP_REST_Meta_Fields {
$current_values = get_metadata( $meta_type, $object_id, $meta_key, false );
$subtype = get_object_subtype( $meta_type, $object_id );
if ( ! is_array( $current_values ) ) {
$current_values = array();
}
$to_remove = $current_values;
$to_add = $values;
@ -377,7 +383,9 @@ abstract class WP_REST_Meta_Fields {
$old_value = get_metadata( $meta_type, $object_id, $meta_key );
$subtype = get_object_subtype( $meta_type, $object_id );
if ( 1 === count( $old_value ) && $this->is_meta_value_same_as_stored_value( $meta_key, $subtype, $old_value[0], $value ) ) {
if ( is_array( $old_value ) && 1 === count( $old_value )
&& $this->is_meta_value_same_as_stored_value( $meta_key, $subtype, $old_value[0], $value )
) {
return true;
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.8-alpha-50792';
$wp_version = '5.8-alpha-50793';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.