| Conditions | 7 |
| Paths | 12 |
| Total Lines | 68 |
| Code Lines | 35 |
| 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 |
||
| 118 | public function custom_bulk_action() { |
||
| 119 | |||
| 120 | global $typenow; |
||
| 121 | $post_type = $typenow; |
||
| 122 | |||
| 123 | if ( $post_type == $this->bulk_action_post_type ) { |
||
| 124 | |||
| 125 | // Get the action. |
||
| 126 | // Depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc. |
||
| 127 | $wp_list_table = _get_list_table( 'WP_Posts_List_Table' ); |
||
| 128 | $action = $wp_list_table->current_action(); |
||
| 129 | |||
| 130 | // Allow only defined actions. |
||
| 131 | $allowed_actions = array_keys( $this->actions ); |
||
| 132 | if ( ! in_array( $action, $allowed_actions ) ) { |
||
| 133 | return; |
||
| 134 | } |
||
| 135 | |||
| 136 | // Security check. |
||
| 137 | check_admin_referer( 'bulk-posts' ); |
||
| 138 | |||
| 139 | // Make sure ids are submitted. |
||
| 140 | // Depending on the resource type, this may be 'media' or 'ids'. |
||
| 141 | if ( isset( $_REQUEST['post'] ) ) { |
||
| 142 | $post_ids = array_map( 'intval', $_REQUEST['post'] ); |
||
| 143 | } |
||
| 144 | |||
| 145 | if ( empty( $post_ids ) ) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | // This is based on wp-admin/edit.php. |
||
| 150 | $sendback = remove_query_arg( |
||
| 151 | array( 'exported', 'untrashed', 'deleted', 'ids' ), |
||
| 152 | wp_get_referer() |
||
| 153 | ); |
||
| 154 | if ( ! $sendback ) { |
||
| 155 | $sendback = admin_url( "edit.php?post_type=$post_type" ); |
||
| 156 | } |
||
| 157 | |||
| 158 | $pagenum = $wp_list_table->get_pagenum(); |
||
| 159 | $sendback = add_query_arg( 'paged', $pagenum, $sendback ); |
||
| 160 | |||
| 161 | // Check that we have anonymous function as a callback. |
||
| 162 | $anon_fns = array_filter( $this->actions[ $action ], function( $el ) { |
||
| 163 | return $el instanceof \Closure; |
||
| 164 | } ); |
||
| 165 | |||
| 166 | if ( count( $anon_fns ) > 0 ) { |
||
| 167 | $this->actions[ $action ]['callback']( $post_ids ); |
||
| 168 | } else { |
||
| 169 | call_user_func( $this->actions[ $action ]['callback'], $post_ids ); |
||
| 170 | } |
||
| 171 | |||
| 172 | $sendback = add_query_arg( |
||
| 173 | array( 'success_action' => $action, 'ids' => join( ',', $post_ids ) ), |
||
| 174 | $sendback |
||
| 175 | ); |
||
| 176 | $sendback = remove_query_arg( |
||
| 177 | array( 'action', 'paged', 'mode', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view' ), |
||
| 178 | $sendback |
||
| 179 | ); |
||
| 180 | |||
| 181 | wp_redirect( $sendback ); |
||
| 182 | |||
| 183 | exit; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 228 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state