├── .gitignore
├── composer.json
├── example
├── oop-example.php
└── procedural-example.php
├── plugin.php
├── readme.md
├── readme.txt
├── screenshot-1.png
└── src
└── class.settings-api.php
/.gitignore:
--------------------------------------------------------------------------------
1 | nbproject/
2 | deploy.sh
3 | version.txt
4 | /~/
5 | /.svnignore
6 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "tareq1988/wordpress-settings-api-class",
3 | "type": "library",
4 | "description": "WordPress settings API Abstraction Class",
5 | "keywords": ["wp","wordpress", "settings-api"],
6 | "homepage": "https://github.com/tareq1988/wordpress-settings-api-class",
7 | "license": "GPLv2",
8 | "authors": [
9 | {
10 | "name": "Tareq Hasan",
11 | "email": "tareq@wedevs.com",
12 | "homepage": "http://tareq.wedevs.com",
13 | "role": "Developer"
14 | }
15 | ],
16 | "require": {
17 | "php": ">=5.2.4"
18 | },
19 | "autoload": {
20 | "classmap": ["src/"]
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/example/oop-example.php:
--------------------------------------------------------------------------------
1 | settings_api = new WeDevs_Settings_API;
15 |
16 | add_action( 'admin_init', array($this, 'admin_init') );
17 | add_action( 'admin_menu', array($this, 'admin_menu') );
18 | }
19 |
20 | function admin_init() {
21 |
22 | //set the settings
23 | $this->settings_api->set_sections( $this->get_settings_sections() );
24 | $this->settings_api->set_fields( $this->get_settings_fields() );
25 |
26 | //initialize settings
27 | $this->settings_api->admin_init();
28 | }
29 |
30 | function admin_menu() {
31 | add_options_page( 'Settings API', 'Settings API', 'delete_posts', 'settings_api_test', array($this, 'plugin_page') );
32 | }
33 |
34 | function get_settings_sections() {
35 | $sections = array(
36 | array(
37 | 'id' => 'wedevs_basics',
38 | 'title' => __( 'Basic Settings', 'wedevs' )
39 | ),
40 | array(
41 | 'id' => 'wedevs_advanced',
42 | 'title' => __( 'Advanced Settings', 'wedevs' )
43 | )
44 | );
45 | return $sections;
46 | }
47 |
48 | /**
49 | * Returns all the settings fields
50 | *
51 | * @return array settings fields
52 | */
53 | function get_settings_fields() {
54 | $settings_fields = array(
55 | 'wedevs_basics' => array(
56 | array(
57 | 'name' => 'text_val',
58 | 'label' => __( 'Text Input', 'wedevs' ),
59 | 'desc' => __( 'Text input description', 'wedevs' ),
60 | 'placeholder' => __( 'Text Input placeholder', 'wedevs' ),
61 | 'type' => 'text',
62 | 'default' => 'Title',
63 | 'sanitize_callback' => 'sanitize_text_field'
64 | ),
65 | array(
66 | 'name' => 'number_input',
67 | 'label' => __( 'Number Input', 'wedevs' ),
68 | 'desc' => __( 'Number field with validation callback `floatval`', 'wedevs' ),
69 | 'placeholder' => __( '1.99', 'wedevs' ),
70 | 'min' => 0,
71 | 'max' => 100,
72 | 'step' => '0.01',
73 | 'type' => 'number',
74 | 'default' => 'Title',
75 | 'sanitize_callback' => 'floatval'
76 | ),
77 | array(
78 | 'name' => 'textarea',
79 | 'label' => __( 'Textarea Input', 'wedevs' ),
80 | 'desc' => __( 'Textarea description', 'wedevs' ),
81 | 'placeholder' => __( 'Textarea placeholder', 'wedevs' ),
82 | 'type' => 'textarea'
83 | ),
84 | array(
85 | 'name' => 'html',
86 | 'desc' => __( 'HTML area description. You can use any bold or other HTML elements.', 'wedevs' ),
87 | 'type' => 'html'
88 | ),
89 | array(
90 | 'name' => 'checkbox',
91 | 'label' => __( 'Checkbox', 'wedevs' ),
92 | 'desc' => __( 'Checkbox Label', 'wedevs' ),
93 | 'type' => 'checkbox'
94 | ),
95 | array(
96 | 'name' => 'radio',
97 | 'label' => __( 'Radio Button', 'wedevs' ),
98 | 'desc' => __( 'A radio button', 'wedevs' ),
99 | 'type' => 'radio',
100 | 'options' => array(
101 | 'yes' => 'Yes',
102 | 'no' => 'No'
103 | )
104 | ),
105 | array(
106 | 'name' => 'selectbox',
107 | 'label' => __( 'A Dropdown', 'wedevs' ),
108 | 'desc' => __( 'Dropdown description', 'wedevs' ),
109 | 'type' => 'select',
110 | 'default' => 'no',
111 | 'options' => array(
112 | 'yes' => 'Yes',
113 | 'no' => 'No'
114 | )
115 | ),
116 | array(
117 | 'name' => 'password',
118 | 'label' => __( 'Password', 'wedevs' ),
119 | 'desc' => __( 'Password description', 'wedevs' ),
120 | 'type' => 'password',
121 | 'default' => ''
122 | ),
123 | array(
124 | 'name' => 'file',
125 | 'label' => __( 'File', 'wedevs' ),
126 | 'desc' => __( 'File description', 'wedevs' ),
127 | 'type' => 'file',
128 | 'default' => '',
129 | 'options' => array(
130 | 'button_label' => 'Choose Image'
131 | )
132 | )
133 | ),
134 | 'wedevs_advanced' => array(
135 | array(
136 | 'name' => 'color',
137 | 'label' => __( 'Color', 'wedevs' ),
138 | 'desc' => __( 'Color description', 'wedevs' ),
139 | 'type' => 'color',
140 | 'default' => ''
141 | ),
142 | array(
143 | 'name' => 'password',
144 | 'label' => __( 'Password', 'wedevs' ),
145 | 'desc' => __( 'Password description', 'wedevs' ),
146 | 'type' => 'password',
147 | 'default' => ''
148 | ),
149 | array(
150 | 'name' => 'wysiwyg',
151 | 'label' => __( 'Advanced Editor', 'wedevs' ),
152 | 'desc' => __( 'WP_Editor description', 'wedevs' ),
153 | 'type' => 'wysiwyg',
154 | 'default' => ''
155 | ),
156 | array(
157 | 'name' => 'multicheck',
158 | 'label' => __( 'Multile checkbox', 'wedevs' ),
159 | 'desc' => __( 'Multi checkbox description', 'wedevs' ),
160 | 'type' => 'multicheck',
161 | 'default' => array('one' => 'one', 'four' => 'four'),
162 | 'options' => array(
163 | 'one' => 'One',
164 | 'two' => 'Two',
165 | 'three' => 'Three',
166 | 'four' => 'Four'
167 | )
168 | ),
169 | )
170 | );
171 |
172 | return $settings_fields;
173 | }
174 |
175 | function plugin_page() {
176 | echo '
';
177 |
178 | $this->settings_api->show_navigation();
179 | $this->settings_api->show_forms();
180 |
181 | echo '
';
182 | }
183 |
184 | /**
185 | * Get all the pages
186 | *
187 | * @return array page names with key value pairs
188 | */
189 | function get_pages() {
190 | $pages = get_pages();
191 | $pages_options = array();
192 | if ( $pages ) {
193 | foreach ($pages as $page) {
194 | $pages_options[$page->ID] = $page->post_title;
195 | }
196 | }
197 |
198 | return $pages_options;
199 | }
200 |
201 | }
202 | endif;
203 |
--------------------------------------------------------------------------------
/example/procedural-example.php:
--------------------------------------------------------------------------------
1 |
8 | */
9 |
10 |
11 | /**
12 | * Registers settings section and fields
13 | */
14 | if ( !function_exists( 'wedevs_admin_init' ) ):
15 | function wedevs_admin_init() {
16 |
17 | $sections = array(
18 | array(
19 | 'id' => 'wedevs_basics',
20 | 'title' => __( 'Basic Settings', 'wedevs' )
21 | ),
22 | array(
23 | 'id' => 'wedevs_advanced',
24 | 'title' => __( 'Advanced Settings', 'wedevs' )
25 | ),
26 | array(
27 | 'id' => 'wedevs_others',
28 | 'title' => __( 'Other Settings', 'wpuf' )
29 | )
30 | );
31 |
32 | $fields = array(
33 | 'wedevs_basics' => array(
34 | array(
35 | 'name' => 'text',
36 | 'label' => __( 'Text Input', 'wedevs' ),
37 | 'desc' => __( 'Text input description', 'wedevs' ),
38 | 'type' => 'text',
39 | 'default' => 'Title'
40 | ),
41 | array(
42 | 'name' => 'textarea',
43 | 'label' => __( 'Textarea Input', 'wedevs' ),
44 | 'desc' => __( 'Textarea description', 'wedevs' ),
45 | 'type' => 'textarea'
46 | ),
47 | array(
48 | 'name' => 'checkbox',
49 | 'label' => __( 'Checkbox', 'wedevs' ),
50 | 'desc' => __( 'Checkbox Label', 'wedevs' ),
51 | 'type' => 'checkbox'
52 | ),
53 | array(
54 | 'name' => 'radio',
55 | 'label' => __( 'Radio Button', 'wedevs' ),
56 | 'desc' => __( 'A radio button', 'wedevs' ),
57 | 'type' => 'radio',
58 | 'options' => array(
59 | 'yes' => 'Yes',
60 | 'no' => 'No'
61 | )
62 | ),
63 | array(
64 | 'name' => 'multicheck',
65 | 'label' => __( 'Multile checkbox', 'wedevs' ),
66 | 'desc' => __( 'Multi checkbox description', 'wedevs' ),
67 | 'type' => 'multicheck',
68 | 'options' => array(
69 | 'one' => 'One',
70 | 'two' => 'Two',
71 | 'three' => 'Three',
72 | 'four' => 'Four'
73 | )
74 | ),
75 | array(
76 | 'name' => 'selectbox',
77 | 'label' => __( 'A Dropdown', 'wedevs' ),
78 | 'desc' => __( 'Dropdown description', 'wedevs' ),
79 | 'type' => 'select',
80 | 'default' => 'no',
81 | 'options' => array(
82 | 'yes' => 'Yes',
83 | 'no' => 'No'
84 | )
85 | ),
86 | array(
87 | 'name' => 'password',
88 | 'label' => __( 'Password', 'wedevs' ),
89 | 'desc' => __( 'Password description', 'wedevs' ),
90 | 'type' => 'password',
91 | 'default' => ''
92 | ),
93 | array(
94 | 'name' => 'file',
95 | 'label' => __( 'File', 'wedevs' ),
96 | 'desc' => __( 'File description', 'wedevs' ),
97 | 'type' => 'file',
98 | 'default' => ''
99 | ),
100 | array(
101 | 'name' => 'color',
102 | 'label' => __( 'Color', 'wedevs' ),
103 | 'desc' => __( 'Color description', 'wedevs' ),
104 | 'type' => 'color',
105 | 'default' => ''
106 | )
107 | ),
108 | 'wedevs_advanced' => array(
109 | array(
110 | 'name' => 'text',
111 | 'label' => __( 'Text Input', 'wedevs' ),
112 | 'desc' => __( 'Text input description', 'wedevs' ),
113 | 'type' => 'text',
114 | 'default' => 'Title'
115 | ),
116 | array(
117 | 'name' => 'textarea',
118 | 'label' => __( 'Textarea Input', 'wedevs' ),
119 | 'desc' => __( 'Textarea description', 'wedevs' ),
120 | 'type' => 'textarea'
121 | ),
122 | array(
123 | 'name' => 'checkbox',
124 | 'label' => __( 'Checkbox', 'wedevs' ),
125 | 'desc' => __( 'Checkbox Label', 'wedevs' ),
126 | 'type' => 'checkbox'
127 | ),
128 | array(
129 | 'name' => 'radio',
130 | 'label' => __( 'Radio Button', 'wedevs' ),
131 | 'desc' => __( 'A radio button', 'wedevs' ),
132 | 'type' => 'radio',
133 | 'default' => 'no',
134 | 'options' => array(
135 | 'yes' => 'Yes',
136 | 'no' => 'No'
137 | )
138 | ),
139 | array(
140 | 'name' => 'multicheck',
141 | 'label' => __( 'Multile checkbox', 'wedevs' ),
142 | 'desc' => __( 'Multi checkbox description', 'wedevs' ),
143 | 'type' => 'multicheck',
144 | 'default' => array( 'one' => 'one', 'four' => 'four' ),
145 | 'options' => array(
146 | 'one' => 'One',
147 | 'two' => 'Two',
148 | 'three' => 'Three',
149 | 'four' => 'Four'
150 | )
151 | ),
152 | array(
153 | 'name' => 'selectbox',
154 | 'label' => __( 'A Dropdown', 'wedevs' ),
155 | 'desc' => __( 'Dropdown description', 'wedevs' ),
156 | 'type' => 'select',
157 | 'options' => array(
158 | 'yes' => 'Yes',
159 | 'no' => 'No'
160 | )
161 | ),
162 | array(
163 | 'name' => 'password',
164 | 'label' => __( 'Password', 'wedevs' ),
165 | 'desc' => __( 'Password description', 'wedevs' ),
166 | 'type' => 'password',
167 | 'default' => ''
168 | ),
169 | array(
170 | 'name' => 'file',
171 | 'label' => __( 'File', 'wedevs' ),
172 | 'desc' => __( 'File description', 'wedevs' ),
173 | 'type' => 'file',
174 | 'default' => ''
175 | ),
176 | array(
177 | 'name' => 'color',
178 | 'label' => __( 'Color', 'wedevs' ),
179 | 'desc' => __( 'Color description', 'wedevs' ),
180 | 'type' => 'color',
181 | 'default' => ''
182 | )
183 | ),
184 | 'wedevs_others' => array(
185 | array(
186 | 'name' => 'text',
187 | 'label' => __( 'Text Input', 'wedevs' ),
188 | 'desc' => __( 'Text input description', 'wedevs' ),
189 | 'type' => 'text',
190 | 'default' => 'Title'
191 | ),
192 | array(
193 | 'name' => 'textarea',
194 | 'label' => __( 'Textarea Input', 'wedevs' ),
195 | 'desc' => __( 'Textarea description', 'wedevs' ),
196 | 'type' => 'textarea'
197 | ),
198 | array(
199 | 'name' => 'checkbox',
200 | 'label' => __( 'Checkbox', 'wedevs' ),
201 | 'desc' => __( 'Checkbox Label', 'wedevs' ),
202 | 'type' => 'checkbox'
203 | ),
204 | array(
205 | 'name' => 'radio',
206 | 'label' => __( 'Radio Button', 'wedevs' ),
207 | 'desc' => __( 'A radio button', 'wedevs' ),
208 | 'type' => 'radio',
209 | 'options' => array(
210 | 'yes' => 'Yes',
211 | 'no' => 'No'
212 | )
213 | ),
214 | array(
215 | 'name' => 'multicheck',
216 | 'label' => __( 'Multile checkbox', 'wedevs' ),
217 | 'desc' => __( 'Multi checkbox description', 'wedevs' ),
218 | 'type' => 'multicheck',
219 | 'options' => array(
220 | 'one' => 'One',
221 | 'two' => 'Two',
222 | 'three' => 'Three',
223 | 'four' => 'Four'
224 | )
225 | ),
226 | array(
227 | 'name' => 'selectbox',
228 | 'label' => __( 'A Dropdown', 'wedevs' ),
229 | 'desc' => __( 'Dropdown description', 'wedevs' ),
230 | 'type' => 'select',
231 | 'options' => array(
232 | 'yes' => 'Yes',
233 | 'no' => 'No'
234 | )
235 | ),
236 | array(
237 | 'name' => 'password',
238 | 'label' => __( 'Password', 'wedevs' ),
239 | 'desc' => __( 'Password description', 'wedevs' ),
240 | 'type' => 'password',
241 | 'default' => ''
242 | ),
243 | array(
244 | 'name' => 'file',
245 | 'label' => __( 'File', 'wedevs' ),
246 | 'desc' => __( 'File description', 'wedevs' ),
247 | 'type' => 'file',
248 | 'default' => ''
249 | ),
250 | array(
251 | 'name' => 'color',
252 | 'label' => __( 'Color', 'wedevs' ),
253 | 'desc' => __( 'Color description', 'wedevs' ),
254 | 'type' => 'color',
255 | 'default' => ''
256 | )
257 | )
258 | );
259 |
260 | $settings_api = new WeDevs_Settings_API;
261 |
262 | //set sections and fields
263 | $settings_api->set_sections( $sections );
264 | $settings_api->set_fields( $fields );
265 |
266 | //initialize them
267 | $settings_api->admin_init();
268 | }
269 | endif;
270 | add_action( 'admin_init', 'wedevs_admin_init' );
271 |
272 | if ( !function_exists( 'wedevs_admin_menu' ) ):
273 | /**
274 | * Register the plugin page
275 | */
276 | function wedevs_admin_menu() {
277 | add_options_page( 'Settings API', 'Settings API', 'delete_posts', 'settings_api_test', 'wedevs_plugin_page' );
278 | }
279 | endif;
280 | add_action( 'admin_menu', 'wedevs_admin_menu' );
281 |
282 | /**
283 | * Display the plugin settings options page
284 | */
285 | if ( !function_exists( 'wedevs_plugin_page' ) ):
286 | function wedevs_plugin_page() {
287 | $settings_api = new WeDevs_Settings_API;
288 |
289 | echo '';
290 | settings_errors();
291 |
292 | $settings_api->show_navigation();
293 | $settings_api->show_forms();
294 |
295 | echo '
';
296 | }
297 | endif;
298 |
--------------------------------------------------------------------------------
/plugin.php:
--------------------------------------------------------------------------------
1 |
9 | * @link https://tareq.co Tareq Hasan
10 | * @example example/oop-example.php How to use the class
11 | */
12 | if ( !class_exists( 'WeDevs_Settings_API' ) ):
13 | class WeDevs_Settings_API {
14 |
15 | /**
16 | * settings sections array
17 | *
18 | * @var array
19 | */
20 | protected $settings_sections = array();
21 |
22 | /**
23 | * Settings fields array
24 | *
25 | * @var array
26 | */
27 | protected $settings_fields = array();
28 |
29 | public function __construct() {
30 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
31 | }
32 |
33 | /**
34 | * Enqueue scripts and styles
35 | */
36 | function admin_enqueue_scripts() {
37 | wp_enqueue_style( 'wp-color-picker' );
38 |
39 | wp_enqueue_media();
40 | wp_enqueue_script( 'wp-color-picker' );
41 | wp_enqueue_script( 'jquery' );
42 | }
43 |
44 | /**
45 | * Set settings sections
46 | *
47 | * @param array $sections setting sections array
48 | */
49 | function set_sections( $sections ) {
50 | $this->settings_sections = $sections;
51 |
52 | return $this;
53 | }
54 |
55 | /**
56 | * Add a single section
57 | *
58 | * @param array $section
59 | */
60 | function add_section( $section ) {
61 | $this->settings_sections[] = $section;
62 |
63 | return $this;
64 | }
65 |
66 | /**
67 | * Set settings fields
68 | *
69 | * @param array $fields settings fields array
70 | */
71 | function set_fields( $fields ) {
72 | $this->settings_fields = $fields;
73 |
74 | return $this;
75 | }
76 |
77 | function add_field( $section, $field ) {
78 | $defaults = array(
79 | 'name' => '',
80 | 'label' => '',
81 | 'desc' => '',
82 | 'type' => 'text'
83 | );
84 |
85 | $arg = wp_parse_args( $field, $defaults );
86 | $this->settings_fields[$section][] = $arg;
87 |
88 | return $this;
89 | }
90 |
91 | /**
92 | * Initialize and registers the settings sections and fileds to WordPress
93 | *
94 | * Usually this should be called at `admin_init` hook.
95 | *
96 | * This function gets the initiated settings sections and fields. Then
97 | * registers them to WordPress and ready for use.
98 | */
99 | function admin_init() {
100 | //register settings sections
101 | foreach ( $this->settings_sections as $section ) {
102 | if ( false == get_option( $section['id'] ) ) {
103 | add_option( $section['id'] );
104 | }
105 |
106 | if ( isset($section['desc']) && !empty($section['desc']) ) {
107 | $section['desc'] = '' . $section['desc'] . '
';
108 | $callback = function() use ( $section ) {
109 | echo str_replace( '"', '\"', $section['desc'] );
110 | };
111 | } else if ( isset( $section['callback'] ) ) {
112 | $callback = $section['callback'];
113 | } else {
114 | $callback = null;
115 | }
116 |
117 | add_settings_section( $section['id'], $section['title'], $callback, $section['id'] );
118 | }
119 |
120 | //register settings fields
121 | foreach ( $this->settings_fields as $section => $field ) {
122 | foreach ( $field as $option ) {
123 |
124 | $name = $option['name'];
125 | $type = isset( $option['type'] ) ? $option['type'] : 'text';
126 | $label = isset( $option['label'] ) ? $option['label'] : '';
127 | $callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type );
128 |
129 | $args = array(
130 | 'id' => $name,
131 | 'class' => isset( $option['class'] ) ? $option['class'] : $name,
132 | 'label_for' => "{$section}[{$name}]",
133 | 'desc' => isset( $option['desc'] ) ? $option['desc'] : '',
134 | 'name' => $label,
135 | 'section' => $section,
136 | 'size' => isset( $option['size'] ) ? $option['size'] : null,
137 | 'options' => isset( $option['options'] ) ? $option['options'] : '',
138 | 'std' => isset( $option['default'] ) ? $option['default'] : '',
139 | 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
140 | 'type' => $type,
141 | 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
142 | 'min' => isset( $option['min'] ) ? $option['min'] : '',
143 | 'max' => isset( $option['max'] ) ? $option['max'] : '',
144 | 'step' => isset( $option['step'] ) ? $option['step'] : '',
145 | );
146 |
147 | add_settings_field( "{$section}[{$name}]", $label, $callback, $section, $section, $args );
148 | }
149 | }
150 |
151 | // creates our settings in the options table
152 | foreach ( $this->settings_sections as $section ) {
153 | register_setting( $section['id'], $section['id'], array( $this, 'sanitize_options' ) );
154 | }
155 | }
156 |
157 | /**
158 | * Get field description for display
159 | *
160 | * @param array $args settings field args
161 | */
162 | public function get_field_description( $args ) {
163 | if ( ! empty( $args['desc'] ) ) {
164 | $desc = sprintf( '%s
', $args['desc'] );
165 | } else {
166 | $desc = '';
167 | }
168 |
169 | return $desc;
170 | }
171 |
172 | /**
173 | * Displays a text field for a settings field
174 | *
175 | * @param array $args settings field args
176 | */
177 | function callback_text( $args ) {
178 |
179 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
180 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
181 | $type = isset( $args['type'] ) ? $args['type'] : 'text';
182 | $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
183 |
184 | $html = sprintf( ' ', $type, $size, $args['section'], $args['id'], $value, $placeholder );
185 | $html .= $this->get_field_description( $args );
186 |
187 | echo $html;
188 | }
189 |
190 | /**
191 | * Displays a url field for a settings field
192 | *
193 | * @param array $args settings field args
194 | */
195 | function callback_url( $args ) {
196 | $this->callback_text( $args );
197 | }
198 |
199 | /**
200 | * Displays a number field for a settings field
201 | *
202 | * @param array $args settings field args
203 | */
204 | function callback_number( $args ) {
205 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
206 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
207 | $type = isset( $args['type'] ) ? $args['type'] : 'number';
208 | $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . $args['placeholder'] . '"';
209 | $min = ( $args['min'] == '' ) ? '' : ' min="' . $args['min'] . '"';
210 | $max = ( $args['max'] == '' ) ? '' : ' max="' . $args['max'] . '"';
211 | $step = ( $args['step'] == '' ) ? '' : ' step="' . $args['step'] . '"';
212 |
213 | $html = sprintf( ' ', $type, $size, $args['section'], $args['id'], $value, $placeholder, $min, $max, $step );
214 | $html .= $this->get_field_description( $args );
215 |
216 | echo $html;
217 | }
218 |
219 | /**
220 | * Displays a checkbox for a settings field
221 | *
222 | * @param array $args settings field args
223 | */
224 | function callback_checkbox( $args ) {
225 |
226 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
227 |
228 | $html = '';
229 | $html .= sprintf( '', $args['section'], $args['id'] );
230 | $html .= sprintf( ' ', $args['section'], $args['id'] );
231 | $html .= sprintf( ' ', $args['section'], $args['id'], checked( $value, 'on', false ) );
232 | $html .= sprintf( '%1$s ', $args['desc'] );
233 | $html .= ' ';
234 |
235 | echo $html;
236 | }
237 |
238 | /**
239 | * Displays a multicheckbox for a settings field
240 | *
241 | * @param array $args settings field args
242 | */
243 | function callback_multicheck( $args ) {
244 |
245 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
246 | $html = '';
247 | $html .= sprintf( ' ', $args['section'], $args['id'] );
248 | foreach ( $args['options'] as $key => $label ) {
249 | $checked = isset( $value[$key] ) ? $value[$key] : '0';
250 | $html .= sprintf( '', $args['section'], $args['id'], $key );
251 | $html .= sprintf( ' ', $args['section'], $args['id'], $key, checked( $checked, $key, false ) );
252 | $html .= sprintf( '%1$s ', $label );
253 | }
254 |
255 | $html .= $this->get_field_description( $args );
256 | $html .= ' ';
257 |
258 | echo $html;
259 | }
260 |
261 | /**
262 | * Displays a radio button for a settings field
263 | *
264 | * @param array $args settings field args
265 | */
266 | function callback_radio( $args ) {
267 |
268 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
269 | $html = '';
270 |
271 | foreach ( $args['options'] as $key => $label ) {
272 | $html .= sprintf( '', $args['section'], $args['id'], $key );
273 | $html .= sprintf( ' ', $args['section'], $args['id'], $key, checked( $value, $key, false ) );
274 | $html .= sprintf( '%1$s ', $label );
275 | }
276 |
277 | $html .= $this->get_field_description( $args );
278 | $html .= ' ';
279 |
280 | echo $html;
281 | }
282 |
283 | /**
284 | * Displays a selectbox for a settings field
285 | *
286 | * @param array $args settings field args
287 | */
288 | function callback_select( $args ) {
289 |
290 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
291 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
292 | $html = sprintf( '', $size, $args['section'], $args['id'] );
293 |
294 | foreach ( $args['options'] as $key => $label ) {
295 | $html .= sprintf( '%s ', $key, selected( $value, $key, false ), $label );
296 | }
297 |
298 | $html .= sprintf( ' ' );
299 | $html .= $this->get_field_description( $args );
300 |
301 | echo $html;
302 | }
303 |
304 | /**
305 | * Displays a textarea for a settings field
306 | *
307 | * @param array $args settings field args
308 | */
309 | function callback_textarea( $args ) {
310 |
311 | $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
312 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
313 | $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="'.$args['placeholder'].'"';
314 |
315 | $html = sprintf( '', $size, $args['section'], $args['id'], $placeholder, $value );
316 | $html .= $this->get_field_description( $args );
317 |
318 | echo $html;
319 | }
320 |
321 | /**
322 | * Displays the html for a settings field
323 | *
324 | * @param array $args settings field args
325 | * @return string
326 | */
327 | function callback_html( $args ) {
328 | echo $this->get_field_description( $args );
329 | }
330 |
331 | /**
332 | * Displays a rich text textarea for a settings field
333 | *
334 | * @param array $args settings field args
335 | */
336 | function callback_wysiwyg( $args ) {
337 |
338 | $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
339 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px';
340 |
341 | echo '';
342 |
343 | $editor_settings = array(
344 | 'teeny' => true,
345 | 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
346 | 'textarea_rows' => 10
347 | );
348 |
349 | if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
350 | $editor_settings = array_merge( $editor_settings, $args['options'] );
351 | }
352 |
353 | wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
354 |
355 | echo '
';
356 |
357 | echo $this->get_field_description( $args );
358 | }
359 |
360 | /**
361 | * Displays a file upload field for a settings field
362 | *
363 | * @param array $args settings field args
364 | */
365 | function callback_file( $args ) {
366 |
367 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
368 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
369 | $id = $args['section'] . '[' . $args['id'] . ']';
370 | $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File' );
371 |
372 | $html = sprintf( ' ', $size, $args['section'], $args['id'], $value );
373 | $html .= ' ';
374 | $html .= $this->get_field_description( $args );
375 |
376 | echo $html;
377 | }
378 |
379 | /**
380 | * Displays a password field for a settings field
381 | *
382 | * @param array $args settings field args
383 | */
384 | function callback_password( $args ) {
385 |
386 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
387 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
388 |
389 | $html = sprintf( ' ', $size, $args['section'], $args['id'], $value );
390 | $html .= $this->get_field_description( $args );
391 |
392 | echo $html;
393 | }
394 |
395 | /**
396 | * Displays a color picker field for a settings field
397 | *
398 | * @param array $args settings field args
399 | */
400 | function callback_color( $args ) {
401 |
402 | $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
403 | $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
404 |
405 | $html = sprintf( ' ', $size, $args['section'], $args['id'], $value, $args['std'] );
406 | $html .= $this->get_field_description( $args );
407 |
408 | echo $html;
409 | }
410 |
411 |
412 | /**
413 | * Displays a select box for creating the pages select box
414 | *
415 | * @param array $args settings field args
416 | */
417 | function callback_pages( $args ) {
418 |
419 | $dropdown_args = array(
420 | 'selected' => esc_attr($this->get_option($args['id'], $args['section'], $args['std'] ) ),
421 | 'name' => $args['section'] . '[' . $args['id'] . ']',
422 | 'id' => $args['section'] . '[' . $args['id'] . ']',
423 | 'echo' => 0
424 | );
425 | $html = wp_dropdown_pages( $dropdown_args );
426 | echo $html;
427 | }
428 |
429 | /**
430 | * Sanitize callback for Settings API
431 | *
432 | * @return mixed
433 | */
434 | function sanitize_options( $options ) {
435 |
436 | if ( !$options ) {
437 | return $options;
438 | }
439 |
440 | foreach( $options as $option_slug => $option_value ) {
441 | $sanitize_callback = $this->get_sanitize_callback( $option_slug );
442 |
443 | // If callback is set, call it
444 | if ( $sanitize_callback ) {
445 | $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
446 | continue;
447 | }
448 | }
449 |
450 | return $options;
451 | }
452 |
453 | /**
454 | * Get sanitization callback for given option slug
455 | *
456 | * @param string $slug option slug
457 | *
458 | * @return mixed string or bool false
459 | */
460 | function get_sanitize_callback( $slug = '' ) {
461 | if ( empty( $slug ) ) {
462 | return false;
463 | }
464 |
465 | // Iterate over registered fields and see if we can find proper callback
466 | foreach( $this->settings_fields as $section => $options ) {
467 | foreach ( $options as $option ) {
468 | if ( $option['name'] != $slug ) {
469 | continue;
470 | }
471 |
472 | // Return the callback name
473 | return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
474 | }
475 | }
476 |
477 | return false;
478 | }
479 |
480 | /**
481 | * Get the value of a settings field
482 | *
483 | * @param string $option settings field name
484 | * @param string $section the section name this field belongs to
485 | * @param string $default default text if it's not found
486 | * @return string
487 | */
488 | function get_option( $option, $section, $default = '' ) {
489 |
490 | $options = get_option( $section );
491 |
492 | if ( isset( $options[$option] ) ) {
493 | return $options[$option];
494 | }
495 |
496 | return $default;
497 | }
498 |
499 | /**
500 | * Show navigations as tab
501 | *
502 | * Shows all the settings section labels as tab
503 | */
504 | function show_navigation() {
505 | $html = '';
506 |
507 | $count = count( $this->settings_sections );
508 |
509 | // don't show the navigation if only one section exists
510 | if ( $count === 1 ) {
511 | return;
512 | }
513 |
514 | foreach ( $this->settings_sections as $tab ) {
515 | $html .= sprintf( '%2$s ', $tab['id'], $tab['title'] );
516 | }
517 |
518 | $html .= ' ';
519 |
520 | echo $html;
521 | }
522 |
523 | /**
524 | * Show the section settings forms
525 | *
526 | * This function displays every sections in a different form
527 | */
528 | function show_forms() {
529 | ?>
530 |
549 | script();
551 | }
552 |
553 | /**
554 | * Tabbable JavaScript codes & Initiate Color Picker
555 | *
556 | * This code uses localstorage for displaying active tabs
557 | */
558 | function script() {
559 | ?>
560 |
638 | _style_fix();
640 | }
641 |
642 | function _style_fix() {
643 | global $wp_version;
644 |
645 | if (version_compare($wp_version, '3.8', '<=')):
646 | ?>
647 |
652 |