setting = $setting;
if(isset( $this->setting['default'] )){
$this->saved_value = get_option($this->setting['field'], $this->setting['default']);
}else{
$this->saved_value = get_option($this->setting['field']);
}
if(isset( $this->setting['pro'] )){
if($this->setting['pro']){
$this->pro = ' free-version ';
//$this->setting['desc'] = 'Workes in Pro version only / Without PRO version this setting will have no effect ';
}else{
$this->pro = ' paid-version ';
}
}else{
$this->pro = "";
}
$allowed_atts = array_fill_keys( array(
'align', 'class', 'selected', 'multiple', 'checked', 'type', 'id', 'dir', 'lang',
'style', 'xml:lang', 'src', 'alt', 'href', 'rel', 'rev', 'target', 'novalidate',
'value', 'name', 'tabindex', 'action', 'method', 'for', 'width', 'height', 'data',
'title', 'min', 'max', 'step', 'required', 'readonly'
), array() );
$tags = ['form','br','label','input','select','option','textarea','iframe','script','style',
'strong','small','table','span','abbr','code','pre','div','img','h1','h2','h3','h4','h5','h6','ol','ul','li','em','hr','tr','td','p','a','b','i'];
$this->allowed_tags = array_fill_keys( $tags, $allowed_atts );
$this->check_field_type();
}
function check_field_type(){
if ( ! isset( $this->setting['type'] ) ) return;
$method_map = array(
'select' => 'select_box',
'radio' => 'radio_group',
'number' => 'number_box',
'text' => 'text_box',
'text_html' => 'text_box',
'textarea' => 'textarea_box',
'multiselect' => 'multiselect_box',
'color' => 'color_box',
'hidden' => 'hidden_box',
'switch' => 'switch_display',
'switch_category' => 'switch_category_display',
'setting_category'=> 'setting_category',
'image' => 'image',
'group' => 'field_group',
);
$method = isset( $method_map[ $this->setting['type'] ] ) ? $method_map[ $this->setting['type'] ] : null;
if ( $method ) {
$this->$method();
} else {
$this->custom_field();
}
}
/**
*
* array(
'field' => 'shipping_group_1', // still required, even though it's not a real option — used for id/hooks
'type' => 'group',
'label' => 'Shipping Options',
'desc' => 'Configure how rates are calculated.',
'fields' => array(
array('field' => 'shipping_flat_rate', 'type' => 'number', 'label' => 'Flat rate', ...),
array('field' => 'shipping_free_over', 'type' => 'number', 'label' => 'Free over', ...),
),
),
*/
function field_group(){
$prefix = $this->get_hook_prefix();
$collapsible = !empty($this->setting['collapsible']);
$is_open = !isset($this->setting['open']) || $this->setting['open']; // defaults to open
$group_desc = isset($this->setting['desc'])
? '
'.wp_kses($this->setting['desc'], $this->allowed_tags).'
'
: '';
$group_links = $this->get_link_html();
// Prevent link clicks from toggling when inside
if(!empty($group_links) && !empty($collapsible)){
$group_links = str_replace('setting['label'])
? ''.wp_kses_post($this->setting['label']).' '
: '';
$group_open = ''
. ''.$group_label
.$group_desc.$group_links_html.' '
. '';
$group_close = '
';
}else{
$group_label = isset($this->setting['label'])
? ''.wp_kses_post($this->setting['label']).' '
: '';
$group_open = '';
}
$group_open = apply_filters("{$prefix}_formmaker_group_html_open", $group_open, $this->setting, $this);
$group_open = apply_filters("{$prefix}_formmaker_group_html_open_field_{$this->setting['field']}", $group_open, $this->setting, $this);
$group_close = apply_filters("{$prefix}_formmaker_group_html_close", $group_close, $this->setting, $this);
$group_close = apply_filters("{$prefix}_formmaker_group_html_close_field_{$this->setting['field']}", $group_close, $this->setting, $this);
$fields_html = '';
if(!empty($this->setting['fields']) && is_array($this->setting['fields'])){
ob_start();
foreach($this->setting['fields'] as $sub_setting){
new self($sub_setting);
}
$fields_html = ob_get_clean();
}
echo $group_open . $fields_html . $group_close;
}
function get_label_html(){
if($this->setting['type'] == 'setting_category') return ''.wp_kses_post($this->setting['label']).' ';
return ''.wp_kses_post($this->setting['label']).' ';
}
function get_desc_html(){
return (isset($this->setting['desc'])) && !empty($this->setting['desc']) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).'
' : "";
}
function get_link_html(){
/*
'links'=>array(array('name'=>"Video", 'url'=>"https://www.youtube.com/watch?v=KNC5lkoE2Fs", 'type'=>'iframe'))
'links'=>array(array('name'=>"Video", 'url'=>"image url", 'type'=>'image'))
*/
if(!isset($this->setting['links']) || !is_array($this->setting['links']) || empty($this->setting['links'])) return;
$html = '';
$links = $this->setting['links'];
foreach($links as $link){
$class = 'pi-'.($link['type'] ?? 'link');
$html .= ' '.esc_html($link['name']).' ';
}
return !empty($html) ? ''.$html.'
' : '';
}
function get_title_col_width(){
$prefix = $this->get_hook_prefix();
$width_array = apply_filters("{$prefix}_formmaker_title_col_width_array", array('switch_category' => 9, 'setting_category' => 12));
$width = isset($width_array[$this->setting['type']]) ? $width_array[$this->setting['type']] : 6;
// portfolio-wide override, applies to all types unless something more specific overrides it
$width = apply_filters("{$prefix}_formmaker_title_col_width", $width, $this->setting['type']);
// type-specific override, e.g. 'pisol_form_maker_title_col_width_switch_category'
$width = apply_filters("{$prefix}_formmaker_title_col_width_{$this->setting['type']}", $width, $this->setting['type']);
// field-level override — set directly in the setting array, wins over any filter
if (isset($this->setting['col_width'])) {
$width = $this->setting['col_width'];
}
return (int) $width;
}
function bootstrap($field_html){
$label_html = $this->get_label_html();
$desc_html = $this->get_desc_html();
$links_html = $this->get_link_html();
$title_col = $this->get_title_col_width();
$setting_col = 12 - $title_col;
if($setting_col < 1) {
$setting_col = 12; // Ensure at least 1 column for the setting
}
$prefix = $this->get_hook_prefix();
// hook 1: let something modify the field's own HTML before it's placed in the row
$field_html = apply_filters("{$prefix}_formmaker_field_html", $field_html, $this->setting['type'], $this->setting, $this);
$field_html = apply_filters("{$prefix}_formmaker_field_html_{$this->setting['type']}", $field_html, $this->setting, $this);
$field_html = apply_filters("{$prefix}_formmaker_field_html_field_{$this->setting['field']}", $field_html, $this->setting, $this);
ob_start();
if($this->setting['type'] != 'hidden'){
?>
setting['type'], $this->setting, $this);
$row_html = apply_filters("{$prefix}_formmaker_row_html_{$this->setting['type']}", $row_html, $this->setting, $this);
$row_html = apply_filters("{$prefix}_formmaker_row_html_field_{$this->setting['field']}", $row_html, $this->setting, $this);
echo $row_html;
}
function get_hook_prefix(){
// Relies on each plugin's copy having a renamed class/namespace,
// per the copy-and-rename convention used for this shared file.
return strtolower( str_replace('\\', '_', get_class($this)) );
}
/*
Field type: select box
*/
function select_box(){
$field = 'setting['multiple']) ? ' multiple="'.esc_attr($this->setting['multiple']).'"': '')
.'>';
foreach($this->setting['value'] as $key => $val){
$field .= 'saved_value == $key) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).' ';
}
$field .= ' ';
$this->bootstrap($field);
}
function radio_group(){
$field = '';
foreach($this->setting['value'] as $key => $val){
$is_last = ($key === array_key_last($this->setting['value']));
$mb_class = $is_last ? '' : ' mb-3';
$field .= '
saved_value, $key, false).'>
'.esc_html($val).'
';
}
$field .= '
';
$this->bootstrap($field);
}
/*
Field type: select box
*/
function multiselect_box(){
$field = '';
foreach($this->setting['value'] as $key => $val){
if(isset($this->saved_value) && $this->saved_value != false){
$field .='saved_value) ) ? " selected=\"selected\" " : "" ).'>'.esc_html($val).' ';
}else{
$field .= ''.esc_html($val).' ';
}
}
$field .= ' ';
$this->bootstrap($field);
}
/*
Field type: Number box
*/
function number_box(){
$field = ' setting['min']) ? ' min="'.esc_attr($this->setting['min']).'"': '')
.(isset($this->setting['max']) ? ' max="'.esc_attr($this->setting['max']).'"': '')
.(isset($this->setting['step']) ? ' step="'.esc_attr($this->setting['step']).'"': '')
.(isset($this->setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
.(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
.'>';
$this->bootstrap($field);
}
/*
Field type: Number box
*/
function text_box(){
$field = ' setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
.(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
.'>';
$this->bootstrap($field);
}
/*
Textarea field
*/
function textarea_box(){
$field = '';
$this->bootstrap($field);
}
/*
Field type: color
*/
function color_box(){
$field = ' setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
.(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
.'>';
$this->bootstrap($field);
}
function hidden_box(){
$field =' setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
.(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
.'>';
$this->bootstrap($field);
}
/*
Field type: switch
*/
function switch_display(){
$field = '
saved_value == true) ? "checked='checked'": "").' >
';
$this->bootstrap($field);
}
function switch_category_display(){
$field = '
saved_value) ? "checked='checked'": "").' >
';
$this->bootstrap($field);
}
/**
* Category: is to devide setting in different part
*/
function setting_category(){
$this->bootstrap('');
}
function image(){
wp_enqueue_media();
add_action( 'admin_footer', array($this,'media_selector_scripts') );
$field = '
';
$this->bootstrap($field);
}
function media_selector_scripts(){
$my_saved_attachment_post_id = get_option($this->setting['field'], 0 );
?>
setting['type'], $this->setting, $this->saved_value);
}
/**
* if a field don't want to do any sanitization then they will set
* 'validation' => false
* and if they want to add there custom sanitization function then they will do
* 'sanitize_callback' => 'function_name' OR
* 'sanitize_callback' => array('class_name', 'function_name')
* if they want to use different sanitization function that is other then the one defined for them then they will use
* 'sanitize_callback' => 'sanitize_text_field' => directly add the sanitization function name
*/
static function register_setting($group, $setting){
if(isset($setting['type']) && $setting['type'] === 'group'){
if(!empty($setting['fields']) && is_array($setting['fields'])){
foreach($setting['fields'] as $sub_setting){
self::register_setting($group, $sub_setting);
}
}
return;
}
$validation_function = self::getValidationFunction($setting);
if($validation_function !== false){
if(!is_array($validation_function) && method_exists(__CLASS__, $validation_function)){
register_setting($group, $setting['field'], [
'sanitize_callback' => [__CLASS__, $validation_function]
]);
return;
}else{
if(is_array($validation_function) && count($validation_function) == 2 && method_exists($validation_function[0], $validation_function[1])){
register_setting($group, $setting['field'], [
'sanitize_callback' => $validation_function
]);
return;
}elseif(!is_array($validation_function) && function_exists($validation_function)){
register_setting($group, $setting['field'], [
'sanitize_callback' => $validation_function
]);
return;
}
}
}
register_setting($group, $setting['field']);
}
static function getValidationFunction($setting){
if(isset($setting['validation']) && $setting['validation'] === false) return false;
if(isset($setting['sanitize_callback'])){
return $setting['sanitize_callback'];
}
$sanitize_text_allow_basic_html_field_types = ['text_html'];
$sanitize_text_field_types = ['select', 'text', 'multiselect', 'color', 'hidden', 'switch', 'switch_category', 'radio'];
$sanitize_textarea_field_types = ['textarea'];
$sanitize_number_field_types = ['number'];
if(isset($setting['type']) && in_array($setting['type'], $sanitize_text_field_types)){
return 'sanitize_text_field';
}
if(isset($setting['type']) && in_array($setting['type'], $sanitize_textarea_field_types)){
return 'sanitize_textarea_field';
}
if(isset($setting['type']) && in_array($setting['type'], $sanitize_number_field_types)){
return 'sanitize_numeric_values';
}
if(isset($setting['type']) && in_array($setting['type'], $sanitize_text_allow_basic_html_field_types)){
return 'sanitize_text_allow_basic_html';
}
return false;
}
static function sanitize_text_field($input) {
$sanitized_input = is_array($input) ? array_map([__CLASS__,'sanitize_text_field'], $input) : sanitize_text_field($input);
return $sanitized_input;
}
static function sanitize_textarea_field($input) {
$sanitized_input = sanitize_textarea_field($input);
return $sanitized_input;
}
static function sanitize_text_allow_basic_html($input) {
$allowed_tags = array(
'span' => array(),
'strong' => array(),
'b' => array(),
'i' => array(),
'br' => array(),
);
$sanitized_input = wp_kses($input, $allowed_tags);
return $sanitized_input;
}
// Sanitize numeric input (supports both integer and float)
static function sanitize_numeric_values($input) {
if (is_numeric($input)) {
if (ctype_digit((string) $input)) {
$sanitized_input = intval($input);
} else {
$sanitized_input = floatval($input);
}
} else {
$sanitized_input = 0; // You can change this to any default value
}
return $sanitized_input;
}
}