PluginProbe
Passster – Password Protect Pages and Content / 3.5.5.9
Passster – Password Protect Pages and Content v3.5.5.9
4.3.15 4.3.14 4.3.12 4.3.13 4.3.11 4.3.10 4.3.9 4.3.8 4.3.7 4.3.6 4.3.5 trunk 3.5.4 3.5.5.2 3.5.5.8 3.5.5.9 4.0 4.1.4 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.15 4.2.16 All 47 releases
content-protector / src / admin / class-ps-settings.php

class-ps-settings.php in Passster – Password Protect Pages and Content 3.5.5.9, at src/admin/class-ps-settings.php

821 lines 28.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace passster;
4
5
6 if ( !class_exists( 'PS_Settings' ) ) {
7 class PS_Settings
8 {
9 /**
10 * Sections array.
11 *
12 * @var array
13 * @since 1.0.0
14 */
15 private $sections_array = array() ;
16 /**
17 * Fields array.
18 *
19 * @var array
20 * @since 1.0.0
21 */
22 private $fields_array = array() ;
23 /**
24 * Constructor.
25 *
26 * @since 1.0.0
27 */
28 public function __construct()
29 {
30 // Enqueue the admin scripts.
31 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
32 // Hook it up.
33 add_action( 'admin_init', array( $this, 'admin_init' ) );
34 // Menu.
35 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
36 }
37
38 /**
39 * Admin Scripts.
40 *
41 * @since 1.0.0
42 */
43 public function admin_scripts()
44 {
45 // jQuery is needed.
46 wp_enqueue_script( 'jquery' );
47 // Color Picker.
48 wp_enqueue_script(
49 'iris',
50 admin_url( 'js/iris.min.js' ),
51 array( 'jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch' ),
52 false,
53 1
54 );
55 // Media Uploader.
56 wp_enqueue_media();
57 }
58
59 /**
60 * Set Sections.
61 *
62 * @param array $sections
63 *
64 * @since 1.0.0
65 */
66 public function set_sections( $sections )
67 {
68 // Bail if not array.
69 if ( !is_array( $sections ) ) {
70 return false;
71 }
72 // Assign to the sections array.
73 $this->sections_array = $sections;
74 return $this;
75 }
76
77 /**
78 * Add a single section.
79 *
80 * @param array $section
81 *
82 * @since 1.0.0
83 */
84 public function add_section( $section )
85 {
86 // Bail if not array.
87 if ( !is_array( $section ) ) {
88 return false;
89 }
90 // Assign the section to sections array.
91 $this->sections_array[] = $section;
92 return $this;
93 }
94
95 /**
96 * Set Fields.
97 *
98 * @since 1.0.0
99 */
100 public function set_fields( $fields )
101 {
102 // Bail if not array.
103 if ( !is_array( $fields ) ) {
104 return false;
105 }
106 // Assign the fields.
107 $this->fields_array = $fields;
108 return $this;
109 }
110
111 /**
112 * Add a single field.
113 *
114 * @since 1.0.0
115 */
116 public function add_field( $section, $field_array )
117 {
118 // Set the defaults
119 $defaults = array(
120 'id' => '',
121 'name' => '',
122 'desc' => '',
123 'type' => 'text',
124 );
125 // Combine the defaults with user's arguements.
126 $arg = wp_parse_args( $field_array, $defaults );
127 // Each field is an array named against its section.
128 $this->fields_array[$section][] = $arg;
129 return $this;
130 }
131
132 /**
133 * Initialize API.
134 *
135 * Initializes and registers the settings sections and fields.
136 * Usually this should be called at `admin_init` hook.
137 *
138 * @since 1.0.0
139 */
140 function admin_init()
141 {
142 /**
143 * Register the sections.
144 *
145 * Sections array is like this:
146 *
147 * $sections_array = array (
148 * $section_array,
149 * $section_array,
150 * $section_array,
151 * );
152 *
153 * Section array is like this:
154 *
155 * $section_array = array (
156 * 'id' => 'section_id',
157 * 'title' => 'Section Title'
158 * );
159 *
160 * @since 1.0.0
161 */
162 foreach ( $this->sections_array as $section ) {
163 if ( false == get_option( $section['id'] ) ) {
164 // Add a new field as section ID.
165 add_option( $section['id'] );
166 }
167 // Deals with sections description.
168
169 if ( isset( $section['desc'] ) && !empty($section['desc']) ) {
170 // Build HTML.
171 $section['desc'] = '<div class="inside">' . $section['desc'] . '</div>';
172 // Create the callback for description.
173 $callback = function () use( $section ) {
174 echo str_replace( '"', '\\"', $section['desc'] ) ;
175 };
176 } elseif ( isset( $section['callback'] ) ) {
177 $callback = $section['callback'];
178 } else {
179 $callback = null;
180 }
181
182 /**
183 * Add a new section to a settings page.
184 *
185 * @param string $id
186 * @param string $title
187 * @param callable $callback
188 * @param string $page | Page is same as section ID.
189 *
190 * @since 1.0.0
191 */
192 add_settings_section(
193 $section['id'],
194 $section['title'],
195 $callback,
196 $section['id']
197 );
198 }
199 // foreach ended.
200 /**
201 * Register settings fields.
202 *
203 * Fields array is like this:
204 *
205 * $fields_array = array (
206 * $section => $field_array,
207 * $section => $field_array,
208 * $section => $field_array,
209 * );
210 *
211 *
212 * Field array is like this:
213 *
214 * $field_array = array (
215 * 'id' => 'id',
216 * 'name' => 'Name',
217 * 'type' => 'text',
218 * );
219 *
220 * @since 1.0.0
221 */
222 foreach ( $this->fields_array as $section => $field_array ) {
223 foreach ( $field_array as $field ) {
224 // ID.
225 $id = ( isset( $field['id'] ) ? $field['id'] : false );
226 // Type.
227 $type = ( isset( $field['type'] ) ? $field['type'] : 'text' );
228 // Name.
229 $name = ( isset( $field['name'] ) ? $field['name'] : 'No Name Added' );
230 // Label for.
231 $label_for = "{$section}[{$field['id']}]";
232 // Description.
233 $description = ( isset( $field['desc'] ) ? $field['desc'] : '' );
234 // Premium.
235 $premium = ( isset( $field['premium'] ) ? $field['premium'] : '' );
236 // Min and max.
237 $min = ( isset( $field['min'] ) ? $field['min'] : '' );
238 $max = ( isset( $field['max'] ) ? $field['max'] : '' );
239 // Size.
240 $size = ( isset( $field['size'] ) ? $field['size'] : null );
241 // Options.
242 $options = ( isset( $field['options'] ) ? $field['options'] : '' );
243 // Standard default value.
244 $default = ( isset( $field['default'] ) ? $field['default'] : '' );
245 // Standard default placeholder.
246 $placeholder = ( isset( $field['placeholder'] ) ? $field['placeholder'] : '' );
247 // Sanitize Callback.
248 $sanitize_callback = ( isset( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : '' );
249 $args = array(
250 'id' => $id,
251 'type' => $type,
252 'name' => $name,
253 'label_for' => $label_for,
254 'desc' => $description,
255 'premium' => $premium,
256 'min' => $min,
257 'max' => $max,
258 'section' => $section,
259 'size' => $size,
260 'options' => $options,
261 'std' => $default,
262 'placeholder' => $placeholder,
263 'sanitize_callback' => $sanitize_callback,
264 );
265 /**
266 * Add a new field to a section of a settings page.
267 *
268 * @param string $id
269 * @param string $title
270 * @param callable $callback
271 * @param string $page
272 * @param string $section = 'default'
273 * @param array $args = array()
274 *
275 * @since 1.0.0
276 */
277 // @param string $id
278 $field_id = $section . '[' . $field['id'] . ']';
279 add_settings_field(
280 $field_id,
281 $name,
282 array( $this, 'callback_' . $type ),
283 $section,
284 $section,
285 $args
286 );
287 }
288 // foreach ended.
289 }
290 // foreach ended.
291 // Creates our settings in the fields table.
292 foreach ( $this->sections_array as $section ) {
293 /**
294 * Registers a setting and its sanitization callback.
295 *
296 * @param string $field_group | A settings group name.
297 * @param string $field_name | The name of an option to sanitize and save.
298 * @param callable $sanitize_callback = ''
299 *
300 * @since 1.0.0
301 */
302 register_setting( $section['id'], $section['id'], array( $this, 'sanitize_fields' ) );
303 }
304 // foreach ended.
305 }
306
307 // admin_init() ended.
308 /**
309 * Sanitize callback for Settings API fields.
310 *
311 * @since 1.0.0
312 */
313 public function sanitize_fields( $fields )
314 {
315 foreach ( $fields as $field_slug => $field_value ) {
316 $sanitize_callback = $this->get_sanitize_callback( $field_slug );
317 // If callback is set, call it.
318
319 if ( $sanitize_callback ) {
320 $fields[$field_slug] = call_user_func( $sanitize_callback, $field_value );
321 continue;
322 }
323
324 }
325 return $fields;
326 }
327
328 /**
329 * Get sanitization callback for given option slug
330 *
331 * @param string $slug option slug.
332 *
333 * @return mixed string | bool false
334 * @since 1.0.0
335 */
336 function get_sanitize_callback( $slug = '' )
337 {
338 if ( empty($slug) ) {
339 return false;
340 }
341 // Iterate over registered fields and see if we can find proper callback.
342 foreach ( $this->fields_array as $section => $field_array ) {
343 foreach ( $field_array as $field ) {
344 if ( $field['name'] != $slug ) {
345 continue;
346 }
347 // Return the callback name.
348 return ( isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : false );
349 }
350 }
351 return false;
352 }
353
354 /**
355 * Get field description for display
356 *
357 * @param array $args settings field args
358 */
359 public function get_field_description( $args )
360 {
361
362 if ( !empty($args['desc']) ) {
363 $desc = sprintf( '<p class="description">%s</p>', $args['desc'] );
364 } else {
365 $desc = '';
366 }
367
368 return $desc;
369 }
370
371 /**
372 * Displays a title field for a settings field
373 *
374 * @param array $args settings field args
375 */
376 function callback_title( $args )
377 {
378 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
379
380 if ( '' !== $args['name'] ) {
381 $name = $args['name'];
382 } else {
383 }
384
385 $type = ( isset( $args['type'] ) ? $args['type'] : 'title' );
386 $html = '';
387 echo $html ;
388 }
389
390 /**
391 * Displays a text field for a settings field
392 *
393 * @param array $args settings field args
394 */
395 function callback_text( $args )
396 {
397 $value = esc_attr( $this->get_option(
398 $args['id'],
399 $args['section'],
400 $args['std'],
401 $args['placeholder']
402 ) );
403 $size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' );
404 $type = ( isset( $args['type'] ) ? $args['type'] : 'text' );
405 $html = sprintf(
406 '<input type="%1$s" class="%2$s-text ' . $args['premium'] . '" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"placeholder="%6$s"/>',
407 $type,
408 $size,
409 $args['section'],
410 $args['id'],
411 $value,
412 $args['placeholder']
413 );
414 $html .= $this->get_field_description( $args );
415 echo $html ;
416 }
417
418 /**
419 * Displays a number field for a settings field
420 *
421 * @param array $args settings field args
422 */
423 function callback_number( $args )
424 {
425 $value = esc_attr( $this->get_option(
426 $args['id'],
427 $args['section'],
428 $args['std'],
429 $args['placeholder']
430 ) );
431 $size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' );
432 $type = ( isset( $args['type'] ) ? $args['type'] : 'number' );
433 $min = ( isset( $args['min'] ) ? 'min="' . $args['min'] . '"' : '' );
434 $max = ( isset( $args['max'] ) ? 'max="' . $args['max'] . '"' : '' );
435 $html = sprintf(
436 '<input type="%1$s" class="%2$s-text ' . $args['premium'] . '" id="%3$s[%4$s]" name="%3$s[%4$s]" ' . $min . $max . ' value="%5$s"placeholder="%6$s"/>',
437 $type,
438 $size,
439 $args['section'],
440 $args['id'],
441 $value,
442 $args['placeholder']
443 );
444 $html .= $this->get_field_description( $args );
445 echo $html ;
446 }
447
448 /**
449 * Displays a checkbox for a settings field
450 *
451 * @param array $args settings field args
452 */
453 function callback_checkbox( $args )
454 {
455 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
456 $html = '<fieldset>';
457 $html .= sprintf( '<label for="wposa-%1$s[%2$s]">', $args['section'], $args['id'] );
458 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
459 $html .= sprintf(
460 '<input type="checkbox" class="checkbox" id="wposa-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />',
461 $args['section'],
462 $args['id'],
463 checked( $value, 'on', false )
464 );
465 $html .= sprintf( '%1$s</label>', $args['desc'] );
466 $html .= '</fieldset>';
467 echo $html ;
468 }
469
470 /**
471 * Displays a selectbox for a settings field
472 *
473 * @param array $args settings field args
474 */
475 function callback_select( $args )
476 {
477 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
478 $size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' );
479 $html = sprintf(
480 '<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">',
481 $size,
482 $args['section'],
483 $args['id']
484 );
485 foreach ( $args['options'] as $key => $label ) {
486 $html .= sprintf(
487 '<option value="%s"%s>%s</option>',
488 $key,
489 selected( $value, $key, false ),
490 $label
491 );
492 }
493 $html .= sprintf( '</select>' );
494 $html .= $this->get_field_description( $args );
495 echo $html ;
496 }
497
498 /**
499 * Displays a textarea for a settings field
500 *
501 * @param array $args settings field args
502 */
503 function callback_textarea( $args )
504 {
505 $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
506 $size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ? $args['size'] : 'regular' );
507 $html = sprintf(
508 '<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]">%4$s</textarea>',
509 $size,
510 $args['section'],
511 $args['id'],
512 $value
513 );
514 $html .= $this->get_field_description( $args );
515 echo $html ;
516 }
517
518 /**
519 * Displays a toggle for a settings field
520 *
521 * @param array $args settings field args
522 */
523 function callback_toggle( $args )
524 {
525 $value = esc_attr( $this->get_option( $args['id'], $args['section'], $args['std'] ) );
526 $html = '<fieldset>';
527 $html .= sprintf( '<label class="switch" for="wposa-%1$s[%2$s]">', $args['section'], $args['id'] );
528 $html .= sprintf( '<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id'] );
529 $html .= sprintf(
530 '<input type="checkbox" class="toggle-checkbox ' . $args['premium'] . '" id="wposa-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />',
531 $args['section'],
532 $args['id'],
533 checked( $value, 'on', false )
534 );
535 $html .= '<span class="slider round"></span>';
536 $html .= '</label>';
537 $html .= '</fieldset>';
538 echo $html ;
539 }
540
541 /**
542 * Get the value of a settings field
543 *
544 * @param string $option settings field name.
545 * @param string $section the section name this field belongs to.
546 * @param string $default default text if it's not found.
547 *
548 * @return string
549 */
550 function get_option( $option, $section, $default = '' )
551 {
552 $options = get_option( $section );
553 if ( isset( $options[$option] ) ) {
554 return $options[$option];
555 }
556 return $default;
557 }
558
559 /**
560 * Add submenu page to the Settings main menu.
561 *
562 * @param string $page_title
563 * @param string $menu_title
564 * @param string $capability
565 * @param string $menu_slug
566 * @param callable $function = ''
567 *
568 * @author Ahmad Awais
569 * @since [version]
570 */
571 public function admin_menu()
572 {
573 add_menu_page(
574 __( 'Passster', 'content-protector' ),
575 'Passster',
576 apply_filters( 'passster_user_capability', 'manage_options' ),
577 'passster',
578 array( $this, 'plugin_page' ),
579 'dashicons-lock',
580 85
581 );
582 add_submenu_page(
583 'passster',
584 __( 'Options', 'content-protector' ),
585 __( 'Options', 'content-protector' ),
586 apply_filters( 'passster_user_capability', 'manage_options' ),
587 'passster_settings',
588 array( $this, 'plugin_page' )
589 );
590 }
591
592 /**
593 * Add admin page content.
594 *
595 * @return void
596 */
597 public function plugin_page()
598 {
599 ?>
600 <div class="passster-admin-header">
601 <div class="logo"><img src="<?php
602 echo PASSSTER_URL . '/assets/admin/passster-logo.svg' ;
603 ?>"/></div>
604 <div class="info-links">
605 <?php
606 ?>
607 <a href="https://patrickposner.dev/plugins/passster" target="_blank">Go Pro</a>
608 <a href="https://patrickposner.dev/docs/passster" target="_blank">Documentation</a>
609 <a href="https://wordpress.org/plugins/content-protector/" target="_blank">Support</a>
610 <?php
611 ?>
612 </div>
613 </div>
614 <div class="wrap passster-admin">
615 <?php
616 $this->show_navigation();
617 ?>
618 <?php
619 $this->show_forms();
620 ?>
621 </div>
622 <?php
623 }
624
625 /**
626 * Show navigations as tab
627 *
628 * Shows all the settings section labels as tab
629 */
630 function show_navigation()
631 {
632 $html = '<h2 class="nav-tab-wrapper">';
633 foreach ( $this->sections_array as $tab ) {
634 $html .= sprintf( '<a href="#%1$s" class="nav-tab" id="%1$s-tab">%2$s</a>', $tab['id'], $tab['title'] );
635 }
636 $html .= '</h2>';
637 echo $html ;
638 }
639
640 /**
641 * Show the section settings forms
642 *
643 * This function displays every sections in a different form
644 */
645 function show_forms()
646 {
647 ?>
648 <div class="metabox-holder">
649 <?php
650 foreach ( $this->sections_array as $form ) {
651 ?>
652 <!-- style="display: none;" -->
653 <div id="<?php
654 echo $form['id'] ;
655 ?>" class="group">
656 <form method="post" action="options.php">
657 <?php
658 do_action( 'wsa_form_top_' . $form['id'], $form );
659 settings_fields( $form['id'] );
660 do_settings_sections( $form['id'] );
661 do_action( 'wsa_form_bottom_' . $form['id'], $form );
662 ?>
663 <div style="padding-left: 10px">
664 <?php
665 submit_button();
666 ?>
667 </div>
668 </form>
669 </div>
670 <?php
671 }
672 ?>
673 </div>
674 <?php
675 $this->script();
676 }
677
678 /**
679 * Tabbable JavaScript codes & Initiate Color Picker
680 *
681 * This code uses localstorage for displaying active tabs
682 */
683 function script()
684 {
685 ?>
686 <script>
687 jQuery(document).ready(function ($) {
688
689 //Initiate Color Picker.
690 $('.color-picker').iris();
691
692 // Switches option sections
693 $('.group').hide();
694 var activetab = '';
695 if ('undefined' != typeof localStorage) {
696 activetab = localStorage.getItem('activetab');
697 }
698 if ('' != activetab && $(activetab).length) {
699 $(activetab).fadeIn();
700 } else {
701 $('.group:first').fadeIn();
702 }
703 $('.group .collapsed').each(function () {
704 $(this)
705 .find('input:checked')
706 .parent()
707 .parent()
708 .parent()
709 .nextAll()
710 .each(function () {
711 if ($(this).hasClass('last')) {
712 $(this).removeClass('hidden');
713 return false;
714 }
715 $(this)
716 .filter('.hidden')
717 .removeClass('hidden');
718 });
719 });
720
721 if ('' != activetab && $(activetab + '-tab').length) {
722 $(activetab + '-tab').addClass('nav-tab-active');
723 } else {
724 $('.nav-tab-wrapper a:first').addClass('nav-tab-active');
725 }
726 $('.nav-tab-wrapper a').click(function (evt) {
727 $('.nav-tab-wrapper a').removeClass('nav-tab-active');
728 $(this)
729 .addClass('nav-tab-active')
730 .blur();
731 var clicked_group = $(this).attr('href');
732 if ('undefined' != typeof localStorage) {
733 localStorage.setItem('activetab', $(this).attr('href'));
734 }
735 $('.group').hide();
736 $(clicked_group).fadeIn();
737 evt.preventDefault();
738 });
739
740 $('.wpsa-browse').on('click', function (event) {
741 event.preventDefault();
742
743 var self = $(this);
744
745 // Create the media frame.
746 var file_frame = (wp.media.frames.file_frame = wp.media({
747 title: self.data('uploader_title'),
748 button: {
749 text: self.data('uploader_button_text')
750 },
751 multiple: false
752 }));
753
754 file_frame.on('select', function () {
755 attachment = file_frame
756 .state()
757 .get('selection')
758 .first()
759 .toJSON();
760
761 self
762 .prev('.wpsa-url')
763 .val(attachment.url)
764 .change();
765 });
766
767 // Finally, open the modal
768 file_frame.open();
769 });
770
771 $('input.wpsa-url')
772 .on('change keyup paste input', function () {
773 var self = $(this);
774 self
775 .next()
776 .parent()
777 .children('.wpsa-image-preview')
778 .children('img')
779 .attr('src', self.val());
780 })
781 .change();
782 });
783
784 </script>
785
786 <style type="text/css">
787 /** WordPress 3.8 Fix **/
788 .form-table th {
789 padding: 20px 10px;
790 }
791
792 #wpbody-content .metabox-holder {
793 padding-top: 5px;
794 }
795
796 .wpsa-image-preview img {
797 height: auto;
798 max-width: 70px;
799 }
800
801 .wpsa-settings-separator {
802 background: #ccc;
803 border: 0;
804 color: #ccc;
805 height: 1px;
806 position: absolute;
807 left: 0;
808 width: 99%;
809 }
810
811 .group .form-table input.color-picker {
812 max-width: 100px;
813 }
814 </style>
815 <?php
816 }
817
818 }
819 // WP_OSA ended.
820 }
821