PluginProbe
Elementor Website Builder – more than just a page builder / 3.34.1
Elementor Website Builder – more than just a page builder v3.34.1
4.3.0-beta3 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 All 452 releases
elementor / core / role-manager / role-manager.php

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

345 lines 10.7 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 ?>
150 <div class="elementor-role-row <?php echo esc_attr( $role_slug ); ?>">
151 <div class="elementor-role-label">
152 <span class="elementor-role-name"><?php echo esc_html( translate_user_role( $role_data['name'] ) ); ?></span>
153 <span data-excluded-label="<?php esc_attr_e( 'Role Excluded', 'elementor' ); ?>" class="elementor-role-excluded-indicator"></span>
154 <span class="elementor-role-toggle"><span class="dashicons dashicons-arrow-down"></span></span>
155 </div>
156 <div class="elementor-role-controls hidden">
157 <div class="elementor-role-control">
158 <label>
159 <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 ); ?>>
160 <?php echo esc_html__( 'No access to editor', 'elementor' ); ?>
161 </label>
162 </div>
163 <div class="elementor-role-controls-advanced">
164 <?php
165 /**
166 * Role restrictions controls.
167 *
168 * Fires after the role manager checkbox that allows the user to
169 * exclude the role.
170 *
171 * This filter allows developers to add custom controls to the role
172 * manager.
173 *
174 * @since 2.0.0
175 *
176 * @param string $role_slug The role slug.
177 * @param array $role_data An array with role data.
178 */
179 do_action( 'elementor/role/restrictions/controls', $role_slug, $role_data );
180 ?>
181 </div>
182 </div>
183 </div>
184 <?php
185 }
186
187 public function add_json_enable_control( $role_slug ) {
188 $value = 'json-upload';
189 $id = self::ROLE_MANAGER_ADVANCED . '_' . $role_slug . '_' . $value;
190 $name = 'elementor_' . self::ROLE_MANAGER_ADVANCED . '[' . $role_slug . '][]';
191
192 $advanced_options = $this->get_user_advanced_options();
193 $checked = isset( $advanced_options[ $role_slug ] ) ? $advanced_options[ $role_slug ] : [];
194 ?>
195 <div class="elementor-role-control">
196 <label for="<?php echo esc_attr( $id ); ?>">
197 <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 ); ?>>
198 <?php echo esc_html__( 'Enable the option to upload JSON files', 'elementor' ); ?>
199 </label>
200 <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>
201 </div>
202 <?php
203 }
204
205 public function add_custom_html_enable_control( $role_slug ) {
206 $value = 'custom-html';
207 $id = self::ROLE_MANAGER_ADVANCED . '_' . $role_slug . '_' . $value;
208 $name = 'elementor_' . self::ROLE_MANAGER_ADVANCED . '[' . $role_slug . '][]';
209
210 $advanced_options = $this->get_user_advanced_options();
211 $checked = isset( $advanced_options[ $role_slug ] ) ? $advanced_options[ $role_slug ] : [];
212 ?>
213 <div class="elementor-role-control">
214 <label for="<?php echo esc_attr( $id ); ?>">
215 <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 ); ?>>
216 <?php echo esc_html__( 'Enable the option to use the HTML widget', 'elementor' ); ?>
217 </label>
218 <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>
219 </div>
220 <?php
221 }
222
223 /**
224 * @since 2.0.0
225 * @access public
226 */
227 public function get_go_pro_link_html() {
228 $promotion = $this->get_go_pro_link_content();
229
230 ?>
231 <div class="elementor-role-go-pro">
232 <div class="elementor-role-go-pro__desc"><?php echo esc_html( $promotion['description'] ); ?></div>
233 <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>
234 </div>
235 <?php
236 }
237
238 public function get_go_pro_link_content() {
239 $upgrade_url = 'https://go.elementor.com/go-pro-role-manager/';
240
241 $promotion = [
242 'description' => esc_html__( 'Want to give access only to content?', 'elementor' ),
243 'upgrade_url' => esc_url( $upgrade_url ),
244 'upgrade_text' => esc_html__( 'Upgrade', 'elementor' ),
245 ];
246
247 return Filtered_Promotions_Manager::get_filtered_promotion_data( $promotion, 'elementor/role/custom_promotion', 'upgrade_url' );
248 }
249
250 /**
251 * @since 2.0.0
252 * @access public
253 */
254 public function get_user_restrictions_array() {
255 $user = wp_get_current_user();
256 $user_roles = $user->roles;
257 $options = $this->get_user_restrictions();
258 $restrictions = [];
259 if ( empty( $options ) ) {
260 return $restrictions;
261 }
262
263 foreach ( $user_roles as $role ) {
264 if ( ! isset( $options[ $role ] ) ) {
265 continue;
266 }
267 $restrictions = array_merge( $restrictions, $options[ $role ] );
268 }
269 return array_unique( $restrictions );
270 }
271
272 /**
273 * @since 2.0.0
274 * @access private
275 */
276 private function get_user_restrictions() {
277 static $restrictions = false;
278 if ( ! $restrictions ) {
279 $restrictions = [];
280
281 /**
282 * Editor user restrictions.
283 *
284 * Filters the user restrictions in the editor.
285 *
286 * @since 2.0.0
287 *
288 * @param array $restrictions User restrictions.
289 */
290 $restrictions = apply_filters( 'elementor/editor/user/restrictions', $restrictions );
291 }
292 return $restrictions;
293 }
294
295 /**
296 * @since 2.0.0
297 * @access public
298 *
299 * @param $capability
300 *
301 * @return bool
302 */
303 public function user_can( $capability ) {
304 $options = $this->get_user_restrictions_array();
305
306 if ( in_array( $capability, $options, true ) ) {
307 return false;
308 }
309
310 return true;
311 }
312
313 private function register_editor_one_menu( Menu_Data_Provider $menu_data_provider ) {
314 $menu_data_provider->register_menu( new Editor_One_Role_Manager_Menu() );
315 }
316
317 private function is_editor_one_active(): bool {
318 return (bool) Plugin::instance()->modules_manager->get_modules( 'editor-one' );
319 }
320
321 /**
322 * @since 2.0.0
323 * @access public
324 */
325 public function __construct() {
326 parent::__construct();
327
328 add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
329 if ( ! $this->is_editor_one_active() ) {
330 $this->register_admin_menu( $admin_menu );
331 }
332 }, Settings::ADMIN_MENU_PRIORITY + 10 );
333
334 add_action( 'elementor/editor-one/menu/register', function ( Menu_Data_Provider $menu_data_provider ) {
335 $this->register_editor_one_menu( $menu_data_provider );
336 } );
337
338 add_action( 'elementor/role/restrictions/controls', [ $this, 'add_json_enable_control' ] );
339 add_action( 'elementor/role/restrictions/controls', [ $this, 'add_custom_html_enable_control' ] );
340 add_action( 'elementor/role/restrictions/controls', [ $this, 'get_go_pro_link_html' ] );
341
342 add_filter( 'elementor/editor/user/restrictions', [ $this, 'get_role_manager_advanced_options' ] );
343 }
344 }
345