-
Notifications
You must be signed in to change notification settings - Fork 9
/
vip-compat.php
152 lines (134 loc) · 4.08 KB
/
vip-compat.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
<?php
/**
* Handles the VIP functionality.
*
* @package Eight_Day_Week
*/
namespace Eight_Day_Week;
/**
* Retrieves the URL to the plugins directory or a specific plugin file.
*
* This function checks if the function wpcom_vip_plugins_url() exists and if it does, it calls that function to get the URL. Otherwise, it falls back to the \plugins_url() function.
*
* @param string $file The file path within the plugins directory. Empty string by default.
* @return string The URL to the plugins directory or a specific plugin file.
*/
function plugins_url( $file ) {
if ( function_exists( 'wpcom_vip_plugins_url' ) ) {
return wpcom_vip_plugins_url( '', '', $file );
}
return \plugins_url( '/', $file );
}
/**
* Duplicate a role and its capabilities.
*
* @param mixed $from_role The role to duplicate from.
* @param mixed $to_role The role to duplicate to.
* @param string $to_role_name The name of the duplicated role.
* @param array $new_caps The capabilities to add to the duplicated role.
*/
function duplicate_role( $from_role, $to_role, $to_role_name, $new_caps ) {
if ( function_exists( 'wpcom_vip_duplicate_role' ) ) {
wpcom_vip_duplicate_role( $from_role, $to_role, $to_role_name, $new_caps );
} else {
$caps = array_merge( get_role_caps( $from_role ), $new_caps );
add_role( $to_role, $to_role_name, $caps );
}
}
/**
* Get a list of capabilities for a role.
*
* @param string $role Role name.
*
* @return array Array of caps for the role
*/
function get_role_caps( $role ) {
if ( function_exists( 'wpcom_vip_get_role_caps' ) ) {
$caps = wpcom_vip_get_role_caps( $role );
} else {
$caps = array();
$role_obj = get_role( $role );
if ( $role_obj && isset( $role_obj->capabilities ) ) {
$caps = $role_obj->capabilities;
}
}
return $caps;
}
/**
* Add a new role
*
* Usage: add_role( 'super-editor', 'Super Editor', array( 'level_0' => true ) );
*
* @param string $role Role name.
* @param string $name Display name for the role.
* @param array $capabilities Key/value array of capabilities for the role.
*/
function add_role( $role, $name, $capabilities ) {
if ( function_exists( 'wpcom_vip_add_role' ) ) {
wpcom_vip_add_role( $role, $name, $capabilities );
} else {
global $wp_user_roles;
$role_obj = get_role( $role );
if ( ! $role_obj ) {
\add_role( $role, $name, $capabilities );
if ( ! isset( $wp_user_roles[ $role ] ) ) {
$wp_user_roles[ $role ] = array( // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
'name' => $name,
'capabilities' => $capabilities,
);
}
} else {
merge_role_caps( $role, $capabilities );
}
}
}
/**
* Add capabilities to an existing role
*
* Usage: add_role_caps( 'contributor', array( 'upload_files' ) );
*
* @param string $role Role name.
* @param array $caps Capabilities to add to the role.
*/
function add_role_caps( $role, $caps ) {
if ( function_exists( 'wpcom_vip_add_role_caps' ) ) {
wpcom_vip_add_role_caps( $role, $caps );
} else {
$filtered_caps = array();
foreach ( (array) $caps as $cap ) {
$filtered_caps[ $cap ] = true;
}
merge_role_caps( $role, $filtered_caps );
}
}
/**
* Add new or change existing capabilities for a given role
*
* Usage: merge_role_caps( 'author', array( 'publish_posts' => false ) );
*
* @param string $role Role name.
* @param array $caps Key/value array of capabilities for this role.
*/
function merge_role_caps( $role, $caps ) {
if ( function_exists( 'wpcom_vip_merge_role_caps' ) ) {
wpcom_vip_merge_role_caps( $role, $caps );
} else {
global $wp_user_roles;
$role_obj = get_role( $role );
if ( ! $role_obj ) {
return;
}
$current_caps = (array) get_role_caps( $role );
$new_caps = array_merge( $current_caps, (array) $caps );
foreach ( $new_caps as $cap => $role_can ) {
if ( $role_can ) {
$role_obj->add_cap( $cap );
} else {
$role_obj->remove_cap( $cap );
}
}
if ( isset( $wp_user_roles[ $role ] ) ) {
$wp_user_roles[ $role ]['capabilities'] = array_merge( $current_caps, (array) $caps ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
}