| Conditions | 16 |
| Paths | 138 |
| Total Lines | 100 |
| Code Lines | 58 |
| Lines | 4 |
| Ratio | 4 % |
| 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 |
||
| 31 | public function widget( $args, $instance ) { |
||
| 32 | global $wpdb; |
||
| 33 | |||
| 34 | $cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors'; |
||
| 35 | |||
| 36 | if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { |
||
| 37 | if ( $output = wp_cache_get( $cache_bucket, 'widget') ) { |
||
| 38 | echo $output; |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | ob_start(); |
||
| 43 | } |
||
| 44 | |||
| 45 | $instance = wp_parse_args( $instance, array( 'title' => __( 'Authors' ), 'all' => false, 'number' => 5, 'avatar_size' => 48 ) ); |
||
| 46 | $instance['number'] = min( 10, max( 0, (int) $instance['number'] ) ); |
||
| 47 | |||
| 48 | // We need to query at least one post to determine whether an author has written any posts or not |
||
| 49 | $query_number = max( $instance['number'], 1 ); |
||
| 50 | |||
| 51 | $authors = get_users( array( |
||
| 52 | 'fields' => 'all', |
||
| 53 | 'who' => 'authors' |
||
| 54 | ) ); |
||
| 55 | |||
| 56 | echo $args['before_widget']; |
||
| 57 | echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title']; |
||
| 58 | echo '<ul>'; |
||
| 59 | |||
| 60 | foreach ( $authors as $author ) { |
||
| 61 | $r = new WP_Query( array( |
||
| 62 | 'author' => $author->ID, |
||
| 63 | 'posts_per_page' => $query_number, |
||
| 64 | 'post_type' => 'post', |
||
| 65 | 'post_status' => 'publish', |
||
| 66 | 'no_found_rows' => true, |
||
| 67 | ) ); |
||
| 68 | |||
| 69 | if ( ! $r->have_posts() && ! $instance['all'] ) |
||
| 70 | continue; |
||
| 71 | |||
| 72 | echo '<li>'; |
||
| 73 | |||
| 74 | // Display avatar and author name |
||
| 75 | if ( $r->have_posts() ) { |
||
| 76 | echo '<a href="' . get_author_posts_url( $author->ID ) . '">'; |
||
| 77 | |||
| 78 | View Code Duplication | if ( $instance['avatar_size'] > 1 ) |
|
| 79 | echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
||
| 80 | |||
| 81 | echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
||
| 82 | echo '</a>'; |
||
| 83 | } |
||
| 84 | else if ( $instance['all'] ) { |
||
| 85 | View Code Duplication | if ( $instance['avatar_size'] > 1 ) |
|
| 86 | echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
||
| 87 | |||
| 88 | echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ( 0 == $instance['number'] ) { |
||
| 92 | echo '</li>'; |
||
| 93 | continue; |
||
| 94 | } |
||
| 95 | |||
| 96 | // Display a short list of recent posts for this author |
||
| 97 | |||
| 98 | |||
| 99 | if ( $r->have_posts() ){ |
||
| 100 | echo '<ul>'; |
||
| 101 | |||
| 102 | while ( $r->have_posts() ) { |
||
| 103 | $r->the_post(); |
||
| 104 | echo '<li><a href="' . get_permalink() . '">'; |
||
| 105 | |||
| 106 | if ( get_the_title() ) |
||
| 107 | echo get_the_title(); |
||
| 108 | else |
||
| 109 | echo get_the_ID(); |
||
| 110 | |||
| 111 | echo '</a></li>'; |
||
| 112 | } |
||
| 113 | |||
| 114 | echo '</ul>'; |
||
| 115 | } |
||
| 116 | |||
| 117 | echo '</li>'; |
||
| 118 | } |
||
| 119 | |||
| 120 | echo '</ul>'; |
||
| 121 | echo $args['after_widget']; |
||
| 122 | |||
| 123 | wp_reset_postdata(); |
||
| 124 | |||
| 125 | if ( '%BEG_OF_TITLE%' != $args['before_title'] ) |
||
| 126 | wp_cache_add( $cache_bucket, ob_get_flush(), 'widget'); |
||
| 127 | |||
| 128 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
| 129 | do_action( 'jetpack_stats_extra', 'widget_view', 'authors' ); |
||
| 130 | } |
||
| 131 | |||
| 183 |