-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMSetting.php
186 lines (169 loc) · 5.98 KB
/
KMSetting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Created by PhpStorm.
* User: kofi
* Date: 6/5/19
* Time: 12:41 PM
* @version 1.0.2
* @author kofi mokome
*/
if ( ! class_exists( 'KMSetting' ) ) {
#[AllowDynamicProperties]
class KMSetting {
private $menu_slug;
private $fields;
private $section_id;
private $sections;
/**
* @param string $menu_slug The menu slug of the menu or sub menu page
*
* @since 1.0.0
*/
public function __construct( $menu_slug ) {
$menu_slug = sanitize_text_field( $menu_slug );
$this->menu_slug = $menu_slug;
$this->fields = array();
$this->sections = array();
}
/**
* @since 1.0.0
*/
public function show_form() {
settings_errors(); ?>
<form method="post" action="options.php">
<?php
foreach ( $this->sections as $section ):
settings_fields( $section[0] );
do_settings_sections( $this->menu_slug );
endforeach;
submit_button();
?>
</form>
<?php
//echo $this->default_content;
}
/**
* @since 1.0.0
*/
public function save() {
add_action( 'admin_init', array( $this, 'add_settings' ) );
}
/**
* @since 1.0.0
*/
public function add_settings() {
foreach ( $this->sections as $section ) {
add_settings_section(
$section[0],
$section[1],
array( $this, 'default_section_callback' ),
$this->menu_slug );
}
foreach ( $this->fields as $field ) {
$id = sanitize_text_field( $field['id'] );
$label = sanitize_text_field( $field['label'] );
$section_id = sanitize_text_field( $field['section_id'] );
add_settings_field(
$id,
$label,
array( $this, 'default_field_callback' ),
$this->menu_slug,
$section_id,
$field
);
register_setting( $section_id, $id );
}
}
/**
* @since 1.0.0
*/
public function default_field_callback( array $data ) {
$tip = $data['tip'];
$id = sanitize_text_field( $data['id'] );
$input_class = sanitize_html_class( $data['input_class'] );
$placeholder = sanitize_text_field( $data['placeholder'] );
$disabled = sanitize_text_field( $data['disabled'] );
$autocomplete = sanitize_text_field( $data['autocomplete'] );
$min = sanitize_text_field( $data['min'] );
$max = sanitize_text_field( $data['max'] );
$read_only = sanitize_text_field( $data['read_only'] );
$value = get_option( $id );
$value = apply_filters( 'km_setting_' . $id, $value );
switch ( $data['type'] ) {
case 'text':
echo "<p><input type='text' name='" . esc_attr( $id ) . "' value='" . esc_html( $value ) . "' class='" . esc_attr( $input_class ) . "' placeholder='" . esc_attr( $placeholder ) . "'" . ( $read_only ? ' readonly' : '' ) . ( $disabled ? ' disabled' : '' ) . "></p>";
echo "<strong>" . wp_kses_post( $tip ) . "</strong>";
break;
case 'number':
echo "<p><input type='number' name='" . esc_attr( $id ) . "' value='" . esc_html( $value ) . "' min='" . esc_attr( $min ) . "' max='" . esc_attr( $max ) . "' class='" . esc_attr( $input_class ) . "' placeholder='" . esc_attr( $placeholder ) . "'" . ( $read_only ? ' readonly' : '' ) . ( $disabled ? ' disabled' : '' ) . "></p>";
echo "<strong>" . wp_kses_post( $tip ) . "</strong>";
break;
case 'textarea':
echo "<p><textarea name='" . esc_attr( $id ) . "' id='" . esc_attr( $id ) . "' cols='80'
rows='8'
placeholder='" . esc_attr( $placeholder ) . "' class='" . esc_attr( $input_class ) . "' autocomplete='" . esc_attr( $autocomplete ) . "'" . ( $read_only ? 'readonly' : '' ) . ( $disabled ? ' disabled' : '' ) . ">" . esc_html( $value ) . "</textarea></p>";
echo "<strong>" . wp_kses_post( $tip ) . "</strong>";
break;
case 'checkbox':
$state = get_option( $id ) == 'on' ? 'checked' : '';
echo "<p><input type='checkbox' name='" . esc_attr( $id ) . "' id='" . esc_attr( $id ) . "' " . $state . " class='" . esc_attr( $input_class ) . "'" . ( $read_only ? 'onclick="return false;"' : '' ) . ( $disabled ? ' disabled' : '' ) . "></p>";
echo "<strong>" . wp_kses_post( $tip ) . "</strong>";
break;
case 'select':
$selected_value = get_option( $id );
echo "<p><select type='text' name='" . esc_attr( $id ) . "' id='" . esc_attr( $id ) . "' class='" . esc_attr( $input_class ) . "'" . ( $read_only ? 'readonly' : '' ) . ( $disabled ? ' disabled' : '' ) . ">";
foreach ( $data['options'] as $key => $value ):?>
<option value='<?php echo esc_attr( $key ) ?>' <?php echo ( $key == $selected_value ) ? 'selected' : '' ?> ><?php echo esc_html( $value ) ?></option>
<?php
endforeach;
echo "</select></p>";
echo "<strong>" . wp_kses_post( $tip ) . "</strong>";
break;
default:
echo "<< <span style='color: red;'>Please enter a valid field type</span> >>";
break;
}
}
/**
* @param array $data Contains parameters of the field
*
* @since 1.0.0
*/
public function add_field( $data ) {
$default_data = array(
'type' => '',
'id' => '',
'label' => '',
'tip' => '',
'min' => '',
'max' => '',
'disabled' => false,
'read_only' => false,
'input_class' => '', // class for input element
'class' => '', // class for parent element
'options' => array( 'Select a value' => '' ),
'default_option' => '',
'autocomplete' => 'on',
'placeholder' => ''
);
$data = array_merge( $default_data, $data );
// todo: compare two arrays
$data['section_id'] = $this->section_id;
array_push( $this->fields, $data );
}
/**
* @since 1.0.0
*/
public function add_section( $id, $title = '' ) {
$title = sanitize_text_field( $title );
$id = sanitize_text_field( $id );
array_push( $this->sections, array( $id, $title ) );
$this->section_id = $id;
}
/**
* @since 1.0.0
*/
public function default_section_callback() {
}
}
}