| Conditions | 38 |
| Paths | > 20000 |
| Total Lines | 302 |
| Lines | 49 |
| Ratio | 16.23 % |
| 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 | $version = wp_kses( $plugin['version'], $plugins_allowedtags ); |
||
| 222 | |||
| 223 | $name = strip_tags( $title . ' ' . $version ); |
||
| 224 | |||
| 225 | // Remove any HTML from the description. |
||
| 226 | $description = strip_tags( $plugin['short_description'] ); |
||
| 227 | |||
| 228 | $wp_version = get_bloginfo( 'version' ); |
||
| 229 | |||
| 230 | $compatible_php = ( empty( $plugin['requires_php'] ) || version_compare( phpversion(), $plugin['requires_php'], '>=' ) ); |
||
| 231 | $tested_wp = ( empty( $plugin['tested'] ) || version_compare( $wp_version, $plugin['tested'], '<=' ) ); |
||
| 232 | $compatible_wp = ( empty( $plugin['requires'] ) || version_compare( $wp_version, $plugin['requires'], '>=' ) ); |
||
| 233 | |||
| 234 | $action_links = array(); |
||
| 235 | |||
| 236 | // install button |
||
| 237 | if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) { |
||
| 238 | $status = install_plugin_install_status( $plugin ); |
||
| 239 | switch ( $status['status'] ) { |
||
| 240 | case 'install': |
||
| 241 | View Code Duplication | if ( $status['url'] ) { |
|
| 242 | if ( $compatible_php && $compatible_wp ) { |
||
| 243 | $action_links[] = sprintf( |
||
| 244 | '<a class="install-now button" data-slug="%s" href="%s" aria-label="%s" data-name="%s">%s</a>', |
||
| 245 | esc_attr( $plugin['slug'] ), |
||
| 246 | esc_url( $status['url'] ), |
||
| 247 | /* translators: %s: plugin name and version */ |
||
| 248 | esc_attr( sprintf( __( 'Install %s now' ), $name ) ), |
||
| 249 | esc_attr( $name ), |
||
| 250 | __( 'Install Now' ) |
||
| 251 | ); |
||
| 252 | } else { |
||
| 253 | $action_links[] = sprintf( |
||
| 254 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
| 255 | _x( 'Cannot Install', 'plugin' ) |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | break; |
||
| 260 | |||
| 261 | case 'update_available': |
||
| 262 | View Code Duplication | if ( $status['url'] ) { |
|
| 263 | $action_links[] = sprintf( |
||
| 264 | '<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>', |
||
| 265 | esc_attr( $status['file'] ), |
||
| 266 | esc_attr( $plugin['slug'] ), |
||
| 267 | esc_url( $status['url'] ), |
||
| 268 | /* translators: %s: plugin name and version */ |
||
| 269 | esc_attr( sprintf( __( 'Update %s now' ), $name ) ), |
||
| 270 | esc_attr( $name ), |
||
| 271 | __( 'Update Now' ) |
||
| 272 | ); |
||
| 273 | } |
||
| 274 | break; |
||
| 275 | |||
| 276 | case 'latest_installed': |
||
| 277 | case 'newer_installed': |
||
| 278 | if ( is_plugin_active( $status['file'] ) ) { |
||
| 279 | $action_links[] = sprintf( |
||
| 280 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
| 281 | _x( 'Active', 'plugin' ) |
||
| 282 | ); |
||
| 283 | } elseif ( current_user_can( 'activate_plugin', $status['file'] ) ) { |
||
| 284 | $button_text = __( 'Activate' ); |
||
| 285 | /* translators: %s: plugin name */ |
||
| 286 | $button_label = _x( 'Activate %s', 'plugin' ); |
||
| 287 | $activate_url = add_query_arg( |
||
| 288 | array( |
||
| 289 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ), |
||
| 290 | 'action' => 'activate', |
||
| 291 | 'plugin' => $status['file'], |
||
| 292 | ), |
||
| 293 | network_admin_url( 'plugins.php' ) |
||
| 294 | ); |
||
| 295 | |||
| 296 | if ( is_network_admin() ) { |
||
| 297 | $button_text = __( 'Network Activate' ); |
||
| 298 | /* translators: %s: plugin name */ |
||
| 299 | $button_label = _x( 'Network Activate %s', 'plugin' ); |
||
| 300 | $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url ); |
||
| 301 | } |
||
| 302 | |||
| 303 | $action_links[] = sprintf( |
||
| 304 | '<a href="%1$s" class="button activate-now" aria-label="%2$s">%3$s</a>', |
||
| 305 | esc_url( $activate_url ), |
||
| 306 | esc_attr( sprintf( $button_label, $plugin['name'] ) ), |
||
| 307 | $button_text |
||
| 308 | ); |
||
| 309 | } else { |
||
| 310 | $action_links[] = sprintf( |
||
| 311 | '<button type="button" class="button button-disabled" disabled="disabled">%s</button>', |
||
| 312 | _x( 'Installed', 'plugin' ) |
||
| 313 | ); |
||
| 314 | } |
||
| 315 | break; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | $details_link = self_admin_url( |
||
| 320 | 'plugin-install.php?tab=plugin-information&plugin=' . $plugin['slug'] . |
||
| 321 | '&TB_iframe=true&width=600&height=550' |
||
| 322 | ); |
||
| 323 | |||
| 324 | if ( ! empty( $plugin['icons']['svg'] ) ) { |
||
| 325 | $plugin_icon_url = $plugin['icons']['svg']; |
||
| 326 | } elseif ( ! empty( $plugin['icons']['2x'] ) ) { |
||
| 327 | $plugin_icon_url = $plugin['icons']['2x']; |
||
| 328 | } elseif ( ! empty( $plugin['icons']['1x'] ) ) { |
||
| 329 | $plugin_icon_url = $plugin['icons']['1x']; |
||
| 330 | } else { |
||
| 331 | $plugin_icon_url = $plugin['icons']['default']; |
||
| 332 | } |
||
| 333 | ?> |
||
| 334 | |||
| 335 | <li class="jetpack-about__plugin plugin-card-<?php echo sanitize_html_class( $plugin['slug'] ); ?>"> |
||
| 336 | <?php |
||
| 337 | if ( ! $compatible_php || ! $compatible_wp ) { |
||
| 338 | echo '<div class="notice inline notice-error notice-alt"><p>'; |
||
| 339 | if ( ! $compatible_php && ! $compatible_wp ) { |
||
| 340 | _e( 'This plugin doesn’t work with your versions of WordPress and PHP.' ); |
||
| 341 | if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) { |
||
| 342 | printf( |
||
| 343 | /* translators: 1: "Update WordPress" screen URL, 2: "Update PHP" page URL */ |
||
| 344 | ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ), |
||
| 345 | self_admin_url( 'update-core.php' ), |
||
| 346 | esc_url( wp_get_update_php_url() ) |
||
| 347 | ); |
||
| 348 | wp_update_php_annotation(); |
||
| 349 | } elseif ( current_user_can( 'update_core' ) ) { |
||
| 350 | printf( |
||
| 351 | /* translators: %s: "Update WordPress" screen URL */ |
||
| 352 | ' ' . __( '<a href="%s">Please update WordPress</a>.' ), |
||
| 353 | self_admin_url( 'update-core.php' ) |
||
| 354 | ); |
||
| 355 | View Code Duplication | } elseif ( current_user_can( 'update_php' ) ) { |
|
| 356 | printf( |
||
| 357 | /* translators: %s: "Update PHP" page URL */ |
||
| 358 | ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ), |
||
| 359 | esc_url( wp_get_update_php_url() ) |
||
| 360 | ); |
||
| 361 | wp_update_php_annotation(); |
||
| 362 | } |
||
| 363 | } elseif ( ! $compatible_wp ) { |
||
| 364 | _e( 'This plugin doesn’t work with your version of WordPress.' ); |
||
| 365 | if ( current_user_can( 'update_core' ) ) { |
||
| 366 | printf( |
||
| 367 | /* translators: %s: "Update WordPress" screen URL */ |
||
| 368 | ' ' . __( '<a href="%s">Please update WordPress</a>.' ), |
||
| 369 | self_admin_url( 'update-core.php' ) |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | View Code Duplication | } elseif ( ! $compatible_php ) { |
|
| 373 | _e( 'This plugin doesn’t work with your version of PHP.' ); |
||
| 374 | if ( current_user_can( 'update_php' ) ) { |
||
| 375 | printf( |
||
| 376 | /* translators: %s: "Update PHP" page URL */ |
||
| 377 | ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ), |
||
| 378 | esc_url( wp_get_update_php_url() ) |
||
| 379 | ); |
||
| 380 | wp_update_php_annotation(); |
||
| 381 | } |
||
| 382 | } |
||
| 383 | echo '</p></div>'; |
||
| 384 | } |
||
| 385 | ?> |
||
| 386 | |||
| 387 | <div class="plugin-card-top"> |
||
| 388 | <div class="name column-name"> |
||
| 389 | <h3> |
||
| 390 | <a href="<?php echo esc_url( $details_link ); ?>" class="thickbox open-plugin-details-modal"> |
||
| 391 | <?php echo $title; ?> |
||
| 392 | <img src="<?php echo esc_attr( $plugin_icon_url ); ?>" class="plugin-icon" alt=""> |
||
| 393 | </a> |
||
| 394 | </h3> |
||
| 395 | </div> |
||
| 396 | <div class="desc column-description"> |
||
| 397 | <p><?php echo $description; ?></p> |
||
| 398 | </div> |
||
| 399 | |||
| 400 | <div class="details-link"> |
||
| 401 | <a class="thickbox open-plugin-details-modal" href="<?php echo $details_link; ?>"><?php _e( 'More Details', 'jetpack' ); ?></a> |
||
| 402 | </div> |
||
| 403 | </div> |
||
| 404 | |||
| 405 | <div class="plugin-card-bottom"> |
||
| 406 | <div class="action-links"> |
||
| 407 | <?php |
||
| 408 | if ( $action_links ) { |
||
| 409 | echo '<ul class="plugin-action-buttons"><li>' . implode( '</li><li>', $action_links ) . '</li></ul>'; |
||
| 410 | } |
||
| 411 | ?> |
||
| 412 | </div> |
||
| 413 | <div class="vers column-rating"> |
||
| 414 | <?php |
||
| 415 | wp_star_rating( |
||
| 416 | array( |
||
| 417 | 'rating' => $plugin['rating'], |
||
| 418 | 'type' => 'percent', |
||
| 419 | 'number' => $plugin['num_ratings'], |
||
| 420 | ) |
||
| 421 | ); |
||
| 422 | ?> |
||
| 423 | <span class="num-ratings" aria-hidden="true">(<?php echo number_format_i18n( $plugin['num_ratings'] ); ?> <?php esc_html_e( 'ratings', 'jetpack' ); ?>)</span> |
||
| 424 | </div> |
||
| 425 | <div class="column-downloaded"> |
||
| 426 | <?php |
||
| 427 | if ( $plugin['active_installs'] >= 1000000 ) { |
||
| 428 | $active_installs_millions = floor( $plugin['active_installs'] / 1000000 ); |
||
| 429 | $active_installs_text = sprintf( |
||
| 430 | _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ), |
||
| 431 | number_format_i18n( $active_installs_millions ) |
||
| 432 | ); |
||
| 433 | } elseif ( 0 == $plugin['active_installs'] ) { |
||
| 434 | $active_installs_text = _x( 'Less Than 10', 'Active plugin installations' ); |
||
| 435 | } else { |
||
| 436 | $active_installs_text = number_format_i18n( $plugin['active_installs'] ) . '+'; |
||
| 437 | } |
||
| 438 | printf( __( '%s Active Installations' ), $active_installs_text ); |
||
| 439 | ?> |
||
| 440 | </div> |
||
| 441 | </div> |
||
| 442 | </li> |
||
| 443 | <?php |
||
| 444 | |||
| 445 | } |
||
| 446 | |||
| 447 | } |
||
| 448 | |||
| 514 |
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.