Conditions | 27 |
Paths | 2320 |
Total Lines | 173 |
Code Lines | 72 |
Lines | 38 |
Ratio | 21.97 % |
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 |
||
84 | function wordpoints_ajax_save_points_hook() { |
||
85 | |||
86 | View Code Duplication | if ( check_ajax_referer( 'save-network-wordpoints-points-hooks', 'savehooks', false ) ) { |
|
87 | |||
88 | if ( ! current_user_can( 'manage_network_wordpoints_points_hooks' ) ) { |
||
89 | wp_die( -1, '', array( 'response' => 403 ) ); |
||
90 | } |
||
91 | |||
92 | // Saving network hooks, turn on network mode. |
||
93 | WordPoints_Points_Hooks::set_network_mode( true ); |
||
94 | |||
95 | } elseif ( check_ajax_referer( 'save-wordpoints-points-hooks', 'savehooks', false ) ) { |
||
96 | |||
97 | if ( ! current_user_can( 'manage_options' ) ) { |
||
98 | wp_die( -1, '', array( 'response' => 403 ) ); |
||
99 | } |
||
100 | |||
101 | } else { |
||
102 | |||
103 | // CSRF attack (or, more probably the user left the browser open too long). |
||
104 | wp_die( -1, '', array( 'response' => 403 ) ); |
||
105 | } |
||
106 | |||
107 | $error = __( 'An error has occurred. Please reload the page and try again.', 'wordpoints' ); |
||
108 | |||
109 | if ( isset( $_POST['points-slug'] ) ) { |
||
110 | |||
111 | // - We are saving the settings for a points type. |
||
112 | |||
113 | // We don't use this part after 2.1.0, but we keep it around for back-compat. |
||
114 | _deprecated_argument( |
||
115 | __FUNCTION__ |
||
116 | , '2.1.0' |
||
117 | , 'Performing CRUD actions on points types using this function is deprecated.' |
||
118 | ); |
||
119 | |||
120 | if ( ! current_user_can( 'manage_wordpoints_points_types' ) ) { |
||
121 | wp_die( -1, '', array( 'response' => 403 ) ); |
||
122 | } |
||
123 | |||
124 | $settings = array(); |
||
125 | |||
126 | View Code Duplication | if ( isset( $_POST['points-name'] ) ) { |
|
127 | $settings['name'] = trim( sanitize_text_field( wp_unslash( $_POST['points-name'] ) ) ); |
||
128 | } |
||
129 | |||
130 | View Code Duplication | if ( isset( $_POST['points-prefix'] ) ) { |
|
131 | $settings['prefix'] = ltrim( sanitize_text_field( wp_unslash( $_POST['points-prefix'] ) ) ); |
||
132 | } |
||
133 | |||
134 | View Code Duplication | if ( isset( $_POST['points-suffix'] ) ) { |
|
135 | $settings['suffix'] = rtrim( sanitize_text_field( wp_unslash( $_POST['points-suffix'] ) ) ); |
||
136 | } |
||
137 | |||
138 | $points_type = sanitize_key( $_POST['points-slug'] ); |
||
139 | |||
140 | $old_settings = wordpoints_get_points_type( $points_type ); |
||
141 | |||
142 | if ( false === $old_settings ) { |
||
143 | wp_die( -1, '', array( 'response' => 200 ) ); |
||
144 | } |
||
145 | |||
146 | if ( is_array( $old_settings ) ) { |
||
147 | $settings = array_merge( $old_settings, $settings ); |
||
148 | } |
||
149 | |||
150 | if ( ! wordpoints_update_points_type( $points_type, $settings ) ) { |
||
151 | |||
152 | // If this fails, show the user a message along with the form. |
||
153 | echo '<p>' . esc_html__( 'An error has occurred. Please try again.', 'wordpoints' ) . '</p>'; |
||
154 | |||
155 | WordPoints_Points_Hooks::points_type_form( $points_type, 'none' ); |
||
156 | } |
||
157 | |||
158 | } else { |
||
159 | |||
160 | // - We are creating/updating/deleting an instance of a hook. |
||
161 | |||
162 | if ( ! isset( $_POST['id_base'], $_POST['hook-id'], $_POST['points_type'], $_POST['hook_number'] ) ) { |
||
163 | wp_die( -1, '', array( 'response' => 400 ) ); |
||
164 | } |
||
165 | |||
166 | $id_base = sanitize_key( $_POST['id_base'] ); |
||
167 | $hook_id = sanitize_key( $_POST['hook-id'] ); |
||
168 | $points_type_id = sanitize_key( $_POST['points_type'] ); |
||
169 | $number = (int) $_POST['hook_number']; |
||
170 | |||
171 | /* |
||
172 | * Normally the hook ID will be in 'hook-id' when we are updating a hook. |
||
173 | * But when we are saving a brand new instance of a hook or updating a newly |
||
174 | * created hook, the ID won't have been set when the form was output, so |
||
175 | * 'hook-id' will be empty, and we'll get the ID from 'multi_number'. |
||
176 | */ |
||
177 | if ( ! $number ) { |
||
178 | |||
179 | // This holds the ID number if the hook is brand new. |
||
180 | if ( ! isset( $_POST['multi_number'] ) || ! wordpoints_posint( $_POST['multi_number'] ) ) { |
||
181 | wp_die( '<p>' . esc_html( $error ) . '</p>', '', array( 'response' => 400 ) ); |
||
182 | } |
||
183 | |||
184 | $number = (int) $_POST['multi_number']; |
||
185 | $hook_id = $id_base . '-' . $number; |
||
186 | } |
||
187 | |||
188 | $hook = WordPoints_Points_Hooks::get_handler( $hook_id ); |
||
189 | |||
190 | $settings = false; |
||
191 | |||
192 | View Code Duplication | if ( isset( $_POST[ 'hook-' . $id_base ] ) && is_array( $_POST[ 'hook-' . $id_base ] ) ) { |
|
193 | $settings = wp_unslash( $_POST[ 'hook-' . $id_base ] ); // WPCS: sanitization OK. |
||
194 | } |
||
195 | |||
196 | $points_types_hooks = WordPoints_Points_Hooks::get_points_types_hooks(); |
||
197 | |||
198 | // Get the hooks for this points type. |
||
199 | $points_type_hooks = ( isset( $points_types_hooks[ $points_type_id ] ) ) ? $points_types_hooks[ $points_type_id ] : array(); |
||
200 | |||
201 | if ( ! empty( $_POST['delete_hook'] ) ) { |
||
202 | |||
203 | // - We are deleting a hook instance. |
||
204 | |||
205 | View Code Duplication | if ( false === $hook ) { |
|
206 | wp_die( '<p>' . esc_html( $error ) . '</p>', '', array( 'response' => 400 ) ); |
||
207 | } |
||
208 | |||
209 | $hook->delete_callback( $number ); |
||
210 | |||
211 | // Remove this instance of the hook, and reset the positions (keys). |
||
212 | $points_types_hooks[ $points_type_id ] = array_diff( $points_type_hooks, array( $hook_id ) ); |
||
213 | |||
214 | WordPoints_Points_Hooks::save_points_types_hooks( $points_types_hooks ); |
||
215 | |||
216 | wp_die( esc_html( "deleted:{$hook_id}" ), '', array( 'response' => 200 ) ); |
||
217 | |||
218 | } elseif ( $settings && false === $hook ) { |
||
219 | |||
220 | // - We are creating a new instance of a hook. |
||
221 | |||
222 | /* |
||
223 | * Get a hook object for this type of hook. We have to do this because |
||
224 | * since the hook is new, it hasn't been assigned an ID yet, so we can't |
||
225 | * just get it from the array of hooks by ID. |
||
226 | */ |
||
227 | $hook = WordPoints_Points_Hooks::get_handler_by_id_base( $id_base ); |
||
228 | |||
229 | $new_instance = reset( $settings ); |
||
230 | |||
231 | // Save the points types-hooks associations. |
||
232 | $points_type_hooks[] = $hook->get_id( $number ); |
||
233 | $points_types_hooks[ $points_type_id ] = $points_type_hooks; |
||
234 | WordPoints_Points_Hooks::save_points_types_hooks( $points_types_hooks ); |
||
235 | |||
236 | } else { |
||
237 | |||
238 | // - We are updating the settings for an instance of a hook. |
||
239 | |||
240 | View Code Duplication | if ( false === $hook ) { |
|
241 | wp_die( '<p>' . esc_html( $error ) . '</p>', '', array( 'response' => 400 ) ); |
||
242 | } |
||
243 | |||
244 | $new_instance = ( ! empty( $settings ) ) ? reset( $settings ) : array(); |
||
245 | |||
246 | } // End if ( deleting ) elseif ( creating ) else { updating }. |
||
247 | |||
248 | $hook->update_callback( wp_unslash( $new_instance ), $number ); |
||
249 | |||
250 | if ( empty( $_POST['add_new'] ) ) { |
||
251 | $hook->form_callback( $number ); |
||
252 | } |
||
253 | |||
254 | } // End if ( saving points type ) else { deleting/creation/updating hook }. |
||
255 | |||
256 | wp_die( '', '', array( 'response' => 200 ) ); |
||
257 | } |
||
260 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.