Passed
Push — master ( c16653...06c25c )
by Brian
04:36
created

AUI_Component_Pagination::get()   C

Complexity

Conditions 12
Paths 170

Size

Total Lines 64
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 37
c 1
b 0
f 1
dl 0
loc 64
rs 6.3833
cc 12
nc 170
nop 1

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
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * A component class for rendering a bootstrap pagination.
9
 *
10
 * @since 1.0.0
11
 */
12
class AUI_Component_Pagination {
13
14
	/**
15
	 * Build the component.
16
	 *
17
	 * @param array $args
18
	 *
19
	 * @return string The rendered component.
20
	 */
21
	public static function get( $args = array() ) {
22
		global $wp_query;
23
24
		$defaults = array(
25
			'class'              => '',
26
			'mid_size'           => 2,
27
			'prev_text'          => '<i class="fas fa-chevron-left"></i>',
28
			'next_text'          => '<i class="fas fa-chevron-right"></i>',
29
			'screen_reader_text' => __( 'Posts navigation' ),
30
			'before_paging' => '',
31
			'after_paging'  => '',
32
			'type'               => 'array',
33
			'total'              => isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1,
34
			'links'              => array() // an array of links if using custom links, this includes the a tag.
35
		);
36
37
		/**
38
		 * Parse incoming $args into an array and merge it with $defaults
39
		 */
40
		$args = wp_parse_args( $args, $defaults );
41
42
		$output = '';
43
44
		// Don't print empty markup if there's only one page.
45
		if ( $args['total'] > 1 ) {
46
47
			// Set up paginated links.
48
			$links = !empty(  $args['links'] ) ? $args['links'] :  paginate_links( $args );
49
50
			$class = !empty($args['class']) ? $args['class'] : '';
51
52
			// make the output bootstrap ready
53
			$links_html = "<ul class='pagination m-0 p-0 $class'>";
54
			if ( ! empty( $links ) ) {
55
				foreach ( $links as $link ) {
56
					$active = strpos( $link, 'current' ) !== false ? 'active' : '';
57
					$links_html .= "<li class='page-item $active'>";
58
					$links_html .= str_replace( "page-numbers", "page-link", $link );
59
					$links_html .= "</li>";
60
				}
61
			}
62
			$links_html .= "</ul>";
63
64
			if ( $links ) {
65
				$output .= '<section class="px-0 py-2 w-100">';
66
				$output .= _navigation_markup( $links_html, 'aui-pagination', $args['screen_reader_text'] );
67
				$output .= '</section>';
68
			}
69
70
			$output = str_replace( "screen-reader-text", "screen-reader-text sr-only", $output );
71
			$output = str_replace( "nav-links", "aui-nav-links", $output );
72
		}
73
74
		if ( $output ) {
75
			if ( ! empty( $args['before_paging'] ) ) {
76
				$output = $args['before_paging'] . $output;
77
			}
78
79
			if ( ! empty( $args['after_paging'] ) ) {
80
				$output = $output . $args['after_paging'];
81
			}
82
		}
83
84
		return $output;
85
	}
86
87
}