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 | echo '<li>'; |
||
72 | |||
73 | // Display avatar and author name |
||
74 | if ( $r->have_posts() ) { |
||
75 | echo '<a href="' . get_author_posts_url( $author->ID ) . '">'; |
||
76 | |||
77 | View Code Duplication | if ( $instance['avatar_size'] > 1 ) |
|
78 | echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
||
79 | |||
80 | echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
||
81 | echo '</a>'; |
||
82 | } |
||
83 | else if ( $instance['all'] ) { |
||
84 | View Code Duplication | if ( $instance['avatar_size'] > 1 ) |
|
85 | echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
||
86 | |||
87 | echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
||
88 | } |
||
89 | |||
90 | if ( 0 == $instance['number'] ) { |
||
91 | echo '</li>'; |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | // Display a short list of recent posts for this author |
||
96 | |||
97 | |||
98 | if ( $r->have_posts() ){ |
||
99 | echo '<ul>'; |
||
100 | |||
101 | while ( $r->have_posts() ) { |
||
102 | $r->the_post(); |
||
103 | echo '<li><a href="' . get_permalink() . '">'; |
||
104 | |||
105 | if ( get_the_title() ) |
||
106 | echo get_the_title(); |
||
107 | else |
||
108 | echo get_the_ID(); |
||
109 | |||
110 | echo '</a></li>'; |
||
111 | } |
||
112 | |||
113 | echo '</ul>'; |
||
114 | } |
||
115 | |||
116 | echo '</li>'; |
||
117 | } |
||
118 | |||
119 | echo '</ul>'; |
||
120 | echo $args['after_widget']; |
||
121 | |||
122 | wp_reset_postdata(); |
||
123 | |||
124 | if ( '%BEG_OF_TITLE%' != $args['before_title'] ) |
||
125 | wp_cache_add( $cache_bucket, ob_get_flush(), 'widget'); |
||
126 | |||
127 | stats_extra( 'widget_view', 'authors' ); |
||
128 | } |
||
129 | |||
181 |