PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.1
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 / Controllers / Admin / WidgetEditor.php

WidgetEditor.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.1, at classes/Controllers/Admin/WidgetEditor.php

134 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Widget Editor controller.
4 *
5 * @note This is only used for the old WP -4.9 widget editor.
6 *
7 * @package ContentControl\Admin
8 * @copyright (c) 2023 Code Atlantic LLC
9 */
10
11 namespace ContentControl\Controllers\Admin;
12
13 use ContentControl\Base\Controller;
14
15 use function ContentControl\Rules\allowed_user_roles;
16 use function ContentControl\Widgets\parse_options as parse_widget_options;
17
18 /**
19 * WidgetEditor controller class.
20 */
21 class WidgetEditor extends Controller {
22
23 /**
24 * Initialize widget editor UX.
25 *
26 * @return void
27 */
28 public function init() {
29 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
30 add_action( 'in_widget_form', [ $this, 'fields' ], 5, 3 );
31 add_filter( 'widget_update_callback', [ $this, 'save' ], 5, 3 );
32 }
33
34 /**
35 * Enqueue v1 admin scripts.
36 *
37 * @param mixed $hook Admin page hook name.
38 */
39 public function enqueue_assets( $hook ) {
40 if ( 'widgets.php' === $hook ) {
41 wp_enqueue_style( 'content-control-widget-editor' );
42 wp_enqueue_script( 'content-control-widget-editor' );
43 }
44 }
45
46 /**
47 * Renders additional widget option fields.
48 *
49 * @param \WP_Widget $widget Widget instance.
50 * @param bool $ret Whether to return the output.
51 * @param array $instance Widget instance options.
52 */
53 public function fields( $widget, $ret, $instance ) {
54 $allowed_user_roles = allowed_user_roles();
55
56 wp_nonce_field( 'content-control-widget-editor-nonce', 'content-control-widget-editor-nonce' );
57
58 $which_users_options = [
59 '' => __( 'Everyone', 'content-control' ),
60 'logged_out' => __( 'Logged Out Users', 'content-control' ),
61 'logged_in' => __( 'Logged In Users', 'content-control' ),
62 ];
63
64 $instance = parse_widget_options( $instance );
65
66 ?>
67 <p class="widget_options-which_users">
68 <label for="<?php echo esc_attr( $widget->get_field_id( 'which_users' ) ); ?>">
69 <?php esc_html_e( 'Who can see this widget?', 'content-control' ); ?><br />
70 <select name="<?php echo esc_attr( $widget->get_field_name( 'which_users' ) ); ?>" id="<?php echo esc_attr( $widget->get_field_id( 'which_users' ) ); ?>" class="widefat">
71 <?php foreach ( $which_users_options as $option => $label ) : ?>
72 <option value="<?php echo esc_attr( $option ); ?>" <?php selected( $option, $instance['which_users'] ); ?>>
73 <?php echo esc_html( $label ); ?>
74 </option>
75 <?php endforeach; ?>
76 </select>
77 </label>
78 </p>
79
80 <p class="widget_options-roles">
81 <?php esc_html_e( 'Choose which roles can see this widget', 'content-control' ); ?><br />
82 <?php foreach ( $allowed_user_roles as $option => $label ) : ?>
83 <label>
84 <input type="checkbox" name="<?php echo esc_attr( $widget->get_field_name( 'roles' ) ); ?>[]" value="<?php echo esc_attr( $option ); ?>" <?php checked( in_array( $option, $instance['roles'], true ), true ); ?>/>
85 <?php echo esc_html( $label ); ?>
86 </label>
87 <?php endforeach; ?>
88 </p>
89 <?php
90 }
91
92 /**
93 * Validates & saves additional widget options.
94 *
95 * @param array $instance Widget instance options.
96 * @param array $new_instance New widget instance options.
97 * @param array $old_instance Old widget instance options.
98 *
99 * @return array|bool
100 */
101 public function save( $instance, $new_instance, $old_instance ) {
102 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
103 if ( isset( $_POST['content-control-widget-editor-nonce'] ) && wp_verify_nonce( $_POST['content-control-widget-editor-nonce'], 'content-control-widget-editor-nonce' ) ) {
104 $new_instance = parse_widget_options( $new_instance );
105 $instance['which_users'] = $new_instance['which_users'];
106 $instance['roles'] = $new_instance['roles'];
107
108 if ( 'logged_in' === $instance['which_users'] ) {
109 $allowed_roles = allowed_user_roles();
110
111 // Validate chosen roles and remove non-allowed roles.
112 foreach ( (array) $instance['roles'] as $key => $role ) {
113 if ( ! array_key_exists( $role, $allowed_roles ) ) {
114 unset( $instance['roles'][ $key ] );
115 }
116 }
117 } else {
118 unset( $instance['roles'] );
119 }
120 } else {
121 $old_instance = parse_widget_options( $old_instance );
122 $instance['which_users'] = $old_instance['which_users'];
123
124 if ( empty( $old_instance['roles'] ) ) {
125 unset( $instance['roles'] );
126 } else {
127 $instance['roles'] = $old_instance['roles'];
128 }
129 }
130
131 return $instance;
132 }
133 }
134