-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsac_demo.php
405 lines (342 loc) · 12.8 KB
/
sac_demo.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
<?php
/**
* @package WordPress
* @subpackage Easy Settings API Class
* @author Ralf Albert
* @version 0.7.0
*
*/
/**
* Plugin Name: Easy Settings-API Class (Demo)
* Plugin URI: http://yoda.neun12.de/
* Text Domain: ESAC_DEMO
* Domain Path: /languages
* Description: Demo for the the Easy Settings-API Class (ESAC). This plugin create a simple option page to show all available setting fields
* Author: Ralf Albert
* Version: 0.7.0
* Author URI: http://neun12.de/
* Licence: GPL
*/
if( ! class_exists( 'Easy_Settings_API' ) )
require_once dirname( __FILE__ ) . '/class-easy_settings_api.php';
if( ! class_exists( 'Easy_Settings_API_Class_Demo' ) && function_exists( 'add_action' ) )
{
add_action( 'plugins_loaded', array( 'Easy_Settings_API_Class_Demo', 'plugin_start' ) );
class Easy_Settings_API_Class_Demo
{
/**
*
* Instance of this class. Will be used in validate_input()
* @var object $plugin_self
*/
private static $plugin_self = null;
/**
*
* Instance of Easy_Settings_API. Will be used in validate_input()
* @var object $options_page
*/
public static $optionspage = null;
/**
*
* Name of the options in database
* @var const string
*/
const OPTIONS_NAME = 'SAC_DEMO_SETTINGS';
/**
*
* Name of the options-group
* @var const string
*/
const OPTIONS_GROUP = 'SAC_DEMO';
/**
*
* Initialize the plugin and save an instance of the class
* @param none
* @return object $plugin_self
* @since 0.3
* @access public
*/
public static function plugin_start()
{
if( null === self::$plugin_self )
self::$plugin_self = new self;
return self::$plugin_self;
}
/**
*
* Will be called when the plugin is deactivated.
* The options are removed from database
* @param none
* @return void
* @since 0.1
* @access public
*/
public function deactivation()
{
delete_option( self::OPTIONS_NAME );
}
/**
*
* Constructor
* @@param none
* @return void
* @since 0.1
* @access public
* @return object
*/
public function __construct()
{
if( null !== self::$plugin_self )
return self::$plugin_self;
// register the deactivation hook
register_deactivation_hook( __FILE__, array( &$this, 'deactivation' ) );
// create an instance of the class
$optionspage = new Easy_Settings_API();
// starts with basic configuration
$base_config = new stdClass();
$base_config->options_group = self::OPTIONS_GROUP;
$base_config->options_name = self::OPTIONS_NAME;
$base_config->validate_callback = array( __CLASS__, 'validate_input' ); //'Settings_API_Class_Demo::validate_input',
$base_config->menu_position = 'options';
$base_config->page_slug = 'sac_demopage';
$base_config->page_title = 'Settings API Class Demo Page';
$base_config->menu_title = 'SAC Demopage';
$base_config->description = 'This is a demo page for the Settings API Class.';
$base_config->capability = 'manage_options';
$base_config->icon = 'icon-options-general';
$optionspage->basic_config( $base_config, __FILE__ );
/*
* JavaScript
*
* tag = source :: simplest way (script will be enqueued with no dependencies, in head)
*
* tag->src = string :: full relative path to file
* tag->dependencies = array() :: dependencies as array
* tag->version = string :: js version
* tag->in_footer = bool :: load script in footer (true) or head (false)
*/
$scripts = new stdClass();
$scripts->sac_demo_js = plugins_url( 'js/demo_js.js', __FILE__ );
$scripts->sac_demo1_js->src = plugins_url( 'js/alert.js', __FILE__ );
$scripts->sac_demo2_js->src = plugins_url('/js/demo_js.js', __FILE__ );
$scripts->sac_demo2_js->dependencies = array( 'jquery' );
$scripts->sac_demo2_js->version = false;
$scripts->sac_demo2_js->in_footer = true;
$optionspage->add_script( $scripts );
/*
* Stylesheets
*
* tag->src = string :: full relative path to sourcefile
* tag->deps = array :: array with dependencies
*/
$styles = new stdClass();
$styles->first_demo_style->src = plugins_url( 'css/demostyle.css', __FILE__ );
$styles->first_demo_style->deps = 'none';
$styles->second_demo_style->src = plugins_url( 'css/demostyle.css', __FILE__ );
$styles->third_demo_style = plugins_url( 'css/demostyle.css', __FILE__ );
$optionspage->add_style( $styles );
/*
* Sections
*
* tag->title
* tag->description
*/
$sections= new stdClass();
$sections->general->title = __('General Settings');
$sections->general->description = __('Description for general settings (optional).');
$sections->multi->title = __('Multiple Choice');
$sections->multi->description = __('More than one choice are available.');
$optionspage->add_section( $sections );
/*
* Settings fields
*
* Each field can define some params. Minimum are 'id', 'type', 'title' and 'section'
*
* tag->id
* tag->type
* tag->title
* tag->section
* tag->some_other_params
*/
/* fields for section 'general' */
// heading field
$field = new stdClass();
$field->heading->id = 'demo_heading';
$field->heading->type = 'heading';
$field->heading->title = 'Heading';
$field->heading->section = 'general';
$field->heading->description = __('Headings only use the title and description as parameter.');
$optionspage->add_field( $field );
// custom field
$field = new stdClass();
$field->custom->id = 'demo_custom';
$field->custom->type = 'custom';
$field->custom->title = 'Custom';
$field->custom->section = 'general';
$field->custom->description = 'Custum is using a callbackfunction to display the input';
$field->custom->callback = array( __CLASS__, 'custom_callback' );
// each single array-element is passed as single argument to the
// callback-function.
// all keys in an associativ array will be lost.
// if an array should passed as argument to the callback-function,
// it must be itself an array.
$field->custom->arguments = array( 'one' => 'eins', array( 'two' => 'zwei' ) );
$optionspage->add_field( $field );
// checkbox field
$field = new stdClass();
$field->checkbox->id = 'demo_checkbox';
$field->checkbox->type = 'checkbox';
$field->checkbox->title = __('Checkbox');
$field->checkbox->section = 'general';
$field->checkbox->description = __('The description of the checkbox');
$field->checkbox->text_after = __('Text after the checkbox. This text is formated as <code>label</code>.');
$field->checkbox->std = 'on'; // values are 'on' or '' (empty). Everything else than 'on' is equal to an empty value
$optionspage->add_field( $field );
// adding more than one field at once
$field = new stdClass();
// input field
$field->input->id = 'demo_textinput';
$field->input->title = __('Text input');
$field->input->section = 'general';
$field->input->description = __('The description of the text input');
$field->input->text_after = __('Text after the input-field');
$field->input->std = 'demo text';
$field->input->size = 30;
$field->input->type = 'text';
// password field
$field->password->id = 'demo_password';
$field->password->title = __('Password');
$field->password->section = 'general';
$field->password->description = __('You can even preselect a standard password');
$field->password->text_after = __('Text after the password-field');
$field->password->std = 'password';
$field->password->size = 30;
$field->password->type = 'password';
// textarea field
$field->textarea->id = 'demo_texarea';
$field->textarea->title = __('Textarea');
$field->textarea->description = __('The description of the textarea');
$field->textarea->text_after = __('Text after the textarea');
$field->textarea->std = 'Textareas are good for longer inputs. You can select the width and height of the textarea with the rows- and cols-parameter.';
$field->textarea->rows = 3;
$field->textarea->cols = 30;
$field->textarea->type = 'textarea';
$field->textarea->section = 'general';
$optionspage->add_field( $field );
/* fields for section 'multi' (inputs with multiple choice) */
$mfield = new stdClass();
// radio field
$mfields->radio->id = 'demo_radio';
$mfields->radio->title = __('Radio');
$mfields->radio->description = __('The description of the radio');
$mfields->radio->choices = array(
'yes' => 'Yes',
'no' => 'No',
'maybe' => 'Maybe'
);
$mfields->radio->std = 'yes';
$mfields->radio->type = 'radio';
//$mfields->radio->section = 'multi';
// select field
$mfields->select->id = 'demo_select';
$mfields->select->title = __('Select');
$mfields->select->description = __('The description of select');
$mfields->select->choices = array(
'' => 'Please select',
'yes' => 'Yes',
'no' => 'No',
'maybe' => 'Maybe'
);
$mfields->select->std = '';
$mfields->select->type = 'select';
//$mfields->select->section = 'multi';
// field multicheckbox
$mfields->mcheckbox->id = 'demo_multicheckbox';
$mfields->mcheckbox->title = __('Multi checkbox');
$mfields->mcheckbox->description = __('The description of multi checkbox');
$mfields->mcheckbox->choices = array(
'yes' => 'Yes',
'no' => 'No',
'maybe' => 'Maybe'
);
$mfields->mcheckbox->std = array( 'no', 'maybe' );
$mfields->mcheckbox->type = 'mcheckbox';
//$mfields->mcheckbox->section = 'multi';
// field multiselect
$mfields->mselect->id = 'demo_mselect';
$mfields->mselect->title = __('Multi-Select');
$mfields->mselect->description = __('The description of multi-select');
$mfields->mselect->choices = array(
'' => 'Please select',
'yes' => 'Yes',
'no' => 'No',
'maybe' => 'Maybe'
);
$mfields->mselect->std = array( 'yes', 'no' );
$mfields->mselect->size = 0;
$mfields->mselect->type = 'mselect';
// adding fields to section 'multi'
// notice that the section where to add the fields
// is set in method-call not in the field definitions
$optionspage->add_field( $mfields, 'multi' );
// create the optionspage
$optionspage->create_optionspage();
// copy the config for use in validate-callback
self::$optionspage = $optionspage;
}
/**
*
* Demo for settings-type 'custom'
* @param mixed $arg_one
* @param array $arg_two
* @return void
* @since 0.5
* @access public static
*/
public static function custom_callback( $arg_one, array $arg_two )
{
var_dump( $arg_one );
var_dump( $arg_two );
}
/**
*
* Validate the data send by formular
* @param array $input Data send by formular
* @return array $input Validated data
* @since 0.1
* @access public static
*/
public static function validate_input( $input )
{
/*
* Checkboxes send no value if they are NOT selected. This means, the array-value
* for the checkbox is not set. In this case, WordPress delete the option for the checkbox
* in the database.
* If there is no option set in database, but a standard-value is set, the class will
* take the standard-value as option for the checkbox.
* In short: If there is no option-value, the standard-value will be used
*
* For unselected checkboxes we have to set a value of 0|false|mettigel or something
* else than 'on'.
*
* If the checkbox IS selected, there will be a array-value 'on' and we do not have
* to care about the rest.
*
*/
/*
* this is a bit tricky. first we get the object with all sections. each section have an
* array with it's fields. in a second step we extract the fields from the sections and
* copy them into another array that will be used in the method-call
*/
$config = self::$optionspage->get_config();
$fields = array();
foreach( $config->sections as $section ){
foreach( $section->fields as $field ){
array_push( $fields, (array) $field );
}
}
$input = call_user_func_array( array( self::$optionspage, 'check_checkboxes' ), array( $fields, $input ) );
return $input;
}
}
}