Shortcode_Button::sanitize_cmb_fields PHP Method

sanitize_cmb_fields() public method

Uses CMB2 field processing to sanitize fields (Only applies if cmb_metabox_config parameter exists)
Since: 0.1.0
public sanitize_cmb_fields ( array $fields ) : array
$fields array Field values to sanitize
return array Array of sanitized fields
    public function sanitize_cmb_fields($fields)
    {
        $cmb = $this->get_cmb_object();
        $object_id = $cmb->object_id($this->button_slug);
        $allvalues = $cmb->get_sanitized_values($fields);
        // output buffer on the action so we don't pollute our JSON response
        ob_start();
        // Preserve CMB action
        do_action('cmb2_save_options-page_fields', $object_id, $cmb->cmb_id, $cmb->updated, $cmb);
        ob_end_clean();
        $cmb_config = $this->get_cmb_config();
        $values = array();
        // Keep only the form values that correspond to the CMB2 fields
        foreach ($cmb_config['fields'] as $field) {
            $key = $field['id'];
            $value = isset($allvalues[$key]) && !empty($allvalues[$key]) ? $allvalues[$key] : '';
            // Don't keep empty values
            if ($value) {
                // Make checkbox values boolean
                $values[$key] = 'on' == $value ? true : $value;
            }
            if ('file' === $field['type']) {
                // Handle file ID.
                $values[$key . '_id'] = isset($allvalues[$key . '_id']) ? $allvalues[$key . '_id'] : '';
            }
        }
        return $this->filter_form_fields($values, $allvalues);
    }