| Conditions | 4 |
| Paths | 4 |
| Total Lines | 55 |
| Code Lines | 37 |
| 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 |
||
| 9 | function spurs_pagination( $args = [], $class = 'pagination' ) { |
||
|
|
|||
| 10 | |||
| 11 | if ( $GLOBALS['wp_query']->max_num_pages <= 1 ) { |
||
| 12 | return; |
||
| 13 | } |
||
| 14 | |||
| 15 | $args = wp_parse_args( $args, [ |
||
| 16 | 'mid_size' => 2, |
||
| 17 | 'prev_next' => false, |
||
| 18 | 'prev_text' => __( '«', 'spurs' ), |
||
| 19 | 'next_text' => __( '»', 'spurs' ), |
||
| 20 | 'screen_reader_text' => __( 'Posts navigation', 'spurs' ), |
||
| 21 | 'type' => 'array', |
||
| 22 | 'current' => max( 1, get_query_var( 'paged' ) ), |
||
| 23 | ] ); |
||
| 24 | |||
| 25 | $links = paginate_links( $args ); |
||
| 26 | $next_link = get_next_posts_page_link(); |
||
| 27 | $prev_link = get_previous_posts_page_link(); |
||
| 28 | |||
| 29 | ?> |
||
| 30 | |||
| 31 | <nav aria-label="<?php echo $args['screen_reader_text']; ?>"> |
||
| 32 | <ul class="pagination"> |
||
| 33 | <li class="page-item"> |
||
| 34 | <a class="page-link" href="<?php echo esc_attr( $prev_link ); ?>" |
||
| 35 | aria-label="<?php echo __( 'Previous', 'spurs' ); ?>"> |
||
| 36 | <span aria-hidden="true"><?php echo esc_attr( $args['prev_text'] ); ?></span> |
||
| 37 | <span class="sr-only"><?php echo __( 'Previous', 'spurs' ); ?></span> |
||
| 38 | </a> |
||
| 39 | </li> |
||
| 40 | |||
| 41 | <?php |
||
| 42 | $i = 1; |
||
| 43 | foreach ( $links as $link ) { ?> |
||
| 44 | <li class="page-item <?php if ( $i == $args['current'] ) { |
||
| 45 | echo 'active'; |
||
| 46 | }; ?>"> |
||
| 47 | <?php echo str_replace( 'page-numbers', 'page-link', $link ); ?> |
||
| 48 | </li> |
||
| 49 | |||
| 50 | <?php $i ++; |
||
| 51 | } ?> |
||
| 52 | |||
| 53 | <li class="page-item"> |
||
| 54 | <a class="page-link" href="<?php echo esc_attr( $next_link ); ?>" |
||
| 55 | aria-label="<?php echo __( 'Next', 'spurs' ); ?>"> |
||
| 56 | <span aria-hidden="true"><?php echo esc_attr( $args['next_text'] ); ?></span> |
||
| 57 | <span class="sr-only"><?php echo __( 'Next', 'spurs' ); ?></span> |
||
| 58 | </a> |
||
| 59 | </li> |
||
| 60 | </ul> |
||
| 61 | </nav> |
||
| 62 | <?php |
||
| 63 | } |
||
| 64 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: