PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.1
Elementor Website Builder – more than just a page builder v3.30.1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / role-manager / role-manager.php

role-manager.php in Elementor Website Builder – more than just a page builder 3.30.1, at core/role-manager/role-manager.php

328 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\RoleManager;
3
4 use Elementor\Core\Admin\Menu\Admin_Menu_Manager;
5 use Elementor\Core\Utils\Promotions\Filtered_Promotions_Manager;
6 use Elementor\Plugin;
7 use Elementor\Settings;
8 use Elementor\Settings_Page;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 class Role_Manager extends Settings_Page {
15
16 const PAGE_ID = 'elementor-role-manager';
17
18 const ROLE_MANAGER_OPTION_NAME = 'exclude_user_roles';
19
20 const ROLE_MANAGER_ADVANCED = 'role-manager';
21
22 private static $advanced_options = [];
23
24 /**
25 * @since 2.0.0
26 * @access public
27 */
28 public function get_role_manager_options() {
29 return get_option( 'elementor_' . self::ROLE_MANAGER_OPTION_NAME, [] );
30 }
31
32 public function get_role_manager_advanced_options() {
33 return get_option( 'elementor_' . self::ROLE_MANAGER_ADVANCED, [] );
34 }
35
36 public function get_user_advanced_options() {
37 if ( ! empty( static::$advanced_options ) ) {
38 return static::$advanced_options;
39 }
40
41 static::$advanced_options = $this->get_role_manager_advanced_options();
42 return static::$advanced_options;
43 }
44
45 /**
46 * @since 2.0.0
47 * @access protected
48 */
49 protected function get_page_title() {
50 return esc_html__( 'Role Manager', 'elementor' );
51 }
52
53 /**
54 * @since 2.0.0
55 * @access public
56 */
57 public function register_admin_menu( Admin_Menu_Manager $admin_menu ) {
58 $admin_menu->register( static::PAGE_ID, new Role_Manager_Menu_Item( $this ) );
59 }
60
61 /**
62 * @since 2.0.0
63 * @access protected
64 */
65 protected function create_tabs() {
66 $validation_class = 'Elementor\Settings_Validations';
67 return [
68 'general' => [
69 'label' => esc_html__( 'General', 'elementor' ),
70 'sections' => [
71 'tools' => [
72 'fields' => [
73 'exclude_user_roles' => [
74 'label' => esc_html__( 'Exclude Roles', 'elementor' ),
75 'field_args' => [
76 'type' => 'checkbox_list_roles',
77 'exclude' => [ 'super_admin', 'administrator' ],
78 ],
79 'setting_args' => [
80 'sanitize_callback' => [ $validation_class, 'checkbox_list' ],
81 ],
82 ],
83 self::ROLE_MANAGER_ADVANCED => [
84 'field_args' => [
85 'type' => 'raw_html',
86 'html' => '',
87 ],
88 'setting_args' => [
89 'sanitize_callback' => [ $this, 'save_advanced_options' ],
90 ],
91 ],
92 ],
93 ],
94 ],
95 ],
96 ];
97 }
98
99 public function save_advanced_options( $input ) {
100 return $input;
101 }
102
103 /**
104 * @since 2.0.0
105 * @access public
106 */
107 public function display_settings_page() {
108 $this->get_tabs();
109 ?>
110 <div class="wrap">
111 <h1 class="wp-heading-inline"><?php echo esc_html( $this->get_page_title() ); ?></h1>
112
113 <div id="elementor-role-manager">
114 <h3><?php echo esc_html__( 'Manage What Your Users Can Edit In Elementor', 'elementor' ); ?></h3>
115 <form id="elementor-settings-form" method="post" action="options.php">
116 <?php
117 settings_fields( static::PAGE_ID );
118 echo '<div class="elementor-settings-form-page elementor-active">';
119 foreach ( get_editable_roles() as $role_slug => $role_data ) {
120 if ( 'administrator' === $role_slug ) {
121 continue;
122 }
123 $this->display_role_controls( $role_slug, $role_data );
124 }
125 submit_button();
126 ?>
127 </form>
128 </div>
129 </div><!-- /.wrap -->
130 <?php
131 }
132
133 /**
134 * @since 2.0.0
135 * @access private
136 *
137 * @param string $role_slug The role slug.
138 * @param array $role_data An array with role data.
139 */
140 private function display_role_controls( $role_slug, $role_data ) {
141 static $excluded_options = false;
142 if ( false === $excluded_options ) {
143 $excluded_options = $this->get_role_manager_options();
144 }
145
146 ?>
147 <div class="elementor-role-row <?php echo esc_attr( $role_slug ); ?>">
148 <div class="elementor-role-label">
149 <span class="elementor-role-name"><?php echo esc_html( translate_user_role( $role_data['name'] ) ); ?></span>
150 <span data-excluded-label="<?php esc_attr_e( 'Role Excluded', 'elementor' ); ?>" class="elementor-role-excluded-indicator"></span>
151 <span class="elementor-role-toggle"><span class="dashicons dashicons-arrow-down"></span></span>
152 </div>
153 <div class="elementor-role-controls hidden">
154 <div class="elementor-role-control">
155 <label>
156 <input type="checkbox" name="elementor_exclude_user_roles[]" value="<?php echo esc_attr( $role_slug ); ?>"<?php checked( in_array( $role_slug, $excluded_options, true ), true ); ?>>
157 <?php echo esc_html__( 'No access to editor', 'elementor' ); ?>
158 </label>
159 </div>
160 <div class="elementor-role-controls-advanced">
161 <?php
162 /**
163 * Role restrictions controls.
164 *
165 * Fires after the role manager checkbox that allows the user to
166 * exclude the role.
167 *
168 * This filter allows developers to add custom controls to the role
169 * manager.
170 *
171 * @since 2.0.0
172 *
173 * @param string $role_slug The role slug.
174 * @param array $role_data An array with role data.
175 */
176 do_action( 'elementor/role/restrictions/controls', $role_slug, $role_data );
177 ?>
178 </div>
179 </div>
180 </div>
181 <?php
182 }
183
184 public function add_json_enable_control( $role_slug ) {
185 $value = 'json-upload';
186 $id = self::ROLE_MANAGER_ADVANCED . '_' . $role_slug . '_' . $value;
187 $name = 'elementor_' . self::ROLE_MANAGER_ADVANCED . '[' . $role_slug . '][]';
188
189 $advanced_options = $this->get_user_advanced_options();
190 $checked = isset( $advanced_options[ $role_slug ] ) ? $advanced_options[ $role_slug ] : [];
191 ?>
192 <div class="elementor-role-control">
193 <label for="<?php echo esc_attr( $id ); ?>">
194 <input type="checkbox" name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $value ); ?>" <?php checked( in_array( $value, $checked ), true ); ?>>
195 <?php echo esc_html__( 'Enable the option to upload JSON files', 'elementor' ); ?>
196 </label>
197 <p class="elementor-role-control-warning"><strong><?php echo esc_html__( 'Heads up', 'elementor' ); ?>:</strong> <?php echo esc_html__( 'Giving broad access to upload JSON files can pose a security risk to your website because such files may contain malicious scripts, etc.', 'elementor' ); ?></p>
198 </div>
199 <?php
200 }
201
202 public function add_custom_html_enable_control( $role_slug ) {
203 $value = 'custom-html';
204 $id = self::ROLE_MANAGER_ADVANCED . '_' . $role_slug . '_' . $value;
205 $name = 'elementor_' . self::ROLE_MANAGER_ADVANCED . '[' . $role_slug . '][]';
206
207 $advanced_options = $this->get_user_advanced_options();
208 $checked = isset( $advanced_options[ $role_slug ] ) ? $advanced_options[ $role_slug ] : [];
209 ?>
210 <div class="elementor-role-control">
211 <label for="<?php echo esc_attr( $id ); ?>">
212 <input type="checkbox" name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $value ); ?>" <?php checked( in_array( $value, $checked ), true ); ?>>
213 <?php echo esc_html__( 'Enable the option to use the HTML widget', 'elementor' ); ?>
214 </label>
215 <p class="elementor-role-control-warning"><strong><?php echo esc_html__( 'Heads up', 'elementor' ); ?>:</strong> <?php echo esc_html__( 'Giving broad access to edit the HTML widget can pose a security risk to your website because it enables users to run malicious scripts, etc.', 'elementor' ); ?></p>
216 </div>
217 <?php
218 }
219
220 /**
221 * @since 2.0.0
222 * @access public
223 */
224 public function get_go_pro_link_html() {
225 $promotion = $this->get_go_pro_link_content();
226
227 ?>
228 <div class="elementor-role-go-pro">
229 <div class="elementor-role-go-pro__desc"><?php echo esc_html( $promotion['description'] ); ?></div>
230 <div class="elementor-role-go-pro__link"><a class="elementor-button go-pro" target="_blank" href="<?php echo esc_url( $promotion['upgrade_url'] ); ?>"><?php echo esc_html( $promotion['upgrade_text'] ); ?></a></div>
231 </div>
232 <?php
233 }
234
235 public function get_go_pro_link_content() {
236 $upgrade_url = 'https://go.elementor.com/go-pro-role-manager/';
237
238 $promotion = [
239 'description' => esc_html__( 'Want to give access only to content?', 'elementor' ),
240 'upgrade_url' => esc_url( $upgrade_url ),
241 'upgrade_text' => esc_html__( 'Upgrade', 'elementor' ),
242 ];
243
244 return Filtered_Promotions_Manager::get_filtered_promotion_data( $promotion, 'elementor/role/custom_promotion', 'upgrade_url' );
245 }
246
247 /**
248 * @since 2.0.0
249 * @access public
250 */
251 public function get_user_restrictions_array() {
252 $user = wp_get_current_user();
253 $user_roles = $user->roles;
254 $options = $this->get_user_restrictions();
255 $restrictions = [];
256 if ( empty( $options ) ) {
257 return $restrictions;
258 }
259
260 foreach ( $user_roles as $role ) {
261 if ( ! isset( $options[ $role ] ) ) {
262 continue;
263 }
264 $restrictions = array_merge( $restrictions, $options[ $role ] );
265 }
266 return array_unique( $restrictions );
267 }
268
269 /**
270 * @since 2.0.0
271 * @access private
272 */
273 private function get_user_restrictions() {
274 static $restrictions = false;
275 if ( ! $restrictions ) {
276 $restrictions = [];
277
278 /**
279 * Editor user restrictions.
280 *
281 * Filters the user restrictions in the editor.
282 *
283 * @since 2.0.0
284 *
285 * @param array $restrictions User restrictions.
286 */
287 $restrictions = apply_filters( 'elementor/editor/user/restrictions', $restrictions );
288 }
289 return $restrictions;
290 }
291
292 /**
293 * @since 2.0.0
294 * @access public
295 *
296 * @param $capability
297 *
298 * @return bool
299 */
300 public function user_can( $capability ) {
301 $options = $this->get_user_restrictions_array();
302
303 if ( in_array( $capability, $options, true ) ) {
304 return false;
305 }
306
307 return true;
308 }
309
310 /**
311 * @since 2.0.0
312 * @access public
313 */
314 public function __construct() {
315 parent::__construct();
316
317 add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
318 $this->register_admin_menu( $admin_menu );
319 }, Settings::ADMIN_MENU_PRIORITY + 10 );
320
321 add_action( 'elementor/role/restrictions/controls', [ $this, 'add_json_enable_control' ] );
322 add_action( 'elementor/role/restrictions/controls', [ $this, 'add_custom_html_enable_control' ] );
323 add_action( 'elementor/role/restrictions/controls', [ $this, 'get_go_pro_link_html' ] );
324
325 add_filter( 'elementor/editor/user/restrictions', [ $this, 'get_role_manager_advanced_options' ] );
326 }
327 }
328