| Conditions | 12 |
| Paths | 18 |
| Total Lines | 42 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 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 |
||
| 63 | function adjust_meta_caps( $caps, $cap, $user_id, $args ) { |
||
| 64 | |||
| 65 | // only run for REST API requests |
||
| 66 | if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) |
||
| 67 | return $caps; |
||
| 68 | |||
| 69 | // only modify caps for meta caps and for bbPress meta keys |
||
| 70 | if ( ! in_array( $cap, array( 'edit_post_meta', 'delete_post_meta', 'add_post_meta' ) ) || empty( $args[1] ) || false === strpos( $args[1], '_bbp_' ) ) |
||
| 71 | return $caps; |
||
| 72 | |||
| 73 | // $args[0] could be a post ID or a post_type string |
||
| 74 | if ( is_int( $args[0] ) ) { |
||
| 75 | $_post = get_post( $args[0] ); |
||
| 76 | if ( ! empty( $_post ) ) { |
||
| 77 | $post_type = get_post_type_object( $_post->post_type ); |
||
| 78 | } |
||
| 79 | } elseif ( is_string( $args[0] ) ) { |
||
| 80 | $post_type = get_post_type_object( $args[0] ); |
||
| 81 | } |
||
| 82 | |||
| 83 | // no post type found, bail |
||
| 84 | if ( empty( $post_type ) ) |
||
| 85 | return $caps; |
||
| 86 | |||
| 87 | // reset the needed caps |
||
| 88 | $caps = array(); |
||
| 89 | |||
| 90 | // Add 'do_not_allow' cap if user is spam or deleted |
||
| 91 | if ( bbp_is_user_inactive( $user_id ) ) { |
||
| 92 | $caps[] = 'do_not_allow'; |
||
| 93 | |||
| 94 | // Moderators can always edit meta |
||
| 95 | } elseif ( user_can( $user_id, 'moderate' ) ) { |
||
| 96 | $caps[] = 'moderate'; |
||
| 97 | |||
| 98 | // Unknown so map to edit_posts |
||
| 99 | } else { |
||
| 100 | $caps[] = $post_type->cap->edit_posts; |
||
| 101 | } |
||
| 102 | |||
| 103 | return $caps; |
||
| 104 | } |
||
| 105 | |||
| 109 |