Conditions | 36 |
Paths | > 20000 |
Total Lines | 277 |
Lines | 5 |
Ratio | 1.81 % |
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 |
||
216 | function widget( $args, $instance ) { |
||
217 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
218 | do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' ); |
||
219 | |||
220 | $instance = wp_parse_args( (array) $instance, $this->defaults() ); |
||
221 | |||
222 | $title = isset( $instance['title'] ) ? $instance['title'] : false; |
||
223 | if ( false === $title ) { |
||
224 | $title = $this->default_title; |
||
225 | } |
||
226 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
227 | $title = apply_filters( 'widget_title', $title ); |
||
228 | |||
229 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
||
230 | if ( $count < 1 || 10 < $count ) { |
||
231 | $count = 10; |
||
232 | } |
||
233 | /** |
||
234 | * Control the number of displayed posts. |
||
235 | * |
||
236 | * @module widgets |
||
237 | * |
||
238 | * @since 3.3.0 |
||
239 | * |
||
240 | * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10. |
||
241 | */ |
||
242 | $count = apply_filters( 'jetpack_top_posts_widget_count', $count ); |
||
243 | |||
244 | $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' ); |
||
245 | |||
246 | // 'likes' are not available in Jetpack |
||
247 | $ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views'; |
||
248 | |||
249 | View Code Duplication | if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { |
|
250 | $display = $instance['display']; |
||
251 | } else { |
||
252 | $display = 'text'; |
||
253 | } |
||
254 | |||
255 | if ( 'text' != $display ) { |
||
256 | $get_image_options = array( |
||
257 | 'fallback_to_avatars' => true, |
||
258 | /** This filter is documented in modules/stats.php */ |
||
259 | 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'https://en.wordpress.com/i/logo/white-gray-80.png' ) ), |
||
260 | 'avatar_size' => 40, |
||
261 | 'width' => null, |
||
262 | 'height' => null, |
||
263 | ); |
||
264 | if ( 'grid' == $display ) { |
||
265 | $get_image_options['avatar_size'] = 200; |
||
266 | } |
||
267 | /** |
||
268 | * Top Posts Widget Image options. |
||
269 | * |
||
270 | * @module widgets |
||
271 | * |
||
272 | * @since 1.8.0 |
||
273 | * |
||
274 | * @param array $get_image_options { |
||
275 | * Array of Image options. |
||
276 | * @type bool true Should we default to Gravatars when no image is found? Default is true. |
||
277 | * @type string $gravatar_default Default Image URL if no Gravatar is found. |
||
278 | * @type int $avatar_size Default Image size. |
||
279 | * @type mixed $width Image width, not set by default and $avatar_size is used instead. |
||
280 | * @type mixed $height Image height, not set by default and $avatar_size is used instead. |
||
281 | * } |
||
282 | */ |
||
283 | $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); |
||
284 | } |
||
285 | |||
286 | if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) { |
||
287 | $posts = $this->get_by_likes( $count, $types ); |
||
288 | } else { |
||
289 | $posts = $this->get_by_views( $count, $args, $types ); |
||
290 | } |
||
291 | |||
292 | if ( ! $posts ) { |
||
|
|||
293 | $posts = $this->get_fallback_posts( $count, $types ); |
||
294 | } |
||
295 | |||
296 | echo $args['before_widget']; |
||
297 | if ( ! empty( $title ) ) { |
||
298 | echo $args['before_title'] . $title . $args['after_title']; |
||
299 | } |
||
300 | |||
301 | if ( ! $posts ) { |
||
302 | $link = 'https://jetpack.com/support/getting-more-views-and-traffic/'; |
||
303 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
304 | $link = 'https://en.support.wordpress.com/getting-more-site-traffic/'; |
||
305 | } |
||
306 | |||
307 | if ( current_user_can( 'edit_theme_options' ) ) { |
||
308 | echo '<p>' . sprintf( |
||
309 | __( 'There are no posts to display. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ), |
||
310 | esc_url( $link ) |
||
311 | ) . '</p>'; |
||
312 | } |
||
313 | |||
314 | echo $args['after_widget']; |
||
315 | return; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Filter the layout of the Top Posts Widget |
||
320 | * |
||
321 | * @module widgets |
||
322 | * |
||
323 | * @since 6.4.0 |
||
324 | * |
||
325 | * @param string $layout layout of the Top Posts Widget (empty string) |
||
326 | * @param array $posts IDs of the posts to be displayed |
||
327 | * @param array $display Display option from widget form |
||
328 | */ |
||
329 | $layout = apply_filters( 'jetpack_top_posts_widget_layout', '', $posts, $display ); |
||
330 | if ( ! empty( $layout ) ) { |
||
331 | echo $layout; |
||
332 | echo $args['after_widget']; |
||
333 | return; |
||
334 | } |
||
335 | |||
336 | switch ( $display ) { |
||
337 | case 'list': |
||
338 | case 'grid': |
||
339 | // Keep the avatar_size as default dimensions for backward compatibility. |
||
340 | $width = (int) $get_image_options['avatar_size']; |
||
341 | $height = (int) $get_image_options['avatar_size']; |
||
342 | |||
343 | // Check if the user has changed the width. |
||
344 | if ( ! empty( $get_image_options['width'] ) ) { |
||
345 | $width = (int) $get_image_options['width']; |
||
346 | } |
||
347 | |||
348 | // Check if the user has changed the height. |
||
349 | if ( ! empty( $get_image_options['height'] ) ) { |
||
350 | $height = (int) $get_image_options['height']; |
||
351 | } |
||
352 | |||
353 | foreach ( $posts as &$post ) { |
||
354 | $image = Jetpack_PostImages::get_image( |
||
355 | $post['post_id'], |
||
356 | array( |
||
357 | 'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'], |
||
358 | 'width' => (int) $width, |
||
359 | 'height' => (int) $height, |
||
360 | 'avatar_size' => (int) $get_image_options['avatar_size'], |
||
361 | ) |
||
362 | ); |
||
363 | $post['image'] = $image['src']; |
||
364 | if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { |
||
365 | $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) ); |
||
366 | } |
||
367 | } |
||
368 | |||
369 | unset( $post ); |
||
370 | |||
371 | if ( 'grid' == $display ) { |
||
372 | echo "<div class='widgets-grid-layout no-grav'>\n"; |
||
373 | foreach ( $posts as $post ) : |
||
374 | ?> |
||
375 | <div class="widget-grid-view-image"> |
||
376 | <?php |
||
377 | /** |
||
378 | * Fires before each Top Post result, inside <li>. |
||
379 | * |
||
380 | * @module widgets |
||
381 | * |
||
382 | * @since 3.2.0 |
||
383 | * |
||
384 | * @param string $post['post_id'] Post ID. |
||
385 | */ |
||
386 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
387 | |||
388 | /** |
||
389 | * Filter the permalink of items in the Top Posts widget. |
||
390 | * |
||
391 | * @module widgets |
||
392 | * |
||
393 | * @since 4.4.0 |
||
394 | * |
||
395 | * @param string $post['permalink'] Post permalink. |
||
396 | * @param array $post Post array. |
||
397 | */ |
||
398 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
399 | |||
400 | printf( |
||
401 | '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true"/></a>', |
||
402 | esc_url( $filtered_permalink ), |
||
403 | esc_attr( wp_kses( $post['title'], array() ) ), |
||
404 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
||
405 | absint( $width ), |
||
406 | absint( $height ), |
||
407 | esc_url( $post['image'] ) |
||
408 | ); |
||
409 | |||
410 | /** |
||
411 | * Fires after each Top Post result, inside <li>. |
||
412 | * |
||
413 | * @module widgets |
||
414 | * |
||
415 | * @since 3.2.0 |
||
416 | * |
||
417 | * @param string $post['post_id'] Post ID. |
||
418 | */ |
||
419 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
420 | ?> |
||
421 | </div> |
||
422 | <?php |
||
423 | endforeach; |
||
424 | echo "</div>\n"; |
||
425 | } else { |
||
426 | echo "<ul class='widgets-list-layout no-grav'>\n"; |
||
427 | foreach ( $posts as $post ) : |
||
428 | ?> |
||
429 | <li> |
||
430 | <?php |
||
431 | /** This action is documented in modules/widgets/top-posts.php */ |
||
432 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
433 | |||
434 | /** This filter is documented in modules/widgets/top-posts.php */ |
||
435 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
436 | |||
437 | printf( |
||
438 | '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s> |
||
439 | <img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true" class="widgets-list-layout-blavatar"/> |
||
440 | </a> |
||
441 | <div class="widgets-list-layout-links"> |
||
442 | <a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s>%7$s</a> |
||
443 | </div> |
||
444 | ', |
||
445 | esc_url( $filtered_permalink ), |
||
446 | esc_attr( wp_kses( $post['title'], array() ) ), |
||
447 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
||
448 | absint( $width ), |
||
449 | absint( $height ), |
||
450 | esc_url( $post['image'] ), |
||
451 | esc_html( wp_kses( $post['title'], array() ) ) |
||
452 | ); |
||
453 | |||
454 | /** This action is documented in modules/widgets/top-posts.php */ |
||
455 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
456 | ?> |
||
457 | </li> |
||
458 | <?php |
||
459 | endforeach; |
||
460 | echo "</ul>\n"; |
||
461 | } |
||
462 | break; |
||
463 | default: |
||
464 | echo '<ul>'; |
||
465 | foreach ( $posts as $post ) : |
||
466 | ?> |
||
467 | <li> |
||
468 | <?php |
||
469 | /** This action is documented in modules/widgets/top-posts.php */ |
||
470 | do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] ); |
||
471 | |||
472 | /** This filter is documented in modules/widgets/top-posts.php */ |
||
473 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
||
474 | |||
475 | printf( |
||
476 | '<a href="%1$s" class="bump-view" data-bump-view="tp"%2$s>%3$s</a>', |
||
477 | esc_url( $filtered_permalink ), |
||
478 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
||
479 | esc_html( wp_kses( $post['title'], array() ) ) |
||
480 | ); |
||
481 | |||
482 | /** This action is documented in modules/widgets/top-posts.php */ |
||
483 | do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); |
||
484 | ?> |
||
485 | </li> |
||
486 | <?php |
||
487 | endforeach; |
||
488 | echo '</ul>'; |
||
489 | } |
||
490 | |||
491 | echo $args['after_widget']; |
||
492 | } |
||
493 | |||
728 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.