When trying to use a sanitization callback for a value which I would like to store as boolean, I keep getting an error in the Customizer saying "Invalid input" for a checkbox (the error appears almost a minute after checking or unchecking the checkbox, and prevents me from "publishing" the changes).
Here is my sanitization function:
function sanitize_boolean($input){
if(!isset($input)){ return false; }
$retval = false;
if(is_string($input)){
if($input == 'true' || $input == '1' || $input == 'on' || $input == 'yes'){
$retval = true;
}
}elseif(is_numeric($input)){
if($input === 1){
$retval = true;
}
}elseif(is_bool($input)){
$retval = $input;
}
return $retval;
}
Am I doing something wrong in my sanitization function which causes the "Invalid input" error in the Customizer?
I'm storing values as option array. Say my option name is 'BVALS'. In my php, $BVALS is an array of attributes which are being stored as the option 'BVALS', which is synchronized with a Gutenberg block's attributes. Say one of the attributes is 'HASBOLDSTYLE'. When defining the attributes for the Gutenberg block I set the type to 'boolean', and when the value is saved and I inspect the database record I see that it is being stored correctly as boolean 1 or 0 (under 'option_name' 'BVALS' I find 'a:61:{' (so an array with 61 elements) and then I find somewhere within the serialized array s:12:'HASBOLDSTYLE';b:0;. If I change the attribute from false to true (let's say with a checkbox) in my Gutenberg block's InspectorControls and then update the option, I see the value is updated correctly s:12:'HASBOLDSTYLE';b:1;.
Now I'm trying to use a control in the Customizer to read or write that same value.
I have something like this:
$wp_customize->add_setting(
'BVALS[HASBOLDSTYLE]',
array(
'default' => $attributes['HASBOLDSTYLE']["default"], //use same default value as for Gutenberg block attributes
'type' => 'option',
'capability' => 'manage_options',
'transport' => 'postMessage',
'sanitize_callback' => 'sanitize_boolean'
)
);
$wp_customize->add_control(
'BVALS[HASBOLDSTYLE]_ctl',
array(
'label' => 'Bold style',
'settings' => 'BVALS[HASBOLDSTYLE]',
'priority' => 10,
'section' => 'custom_styles',
'type' => 'checkbox'
)
);
And of course the sanitization callback as defined at the top. Every time I try to check or uncheck the checkbox in the Customizer, I get 'Invalid value' above the checkbox after about a minute. I'm able to sanitize and set any other value types: integer, float, and string (using absint, floatval, and wp_filter_nohtml_kses respectively), however boolean values with checkboxes are giving me errors preventing me from saving the values to the option.
UPDATE 1 inspecting the network requests, I'm seeing that when I change an integer value, there is a request to admin-ajax.php with this response:
{"success":true,"data":{"setting_validities":{"BVALS[BORDERWIDTH]":true},"changeset_status":"auto-draft","autosaved":true}}
Whereas, when checking a checkbox I get this response:
{"success":true,"data":{"setting_validities":{"BVALS[HASBOLDSTYLE]":{"invalid_value":{"message":"Invalid value.","data":null}}},"changeset_status":"auto-draft","autosaved":true}}
UPDATE 2 I found the problem. Since I'm working from within a static class, I was calling the sanitize_boolean function (which is a public static function within the class) as 'self::sanitize_boolean'. Changing that to 'MyClassName::sanitize_boolean' seems to have done the trick. I'm guessing that the function is being called from an ajax call (perhaps like a heartbeat function or something?), and so we lose context and 'self' doesn't mean much anymore. It was only happening for booleans because it was the only function defined within the class, absint and floatval and wp_filter_nohtml_kses were available in any case.