PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.1.36
Gallery : FooGallery v3.1.36
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / foopluginbase / classes / class-foo-plugin-settings.php
foogallery / includes / foopluginbase / classes Last commit date
class-foo-friendly-dates.php 2 months ago class-foo-plugin-base.php 2 months ago class-foo-plugin-file-locator.php 2 months ago class-foo-plugin-metabox-sanity.php 2 months ago class-foo-plugin-options.php 2 months ago class-foo-plugin-settings.php 2 months ago class-foo-plugin-textdomain.php 2 months ago index.php 2 months ago
class-foo-plugin-settings.php
502 lines
1 <?php
2 /*
3 * Foo Plugin Settings
4 *
5 * A helpful class to handle settings for a plugin
6 *
7 * Version: 2.1
8 * Author: Brad Vincent
9 * Author URI: http://fooplugins.com
10 * License: GPL2
11 */
12
13 if ( !class_exists( 'Foo_Plugin_Settings_v2_2' ) ) {
14 class Foo_Plugin_Settings_v2_2 {
15
16 protected $plugin_slug;
17
18 protected $_settings = array(); //the plugin settings array
19 protected $_settings_sections = array(); //the plugin sections array
20 protected $_settings_tabs = array(); //the plugin tabs array
21 protected $_admin_errors = false; //store of admin errors
22
23 /**
24 * Constructor for the Foo_Plugin_Settings_v2_2 class
25 *
26 * @param string $plugin_slug The slug of the plugin.
27 */
28 public function __construct( $plugin_slug ) {
29 $this->plugin_slug = $plugin_slug;
30
31 $this->register_settings();
32 }
33
34 /**
35 * Get the tabs of the plugin settings
36 *
37 * @return array
38 */
39 public function get_tabs() {
40 return $this->_settings_tabs;
41 }
42
43 /**
44 * Check if there is any setting of a certain type
45 *
46 * @param string $type The type of setting to check.
47 * @return bool
48 */
49 public function has_setting_of_type( $type ) {
50 foreach ( $this->_settings as $setting ) {
51 if ( $setting['type'] == $type ) {
52 return true;
53 }
54 }
55
56 return false;
57 }
58
59 /**
60 * Check if a tab with a specific ID exists
61 *
62 * @param string $tab_id The ID of the tab to check.
63 * @return bool
64 */
65 public function has_tab( $tab_id ) {
66 return array_key_exists( $tab_id, $this->_settings_tabs );
67 }
68
69 /**
70 * Add a setting tab
71 *
72 * @param string $tab_id The ID of the tab.
73 * @param string $title The title of the tab.
74 */
75 function add_tab( $tab_id, $title ) {
76 if ( !$this->has_tab( $tab_id ) ) {
77
78 //pre action
79 do_action( $this->plugin_slug . '_admin_settings_before_tab', $tab_id, $title );
80
81 $tab = array(
82 'id' => $tab_id,
83 'title' => $title
84 );
85
86 $this->_settings_tabs[$tab_id] = $tab;
87
88 //post action
89 do_action( $this->plugin_slug . '_admin_settings_after_tab', $tab_id, $title );
90 }
91 }
92
93 /**
94 * Check if a section with a specific ID exists
95 *
96 * @param string $section_id The ID of the section.
97 * @return bool
98 */
99 function has_section( $section_id ) {
100 return array_key_exists( $section_id, $this->_settings_sections );
101 }
102
103 /**
104 * Add a setting section
105 *
106 * @param string $section_id The ID of the section.
107 * @param string $title The title of the section.
108 * @param string $desc The description of the section (default is an empty string).
109 */
110 function add_section( $section_id, $title, $desc = '' ) {
111
112 //check we have the section
113 if ( !$this->has_section( $section_id ) ) {
114
115 //pre action
116 do_action( $this->plugin_slug . '_admin_settings_before_section', $section_id, $title, $desc );
117
118 $section = array(
119 'id' => $section_id,
120 'title' => $title,
121 'desc' => $desc
122 );
123
124 $this->_settings_sections[$section_id] = $section;
125
126 add_settings_section( $section_id, $title, array( $this, 'echo_section_desc' ), $this->plugin_slug );
127
128 //post action
129 do_action( $this->plugin_slug . '_admin_settings_after_section', $section_id, $title, $desc );
130 }
131 }
132
133 /**
134 * Echo the description of a section
135 *
136 * @param array $arg An array of arguments passed to the callback.
137 */
138 function echo_section_desc( $arg ) {
139 $section = $this->_settings_sections[ $arg['id'] ];
140 echo wp_kses_post( $section['desc'] );
141 }
142
143 /**
144 * Add a section to a tab
145 *
146 * @param string $tab_id The ID of the tab.
147 * @param string $section_id The ID of the section.
148 * @param string $title The title of the section.
149 * @param string $desc The description of the section (default is an empty string).
150 * @return string The ID of the section.
151 */
152 function add_section_to_tab( $tab_id, $section_id, $title, $desc = '' ) {
153 if ( array_key_exists( $tab_id, $this->_settings_tabs ) ) {
154
155 //get the correct section id for the tab
156 $section_id = $tab_id . '-' . $section_id;
157
158 //add the section to the tab
159 if ( !array_key_exists( $section_id, $this->_settings_sections ) ) {
160 $this->_settings_tabs[$tab_id]['sections'][$section_id] = $section_id;
161 }
162
163 //add the section
164 $this->add_section( $section_id, $title, $desc );
165
166 }
167
168 return $section_id;
169 }
170
171 /**
172 * Add settings based on the provided configuration.
173 *
174 * @param array|bool $settings The settings configuration.
175 */
176 function add_settings( $settings = false ) {
177 if ( !is_array( $settings ) || ( !array_key_exists( 'settings', $settings ) ) ) return;
178
179 foreach( $settings['settings'] as $setting ) {
180 //add a tab if needed
181 $tab_id = foo_safe_get( $setting, 'tab', false );
182
183 if ( $tab_id !== false && !$this->has_tab( $tab_id ) && array_key_exists( 'tabs', $settings ) && array_key_exists( $tab_id, $settings['tabs'] ) ) {
184 $tab = $settings['tabs'][$tab_id];
185 $this->add_tab( $tab_id, $tab );
186 }
187
188 //add a section if needed
189 $section_id = foo_safe_get( $setting, 'section', false );
190 if ( $section_id !== false && !$this->has_section( $section_id ) && array_key_exists( 'sections', $settings ) && array_key_exists( $section_id, $settings['sections'] ) ) {
191 $section = $settings['sections'][$section_id];
192 $this->add_section_to_tab( $tab_id, $section_id, $section['name'] );
193 }
194
195 $this->add_setting( $setting );
196 }
197 }
198
199 /**
200 * Register settings for the plugin.
201 */
202 function register_settings() {
203 register_setting( $this->plugin_slug, $this->plugin_slug, array( 'sanitize_callback' => array( $this, 'validate' ) ) );
204 }
205
206 /**
207 * Add a settings field.
208 *
209 * @param array $args The arguments for the setting.
210 */
211 function add_setting( $args = array() ) {
212
213 $defaults = array(
214 'id' => 'default_field',
215 'title' => 'Default Field',
216 'desc' => '',
217 'default' => '',
218 'placeholder' => '',
219 'type' => 'text',
220 'section' => '',
221 'choices' => array(),
222 'class' => '',
223 'tab' => ''
224 );
225
226 //only declare up front so no debug warnings are shown
227 $title = $type = $id = $desc = $default = $placeholder = $choices = $class = $section = $tab = null;
228
229 extract( wp_parse_args( $args, $defaults ) );
230
231 $field_args = array(
232 'type' => $type,
233 'id' => $id,
234 'desc' => $desc,
235 'default' => $default,
236 'placeholder' => $placeholder,
237 'choices' => $choices,
238 'label_for' => $id,
239 'class' => $class
240 );
241
242 $this->_settings[] = $args;
243
244 $section_id = foo_convert_to_key( $section );
245
246 //check we have the tab
247 if ( !empty( $tab ) ) {
248 $tab_id = foo_convert_to_key( $tab );
249
250 //add the tab
251 $this->add_tab( $tab_id, foo_title_case( $tab ) );
252
253 //add the section
254 $section_id = $this->add_section_to_tab( $tab_id, $section_id, foo_title_case( $section ) );
255 } else {
256 //just add the section
257 $this->add_section( $section_id, foo_title_case( $section ) );
258 }
259
260 do_action( $this->plugin_slug . '_admin_settings_before_setting', $args );
261
262 //add the setting!
263 add_settings_field( $id, $title, array( $this, 'render' ), $this->plugin_slug, $section_id, $field_args );
264
265 do_action( $this->plugin_slug . '_admin_settings_after_setting', $args );
266 }
267
268 /**
269 * Render HTML for individual settings.
270 *
271 * @param array $args The arguments for rendering the setting.
272 */
273 function render( $args = array() ) {
274
275 //only declare up front so no debug warnings are shown
276 $type = $id = $desc = $default = $placeholder = $choices = $class = $section = $tab = null;
277
278 extract( $args );
279
280 $options = get_option( $this->plugin_slug );
281
282 $has_options = $options !== false;
283
284 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
285 // If we are in the network settings, use site options directly.
286 if ( is_network_admin() ) {
287 $options = get_site_option( $this->plugin_slug );
288 } else {
289 $site_options = get_site_option( $this->plugin_slug );
290 $options = is_array( $options ) ? wp_parse_args( $options, $site_options ) : (array) $site_options;
291 }
292 } else {
293 $options = is_array( $options ) ? $options : array();
294 }
295
296 if ( $options === '' ) {
297 $options = array();
298 }
299
300 if ( !isset( $options[$id]) && $type != 'checkbox' ) {
301 $options[$id] = $default;
302 }
303
304 $field_class = '';
305 if ( $class != '' ) {
306 $field_class = ' class="' . $class . '"';
307 }
308
309 $errors = get_settings_errors( $id );
310
311 do_action( $this->plugin_slug . '_admin_settings_before_render_setting', $args );
312
313 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Form field generation with internal settings
314 switch ( $type ) {
315
316 case 'heading':
317 echo '</td></tr><tr valign="top"><td colspan="2">' . $desc;
318 break;
319
320 case 'html':
321 echo $desc;
322 break;
323
324 case 'checkbox':
325 $checked = '';
326 if ( isset( $options[$id] ) && $options[$id] == 'on' ) {
327 $checked = ' checked="checked"';
328 } else if ( $options === false && $default == 'on' ) {
329 $checked = ' checked="checked"';
330 } else if ( $has_options === false && $default == 'on' ) {
331 $checked = ' checked="checked"';
332 }
333
334 //echo '<input type="hidden" name="'.$this->plugin_slug.'[' . $id . '_default]" value="' . $default . '" />';
335 echo '<input' . $field_class . ' type="checkbox" id="' . $id . '" name="' . $this->plugin_slug . '[' . $id . ']" value="on"' . $checked . ' /> <label for="' . $id . '"><small>' . $desc . '</small></label>';
336
337 break;
338
339 case 'select':
340 echo '<select' . $field_class . ' name="' . $this->plugin_slug . '[' . $id . ']">';
341
342 foreach ( $choices as $value => $label ) {
343 $selected = '';
344 if ( $options[$id] == $value ) {
345 $selected = ' selected="selected"';
346 }
347 echo '<option ' . $selected . ' value="' . $value . '">' . $label . '</option>';
348 }
349
350 echo '</select>';
351
352 break;
353
354 case 'radio':
355 $i = 0;
356 $saved_value = $options[$id];
357 if ( empty( $saved_value ) ) {
358 $saved_value = $default;
359 }
360 foreach ( $choices as $value => $label ) {
361 $selected = '';
362 if ( $saved_value == $value ) {
363 $selected = ' checked="checked"';
364 }
365 echo '<input' . $field_class . $selected . ' type="radio" name="' . $this->plugin_slug . '[' . $id . ']" id="' . $id . $i . '" value="' . $value . '"> <label for="' . $id . $i . '">' . $label . '</label>';
366 if ( $i < count( $choices ) - 1 ) {
367 echo '<br />';
368 }
369 $i++;
370 }
371
372 break;
373
374 case 'textarea':
375 echo '<textarea' . $field_class . ' id="' . $id . '" name="' . $this->plugin_slug . '[' . $id . ']" placeholder="' . $placeholder . '">' . esc_textarea( $options[$id] ) . '</textarea>';
376
377 break;
378
379 case 'password':
380 echo '<input' . $field_class . ' type="password" id="' . $id . '" name="' . $this->plugin_slug . '[' . $id . ']" value="' . esc_attr( $options[$id] ) . '" />';
381
382 break;
383
384 case 'text':
385 echo '<input class="regular-text ' . esc_attr( $class ) . '" type="text" id="' . esc_attr( $id ) . '" name="' . esc_attr( $this->plugin_slug ) . '[' . esc_attr( $id ) . ']" placeholder="' . esc_attr( $placeholder ) . '" value="' . esc_attr( foogallery_sanitize_javascript( $options[$id], array() ) ) . '" />';
386
387 break;
388
389 case 'checkboxlist':
390 $i = 0;
391 foreach ( $choices as $value => $label ) {
392
393 $checked = '';
394 if ( isset( $options[$id][$value] ) && $options[$id][$value] == 'true' ) {
395 $checked = 'checked="checked"';
396 }
397
398 echo '<input' . $field_class . ' ' . $checked . ' type="checkbox" name="' . $this->plugin_slug . '[' . $id . '|' . $value . ']" id="' . $id . $i . '" value="on"> <label for="' . $id . $i . '">' . $label . '</label>';
399 if ( $i < count( $choices ) - 1 ) {
400 echo '<br />';
401 }
402 $i++;
403 }
404
405 break;
406 case 'image':
407 echo '<input class="regular-text image-upload-url" type="text" id="' . $id . '" name="' . $this->plugin_slug . '[' . $id . ']" placeholder="' . $placeholder . '" value="' . esc_attr( $options[$id] ) . '" />';
408 echo '<input data-uploader-title="' . __('Select An Image', $this->plugin_slug) . '" data-link="' . $id . '" class="image-upload-button" type="button" name="upload_button" value="' . __( 'Select Image', $this->plugin_slug ) . '" />';
409 break;
410
411 default:
412 do_action( $this->plugin_slug . '_admin_settings_custom_type_render_setting', $args );
413 break;
414 }
415
416 do_action( $this->plugin_slug . '_admin_settings_after_render_setting', $args );
417
418 if ( is_array( $errors ) ) {
419 foreach ( $errors as $error ) {
420 echo "<span class='error'>{$error['message']}</span>";
421 }
422 }
423
424 if ( $type != 'checkbox' && $type != 'heading' && $type != 'html' && $desc != '' ) {
425 echo '<br /><small>' . $desc . '</small>';
426 }
427 // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
428 }
429
430 /**
431 * Validate the input settings.
432 *
433 * @param array $input The input settings to validate.
434 *
435 * @return array The validated settings.
436 */
437 function validate( $input ) {
438
439 //check to see if the options were reset
440 if ( isset ( $input['reset-defaults']) ) {
441 delete_option( $this->plugin_slug );
442 add_settings_error(
443 'reset',
444 'reset_error',
445 __( 'Settings restored to default values', $this->plugin_slug ),
446 'updated'
447 );
448
449 return false;
450 }
451
452 if ( empty( $this->_settings ) ) {
453 $settings = apply_filters( $this->plugin_slug . '_admin_settings', false );
454 $this->add_settings( $settings );
455 }
456
457 foreach ( $this->_settings as $setting ) {
458 $this->validate_setting( $setting, $input );
459 }
460
461 return $input;
462 }
463
464 /**
465 * Validate a single setting.
466 *
467 * @param array $setting The setting configuration.
468 * @param array $input The input settings to validate.
469 */
470 function validate_setting( $setting, &$input ) {
471 //validate a single setting
472
473 if ( $setting['type'] == 'checkbox' ) {
474 if ( ! empty( $input[ $setting['id'] ] ) ) {
475 $input[ $setting['id'] ] = 'on';
476 } else if ( isset( $setting['default'] ) && 'on' === $setting['default'] ) {
477 $input[ $setting['id'] ] = 'off';
478 }
479 }
480
481 if ( $setting['type'] == 'checkboxlist' ) {
482
483 unset( $checkboxarray);
484
485 foreach ( $setting['choices'] as $value => $label ) {
486 if ( !empty( $input[$setting['id'] . '|' . $value]) ) {
487 // If it's not null, make sure it's true, add it to an array
488 $checkboxarray[$value] = 'true';
489 } else {
490 $checkboxarray[$value] = 'false';
491 }
492 }
493
494 if ( !empty( $checkboxarray) ) {
495 $input[$setting['id']] = $checkboxarray;
496 }
497
498 }
499 }
500 }
501 }
502