AUI_Component_Pagination::get()   F
last analyzed

Complexity

Conditions 27
Paths 714

Size

Total Lines 99
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
eloc 63
c 0
b 0
f 0
nc 714
nop 1
dl 0
loc 99
rs 0.3972

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, $aui_bs5;
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', 'ayecode-connect' ),
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
			'rounded_style'      => false,
36
			'custom_next_text'   => '', // Custom next page text
37
			'custom_prev_text'   => '', // Custom prev page text
38
		);
39
40
		/**
41
		 * Parse incoming $args into an array and merge it with $defaults
42
		 */
43
		$args = wp_parse_args( $args, $defaults );
44
45
		$output = '';
46
47
		// Don't print empty markup if there's only one page.
48
		if ( $args['total'] > 1 ) {
49
			// Set up paginated links.
50
			$links = !empty(  $args['links'] ) ? $args['links'] :  paginate_links( $args );
51
52
			$class = !empty($args['class']) ? $args['class'] : '';
53
54
			$custom_prev_link = '';
55
			$custom_next_link = '';
56
57
			// make the output bootstrap ready
58
			$links_html = "<ul class='pagination m-0 p-0 $class'>";
59
			if ( ! empty( $links ) ) {
60
				foreach ( $links as $link ) {
61
					$_link = $link;
62
63
					if ( $aui_bs5 ) {
64
						$link_class = $args['rounded_style'] ? 'page-link badge rounded-pill border-0 mx-1 fs-base text-dark link-primary' : 'page-link';
65
						$link_class_active = $args['rounded_style'] ? ' current active fw-bold badge rounded-pill' : ' current active';
66
						$links_html .= "<li class='page-item mx-0'>";
67
						$link = str_replace( array( "page-numbers", " current" ), array( $link_class, $link_class_active ), $link );
68
						$link = str_replace( 'text-dark link-primary current', 'current', $link );
69
						$links_html .=  $link;
70
						$links_html .= "</li>";
71
					} else {
72
						$active = strpos( $link, 'current' ) !== false ? 'active' : '';
73
						$links_html .= "<li class='page-item $active'>";
74
						$links_html .= str_replace( "page-numbers", "page-link", $link );
75
						$links_html .= "</li>";
76
					}
77
78
					if ( strpos( $_link, 'next page-numbers' ) || strpos( $_link, 'prev page-numbers' ) ) {
79
						$link = str_replace( array( "page-numbers", " current" ), array( 'btn btn-outline-primary rounded' . ( $args['rounded_style'] ? '-pill' : '' ) . ' mx-1 fs-base text-dark link-primary', ' current active fw-bold badge rounded-pill' ), $_link );
80
						$link = str_replace( 'text-dark link-primary current', 'current', $link );
81
82
						if ( strpos( $_link, 'next page-numbers' ) && ! empty( $args['custom_next_text'] ) ) {
83
							$custom_next_link = str_replace( $args['next_text'], $args['custom_next_text'], $link );
84
						} else if ( strpos( $_link, 'prev page-numbers' ) && ! empty( $args['custom_prev_text'] ) ) {
85
							$custom_prev_link = str_replace( $args['prev_text'], $args['custom_prev_text'], $link );
86
						}
87
					}
88
				}
89
			}
90
			$links_html .= "</ul>";
91
92
			if ( $links ) {
93
				$output .= '<section class="px-0 py-2 w-100">';
94
				$output .= _navigation_markup( $links_html, 'aui-pagination', $args['screen_reader_text'] );
95
				$output .= '</section>';
96
			}
97
98
			$output = str_replace( "screen-reader-text", "screen-reader-text sr-only" . ( $aui_bs5 ? ' visually-hidden' : '' ), $output );
99
			$output = str_replace( "nav-links", "aui-nav-links", $output );
100
		}
101
102
		if ( $output ) {
103
			if ( $custom_next_link || $custom_prev_link ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $custom_next_link does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $custom_prev_link does not seem to be defined for all execution paths leading up to this point.
Loading history...
104
				$total   = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $total is dead and can be removed.
Loading history...
105
				$current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
106
107
				$output = '<div class="row d-flex align-items-center justify-content-between"><div class="col text-start">' . $custom_prev_link . '</div><div class="col text-center d-none d-md-block">' . $output . '</div><div class="col text-center d-md-none">' . $current . '/' . $args['total'] . '</div><div class="col text-end">' . $custom_next_link . '</div></div>';
108
			}
109
110
			if ( ! empty( $args['before_paging'] ) ) {
111
				$output = $args['before_paging'] . $output;
112
			}
113
114
			if ( ! empty( $args['after_paging'] ) ) {
115
				$output = $output . $args['after_paging'];
116
			}
117
		}
118
119
		return $output;
120
	}
121
122
}