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