PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.7
Elementor Website Builder – more than just a page builder v3.35.7
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.35.7, at core/role-manager/role-manager.php

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