| |
| <?php
|
|
|
| add_action('admin_menu', function () {
|
| $user = wp_get_current_user();
|
| if (!in_array('shop_manager_-_reseller_admin', (array) $user->roles)) return;
|
|
|
| add_menu_page(
|
| 'Create Customer Role',
|
| 'Roles',
|
| 'manage_product_terms',
|
| 'create-customer-role',
|
| 'render_custom_role_form',
|
| 'dashicons-groups',
|
| 56
|
| );
|
| });
|
|
|
|
|
| function render_custom_role_form() {
|
| ?>
|
| <div class="wrap">
|
| <h1>Create a Custom Customer Role</h1>
|
| <form method="post">
|
| <?php wp_nonce_field('create_custom_customer_role'); ?>
|
| <table class="form-table">
|
| <tr>
|
| <th><label for="new_role_name">New Role Name</label></th>
|
| <td><input type="text" id="new_role_name" name="new_role_name" required></td>
|
| </tr>
|
| </table>
|
| <p class="submit">
|
| <input type="submit" name="submit_custom_role" class="button-primary" value="Create Role">
|
| </p>
|
| </form>
|
|
|
| <hr>
|
| <h2>Manage Roles</h2>
|
| <table class="widefat striped">
|
| <thead>
|
| <tr>
|
| <th>Role Name</th>
|
| <th>Slug</th>
|
| <th>Actions</th>
|
| </tr>
|
| </thead>
|
| <tbody>
|
| <?php
|
| global $wp_roles;
|
| $excluded_roles = [
|
| 'administrator',
|
| 'editor',
|
| 'author',
|
| 'contributor',
|
| 'subscriber',
|
| 'shop_manager',
|
| 'shop_manager_-_view_only',
|
| 'shop_manager_-_reseller_admin',
|
| ];
|
|
|
| foreach ($wp_roles->roles as $slug => $role) {
|
| if (in_array($slug, $excluded_roles)) continue;
|
|
|
| echo '<tr>';
|
| echo '<td>' . esc_html($role['name']) . '</td>';
|
| echo '<td>' . esc_html($slug) . '</td>';
|
| echo '<td>';
|
|
|
| if ($slug === 'customer') {
|
| echo '<em>Protected Role</em>';
|
| } else {
|
| echo '
|
| <form method="post" style="display:inline;">
|
| ' . wp_nonce_field('rename_role_' . $slug, '_wpnonce', true, false) . '
|
| <input type="text" name="new_role_name" placeholder="New Name" required>
|
| <input type="hidden" name="role_to_rename" value="' . esc_attr($slug) . '">
|
| <input type="submit" name="rename_role" class="button" value="Rename">
|
| </form>
|
| <form method="post" style="display:inline; margin-left:10px;" onsubmit="return confirm(\'Are you sure you want to delete this role? This action is permanent and cannot be undone. Any users currently assigned to this role will be unassigned. Please confirm you understand the consequences before proceeding.\')">
|
| ' . wp_nonce_field('delete_role_' . $slug, '_wpnonce', true, false) . '
|
| <input type="hidden" name="role_to_delete" value="' . esc_attr($slug) . '">
|
| <input type="submit" name="delete_role" class="button button-secondary" value="Delete">
|
| </form>';
|
| }
|
|
|
| echo '</td></tr>';
|
| }
|
| ?>
|
| </tbody>
|
| </table>
|
| </div>
|
| <?php
|
| }
|
|
|
|
|
| add_action('admin_notices', function () {
|
| if (!isset($_GET['page']) || $_GET['page'] !== 'create-customer-role') return;
|
|
|
| if (!empty($_GET['created'])) {
|
| $name = sanitize_text_field($_GET['created']);
|
| echo '<div class="notice notice-success is-dismissible"><p>✅ A new customer role named <strong>' . esc_html($name) . '</strong> has been created successfully.</p></div>';
|
| }
|
|
|
| if (!empty($_GET['renamed'])) {
|
| $name = sanitize_text_field($_GET['renamed']);
|
| echo '<div class="notice notice-warning is-dismissible"><p>✏️ The role has been renamed to <strong>' . esc_html($name) . '</strong>.</p></div>';
|
| }
|
|
|
| if (!empty($_GET['deleted'])) {
|
| $name = sanitize_text_field($_GET['deleted']);
|
| echo '<div class="notice notice-error is-dismissible"><p>🗑️ The role <strong>' . esc_html($name) . '</strong> has been deleted and users unassigned.</p></div>';
|
| }
|
| });
|
|
|
|
|
| add_action('admin_init', function () {
|
| if (!isset($_POST['submit_custom_role'])) return;
|
| if (!current_user_can('shop_manager_-_reseller_admin')) return;
|
| if (!wp_verify_nonce($_POST['_wpnonce'], 'create_custom_customer_role')) wp_die('Security check failed');
|
|
|
| $display_name = sanitize_text_field($_POST['new_role_name']);
|
| $role_slug = sanitize_title_with_dashes($display_name);
|
|
|
| if (get_role($role_slug)) {
|
| add_action('admin_notices', function () use ($display_name) {
|
| echo '<div class="notice notice-error"><p>⚠️ Role already exists: <strong>' . esc_html($display_name) . '</strong></p></div>';
|
| });
|
| return;
|
| }
|
|
|
| $base_role = get_role('customer');
|
| if (!$base_role) wp_die('Base role "customer" not found.');
|
|
|
| add_role($role_slug, $display_name, $base_role->capabilities);
|
| wp_redirect(admin_url('admin.php?page=create-customer-role&created=' . urlencode($display_name)));
|
| exit;
|
| });
|
|
|
|
|
| add_action('admin_init', function () {
|
| if (!isset($_POST['rename_role'])) return;
|
| if (!current_user_can('shop_manager_-_reseller_admin')) return;
|
|
|
| $old_slug = sanitize_key($_POST['role_to_rename']);
|
| $new_name = sanitize_text_field($_POST['new_role_name']);
|
| $new_slug = sanitize_title_with_dashes($new_name);
|
|
|
| if (!wp_verify_nonce($_POST['_wpnonce'], 'rename_role_' . $old_slug)) {
|
| wp_die('Security check failed');
|
| }
|
|
|
| global $wp_roles;
|
| if (!isset($wp_roles->roles[$old_slug])) return;
|
| if ($old_slug === 'customer') return;
|
|
|
| $capabilities = $wp_roles->roles[$old_slug]['capabilities'];
|
|
|
|
|
| $users = get_users(['role' => $old_slug]);
|
| foreach ($users as $user) {
|
| $user->remove_role($old_slug);
|
| $user->add_role($new_slug);
|
| }
|
|
|
| remove_role($old_slug);
|
| add_role($new_slug, $new_name, $capabilities);
|
|
|
| wp_redirect(admin_url('admin.php?page=create-customer-role&renamed=' . urlencode($new_name)));
|
| exit;
|
| });
|
|
|
|
|
| add_action('admin_init', function () {
|
| if (!isset($_POST['delete_role'])) return;
|
| if (!current_user_can('shop_manager_-_reseller_admin')) return;
|
|
|
| $slug = sanitize_key($_POST['role_to_delete']);
|
| if ($slug === 'customer') return;
|
|
|
| if (!wp_verify_nonce($_POST['_wpnonce'], 'delete_role_' . $slug)) {
|
| wp_die('Security check failed');
|
| }
|
|
|
|
|
| $users = get_users(['role' => $slug]);
|
| foreach ($users as $user) {
|
| $user->remove_role($slug);
|
| }
|
|
|
| remove_role($slug);
|
| wp_redirect(admin_url('admin.php?page=create-customer-role&deleted=' . urlencode($slug)));
|
| exit;
|
| });
|
| |
| |
Comments