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

AUI   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 33
c 1
b 0
f 1
dl 0
loc 189
rs 10
wmc 22

12 Methods

Rating   Name   Duplication   Size   Complexity  
A select() 0 2 1
A button() 0 2 1
A autoload() 0 5 3
A dropdown() 0 2 1
A pagination() 0 2 1
A input() 0 2 1
A alert() 0 2 1
A badge() 0 16 2
A __construct() 0 5 2
A textarea() 0 2 1
A instance() 0 6 2
A render() 0 13 6
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * A singleton class to output AyeCode UI Components.
9
 *
10
 * @since 1.0.0
11
 */
12
class AUI {
13
14
	/**
15
	 * Holds the class instance.
16
	 *
17
	 * @since 1.0.0
18
	 * @var null
19
	 */
20
	private static $instance = null;
21
22
	/**
23
	 * Holds the current AUI version number.
24
	 *
25
	 * @var string $ver The current version number.
26
	 */
27
	public static $ver = '0.1.11';
28
29
	/**
30
	 * There can be only one.
31
	 *
32
	 * @since 1.0.0
33
	 * @return AUI|null
34
	 */
35
	public static function instance() {
36
		if ( self::$instance == null ) {
37
			self::$instance = new AUI();
0 ignored issues
show
Documentation Bug introduced by
It seems like new AUI() of type AUI is incompatible with the declared type null of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
		}
39
40
		return self::$instance;
41
	}
42
43
	/**
44
	 * AUI constructor.
45
	 *
46
	 * @since 1.0.0
47
	 */
48
	private function __construct() {
49
		if ( function_exists( "__autoload" ) ) {
50
			spl_autoload_register( "__autoload" );
51
		}
52
		spl_autoload_register( array( $this, 'autoload' ) );
53
	}
54
55
	/**
56
	 * Autoload any components on the fly.
57
	 *
58
	 * @since 1.0.0
59
	 *
60
	 * @param $classname
61
	 */
62
	private function autoload( $classname ) {
63
		$class     = str_replace( '_', '-', strtolower( $classname ) );
64
		$file_path = trailingslashit( dirname( __FILE__ ) ) . "components/class-" . $class . '.php';
65
		if ( $file_path && is_readable( $file_path ) ) {
66
			include_once( $file_path );
67
		}
68
	}
69
70
	public function render( $items = array() ) {
71
		$output = '';
72
73
		if ( ! empty( $items ) ) {
74
			foreach ( $items as $args ) {
75
				$render = isset( $args['render'] ) ? $args['render'] : '';
76
				if ( $render && method_exists( __CLASS__, $render ) ) {
77
					$output .= $this->$render( $args );
78
				}
79
			}
80
		}
81
82
		return $output;
83
	}
84
85
	/**
86
	 * Render and return a bootstrap alert component.
87
	 *
88
	 * @since 1.0.0
89
	 *
90
	 * @param array $args
91
	 *
92
	 * @return string The rendered component.
93
	 */
94
	public function alert( $args = array() ) {
95
		return AUI_Component_Alert::get( $args );
96
	}
97
98
	/**
99
	 * Render and return a bootstrap input component.
100
	 *
101
	 * @since 1.0.0
102
	 *
103
	 * @param array $args
104
	 *
105
	 * @return string The rendered component.
106
	 */
107
	public function input( $args = array() ) {
108
		return AUI_Component_Input::input( $args );
109
	}
110
111
	/**
112
	 * Render and return a bootstrap textarea component.
113
	 *
114
	 * @since 1.0.0
115
	 *
116
	 * @param array $args
117
	 *
118
	 * @return string The rendered component.
119
	 */
120
	public function textarea( $args = array() ) {
121
		return AUI_Component_Input::textarea( $args );
122
	}
123
124
	/**
125
	 * Render and return a bootstrap button component.
126
	 *
127
	 * @since 1.0.0
128
	 *
129
	 * @param array $args
130
	 *
131
	 * @return string The rendered component.
132
	 */
133
	public function button( $args = array() ) {
134
		return AUI_Component_Button::get( $args );
135
	}
136
137
	/**
138
	 * Render and return a bootstrap button component.
139
	 *
140
	 * @since 1.0.0
141
	 *
142
	 * @param array $args
143
	 *
144
	 * @return string The rendered component.
145
	 */
146
	public function badge( $args = array() ) {
147
		$defaults = array(
148
			'class' => 'badge badge-primary align-middle',
149
		);
150
151
		// maybe set type
152
		if ( empty( $args['href'] ) ) {
153
			$defaults['type'] = 'badge';
154
		}
155
156
		/**
157
		 * Parse incoming $args into an array and merge it with $defaults
158
		 */
159
		$args = wp_parse_args( $args, $defaults );
160
161
		return AUI_Component_Button::get( $args );
162
	}
163
164
	/**
165
	 * Render and return a bootstrap dropdown component.
166
	 *
167
	 * @since 1.0.0
168
	 *
169
	 * @param array $args
170
	 *
171
	 * @return string The rendered component.
172
	 */
173
	public function dropdown( $args = array() ) {
174
		return AUI_Component_Dropdown::get( $args );
175
	}
176
177
	/**
178
	 * Render and return a bootstrap select component.
179
	 *
180
	 * @since 1.0.0
181
	 *
182
	 * @param array $args
183
	 *
184
	 * @return string The rendered component.
185
	 */
186
	public function select( $args = array() ) {
187
		return AUI_Component_Input::select( $args );
188
	}
189
190
	/**
191
	 * Render and return a bootstrap pagination component.
192
	 *
193
	 * @since 1.0.0
194
	 *
195
	 * @param array $args
196
	 *
197
	 * @return string The rendered component.
198
	 */
199
	public function pagination( $args = array() ) {
200
		return AUI_Component_Pagination::get( $args );
201
	}
202
203
}