| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Settings { |
| 4 |
|
| 5 |
use WPDataAccess\Utilities\WPDA_Message_Box; |
| 6 |
use WPDataAccess\WPDA; |
| 7 |
|
| 8 |
class WPDA_Settings_ManageRoles extends WPDA_Settings { |
| 9 |
|
| 10 |
/** |
| 11 |
* Add roles tab content |
| 12 |
* |
| 13 |
* See class documentation for flow explanation. |
| 14 |
* |
| 15 |
* @since 2.7.0 |
| 16 |
*/ |
| 17 |
protected function add_content() { |
| 18 |
$wp_default_roles = array( |
| 19 |
'administrator' => true, |
| 20 |
'editor' => true, |
| 21 |
'author' => true, |
| 22 |
'contributor' => true, |
| 23 |
'subscriber' => true, |
| 24 |
); |
| 25 |
|
| 26 |
if ( isset( $_REQUEST['action'] ) ) { |
| 27 |
// Security check. |
| 28 |
if ( 'delete' === $_REQUEST['action'] ) { |
| 29 |
$wp_nonce = isset( $_REQUEST['_wpnoncedelrole'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnoncedelrole'] ) ) : ''; // input var okay. |
| 30 |
if ( ! wp_verify_nonce( $wp_nonce, 'wpda-manage-roles-settings-' . WPDA::get_current_user_login() ) ) { |
| 31 |
wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); |
| 32 |
} |
| 33 |
} else { |
| 34 |
$wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // input var okay. |
| 35 |
if ( ! wp_verify_nonce( $wp_nonce, 'wpda-manage-roles-settings-' . WPDA::get_current_user_login() ) ) { |
| 36 |
wp_die( __( 'ERROR: Not authorized', 'wp-data-access' ) ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
if ( 'save' === $_REQUEST['action'] ) { |
| 41 |
WPDA::set_option( |
| 42 |
WPDA::OPTION_WPDA_ENABLE_ROLE_MANAGEMENT, |
| 43 |
isset( $_REQUEST['enable_role_management'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['enable_role_management'] ) ) : 'off' // input var okay. |
| 44 |
); |
| 45 |
|
| 46 |
WPDA::set_option( |
| 47 |
WPDA::OPTION_WPDA_USE_ROLES_IN_SHORTCODE, |
| 48 |
isset( $_REQUEST['use_roles_in_shortcode'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['use_roles_in_shortcode'] ) ) : 'off' // input var okay. |
| 49 |
); |
| 50 |
|
| 51 |
if ( isset( $_REQUEST['wpda_role_name'] ) && is_array( $_REQUEST['wpda_role_name'] ) && |
| 52 |
isset( $_REQUEST['wpda_role_label'] ) && is_array( $_REQUEST['wpda_role_label'] ) |
| 53 |
) { |
| 54 |
$no_roles = count( $_REQUEST['wpda_role_name'] );//phpcs:ignore - 8.1 proof |
| 55 |
for ( $i = 0; $i < $no_roles; $i ++ ) { |
| 56 |
$sanitized_new_role_name = sanitize_text_field( wp_unslash( $_REQUEST['wpda_role_name'][ $i ] ) ); // input var okay. |
| 57 |
$sanitized_new_role_label = sanitize_text_field( wp_unslash( $_REQUEST['wpda_role_label'][ $i ] ) ); // input var okay. |
| 58 |
add_role( $sanitized_new_role_name, $sanitized_new_role_label ); |
| 59 |
} |
| 60 |
} |
| 61 |
$msg = new WPDA_Message_Box( |
| 62 |
array( |
| 63 |
'message_text' => __( 'Settings saved', 'wp-data-access' ), |
| 64 |
) |
| 65 |
); |
| 66 |
$msg->box(); |
| 67 |
} elseif ( 'delete' === $_REQUEST['action'] ) { |
| 68 |
if ( isset( $_REQUEST['delete_role_name'] ) ) { |
| 69 |
$sanitized_role_name = sanitize_text_field( wp_unslash( $_REQUEST['delete_role_name'] ) ); // input var okay. |
| 70 |
$all_users = get_users( |
| 71 |
array( 'role' => $sanitized_role_name ) |
| 72 |
); |
| 73 |
foreach ( $all_users as $user ) { |
| 74 |
$wp_user = new \WP_User( $user->ID ); |
| 75 |
$wp_user->remove_role( $sanitized_role_name ); |
| 76 |
} |
| 77 |
remove_role( $sanitized_role_name ); |
| 78 |
|
| 79 |
$msg = new WPDA_Message_Box( |
| 80 |
array( |
| 81 |
'message_text' => __( 'Settings saved', 'wp-data-access' ), |
| 82 |
) |
| 83 |
); |
| 84 |
$msg->box(); |
| 85 |
} |
| 86 |
} elseif ( 'setdefaults' === $_REQUEST['action'] ) { |
| 87 |
// Set back to default values. |
| 88 |
WPDA::set_option( WPDA::OPTION_WPDA_ENABLE_ROLE_MANAGEMENT ); |
| 89 |
WPDA::set_option( WPDA::OPTION_WPDA_USE_ROLES_IN_SHORTCODE ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$enable_role_management = WPDA::get_option( WPDA::OPTION_WPDA_ENABLE_ROLE_MANAGEMENT ); |
| 94 |
$use_roles_in_shortcode = WPDA::get_option( WPDA::OPTION_WPDA_USE_ROLES_IN_SHORTCODE ); |
| 95 |
?> |
| 96 |
<form id="wpda_settings_manage_roles" |
| 97 |
method="post" |
| 98 |
action="?page=<?php echo esc_attr( $this->page ); ?>&tab=roles"> |
| 99 |
|
| 100 |
<table class="wpda-table-settings"> |
| 101 |
|
| 102 |
<tr> |
| 103 |
<th> |
| 104 |
<?php echo __( 'Plugin Role Management', 'wp-data-access' ); ?> |
| 105 |
</th> |
| 106 |
<td> |
| 107 |
<label> |
| 108 |
<input type="checkbox" name="enable_role_management" |
| 109 |
<?php echo 'on' === $enable_role_management ? 'checked' : ''; ?>/> |
| 110 |
<?php echo __( 'Enable role management', 'wp-data-access' ); ?> |
| 111 |
</label> |
| 112 |
<br/> |
| 113 |
<label> |
| 114 |
<input type="checkbox" name="use_roles_in_shortcode" |
| 115 |
<?php echo 'on' === $use_roles_in_shortcode ? 'checked' : ''; ?>/> |
| 116 |
<?php echo __( 'Use roles in Data Projects shortcodes', 'wp-data-access' ); ?> |
| 117 |
</label> |
| 118 |
</td> |
| 119 |
</tr> |
| 120 |
|
| 121 |
<tr> |
| 122 |
<th> |
| 123 |
<?php echo __( 'Available Roles', 'wp-data-access' ); ?> |
| 124 |
</th> |
| 125 |
<td> |
| 126 |
<div id="list_roles"> |
| 127 |
<?php |
| 128 |
global $wp_roles; |
| 129 |
foreach ( $wp_roles->roles as $role => $val ) { |
| 130 |
$is_wp_role = isset( $wp_default_roles[ $role ] ); |
| 131 |
$role_label = isset( $val['name'] ) ? $val['name'] : $role; |
| 132 |
?> |
| 133 |
<div id="<?php echo esc_attr( $role ); ?>"> |
| 134 |
<span class="dashicons <?php echo $is_wp_role ? 'dashicons-wordpress' : 'dashicons-trash'; ?> wpda_tooltip" |
| 135 |
style="font-size: 14px; vertical-align: text-top;<?php echo $is_wp_role ? '' : ' cursor: pointer;'; ?>" |
| 136 |
<?php echo $is_wp_role ? '' : 'title="Delete role"'; ?>></span> |
| 137 |
<?php echo esc_attr( $role_label ); ?> |
| 138 |
</div> |
| 139 |
<?php |
| 140 |
} |
| 141 |
?> |
| 142 |
</div> |
| 143 |
<p> |
| 144 |
<a href="javascript:void(0)" class="button" onclick="add_new_role()">Add |
| 145 |
New Role</a> |
| 146 |
</p> |
| 147 |
</td> |
| 148 |
</tr> |
| 149 |
|
| 150 |
</table> |
| 151 |
|
| 152 |
<div class="wpda-table-settings-button"> |
| 153 |
<input type="hidden" name="action" value="save"/> |
| 154 |
<button type="submit" class="button button-primary"> |
| 155 |
<i class="fas fa-check wpda_icon_on_button"></i> |
| 156 |
<?php echo __( 'Save Manage Roles Settings', 'wp-data-access' ); ?> |
| 157 |
</button> |
| 158 |
<a href="javascript:void(0)" |
| 159 |
onclick="if (confirm('<?php echo __( 'Reset to defaults?', 'wp-data-access' ); ?>')) { |
| 160 |
jQuery('input[name=\'action\']').val('setdefaults'); |
| 161 |
jQuery('#wpda_settings_manage_roles').trigger('submit'); |
| 162 |
}" |
| 163 |
class="button button-secondary"> |
| 164 |
<i class="fas fa-times-circle wpda_icon_on_button"></i> |
| 165 |
<?php echo __( 'Reset Manage Roles Settings To Defaults', 'wp-data-access' ); ?> |
| 166 |
</a> |
| 167 |
</div> |
| 168 |
<?php wp_nonce_field( 'wpda-manage-roles-settings-' . WPDA::get_current_user_login(), '_wpnonce', false ); ?> |
| 169 |
|
| 170 |
</form> |
| 171 |
|
| 172 |
<form id="delete_role_form" |
| 173 |
method="post" |
| 174 |
action="?page=<?php echo esc_attr( $this->page ); ?>&tab=roles"> |
| 175 |
<input type="hidden" id="delete_role_name" name="delete_role_name" value=""> |
| 176 |
<input type="hidden" name="action" value="delete"> |
| 177 |
<?php wp_nonce_field( 'wpda-manage-roles-settings-' . WPDA::get_current_user_login(), '_wpnoncedelrole', false ); ?> |
| 178 |
</form> |
| 179 |
|
| 180 |
|
| 181 |
<script type='text/javascript'> |
| 182 |
function add_new_role() { |
| 183 |
jQuery('#list_roles').append( |
| 184 |
'<div>' + |
| 185 |
' <span class="dashicons dashicons-trash" style="font-size: 14px; vertical-align: text-top; cursor: pointer;" onclick="jQuery(this).parent().remove();"></span>' + |
| 186 |
' <label for="wpda_role_name[]">Name: </label><input name="wpda_role_name[]" style="vertical-align: middle; text-transform: lowercase;"/>' + |
| 187 |
' <label for="wpda_role_label[]">Label: </label><input name="wpda_role_label[]" style="vertical-align: middle;"/>' + |
| 188 |
'</div>'); |
| 189 |
} |
| 190 |
|
| 191 |
jQuery('.dashicons-trash').on('click', function (e) { |
| 192 |
if (confirm('<?php echo __( 'Delete role?', 'wp-data-access' ) . '\n' . __( 'Role will be removed from all users.', 'wp-data-access' ) . '\n' . __( 'This action cannot be undone!', 'wp-data-access' ); ?>')) { |
| 193 |
parent = jQuery(e.target).parent(); |
| 194 |
parent_id = parent.attr('id'); |
| 195 |
jQuery('#delete_role_name').val(parent_id); |
| 196 |
jQuery('#delete_role_form').submit(); |
| 197 |
} |
| 198 |
}); |
| 199 |
</script> |
| 200 |
|
| 201 |
<?php |
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|