| Conditions | 33 |
| Paths | > 20000 |
| Total Lines | 244 |
| Code Lines | 127 |
| Lines | 11 |
| Ratio | 4.51 % |
| 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 |
||
| 209 | function widget( $args, $instance ) { |
||
| 210 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
| 211 | do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' ); |
||
| 212 | |||
| 213 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
| 214 | |||
| 215 | $title = isset( $instance['title' ] ) ? $instance['title'] : false; |
||
| 216 | if ( false === $title ) { |
||
| 217 | $title = $this->default_title; |
||
| 218 | } |
||
| 219 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
| 220 | $title = apply_filters( 'widget_title', $title ); |
||
| 221 | |||
| 222 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
||
| 223 | if ( $count < 1 || 10 < $count ) { |
||
| 224 | $count = 10; |
||
| 225 | } |
||
| 226 | /** |
||
| 227 | * Control the number of displayed posts. |
||
| 228 | * |
||
| 229 | * @module widgets |
||
| 230 | * |
||
| 231 | * @since 3.3.0 |
||
| 232 | * |
||
| 233 | * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. |
||
| 234 | */ |
||
| 235 | $count = apply_filters( 'jetpack_top_posts_widget_count', $count ); |
||
| 236 | |||
| 237 | $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
||
| 238 | |||
| 239 | // 'likes' are not available in Jetpack |
||
| 240 | $ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views'; |
||
| 241 | |||
| 242 | View Code Duplication | if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
|
| 243 | $display = $instance['display']; |
||
| 244 | } else { |
||
| 245 | $display = 'text'; |
||
| 246 | } |
||
| 247 | |||
| 248 | if ( 'text' != $display ) { |
||
| 249 | $get_image_options = array( |
||
| 250 | 'fallback_to_avatars' => true, |
||
| 251 | /** This filter is documented in modules/stats.php */ |
||
| 252 | 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'https://en.wordpress.com/i/logo/white-gray-80.png' ) ), |
||
| 253 | 'avatar_size' => 40, |
||
| 254 | 'width' => null, |
||
| 255 | 'height' => null, |
||
| 256 | ); |
||
| 257 | if ( 'grid' == $display ) { |
||
| 258 | $get_image_options['avatar_size'] = 200; |
||
| 259 | } |
||
| 260 | /** |
||
| 261 | * Top Posts Widget Image options. |
||
| 262 | * |
||
| 263 | * @module widgets |
||
| 264 | * |
||
| 265 | * @since 1.8.0 |
||
| 266 | * |
||
| 267 | * @param array $get_image_options { |
||
| 268 | * Array of Image options. |
||
| 269 | * @type bool true Should we default to Gravatars when no image is found? Default is true. |
||
| 270 | * @type string $gravatar_default Default Image URL if no Gravatar is found. |
||
| 271 | * @type int $avatar_size Default Image size. |
||
| 272 | * @type mixed $width Image width, not set by default and $avatar_size is used instead. |
||
| 273 | * @type mixed $height Image height, not set by default and $avatar_size is used instead. |
||
| 274 | * } |
||
| 275 | */ |
||
| 276 | $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); |
||
| 277 | } |
||
| 278 | |||
| 279 | if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) { |
||
| 280 | $posts = $this->get_by_likes( $count ); |
||
| 281 | } else { |
||
| 282 | $posts = $this->get_by_views( $count, $args ); |
||
| 283 | } |
||
| 284 | |||
| 285 | // Filter the returned posts. Remove all posts that do not match the chosen Post Types. |
||
| 286 | if ( isset( $types ) ) { |
||
| 287 | foreach ( $posts as $k => $post ) { |
||
| 288 | if ( ! in_array( $post['post_type'], $types ) ) { |
||
| 289 | unset( $posts[$k] ); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | if ( ! $posts ) { |
||
| 295 | $posts = $this->get_fallback_posts(); |
||
| 296 | } |
||
| 297 | |||
| 298 | echo $args['before_widget']; |
||
| 299 | if ( ! empty( $title ) ) |
||
| 300 | echo $args['before_title'] . $title . $args['after_title']; |
||
| 301 | |||
| 302 | if ( ! $posts ) { |
||
| 303 | View Code Duplication | if ( current_user_can( 'edit_theme_options' ) ) { |
|
| 304 | echo '<p>' . sprintf( |
||
| 305 | __( 'There are no posts to display. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ), |
||
| 306 | 'https://jetpack.com/support/getting-more-views-and-traffic/' |
||
| 307 | ) . '</p>'; |
||
| 308 | } |
||
| 309 | |||
| 310 | echo $args['after_widget']; |
||
| 311 | return; |
||
| 312 | } |
||
| 313 | |||
| 314 | switch ( $display ) { |
||
| 315 | case 'list' : |
||
| 316 | case 'grid' : |
||
| 317 | // Keep the avatar_size as default dimensions for backward compatibility. |
||
| 318 | $width = (int) $get_image_options['avatar_size']; |
||
|
|
|||
| 319 | $height = (int) $get_image_options['avatar_size']; |
||
| 320 | |||
| 321 | // Check if the user has changed the width. |
||
| 322 | if ( ! empty( $get_image_options['width'] ) ) { |
||
| 323 | $width = (int) $get_image_options['width']; |
||
| 324 | } |
||
| 325 | |||
| 326 | // Check if the user has changed the height. |
||
| 327 | if ( ! empty( $get_image_options['height'] ) ) { |
||
| 328 | $height = (int) $get_image_options['height']; |
||
| 329 | } |
||
| 330 | |||
| 331 | foreach ( $posts as &$post ) { |
||
| 332 | $image = Jetpack_PostImages::get_image( |
||
| 333 | $post['post_id'], |
||
| 334 | array( |
||
| 335 | 'fallback_to_avatars' => true, |
||
| 336 | 'avatar_size' => (int) $get_image_options['avatar_size'], |
||
| 337 | ) |
||
| 338 | ); |
||
| 339 | $post['image'] = $image['src']; |
||
| 340 | if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { |
||
| 341 | $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) ); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | unset( $post ); |
||
| 346 | |||
| 347 | if ( 'grid' == $display ) { |
||
| 348 | echo "<div class='widgets-grid-layout no-grav'>\n"; |
||
| 349 | foreach ( $posts as $post ) : |
||
| 350 | ?> |
||
| 351 | <div class="widget-grid-view-image"> |
||
| 352 | <?php |
||
| 353 | /** |
||
| 354 | * Fires before each Top Post result, inside <li>. |
||
| 355 | * |
||
| 356 | * @module widgets |
||
| 357 | * |
||
| 358 | * @since 3.2.0 |
||
| 359 | * |
||
| 360 | * @param string $post['post_id'] Post ID. |
||
| 361 | */ |
||
| 362 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Filter the permalink of items in the Top Posts widget. |
||
| 366 | * |
||
| 367 | * @module widgets |
||
| 368 | * |
||
| 369 | * @since 4.4.0 |
||
| 370 | * |
||
| 371 | * @param string $post['permalink'] Post permalink. |
||
| 372 | * @param array $post Post array. |
||
| 373 | */ |
||
| 374 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
| 375 | |||
| 376 | ?> |
||
| 377 | <a href="<?php echo esc_url( $filtered_permalink ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp"> |
||
| 378 | <img width="<?php echo absint( $width ); ?>" height="<?php echo absint( $height ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" /> |
||
| 379 | </a> |
||
| 380 | <?php |
||
| 381 | /** |
||
| 382 | * Fires after each Top Post result, inside <li>. |
||
| 383 | * |
||
| 384 | * @module widgets |
||
| 385 | * |
||
| 386 | * @since 3.2.0 |
||
| 387 | * |
||
| 388 | * @param string $post['post_id'] Post ID. |
||
| 389 | */ |
||
| 390 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
| 391 | ?> |
||
| 392 | </div> |
||
| 393 | <?php |
||
| 394 | endforeach; |
||
| 395 | echo "</div>\n"; |
||
| 396 | } else { |
||
| 397 | echo "<ul class='widgets-list-layout no-grav'>\n"; |
||
| 398 | foreach ( $posts as $post ) : |
||
| 399 | ?> |
||
| 400 | <li> |
||
| 401 | <?php |
||
| 402 | /** This action is documented in modules/widgets/top-posts.php */ |
||
| 403 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
| 404 | |||
| 405 | /** This filter is documented in modules/widgets/top-posts.php */ |
||
| 406 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
| 407 | ?> |
||
| 408 | <a href="<?php echo esc_url( $filtered_permalink ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp"> |
||
| 409 | <img width="<?php echo absint( $width ); ?>" height="<?php echo absint( $height ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" class='widgets-list-layout-blavatar' alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" /> |
||
| 410 | </a> |
||
| 411 | <div class="widgets-list-layout-links"> |
||
| 412 | <a href="<?php echo esc_url( $filtered_permalink ); ?>" class="bump-view" data-bump-view="tp"> |
||
| 413 | <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> |
||
| 414 | </a> |
||
| 415 | </div> |
||
| 416 | <?php |
||
| 417 | /** This action is documented in modules/widgets/top-posts.php */ |
||
| 418 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
| 419 | ?> |
||
| 420 | </li> |
||
| 421 | <?php |
||
| 422 | endforeach; |
||
| 423 | echo "</ul>\n"; |
||
| 424 | } |
||
| 425 | break; |
||
| 426 | default : |
||
| 427 | echo '<ul>'; |
||
| 428 | foreach ( $posts as $post ) : |
||
| 429 | ?> |
||
| 430 | <li> |
||
| 431 | <?php |
||
| 432 | /** This action is documented in modules/widgets/top-posts.php */ |
||
| 433 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
| 434 | |||
| 435 | /** This filter is documented in modules/widgets/top-posts.php */ |
||
| 436 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
| 437 | ?> |
||
| 438 | <a href="<?php echo esc_url( $filtered_permalink ); ?>" class="bump-view" data-bump-view="tp"> |
||
| 439 | <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?> |
||
| 440 | </a> |
||
| 441 | <?php |
||
| 442 | /** This action is documented in modules/widgets/top-posts.php */ |
||
| 443 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
| 444 | ?> |
||
| 445 | </li> |
||
| 446 | <?php |
||
| 447 | endforeach; |
||
| 448 | echo '</ul>'; |
||
| 449 | } |
||
| 450 | |||
| 451 | echo $args['after_widget']; |
||
| 452 | } |
||
| 453 | |||
| 640 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: