| Conditions | 25 |
| Paths | 6784 |
| Total Lines | 172 |
| Code Lines | 133 |
| Lines | 4 |
| Ratio | 2.33 % |
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 |
||
| 70 | public function widget( $args, $instance ) { |
||
| 71 | $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside'; |
||
| 72 | |||
| 73 | switch ( $format ) { |
||
| 74 | case 'image': |
||
| 75 | $format_string = __( 'Images', 'twentyfourteen' ); |
||
| 76 | $format_string_more = __( 'More images', 'twentyfourteen' ); |
||
| 77 | break; |
||
| 78 | case 'video': |
||
| 79 | $format_string = __( 'Videos', 'twentyfourteen' ); |
||
| 80 | $format_string_more = __( 'More videos', 'twentyfourteen' ); |
||
| 81 | break; |
||
| 82 | case 'audio': |
||
| 83 | $format_string = __( 'Audio', 'twentyfourteen' ); |
||
| 84 | $format_string_more = __( 'More audio', 'twentyfourteen' ); |
||
| 85 | break; |
||
| 86 | case 'quote': |
||
| 87 | $format_string = __( 'Quotes', 'twentyfourteen' ); |
||
| 88 | $format_string_more = __( 'More quotes', 'twentyfourteen' ); |
||
| 89 | break; |
||
| 90 | case 'link': |
||
| 91 | $format_string = __( 'Links', 'twentyfourteen' ); |
||
| 92 | $format_string_more = __( 'More links', 'twentyfourteen' ); |
||
| 93 | break; |
||
| 94 | case 'gallery': |
||
| 95 | $format_string = __( 'Galleries', 'twentyfourteen' ); |
||
| 96 | $format_string_more = __( 'More galleries', 'twentyfourteen' ); |
||
| 97 | break; |
||
| 98 | case 'aside': |
||
| 99 | default: |
||
| 100 | $format_string = __( 'Asides', 'twentyfourteen' ); |
||
| 101 | $format_string_more = __( 'More asides', 'twentyfourteen' ); |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | |||
| 105 | $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] ); |
||
| 106 | $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? $format_string : $instance['title'], $instance, $this->id_base ); |
||
| 107 | |||
| 108 | $ephemera = new WP_Query( array( |
||
| 109 | 'order' => 'DESC', |
||
| 110 | 'posts_per_page' => $number, |
||
| 111 | 'no_found_rows' => true, |
||
| 112 | 'post_status' => 'publish', |
||
| 113 | 'post__not_in' => get_option( 'sticky_posts' ), |
||
| 114 | 'tax_query' => array( |
||
| 115 | array( |
||
| 116 | 'taxonomy' => 'post_format', |
||
| 117 | 'terms' => array( "post-format-$format" ), |
||
| 118 | 'field' => 'slug', |
||
| 119 | 'operator' => 'IN', |
||
| 120 | ), |
||
| 121 | ), |
||
| 122 | ) ); |
||
| 123 | |||
| 124 | if ( $ephemera->have_posts() ) : |
||
| 125 | $tmp_content_width = $GLOBALS['content_width']; |
||
| 126 | $GLOBALS['content_width'] = 306; |
||
| 127 | |||
| 128 | echo $args['before_widget']; |
||
| 129 | ?> |
||
| 130 | <h1 class="widget-title <?php echo esc_attr( $format ); ?>"> |
||
| 131 | <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo esc_html( $title ); ?></a> |
||
| 132 | </h1> |
||
| 133 | <ol> |
||
| 134 | |||
| 135 | <?php |
||
| 136 | while ( $ephemera->have_posts() ) : |
||
| 137 | $ephemera->the_post(); |
||
| 138 | $tmp_more = $GLOBALS['more']; |
||
| 139 | $GLOBALS['more'] = 0; |
||
| 140 | ?> |
||
| 141 | <li> |
||
| 142 | <article <?php post_class(); ?>> |
||
| 143 | <div class="entry-content"> |
||
| 144 | <?php |
||
| 145 | if ( has_post_format( 'gallery' ) ) : |
||
| 146 | |||
| 147 | if ( post_password_required() ) : |
||
| 148 | the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); |
||
| 149 | else : |
||
| 150 | $images = array(); |
||
| 151 | |||
| 152 | $galleries = get_post_galleries( get_the_ID(), false ); |
||
| 153 | if ( isset( $galleries[0]['ids'] ) ) |
||
| 154 | $images = explode( ',', $galleries[0]['ids'] ); |
||
| 155 | |||
| 156 | if ( ! $images ) : |
||
| 157 | $images = get_posts( array( |
||
| 158 | 'fields' => 'ids', |
||
| 159 | 'numberposts' => -1, |
||
| 160 | 'order' => 'ASC', |
||
| 161 | 'orderby' => 'menu_order', |
||
| 162 | 'post_mime_type' => 'image', |
||
| 163 | 'post_parent' => get_the_ID(), |
||
| 164 | 'post_type' => 'attachment', |
||
| 165 | ) ); |
||
| 166 | endif; |
||
| 167 | |||
| 168 | $total_images = count( $images ); |
||
| 169 | |||
| 170 | if ( has_post_thumbnail() ) : |
||
| 171 | $post_thumbnail = get_the_post_thumbnail(); |
||
| 172 | elseif ( $total_images > 0 ) : |
||
| 173 | $image = reset( $images ); |
||
| 174 | $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' ); |
||
| 175 | endif; |
||
| 176 | |||
| 177 | if ( ! empty ( $post_thumbnail ) ) : |
||
| 178 | ?> |
||
| 179 | <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a> |
||
| 180 | <?php endif; ?> |
||
| 181 | <p class="wp-caption-text"> |
||
| 182 | <?php |
||
| 183 | printf( _n( 'This gallery contains <a href="%1$s" rel="bookmark">%2$s photo</a>.', 'This gallery contains <a href="%1$s" rel="bookmark">%2$s photos</a>.', $total_images, 'twentyfourteen' ), |
||
| 184 | esc_url( get_permalink() ), |
||
| 185 | number_format_i18n( $total_images ) |
||
| 186 | ); |
||
| 187 | ?> |
||
| 188 | </p> |
||
| 189 | <?php |
||
| 190 | endif; |
||
| 191 | |||
| 192 | else : |
||
| 193 | the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentyfourteen' ) ); |
||
| 194 | endif; |
||
| 195 | ?> |
||
| 196 | </div><!-- .entry-content --> |
||
| 197 | |||
| 198 | <header class="entry-header"> |
||
| 199 | <div class="entry-meta"> |
||
| 200 | <?php |
||
| 201 | if ( ! has_post_format( 'link' ) ) : |
||
| 202 | the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' ); |
||
| 203 | endif; |
||
| 204 | |||
| 205 | printf( '<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span> <span class="byline"><span class="author vcard"><a class="url fn n" href="%4$s" rel="author">%5$s</a></span></span>', |
||
| 206 | esc_url( get_permalink() ), |
||
| 207 | esc_attr( get_the_date( 'c' ) ), |
||
| 208 | esc_html( get_the_date() ), |
||
| 209 | esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ), |
||
| 210 | get_the_author() |
||
| 211 | ); |
||
| 212 | |||
| 213 | View Code Duplication | if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : |
|
| 214 | ?> |
||
| 215 | <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span> |
||
| 216 | <?php endif; ?> |
||
| 217 | </div><!-- .entry-meta --> |
||
| 218 | </header><!-- .entry-header --> |
||
| 219 | </article><!-- #post-## --> |
||
| 220 | </li> |
||
| 221 | <?php endwhile; ?> |
||
| 222 | |||
| 223 | </ol> |
||
| 224 | <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"> |
||
| 225 | <?php |
||
| 226 | /* translators: used with More archives link */ |
||
| 227 | printf( __( '%s <span class="meta-nav">→</span>', 'twentyfourteen' ), $format_string_more ); |
||
| 228 | ?> |
||
| 229 | </a> |
||
| 230 | <?php |
||
| 231 | |||
| 232 | echo $args['after_widget']; |
||
| 233 | |||
| 234 | // Reset the post globals as this query will have stomped on it. |
||
| 235 | wp_reset_postdata(); |
||
| 236 | |||
| 237 | $GLOBALS['more'] = $tmp_more; |
||
| 238 | $GLOBALS['content_width'] = $tmp_content_width; |
||
| 239 | |||
| 240 | endif; // End check for ephemeral posts. |
||
| 241 | } |
||
| 242 | |||
| 291 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.