Passed
Push — master ( de7162...f256de )
by Brian
739:27 queued 623:54
created

AUI_Component_Pagination::get()   B

Complexity

Conditions 10
Paths 50

Size

Total Lines 60
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 34
c 1
b 0
f 1
nc 50
nop 1
dl 0
loc 60
rs 7.6666

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
			'mid_size'           => 2,
26
			'prev_text'          => '<i class="fas fa-chevron-left"></i>',
27
			'next_text'          => '<i class="fas fa-chevron-right"></i>',
28
			'screen_reader_text' => __( 'Posts navigation' ),
29
			'before_page_number' => '',
30
			'after_page_number'  => '',
31
			'type'               => 'array',
32
			'total'              => isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1
33
		);
34
35
		/**
36
		 * Parse incoming $args into an array and merge it with $defaults
37
		 */
38
		$args = wp_parse_args( $args, $defaults );
39
40
		$output = '';
41
42
		// Don't print empty markup if there's only one page.
43
		if ( $args['total'] > 1 ) {
44
45
			// Set up paginated links.
46
			$links = paginate_links( $args );
47
48
			// make the output bootstrap ready
49
			$links_html = "<ul class='pagination m-0 p-0'>";
50
			if ( ! empty( $links ) ) {
51
				foreach ( $links as $link ) {
52
					$active = strpos( $link, 'current' ) !== false ? 'active' : '';
53
					$links_html .= "<li class='page-item $active'>";
54
					$links_html .= str_replace( "page-numbers", "page-link", $link );
55
					$links_html .= "</li>";
56
				}
57
			}
58
			$links_html .= "</ul>";
59
60
			if ( $links ) {
61
				$output .= '<section class="px-0 py-2 w-100">';
62
				$output .= _navigation_markup( $links_html, 'aui-pagination', $args['screen_reader_text'] );
63
				$output .= '</section>';
64
			}
65
66
			$output = str_replace( "screen-reader-text", "screen-reader-text sr-only", $output );
67
			$output = str_replace( "nav-links", "aui-nav-links", $output );
68
		}
69
70
		if ( $output ) {
71
			if ( ! empty( $args['before_paging'] ) ) {
72
				$output = $args['before_paging'] . $output;
73
			}
74
75
			if ( ! empty( $args['after_paging'] ) ) {
76
				$output = $output . $args['after_paging'];
77
			}
78
		}
79
80
		return $output;
81
	}
82
83
}