| Conditions | 123 |
| Paths | 240 |
| Total Lines | 395 |
| Code Lines | 258 |
| Lines | 131 |
| Ratio | 33.16 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | function map_meta_cap( $cap, $user_id ) { |
||
| 30 | $args = array_slice( func_get_args(), 2 ); |
||
| 31 | $caps = array(); |
||
| 32 | |||
| 33 | switch ( $cap ) { |
||
| 34 | case 'remove_user': |
||
| 35 | $caps[] = 'remove_users'; |
||
| 36 | break; |
||
| 37 | case 'promote_user': |
||
| 38 | case 'add_users': |
||
| 39 | $caps[] = 'promote_users'; |
||
| 40 | break; |
||
| 41 | case 'edit_user': |
||
| 42 | case 'edit_users': |
||
| 43 | // Allow user to edit itself |
||
| 44 | if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] ) |
||
| 45 | break; |
||
| 46 | |||
| 47 | // In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin. |
||
| 48 | if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) { |
||
| 49 | $caps[] = 'do_not_allow'; |
||
| 50 | } else { |
||
| 51 | $caps[] = 'edit_users'; // edit_user maps to edit_users. |
||
| 52 | } |
||
| 53 | break; |
||
| 54 | case 'delete_post': |
||
| 55 | View Code Duplication | case 'delete_page': |
|
| 56 | $post = get_post( $args[0] ); |
||
| 57 | if ( ! $post ) { |
||
| 58 | $caps[] = 'do_not_allow'; |
||
| 59 | break; |
||
| 60 | } |
||
| 61 | |||
| 62 | if ( 'revision' == $post->post_type ) { |
||
| 63 | $post = get_post( $post->post_parent ); |
||
| 64 | if ( ! $post ) { |
||
| 65 | $caps[] = 'do_not_allow'; |
||
| 66 | break; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | $post_type = get_post_type_object( $post->post_type ); |
||
| 71 | if ( ! $post_type ) { |
||
| 72 | /* translators: 1: post type, 2: capability name */ |
||
| 73 | _doing_it_wrong( __FUNCTION__, sprintf( __( '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.' ), $post->post_type, $cap ), '4.4.0' ); |
||
| 74 | $caps[] = 'edit_others_posts'; |
||
| 75 | break; |
||
| 76 | } |
||
| 77 | |||
| 78 | if ( ! $post_type->map_meta_cap ) { |
||
| 79 | $caps[] = $post_type->cap->$cap; |
||
| 80 | // Prior to 3.1 we would re-call map_meta_cap here. |
||
| 81 | if ( 'delete_post' == $cap ) |
||
| 82 | $cap = $post_type->cap->$cap; |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | |||
| 86 | // If the post author is set and the user is the author... |
||
| 87 | if ( $post->post_author && $user_id == $post->post_author ) { |
||
| 88 | // If the post is published or scheduled... |
||
| 89 | if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
||
| 90 | $caps[] = $post_type->cap->delete_published_posts; |
||
| 91 | } elseif ( 'trash' == $post->post_status ) { |
||
| 92 | $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true ); |
||
| 93 | if ( in_array( $status, array( 'publish', 'future' ), true ) ) { |
||
| 94 | $caps[] = $post_type->cap->delete_published_posts; |
||
| 95 | } else { |
||
| 96 | $caps[] = $post_type->cap->delete_posts; |
||
| 97 | } |
||
| 98 | } else { |
||
| 99 | // If the post is draft... |
||
| 100 | $caps[] = $post_type->cap->delete_posts; |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | // The user is trying to edit someone else's post. |
||
| 104 | $caps[] = $post_type->cap->delete_others_posts; |
||
| 105 | // The post is published or scheduled, extra cap required. |
||
| 106 | if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
||
| 107 | $caps[] = $post_type->cap->delete_published_posts; |
||
| 108 | } elseif ( 'private' == $post->post_status ) { |
||
| 109 | $caps[] = $post_type->cap->delete_private_posts; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | break; |
||
| 113 | // edit_post breaks down to edit_posts, edit_published_posts, or |
||
| 114 | // edit_others_posts |
||
| 115 | case 'edit_post': |
||
| 116 | View Code Duplication | case 'edit_page': |
|
| 117 | $post = get_post( $args[0] ); |
||
| 118 | if ( ! $post ) { |
||
| 119 | $caps[] = 'do_not_allow'; |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( 'revision' == $post->post_type ) { |
||
| 124 | $post = get_post( $post->post_parent ); |
||
| 125 | if ( ! $post ) { |
||
| 126 | $caps[] = 'do_not_allow'; |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $post_type = get_post_type_object( $post->post_type ); |
||
| 132 | if ( ! $post_type ) { |
||
| 133 | /* translators: 1: post type, 2: capability name */ |
||
| 134 | _doing_it_wrong( __FUNCTION__, sprintf( __( '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.' ), $post->post_type, $cap ), '4.4.0' ); |
||
| 135 | $caps[] = 'edit_others_posts'; |
||
| 136 | break; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ( ! $post_type->map_meta_cap ) { |
||
| 140 | $caps[] = $post_type->cap->$cap; |
||
| 141 | // Prior to 3.1 we would re-call map_meta_cap here. |
||
| 142 | if ( 'edit_post' == $cap ) |
||
| 143 | $cap = $post_type->cap->$cap; |
||
| 144 | break; |
||
| 145 | } |
||
| 146 | |||
| 147 | // If the post author is set and the user is the author... |
||
| 148 | if ( $post->post_author && $user_id == $post->post_author ) { |
||
| 149 | // If the post is published or scheduled... |
||
| 150 | if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
||
| 151 | $caps[] = $post_type->cap->edit_published_posts; |
||
| 152 | } elseif ( 'trash' == $post->post_status ) { |
||
| 153 | $status = get_post_meta( $post->ID, '_wp_trash_meta_status', true ); |
||
| 154 | if ( in_array( $status, array( 'publish', 'future' ), true ) ) { |
||
| 155 | $caps[] = $post_type->cap->edit_published_posts; |
||
| 156 | } else { |
||
| 157 | $caps[] = $post_type->cap->edit_posts; |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | // If the post is draft... |
||
| 161 | $caps[] = $post_type->cap->edit_posts; |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | // The user is trying to edit someone else's post. |
||
| 165 | $caps[] = $post_type->cap->edit_others_posts; |
||
| 166 | // The post is published or scheduled, extra cap required. |
||
| 167 | if ( in_array( $post->post_status, array( 'publish', 'future' ), true ) ) { |
||
| 168 | $caps[] = $post_type->cap->edit_published_posts; |
||
| 169 | } elseif ( 'private' == $post->post_status ) { |
||
| 170 | $caps[] = $post_type->cap->edit_private_posts; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | break; |
||
| 174 | case 'read_post': |
||
| 175 | case 'read_page': |
||
| 176 | $post = get_post( $args[0] ); |
||
| 177 | if ( ! $post ) { |
||
| 178 | $caps[] = 'do_not_allow'; |
||
| 179 | break; |
||
| 180 | } |
||
| 181 | |||
| 182 | if ( 'revision' == $post->post_type ) { |
||
| 183 | $post = get_post( $post->post_parent ); |
||
| 184 | if ( ! $post ) { |
||
| 185 | $caps[] = 'do_not_allow'; |
||
| 186 | break; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $post_type = get_post_type_object( $post->post_type ); |
||
| 191 | if ( ! $post_type ) { |
||
| 192 | /* translators: 1: post type, 2: capability name */ |
||
| 193 | _doing_it_wrong( __FUNCTION__, sprintf( __( '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.' ), $post->post_type, $cap ), '4.4.0' ); |
||
| 194 | $caps[] = 'edit_others_posts'; |
||
| 195 | break; |
||
| 196 | } |
||
| 197 | |||
| 198 | if ( ! $post_type->map_meta_cap ) { |
||
| 199 | $caps[] = $post_type->cap->$cap; |
||
| 200 | // Prior to 3.1 we would re-call map_meta_cap here. |
||
| 201 | if ( 'read_post' == $cap ) |
||
| 202 | $cap = $post_type->cap->$cap; |
||
| 203 | break; |
||
| 204 | } |
||
| 205 | |||
| 206 | $status_obj = get_post_status_object( $post->post_status ); |
||
| 207 | if ( $status_obj->public ) { |
||
| 208 | $caps[] = $post_type->cap->read; |
||
| 209 | break; |
||
| 210 | } |
||
| 211 | |||
| 212 | if ( $post->post_author && $user_id == $post->post_author ) { |
||
| 213 | $caps[] = $post_type->cap->read; |
||
| 214 | } elseif ( $status_obj->private ) { |
||
| 215 | $caps[] = $post_type->cap->read_private_posts; |
||
| 216 | } else { |
||
| 217 | $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
||
| 218 | } |
||
| 219 | break; |
||
| 220 | case 'publish_post': |
||
| 221 | $post = get_post( $args[0] ); |
||
| 222 | if ( ! $post ) { |
||
| 223 | $caps[] = 'do_not_allow'; |
||
| 224 | break; |
||
| 225 | } |
||
| 226 | |||
| 227 | $post_type = get_post_type_object( $post->post_type ); |
||
| 228 | if ( ! $post_type ) { |
||
| 229 | /* translators: 1: post type, 2: capability name */ |
||
| 230 | _doing_it_wrong( __FUNCTION__, sprintf( __( '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.' ), $post->post_type, $cap ), '4.4.0' ); |
||
| 231 | $caps[] = 'edit_others_posts'; |
||
| 232 | break; |
||
| 233 | } |
||
| 234 | |||
| 235 | $caps[] = $post_type->cap->publish_posts; |
||
| 236 | break; |
||
| 237 | case 'edit_post_meta': |
||
| 238 | case 'delete_post_meta': |
||
| 239 | case 'add_post_meta': |
||
| 240 | $post = get_post( $args[0] ); |
||
| 241 | if ( ! $post ) { |
||
| 242 | $caps[] = 'do_not_allow'; |
||
| 243 | break; |
||
| 244 | } |
||
| 245 | |||
| 246 | $post_type = get_post_type( $post ); |
||
| 247 | |||
| 248 | $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
||
| 249 | |||
| 250 | $meta_key = isset( $args[ 1 ] ) ? $args[ 1 ] : false; |
||
| 251 | |||
| 252 | if ( $meta_key && ( has_filter( "auth_post_meta_{$meta_key}" ) || has_filter( "auth_post_{$post_type}_meta_{$meta_key}" ) ) ) { |
||
| 253 | /** |
||
| 254 | * Filters whether the user is allowed to add post meta to a post. |
||
| 255 | * |
||
| 256 | * The dynamic portion of the hook name, `$meta_key`, refers to the |
||
| 257 | * meta key passed to map_meta_cap(). |
||
| 258 | * |
||
| 259 | * @since 3.3.0 |
||
| 260 | * |
||
| 261 | * @param bool $allowed Whether the user can add the post meta. Default false. |
||
| 262 | * @param string $meta_key The meta key. |
||
| 263 | * @param int $post_id Post ID. |
||
| 264 | * @param int $user_id User ID. |
||
| 265 | * @param string $cap Capability name. |
||
| 266 | * @param array $caps User capabilities. |
||
| 267 | */ |
||
| 268 | $allowed = apply_filters( "auth_post_meta_{$meta_key}", false, $meta_key, $post->ID, $user_id, $cap, $caps ); |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Filters whether the user is allowed to add post meta to a post of a given type. |
||
| 272 | * |
||
| 273 | * The dynamic portions of the hook name, `$meta_key` and `$post_type`, |
||
| 274 | * refer to the meta key passed to map_meta_cap() and the post type, respectively. |
||
| 275 | * |
||
| 276 | * @since 4.6.0 |
||
| 277 | * |
||
| 278 | * @param bool $allowed Whether the user can add the post meta. Default false. |
||
| 279 | * @param string $meta_key The meta key. |
||
| 280 | * @param int $post_id Post ID. |
||
| 281 | * @param int $user_id User ID. |
||
| 282 | * @param string $cap Capability name. |
||
| 283 | * @param array $caps User capabilities. |
||
| 284 | */ |
||
| 285 | $allowed = apply_filters( "auth_post_{$post_type}_meta_{$meta_key}", $allowed, $meta_key, $post->ID, $user_id, $cap, $caps ); |
||
| 286 | |||
| 287 | if ( ! $allowed ) |
||
| 288 | $caps[] = $cap; |
||
| 289 | } elseif ( $meta_key && is_protected_meta( $meta_key, 'post' ) ) { |
||
| 290 | $caps[] = $cap; |
||
| 291 | } |
||
| 292 | break; |
||
| 293 | case 'edit_comment': |
||
| 294 | $comment = get_comment( $args[0] ); |
||
| 295 | if ( ! $comment ) { |
||
| 296 | $caps[] = 'do_not_allow'; |
||
| 297 | break; |
||
| 298 | } |
||
| 299 | |||
| 300 | $post = get_post( $comment->comment_post_ID ); |
||
| 301 | |||
| 302 | /* |
||
| 303 | * If the post doesn't exist, we have an orphaned comment. |
||
| 304 | * Fall back to the edit_posts capability, instead. |
||
| 305 | */ |
||
| 306 | if ( $post ) { |
||
| 307 | $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); |
||
| 308 | } else { |
||
| 309 | $caps = map_meta_cap( 'edit_posts', $user_id ); |
||
| 310 | } |
||
| 311 | break; |
||
| 312 | View Code Duplication | case 'unfiltered_upload': |
|
| 313 | if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) ) |
||
| 314 | $caps[] = $cap; |
||
| 315 | else |
||
| 316 | $caps[] = 'do_not_allow'; |
||
| 317 | break; |
||
| 318 | View Code Duplication | case 'unfiltered_html' : |
|
| 319 | // Disallow unfiltered_html for all users, even admins and super admins. |
||
| 320 | if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) |
||
| 321 | $caps[] = 'do_not_allow'; |
||
| 322 | elseif ( is_multisite() && ! is_super_admin( $user_id ) ) |
||
| 323 | $caps[] = 'do_not_allow'; |
||
| 324 | else |
||
| 325 | $caps[] = $cap; |
||
| 326 | break; |
||
| 327 | case 'edit_files': |
||
| 328 | case 'edit_plugins': |
||
| 329 | case 'edit_themes': |
||
| 330 | // Disallow the file editors. |
||
| 331 | if ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) |
||
| 332 | $caps[] = 'do_not_allow'; |
||
| 333 | elseif ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) |
||
| 334 | $caps[] = 'do_not_allow'; |
||
| 335 | elseif ( is_multisite() && ! is_super_admin( $user_id ) ) |
||
| 336 | $caps[] = 'do_not_allow'; |
||
| 337 | else |
||
| 338 | $caps[] = $cap; |
||
| 339 | break; |
||
| 340 | case 'update_plugins': |
||
| 341 | case 'delete_plugins': |
||
| 342 | case 'install_plugins': |
||
| 343 | case 'upload_plugins': |
||
| 344 | case 'update_themes': |
||
| 345 | case 'delete_themes': |
||
| 346 | case 'install_themes': |
||
| 347 | case 'upload_themes': |
||
| 348 | case 'update_core': |
||
| 349 | // Disallow anything that creates, deletes, or updates core, plugin, or theme files. |
||
| 350 | // Files in uploads are excepted. |
||
| 351 | if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) { |
||
| 352 | $caps[] = 'do_not_allow'; |
||
| 353 | } elseif ( is_multisite() && ! is_super_admin( $user_id ) ) { |
||
| 354 | $caps[] = 'do_not_allow'; |
||
| 355 | } elseif ( 'upload_themes' === $cap ) { |
||
| 356 | $caps[] = 'install_themes'; |
||
| 357 | } elseif ( 'upload_plugins' === $cap ) { |
||
| 358 | $caps[] = 'install_plugins'; |
||
| 359 | } else { |
||
| 360 | $caps[] = $cap; |
||
| 361 | } |
||
| 362 | break; |
||
| 363 | case 'activate_plugins': |
||
| 364 | $caps[] = $cap; |
||
| 365 | if ( is_multisite() ) { |
||
| 366 | // update_, install_, and delete_ are handled above with is_super_admin(). |
||
| 367 | $menu_perms = get_site_option( 'menu_items', array() ); |
||
| 368 | if ( empty( $menu_perms['plugins'] ) ) |
||
| 369 | $caps[] = 'manage_network_plugins'; |
||
| 370 | } |
||
| 371 | break; |
||
| 372 | case 'delete_user': |
||
| 373 | case 'delete_users': |
||
| 374 | // If multisite only super admins can delete users. |
||
| 375 | if ( is_multisite() && ! is_super_admin( $user_id ) ) |
||
| 376 | $caps[] = 'do_not_allow'; |
||
| 377 | else |
||
| 378 | $caps[] = 'delete_users'; // delete_user maps to delete_users. |
||
| 379 | break; |
||
| 380 | case 'create_users': |
||
| 381 | if ( !is_multisite() ) |
||
| 382 | $caps[] = $cap; |
||
| 383 | elseif ( is_super_admin( $user_id ) || get_site_option( 'add_new_users' ) ) |
||
| 384 | $caps[] = $cap; |
||
| 385 | else |
||
| 386 | $caps[] = 'do_not_allow'; |
||
| 387 | break; |
||
| 388 | case 'manage_links' : |
||
| 389 | if ( get_option( 'link_manager_enabled' ) ) |
||
| 390 | $caps[] = $cap; |
||
| 391 | else |
||
| 392 | $caps[] = 'do_not_allow'; |
||
| 393 | break; |
||
| 394 | case 'customize' : |
||
| 395 | $caps[] = 'edit_theme_options'; |
||
| 396 | break; |
||
| 397 | case 'delete_site': |
||
| 398 | $caps[] = 'manage_options'; |
||
| 399 | break; |
||
| 400 | default: |
||
| 401 | // Handle meta capabilities for custom post types. |
||
| 402 | global $post_type_meta_caps; |
||
| 403 | if ( isset( $post_type_meta_caps[ $cap ] ) ) { |
||
| 404 | $args = array_merge( array( $post_type_meta_caps[ $cap ], $user_id ), $args ); |
||
| 405 | return call_user_func_array( 'map_meta_cap', $args ); |
||
| 406 | } |
||
| 407 | |||
| 408 | // If no meta caps match, return the original cap. |
||
| 409 | $caps[] = $cap; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Filters a user's capabilities depending on specific context and/or privilege. |
||
| 414 | * |
||
| 415 | * @since 2.8.0 |
||
| 416 | * |
||
| 417 | * @param array $caps Returns the user's actual capabilities. |
||
| 418 | * @param string $cap Capability name. |
||
| 419 | * @param int $user_id The user ID. |
||
| 420 | * @param array $args Adds the context to the cap. Typically the object ID. |
||
| 421 | */ |
||
| 422 | return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args ); |
||
| 423 | } |
||
| 424 | |||
| 738 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.