forked from justintadlock/members
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions-cap-groups.php
311 lines (255 loc) · 7.25 KB
/
functions-cap-groups.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* Capability groups API. Offers a standardized method for creating capability groups.
*
* @package Members
* @subpackage Admin
* @author Justin Tadlock <[email protected]>
* @copyright Copyright (c) 2009 - 2018, Justin Tadlock
* @link https://themehybrid.com/plugins/members
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
# Registers default groups.
add_action( 'init', 'members_register_cap_groups', 95 );
add_action( 'members_register_cap_groups', 'members_register_default_cap_groups', 5 );
/**
* Fires the cap group registration action hook.
*
* @since 1.0.0
* @access public
* @return void
*/
function members_register_cap_groups() {
// Hook for registering cap groups. Plugins should always register on this hook.
do_action( 'members_register_cap_groups' );
}
/**
* Registers the default cap groups.
*
* @since 2.0.0
* @access public
* @return void
*/
function members_register_default_cap_groups() {
// Registers the general group.
members_register_cap_group( 'general',
array(
'label' => esc_html__( 'General', 'members' ),
'icon' => 'dashicons-wordpress',
'priority' => 5
)
);
// Loop through every custom post type.
foreach ( get_post_types( array(), 'objects' ) as $type ) {
// Skip revisions and nave menu items.
if ( in_array( $type->name, array( 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset' ) ) )
continue;
// Get the caps for the post type.
$has_caps = members_get_post_type_group_caps( $type->name );
// Skip if the post type doesn't have caps.
if ( empty( $has_caps ) )
continue;
// Set the default post type icon.
$icon = $type->hierarchical ? 'dashicons-admin-page' : 'dashicons-admin-post';
// Get the post type icon.
if ( is_string( $type->menu_icon ) && preg_match( '/dashicons-/i', $type->menu_icon ) )
$icon = $type->menu_icon;
else if ( 'attachment' === $type->name )
$icon = 'dashicons-admin-media';
else if ( 'download' === $type->name )
$icon = 'dashicons-download'; // EDD
else if ( 'product' === $type->name )
$icon = 'dashicons-cart';
// Register the post type cap group.
members_register_cap_group( "type-{$type->name}",
array(
'label' => $type->labels->name,
'caps' => $has_caps,
'icon' => $icon,
'priority' => 10
)
);
}
// Register the taxonomy group.
members_register_cap_group( 'taxonomy',
array(
'label' => esc_html__( 'Taxonomies', 'members' ),
'caps' => members_get_taxonomy_group_caps(),
'icon' => 'dashicons-tag',
'diff_added' => true,
'priority' => 15
)
);
// Register the theme group.
members_register_cap_group( 'theme',
array(
'label' => esc_html__( 'Appearance', 'members' ),
'icon' => 'dashicons-admin-appearance',
'priority' => 20
)
);
// Register the plugin group.
members_register_cap_group( 'plugin',
array(
'label' => esc_html__( 'Plugins', 'members' ),
'icon' => 'dashicons-admin-plugins',
'priority' => 25
)
);
// Register the user group.
members_register_cap_group( 'user',
array(
'label' => esc_html__( 'Users', 'members' ),
'icon' => 'dashicons-admin-users',
'priority' => 30
)
);
// Register the custom group.
members_register_cap_group( 'custom',
array(
'label' => esc_html__( 'Custom', 'members' ),
'caps' => members_get_capabilities(),
'icon' => 'dashicons-admin-generic',
'diff_added' => true,
'priority' => 995
)
);
}
/**
* Returns the instance of cap group registry.
*
* @since 2.0.0
* @access public
* @return object
*/
function members_cap_group_registry() {
return \Members\Registry::get_instance( 'cap_group' );
}
/**
* Function for registering a cap group.
*
* @since 1.0.0
* @access public
* @param string $name
* @param array $args
* @return void
*/
function members_register_cap_group( $name, $args = array() ) {
members_cap_group_registry()->register( $name, new \Members\Cap_Group( $name, $args ) );
}
/**
* Unregisters a group.
*
* @since 1.0.0
* @access public
* @param string $name
* @return void
*/
function members_unregister_cap_group( $name ) {
members_cap_group_registry()->unregister( $name );
}
/**
* Checks if a group exists.
*
* @since 1.0.0
* @access public
* @param string $name
* @return bool
*/
function members_cap_group_exists( $name ) {
return members_cap_group_registry()->exists( $name );
}
/**
* Returns an array of registered group objects.
*
* @since 1.0.0
* @access public
* @return array
*/
function members_get_cap_groups() {
return members_cap_group_registry()->get_collection();
}
/**
* Returns a group object if it exists. Otherwise, `FALSE`.
*
* @since 1.0.0
* @access public
* @param string $name
* @return object|bool
*/
function members_get_cap_group( $name ) {
return members_cap_group_registry()->get( $name );
}
/**
* Returns the caps for a specific post type capability group.
*
* @since 1.0.0
* @access public
* @return array
*/
function members_get_post_type_group_caps( $post_type = 'post' ) {
// Get the post type caps.
$caps = (array) get_post_type_object( $post_type )->cap;
// remove meta caps.
unset( $caps['edit_post'] );
unset( $caps['read_post'] );
unset( $caps['delete_post'] );
// Get the cap names only.
$caps = array_values( $caps );
// If this is not a core post/page post type.
if ( ! in_array( $post_type, array( 'post', 'page' ) ) ) {
// Get the post and page caps.
$post_caps = array_values( (array) get_post_type_object( 'post' )->cap );
$page_caps = array_values( (array) get_post_type_object( 'page' )->cap );
// Remove post/page caps from the current post type caps.
$caps = array_diff( $caps, $post_caps, $page_caps );
}
// If attachment post type, add the `unfiltered_upload` cap.
if ( 'attachment' === $post_type )
$caps[] = 'unfiltered_upload';
$registered_caps = array_keys( wp_list_filter( members_get_caps(), array( 'group' => "type-{$post_type}" ) ) );
if ( $registered_caps )
array_merge( $caps, $registered_caps );
// Make sure there are no duplicates and return.
return array_unique( $caps );
}
/**
* Returns the caps for the taxonomy capability group.
*
* @since 1.0.0
* @access public
* @return array
*/
function members_get_taxonomy_group_caps() {
$do_not_add = array(
'assign_categories',
'edit_categories',
'delete_categories',
'assign_post_tags',
'edit_post_tags',
'delete_post_tags',
'manage_post_tags'
);
$taxi = get_taxonomies( array(), 'objects' );
$caps = array();
foreach ( $taxi as $tax )
$caps = array_merge( $caps, array_values( (array) $tax->cap ) );
$registered_caps = array_keys( wp_list_filter( members_get_caps(), array( 'group' => 'taxonomy' ) ) );
if ( $registered_caps )
array_merge( $caps, $registered_caps );
return array_diff( array_unique( $caps ), $do_not_add );
}
/**
* Returns the caps for the custom capability group.
*
* @since 1.0.0
* @access public
* @return array
*/
function members_get_custom_group_caps() {
$caps = members_get_capabilities();
$registered_caps = array_keys( wp_list_filter( members_get_caps(), array( 'group' => 'custom' ) ) );
if ( $registered_caps )
array_merge( $caps, $registered_caps );
return array_unique( $caps );
}