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

327 lines 9.3 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\Validate_Promotion;
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 private 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 * @param $promotion
63 * @param string $upgrade_url
64 * @return mixed
65 */
66 private function replace_url( $promotion, string $upgrade_url ) {
67 if ( ! Validate_Promotion::domain_is_on_elementor_dot_com( $promotion['upgrade_url'] ) ) {
68 $promotion['upgrade_url'] = $upgrade_url;
69 }
70 return $promotion;
71 }
72
73 /**
74 * @since 2.0.0
75 * @access protected
76 */
77 protected function create_tabs() {
78 $validation_class = 'Elementor\Settings_Validations';
79 return [
80 'general' => [
81 'label' => esc_html__( 'General', 'elementor' ),
82 'sections' => [
83 'tools' => [
84 'fields' => [
85 'exclude_user_roles' => [
86 'label' => esc_html__( 'Exclude Roles', 'elementor' ),
87 'field_args' => [
88 'type' => 'checkbox_list_roles',
89 'exclude' => [ 'super_admin', 'administrator' ],
90 ],
91 'setting_args' => [
92 'sanitize_callback' => [ $validation_class, 'checkbox_list' ],
93 ],
94 ],
95 self::ROLE_MANAGER_ADVANCED => [
96 'field_args' => [
97 'type' => 'raw_html',
98 'html' => '',
99 ],
100 'setting_args' => [
101 'sanitize_callback' => [ $this, 'save_advanced_options' ],
102 ],
103 ],
104 ],
105 ],
106 ],
107 ],
108 ];
109 }
110
111 public function save_advanced_options( $input ) {
112 return $input;
113 }
114
115 /**
116 * @since 2.0.0
117 * @access public
118 */
119 public function display_settings_page() {
120 $this->get_tabs();
121 ?>
122 <div class="wrap">
123 <h1 class="wp-heading-inline"><?php echo esc_html( $this->get_page_title() ); ?></h1>
124
125 <div id="elementor-role-manager">
126 <h3><?php echo esc_html__( 'Manage What Your Users Can Edit In Elementor', 'elementor' ); ?></h3>
127 <form id="elementor-settings-form" method="post" action="options.php">
128 <?php
129 settings_fields( static::PAGE_ID );
130 echo '<div class="elementor-settings-form-page elementor-active">';
131 foreach ( get_editable_roles() as $role_slug => $role_data ) {
132 if ( 'administrator' === $role_slug ) {
133 continue;
134 }
135 $this->display_role_controls( $role_slug, $role_data );
136 }
137 submit_button();
138 ?>
139 </form>
140 </div>
141 </div><!-- /.wrap -->
142 <?php
143 }
144
145 /**
146 * @since 2.0.0
147 * @access private
148 *
149 * @param string $role_slug The role slug.
150 * @param array $role_data An array with role data.
151 */
152 private function display_role_controls( $role_slug, $role_data ) {
153 static $excluded_options = false;
154 if ( false === $excluded_options ) {
155 $excluded_options = $this->get_role_manager_options();
156 }
157
158 ?>
159 <div class="elementor-role-row <?php echo esc_attr( $role_slug ); ?>">
160 <div class="elementor-role-label">
161 <span class="elementor-role-name"><?php echo esc_html( translate_user_role( $role_data['name'] ) ); ?></span>
162 <span data-excluded-label="<?php esc_attr_e( 'Role Excluded', 'elementor' ); ?>" class="elementor-role-excluded-indicator"></span>
163 <span class="elementor-role-toggle"><span class="dashicons dashicons-arrow-down"></span></span>
164 </div>
165 <div class="elementor-role-controls hidden">
166 <div class="elementor-role-control">
167 <label>
168 <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 ); ?>>
169 <?php echo esc_html__( 'No access to editor', 'elementor' ); ?>
170 </label>
171 </div>
172 <div class="elementor-role-controls-advanced">
173 <?php
174 /**
175 * Role restrictions controls.
176 *
177 * Fires after the role manager checkbox that allows the user to
178 * exclude the role.
179 *
180 * This filter allows developers to add custom controls to the role
181 * manager.
182 *
183 * @since 2.0.0
184 *
185 * @param string $role_slug The role slug.
186 * @param array $role_data An array with role data.
187 */
188 do_action( 'elementor/role/restrictions/controls', $role_slug, $role_data );
189 ?>
190 </div>
191 </div>
192 </div>
193 <?php
194 }
195
196 public function add_json_enable_control( $role_slug ) {
197 $value = 'json-upload';
198 $id = self::ROLE_MANAGER_ADVANCED . '_' . $role_slug . '_' . $value;
199 $name = 'elementor_' . self::ROLE_MANAGER_ADVANCED . '[' . $role_slug . '][]';
200
201 $advanced_options = $this->get_user_advanced_options();
202 $checked = isset( $advanced_options[ $role_slug ] ) ? $advanced_options[ $role_slug ] : [];
203 ?>
204 <div class="elementor-role-control">
205 <label for="<?php echo esc_attr( $id ); ?>">
206 <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 ); ?>>
207 <?php echo esc_html__( 'Enable the option to upload JSON files', 'elementor' ); ?>
208 </label>
209 <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>
210 </div>
211 <?php
212 }
213 /**
214 * @since 2.0.0
215 * @access public
216 */
217 public function get_go_pro_link_html() {
218 $promotion = $this->get_go_pro_link_content();
219
220 ?>
221 <div class="elementor-role-go-pro">
222 <div class="elementor-role-go-pro__desc"><?php echo esc_html( $promotion['description'] ); ?></div>
223 <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>
224 </div>
225 <?php
226 }
227
228 public function get_go_pro_link_content() {
229 $upgrade_url = 'https://go.elementor.com/go-pro-role-manager/';
230
231 $promotion = [
232 'description' => esc_html__( 'Want to give access only to content?', 'elementor' ),
233 'upgrade_url' => esc_url( $upgrade_url ),
234 'upgrade_text' => esc_html__( 'Upgrade', 'elementor' ),
235 ];
236
237 $new_promotion = apply_filters( 'elementor/role/custom_promotion', $promotion );
238
239 if ( ! empty( $new_promotion ) && count( $new_promotion ) <= count( $promotion ) ) {
240 $promotion = $new_promotion;
241 $promotion = $this->replace_url( $promotion, $upgrade_url );
242 }
243
244 return $promotion;
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 if ( ! Plugin::$instance->experiments->is_feature_active( 'admin_menu_rearrangement' ) ) {
318 add_action( 'elementor/admin/menu/register', function ( Admin_Menu_Manager $admin_menu ) {
319 $this->register_admin_menu( $admin_menu );
320 }, Settings::ADMIN_MENU_PRIORITY + 10 );
321 }
322
323 add_action( 'elementor/role/restrictions/controls', [ $this, 'add_json_enable_control' ] );
324 add_action( 'elementor/role/restrictions/controls', [ $this, 'get_go_pro_link_html' ] );
325 }
326 }
327