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