Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#53099 closed defect (bug) (fixed)

PHP warnings due to wp_check_term_meta_support_prefilter callback

Reported by: davidbinda's profile david.binda Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.8 Priority: normal
Severity: normal Version: 5.5
Component: REST API Keywords:
Focuses: Cc:

Description

Following PHP warnings might be triggered in case the callback to get_term_metadata filter (inside the get_metadata_raw function) returns false. For instance, as the default wp_check_term_meta_support_prefilter callback does:

Warning: Invalid argument supplied for foreach() in wp-includes/rest-api/fields/class-wp-rest-meta-fields.php on line 97

Warning: array_filter() expects parameter 1 to be array, bool given in wp-includes/rest-api/fields/class-wp-rest-meta-fields.php on line 293

Warning: array_keys() expects parameter 1 to be array, null given in wp-includes/rest-api/fields/class-wp-rest-meta-fields.php on line 293

Warning: array_map(): Expected parameter 2 to be an array, bool given in wp-includes/rest-api/fields/class-wp-rest-meta-fields.php on line 317

Warning: array_unique() expects parameter 1 to be array, null given in wp-includes/rest-api/fields/class-wp-rest-meta-fields.php on line 317

Warning: Invalid argument supplied for foreach() in wp-includes/rest-api/fields/class-wp-rest-meta-fields.php on line 319.

While the default callback returns bool (false) only on installations with old DB schema, pre term meta feature (and thus does not really produce the warnings in default state), the callback with it's false might serve as an example on how to short-circuit the get_metadata_raw function.

Further, the get_metadata_raw function, or get_metadata which is calling that one, may also return false for other issues.

That said, the boolean value returned by get_metadata function call is something what should, IMHO, be taken into consideration whenever working with the get_metadata function.

However the methods in WP_REST_Meta_Fields class do not check the return value, and expect an array to be returned.

The code, IMHO, should check for the value being an array before using it as an array in related cases.

Attachments (1)

53099.diff (3.2 KB) - added by david.binda 3 years ago.

Download all attachments as: .zip

Change History (3)

@david.binda
3 years ago

#1 @SergeyBiryukov
3 years ago

  • Milestone changed from Awaiting Review to 5.8

#2 @SergeyBiryukov
3 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 50793:

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.

Note: See TracTickets for help on using tickets.