/
capabilities.php
78 lines (62 loc) · 2 KB
/
capabilities.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
<?php
/**
* Register default roles and capabilities
*/
class Fork_Capabilities {
public $defaults = array(
'administrator' => array(
'edit_forks' => true,
'edit_others_forks' => true,
'edit_private_forks' => true,
'edit_published_forks' => false,
'read_forks' => true,
'read_private_forks' => true,
'delete_fork' => true,
'delete_others_forks' => true,
'delete_private_forks' => true,
'delete_published_forks' => true,
'publish_forks' => true,
'fork_posts' => true,
'branch_posts' => true,
),
'subscriber' => array(
'edit_forks' => true,
'edit_others_forks' => false,
'edit_private_forks' => false,
'edit_published_forks' => false,
'read_forks' => true,
'read_private_forks' => false,
'delete_fork' => true,
'delete_others_forks' => false,
'delete_private_forks' => false,
'delete_published_forks' => false,
'publish_forks' => false,
'fork_posts' => true,
'branch_posts' => false,
),
);
/**
* Register with WordPress API
*/
function __construct() {
add_action( 'init', array( $this, 'add_caps' ) );
}
/**
* Adds plugin-specific caps to all roles so that 3rd party plugins can manage them
*/
function add_caps() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles;
foreach ( $wp_roles->role_names as $role=>$label ) {
//if the role is a standard role, map the default caps, otherwise, map as a subscriber
$caps = ( array_key_exists( $role, $this->defaults ) ) ? $this->defaults[$role] : $this->defaults['subscriber'];
//loop and assign
foreach ( $caps as $cap=>$grant ) {
//check to see if the user already has this capability, if so, don't re-add as that would override grant
if ( !isset( $wp_roles->roles[$role]['capabilities'][$cap] ) )
$wp_roles->add_cap( $role, $cap, $grant );
}
}
}
}