PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 1.1.2
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v1.1.2
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Admin / Widget / Settings.php

Settings.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 1.1.2, at classes/Admin/Widget/Settings.php

115 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace JP\CC\Admin\Widget;
4
5 use JP\CC\Widget;
6 use JP\CC\Roles;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class JP\CC\Admin\Widget_Settings
14 */
15 class Settings {
16
17 /**
18 * Initialize Widget Settings
19 */
20 public static function init() {
21 add_action( 'in_widget_form', array( __CLASS__, 'fields' ), 5, 3 );
22 add_filter( 'widget_update_callback', array( __CLASS__, 'save' ), 5, 3 );
23 }
24
25 /**
26 * Renders additional widget option fields.
27 *
28 * @param $widget
29 * @param $return
30 * @param $instance
31 */
32 public static function fields( $widget, $return, $instance ) {
33
34 $allowed_user_roles = Roles::allowed_user_roles();
35
36 wp_nonce_field( 'jpcc-menu-editor-nonce', 'jpcc-menu-editor-nonce' );
37
38 $which_users_options = array(
39 '' => __( 'Everyone', 'content-control' ),
40 'logged_out' => __( 'Logged Out Users', 'content-control' ),
41 'logged_in' => __( 'Logged In Users', 'content-control' ),
42 );
43
44 $instance = Widget::parse_options( $instance ); ?>
45
46 <p class="widget_options-which_users">
47
48 <label for="<?php echo $widget->get_field_id( 'which_users' ); ?>">
49
50 <?php _e( 'Who can see this widget?', 'content-control' ); ?><br />
51
52 <select name="<?php echo $widget->get_field_name( 'which_users' ); ?>" id="<?php echo $widget->get_field_id( 'which_users' ); ?>" class="widefat">
53 <?php foreach ( $which_users_options as $option => $label ) : ?>
54 <option value="<?php echo $option; ?>" <?php selected( $option, $instance['which_users'] ); ?>>
55 <?php echo esc_html( $label ); ?>
56 </option>
57 <?php endforeach; ?>
58 </select>
59
60 </label>
61
62 </p>
63
64 <p class="widget_options-roles">
65
66 <?php _e( 'Choose which roles can see this widget', 'content-control' ); ?><br />
67
68 <?php foreach ( $allowed_user_roles as $option => $label ) : ?>
69 <label>
70 <input type="checkbox" name="<?php echo $widget->get_field_name( 'roles' ); ?>[]" value="<?php echo $option; ?>" <?php checked( in_array( $option, $instance['roles'] ), true ); ?>/>
71 <?php echo esc_html( $label ); ?>
72 </label>
73 <?php endforeach; ?>
74
75 </p>
76
77 <?php
78 }
79
80 /**
81 * Validates & saves additional widget options.
82 *
83 * @param $instance
84 * @param $new_instance
85 *
86 * @return array|bool
87 */
88 public static function save( $instance, $new_instance ) {
89
90 if ( ! isset( $_POST['jpcc-menu-editor-nonce'] ) || ! wp_verify_nonce( $_POST['jpcc-menu-editor-nonce'], 'jpcc-menu-editor-nonce' ) ) {
91 return false;
92 }
93
94 $new_instance = Widget::parse_options( $new_instance );
95
96 if ( $new_instance['which_users'] == 'logged_in' ) {
97
98 $allowed_roles = Roles::allowed_user_roles();
99
100 // Validate chosen roles and remove non-allowed roles.
101 foreach ( (array) $new_instance['roles'] as $key => $role ) {
102 if ( ! array_key_exists( $role, $allowed_roles ) ) {
103 unset( $new_instance['roles'][ $key ] );
104 }
105 }
106
107 } else {
108 unset( $new_instance['roles'] );
109 }
110
111 return $new_instance;
112 }
113
114 }
115