wpmetabox / meta-box

The best plugin for WordPress custom fields and custom meta boxes
https://metabox.io
1.18k stars 423 forks source link

Error message: Uncaught TypeError: Return value of RWMB_Sanitizer::sanitize_color() must be of the type string #1572

Closed vestaxpdx closed 3 months ago

vestaxpdx commented 3 months ago

Issue Overview

As per title we keep getting fatal errors with the following: Error message: Uncaught TypeError: Return value of RWMB_Sanitizer::sanitize_color() must be of the type string

It is due to the strict typing on line 114 of /meta-box/inc/sanitizer.php

private function sanitize_color( $value ) : string

Seems somehow it can return null, so the only way I can fix our site is by removing the strict typing or allowing null returns ?string

Steps to Reproduce (for bugs)

Pressing save on any post type.

Possible Solution

Allow null returns

private function sanitize_color( $value ): ?string

Whilst I appreciate there may be a deeper root issue here. I hope we can allow this so I don't need to keep hot fixing the plugin. This is happening intermittently across many sites I run so it doesn't seem to be related to one site for me.

Plugin version: 5.9.5 WP Version: 6.4.3

rilwis commented 3 months ago

Thanks for your feedback. While a hotfix for removing strict types is easy, we'll need to ensure why that happens. It can help preventing other bugs when working with other extensions (like saving data in custom tables).

Can you please share the way you set up the field and the value of the field when the error occurs?

vestaxpdx commented 3 months ago

The issue is I am nursing an out of date theme 'Atelier' so I was looking for the quick fix :) I will try and find the time to investigate how it is returning null intermittently and report back.

rilwis commented 3 months ago

Hi @vestaxpdx ,

Can you please try the fix in #1573 ? I think the problem is the sanitize_hex_color can return void if the value is not a valid hex format.

vestaxpdx commented 3 months ago

Hi @rilwis

This looks great, thanks! I have tested and so far no fatal errors.