Make WordPress Core


Ignore:
Timestamp:
04/27/2021 06:34:52 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r50567 r50793  
    9595                $value = array();
    9696
    97                 foreach ( $all_values as $row ) {
    98                     $value[] = $this->prepare_value_for_response( $row, $request, $args );
     97                if ( is_array( $all_values ) ) {
     98                    foreach ( $all_values as $row ) {
     99                        $value[] = $this->prepare_value_for_response( $row, $request, $args );
     100                    }
    99101                }
    100102            }
     
    281283        $current_values = get_metadata( $meta_type, $object_id, $meta_key, false );
    282284        $subtype        = get_object_subtype( $meta_type, $object_id );
     285
     286        if ( ! is_array( $current_values ) ) {
     287            $current_values = array();
     288        }
    283289
    284290        $to_remove = $current_values;
     
    378384        $subtype   = get_object_subtype( $meta_type, $object_id );
    379385
    380         if ( 1 === count( $old_value ) && $this->is_meta_value_same_as_stored_value( $meta_key, $subtype, $old_value[0], $value ) ) {
     386        if ( is_array( $old_value ) && 1 === count( $old_value )
     387            && $this->is_meta_value_same_as_stored_value( $meta_key, $subtype, $old_value[0], $value )
     388        ) {
    381389            return true;
    382390        }
Note: See TracChangeset for help on using the changeset viewer.