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(
'align' => array(),
'class' => array(),
'selected' => array(),
'multiple' => array(),
'checked' => array(),
'type' => array(),
'id' => array(),
'dir' => array(),
'lang' => array(),
'style' => array(),
'xml:lang' => array(),
'src' => array(),
'alt' => array(),
'href' => array(),
'rel' => array(),
'rev' => array(),
'target' => array(),
'novalidate' => array(),
'type' => array(),
'value' => array(),
'name' => array(),
'tabindex' => array(),
'action' => array(),
'method' => array(),
'for' => array(),
'width' => array(),
'height' => array(),
'data' => array(),
'title' => array(),
'min' => array(),
'max' => array(),
'step' => array(),
'required' => array(),
'readonly' => array(),
);
$this->allowed_tags['form'] = $allowed_atts;
$this->allowed_tags['br'] = $allowed_atts;
$this->allowed_tags['label'] = $allowed_atts;
$this->allowed_tags['input'] = $allowed_atts;
$this->allowed_tags['select'] = $allowed_atts;
$this->allowed_tags['option'] = $allowed_atts;
$this->allowed_tags['textarea'] = $allowed_atts;
$this->allowed_tags['iframe'] = $allowed_atts;
$this->allowed_tags['script'] = $allowed_atts;
$this->allowed_tags['style'] = $allowed_atts;
$this->allowed_tags['strong'] = $allowed_atts;
$this->allowed_tags['small'] = $allowed_atts;
$this->allowed_tags['table'] = $allowed_atts;
$this->allowed_tags['span'] = $allowed_atts;
$this->allowed_tags['abbr'] = $allowed_atts;
$this->allowed_tags['code'] = $allowed_atts;
$this->allowed_tags['pre'] = $allowed_atts;
$this->allowed_tags['div'] = $allowed_atts;
$this->allowed_tags['img'] = $allowed_atts;
$this->allowed_tags['h1'] = $allowed_atts;
$this->allowed_tags['h2'] = $allowed_atts;
$this->allowed_tags['h3'] = $allowed_atts;
$this->allowed_tags['h4'] = $allowed_atts;
$this->allowed_tags['h5'] = $allowed_atts;
$this->allowed_tags['h6'] = $allowed_atts;
$this->allowed_tags['ol'] = $allowed_atts;
$this->allowed_tags['ul'] = $allowed_atts;
$this->allowed_tags['li'] = $allowed_atts;
$this->allowed_tags['em'] = $allowed_atts;
$this->allowed_tags['hr'] = $allowed_atts;
$this->allowed_tags['br'] = $allowed_atts;
$this->allowed_tags['tr'] = $allowed_atts;
$this->allowed_tags['td'] = $allowed_atts;
$this->allowed_tags['p'] = $allowed_atts;
$this->allowed_tags['a'] = $allowed_atts;
$this->allowed_tags['b'] = $allowed_atts;
$this->allowed_tags['i'] = $allowed_atts;
$this->check_field_type();
}
function check_field_type(){
if(isset($this->setting['type'])):
switch ($this->setting['type']){
case 'select':
$this->select_box();
break;
case 'number':
$this->number_box();
break;
case 'text':
$this->text_box();
break;
case 'text_html':
$this->text_box();
break;
case 'textarea':
$this->textarea_box();
break;
case 'multiselect':
$this->multiselect_box();
break;
case 'color':
$this->color_box();
break;
case 'hidden':
$this->hidden_box();
break;
case 'switch':
$this->switch_display();
break;
case 'switch_category':
$this->switch_category_display();
break;
case 'setting_category':
$this->setting_category();
break;
case 'image':
$this->image();
break;
default:
$this->custom_field();
break;
}
endif;
}
function bootstrap($label, $field, $desc = "", $links = "", $title_col = 5){
$setting_col = 12 - $title_col;
if($this->setting['type'] != 'hidden'){
?>
allowed_tags); ?>
': "", $this->allowed_tags); ?>
allowed_tags); ?>
allowed_tags); ?>
setting['type'] != 'hidden'){
?>
allowed_tags); ?>
': "", $this->allowed_tags); ?>
allowed_tags); ?>
allowed_tags, ['https', 'http', 'javascript']); ?>
">
allowed_tags) ; ?>
': "", $this->allowed_tags); ?>
allowed_tags); ?>
allowed_tags); ?>
setting['field']).'">'.wp_kses_post($this->setting['label']).'';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$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 .= ' ';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links, 7);
}
function generateLinks($setting){
/*
'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($setting['links']) || !is_array($setting['links']) || empty($setting['links'])) return;
$html = '';
$links = $setting['links'];
foreach($links as $link){
$class = 'pi-'.$link['type'];
$html .= ''.esc_html($link['name']).' ';
}
return $html;
}
/*
Field type: select box
*/
function multiselect_box(){
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = ((isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "");
$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 .= ' ';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links);
}
/*
Field type: Number box
*/
function number_box(){
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$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']).'"': '')
.'>';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links, 7);
}
/*
Field type: Number box
*/
function text_box(){
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$field = ' setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
.(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
.'>';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links, 7);
}
/*
Textarea field
*/
function textarea_box(){
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$field = '';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links, 12);
}
/*
Field type: color
*/
function color_box(){
wp_enqueue_style( 'wp-color-picker');
wp_enqueue_script( 'wp-color-picker');
wp_add_inline_script('wp-color-picker','
jQuery(document).ready(function($) {
$(".color-picker").wpColorPicker();
});
');
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$field = ' setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
.(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
.'>';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links, 7);
}
function hidden_box(){
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$field =' setting['required']) ? ' required="'.esc_attr($this->setting['required']).'"': '')
.(isset($this->setting['readonly']) ? ' readonly="'.esc_attr($this->setting['readonly']).'"': '')
.'>';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links);
}
/*
Field type: switch
*/
function switch_display(){
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$field = '
saved_value == true) ? "checked='checked'": "").' >
';
$links = $this->generateLinks($this->setting);
$this->bootstrap($label, $field, $desc, $links, 9);
}
function switch_category_display(){
$label = ''.wp_kses_post($this->setting['label']).' ';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$field = '
saved_value) ? "checked='checked'": "").' >
';
$links = $this->generateLinks($this->setting);
$this->bootstrap_switch_category($label, $field, $desc, $links);
}
/**
* Category: is to devide setting in different part
*/
function setting_category(){
if(isset($this->setting['label']) && $this->setting['label'] != ""):
?>
">
">setting['label']); ?>
setting['field']).'">'.wp_kses_post($this->setting['label']).'';
$desc = (isset($this->setting['desc'])) ? ''.wp_kses($this->setting['desc'], $this->allowed_tags).' ' : "";
$field = '
';
$links = $this->generateLinks($this->setting);
$this->bootstrap_image($label, $field, $desc, $links);
}
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){
$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'], [__CLASS__, 'sanitize_any']);
}
static function sanitize_any($input){
return self::sanitize_text_field($input);
}
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'];
$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($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;
}
}