| Conditions | 12 |
| Paths | 18 |
| Total Lines | 45 |
| 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 |
||
| 97 | public function adjust_meta_caps( $caps, $cap, $user_id, $args ) { |
||
| 98 | |||
| 99 | // only run for REST API requests. |
||
| 100 | if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST ) { |
||
| 101 | return $caps; |
||
| 102 | } |
||
| 103 | |||
| 104 | // only modify caps for meta caps and for bbPress meta keys. |
||
| 105 | if ( ! in_array( $cap, array( 'edit_post_meta', 'delete_post_meta', 'add_post_meta' ), true ) || empty( $args[1] ) || false === strpos( $args[1], '_bbp_' ) ) { |
||
| 106 | return $caps; |
||
| 107 | } |
||
| 108 | |||
| 109 | // $args[0] could be a post ID or a post_type string. |
||
| 110 | if ( is_int( $args[0] ) ) { |
||
| 111 | $_post = get_post( $args[0] ); |
||
| 112 | if ( ! empty( $_post ) ) { |
||
| 113 | $post_type = get_post_type_object( $_post->post_type ); |
||
| 114 | } |
||
| 115 | } elseif ( is_string( $args[0] ) ) { |
||
| 116 | $post_type = get_post_type_object( $args[0] ); |
||
| 117 | } |
||
| 118 | |||
| 119 | // no post type found, bail. |
||
| 120 | if ( empty( $post_type ) ) { |
||
| 121 | return $caps; |
||
| 122 | } |
||
| 123 | |||
| 124 | // reset the needed caps. |
||
| 125 | $caps = array(); |
||
| 126 | |||
| 127 | // Add 'do_not_allow' cap if user is spam or deleted. |
||
| 128 | if ( bbp_is_user_inactive( $user_id ) ) { |
||
| 129 | $caps[] = 'do_not_allow'; |
||
| 130 | |||
| 131 | // Moderators can always edit meta. |
||
| 132 | } elseif ( user_can( $user_id, 'moderate' ) ) { |
||
| 133 | $caps[] = 'moderate'; |
||
| 134 | |||
| 135 | // Unknown so map to edit_posts. |
||
| 136 | } else { |
||
| 137 | $caps[] = $post_type->cap->edit_posts; |
||
| 138 | } |
||
| 139 | |||
| 140 | return $caps; |
||
| 141 | } |
||
| 142 | |||
| 144 |