Completed
Push — development ( dc59a1...87d23e )
by Eric
01:44
created

pagination.php ➔ spurs_pagination()   F

Complexity

Conditions 17
Paths 4322

Size

Total Lines 78
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 41
nc 4322
nop 0
dl 0
loc 78
rs 2.1974
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Pagination layout.
4
 *
5
 * @package spurs
6
 */
7
8
/**
9
 * Custom Pagination with numbers
10
 * Credits to http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/
11
 */
12
13
if ( ! function_exists( 'spurs_pagination' ) ) :
1 ignored issue
show
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
14
function spurs_pagination() {
0 ignored issues
show
Complexity introduced by
This operation has 443520 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
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">&laquo;</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 );
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
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">&raquo;</span><span class="sr-only">Next page</span>' ) );
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
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
93
endif;
94