Opened 5 years ago
Closed 5 years ago
#53099 closed defect (bug) (fixed)
PHP warnings due to wp_check_term_meta_support_prefilter callback
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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.
In 50793: