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