Conditions | 38 |
Paths | > 20000 |
Total Lines | 299 |
Lines | 49 |
Ratio | 16.39 % |
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 |
||
146 | function display_plugins(){ |
||
147 | $plugins_allowedtags = array( |
||
148 | 'a' => array( |
||
149 | 'href' => array(), |
||
150 | 'title' => array(), |
||
151 | 'target' => array(), |
||
152 | ), |
||
153 | 'abbr' => array( 'title' => array() ), |
||
154 | 'acronym' => array( 'title' => array() ), |
||
155 | 'code' => array(), |
||
156 | 'pre' => array(), |
||
157 | 'em' => array(), |
||
158 | 'strong' => array(), |
||
159 | 'ul' => array(), |
||
160 | 'ol' => array(), |
||
161 | 'li' => array(), |
||
162 | 'p' => array(), |
||
163 | 'br' => array(), |
||
164 | ); |
||
165 | |||
166 | // slugs for plugins we want to display |
||
167 | $a8c_plugins = array( |
||
168 | 'akismet', |
||
169 | 'wp-super-cache', |
||
170 | 'vaultpress', |
||
171 | 'polldaddy', |
||
172 | ); |
||
173 | |||
174 | // need this to access the plugins_api() function |
||
175 | include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
||
176 | |||
177 | foreach ( $a8c_plugins as $slug ){ |
||
178 | $args = array( |
||
179 | 'slug' => $slug, |
||
180 | 'fields' => array( |
||
181 | 'added' => false, |
||
182 | 'author' => false, |
||
183 | 'author_profile' => false, |
||
184 | 'banners' => false, |
||
185 | 'contributors' => false, |
||
186 | 'donate_link' => false, |
||
187 | 'homepage' => false, |
||
188 | 'reviews' => false, |
||
189 | 'screenshots' => false, |
||
190 | 'support_threads' => false, |
||
191 | 'support_threads_resolved' => false, |
||
192 | 'sections' => false, |
||
193 | 'tags' => false, |
||
194 | 'versions' => false, |
||
195 | |||
196 | 'compatibility' => true, |
||
197 | 'downloaded' => true, |
||
198 | 'downloadlink' => true, |
||
199 | 'icons' => true, |
||
200 | 'last_updated' => true, |
||
201 | 'num_ratings' => true, |
||
202 | 'rating' => true, |
||
203 | 'requires' => true, |
||
204 | 'requires_php' => true, |
||
205 | 'short_description' => true, |
||
206 | 'tested' => true, |
||
207 | ), |
||
208 | ); |
||
209 | |||
210 | // should probably add some error checking here too |
||
211 | $api = plugins_api( 'plugin_information', $args ); |
||
212 | $plugins[] = $api; |
||
|
|||
213 | } |
||
214 | |||
215 | foreach ( $plugins as $plugin ) { |
||
216 | if ( is_object( $plugin ) ) { |
||
217 | $plugin = (array) $plugin; |
||
218 | } |
||
219 | |||
220 | $title = wp_kses( $plugin['name'], $plugins_allowedtags ); |
||
221 | |||
222 | // Remove any HTML from the description. |
||
223 | $description = strip_tags( $plugin['short_description'] ); |
||
224 | |||
225 | $wp_version = get_bloginfo( 'version' ); |
||
226 | |||
227 | $compatible_php = ( empty( $plugin['requires_php'] ) || version_compare( phpversion(), $plugin['requires_php'], '>=' ) ); |
||
228 | $tested_wp = ( empty( $plugin['tested'] ) || version_compare( $wp_version, $plugin['tested'], '<=' ) ); |
||
229 | $compatible_wp = ( empty( $plugin['requires'] ) || version_compare( $wp_version, $plugin['requires'], '>=' ) ); |
||
230 | |||
231 | $action_links = array(); |
||
232 | |||
233 | // install button |
||
234 | if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) { |
||
235 | $status = install_plugin_install_status( $plugin ); |
||
236 | switch ( $status['status'] ) { |
||
237 | case 'install': |
||
238 | View Code Duplication | if ( $status['url'] ) { |
|
239 | if ( $compatible_php && $compatible_wp ) { |
||
240 | $action_links[] = sprintf( |
||
241 | '<a class="install-now button" data-slug="%s" href="%s" aria-label="%s" data-name="%s">%s</a>', |
||
242 | esc_attr( $plugin['slug'] ), |
||
243 | esc_url( $status['url'] ), |
||
244 | /* translators: %s: plugin name and version */ |
||
245 | esc_attr( sprintf( __( 'Install %s now' ), $name ) ), |
||
246 | esc_attr( $name ), |
||
247 | __( 'Install Now' ) |
||
248 | ); |
||
249 | } else { |
||
250 | $action_links[] = sprintf( |
||
251 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
252 | _x( 'Cannot Install', 'plugin' ) |
||
253 | ); |
||
254 | } |
||
255 | } |
||
256 | break; |
||
257 | |||
258 | case 'update_available': |
||
259 | View Code Duplication | if ( $status['url'] ) { |
|
260 | $action_links[] = sprintf( |
||
261 | '<a class="update-now button aria-button-if-js" data-plugin="%s" data-slug="%s" href="%s" aria-label="%s" data-name="%s">%s</a>', |
||
262 | esc_attr( $status['file'] ), |
||
263 | esc_attr( $plugin['slug'] ), |
||
264 | esc_url( $status['url'] ), |
||
265 | /* translators: %s: plugin name and version */ |
||
266 | esc_attr( sprintf( __( 'Update %s now' ), $name ) ), |
||
267 | esc_attr( $name ), |
||
268 | __( 'Update Now' ) |
||
269 | ); |
||
270 | } |
||
271 | break; |
||
272 | |||
273 | case 'latest_installed': |
||
274 | case 'newer_installed': |
||
275 | if ( is_plugin_active( $status['file'] ) ) { |
||
276 | $action_links[] = sprintf( |
||
277 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
278 | _x( 'Active', 'plugin' ) |
||
279 | ); |
||
280 | } elseif ( current_user_can( 'activate_plugin', $status['file'] ) ) { |
||
281 | $button_text = __( 'Activate' ); |
||
282 | /* translators: %s: plugin name */ |
||
283 | $button_label = _x( 'Activate %s', 'plugin' ); |
||
284 | $activate_url = add_query_arg( |
||
285 | array( |
||
286 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ), |
||
287 | 'action' => 'activate', |
||
288 | 'plugin' => $status['file'], |
||
289 | ), |
||
290 | network_admin_url( 'plugins.php' ) |
||
291 | ); |
||
292 | |||
293 | if ( is_network_admin() ) { |
||
294 | $button_text = __( 'Network Activate' ); |
||
295 | /* translators: %s: plugin name */ |
||
296 | $button_label = _x( 'Network Activate %s', 'plugin' ); |
||
297 | $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url ); |
||
298 | } |
||
299 | |||
300 | $action_links[] = sprintf( |
||
301 | '<a href="%1$s" class="button activate-now" aria-label="%2$s">%3$s</a>', |
||
302 | esc_url( $activate_url ), |
||
303 | esc_attr( sprintf( $button_label, $plugin['name'] ) ), |
||
304 | $button_text |
||
305 | ); |
||
306 | } else { |
||
307 | $action_links[] = sprintf( |
||
308 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
309 | _x( 'Installed', 'plugin' ) |
||
310 | ); |
||
311 | } |
||
312 | break; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | $details_link = self_admin_url( |
||
317 | 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] . |
||
318 | '&TB_iframe=true&width=600&height=550' |
||
319 | ); |
||
320 | |||
321 | if ( ! empty( $plugin['icons']['svg'] ) ) { |
||
322 | $plugin_icon_url = $plugin['icons']['svg']; |
||
323 | } elseif ( ! empty( $plugin['icons']['2x'] ) ) { |
||
324 | $plugin_icon_url = $plugin['icons']['2x']; |
||
325 | } elseif ( ! empty( $plugin['icons']['1x'] ) ) { |
||
326 | $plugin_icon_url = $plugin['icons']['1x']; |
||
327 | } else { |
||
328 | $plugin_icon_url = $plugin['icons']['default']; |
||
329 | } |
||
330 | ?> |
||
331 | |||
332 | <li class="jetpack-about__plugin plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>"> |
||
333 | <?php |
||
334 | if ( ! $compatible_php || ! $compatible_wp ) { |
||
335 | echo '<div class="notice inline notice-error notice-alt"><p>'; |
||
336 | if ( ! $compatible_php && ! $compatible_wp ) { |
||
337 | _e( 'This plugin doesn’t work with your versions of WordPress and PHP.' ); |
||
338 | if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) { |
||
339 | printf( |
||
340 | /* translators: 1: "Update WordPress" screen URL, 2: "Update PHP" page URL */ |
||
341 | ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ), |
||
342 | self_admin_url( 'update-core.php' ), |
||
343 | esc_url( wp_get_update_php_url() ) |
||
344 | ); |
||
345 | wp_update_php_annotation(); |
||
346 | } elseif ( current_user_can( 'update_core' ) ) { |
||
347 | printf( |
||
348 | /* translators: %s: "Update WordPress" screen URL */ |
||
349 | ' ' . __( '<a href="%s">Please update WordPress</a>.' ), |
||
350 | self_admin_url( 'update-core.php' ) |
||
351 | ); |
||
352 | View Code Duplication | } elseif ( current_user_can( 'update_php' ) ) { |
|
353 | printf( |
||
354 | /* translators: %s: "Update PHP" page URL */ |
||
355 | ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ), |
||
356 | esc_url( wp_get_update_php_url() ) |
||
357 | ); |
||
358 | wp_update_php_annotation(); |
||
359 | } |
||
360 | } elseif ( ! $compatible_wp ) { |
||
361 | _e( 'This plugin doesn’t work with your version of WordPress.' ); |
||
362 | if ( current_user_can( 'update_core' ) ) { |
||
363 | printf( |
||
364 | /* translators: %s: "Update WordPress" screen URL */ |
||
365 | ' ' . __( '<a href="%s">Please update WordPress</a>.' ), |
||
366 | self_admin_url( 'update-core.php' ) |
||
367 | ); |
||
368 | } |
||
369 | View Code Duplication | } elseif ( ! $compatible_php ) { |
|
370 | _e( 'This plugin doesn’t work with your version of PHP.' ); |
||
371 | if ( current_user_can( 'update_php' ) ) { |
||
372 | printf( |
||
373 | /* translators: %s: "Update PHP" page URL */ |
||
374 | ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ), |
||
375 | esc_url( wp_get_update_php_url() ) |
||
376 | ); |
||
377 | wp_update_php_annotation(); |
||
378 | } |
||
379 | } |
||
380 | echo '</p></div>'; |
||
381 | } |
||
382 | ?> |
||
383 | |||
384 | <div class="plugin-card-top"> |
||
385 | <div class="name column-name"> |
||
386 | <h3> |
||
387 | <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox open-plugin-details-modal"> |
||
388 | <?php echo $title; ?> |
||
389 | <img src="<?php echo esc_attr( $plugin_icon_url ); ?>" class="plugin-icon" alt=""> |
||
390 | </a> |
||
391 | </h3> |
||
392 | </div> |
||
393 | <div class="desc column-description"> |
||
394 | <p><?php echo $description; ?></p> |
||
395 | </div> |
||
396 | |||
397 | <div class="details-link"> |
||
398 | <a class="thickbox open-plugin-details-modal" href="<?php echo $details_link; ?>"><?php _e( 'More Details', 'jetpack' ); ?></a> |
||
399 | </div> |
||
400 | </div> |
||
401 | |||
402 | <div class="plugin-card-bottom"> |
||
403 | <div class="action-links"> |
||
404 | <?php |
||
405 | if ( $action_links ) { |
||
406 | echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>'; |
||
407 | } |
||
408 | ?> |
||
409 | </div> |
||
410 | <div class="vers column-rating"> |
||
411 | <?php |
||
412 | wp_star_rating( |
||
413 | array( |
||
414 | 'rating' => $plugin['rating'], |
||
415 | 'type' => 'percent', |
||
416 | 'number' => $plugin['num_ratings'], |
||
417 | ) |
||
418 | ); |
||
419 | ?> |
||
420 | <span class="num-ratings" aria-hidden="true">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?> <?php esc_html_e( 'ratings', 'jetpack' ); ?>)</span> |
||
421 | </div> |
||
422 | <div class="column-downloaded"> |
||
423 | <?php |
||
424 | if ( $plugin['active_installs'] >= 1000000 ) { |
||
425 | $active_installs_millions = floor( $plugin['active_installs'] / 1000000 ); |
||
426 | $active_installs_text = sprintf( |
||
427 | _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ), |
||
428 | number_format_i18n( $active_installs_millions ) |
||
429 | ); |
||
430 | } elseif ( 0 == $plugin['active_installs'] ) { |
||
431 | $active_installs_text = _x( 'Less Than 10', 'Active plugin installations' ); |
||
432 | } else { |
||
433 | $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+'; |
||
434 | } |
||
435 | printf( __( '%s Active Installations' ), $active_installs_text ); |
||
436 | ?> |
||
437 | </div> |
||
438 | </div> |
||
439 | </li> |
||
440 | <?php |
||
441 | |||
442 | } |
||
443 | |||
444 | } |
||
445 | |||
447 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.