| 1 |
<?php |
| 2 |
namespace Elementor\Core\RoleManager; |
| 3 |
|
| 4 |
use Elementor\Settings_Page; |
| 5 |
use Elementor\Settings; |
| 6 |
use Elementor\Utils; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Role_Manager extends Settings_Page { |
| 13 |
|
| 14 |
const PAGE_ID = 'elementor-role-manager'; |
| 15 |
|
| 16 |
const ROLE_MANAGER_OPTION_NAME = 'exclude_user_roles'; |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 2.0.0 |
| 20 |
* @access public |
| 21 |
*/ |
| 22 |
public function get_role_manager_options() { |
| 23 |
return get_option( 'elementor_' . self::ROLE_MANAGER_OPTION_NAME, [] ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @since 2.0.0 |
| 28 |
* @access protected |
| 29 |
*/ |
| 30 |
protected function get_page_title() { |
| 31 |
return __( 'Role Manager', 'elementor' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @since 2.0.0 |
| 36 |
* @access public |
| 37 |
*/ |
| 38 |
public function register_admin_menu() { |
| 39 |
add_submenu_page( |
| 40 |
Settings::PAGE_ID, |
| 41 |
$this->get_page_title(), |
| 42 |
$this->get_page_title(), |
| 43 |
'manage_options', |
| 44 |
self::PAGE_ID, |
| 45 |
[ $this, 'display_settings_page' ] |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @since 2.0.0 |
| 51 |
* @access protected |
| 52 |
*/ |
| 53 |
protected function create_tabs() { |
| 54 |
$validation_class = 'Elementor\Settings_Validations'; |
| 55 |
return [ |
| 56 |
'general' => [ |
| 57 |
'label' => __( 'General', 'elementor' ), |
| 58 |
'sections' => [ |
| 59 |
'tools' => [ |
| 60 |
'fields' => [ |
| 61 |
'exclude_user_roles' => [ |
| 62 |
'label' => __( 'Exclude Roles', 'elementor' ), |
| 63 |
'field_args' => [ |
| 64 |
'type' => 'checkbox_list_roles', |
| 65 |
'exclude' => [ 'super_admin', 'administrator' ], |
| 66 |
], |
| 67 |
'setting_args' => [ |
| 68 |
'sanitize_callback' => [ $validation_class, 'checkbox_list' ], |
| 69 |
], |
| 70 |
], |
| 71 |
], |
| 72 |
], |
| 73 |
], |
| 74 |
], |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @since 2.0.0 |
| 80 |
* @access public |
| 81 |
*/ |
| 82 |
public function display_settings_page() { |
| 83 |
$this->get_tabs(); |
| 84 |
?> |
| 85 |
<div class="wrap"> |
| 86 |
<h1><?php echo esc_html( $this->get_page_title() ); ?></h1> |
| 87 |
|
| 88 |
<div id="elementor-role-manager"> |
| 89 |
<h3><?php echo __( 'Manage What Your Users Can Edit In Elementor', 'elementor' ); ?></h3> |
| 90 |
<form id="elementor-settings-form" method="post" action="options.php"> |
| 91 |
<?php |
| 92 |
settings_fields( static::PAGE_ID ); |
| 93 |
echo '<div class="elementor-settings-form-page elementor-active">'; |
| 94 |
foreach ( get_editable_roles() as $role_slug => $role_data ) { |
| 95 |
if ( 'administrator' === $role_slug ) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
$this->display_role_controls( $role_slug, $role_data ); |
| 99 |
} |
| 100 |
submit_button(); |
| 101 |
?> |
| 102 |
</form> |
| 103 |
</div> |
| 104 |
</div><!-- /.wrap --> |
| 105 |
<?php |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @since 2.0.0 |
| 110 |
* @access private |
| 111 |
* |
| 112 |
* @param string $role_slug The role slug. |
| 113 |
* @param array $role_data An array with role data. |
| 114 |
*/ |
| 115 |
private function display_role_controls( $role_slug, $role_data ) { |
| 116 |
static $excluded_options = false; |
| 117 |
if ( false === $excluded_options ) { |
| 118 |
$excluded_options = $this->get_role_manager_options(); |
| 119 |
} |
| 120 |
|
| 121 |
?> |
| 122 |
<div class="elementor-role-row <?php echo esc_attr( $role_slug ); ?>"> |
| 123 |
<div class="elementor-role-label"> |
| 124 |
<span class="elementor-role-name"><?php echo esc_html( $role_data['name'] ); ?></span> |
| 125 |
<span data-excluded-label="<?php esc_attr_e( 'Role Excluded', 'elementor' ); ?>" class="elementor-role-excluded-indicator"></span> |
| 126 |
<span class="elementor-role-toggle"><span class="dashicons dashicons-arrow-down"></span></span> |
| 127 |
</div> |
| 128 |
<div class="elementor-role-controls hidden"> |
| 129 |
<div class="elementor-role-control"> |
| 130 |
<label> |
| 131 |
<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 ); ?>> |
| 132 |
<?php echo __( 'No access to editor', 'elementor' ); ?> |
| 133 |
</label> |
| 134 |
</div> |
| 135 |
<div> |
| 136 |
<?php |
| 137 |
/** |
| 138 |
* Role restrictions controls. |
| 139 |
* |
| 140 |
* Fires after the role manager checkbox that allows the user to |
| 141 |
* exclude the role. |
| 142 |
* |
| 143 |
* This filter allows developers to add custom controls to the role |
| 144 |
* manager. |
| 145 |
* |
| 146 |
* @since 2.0.0 |
| 147 |
* |
| 148 |
* @param string $role_slug The role slug. |
| 149 |
* @param array $role_data An array with role data. |
| 150 |
*/ |
| 151 |
do_action( 'elementor/role/restrictions/controls', $role_slug, $role_data ); |
| 152 |
?> |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
</div> |
| 156 |
<?php |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @since 2.0.0 |
| 161 |
* @access public |
| 162 |
*/ |
| 163 |
public function get_go_pro_link_html() { |
| 164 |
$pro_link = Utils::get_pro_link( 'https://elementor.com/pro/?utm_source=wp-role-manager&utm_campaign=gopro&utm_medium=wp-dash' ); |
| 165 |
?> |
| 166 |
<div class="elementor-role-go-pro"> |
| 167 |
<div class="elementor-role-go-pro__desc"><?php echo __( 'Want to give access only to content?', 'elementor' ); ?></div> |
| 168 |
<div class="elementor-role-go-pro__link"><a class="elementor-button elementor-button-default elementor-button-go-pro" target="_blank" href="<?php echo esc_url( $pro_link ); ?>"><?php echo __( 'Go Pro', 'elementor' ); ?></a></div> |
| 169 |
</div> |
| 170 |
<?php |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @since 2.0.0 |
| 175 |
* @access public |
| 176 |
*/ |
| 177 |
public function get_user_restrictions_array() { |
| 178 |
$user = wp_get_current_user(); |
| 179 |
$user_roles = $user->roles; |
| 180 |
$options = $this->get_user_restrictions(); |
| 181 |
$restrictions = []; |
| 182 |
if ( empty( $options ) ) { |
| 183 |
return $restrictions; |
| 184 |
} |
| 185 |
|
| 186 |
foreach ( $user_roles as $role ) { |
| 187 |
if ( ! isset( $options[ $role ] ) ) { |
| 188 |
continue; |
| 189 |
} |
| 190 |
$restrictions = array_merge( $restrictions, $options[ $role ] ); |
| 191 |
} |
| 192 |
return array_unique( $restrictions ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @since 2.0.0 |
| 197 |
* @access private |
| 198 |
*/ |
| 199 |
private function get_user_restrictions() { |
| 200 |
static $restrictions = false; |
| 201 |
if ( ! $restrictions ) { |
| 202 |
$restrictions = []; |
| 203 |
|
| 204 |
/** |
| 205 |
* Editor user restrictions. |
| 206 |
* |
| 207 |
* Filters the user restrictions in the editor. |
| 208 |
* |
| 209 |
* @since 2.0.0 |
| 210 |
* |
| 211 |
* @param array $restrictions User restrictions. |
| 212 |
*/ |
| 213 |
$restrictions = apply_filters( 'elementor/editor/user/restrictions', $restrictions ); |
| 214 |
} |
| 215 |
return $restrictions; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @since 2.0.0 |
| 220 |
* @access public |
| 221 |
* |
| 222 |
* @param $capability |
| 223 |
* |
| 224 |
* @return bool |
| 225 |
*/ |
| 226 |
public function user_can( $capability ) { |
| 227 |
$options = $this->get_user_restrictions_array(); |
| 228 |
|
| 229 |
if ( in_array( $capability, $options, true ) ) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @since 2.0.0 |
| 238 |
* @access public |
| 239 |
*/ |
| 240 |
public function __construct() { |
| 241 |
parent::__construct(); |
| 242 |
|
| 243 |
add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 100 ); |
| 244 |
add_action( 'elementor/role/restrictions/controls', [ $this, 'get_go_pro_link_html' ] ); |
| 245 |
} |
| 246 |
} |
| 247 |
|