Conditions | 16 |
Paths | 138 |
Total Lines | 132 |
Code Lines | 65 |
Lines | 4 |
Ratio | 3.03 % |
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 |
||
35 | public function widget( $args, $instance ) { |
||
36 | global $wpdb; |
||
37 | |||
38 | $cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors'; |
||
39 | |||
40 | if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { |
||
41 | if ( $output = wp_cache_get( $cache_bucket, 'widget') ) { |
||
42 | echo $output; |
||
43 | return; |
||
44 | } |
||
45 | |||
46 | ob_start(); |
||
47 | } |
||
48 | |||
49 | $instance = wp_parse_args( $instance, array( 'title' => __( 'Authors', 'jetpack' ), 'all' => false, 'number' => 5, 'avatar_size' => 48 ) ); |
||
50 | $instance['number'] = min( 10, max( 0, (int) $instance['number'] ) ); |
||
51 | |||
52 | // We need to query at least one post to determine whether an author has written any posts or not |
||
53 | $query_number = max( $instance['number'], 1 ); |
||
54 | |||
55 | $default_excluded_authors = array(); |
||
56 | /** |
||
57 | * Filter authors from the Widget Authors widget. |
||
58 | * |
||
59 | * @module widgets |
||
60 | * |
||
61 | * @since 4.5.0 |
||
62 | * |
||
63 | * @param array $default_excluded_authors Array of user ID's that will be excluded |
||
64 | */ |
||
65 | $excluded_authors = apply_filters( 'jetpack_widget_authors_exclude', $default_excluded_authors ); |
||
66 | |||
67 | $authors = get_users( array( |
||
68 | 'fields' => 'all', |
||
69 | 'who' => 'authors', |
||
70 | 'exclude' => (array) $excluded_authors, |
||
71 | ) ); |
||
72 | |||
73 | echo $args['before_widget']; |
||
74 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
||
75 | $title = apply_filters( 'widget_title', $instance['title'] ); |
||
76 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; |
||
77 | echo '<ul>'; |
||
78 | |||
79 | $default_post_type = 'post'; |
||
80 | /** |
||
81 | * Filter types of posts that will be counted in the widget |
||
82 | * |
||
83 | * @module widgets |
||
84 | * |
||
85 | * @since 4.5.0 |
||
86 | * |
||
87 | * @param string|array $default_post_type type(s) of posts to count for the widget. |
||
88 | */ |
||
89 | $post_types = apply_filters( 'jetpack_widget_authors_post_types', $default_post_type ); |
||
90 | |||
91 | foreach ( $authors as $author ) { |
||
92 | $r = new WP_Query( array( |
||
93 | 'author' => $author->ID, |
||
94 | 'posts_per_page' => $query_number, |
||
95 | 'post_type' => $post_types, |
||
96 | 'post_status' => 'publish', |
||
97 | 'no_found_rows' => true, |
||
98 | 'has_password' => false, |
||
99 | ) ); |
||
100 | |||
101 | if ( ! $r->have_posts() && ! $instance['all'] ) { |
||
102 | continue; |
||
103 | } |
||
104 | |||
105 | echo '<li>'; |
||
106 | |||
107 | // Display avatar and author name |
||
108 | if ( $r->have_posts() ) { |
||
109 | echo '<a href="' . get_author_posts_url( $author->ID ) . '">'; |
||
110 | |||
111 | View Code Duplication | if ( $instance['avatar_size'] > 1 ) { |
|
112 | echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
||
113 | } |
||
114 | |||
115 | echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
||
116 | echo '</a>'; |
||
117 | } |
||
118 | else if ( $instance['all'] ) { |
||
119 | View Code Duplication | if ( $instance['avatar_size'] > 1 ) { |
|
120 | echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
||
121 | } |
||
122 | |||
123 | echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
||
124 | } |
||
125 | |||
126 | if ( 0 == $instance['number'] ) { |
||
127 | echo '</li>'; |
||
128 | continue; |
||
129 | } |
||
130 | |||
131 | // Display a short list of recent posts for this author |
||
132 | |||
133 | if ( $r->have_posts() ) { |
||
134 | echo '<ul>'; |
||
135 | |||
136 | while ( $r->have_posts() ) { |
||
137 | $r->the_post(); |
||
138 | echo '<li><a href="' . get_permalink() . '">'; |
||
139 | |||
140 | if ( get_the_title() ) { |
||
141 | echo get_the_title(); |
||
142 | } else { |
||
143 | echo get_the_ID(); |
||
144 | } |
||
145 | |||
146 | echo '</a></li>'; |
||
147 | } |
||
148 | |||
149 | echo '</ul>'; |
||
150 | } |
||
151 | |||
152 | echo '</li>'; |
||
153 | } |
||
154 | |||
155 | echo '</ul>'; |
||
156 | echo $args['after_widget']; |
||
157 | |||
158 | wp_reset_postdata(); |
||
159 | |||
160 | if ( '%BEG_OF_TITLE%' != $args['before_title'] ) { |
||
161 | wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' ); |
||
162 | } |
||
163 | |||
164 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
165 | do_action( 'jetpack_stats_extra', 'widget_view', 'authors' ); |
||
166 | } |
||
167 | |||
227 |