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
|
<?php
/* class pluginbuddy_settings * @author Dustin Bolton * * Handles setting up and parsing submitted data for settings pages. Uses form class for handling forms. * If a savepoint is passed to the constructor then settings will be auto-saved on save. * If false is passed to the savepoint then the process() function may be used to validate and grab submitted form data for custom processing. * @see pluginbuddy_form * */ class pb_backupbuddy_settings { // ********** PUBLIC PROPERTIES ********** // ********** PRIVATE PROPERTIES ********** private $_form; private $_form_name = ''; private $_prefix = ''; private $_savepoint; private $_settings = array(); private $_custom_title_width = ''; // ********** FUNCTIONS ********** /* pluginbuddy_settings->__construct() * * Default constructor. * * @param string $form_name Name / slug of the form. * @param string $save_point_or_custom_mode Location in pb_backupbuddy::$options array to save to. Ex: groups#5 saves into: pb_backupbuddy::$options['groups'][5]. * false If false the process() function will not save but will return results instead including form name => value pairs in an array for processing. * array If array then these will be treated as the defaults. Works the same as being false other than this. * @param string $additional_query_string Additional querystring variables to pass in the form action URL. * @param int $custom_title_width Custom title width in pixels. Formats table sizing. * @return null */ function __construct( $form_name, $save_point_or_custom_mode, $additional_query_string = '', $custom_title_width = '' ) { $this->_form_name = $form_name; $this->_prefix = 'pb_' . pb_backupbuddy::settings( 'slug' ) . '_'; $this->_savepoint = $save_point_or_custom_mode; $this->_custom_title_width = $custom_title_width; // TODO: no need to pass savepoint here? below: $this->_form = new pb_backupbuddy_form( $form_name, $save_point_or_custom_mode, $additional_query_string ); } // End __construct(). /* pluginbuddy_settings->add_setting() * * Register and add a setting to the settings form system. * * @param array $settings Array of settings for this added setting. See $default_settings for list of options that can be defined. * @return */ function add_setting( $settings ) { $default_settings = array ( 'type' => '', 'name' => '', 'title' => '', 'tip' => '', 'css' => '', 'before' => '', 'after' => '', 'rules' => '', 'default' => '', // IMPORTANT: Overrides default array. Also useful if savepoint is === false to override. 'options' => array(), 'orientation' => 'horizontal', // Used by radio and checkboxes. TODO: still need to add to checkboxes. 'classes' => '', // String of additional classes. 'row_class' => '', // Class to apply to row td's in row. ); $settings = array_merge( $default_settings, $settings ); $this->_settings[] = $settings; // Figure out defaults. if ( $settings['default'] != '' ) { // Default was passed to add_setting(). $default_value = $settings['default']; } else { // No default explictly set. $savepoint = $this->_savepoint; $raw_name = $settings['name']; if ( stristr( $settings['name'], '#' ) ); if ( false !== ( $last_hashpoint = strrpos( $settings['name'], '#' ) ) ) { $temp_savepoint = substr( $settings['name'], 0, $last_hashpoint ); if ( ( $savepoint === false ) || ( $savepoint == '' ) ) { $savepoint = $temp_savepoint; } else { $savepoint = $savepoint . '#' . $temp_savepoint; } $raw_name = substr( $settings['name'], $last_hashpoint + 1 ); // Item name with savepoint portion stripped out. } if ( $savepoint !== false ) { if ( is_array( $savepoint ) ) { // Array of defaults was passed instead of savepoint. $default_value = $savepoint[ $raw_name ]; } else { // No defaults provided, seek them out in plugins options array. // Default values are overwritten after a process() run with the latest data if a form was submitted. $group = pb_backupbuddy::get_group( $savepoint ); if ( $group === false ) { $default_value = ''; } else { if ( isset( $group[ $raw_name ] ) ) { // Default is defined. $default_value = $group[ $raw_name ]; } else { // Default not defined. $default_value = ''; } } } // end finding defaults in plugin options. } else { // Custom mode without a savepoint provided so no default set unless passed to add_setting(). $default_value = ''; } } // Process adding form item for the setting based on type. switch( $settings['type'] ) { case 'text': $this->_form->text( $settings['name'], $default_value, $settings['rules'] ); break; case 'plaintext': $this->_form->plaintext( $settings['name'], $default_value ); break; case 'color': $this->_form->color( $settings['name'], $default_value, $settings['rules'] ); break; case 'hidden': $this->_form->hidden( $settings['name'], $default_value, $settings['rules'] ); break; case 'wysiwyg': $this->_form->wysiwyg( $settings['name'], $default_value, $settings['rules'], $settings['settings'] ); break; case 'textarea': $this->_form->textarea( $settings['name'], $default_value, $settings['rules'] ); break; case 'select': $this->_form->select( $settings['name'], $settings['options'], $default_value, $settings['rules'] ); break; case 'password': $this->_form->password( $settings['name'], $default_value, $settings['rules'] ); break; case 'radio': $this->_form->radio( $settings['name'], $settings['options'], $default_value, $settings['rules'] ); break; case 'checkbox': $this->_form->checkbox( $settings['name'], $settings['options'], $default_value, $settings['rules'] ); break; case 'submit': $this->_form->submit( $settings['name'], 'DEFAULT' ); // Submit button text is set in display_settings() param. break; case 'title': $this->_form->title( $settings['name'], $default_value, $settings['rules'] ); // Submit button text is set in display_settings() param. break; default: echo '{Error: Unknown settings type.}'; break; } // End switch(). } // End add_setting(). /* pluginbuddy_settings->process() * * Processes the form if applicable (if it was submitted). * TODO: Perhaps add callback ability to this? * This must come after all form elements have been added. * This should usually happen in the controller prior to loading a view. * IMPORTANT: Applies trim() to all submitted form values! * * @return null/array When a savepoint was defined in class constructor nothing is returned. (normal operation) * When savepoint === false an array is returned for custom form processing. * Format: array( 'errors' => false/array, 'data' => array( 'form_keys' => 'form_values' ) ). */ public function process() { if ( ( '' != ( $form_name = pb_backupbuddy::_POST( $this->_prefix ) ) ) && ( pb_backupbuddy::_POST( $this->_prefix ) == $this->_form_name ) ) { // This form was indeed submitted. PROCESS IT! // TODO: $errors = array(); $_posts = pb_backupbuddy::_POST(); // Cleanup foreach( $_posts as &$post_value ) { $post_value = trim( $post_value ); } // loop through all posted variables, if its prefix matches this form's name then foreach( $_posts as $post_name => $post_value ) { if ( substr( $post_name, 0, strlen( $this->_prefix ) ) == $this->_prefix ) { // This settings form. $item_name = substr( $post_name, strlen( $this->_prefix ) ); if ( ( $item_name != '' ) && ( $item_name != 'settings_submit' ) ) { // Skip the form name input; also settings submit button since it is not registered until view. if ( true !== ( $test_result = $this->_form->test( $item_name, $post_value ) ) ) { foreach( $this->_settings as $setting_index => $setting ) { if ( $setting['type'] == 'title' ) { continue; } if ( $setting['name'] == $item_name ) { $this->_settings[$setting_index]['error'] = true; $item_title = $this->_settings[$setting_index]['title']; } } $errors[] = 'Validation failure on `' . $item_title . '`: ' . implode( ' ', $test_result ); unset( $_posts[$post_name] ); // Removes this form item so it will not be updated during save later. } else { // Item validated. Remove prefix for later processing. $_posts[$item_name] = $_posts[$post_name]; $this->_form->set_value( $item_name, $post_value ); // Set value to be equal to submitted value so if one or more item failed validation the entire form is not wiped out. Don't want user to have to re-enter valid data. unset( $_posts[$post_name] ); } } else { // Submit button. Can unset it to clean up array for later. unset( $_posts[$post_name] ); } } else { // Not for this form. Can unset it to clean up array for later. unset( $_posts[$post_name] ); } } // Process! // Only save if in normal settings mode; if savepoint === false no saving here. if ( ( $this->_savepoint === false ) || is_array( $this->_savepoint ) ) { //foreach( $_posts as $post_name => $post_value ) { // Validation above should haveo only left items for this form. Strip prefixes before passing on. // $post_name = substr( $post_name, strlen( $this->_prefix ) ); //} $return = array( 'errors' => $errors, 'data' => $_posts, ); return $return; } else { // Normal settings since savepoint !== false. Save into savepoint! if ( count( $errors ) > 0 ) { // Errors. pb_backupbuddy::alert( 'Error validating one or more fields as indicated below. Error(s):<br>' . implode( '<br>', $errors ), true ); } // Prepare savepoint. if ( $this->_savepoint != '' ) { $savepoint_root = $this->_savepoint . '#'; } else { $savepoint_root = ''; } // The hard work. foreach( $_posts as $post_name => $post_value ) { // Loop through all post items (not all may be our form). @see 83429594837534987. //if ( substr( $post_name, 0, strlen( $this->_prefix ) ) == $this->_prefix ) { // If prefix matches, its this form. //$post_name = substr( $post_name, strlen( $this->_prefix ) ); // Set stripped prefix. //if ( $post_name != '' ) { // Skips the empty prefix name we used for verifying form. @see 83429594837534987. // Update form item value. $this->_form->set_value( $post_name, $post_value ); // From old save_settings(): $savepoint_subsection = &pb_backupbuddy::$options; $savepoint_levels = explode( '#', $savepoint_root . $post_name ); foreach ( $savepoint_levels as $savepoint_level ) { $savepoint_subsection = &$savepoint_subsection{$savepoint_level}; } // Apply settings. $savepoint_subsection = stripslashes_deep( $post_value ); // Remove WordPress' magic-quotes-nonsense. //} //} } // Add a note to the save alert that some values are skipped due to errors. $error_note = ''; if ( count( $errors ) > 0 ) { $error_note = ' One or more fields skipped due to error.'; } pb_backupbuddy::save(); pb_backupbuddy::alert( __( 'Settings saved.' . $error_note, 'it-l10n-backupbuddy' ) ); $return = array( 'errors' => $errors, 'data' => $_posts, ); return $return; //} // end no errors. } // End if savepoint !=== false. } // end submitted form. } // End process(). /* pluginbuddy_settings->display_settings() * * Displays all the registered settings in this object. Entire form and HTML is echo'd out. * @see pluginbuddy_settings->get_settings() * * @param string $submit_button_title Text to display in the submit button. * @param string $before Content before submit after. * @param string $after Content after submit button. * @return null */ public function display_settings( $submit_button_title, $before = '', $after = '', $save_button_class = '' ) { echo $this->get_settings( $submit_button_title, $before, $after, $save_button_class ); } // End display_settings(). /* pluginbuddy_settings->get_settings() * * Returns all the registered settings in this object. Entire form and HTML is returned. * @see pluginbuddy_settings->display_settings() * radio button additional options: orientation [ vertical / horizontal ] * * @param string $submit_button_title Text to display in the submit button. * @param string $before Content before submit after. * @param string $after Content after submit button. * @return string Returns entire string with everything in it to display. */ public function get_settings( $submit_button_title, $before = '', $after = '', $save_button_class = '' ) { $first_title = true; // first title's CSS top padding differs so must track. $return = $this->_form->start(); $return .= '<table class="form-table">'; // style="max-width: 675px;">'; foreach ( $this->_settings as $settings ) { $th_css = ''; if ( $settings['title'] == '' ) { // blank title so hide left column. $th_css .= ' display: none;'; } if ( $settings['type'] == 'title' ) { // Title item. if ( $first_title === true ) { // First title in list. $return .= '<tr style="border: 0;"><th colspan="2" style="border: 0; padding-top: 0; padding-bottom: 0;" class="' . $settings['row_class'] . '"><h3 class="title"'; $return .= ' style="margin-top: 0; margin-bottom: 0.5em;"'; $first_title = false; } else { // Subsequent titles. $return .= '<tr style="border: 0;"><th colspan="2" style="border: 0;" class="' . $settings['row_class'] . '"><h3 class="title"'; $return .= ' style="margin: 0.5em 0;"'; } $return .= '>' . $settings['title'] . '</h3></th>'; } elseif ( $settings['type'] == 'hidden' ) { // hidden form item. no title. $return .= $this->_form->get( $settings['name'], $settings['css'], $settings['classes'] ); } else { // Normal item. $return .= '<tr class="' . $settings['row_class'] . '">'; $return .= '<th scope="row" class="' . $settings['row_class'] . '"'; if ( $this->_custom_title_width != '' ) { $return .= ' style="width: ' . $this->_custom_title_width . 'px; ' . $th_css . '"'; } else { $return .= ' style="' . $th_css . '"'; } $return .= '>'; $return .= $settings['title']; if ( isset( $settings['tip'] ) && ( $settings['tip'] != '' ) ) { $return .= pb_backupbuddy::$ui->tip( $settings['tip'], '', false ); } $return .= '</th>'; if ( $settings['type'] == 'title' ) { // Extend width full length for title item. $return .= ' colspan="2"'; } $return .= '<td class="' . $settings['row_class'] . '"'; if ( $settings['title'] == '' ) { // No title so hide left column. $return .= ' colspan="2"'; } $return .= '>'; $return .= $settings['before']; if ( isset( $settings['error'] ) && ( $settings['error'] === true ) ) { $settings['css'] .= 'border: 1px dashed red;'; } $return .= $this->_form->get( $settings['name'], $settings['css'], $settings['classes'], $settings['orientation'] ); $return .= $settings['after']; $return .= '</td>'; $return .= '</tr>'; } /* $form->add_setting( array( 'type' => 'text', 'name' => 'image_width', 'title' => 'Image Width', 'tip' => 'This controls the size of the images in the Carousel. Images will be generated from the original images uploaded. Images will not be upscaled larger than the originals. You may change this at any time.', 'css' => 'text-align: right;', 'after' => 'px', 'rules' => 'required', ) ); */ } $return .= '</table><br>';
// Submit button $return .= $before; $return .= $this->_form->submit( 'settings_submit', $submit_button_title, $save_button_class ); $return .= $this->_form->get( 'settings_submit', '', $save_button_class ); //, $settings['name'], $settings['classes'] ); $return .= $after;
$return .= $this->_form->end();
return $return; } // End get_settings(). /* clear_values() * * Clears the value of all form items setting the value to an empty string ''. * * @return null */ public function clear_values() { $this->_form->clear_values(); return; } // End clear_values(). /* set_value() * * Replace the value of a form item. * * @param string $item_name Name of the form setting item to update. * @param string $value Value to set the item to. * @return null */ public function set_value( $item_name, $value ) { $this->_form->set_value( $item_name, $value ); return; } // End set_value(). } // End class pluginbuddy_settings.
?>
|