-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-easy_settings_api_html_output.php
475 lines (376 loc) · 12.2 KB
/
class-easy_settings_api_html_output.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* @package WordPress
* @subpackage Settings-API class
* @author Ralf Albert
* @version 0.7.0
* @license GPL
*/
/**
License:
==============================================================================
Copyright 2011 Ralf Albert (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Requirements:
==============================================================================
This plugin requires WordPress >= 3.0 and tested with PHP Interpreter >= 5.2.9
*/
if( ! class_exists( 'Easy_Settings_API_Class_HTML_Output' ) )
{
class Easy_Settings_API_HTML_Output
{
/**
*
* Defaults for settings fields
* @var object $filed_defaults
*/
public static $fields_defaults = null;
/**
*
* Options from database
* @var array $options
*/
public static $options;
/**
*
* Constructor
* @param none
* @return void
* @since 0.4
* @access public
*/
public function __construct()
{
$sfd = new stdClass();
$sfd->title = 'Empty';
$sfd->section = 'general';
$sfd->id = 'default_field';
$sfd->description = '';
$sfd->text_after = '';
$sfd->std = '';
$sfd->type = 'text';
$sfd->size = 0;
$sfd->rows = 3;
$sfd->cols = 25;
$sfd->choices = array();
$sfd->arguments = array();
$sfd->class = '';
$sfd->options_name = 'empty';
self::$fields_defaults = $sfd;
}
/**
*
* Print the options-page frame
* @param object $args
* @return void
* @since 0.4
* @access public
*/
public static function display_page( $args = null ){
if( null === $args )
return false;
$def = new stdClass();
$def->icon = 'icon-options-general';
$def->page_title = __('Empty');
$def->description = __('Empty');
$def->options_group = '';
$def->page_slug = '';
$args = self::parse_args( $args, $def, true );
echo '
<div class="wrap">
<div class="icon32" id="' . $args->icon . '"></div>
<h2>' . $args->page_title . '</h2>
';
if ( '' != $args->description )
echo '<p>' . $args->description . '</p>';
echo '
<form action="options.php" method="post">
';
settings_fields( $args->options_group );
do_settings_sections( $args->page_slug );
echo '
<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="' . __('Save Changes') . '" />
</p>
</form>
</div>
';
}
/* ------------ display settings fields ------------ */
/**
* custom settings field
*/
public static function custom( $args ){
if( null === $args )
return false;
if( isset( $args->callback ) ){
if( ! is_array( $args->callback ) )
return false;
if( ! isset( $args->arguments ) )
$args->arguments = array();
if( ! is_array( $args->arguments ) )
$args->arguments = (array) $args->arguments;
call_user_func_array( $args->callback, $args->arguments );
}
}
/**
* checkbox
*/
public static function checkbox( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
( isset( self::$options[$args->id] ) && 'on' === self::$options[$args->id] ) ?
$checked = 'checked="checked"' : $checked = '';
printf(
'<input id="%1$s" name="%2$s[%1$s]" %3$s type="checkbox" value="on" %5$s />
<label for="%1$s">%4$s</label>',
$args->id, $args->options_name,$args->class, $args->text_after, $checked
);
self::display_field_description( $args->description );
}
/**
* radio buttons
*/
public static function radio( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
$i = 0;
foreach( $args->choices as $key => $label) {
( isset( self::$options[$args->id] ) && self::$options[$args->id] == $key ) ?
$selected = 'checked="checked"' : $selected = '';
printf(
'<input id="%1$s4$s" name="%2$s[%1$s]" %3$s type="radio" value="%5$s" %6$s />
<label for="%1$s%4$s">%7$s</label>',
$args->id, $args->options_name, $args->class, $i, $key, $selected, $label
);
if( $i < count( $args->choices ) - 1)
echo '<br />';
$i++;
}
self::display_field_description( $args->description );
}
/**
* checkboxes with multiple selection
*/
public static function mcheckbox( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
$i = 0;
foreach( $args->choices as $key => $label) {
( isset( self::$options[$args->id . '-' . $key]) && 'on' === self::$options[$args->id . '-' . $key]) ?
$checked = 'checked="checked"' : $checked = '';
printf(
'<input id="%1$s-%4$s" name="%2$s[%1$s-%4$s]" type="checkbox" value="on" %5$s />
<label for="' . $args->id . '">' . $label . '</label>',
$args->id, $args->options_name, $args->class, $key, $checked
);
if ( $i < count( $args->choices ) - 1 )
echo '<br />';
$i++;
}
// this hidden input is neccessary to identify if the form is already saved
// or if it is the initial form with standard values
//echo '<input type="hidden" name="' . $args->options_name . '[' . $args->id . ']" value="on" />';
printf(
'<input type="hidden" name="%2$s[%1$s]" value="on" />',
$args->id, $args->options_name
);
self::display_field_description( $args->description );
}
/**
* select field
*/
public static function select( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
( isset( $args->size ) && 1 < $args->size ) ?
$size = 'size="' . $args->size . '"' : $size = '';
printf(
'<select id="%1$s" name="%3$s[%1$s]" %2$s %4$s style="height:100%%">',
$args->id, $args->class, $args->options_name, $size
);
foreach( $args->choices as $key => $label ) {
( isset( self::$options[$args->id] ) && self::$options[$args->id] == $key ) ?
$selected = 'selected="selected"' : $selected = '';
printf(
'<option value="%1$s" %2$s>%3$s</option>',
$key, $selected, $label
);
}
echo '</select>' . $args->text_after;
self::display_field_description( $args->description );
}
/**
* select field with multiple selection
*/
public static function mselect( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
( isset( $args->size ) && 1 < $args->size ) ?
$size = 'size="' . $args->size . '"' : $size = '';
printf( '<select id="%1$s" %2$s name="%3$s[%1$s][]" %4$s multiple="multiple" style="height:100%%">',
$args->id, $args->class, $args->options_name, $size
);
foreach( $args->choices as $key => $label ) {
( in_array( $key, self::$options[$args->id] ) ) ?
$selected = 'selected="selected"' : $selected = '';
printf(
'<option value="%1$s" %2$s>%3$s</option>',
$key, $selected, $label
);
}
echo '</select>' . $args->text_after;
self::display_field_description( $args->description );
}
/**
* textarea
*/
public static function textarea( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
$value = isset( self::$options[$args->id] ) ? self::$options[$args->id] : $args->std;
printf(
'<textarea id="%1$s" name="%2$s[%1$s]" %3$s rows="%4$s" cols="%5$s" placeholder="%6$s">%7$s</textarea>%8$s',
$args->id, $args->options_name, $args->class, $args->rows, $args->cols, $args->std, esc_textarea( $value ), $args->text_after
);
self::display_field_description( $args->description );
}
/**
* password field
*/
public static function password( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
$value = isset( self::$options[$args->id] ) ? self::$options[$args->id] : $args->std;
printf(
'<input id="%1$s" name="%2$s[%1$s]" %3$s type="password" value="%4$s" />%5$s',
$args->id, $args->options_name, $args->class, $value, $args->text_after
);
self::display_field_description( $args->description );
}
/**
* input field
*/
public static function text( $args = null ){
if( null === $args )
return false;
$args = self::parse_args( $args, self::$fields_defaults );
$value = isset( self::$options[$args->id] ) ? self::$options[$args->id] : $args->std;
printf(
'<input id="%1$s" name="%2$s[%1$s]" %3$s type="text" value="%4$s" size="%6$s" placeholder="%7$s" />%5$s',
$args->id, $args->options_name, $args->class, esc_html( $value ), $args->text_after, $args->size, $args->std
);
self::display_field_description( $args->description );
}
/**
* heading
*/
public static function heading( $args ){
if( null === $args )
return false;
$def = new stdClass();
$def->description = '';
$args = self::parse_args( $args, $def, true );
echo '</td></tr><tr valign="top"><td colspan="2">' . $args->description;
}
/**
*
* Helper function
* @param string $desc
* @return void
* @since 0.2
* @access public static
*/
public static function display_field_description( $desc ){
if( ! empty( $desc ) )
echo '<br /><small>' . $desc . '</small>';
}
/**
*
* Setter for $options
* @param array $options
* @return void
* @since 0.5
* @access public
*/
public function set_options( $options = null ){
if( null !== $options )
self::$options = $options;
}
/**
*
* Getter for $options
* @param none
* @return array $options
* @since 0.5
* @access public
*/
public function get_options(){
return self::$options;
}
/**
*
* Getter for fields defaults
* @param none
* @return object $fields_defaults
* @since 0.5.2
* @access public
*/
public function get_fields_defaults(){
return self::$fields_defaults;
}
/**
*
* Parsing arguments
*
* Retrieving two objects and fill the input-object with the default-object
* If the optional parameter $cleaning is set to true, keys which are not set in
* the defaults-object will be deleted.
*
* @param object $input
* @param object $defaults
* @param bool $cleaning
* @return object $input parsed (and cleanded) object
* @since 0.5
* @access public static
*/
public static function parse_args( $input = null, $defaults = null, $cleaning = false ){
if( null === $input || null === $defaults )
return false;
// let WordPress parsing arrays. cast the returned array to an object
if( is_array( $input ) ){
$input = (object) wp_parse_args( $input, (array) $defaults );
}
else {
foreach( $defaults as $key => $value ){
if( ! isset( $input->$key ) )
$input->$key = $defaults->$key;
}
}
if( $cleaning ){
foreach( $input as $key => $value ){
if( ! isset( $defaults->$key ) )
unset( $input->$key );
}
}
return $input;
}
} // end class_Settings_API_Class_HTML_Output
} // end if_class_exists