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