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