From 8cd601c8ba6a6a16949ae0cb626921f3169ce7b8 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 27 Apr 2021 18:36:04 +0000 Subject: [PATCH] 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 --- .../rest-api/fields/class-wp-rest-meta-fields.php | 14 +++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php b/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php index 817f5c106a..f186648545 100644 --- a/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php +++ b/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php @@ -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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index f0233bc13b..4ec6f8d22b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.