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

139 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 WP_Widget;
14 use ContentControl\Base\Controller;
15
16 use function ContentControl\Rules\allowed_user_roles;
17 use function ContentControl\Widgets\parse_options as parse_widget_options;
18
19 /**
20 * WidgetEditor controller class.
21 */
22 class WidgetEditor extends Controller {
23
24 /**
25 * Initialize widget editor UX.
26 *
27 * @return void
28 */
29 public function init() {
30 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
31 add_action( 'in_widget_form', [ $this, 'fields' ], 5, 3 );
32 add_filter( 'widget_update_callback', [ $this, 'save' ], 5, 3 );
33 }
34
35 /**
36 * Enqueue v1 admin scripts.
37 *
38 * @param mixed $hook Admin page hook name.
39 *
40 * @return void
41 */
42 public function enqueue_assets( $hook ) {
43 if ( 'widgets.php' === $hook ) {
44 wp_enqueue_style( 'content-control-widget-editor' );
45 wp_enqueue_script( 'content-control-widget-editor' );
46 }
47 }
48
49 /**
50 * Renders additional widget option fields.
51 *
52 * @param \WP_Widget $widget Widget instance.
53 * @param bool $ret Whether to return the output.
54 * @param array<string,mixed> $instance Widget instance options.
55 *
56 * @return void
57 */
58 public function fields( $widget, $ret, $instance ) {
59 $allowed_user_roles = allowed_user_roles();
60
61 wp_nonce_field( 'content-control-widget-editor-nonce', 'content-control-widget-editor-nonce' );
62
63 $which_users_options = [
64 '' => __( 'Everyone', 'content-control' ),
65 'logged_out' => __( 'Logged Out Users', 'content-control' ),
66 'logged_in' => __( 'Logged In Users', 'content-control' ),
67 ];
68
69 $instance = parse_widget_options( $instance );
70
71 ?>
72 <p class="widget_options-which_users">
73 <label for="<?php echo esc_attr( $widget->get_field_id( 'which_users' ) ); ?>">
74 <?php esc_html_e( 'Who can see this widget?', 'content-control' ); ?><br />
75 <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">
76 <?php foreach ( $which_users_options as $option => $label ) : ?>
77 <option value="<?php echo esc_attr( $option ); ?>" <?php selected( $option, $instance['which_users'] ); ?>>
78 <?php echo esc_html( $label ); ?>
79 </option>
80 <?php endforeach; ?>
81 </select>
82 </label>
83 </p>
84
85 <p class="widget_options-roles">
86 <?php esc_html_e( 'Choose which roles can see this widget', 'content-control' ); ?><br />
87 <?php foreach ( $allowed_user_roles as $option => $label ) : ?>
88 <label>
89 <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 ); ?>/>
90 <?php echo esc_html( $label ); ?>
91 </label>
92 <?php endforeach; ?>
93 </p>
94 <?php
95 }
96
97 /**
98 * Validates & saves additional widget options.
99 *
100 * @param array<string,mixed> $instance Widget instance options.
101 * @param array<string,mixed> $new_instance New widget instance options.
102 * @param array<string,mixed> $old_instance Old widget instance options.
103 *
104 * @return array<string,mixed>|bool
105 */
106 public function save( $instance, $new_instance, $old_instance ) {
107 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' ) ) {
108 $new_instance = parse_widget_options( $new_instance );
109 $instance['which_users'] = $new_instance['which_users'];
110 $instance['roles'] = $new_instance['roles'];
111
112 if ( 'logged_in' === $instance['which_users'] ) {
113 $allowed_roles = allowed_user_roles();
114
115 // Validate chosen roles and remove non-allowed roles.
116 foreach ( (array) $instance['roles'] as $key => $role ) {
117 if ( ! array_key_exists( $role, $allowed_roles ) ) {
118 unset( $instance['roles'][ $key ] );
119 }
120 }
121 } else {
122 unset( $instance['roles'] );
123 }
124 } else {
125 // Failed validation, use old instance.
126 $old_instance = parse_widget_options( $old_instance );
127 $instance['which_users'] = $old_instance['which_users'];
128
129 if ( empty( $old_instance['roles'] ) ) {
130 unset( $instance['roles'] );
131 } else {
132 $instance['roles'] = $old_instance['roles'];
133 }
134 }
135
136 return $instance;
137 }
138 }
139