Skip to content

Commit fe69110

Browse files
author
Angelo Rocha
committed
First commit
1 parent f6c1046 commit fe69110

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+23373
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
# wpss-ultimate-user-management
1+
This plugin allows efficient management of users, roles and capabilities. Create, edit and delete user permissions faster and easier.
2+
3+
### Features:
4+
- Lists available roles;
5+
- Add custom roles;
6+
- Delete custom roles;
7+
- Permissions based on custom roles;
8+
- User management;
9+
- Adds more than one role for a user;
10+
- Set default role for user without roles;
11+
- Access to admin menu by role;
12+
- Auto add role to new users;
13+
14+
### Installation
15+
1. Upload plugin folder to the `/wp-content/plugins/` directory
16+
2. Activate the plugin through the 'Plugins' menu in WordPress
17+
3. Enjoy
18+
19+
### Screenshots
20+
![Screen](./assets/screens/screenshot-1.png "Screen")
21+
![Screen](./assets/screens/screenshot-2.png "Screen")
22+
![Screen](./assets/screens/screenshot-3.png "Screen")
23+
![Screen](./assets/screens/screenshot-4.png "Screen")
24+
![Screen](./assets/screens/screenshot-5.png "Screen")
25+
![Screen](./assets/screens/screenshot-6.png "Screen")
26+
![Screen](./assets/screens/screenshot-7.png "Screen")

admin/classes/WPSSAdminFrontend.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace WpssUserManager\Admin;
4+
5+
/** Prevent direct access */
6+
if ( ! function_exists( 'add_action' ) ):
7+
header( 'HTTP/1.0 403 Forbidden' );
8+
exit;
9+
endif;
10+
11+
/**
12+
* Class WPSSAdminFrontend
13+
* @since 1.0.0
14+
*/
15+
class WPSSAdminFrontend {
16+
17+
/**
18+
* Render main admin page template
19+
* @return void
20+
* @since 1.0.0
21+
*/
22+
public static function admin_main_content(): void {
23+
$args = [
24+
'template' => 'main',
25+
'args' => '',
26+
];
27+
self::render_template( $args );
28+
}
29+
30+
/**
31+
* Return plugin admin menu nav
32+
* @return array
33+
* @since 1.0.0
34+
*/
35+
public static function nav_menu_tabs(): array {
36+
return [
37+
'roles-tab' => __( 'Roles List', 'wpss-ultimate-user-management' ),
38+
'menus-tab' => __( 'Menu Items', 'wpss-ultimate-user-management' ),
39+
'caps-tab' => __( 'Capabilities List', 'wpss-ultimate-user-management' ),
40+
'users-tab' => __( 'User Management', 'wpss-ultimate-user-management' ),
41+
'settings-tab' => __( 'Settings', 'wpss-ultimate-user-management' ),
42+
];
43+
}
44+
45+
/**
46+
* Get plugin admin templates
47+
*
48+
* @param array $template template name and args, use keys 'template' to set
49+
* template name and ...args to pass another values
50+
*
51+
* @return void
52+
* @since 1.0.0
53+
*/
54+
public static function render_template( array $template ): void {
55+
if ( in_array( $template['template'], self::template_whitelist() ) ) {
56+
$file_path = WPSS_URCM_PLUGIN_PATH . "admin/templates/{$template['template']}.php";
57+
$output = __( 'Template not found...', 'wpss-ultimate-user-management' );
58+
if ( file_exists( $file_path ) ):
59+
ob_start();
60+
require $file_path;
61+
$output = ob_get_clean();
62+
endif;
63+
echo wp_kses( $output, self::sanitize_output() );
64+
}
65+
}
66+
67+
/**
68+
* Sanitize html output
69+
* @return array
70+
* @since 1.0.0
71+
*/
72+
public static function sanitize_output(): array {
73+
return [
74+
'div' => [ 'class' => [], 'id' => [] ],
75+
'table' => [ 'class' => [], 'id' => [] ],
76+
'thead' => [ 'class' => [], 'id' => [] ],
77+
'tr' => [ 'class' => [], 'id' => [] ],
78+
'td' => [ 'class' => [], 'id' => [], 'colspan' => [] ],
79+
'th' => [ 'scope' => [] ],
80+
'caption' => [ 'class' => [] ],
81+
'tbody' => [ 'class' => [], 'id' => [] ],
82+
'tfoot' => [ 'class' => [], 'id' => [] ],
83+
'a' => [ 'href' => [], 'title' => [], 'class' => [], 'id' => [], 'target' => [] ],
84+
'p' => [ 'class' => [], 'id' => [] ],
85+
'hr' => [],
86+
'ul' => [ 'class' => [], 'id' => [] ],
87+
'li' => [ 'label' => [], 'class' => [] ],
88+
'h3' => [ 'class' => [], 'id' => [] ],
89+
'u' => [],
90+
'br' => [],
91+
'img' => [ 'alt' => [], 'src' => [], 'class' => [], 'id' => [] ],
92+
'strong' => [ 'class' => [], 'id' => [] ],
93+
'span' => [
94+
'class' => [],
95+
'id' => [],
96+
'data-role-id' => [],
97+
'data-role-name' => [],
98+
'data-user-id' => [],
99+
'title' => [],
100+
],
101+
'form' => [ 'method' => [], 'action' => [], 'class' => [] ],
102+
'label' => [ 'for' => [], 'class' => [], 'id' => [] ],
103+
'input' => [
104+
'type' => [],
105+
'name' => [],
106+
'value' => [],
107+
'id' => [],
108+
'class' => [],
109+
'required' => [],
110+
'checked' => [],
111+
'placeholder' => [],
112+
'title' => [],
113+
],
114+
'select' => [ 'name' => [], 'class' => [], 'id' => [], 'required' => [], 'onchange' => [], ],
115+
'option' => [ 'value' => [], 'selected' => [] ],
116+
'button' => [ 'type' => [], 'class' => [], 'id' => [] ],
117+
];
118+
}
119+
120+
/**
121+
* Define allowed templates
122+
* @return array
123+
* @since 1.0.0
124+
*/
125+
public static function template_whitelist(): array {
126+
return [
127+
'caps-tab',
128+
'main',
129+
'menus-tab',
130+
'roles-tab',
131+
'users-tab',
132+
'settings-tab',
133+
'content/caps-actions',
134+
'content/user-details',
135+
'content/users-table',
136+
'messages/user-role-add',
137+
'messages/user-role-remove',
138+
'messages/add-role-cap',
139+
'messages/remove-role-cap',
140+
];
141+
}
142+
}

