PluginProbe
WProofreader spell & grammar check plugin for WordPress / trunk
WProofreader spell & grammar check plugin for WordPress vtrunk
3.2.1 3.2.2 3.2.0 trunk 1.0 1.1 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.6.1 2.6.10 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 All 28 releases
webspellchecker / vendor / class.settings-api.php

class.settings-api.php in WProofreader spell & grammar check plugin for WordPress trunk, at vendor/class.settings-api.php

623 lines 22.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( !class_exists( 'WSC_Settings_API' ) ):
4 class WSC_Settings_API {
5
6 /**
7 * settings sections array
8 *
9 * @var array
10 */
11 protected $settings_sections = array();
12
13 /**
14 * Settings fields array
15 *
16 * @var array
17 */
18 protected $settings_fields = array();
19
20 public function __construct() {
21 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
22 }
23
24 /**
25 * Enqueue scripts and styles
26 */
27 function admin_enqueue_scripts() {
28 wp_enqueue_style( 'wp-color-picker' );
29
30 wp_enqueue_media();
31 wp_enqueue_script( 'wp-color-picker' );
32 wp_enqueue_script( 'jquery' );
33 }
34
35 /**
36 * Set settings sections
37 *
38 * @param array $sections setting sections array
39 */
40 function set_sections( $sections ) {
41 $this->settings_sections = $sections;
42
43 return $this;
44 }
45
46 /**
47 * Add a single section
48 *
49 * @param array $section
50 */
51 function add_section( $section ) {
52 $this->settings_sections[] = $section;
53
54 return $this;
55 }
56
57 /**
58 * Set settings fields
59 *
60 * @param array $fields settings fields array
61 */
62 function set_fields( $fields ) {
63 $this->settings_fields = $fields;
64
65 return $this;
66 }
67
68 function add_field( $section, $field ) {
69 $defaults = array(
70 'name' => '',
71 'label' => '',
72 'desc' => '',
73 'type' => 'text'
74 );
75
76 $arg = wp_parse_args( $field, $defaults );
77 $this->settings_fields[$section][] = $arg;
78
79 return $this;
80 }
81
82 /**
83 * Initialize and registers the settings sections and fileds to WordPress
84 *
85 * Usually this should be called at `admin_init` hook.
86 *
87 * This function gets the initiated settings sections and fields. Then
88 * registers them to WordPress and ready for use.
89 */
90 function admin_init() {
91 //register settings sections
92 foreach ( $this->settings_sections as $section ) {
93 if ( false == get_option( $section['id'] ) ) {
94 add_option( $section['id'] );
95 }
96
97 if ( isset($section['desc']) && !empty($section['desc']) ) {
98 $desc = '<div class="inside">' . $section['desc'] . '</div>';
99 $callback = function () use ( $desc ) {
100 echo wp_kses_post( $desc );
101 };
102 } else if ( isset( $section['callback'] ) ) {
103 $callback = $section['callback'];
104 } else {
105 $callback = null;
106 }
107
108 add_settings_section( $section['id'], $section['title'], $callback, $section['id'] );
109 }
110
111 //register settings fields
112 foreach ( $this->settings_fields as $section => $field ) {
113 foreach ( $field as $option ) {
114
115 $name = $option['name'];
116 $type = isset( $option['type'] ) ? $option['type'] : 'text';
117 $label = isset( $option['label'] ) ? $option['label'] : '';
118 $callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type );
119
120 $args = array(
121 'id' => $name,
122 'label_for' => "{$section}[{$name}]",
123 'desc' => isset( $option['desc'] ) ? $option['desc'] : '',
124 'name' => $label,
125 'section' => $section,
126 'size' => isset( $option['size'] ) ? $option['size'] : null,
127 'options' => isset( $option['options'] ) ? $option['options'] : '',
128 'std' => isset( $option['default'] ) ? $option['default'] : '',
129 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '',
130 'type' => $type,
131 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '',
132 'min' => isset( $option['min'] ) ? $option['min'] : '',
133 'max' => isset( $option['max'] ) ? $option['max'] : '',
134 'step' => isset( $option['step'] ) ? $option['step'] : '',
135 );
136
137 add_settings_field( "{$section}[{$name}]", $label, $callback, $section, $section, $args );
138 }
139 }
140
141 // creates our settings in the options table
142 foreach ( $this->settings_sections as $section ) {
143 register_setting( $section['id'], $section['id'], array( $this, 'sanitize_options' ) );
144 }
145 }
146
147 /**
148 * Get field description for display
149 *
150 * @param array $args settings field args
151 */
152 public function get_field_description( $args ) {
153 if ( ! empty( $args['desc'] ) ) {
154 $desc = sprintf( '<p class="description">%s</p>', wp_kses_post( $args['desc'] ) );
155 } else {
156 $desc = '';
157 }
158
159 return $desc;
160 }
161
162 /**
163 * Displays a text field for a settings field
164 *
165 * @param array $args settings field args
166 */
167 function callback_text( $args ) {
168
169 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
170 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
171 $type = isset( $args['type'] ) ? $args['type'] : 'text';
172 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . esc_attr( $args['placeholder'] ) . '"';
173
174 $html = sprintf( '<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s/>', esc_attr( $type ), esc_attr( $size ), esc_attr( $args['section'] ), esc_attr( $args['id'] ), $value, $placeholder );
175 $html .= $this->get_field_description( $args );
176
177 echo $html;
178 }
179
180 /**
181 * Displays a url field for a settings field
182 *
183 * @param array $args settings field args
184 */
185 function callback_url( $args ) {
186 $this->callback_text( $args );
187 }
188
189 /**
190 * Displays a number field for a settings field
191 *
192 * @param array $args settings field args
193 */
194 function callback_number( $args ) {
195 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
196 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
197 $type = isset( $args['type'] ) ? $args['type'] : 'number';
198 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . esc_attr( $args['placeholder'] ) . '"';
199 $min = empty( $args['min'] ) ? '' : ' min="' . esc_attr( $args['min'] ) . '"';
200 $max = empty( $args['max'] ) ? '' : ' max="' . esc_attr( $args['max'] ) . '"';
201 $step = empty( $args['max'] ) ? '' : ' step="' . esc_attr( $args['step'] ) . '"';
202
203 $html = sprintf( '<input type="%1$s" class="%2$s-number" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s%7$s%8$s%9$s/>', esc_attr( $type ), esc_attr( $size ), esc_attr( $args['section'] ), esc_attr( $args['id'] ), $value, $placeholder, $min, $max, $step );
204 $html .= $this->get_field_description( $args );
205
206 echo $html;
207 }
208
209 /**
210 * Displays a checkbox for a settings field
211 *
212 * @param array $args settings field args
213 */
214 function callback_checkbox( $args ) {
215
216 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
217
218 $html = '<fieldset>';
219 $html .= sprintf( '<label for="wpuf-%1$s[%2$s]">', esc_attr( $args['section'] ), esc_attr( $args['id'] ) );
220 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', esc_attr( $args['section'] ), esc_attr( $args['id'] ) );
221 $html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', esc_attr( $args['section'] ), esc_attr( $args['id'] ), checked( $value, 'on', false ) );
222 $html .= sprintf( '%1$s</label>', wp_kses_post( $args['desc'] ) );
223 $html .= '</fieldset>';
224
225 echo $html;
226 }
227
228 /**
229 * Displays a multicheckbox a settings field
230 *
231 * @param array $args settings field args
232 */
233 function callback_multicheck( $args ) {
234
235 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
236 $html = '<fieldset>';
237 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="" />', esc_attr( $args['section'] ), esc_attr( $args['id'] ) );
238 foreach ( $args['options'] as $key => $label ) {
239 $checked = isset( $value[$key] ) ? $value[$key] : '0';
240 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', esc_attr( $args['section'] ), esc_attr( $args['id'] ), esc_attr( $key ) );
241 $html .= sprintf( '<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', esc_attr( $args['section'] ), esc_attr( $args['id'] ), esc_attr( $key ), checked( $checked, $key, false ) );
242 $html .= sprintf( '%1$s</label><br>', esc_html( $label ) );
243 }
244
245 $html .= $this->get_field_description( $args );
246 $html .= '</fieldset>';
247
248 echo $html;
249 }
250
251 /**
252 * Displays a multicheckbox a settings field
253 *
254 * @param array $args settings field args
255 */
256 function callback_radio( $args ) {
257
258 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
259 $html = '<fieldset>';
260
261 foreach ( $args['options'] as $key => $label ) {
262 $html .= sprintf( '<label for="wpuf-%1$s[%2$s][%3$s]">', esc_attr( $args['section'] ), esc_attr( $args['id'] ), esc_attr( $key ) );
263 $html .= sprintf( '<input type="radio" class="radio" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', esc_attr( $args['section'] ), esc_attr( $args['id'] ), esc_attr( $key ), checked( $value, $key, false ) );
264 $html .= sprintf( '%1$s</label><br>', esc_html( $label ) );
265 }
266
267 $html .= $this->get_field_description( $args );
268 $html .= '</fieldset>';
269
270 echo $html;
271 }
272
273 /**
274 * Displays a selectbox for a settings field
275 *
276 * @param array $args settings field args
277 */
278 function callback_select( $args ) {
279
280 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
281 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
282 $html = sprintf( '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', esc_attr( $size ), esc_attr( $args['section'] ), esc_attr( $args['id'] ) );
283
284 foreach ( $args['options'] as $key => $label ) {
285 $html .= sprintf( '<option value="%s"%s>%s</option>', esc_attr( $key ), selected( $value, $key, false ), esc_html( $label ) );
286 }
287
288 $html .= sprintf( '</select>' );
289 $html .= $this->get_field_description( $args );
290
291 echo $html;
292 }
293
294 /**
295 * Displays a textarea for a settings field
296 *
297 * @param array $args settings field args
298 */
299 function callback_textarea( $args ) {
300
301 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
302 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
303 $placeholder = empty( $args['placeholder'] ) ? '' : ' placeholder="' . esc_attr( $args['placeholder'] ) . '"';
304
305 $html = sprintf( '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]"%4$s>%5$s</textarea>', esc_attr( $size ), esc_attr( $args['section'] ), esc_attr( $args['id'] ), $placeholder, $value );
306 $html .= $this->get_field_description( $args );
307
308 echo $html;
309 }
310
311 /**
312 * Displays a textarea for a settings field
313 *
314 * @param array $args settings field args
315 * @return string
316 */
317 function callback_html( $args ) {
318 echo $this->get_field_description( $args );
319 }
320
321 /**
322 * Displays a rich text textarea for a settings field
323 *
324 * @param array $args settings field args
325 */
326 function callback_wysiwyg( $args ) {
327
328 $value = $this->get_option( $args['id'], $args['section'], $args['std'] );
329 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : '500px';
330
331 echo '<div style="max-width: ' . esc_attr( $size ) . ';">';
332
333 $editor_settings = array(
334 'teeny' => true,
335 'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
336 'textarea_rows' => 10
337 );
338
339 if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
340 $editor_settings = array_merge( $editor_settings, $args['options'] );
341 }
342
343 wp_editor( $value, $args['section'] . '-' . $args['id'], $editor_settings );
344
345 echo '</div>';
346
347 echo $this->get_field_description( $args );
348 }
349
350 /**
351 * Displays a file upload field for a settings field
352 *
353 * @param array $args settings field args
354 */
355 function callback_file( $args ) {
356
357 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
358 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
359 $id = $args['section'] . '[' . $args['id'] . ']';
360 $label = isset( $args['options']['button_label'] ) ? $args['options']['button_label'] : __( 'Choose File' );
361
362 $html = sprintf( '<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', esc_attr( $size ), esc_attr( $args['section'] ), esc_attr( $args['id'] ), $value );
363 $html .= '<input type="button" class="button wpsa-browse" value="' . esc_attr( $label ) . '" />';
364 $html .= $this->get_field_description( $args );
365
366 echo $html;
367 }
368
369 /**
370 * Displays a password field for a settings field
371 *
372 * @param array $args settings field args
373 */
374 function callback_password( $args ) {
375
376 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
377 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
378
379 $html = sprintf( '<input type="password" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', esc_attr( $size ), esc_attr( $args['section'] ), esc_attr( $args['id'] ), $value );
380 $html .= $this->get_field_description( $args );
381
382 echo $html;
383 }
384
385 /**
386 * Displays a color picker field for a settings field
387 *
388 * @param array $args settings field args
389 */
390 function callback_color( $args ) {
391
392 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
393 $size = isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular';
394
395 $html = sprintf( '<input type="text" class="%1$s-text wp-color-picker-field" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s" data-default-color="%5$s" />', esc_attr( $size ), esc_attr( $args['section'] ), esc_attr( $args['id'] ), $value, esc_attr( $args['std'] ) );
396 $html .= $this->get_field_description( $args );
397
398 echo $html;
399 }
400
401 /**
402 * Sanitize callback for Settings API
403 *
404 * @return mixed
405 */
406 function sanitize_options( $options ) {
407
408 if ( !$options ) {
409 return $options;
410 }
411
412 foreach( $options as $option_slug => $option_value ) {
413 $sanitize_callback = $this->get_sanitize_callback( $option_slug );
414
415 // If callback is set, call it
416 if ( $sanitize_callback ) {
417 $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value );
418 continue;
419 }
420 }
421
422 return $options;
423 }
424
425 /**
426 * Get sanitization callback for given option slug
427 *
428 * @param string $slug option slug
429 *
430 * @return mixed string or bool false
431 */
432 function get_sanitize_callback( $slug = '' ) {
433 if ( empty( $slug ) ) {
434 return false;
435 }
436
437 // Iterate over registered fields and see if we can find proper callback
438 foreach( $this->settings_fields as $section => $options ) {
439 foreach ( $options as $option ) {
440 if ( $option['name'] != $slug ) {
441 continue;
442 }
443
444 // Return the callback name
445 return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false;
446 }
447 }
448
449 return false;
450 }
451
452 /**
453 * Get the value of a settings field
454 *
455 * @param string $option settings field name
456 * @param string $section the section name this field belongs to
457 * @param string $default default text if it's not found
458 * @return string
459 */
460 function get_option( $option, $section, $default = '' ) {
461
462 $options = get_option( $section );
463
464 if ( isset( $options[$option] ) ) {
465 return $options[$option];
466 }
467
468 return $default;
469 }
470
471 /**
472 * Show navigations as tab
473 *
474 * Shows all the settings section labels as tab
475 */
476 function show_navigation() {
477 $html = '<h2 class="nav-tab-wrapper">';
478
479 $count = count( $this->settings_sections );
480
481 // don't show the navigation if only one section exists
482 if ( $count === 1 ) {
483 return;
484 }
485
486 foreach ( $this->settings_sections as $tab ) {
487 $html .= sprintf( '<a href="#%1$s" class="nav-tab" id="%1$s-tab">%2$s</a>', esc_attr( $tab['id'] ), esc_html( $tab['title'] ) );
488 }
489
490 $html .= '</h2>';
491
492 echo $html;
493 }
494
495 /**
496 * Show the section settings forms
497 *
498 * This function displays every sections in a different form
499 */
500 function show_forms() {
501 ?>
502 <div class="metabox-holder">
503 <?php foreach ( $this->settings_sections as $form ) { ?>
504 <div id="<?php echo esc_attr( $form['id'] ); ?>" class="group" style="display: none;">
505 <form method="post" action="options.php">
506 <?php
507 do_action( 'wsa_form_top_' . $form['id'], $form );
508 settings_fields( $form['id'] );
509 do_settings_sections( $form['id'] );
510 do_action( 'wsa_form_bottom_' . $form['id'], $form );
511 if ( isset( $this->settings_fields[ $form['id'] ] ) ):
512 ?>
513 <div><i>We encourage you to take the time to review our revised <a href="<?php echo esc_url( 'https://webspellchecker.com/privacy-policy/' ); ?>" target="_blank">Privacy Policy</a> ‌and <a href="<?php echo esc_url( 'https://webspellchecker.com/terms-of-service/' ); ?>" target="_blank">Terms of Service</a>. By continuing to use WebSpellChecker Services, you acknowledge our Privacy Policy and agree to our Terms of Service.</i></div>
514 <div style="padding-left: 10px">
515 <?php submit_button(); ?>
516 </div>
517 <?php endif; ?>
518 </form>
519 </div>
520 <?php } ?>
521 </div>
522 <?php
523 $this->script();
524 }
525
526 /**
527 * Tabbable JavaScript codes & Initiate Color Picker
528 *
529 * This code uses localstorage for displaying active tabs
530 */
531 function script() {
532 ?>
533 <script>
534 jQuery(document).ready(function($) {
535 //Initiate Color Picker
536 $('.wp-color-picker-field').wpColorPicker();
537
538 // Switches option sections
539 $('.group').hide();
540 var activetab = '';
541 if (typeof(localStorage) != 'undefined' ) {
542 activetab = localStorage.getItem("activetab");
543 }
544 if (activetab != '' && $(activetab).length ) {
545 $(activetab).fadeIn();
546 } else {
547 $('.group:first').fadeIn();
548 }
549 $('.group .collapsed').each(function(){
550 $(this).find('input:checked').parent().parent().parent().nextAll().each(
551 function(){
552 if ($(this).hasClass('last')) {
553 $(this).removeClass('hidden');
554 return false;
555 }
556 $(this).filter('.hidden').removeClass('hidden');
557 });
558 });
559
560 if (activetab != '' && $(activetab + '-tab').length ) {
561 $(activetab + '-tab').addClass('nav-tab-active');
562 }
563 else {
564 $('.nav-tab-wrapper a:first').addClass('nav-tab-active');
565 }
566 $('.nav-tab-wrapper a').click(function(evt) {
567 $('.nav-tab-wrapper a').removeClass('nav-tab-active');
568 $(this).addClass('nav-tab-active').blur();
569 var clicked_group = $(this).attr('href');
570 if (typeof(localStorage) != 'undefined' ) {
571 localStorage.setItem("activetab", $(this).attr('href'));
572 }
573 $('.group').hide();
574 $(clicked_group).fadeIn();
575 evt.preventDefault();
576 });
577
578 $('.wpsa-browse').on('click', function (event) {
579 event.preventDefault();
580
581 var self = $(this);
582
583 // Create the media frame.
584 var file_frame = wp.media.frames.file_frame = wp.media({
585 title: self.data('uploader_title'),
586 button: {
587 text: self.data('uploader_button_text'),
588 },
589 multiple: false
590 });
591
592 file_frame.on('select', function () {
593 attachment = file_frame.state().get('selection').first().toJSON();
594 self.prev('.wpsa-url').val(attachment.url).change();
595 });
596
597 // Finally, open the modal
598 file_frame.open();
599 });
600 });
601 </script>
602 <?php
603 $this->_style_fix();
604 }
605
606 function _style_fix() {
607 global $wp_version;
608
609 if (version_compare($wp_version, '3.8', '<=')):
610 ?>
611 <style type="text/css">
612 /** WordPress 3.8 Fix **/
613 .form-table th { padding: 20px 10px; }
614 #wpbody-content .metabox-holder { padding-top: 5px; }
615 </style>
616 <?php
617 endif;
618 }
619
620 }
621
622 endif;
623