| Conditions | 38 |
| Paths | > 20000 |
| Total Lines | 306 |
| Lines | 51 |
| Ratio | 16.67 % |
| 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 |
||
| 186 | function display_plugins() { |
||
| 187 | $plugins_allowedtags = array( |
||
| 188 | 'a' => array( |
||
| 189 | 'href' => array(), |
||
| 190 | 'title' => array(), |
||
| 191 | 'target' => array(), |
||
| 192 | ), |
||
| 193 | 'abbr' => array( 'title' => array() ), |
||
| 194 | 'acronym' => array( 'title' => array() ), |
||
| 195 | 'code' => array(), |
||
| 196 | 'pre' => array(), |
||
| 197 | 'em' => array(), |
||
| 198 | 'strong' => array(), |
||
| 199 | 'ul' => array(), |
||
| 200 | 'ol' => array(), |
||
| 201 | 'li' => array(), |
||
| 202 | 'p' => array(), |
||
| 203 | 'br' => array(), |
||
| 204 | ); |
||
| 205 | |||
| 206 | // slugs for plugins we want to display |
||
| 207 | $a8c_plugins = array( |
||
| 208 | 'akismet', |
||
| 209 | 'wp-super-cache', |
||
| 210 | 'vaultpress', |
||
| 211 | 'polldaddy', |
||
| 212 | ); |
||
| 213 | |||
| 214 | // need this to access the plugins_api() function |
||
| 215 | include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
||
| 216 | |||
| 217 | foreach ( $a8c_plugins as $slug ){ |
||
| 218 | $args = array( |
||
| 219 | 'slug' => $slug, |
||
| 220 | 'fields' => array( |
||
| 221 | 'added' => false, |
||
| 222 | 'author' => false, |
||
| 223 | 'author_profile' => false, |
||
| 224 | 'banners' => false, |
||
| 225 | 'contributors' => false, |
||
| 226 | 'donate_link' => false, |
||
| 227 | 'homepage' => false, |
||
| 228 | 'reviews' => false, |
||
| 229 | 'screenshots' => false, |
||
| 230 | 'support_threads' => false, |
||
| 231 | 'support_threads_resolved' => false, |
||
| 232 | 'sections' => false, |
||
| 233 | 'tags' => false, |
||
| 234 | 'versions' => false, |
||
| 235 | |||
| 236 | 'compatibility' => true, |
||
| 237 | 'downloaded' => true, |
||
| 238 | 'downloadlink' => true, |
||
| 239 | 'icons' => true, |
||
| 240 | 'last_updated' => true, |
||
| 241 | 'num_ratings' => true, |
||
| 242 | 'rating' => true, |
||
| 243 | 'requires' => true, |
||
| 244 | 'requires_php' => true, |
||
| 245 | 'short_description' => true, |
||
| 246 | 'tested' => true, |
||
| 247 | ), |
||
| 248 | ); |
||
| 249 | |||
| 250 | // should probably add some error checking here too |
||
| 251 | $api = plugins_api( 'plugin_information', $args ); |
||
| 252 | $plugins[] = $api; |
||
|
|
|||
| 253 | } |
||
| 254 | |||
| 255 | foreach ( $plugins as $plugin ) { |
||
| 256 | if ( is_object( $plugin ) ) { |
||
| 257 | $plugin = (array) $plugin; |
||
| 258 | } |
||
| 259 | |||
| 260 | $title = wp_kses( $plugin['name'], $plugins_allowedtags ); |
||
| 261 | $version = wp_kses( $plugin['version'], $plugins_allowedtags ); |
||
| 262 | |||
| 263 | $name = strip_tags( $title . ' ' . $version ); |
||
| 264 | |||
| 265 | // Remove any HTML from the description. |
||
| 266 | $description = strip_tags( $plugin['short_description'] ); |
||
| 267 | |||
| 268 | $wp_version = get_bloginfo( 'version' ); |
||
| 269 | |||
| 270 | $compatible_php = ( empty( $plugin['requires_php'] ) || version_compare( phpversion(), $plugin['requires_php'], '>=' ) ); |
||
| 271 | $tested_wp = ( empty( $plugin['tested'] ) || version_compare( $wp_version, $plugin['tested'], '<=' ) ); |
||
| 272 | $compatible_wp = ( empty( $plugin['requires'] ) || version_compare( $wp_version, $plugin['requires'], '>=' ) ); |
||
| 273 | |||
| 274 | $action_links = array(); |
||
| 275 | |||
| 276 | // install button |
||
| 277 | if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) { |
||
| 278 | $status = install_plugin_install_status( $plugin ); |
||
| 279 | switch ( $status['status'] ) { |
||
| 280 | case 'install': |
||
| 281 | View Code Duplication | if ( $status['url'] ) { |
|
| 282 | if ( $compatible_php && $compatible_wp ) { |
||
| 283 | $action_links[] = sprintf( |
||
| 284 | '<a class="install-now button jptracks" data-slug="%s" href="%s" aria-label="%s" data-name="%s" data-jptracks-name="jetpack_about_install_button" data-jptracks-prop="%s">%s</a>', |
||
| 285 | esc_attr( $plugin['slug'] ), |
||
| 286 | esc_url( $status['url'] ), |
||
| 287 | /* translators: %s: plugin name and version */ |
||
| 288 | esc_attr( sprintf( __( 'Install %s now' ), $name ) ), |
||
| 289 | esc_attr( $name ), |
||
| 290 | esc_attr( $name ), |
||
| 291 | __( 'Install Now' ) |
||
| 292 | ); |
||
| 293 | } else { |
||
| 294 | $action_links[] = sprintf( |
||
| 295 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
| 296 | _x( 'Cannot Install', 'plugin' ) |
||
| 297 | ); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | break; |
||
| 301 | |||
| 302 | case 'update_available': |
||
| 303 | View Code Duplication | if ( $status['url'] ) { |
|
| 304 | $action_links[] = sprintf( |
||
| 305 | '<a class="update-now button aria-button-if-js jptracks" data-plugin="%s" data-slug="%s" href="%s" aria-label="%s" data-name="%s" data-jptracks-name="jetpack_about_update_button" data-jptracks-prop="%s">%s</a>', |
||
| 306 | esc_attr( $status['file'] ), |
||
| 307 | esc_attr( $plugin['slug'] ), |
||
| 308 | esc_url( $status['url'] ), |
||
| 309 | /* translators: %s: plugin name and version */ |
||
| 310 | esc_attr( sprintf( __( 'Update %s now' ), $name ) ), |
||
| 311 | esc_attr( $name ), |
||
| 312 | esc_attr( $name ), |
||
| 313 | __( 'Update Now' ) |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | break; |
||
| 317 | |||
| 318 | case 'latest_installed': |
||
| 319 | case 'newer_installed': |
||
| 320 | if ( is_plugin_active( $status['file'] ) ) { |
||
| 321 | $action_links[] = sprintf( |
||
| 322 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
| 323 | _x( 'Active', 'plugin' ) |
||
| 324 | ); |
||
| 325 | } elseif ( current_user_can( 'activate_plugin', $status['file'] ) ) { |
||
| 326 | $button_text = __( 'Activate' ); |
||
| 327 | /* translators: %s: plugin name */ |
||
| 328 | $button_label = _x( 'Activate %s', 'plugin' ); |
||
| 329 | $activate_url = add_query_arg( |
||
| 330 | array( |
||
| 331 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ), |
||
| 332 | 'action' => 'activate', |
||
| 333 | 'plugin' => $status['file'], |
||
| 334 | ), |
||
| 335 | network_admin_url( 'plugins.php' ) |
||
| 336 | ); |
||
| 337 | |||
| 338 | if ( is_network_admin() ) { |
||
| 339 | $button_text = __( 'Network Activate' ); |
||
| 340 | /* translators: %s: plugin name */ |
||
| 341 | $button_label = _x( 'Network Activate %s', 'plugin' ); |
||
| 342 | $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url ); |
||
| 343 | } |
||
| 344 | |||
| 345 | $action_links[] = sprintf( |
||
| 346 | '<a href="%1$s" class="button activate-now" aria-label="%2$s" data-jptracks-name="jetpack_about_activate_button" data-jptracks-prop="%3$s">%4$s</a>', |
||
| 347 | esc_url( $activate_url ), |
||
| 348 | esc_attr( sprintf( $button_label, $plugin['name'] ) ), |
||
| 349 | esc_attr( $plugin['name'] ), |
||
| 350 | $button_text |
||
| 351 | ); |
||
| 352 | } else { |
||
| 353 | $action_links[] = sprintf( |
||
| 354 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
| 355 | _x( 'Installed', 'plugin' ) |
||
| 356 | ); |
||
| 357 | } |
||
| 358 | break; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | $details_link = self_admin_url( |
||
| 363 | 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] . |
||
| 364 | '&TB_iframe=true&width=600&height=550' |
||
| 365 | ); |
||
| 366 | |||
| 367 | if ( ! empty( $plugin['icons']['svg'] ) ) { |
||
| 368 | $plugin_icon_url = $plugin['icons']['svg']; |
||
| 369 | } elseif ( ! empty( $plugin['icons']['2x'] ) ) { |
||
| 370 | $plugin_icon_url = $plugin['icons']['2x']; |
||
| 371 | } elseif ( ! empty( $plugin['icons']['1x'] ) ) { |
||
| 372 | $plugin_icon_url = $plugin['icons']['1x']; |
||
| 373 | } else { |
||
| 374 | $plugin_icon_url = $plugin['icons']['default']; |
||
| 375 | } |
||
| 376 | ?> |
||
| 377 | |||
| 378 | <li class="jetpack-about__plugin plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>"> |
||
| 379 | <?php |
||
| 380 | if ( ! $compatible_php || ! $compatible_wp ) { |
||
| 381 | echo '<div class="notice inline notice-error notice-alt"><p>'; |
||
| 382 | if ( ! $compatible_php && ! $compatible_wp ) { |
||
| 383 | _e( 'This plugin doesn’t work with your versions of WordPress and PHP.' ); |
||
| 384 | if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) { |
||
| 385 | printf( |
||
| 386 | /* translators: 1: "Update WordPress" screen URL, 2: "Update PHP" page URL */ |
||
| 387 | ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ), |
||
| 388 | self_admin_url( 'update-core.php' ), |
||
| 389 | esc_url( wp_get_update_php_url() ) |
||
| 390 | ); |
||
| 391 | wp_update_php_annotation(); |
||
| 392 | } elseif ( current_user_can( 'update_core' ) ) { |
||
| 393 | printf( |
||
| 394 | /* translators: %s: "Update WordPress" screen URL */ |
||
| 395 | ' ' . __( '<a href="%s">Please update WordPress</a>.' ), |
||
| 396 | self_admin_url( 'update-core.php' ) |
||
| 397 | ); |
||
| 398 | View Code Duplication | } elseif ( current_user_can( 'update_php' ) ) { |
|
| 399 | printf( |
||
| 400 | /* translators: %s: "Update PHP" page URL */ |
||
| 401 | ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ), |
||
| 402 | esc_url( wp_get_update_php_url() ) |
||
| 403 | ); |
||
| 404 | wp_update_php_annotation(); |
||
| 405 | } |
||
| 406 | } elseif ( ! $compatible_wp ) { |
||
| 407 | _e( 'This plugin doesn’t work with your version of WordPress.' ); |
||
| 408 | if ( current_user_can( 'update_core' ) ) { |
||
| 409 | printf( |
||
| 410 | /* translators: %s: "Update WordPress" screen URL */ |
||
| 411 | ' ' . __( '<a href="%s">Please update WordPress</a>.' ), |
||
| 412 | self_admin_url( 'update-core.php' ) |
||
| 413 | ); |
||
| 414 | } |
||
| 415 | View Code Duplication | } elseif ( ! $compatible_php ) { |
|
| 416 | _e( 'This plugin doesn’t work with your version of PHP.' ); |
||
| 417 | if ( current_user_can( 'update_php' ) ) { |
||
| 418 | printf( |
||
| 419 | /* translators: %s: "Update PHP" page URL */ |
||
| 420 | ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ), |
||
| 421 | esc_url( wp_get_update_php_url() ) |
||
| 422 | ); |
||
| 423 | wp_update_php_annotation(); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | echo '</p></div>'; |
||
| 427 | } |
||
| 428 | ?> |
||
| 429 | |||
| 430 | <div class="plugin-card-top"> |
||
| 431 | <div class="name column-name"> |
||
| 432 | <h3> |
||
| 433 | <a href="<?php echo esc_url( $details_link ); ?>" class="jptracks thickbox open-plugin-details-modal" data-jptracks-name="jetpack_about_plugin_modal" data-jptracks-prop="<?php echo esc_attr( $plugin['slug'] ); ?>"> |
||
| 434 | <?php echo $title; ?> |
||
| 435 | <img src="<?php echo esc_attr( $plugin_icon_url ); ?>" class="plugin-icon" alt=""> |
||
| 436 | </a> |
||
| 437 | </h3> |
||
| 438 | </div> |
||
| 439 | <div class="desc column-description"> |
||
| 440 | <p><?php echo $description; ?></p> |
||
| 441 | </div> |
||
| 442 | |||
| 443 | <div class="details-link"> |
||
| 444 | <a class="jptracks thickbox open-plugin-details-modal" href="<?php echo $details_link; ?>" data-jptracks-name="jetpack_about_plugin_details_modal" data-jptracks-prop="<?php echo esc_attr( $plugin['slug'] ); ?>"><?php _e( 'More Details', 'jetpack' ); ?></a> |
||
| 445 | </div> |
||
| 446 | </div> |
||
| 447 | |||
| 448 | <div class="plugin-card-bottom"> |
||
| 449 | <div class="meta"> |
||
| 450 | <?php |
||
| 451 | wp_star_rating( |
||
| 452 | array( |
||
| 453 | 'rating' => $plugin['rating'], |
||
| 454 | 'type' => 'percent', |
||
| 455 | 'number' => $plugin['num_ratings'], |
||
| 456 | ) |
||
| 457 | ); |
||
| 458 | ?> |
||
| 459 | <span class="num-ratings" aria-hidden="true">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?> <?php esc_html_e( 'ratings', 'jetpack' ); ?>)</span> |
||
| 460 | <div class="downloaded"> |
||
| 461 | <?php |
||
| 462 | if ( $plugin['active_installs'] >= 1000000 ) { |
||
| 463 | $active_installs_millions = floor( $plugin['active_installs'] / 1000000 ); |
||
| 464 | $active_installs_text = sprintf( |
||
| 465 | _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ), |
||
| 466 | number_format_i18n( $active_installs_millions ) |
||
| 467 | ); |
||
| 468 | } elseif ( 0 == $plugin['active_installs'] ) { |
||
| 469 | $active_installs_text = _x( 'Less Than 10', 'Active plugin installations' ); |
||
| 470 | } else { |
||
| 471 | $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+'; |
||
| 472 | } |
||
| 473 | printf( __( '%s Active Installations' ), $active_installs_text ); |
||
| 474 | ?> |
||
| 475 | </div> |
||
| 476 | </div> |
||
| 477 | |||
| 478 | <div class="action-links"> |
||
| 479 | <?php |
||
| 480 | if ( $action_links ) { |
||
| 481 | echo '<ul class="action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>'; |
||
| 482 | } |
||
| 483 | ?> |
||
| 484 | </div> |
||
| 485 | </div> |
||
| 486 | </li> |
||
| 487 | <?php |
||
| 488 | |||
| 489 | } |
||
| 490 | |||
| 491 | } |
||
| 492 | |||
| 558 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.