admin/classes/WPSSAdminPages.php

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?php
2+
3+
namespace WpssUserManager\Admin;
4+
5+
use JetBrains\PhpStorm\NoReturn;
6+
7+
/** Prevent direct access */
8+
if ( ! function_exists( 'add_action' ) ):
9+
header( 'HTTP/1.0 403 Forbidden' );
10+
exit;
11+
endif;
12+
13+
/**
14+
* Class WPSSAdminPages
15+
* @since 1.0.0
16+
*/
17+
class WPSSAdminPages {
18+
19+
/**
20+
* Instance of this class.
21+
*
22+
* @var object|null
23+
* @since 1.0.0
24+
*/
25+
protected static ?object $instance = null;
26+
27+
/**
28+
* Get all admin menu pages
29+
* @since 1.0.0
30+
* @var array
31+
*/
32+
private static array $get_menus = [];
33+
34+
/**
35+
* WordPress option to control admin menus access
36+
* @since 1.0.0
37+
* @var string
38+
*/
39+
private static string $admin_menu_perms_option = 'wpss_admin_menu_access';
40+
41+
public function __construct() {
42+
add_action( 'admin_menu', [ $this, 'get_menu_list' ] );
43+
add_action( 'admin_init', [ $this, 'remove_menu_items_from_role' ], 20 );
44+
45+
add_action( 'wp_ajax_menage_admin_menu_options_action', [ $this, 'insert_options_action' ] );
46+
add_action( 'wp_ajax_nopriv_menage_admin_menu_options_action', [ $this, 'insert_options_action' ] );
47+
}
48+
49+
/**
50+
* Get class instance
51+
*
52+
* @return object
53+
* @since 1.0.0
54+
*/
55+
public static function instance(): object {
56+
if ( is_null( self::$instance ) ):
57+
self::$instance = new self();
58+
endif;
59+
60+
return self::$instance;
61+
}
62+
63+
/**
64+
* Insert options action
65+
* @since 1.0.0
66+
*/
67+
#[NoReturn] public static function insert_options_action(): void {
68+
WPSSUserRolesCapsManager::wpss_ajax_check_referer();
69+
$get_data = WPSSPostGet::post('wpss_admin_menus');
70+
parse_str( $get_data, $menu_data );
71+
$key = wp_strip_all_tags( $menu_data['wpss-get-role-to-remove-menu'] );
72+
$val = [];
73+
if ( ! empty( $menu_data['wpss-show-menu-item'] ) ) {
74+
$val = array_map( 'wp_strip_all_tags', (array) $menu_data['wpss-show-menu-item'] );
75+
}
76+
$format_data = [ $key => $val ];
77+
if ( empty( $key ) ):
78+
echo esc_html__( 'Select a valid role', 'wpss-ultimate-user-management' );
79+
exit;
80+
endif;
81+
self::instance()->set_option( wp_json_encode( $format_data ) );
82+
echo esc_html__( 'Options updated successfully', 'wpss-ultimate-user-management' );
83+
exit;
84+
}
85+
86+
/**
87+
* Remove admin menus from a role
88+
* @since 1.0.0
89+
*/
90+
public function remove_menu_items_from_role(): void {
91+
if ( ! empty( self::get_option() ) ):
92+
global $menu;
93+
foreach ( self::get_option() as $get_role => $get_menu ):
94+
if ( current_user_can( $get_role ) && ! is_super_admin() ):
95+
foreach ( $get_menu as $remove_menu ):
96+
/** @var array $menu Avoid php warnings, related bug
97+
* here: https://core.trac.wordpress.org/ticket/23767
98+
* Some menus are not removed in the admin_menu hook, to
99+
* solve this problem this method is linked to the admin_init hook.
100+
*/
101+
$menu[] = $remove_menu;
102+
remove_menu_page( $remove_menu );
103+
endforeach;
104+
endif;
105+
endforeach;
106+
endif;
107+
}
108+
109+
/**
110+
* Get filtered admin menus
111+
* @return array
112+
* @since 1.0.0
113+
*/
114+
public static function get_menu_list(): array {
115+
/** @var $menus
116+
* Key 0: Menu name
117+
* Key 1: Menu capabilities
118+
* Key 2: Menu ID (used to unset)
119+
*/
120+
$menus = self::instance()->get_admin_menu();
121+
$get_menus = [];
122+
foreach ( $menus as $menu ):
123+
if ( $menu[1] !== 'read' ):
124+
/** @var array $menu_title remove menu notifications from option title */
125+
preg_match( '/(?<=^|>).*?(?=<|$)/s', $menu[0], $menu_title );
126+
$get_menus[ $menu[2] ] = esc_attr( $menu_title[0] );
127+
endif;
128+
endforeach;
129+
130+
return $get_menus;
131+
}
132+
133+
/**
134+
* Get option value
135+
* @return array
136+
* @since 1.0.0
137+
*/
138+
public static function get_option(): array {
139+
$instance = self::instance();
140+
$output = [];
141+
if ( $instance->option_exists() ):
142+
$output = json_decode( WPSSPluginHelper::get_option( self::$admin_menu_perms_option ), true );
143+
endif;
144+
145+
return $output;
146+
}
147+
148+
/**
149+
* Set access options
150+
*
151+
* @param string $value
152+
*
153+
* @since 1.0.0
154+
*/
155+
public function set_option( string $value ): void {
156+
if ( ! self::option_exists() ):
157+
WPSSPluginHelper::add_option( self::$admin_menu_perms_option, $value );
158+
else:
159+
self::update_option( $value );
160+
endif;
161+
}
162+
163+
/**
164+
* Update access options
165+
*
166+
* @param string $update
167+
*
168+
* @since 1.0.0
169+
*/
170+
public function update_option( string $update ): void {
171+
$update_data = self::get_option();
172+
$get_data = json_decode( $update, true );
173+
foreach ( $get_data as $key => $val ):
174+
$update_data[ $key ] = $val;
175+
if ( empty( $update_data[ $key ] ) ):
176+
unset( $update_data[ $key ] );
177+
endif;
178+
endforeach;
179+
WPSSPluginHelper::update_option( self::$admin_menu_perms_option, wp_json_encode( $update_data ) );
180+
}
181+
182+
/**
183+
* Check if option exists
184+
* @return bool
185+
* @since 1.0.0
186+
*/
187+
public function option_exists(): bool {
188+
if ( ! WPSSPluginHelper::get_option( self::$admin_menu_perms_option ) ):
189+
return false;
190+
endif;
191+
192+
return true;
193+
}
194+
195+
/**
196+
* Get admin menu data
197+
* @return array
198+
* @since 1.0.0
199+
*/
200+
public function get_admin_menu(): array {
201+
global $menu;
202+
if ( empty( self::$get_menus ) ):
203+
self::$get_menus = $menu;
204+
endif;
205+
206+
return self::$get_menus;
207+
}
208+
}

0 commit comments

Comments
 (0)