author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core User Role & Capabilities API |
0 | 4 |
* |
5 |
* @package WordPress |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* @subpackage Users |
0 | 7 |
*/ |
8 |
||
9 |
/** |
|
18 | 10 |
* Maps a capability to the primitive capabilities required of the given user to |
11 |
* satisfy the capability being checked. |
|
16 | 12 |
* |
13 |
* This function also accepts an ID of an object to map against if the capability is a meta capability. Meta |
|
14 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by this function to map to primitive |
|
18 | 15 |
* capabilities that a user or role requires, such as `edit_posts` and `edit_others_posts`. |
16 | 16 |
* |
17 |
* Example usage: |
|
18 |
* |
|
19 |
* map_meta_cap( 'edit_posts', $user->ID ); |
|
20 |
* map_meta_cap( 'edit_post', $user->ID, $post->ID ); |
|
21 |
* map_meta_cap( 'edit_post_meta', $user->ID, $post->ID, $meta_key ); |
|
0 | 22 |
* |
18 | 23 |
* This function does not check whether the user has the required capabilities, |
24 |
* it just returns what the required capabilities are. |
|
0 | 25 |
* |
26 |
* @since 2.0.0 |
|
18 | 27 |
* @since 4.9.6 Added the `export_others_personal_data`, `erase_others_personal_data`, |
28 |
* and `manage_privacy_options` capabilities. |
|
29 |
* @since 5.1.0 Added the `update_php` capability. |
|
30 |
* @since 5.2.0 Added the `resume_plugin` and `resume_theme` capabilities. |
|
16 | 31 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
32 |
* by adding it to the function signature. |
|
18 | 33 |
* @since 5.7.0 Added the `create_app_password`, `list_app_passwords`, `read_app_password`, |
34 |
* `edit_app_password`, `delete_app_passwords`, `delete_app_password`, |
|
35 |
* and `update_https` capabilities. |
|
0 | 36 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
* @global array $post_type_meta_caps Used to get post type meta capabilities. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
* |
18 | 39 |
* @param string $cap Capability being checked. |
16 | 40 |
* @param int $user_id User ID. |
41 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
|
18 | 42 |
* @return string[] Primitive capabilities required of the user. |
0 | 43 |
*/ |
16 | 44 |
function map_meta_cap( $cap, $user_id, ...$args ) { |
0 | 45 |
$caps = array(); |
46 |
||
47 |
switch ( $cap ) { |
|
9 | 48 |
case 'remove_user': |
49 |
// In multisite the user must be a super admin to remove themselves. |
|
50 |
if ( isset( $args[0] ) && $user_id == $args[0] && ! is_super_admin( $user_id ) ) { |
|
51 |
$caps[] = 'do_not_allow'; |
|
52 |
} else { |
|
53 |
$caps[] = 'remove_users'; |
|
54 |
} |
|
55 |
break; |
|
56 |
case 'promote_user': |
|
57 |
case 'add_users': |
|
58 |
$caps[] = 'promote_users'; |
|
0 | 59 |
break; |
9 | 60 |
case 'edit_user': |
61 |
case 'edit_users': |
|
16 | 62 |
// Allow user to edit themselves. |
63 |
if ( 'edit_user' === $cap && isset( $args[0] ) && $user_id == $args[0] ) { |
|
9 | 64 |
break; |
65 |
} |
|
66 |
||
67 |
// In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin. |
|
68 |
if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) { |
|
69 |
$caps[] = 'do_not_allow'; |
|
70 |
} else { |
|
71 |
$caps[] = 'edit_users'; // edit_user maps to edit_users. |
|
72 |
} |
|
73 |
break; |
|
74 |
case 'delete_post': |
|
75 |
case 'delete_page': |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
76 |
if ( ! isset( $args[0] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
77 |
if ( 'delete_post' === $cap ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
78 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
79 |
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
80 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
81 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
82 |
$message = __( 'When checking for the %s capability, you must always check it against a specific page.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
84 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
86 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
87 |
sprintf( $message, '<code>' . $cap . '</code>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
88 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
89 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
90 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
$caps[] = 'do_not_allow'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
92 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
93 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
|
9 | 95 |
$post = get_post( $args[0] ); |
96 |
if ( ! $post ) { |
|
97 |
$caps[] = 'do_not_allow'; |
|
98 |
break; |
|
99 |
} |
|
100 |
||
16 | 101 |
if ( 'revision' === $post->post_type ) { |
102 |
$caps[] = 'do_not_allow'; |
|
103 |
break; |
|
9 | 104 |
} |
105 |
||
106 |
if ( ( get_option( 'page_for_posts' ) == $post->ID ) || ( get_option( 'page_on_front' ) == $post->ID ) ) { |
|
107 |
$caps[] = 'manage_options'; |
|
108 |
break; |
|
109 |
} |
|
0 | 110 |
|
9 | 111 |
$post_type = get_post_type_object( $post->post_type ); |
112 |
if ( ! $post_type ) { |
|
16 | 113 |
/* translators: 1: Post type, 2: Capability name. */ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
119 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
'<code>' . $post->post_type . '</code>', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
121 |
'<code>' . $cap . '</code>' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
122 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
123 |
'4.4.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
124 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
125 |
|
9 | 126 |
$caps[] = 'edit_others_posts'; |
127 |
break; |
|
128 |
} |
|
129 |
||
130 |
if ( ! $post_type->map_meta_cap ) { |
|
131 |
$caps[] = $post_type->cap->$cap; |
|
132 |
// Prior to 3.1 we would re-call map_meta_cap here. |
|
16 | 133 |
if ( 'delete_post' === $cap ) { |
9 | 134 |
$cap = $post_type->cap->$cap; |
135 |
} |
|
136 |
break; |
|
137 |
} |
|
138 |
||
139 |
// If the post author is set and the user is the author... |
|
140 |
if ( $post->post_author && $user_id == $post->post_author ) { |
|
141 |
// If the post is published or scheduled... |
|
142 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
143 |
$caps[] = $post_type->cap->delete_published_posts; |
|
16 | 144 |
} elseif ( 'trash' === $post->post_status ) { |
9 | 145 |
$status = get_post_meta( $post->ID, '_wp_trash_meta_status', true ); |
146 |
if ( in_array( $status, array( 'publish', 'future' ), true ) ) { |
|
147 |
$caps[] = $post_type->cap->delete_published_posts; |
|
148 |
} else { |
|
149 |
$caps[] = $post_type->cap->delete_posts; |
|
150 |
} |
|
151 |
} else { |
|
152 |
// If the post is draft... |
|
153 |
$caps[] = $post_type->cap->delete_posts; |
|
154 |
} |
|
155 |
} else { |
|
156 |
// The user is trying to edit someone else's post. |
|
157 |
$caps[] = $post_type->cap->delete_others_posts; |
|
158 |
// The post is published or scheduled, extra cap required. |
|
159 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
160 |
$caps[] = $post_type->cap->delete_published_posts; |
|
16 | 161 |
} elseif ( 'private' === $post->post_status ) { |
9 | 162 |
$caps[] = $post_type->cap->delete_private_posts; |
163 |
} |
|
164 |
} |
|
165 |
||
166 |
/* |
|
167 |
* Setting the privacy policy page requires `manage_privacy_options`, |
|
168 |
* so deleting it should require that too. |
|
169 |
*/ |
|
170 |
if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) { |
|
171 |
$caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) ); |
|
172 |
} |
|
173 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
break; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
175 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
176 |
* edit_post breaks down to edit_posts, edit_published_posts, or |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
177 |
* edit_others_posts. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
178 |
*/ |
9 | 179 |
case 'edit_post': |
180 |
case 'edit_page': |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
181 |
if ( ! isset( $args[0] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
182 |
if ( 'edit_post' === $cap ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
183 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
184 |
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
185 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
186 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
187 |
$message = __( 'When checking for the %s capability, you must always check it against a specific page.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
188 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
189 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
190 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
191 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
192 |
sprintf( $message, '<code>' . $cap . '</code>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
193 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
194 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
195 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
196 |
$caps[] = 'do_not_allow'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
197 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
198 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
199 |
|
9 | 200 |
$post = get_post( $args[0] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
$caps[] = 'do_not_allow'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
} |
0 | 205 |
|
16 | 206 |
if ( 'revision' === $post->post_type ) { |
9 | 207 |
$post = get_post( $post->post_parent ); |
208 |
if ( ! $post ) { |
|
209 |
$caps[] = 'do_not_allow'; |
|
210 |
break; |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
$post_type = get_post_type_object( $post->post_type ); |
|
215 |
if ( ! $post_type ) { |
|
16 | 216 |
/* translators: 1: Post type, 2: Capability name. */ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
217 |
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
218 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
219 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
220 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
221 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
222 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
223 |
'<code>' . $post->post_type . '</code>', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
224 |
'<code>' . $cap . '</code>' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
225 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
226 |
'4.4.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
227 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
228 |
|
9 | 229 |
$caps[] = 'edit_others_posts'; |
230 |
break; |
|
231 |
} |
|
0 | 232 |
|
9 | 233 |
if ( ! $post_type->map_meta_cap ) { |
234 |
$caps[] = $post_type->cap->$cap; |
|
235 |
// Prior to 3.1 we would re-call map_meta_cap here. |
|
16 | 236 |
if ( 'edit_post' === $cap ) { |
9 | 237 |
$cap = $post_type->cap->$cap; |
238 |
} |
|
239 |
break; |
|
240 |
} |
|
0 | 241 |
|
9 | 242 |
// If the post author is set and the user is the author... |
243 |
if ( $post->post_author && $user_id == $post->post_author ) { |
|
244 |
// If the post is published or scheduled... |
|
245 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
246 |
$caps[] = $post_type->cap->edit_published_posts; |
|
16 | 247 |
} elseif ( 'trash' === $post->post_status ) { |
9 | 248 |
$status = get_post_meta( $post->ID, '_wp_trash_meta_status', true ); |
249 |
if ( in_array( $status, array( 'publish', 'future' ), true ) ) { |
|
250 |
$caps[] = $post_type->cap->edit_published_posts; |
|
251 |
} else { |
|
252 |
$caps[] = $post_type->cap->edit_posts; |
|
253 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
} else { |
9 | 255 |
// If the post is draft... |
256 |
$caps[] = $post_type->cap->edit_posts; |
|
5 | 257 |
} |
0 | 258 |
} else { |
9 | 259 |
// The user is trying to edit someone else's post. |
260 |
$caps[] = $post_type->cap->edit_others_posts; |
|
261 |
// The post is published or scheduled, extra cap required. |
|
262 |
if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
|
263 |
$caps[] = $post_type->cap->edit_published_posts; |
|
16 | 264 |
} elseif ( 'private' === $post->post_status ) { |
9 | 265 |
$caps[] = $post_type->cap->edit_private_posts; |
266 |
} |
|
0 | 267 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
|
9 | 269 |
/* |
270 |
* Setting the privacy policy page requires `manage_privacy_options`, |
|
271 |
* so editing it should require that too. |
|
272 |
*/ |
|
273 |
if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) { |
|
274 |
$caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) ); |
|
275 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
|
0 | 277 |
break; |
9 | 278 |
case 'read_post': |
279 |
case 'read_page': |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
280 |
if ( ! isset( $args[0] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
281 |
if ( 'read_post' === $cap ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
282 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
283 |
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
284 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
285 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
286 |
$message = __( 'When checking for the %s capability, you must always check it against a specific page.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
287 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
288 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
289 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
290 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
291 |
sprintf( $message, '<code>' . $cap . '</code>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
292 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
293 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
294 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
295 |
$caps[] = 'do_not_allow'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
296 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
297 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
298 |
|
9 | 299 |
$post = get_post( $args[0] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
$caps[] = 'do_not_allow'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
} |
0 | 304 |
|
16 | 305 |
if ( 'revision' === $post->post_type ) { |
9 | 306 |
$post = get_post( $post->post_parent ); |
307 |
if ( ! $post ) { |
|
308 |
$caps[] = 'do_not_allow'; |
|
309 |
break; |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
$post_type = get_post_type_object( $post->post_type ); |
|
314 |
if ( ! $post_type ) { |
|
16 | 315 |
/* translators: 1: Post type, 2: Capability name. */ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
316 |
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
317 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
318 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
319 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
320 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
321 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
322 |
'<code>' . $post->post_type . '</code>', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
323 |
'<code>' . $cap . '</code>' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
324 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
325 |
'4.4.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
326 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
327 |
|
9 | 328 |
$caps[] = 'edit_others_posts'; |
329 |
break; |
|
330 |
} |
|
0 | 331 |
|
9 | 332 |
if ( ! $post_type->map_meta_cap ) { |
333 |
$caps[] = $post_type->cap->$cap; |
|
334 |
// Prior to 3.1 we would re-call map_meta_cap here. |
|
16 | 335 |
if ( 'read_post' === $cap ) { |
9 | 336 |
$cap = $post_type->cap->$cap; |
337 |
} |
|
338 |
break; |
|
339 |
} |
|
0 | 340 |
|
18 | 341 |
$status_obj = get_post_status_object( get_post_status( $post ) ); |
16 | 342 |
if ( ! $status_obj ) { |
343 |
/* translators: 1: Post status, 2: Capability name. */ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
344 |
$message = __( 'The post status %1$s is not registered, so it may not be reliable to check the capability %2$s against a post with that status.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
345 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
346 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
347 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
348 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
349 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
350 |
'<code>' . get_post_status( $post ) . '</code>', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
351 |
'<code>' . $cap . '</code>' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
352 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
353 |
'5.4.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
354 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
355 |
|
16 | 356 |
$caps[] = 'edit_others_posts'; |
357 |
break; |
|
358 |
} |
|
359 |
||
9 | 360 |
if ( $status_obj->public ) { |
361 |
$caps[] = $post_type->cap->read; |
|
362 |
break; |
|
363 |
} |
|
364 |
||
365 |
if ( $post->post_author && $user_id == $post->post_author ) { |
|
366 |
$caps[] = $post_type->cap->read; |
|
367 |
} elseif ( $status_obj->private ) { |
|
368 |
$caps[] = $post_type->cap->read_private_posts; |
|
0 | 369 |
} else { |
9 | 370 |
$caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
5 | 371 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
break; |
9 | 373 |
case 'publish_post': |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
374 |
if ( ! isset( $args[0] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
375 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
376 |
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
377 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
378 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
379 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
380 |
sprintf( $message, '<code>' . $cap . '</code>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
381 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
382 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
383 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
384 |
$caps[] = 'do_not_allow'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
385 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
386 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
387 |
|
9 | 388 |
$post = get_post( $args[0] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
$caps[] = 'do_not_allow'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
} |
0 | 393 |
|
9 | 394 |
$post_type = get_post_type_object( $post->post_type ); |
395 |
if ( ! $post_type ) { |
|
16 | 396 |
/* translators: 1: Post type, 2: Capability name. */ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
397 |
$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
398 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
399 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
400 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
401 |
sprintf( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
402 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
403 |
'<code>' . $post->post_type . '</code>', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
404 |
'<code>' . $cap . '</code>' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
405 |
), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
406 |
'4.4.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
407 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
408 |
|
9 | 409 |
$caps[] = 'edit_others_posts'; |
410 |
break; |
|
411 |
} |
|
0 | 412 |
|
9 | 413 |
$caps[] = $post_type->cap->publish_posts; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
break; |
9 | 415 |
case 'edit_post_meta': |
416 |
case 'delete_post_meta': |
|
417 |
case 'add_post_meta': |
|
418 |
case 'edit_comment_meta': |
|
419 |
case 'delete_comment_meta': |
|
420 |
case 'add_comment_meta': |
|
421 |
case 'edit_term_meta': |
|
422 |
case 'delete_term_meta': |
|
423 |
case 'add_term_meta': |
|
424 |
case 'edit_user_meta': |
|
425 |
case 'delete_user_meta': |
|
426 |
case 'add_user_meta': |
|
16 | 427 |
$object_type = explode( '_', $cap )[1]; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
428 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
429 |
if ( ! isset( $args[0] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
430 |
if ( 'post' === $object_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
431 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
432 |
$message = __( 'When checking for the %s capability, you must always check it against a specific post.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
433 |
} elseif ( 'comment' === $object_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
434 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
435 |
$message = __( 'When checking for the %s capability, you must always check it against a specific comment.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
436 |
} elseif ( 'term' === $object_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
437 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
438 |
$message = __( 'When checking for the %s capability, you must always check it against a specific term.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
439 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
440 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
441 |
$message = __( 'When checking for the %s capability, you must always check it against a specific user.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
442 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
443 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
444 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
445 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
446 |
sprintf( $message, '<code>' . $cap . '</code>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
447 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
448 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
449 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
450 |
$caps[] = 'do_not_allow'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
451 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
452 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
453 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
454 |
$object_id = (int) $args[0]; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
|
9 | 456 |
$object_subtype = get_object_subtype( $object_type, $object_id ); |
0 | 457 |
|
9 | 458 |
if ( empty( $object_subtype ) ) { |
459 |
$caps[] = 'do_not_allow'; |
|
460 |
break; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
|
9 | 463 |
$caps = map_meta_cap( "edit_{$object_type}", $user_id, $object_id ); |
464 |
||
465 |
$meta_key = isset( $args[1] ) ? $args[1] : false; |
|
466 |
||
467 |
if ( $meta_key ) { |
|
468 |
$allowed = ! is_protected_meta( $meta_key, $object_type ); |
|
469 |
||
470 |
if ( ! empty( $object_subtype ) && has_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { |
|
471 |
||
472 |
/** |
|
473 |
* Filters whether the user is allowed to edit a specific meta key of a specific object type and subtype. |
|
474 |
* |
|
475 |
* The dynamic portions of the hook name, `$object_type`, `$meta_key`, |
|
476 |
* and `$object_subtype`, refer to the metadata object type (comment, post, term or user), |
|
477 |
* the meta key value, and the object subtype respectively. |
|
478 |
* |
|
479 |
* @since 4.9.8 |
|
480 |
* |
|
481 |
* @param bool $allowed Whether the user can add the object meta. Default false. |
|
482 |
* @param string $meta_key The meta key. |
|
483 |
* @param int $object_id Object ID. |
|
484 |
* @param int $user_id User ID. |
|
485 |
* @param string $cap Capability name. |
|
486 |
* @param string[] $caps Array of the user's capabilities. |
|
487 |
*/ |
|
488 |
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps ); |
|
489 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
|
9 | 491 |
/** |
492 |
* Filters whether the user is allowed to edit a specific meta key of a specific object type. |
|
493 |
* |
|
494 |
* Return true to have the mapped meta caps from `edit_{$object_type}` apply. |
|
495 |
* |
|
496 |
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered. |
|
497 |
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap(). |
|
498 |
* |
|
499 |
* @since 3.3.0 As `auth_post_meta_{$meta_key}`. |
|
500 |
* @since 4.6.0 |
|
501 |
* |
|
502 |
* @param bool $allowed Whether the user can add the object meta. Default false. |
|
503 |
* @param string $meta_key The meta key. |
|
504 |
* @param int $object_id Object ID. |
|
505 |
* @param int $user_id User ID. |
|
506 |
* @param string $cap Capability name. |
|
507 |
* @param string[] $caps Array of the user's capabilities. |
|
508 |
*/ |
|
509 |
$allowed = apply_filters( "auth_{$object_type}_meta_{$meta_key}", $allowed, $meta_key, $object_id, $user_id, $cap, $caps ); |
|
510 |
} |
|
511 |
||
512 |
if ( ! empty( $object_subtype ) ) { |
|
513 |
||
514 |
/** |
|
515 |
* Filters whether the user is allowed to edit meta for specific object types/subtypes. |
|
516 |
* |
|
517 |
* Return true to have the mapped meta caps from `edit_{$object_type}` apply. |
|
518 |
* |
|
519 |
* The dynamic portion of the hook name, `$object_type` refers to the object type being filtered. |
|
520 |
* The dynamic portion of the hook name, `$object_subtype` refers to the object subtype being filtered. |
|
521 |
* The dynamic portion of the hook name, `$meta_key`, refers to the meta key passed to map_meta_cap(). |
|
522 |
* |
|
523 |
* @since 4.6.0 As `auth_post_{$post_type}_meta_{$meta_key}`. |
|
16 | 524 |
* @since 4.7.0 Renamed from `auth_post_{$post_type}_meta_{$meta_key}` to |
525 |
* `auth_{$object_type}_{$object_subtype}_meta_{$meta_key}`. |
|
526 |
* @deprecated 4.9.8 Use {@see 'auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}'} instead. |
|
9 | 527 |
* |
528 |
* @param bool $allowed Whether the user can add the object meta. Default false. |
|
529 |
* @param string $meta_key The meta key. |
|
530 |
* @param int $object_id Object ID. |
|
531 |
* @param int $user_id User ID. |
|
532 |
* @param string $cap Capability name. |
|
533 |
* @param string[] $caps Array of the user's capabilities. |
|
534 |
*/ |
|
16 | 535 |
$allowed = apply_filters_deprecated( |
536 |
"auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", |
|
537 |
array( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ), |
|
538 |
'4.9.8', |
|
539 |
"auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" |
|
540 |
); |
|
9 | 541 |
} |
542 |
||
543 |
if ( ! $allowed ) { |
|
544 |
$caps[] = $cap; |
|
545 |
} |
|
546 |
} |
|
547 |
break; |
|
548 |
case 'edit_comment': |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
549 |
if ( ! isset( $args[0] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
550 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
551 |
$message = __( 'When checking for the %s capability, you must always check it against a specific comment.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
552 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
553 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
554 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
555 |
sprintf( $message, '<code>' . $cap . '</code>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
556 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
557 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
558 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
559 |
$caps[] = 'do_not_allow'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
560 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
561 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
562 |
|
9 | 563 |
$comment = get_comment( $args[0] ); |
564 |
if ( ! $comment ) { |
|
565 |
$caps[] = 'do_not_allow'; |
|
566 |
break; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
567 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
|
9 | 569 |
$post = get_post( $comment->comment_post_ID ); |
570 |
||
571 |
/* |
|
572 |
* If the post doesn't exist, we have an orphaned comment. |
|
573 |
* Fall back to the edit_posts capability, instead. |
|
574 |
*/ |
|
575 |
if ( $post ) { |
|
576 |
$caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
|
577 |
} else { |
|
578 |
$caps = map_meta_cap( 'edit_posts', $user_id ); |
|
579 |
} |
|
580 |
break; |
|
581 |
case 'unfiltered_upload': |
|
582 |
if ( defined( 'ALLOW_UNFILTERED_UPLOADS' ) && ALLOW_UNFILTERED_UPLOADS && ( ! is_multisite() || is_super_admin( $user_id ) ) ) { |
|
583 |
$caps[] = $cap; |
|
584 |
} else { |
|
585 |
$caps[] = 'do_not_allow'; |
|
586 |
} |
|
587 |
break; |
|
588 |
case 'edit_css': |
|
589 |
case 'unfiltered_html': |
|
590 |
// Disallow unfiltered_html for all users, even admins and super admins. |
|
591 |
if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) { |
|
592 |
$caps[] = 'do_not_allow'; |
|
593 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
594 |
$caps[] = 'do_not_allow'; |
|
595 |
} else { |
|
596 |
$caps[] = 'unfiltered_html'; |
|
597 |
} |
|
598 |
break; |
|
599 |
case 'edit_files': |
|
600 |
case 'edit_plugins': |
|
601 |
case 'edit_themes': |
|
602 |
// Disallow the file editors. |
|
603 |
if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) { |
|
604 |
$caps[] = 'do_not_allow'; |
|
605 |
} elseif ( ! wp_is_file_mod_allowed( 'capability_edit_themes' ) ) { |
|
606 |
$caps[] = 'do_not_allow'; |
|
607 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
608 |
$caps[] = 'do_not_allow'; |
|
609 |
} else { |
|
0 | 610 |
$caps[] = $cap; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
} |
9 | 612 |
break; |
613 |
case 'update_plugins': |
|
614 |
case 'delete_plugins': |
|
615 |
case 'install_plugins': |
|
616 |
case 'upload_plugins': |
|
617 |
case 'update_themes': |
|
618 |
case 'delete_themes': |
|
619 |
case 'install_themes': |
|
620 |
case 'upload_themes': |
|
621 |
case 'update_core': |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
622 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
623 |
* Disallow anything that creates, deletes, or updates core, plugin, or theme files. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
624 |
* Files in uploads are excepted. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
625 |
*/ |
9 | 626 |
if ( ! wp_is_file_mod_allowed( 'capability_update_core' ) ) { |
627 |
$caps[] = 'do_not_allow'; |
|
628 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
629 |
$caps[] = 'do_not_allow'; |
|
630 |
} elseif ( 'upload_themes' === $cap ) { |
|
631 |
$caps[] = 'install_themes'; |
|
632 |
} elseif ( 'upload_plugins' === $cap ) { |
|
633 |
$caps[] = 'install_plugins'; |
|
634 |
} else { |
|
635 |
$caps[] = $cap; |
|
636 |
} |
|
637 |
break; |
|
638 |
case 'install_languages': |
|
639 |
case 'update_languages': |
|
640 |
if ( ! wp_is_file_mod_allowed( 'can_install_language_pack' ) ) { |
|
641 |
$caps[] = 'do_not_allow'; |
|
642 |
} elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
643 |
$caps[] = 'do_not_allow'; |
|
644 |
} else { |
|
645 |
$caps[] = 'install_languages'; |
|
646 |
} |
|
647 |
break; |
|
648 |
case 'activate_plugins': |
|
649 |
case 'deactivate_plugins': |
|
650 |
case 'activate_plugin': |
|
651 |
case 'deactivate_plugin': |
|
652 |
$caps[] = 'activate_plugins'; |
|
653 |
if ( is_multisite() ) { |
|
654 |
// update_, install_, and delete_ are handled above with is_super_admin(). |
|
655 |
$menu_perms = get_site_option( 'menu_items', array() ); |
|
656 |
if ( empty( $menu_perms['plugins'] ) ) { |
|
657 |
$caps[] = 'manage_network_plugins'; |
|
658 |
} |
|
659 |
} |
|
0 | 660 |
break; |
9 | 661 |
case 'resume_plugin': |
662 |
$caps[] = 'resume_plugins'; |
|
663 |
break; |
|
664 |
case 'resume_theme': |
|
665 |
$caps[] = 'resume_themes'; |
|
666 |
break; |
|
667 |
case 'delete_user': |
|
668 |
case 'delete_users': |
|
669 |
// If multisite only super admins can delete users. |
|
670 |
if ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
671 |
$caps[] = 'do_not_allow'; |
|
672 |
} else { |
|
673 |
$caps[] = 'delete_users'; // delete_user maps to delete_users. |
|
674 |
} |
|
675 |
break; |
|
676 |
case 'create_users': |
|
677 |
if ( ! is_multisite() ) { |
|
678 |
$caps[] = $cap; |
|
679 |
} elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) ) { |
|
680 |
$caps[] = $cap; |
|
681 |
} else { |
|
682 |
$caps[] = 'do_not_allow'; |
|
683 |
} |
|
684 |
break; |
|
685 |
case 'manage_links': |
|
686 |
if ( get_option( 'link_manager_enabled' ) ) { |
|
687 |
$caps[] = $cap; |
|
688 |
} else { |
|
689 |
$caps[] = 'do_not_allow'; |
|
690 |
} |
|
691 |
break; |
|
692 |
case 'customize': |
|
693 |
$caps[] = 'edit_theme_options'; |
|
694 |
break; |
|
695 |
case 'delete_site': |
|
696 |
if ( is_multisite() ) { |
|
697 |
$caps[] = 'manage_options'; |
|
698 |
} else { |
|
699 |
$caps[] = 'do_not_allow'; |
|
700 |
} |
|
701 |
break; |
|
702 |
case 'edit_term': |
|
703 |
case 'delete_term': |
|
704 |
case 'assign_term': |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
705 |
if ( ! isset( $args[0] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
706 |
/* translators: %s: Capability name. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
707 |
$message = __( 'When checking for the %s capability, you must always check it against a specific term.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
708 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
709 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
710 |
__FUNCTION__, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
711 |
sprintf( $message, '<code>' . $cap . '</code>' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
712 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
713 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
714 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
715 |
$caps[] = 'do_not_allow'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
716 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
717 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
718 |
|
9 | 719 |
$term_id = (int) $args[0]; |
720 |
$term = get_term( $term_id ); |
|
721 |
if ( ! $term || is_wp_error( $term ) ) { |
|
722 |
$caps[] = 'do_not_allow'; |
|
723 |
break; |
|
724 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
|
9 | 726 |
$tax = get_taxonomy( $term->taxonomy ); |
727 |
if ( ! $tax ) { |
|
728 |
$caps[] = 'do_not_allow'; |
|
729 |
break; |
|
730 |
} |
|
731 |
||
16 | 732 |
if ( 'delete_term' === $cap |
733 |
&& ( get_option( 'default_' . $term->taxonomy ) == $term->term_id |
|
734 |
|| get_option( 'default_term_' . $term->taxonomy ) == $term->term_id ) |
|
735 |
) { |
|
9 | 736 |
$caps[] = 'do_not_allow'; |
737 |
break; |
|
738 |
} |
|
739 |
||
740 |
$taxo_cap = $cap . 's'; |
|
741 |
||
742 |
$caps = map_meta_cap( $tax->cap->$taxo_cap, $user_id, $term_id ); |
|
743 |
||
744 |
break; |
|
745 |
case 'manage_post_tags': |
|
746 |
case 'edit_categories': |
|
747 |
case 'edit_post_tags': |
|
748 |
case 'delete_categories': |
|
749 |
case 'delete_post_tags': |
|
750 |
$caps[] = 'manage_categories'; |
|
751 |
break; |
|
752 |
case 'assign_categories': |
|
753 |
case 'assign_post_tags': |
|
754 |
$caps[] = 'edit_posts'; |
|
755 |
break; |
|
756 |
case 'create_sites': |
|
757 |
case 'delete_sites': |
|
758 |
case 'manage_network': |
|
759 |
case 'manage_sites': |
|
760 |
case 'manage_network_users': |
|
761 |
case 'manage_network_plugins': |
|
762 |
case 'manage_network_themes': |
|
763 |
case 'manage_network_options': |
|
764 |
case 'upgrade_network': |
|
0 | 765 |
$caps[] = $cap; |
9 | 766 |
break; |
767 |
case 'setup_network': |
|
768 |
if ( is_multisite() ) { |
|
769 |
$caps[] = 'manage_network_options'; |
|
770 |
} else { |
|
771 |
$caps[] = 'manage_options'; |
|
772 |
} |
|
773 |
break; |
|
774 |
case 'update_php': |
|
775 |
if ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
776 |
$caps[] = 'do_not_allow'; |
|
777 |
} else { |
|
778 |
$caps[] = 'update_core'; |
|
779 |
} |
|
780 |
break; |
|
18 | 781 |
case 'update_https': |
782 |
if ( is_multisite() && ! is_super_admin( $user_id ) ) { |
|
783 |
$caps[] = 'do_not_allow'; |
|
784 |
} else { |
|
785 |
$caps[] = 'manage_options'; |
|
786 |
$caps[] = 'update_core'; |
|
787 |
} |
|
788 |
break; |
|
9 | 789 |
case 'export_others_personal_data': |
790 |
case 'erase_others_personal_data': |
|
791 |
case 'manage_privacy_options': |
|
792 |
$caps[] = is_multisite() ? 'manage_network' : 'manage_options'; |
|
793 |
break; |
|
18 | 794 |
case 'create_app_password': |
795 |
case 'list_app_passwords': |
|
796 |
case 'read_app_password': |
|
797 |
case 'edit_app_password': |
|
798 |
case 'delete_app_passwords': |
|
799 |
case 'delete_app_password': |
|
800 |
$caps = map_meta_cap( 'edit_user', $user_id, $args[0] ); |
|
801 |
break; |
|
9 | 802 |
default: |
803 |
// Handle meta capabilities for custom post types. |
|
804 |
global $post_type_meta_caps; |
|
805 |
if ( isset( $post_type_meta_caps[ $cap ] ) ) { |
|
16 | 806 |
return map_meta_cap( $post_type_meta_caps[ $cap ], $user_id, ...$args ); |
9 | 807 |
} |
808 |
||
809 |
// Block capabilities map to their post equivalent. |
|
810 |
$block_caps = array( |
|
811 |
'edit_blocks', |
|
812 |
'edit_others_blocks', |
|
813 |
'publish_blocks', |
|
814 |
'read_private_blocks', |
|
815 |
'delete_blocks', |
|
816 |
'delete_private_blocks', |
|
817 |
'delete_published_blocks', |
|
818 |
'delete_others_blocks', |
|
819 |
'edit_private_blocks', |
|
820 |
'edit_published_blocks', |
|
821 |
); |
|
822 |
if ( in_array( $cap, $block_caps, true ) ) { |
|
823 |
$cap = str_replace( '_blocks', '_posts', $cap ); |
|
824 |
} |
|
825 |
||
826 |
// If no meta caps match, return the original cap. |
|
0 | 827 |
$caps[] = $cap; |
828 |
} |
|
829 |
||
5 | 830 |
/** |
18 | 831 |
* Filters the primitive capabilities required of the given user to satisfy the |
832 |
* capability being checked. |
|
5 | 833 |
* |
834 |
* @since 2.8.0 |
|
835 |
* |
|
18 | 836 |
* @param string[] $caps Primitive capabilities required of the user. |
837 |
* @param string $cap Capability being checked. |
|
9 | 838 |
* @param int $user_id The user ID. |
18 | 839 |
* @param array $args Adds context to the capability check, typically |
840 |
* starting with an object ID. |
|
5 | 841 |
*/ |
842 |
return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args ); |
|
0 | 843 |
} |
844 |
||
845 |
/** |
|
16 | 846 |
* Returns whether the current user has the specified capability. |
847 |
* |
|
848 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
849 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
850 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
851 |
* |
|
852 |
* Example usage: |
|
853 |
* |
|
854 |
* current_user_can( 'edit_posts' ); |
|
855 |
* current_user_can( 'edit_post', $post->ID ); |
|
856 |
* current_user_can( 'edit_post_meta', $post->ID, $meta_key ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
857 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
* While checking against particular roles in place of a capability is supported |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
* in part, this practice is discouraged as it may produce unreliable results. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
860 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
* Note: Will always return true if the current user is a super admin, unless specifically denied. |
0 | 862 |
* |
863 |
* @since 2.0.0 |
|
16 | 864 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
865 |
* by adding it to the function signature. |
|
18 | 866 |
* @since 5.8.0 Converted to wrapper for the user_can() function. |
0 | 867 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
* @see WP_User::has_cap() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
* @see map_meta_cap() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
* @param string $capability Capability name. |
16 | 872 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
873 |
* @return bool Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
874 |
* passed, whether the current user has the given meta capability for the given object. |
0 | 875 |
*/ |
16 | 876 |
function current_user_can( $capability, ...$args ) { |
18 | 877 |
return user_can( wp_get_current_user(), $capability, ...$args ); |
0 | 878 |
} |
879 |
||
880 |
/** |
|
16 | 881 |
* Returns whether the current user has the specified capability for a given site. |
882 |
* |
|
883 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
884 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
885 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
886 |
* |
|
887 |
* Example usage: |
|
888 |
* |
|
889 |
* current_user_can_for_blog( $blog_id, 'edit_posts' ); |
|
890 |
* current_user_can_for_blog( $blog_id, 'edit_post', $post->ID ); |
|
891 |
* current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key ); |
|
0 | 892 |
* |
893 |
* @since 3.0.0 |
|
16 | 894 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
895 |
* by adding it to the function signature. |
|
18 | 896 |
* @since 5.8.0 Wraps current_user_can() after switching to blog. |
0 | 897 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
898 |
* @param int $blog_id Site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
899 |
* @param string $capability Capability name. |
16 | 900 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
* @return bool Whether the user has the given capability. |
0 | 902 |
*/ |
16 | 903 |
function current_user_can_for_blog( $blog_id, $capability, ...$args ) { |
5 | 904 |
$switched = is_multisite() ? switch_to_blog( $blog_id ) : false; |
0 | 905 |
|
18 | 906 |
$can = current_user_can( $capability, ...$args ); |
0 | 907 |
|
5 | 908 |
if ( $switched ) { |
0 | 909 |
restore_current_blog(); |
5 | 910 |
} |
0 | 911 |
|
912 |
return $can; |
|
913 |
} |
|
914 |
||
915 |
/** |
|
16 | 916 |
* Returns whether the author of the supplied post has the specified capability. |
917 |
* |
|
918 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
919 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
920 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
921 |
* |
|
922 |
* Example usage: |
|
923 |
* |
|
924 |
* author_can( $post, 'edit_posts' ); |
|
925 |
* author_can( $post, 'edit_post', $post->ID ); |
|
926 |
* author_can( $post, 'edit_post_meta', $post->ID, $meta_key ); |
|
0 | 927 |
* |
928 |
* @since 2.9.0 |
|
16 | 929 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
930 |
* by adding it to the function signature. |
|
0 | 931 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
932 |
* @param int|WP_Post $post Post ID or post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
933 |
* @param string $capability Capability name. |
16 | 934 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
* @return bool Whether the post author has the given capability. |
0 | 936 |
*/ |
16 | 937 |
function author_can( $post, $capability, ...$args ) { |
938 |
$post = get_post( $post ); |
|
939 |
if ( ! $post ) { |
|
0 | 940 |
return false; |
9 | 941 |
} |
0 | 942 |
|
943 |
$author = get_userdata( $post->post_author ); |
|
944 |
||
9 | 945 |
if ( ! $author ) { |
0 | 946 |
return false; |
9 | 947 |
} |
0 | 948 |
|
16 | 949 |
return $author->has_cap( $capability, ...$args ); |
0 | 950 |
} |
951 |
||
952 |
/** |
|
16 | 953 |
* Returns whether a particular user has the specified capability. |
954 |
* |
|
955 |
* This function also accepts an ID of an object to check against if the capability is a meta capability. Meta |
|
956 |
* capabilities such as `edit_post` and `edit_user` are capabilities used by the `map_meta_cap()` function to |
|
957 |
* map to primitive capabilities that a user or role has, such as `edit_posts` and `edit_others_posts`. |
|
958 |
* |
|
959 |
* Example usage: |
|
960 |
* |
|
961 |
* user_can( $user->ID, 'edit_posts' ); |
|
962 |
* user_can( $user->ID, 'edit_post', $post->ID ); |
|
963 |
* user_can( $user->ID, 'edit_post_meta', $post->ID, $meta_key ); |
|
0 | 964 |
* |
965 |
* @since 3.1.0 |
|
16 | 966 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
967 |
* by adding it to the function signature. |
|
0 | 968 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
* @param int|WP_User $user User ID or object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
* @param string $capability Capability name. |
16 | 971 |
* @param mixed ...$args Optional further parameters, typically starting with an object ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
* @return bool Whether the user has the given capability. |
0 | 973 |
*/ |
16 | 974 |
function user_can( $user, $capability, ...$args ) { |
9 | 975 |
if ( ! is_object( $user ) ) { |
0 | 976 |
$user = get_userdata( $user ); |
9 | 977 |
} |
0 | 978 |
|
18 | 979 |
if ( empty( $user ) ) { |
980 |
// User is logged out, create anonymous user object. |
|
981 |
$user = new WP_User( 0 ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
982 |
$user->init( new stdClass() ); |
9 | 983 |
} |
0 | 984 |
|
16 | 985 |
return $user->has_cap( $capability, ...$args ); |
0 | 986 |
} |
987 |
||
988 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
* Retrieves the global WP_Roles instance and instantiates it if necessary. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
* |
16 | 993 |
* @global WP_Roles $wp_roles WordPress role management object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
* @return WP_Roles WP_Roles global instance if not already instantiated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
997 |
function wp_roles() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
998 |
global $wp_roles; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
999 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1000 |
if ( ! isset( $wp_roles ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1001 |
$wp_roles = new WP_Roles(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1002 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1003 |
return $wp_roles; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1005 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1006 |
/** |
19 | 1007 |
* Retrieves role object. |
0 | 1008 |
* |
1009 |
* @since 2.0.0 |
|
1010 |
* |
|
1011 |
* @param string $role Role name. |
|
1012 |
* @return WP_Role|null WP_Role object if found, null if the role does not exist. |
|
1013 |
*/ |
|
1014 |
function get_role( $role ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1015 |
return wp_roles()->get_role( $role ); |
0 | 1016 |
} |
1017 |
||
1018 |
/** |
|
19 | 1019 |
* Adds a role, if it does not exist. |
0 | 1020 |
* |
1021 |
* @since 2.0.0 |
|
1022 |
* |
|
16 | 1023 |
* @param string $role Role name. |
0 | 1024 |
* @param string $display_name Display name for role. |
16 | 1025 |
* @param bool[] $capabilities List of capabilities keyed by the capability name, |
1026 |
* e.g. array( 'edit_posts' => true, 'delete_posts' => false ). |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1027 |
* @return WP_Role|void WP_Role object, if the role is added. |
0 | 1028 |
*/ |
1029 |
function add_role( $role, $display_name, $capabilities = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1030 |
if ( empty( $role ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1031 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1032 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1033 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1034 |
return wp_roles()->add_role( $role, $display_name, $capabilities ); |
0 | 1035 |
} |
1036 |
||
1037 |
/** |
|
19 | 1038 |
* Removes a role, if it exists. |
0 | 1039 |
* |
1040 |
* @since 2.0.0 |
|
1041 |
* |
|
1042 |
* @param string $role Role name. |
|
1043 |
*/ |
|
1044 |
function remove_role( $role ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1045 |
wp_roles()->remove_role( $role ); |
0 | 1046 |
} |
1047 |
||
1048 |
/** |
|
19 | 1049 |
* Retrieves a list of super admins. |
0 | 1050 |
* |
1051 |
* @since 3.0.0 |
|
1052 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
* @global array $super_admins |
0 | 1054 |
* |
16 | 1055 |
* @return string[] List of super admin logins. |
0 | 1056 |
*/ |
1057 |
function get_super_admins() { |
|
1058 |
global $super_admins; |
|
1059 |
||
9 | 1060 |
if ( isset( $super_admins ) ) { |
0 | 1061 |
return $super_admins; |
9 | 1062 |
} else { |
1063 |
return get_site_option( 'site_admins', array( 'admin' ) ); |
|
1064 |
} |
|
0 | 1065 |
} |
1066 |
||
1067 |
/** |
|
19 | 1068 |
* Determines whether user is a site admin. |
0 | 1069 |
* |
1070 |
* @since 3.0.0 |
|
1071 |
* |
|
18 | 1072 |
* @param int|false $user_id Optional. The ID of a user. Defaults to false, to check the current user. |
1073 |
* @return bool Whether the user is a site admin. |
|
0 | 1074 |
*/ |
1075 |
function is_super_admin( $user_id = false ) { |
|
19 | 1076 |
if ( ! $user_id ) { |
0 | 1077 |
$user = wp_get_current_user(); |
9 | 1078 |
} else { |
0 | 1079 |
$user = get_userdata( $user_id ); |
9 | 1080 |
} |
0 | 1081 |
|
9 | 1082 |
if ( ! $user || ! $user->exists() ) { |
0 | 1083 |
return false; |
9 | 1084 |
} |
0 | 1085 |
|
1086 |
if ( is_multisite() ) { |
|
1087 |
$super_admins = get_super_admins(); |
|
16 | 1088 |
if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins, true ) ) { |
0 | 1089 |
return true; |
9 | 1090 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1091 |
} elseif ( $user->has_cap( 'delete_users' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1092 |
return true; |
0 | 1093 |
} |
1094 |
||
1095 |
return false; |
|
1096 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1097 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1098 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1099 |
* Grants Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1100 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1101 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1102 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1103 |
* @global array $super_admins |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1104 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1105 |
* @param int $user_id ID of the user to be granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1106 |
* @return bool True on success, false on failure. This can fail when the user is |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1107 |
* already a super admin or when the `$super_admins` global is defined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1108 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1109 |
function grant_super_admin( $user_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1110 |
// If global super_admins override is defined, there is nothing to do here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1111 |
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1112 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1113 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1114 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1115 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1116 |
* Fires before the user is granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1117 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1119 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
* @param int $user_id ID of the user that is about to be granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1122 |
do_action( 'grant_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1123 |
|
16 | 1124 |
// Directly fetch site_admins instead of using get_super_admins(). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1125 |
$super_admins = get_site_option( 'site_admins', array( 'admin' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1127 |
$user = get_userdata( $user_id ); |
16 | 1128 |
if ( $user && ! in_array( $user->user_login, $super_admins, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
$super_admins[] = $user->user_login; |
9 | 1130 |
update_site_option( 'site_admins', $super_admins ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
* Fires after the user is granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
* @param int $user_id ID of the user that was granted Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
do_action( 'granted_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1140 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1141 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1144 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1146 |
* Revokes Super Admin privileges. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1148 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1149 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
* @global array $super_admins |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1151 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1152 |
* @param int $user_id ID of the user Super Admin privileges to be revoked from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1153 |
* @return bool True on success, false on failure. This can fail when the user's email |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
* is the network admin email or when the `$super_admins` global is defined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
function revoke_super_admin( $user_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
// If global super_admins override is defined, there is nothing to do here. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
if ( isset( $GLOBALS['super_admins'] ) || ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1160 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1161 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1162 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1163 |
* Fires before the user's Super Admin privileges are revoked. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1164 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1165 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1167 |
* @param int $user_id ID of the user Super Admin privileges are being revoked from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1168 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1169 |
do_action( 'revoke_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1170 |
|
16 | 1171 |
// Directly fetch site_admins instead of using get_super_admins(). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1172 |
$super_admins = get_site_option( 'site_admins', array( 'admin' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
$user = get_userdata( $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
if ( $user && 0 !== strcasecmp( $user->user_email, get_site_option( 'admin_email' ) ) ) { |
16 | 1176 |
$key = array_search( $user->user_login, $super_admins, true ); |
1177 |
if ( false !== $key ) { |
|
9 | 1178 |
unset( $super_admins[ $key ] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
update_site_option( 'site_admins', $super_admins ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
* Fires after the user's Super Admin privileges are revoked. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1186 |
* @param int $user_id ID of the user Super Admin privileges were revoked from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1187 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
do_action( 'revoked_super_admin', $user_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1191 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1193 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1195 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
* Filters the user capabilities to grant the 'install_languages' capability as necessary. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1198 |
* A user must have at least one out of the 'update_core', 'install_plugins', and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
* 'install_themes' capabilities to qualify for 'install_languages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
* |
9 | 1203 |
* @param bool[] $allcaps An array of all the user's capabilities. |
1204 |
* @return bool[] Filtered array of the user's capabilities. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
function wp_maybe_grant_install_languages_cap( $allcaps ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
if ( ! empty( $allcaps['update_core'] ) || ! empty( $allcaps['install_plugins'] ) || ! empty( $allcaps['install_themes'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
$allcaps['install_languages'] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1209 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
return $allcaps; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
} |
9 | 1213 |
|
1214 |
/** |
|
1215 |
* Filters the user capabilities to grant the 'resume_plugins' and 'resume_themes' capabilities as necessary. |
|
1216 |
* |
|
1217 |
* @since 5.2.0 |
|
1218 |
* |
|
1219 |
* @param bool[] $allcaps An array of all the user's capabilities. |
|
1220 |
* @return bool[] Filtered array of the user's capabilities. |
|
1221 |
*/ |
|
1222 |
function wp_maybe_grant_resume_extensions_caps( $allcaps ) { |
|
1223 |
// Even in a multisite, regular administrators should be able to resume plugins. |
|
1224 |
if ( ! empty( $allcaps['activate_plugins'] ) ) { |
|
1225 |
$allcaps['resume_plugins'] = true; |
|
1226 |
} |
|
1227 |
||
1228 |
// Even in a multisite, regular administrators should be able to resume themes. |
|
1229 |
if ( ! empty( $allcaps['switch_themes'] ) ) { |
|
1230 |
$allcaps['resume_themes'] = true; |
|
1231 |
} |
|
1232 |
||
1233 |
return $allcaps; |
|
1234 |
} |
|
1235 |
||
1236 |
/** |
|
1237 |
* Filters the user capabilities to grant the 'view_site_health_checks' capabilities as necessary. |
|
1238 |
* |
|
1239 |
* @since 5.2.2 |
|
1240 |
* |
|
1241 |
* @param bool[] $allcaps An array of all the user's capabilities. |
|
1242 |
* @param string[] $caps Required primitive capabilities for the requested capability. |
|
1243 |
* @param array $args { |
|
1244 |
* Arguments that accompany the requested capability check. |
|
1245 |
* |
|
1246 |
* @type string $0 Requested capability. |
|
1247 |
* @type int $1 Concerned user ID. |
|
1248 |
* @type mixed ...$2 Optional second and further parameters, typically object ID. |
|
1249 |
* } |
|
1250 |
* @param WP_User $user The user object. |
|
1251 |
* @return bool[] Filtered array of the user's capabilities. |
|
1252 |
*/ |
|
1253 |
function wp_maybe_grant_site_health_caps( $allcaps, $caps, $args, $user ) { |
|
1254 |
if ( ! empty( $allcaps['install_plugins'] ) && ( ! is_multisite() || is_super_admin( $user->ID ) ) ) { |
|
1255 |
$allcaps['view_site_health_checks'] = true; |
|
1256 |
} |
|
1257 |
||
1258 |
return $allcaps; |
|
1259 |
} |
|
1260 |
||
1261 |
return; |
|
1262 |
||
1263 |
// Dummy gettext calls to get strings in the catalog. |
|
16 | 1264 |
/* translators: User role for administrators. */ |
9 | 1265 |
_x( 'Administrator', 'User role' ); |
16 | 1266 |
/* translators: User role for editors. */ |
9 | 1267 |
_x( 'Editor', 'User role' ); |
16 | 1268 |
/* translators: User role for authors. */ |
9 | 1269 |
_x( 'Author', 'User role' ); |
16 | 1270 |
/* translators: User role for contributors. */ |
9 | 1271 |
_x( 'Contributor', 'User role' ); |
16 | 1272 |
/* translators: User role for subscribers. */ |
9 | 1273 |
_x( 'Subscriber', 'User role' ); |