get_key_for_field($field); $value = get_option($key, null); // Unsanitize before returning return $this->unsanitize_fields($value); } /** * Save the field value(s) directly to the options table. * * @param Field $field The field to save. */ public function save(Field $field) { if (!empty($field->get_hierarchy())) { return; } $key = $this->get_key_for_field($field); $value = $field->get_full_value(); // If it's a Complex Field, convert to array if (is_a($field, '\\Carbon_Fields\\Field\\Complex_Field')) { $value = $field->get_value_tree(); } // Check and enforce default values $value = $this->check_default_values($value); // Always sanitize $value = $this->sanitize_fields($value); // Now store sanitized data $this->save_key_value_pair_with_autoload($key, $value, $field->get_autoload()); } /** * Delete the field value(s). * * @param Field $field The field to delete. */ public function delete(Field $field) { // Skip nested fields if (!empty($field->get_hierarchy())) { return; } $key = $this->get_key_for_field($field); delete_option($key); } /** * Build the option key for a given field. * * @param Field $field * @return string */ protected function get_key_for_field(Field $field) { return '_' . $field->get_base_name(); } protected function check_default_values($value, $key = null) { // If the value is an array, recurse through each key if (is_array($value)) { foreach ($value as $subKey => &$subValue) { // Recursively check values in nested arrays, passing the key $subValue = $this->check_default_values($subValue, $subKey); } return $value; } // If the key is 'overlay_delay' and the value is not numeric, default it to 0 if ($key === 'overlay_delay' && !is_numeric($value)) { return 0; // Default to 0 } return $value; } /** * Save a single key-value pair to the database with autoload. * * @param string $key * @param mixed $value * @param bool $autoload */ protected function save_key_value_pair_with_autoload($key, $value, $autoload) { $autoload = $autoload ? 'yes' : 'no'; if (!add_option($key, $value, '', $autoload)) { update_option($key, $value, $autoload); } } /** * Sanitize fields recursively (for storing). * * @param mixed $field * @return mixed */ public function sanitize_fields($field) { global $wpdb; // Optional charset testing $testCharset = isset($_GET['test']) ? $_GET['test'] : null; $charset = $testCharset ?: $wpdb->get_col_charset($wpdb->options, 'option_value'); if ($charset === 'utf8mb4') { // No sanitization needed for utf8mb4 return $field; } if (is_string($field)) { if (PHP_VERSION_ID < 80200 && extension_loaded('mbstring')) { // Use mb_convert_encoding for PHP < 8.2 if mbstring is available return mb_convert_encoding($field, 'HTML-ENTITIES', 'UTF-8'); } // Fallback: remove potential emoji & other 4-byte chars return preg_replace( '/[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{1F700}-\x{1F77F}\x{2600}-\x{26FF}\x{2700}-\x{27BF}\x{1F900}-\x{1F9FF}\x{1FA70}-\x{1FAFF}]/u', '', $field ); } if (is_array($field)) { foreach ($field as &$value) { $value = $this->sanitize_fields($value); } } return $field; } /** * Unsanitize fields recursively (for loading). * * @param mixed $field * @return mixed */ public function unsanitize_fields($field) { global $wpdb; $charset = $wpdb->get_col_charset($wpdb->options, 'option_value'); if (is_string($field)) { if ($charset === 'utf8mb4') { // No unsanitization needed for utf8mb4 return $field; } // Convert HTML entities back to original chars return html_entity_decode($field, ENT_QUOTES | ENT_HTML5, 'UTF-8'); } if (is_array($field)) { foreach ($field as &$value) { $value = $this->unsanitize_fields($value); } } return $field; } }