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

138 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 * @return void
40 */
41 public function enqueue_assets( $hook ) {
42 if ( 'widgets.php' === $hook ) {
43 wp_enqueue_style( 'content-control-widget-editor' );
44 wp_enqueue_script( 'content-control-widget-editor' );
45 }
46 }
47
48 /**
49 * Renders additional widget option fields.
50 *
51 * @param \WP_Widget $widget Widget instance.
52 * @param bool $ret Whether to return the output.
53 * @param array<string,mixed> $instance Widget instance options.
54 *
55 * @return void
56 */
57 public function fields( $widget, $ret, $instance ) {
58 $allowed_user_roles = allowed_user_roles();
59
60 wp_nonce_field( 'content-control-widget-editor-nonce', 'content-control-widget-editor-nonce' );
61
62 $which_users_options = [
63 '' => __( 'Everyone', 'content-control' ),
64 'logged_out' => __( 'Logged Out Users', 'content-control' ),
65 'logged_in' => __( 'Logged In Users', 'content-control' ),
66 ];
67
68 $instance = parse_widget_options( $instance );
69
70 ?>
71 <p class="widget_options-which_users">
72 <label for="<?php echo esc_attr( $widget->get_field_id( 'which_users' ) ); ?>">
73 <?php esc_html_e( 'Who can see this widget?', 'content-control' ); ?><br />
74 <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">
75 <?php foreach ( $which_users_options as $option => $label ) : ?>
76 <option value="<?php echo esc_attr( $option ); ?>" <?php selected( $option, $instance['which_users'] ); ?>>
77 <?php echo esc_html( $label ); ?>
78 </option>
79 <?php endforeach; ?>
80 </select>
81 </label>
82 </p>
83
84 <p class="widget_options-roles">
85 <?php esc_html_e( 'Choose which roles can see this widget', 'content-control' ); ?><br />
86 <?php foreach ( $allowed_user_roles as $option => $label ) : ?>
87 <label>
88 <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 ); ?>/>
89 <?php echo esc_html( $label ); ?>
90 </label>
91 <?php endforeach; ?>
92 </p>
93 <?php
94 }
95
96 /**
97 * Validates & saves additional widget options.
98 *
99 * @param array<string,mixed> $instance Widget instance options.
100 * @param array<string,mixed> $new_instance New widget instance options.
101 * @param array<string,mixed> $old_instance Old widget instance options.
102 *
103 * @return array<string,mixed>|bool
104 */
105 public function save( $instance, $new_instance, $old_instance ) {
106 if ( isset( $_POST['content-control-widget-editor-nonce'] ) && wp_verify_nonce( wp_unslash( sanitize_key( $_POST['content-control-widget-editor-nonce'] ) ), 'content-control-widget-editor-nonce' ) ) {
107 $new_instance = parse_widget_options( $new_instance );
108 $instance['which_users'] = $new_instance['which_users'];
109 $instance['roles'] = $new_instance['roles'];
110
111 if ( 'logged_in' === $instance['which_users'] ) {
112 $allowed_roles = allowed_user_roles();
113
114 // Validate chosen roles and remove non-allowed roles.
115 foreach ( (array) $instance['roles'] as $key => $role ) {
116 if ( ! array_key_exists( $role, $allowed_roles ) ) {
117 unset( $instance['roles'][ $key ] );
118 }
119 }
120 } else {
121 unset( $instance['roles'] );
122 }
123 } else {
124 // Failed validation, use old instance.
125 $old_instance = parse_widget_options( $old_instance );
126 $instance['which_users'] = $old_instance['which_users'];
127
128 if ( empty( $old_instance['roles'] ) ) {
129 unset( $instance['roles'] );
130 } else {
131 $instance['roles'] = $old_instance['roles'];
132 }
133 }
134
135 return $instance;
136 }
137 }
138