| Conditions | 17 |
| Paths | 4322 |
| Total Lines | 78 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 14 | function spurs_pagination() { |
||
| 15 | if ( is_singular() ) { |
||
| 16 | return; |
||
| 17 | } |
||
| 18 | |||
| 19 | global $wp_query; |
||
| 20 | |||
| 21 | /** Stop execution if there's only 1 page */ |
||
| 22 | if ( $wp_query->max_num_pages <= 1 ) { |
||
| 23 | return; |
||
| 24 | } |
||
| 25 | |||
| 26 | $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; |
||
| 27 | $max = intval( $wp_query->max_num_pages ); |
||
| 28 | |||
| 29 | /** Add current page to the array */ |
||
| 30 | if ( $paged >= 1 ) { |
||
| 31 | $links[] = $paged; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** Add the pages around the current page to the array */ |
||
| 35 | if ( $paged >= 3 ) { |
||
| 36 | $links[] = $paged - 1; |
||
| 37 | $links[] = $paged - 2; |
||
| 38 | } |
||
| 39 | |||
| 40 | if ( ( $paged + 2 ) <= $max ) { |
||
| 41 | $links[] = $paged + 2; |
||
| 42 | $links[] = $paged + 1; |
||
| 43 | } |
||
| 44 | |||
| 45 | echo '<nav aria-label="Page navigation"><ul class="pagination ">' . "\n"; |
||
| 46 | |||
| 47 | /** Link to first page, plus ellipses if necessary */ |
||
| 48 | if ( ! in_array( 1, $links ) ) { |
||
| 49 | $class = 1 == $paged ? ' class="active page-item"' : ' class="page-item"'; |
||
| 50 | |||
| 51 | printf( '<li %s><a class="page-link" href="%s"><i class="fa fa-step-backward" aria-hidden="true"></i></a></li>' . "\n", |
||
| 52 | $class, esc_url( get_pagenum_link( 1 ) ), '1' ); |
||
| 53 | |||
| 54 | /** Previous Post Link */ |
||
| 55 | if ( get_previous_posts_link() ) { |
||
| 56 | printf( '<li class="page-item page-item-direction page-item-prev"><span class="page-link">%1$s</span></li> ' . "\n", |
||
| 57 | get_previous_posts_link( '<span aria-hidden="true">«</span><span class="sr-only">Previous page</span>' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ( ! in_array( 2, $links ) ) { |
||
| 61 | echo '<li class="page-item"></li>'; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | // Link to current page, plus 2 pages in either direction if necessary. |
||
| 66 | sort( $links ); |
||
| 67 | foreach ( (array) $links as $link ) { |
||
| 68 | $class = $paged == $link ? ' class="active page-item"' : ' class="page-item"'; |
||
| 69 | printf( '<li %s><a href="%s" class="page-link">%s</a></li>' . "\n", $class, |
||
| 70 | esc_url( get_pagenum_link( $link ) ), $link ); |
||
| 71 | } |
||
| 72 | |||
| 73 | // Next Post Link. |
||
| 74 | if ( get_next_posts_link() ) { |
||
| 75 | printf( '<li class="page-item page-item-direction page-item-next"><span class="page-link">%s</span></li>' . "\n", |
||
| 76 | get_next_posts_link( '<span aria-hidden="true">»</span><span class="sr-only">Next page</span>' ) ); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Link to last page, plus ellipses if necessary. |
||
| 80 | if ( ! in_array( $max, $links ) ) { |
||
| 81 | if ( ! in_array( $max - 1, $links ) ) { |
||
| 82 | echo '<li class="page-item"></li>' . "\n"; |
||
| 83 | } |
||
| 84 | |||
| 85 | $class = $paged == $max ? ' class="active "' : ' class="page-item"'; |
||
| 86 | printf( '<li %s><a class="page-link" href="%s" aria-label="Next"><span aria-hidden="true"><i class="fa fa-step-forward" aria-hidden="true"></i></span><span class="sr-only">%s</span></a></li>' . "\n", |
||
| 87 | $class . '', esc_url( get_pagenum_link( esc_html( $max ) ) ), esc_html( $max ) ); |
||
| 88 | } |
||
| 89 | |||
| 90 | echo '</ul></nav>' . "\n"; |
||
| 91 | } |
||
| 92 | |||
| 94 |