Passed
Push — master ( 20b97c...4cbe1f )
by Stiofan
05:37
created

AUI::render()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 10
nc 4
nop 2
dl 0
loc 16
rs 8.8333
c 0
b 0
f 0
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.69';
28
29
	public static $options = null;
30
31
	/**
32
	 * There can be only one.
33
	 *
34
	 * @since 1.0.0
35
	 * @return AUI|null
36
	 */
37
	public static function instance() {
38
		if ( self::$instance == null ) {
39
			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...
40
		}
41
42
		return self::$instance;
43
	}
44
45
	/**
46
	 * AUI constructor.
47
	 *
48
	 * @since 1.0.0
49
	 */
50
	private function __construct() {
51
		if ( function_exists( "__autoload" ) ) {
52
			spl_autoload_register( "__autoload" );
53
		}
54
		spl_autoload_register( array( $this, 'autoload' ) );
55
56
		// load options
57
		self::$options = get_option('aui_options');
58
	}
59
60
	/**
61
	 * Autoload any components on the fly.
62
	 *
63
	 * @since 1.0.0
64
	 *
65
	 * @param $classname
66
	 */
67
	private function autoload( $classname ) {
68
		$class     = str_replace( '_', '-', strtolower( $classname ) );
69
		$file_path = trailingslashit( dirname( __FILE__ ) ) . "components/class-" . $class . '.php';
70
		if ( $file_path && is_readable( $file_path ) ) {
71
			include_once( $file_path );
72
		}
73
	}
74
75
	/**
76
	 * Get the AUI options.
77
	 *
78
	 * @param $option
79
	 *
80
	 * @return string|void
81
	 */
82
	public function get_option( $option ){
83
		$result = isset(self::$options[$option]) ? esc_attr(self::$options[$option]) : '';
84
85
		if ( ! $result && $option) {
86
			if( $option == 'color_primary' ){
87
				$result = AUI_PRIMARY_COLOR;
88
			}elseif( $option == 'color_secondary' ){
89
				$result = AUI_SECONDARY_COLOR;
90
			}
91
		}
92
		return $result;
93
	}
94
95
	public function render( $items = array(), $echo = false ) {
96
		$output = '';
97
98
		if ( ! empty( $items ) ) {
99
			foreach ( $items as $args ) {
100
				$render = isset( $args['render'] ) ? $args['render'] : '';
101
				if ( $render && method_exists( __CLASS__, $render ) ) {
102
					$output .= $this->$render( $args );
103
				}
104
			}
105
		}
106
107
		if ( $echo ) {
108
			echo $output;
109
		}else{
110
			return $output;
111
		}
112
113
	}
114
115
	/**
116
	 * Render and return a bootstrap alert component.
117
	 *
118
	 * @since 1.0.0
119
	 *
120
	 * @param array $args The function arguments.
121
	 * @param bool  $echo If we should return or echo.
122
	 *
123
	 * @return string The rendered component.
124
	 */
125
	public function alert( $args = array(), $echo = false ) {
126
		$output = AUI_Component_Alert::get( $args );
127
128
		if ( $echo ) {
129
			echo $output;
130
		}else{
131
			return $output;
132
		}
133
	}
134
135
	/**
136
	 * Render and return a bootstrap input component.
137
	 *
138
	 * @since 1.0.0
139
	 *
140
	 * @param array $args The function arguments.
141
	 * @param bool  $echo If we should return or echo.
142
	 *
143
	 * @return string The rendered component.
144
	 */
145
	public function input( $args = array(), $echo = false ) {
146
		$output = AUI_Component_Input::input( $args );
147
148
		if ( $echo ) {
149
			echo $output;
150
		}else{
151
			return $output;
152
		}
153
	}
154
155
	/**
156
	 * Render and return a bootstrap textarea component.
157
	 *
158
	 * @since 1.0.0
159
	 *
160
	 * @param array $args The function arguments.
161
	 * @param bool  $echo If we should return or echo.
162
	 *
163
	 * @return string The rendered component.
164
	 */
165
	public function textarea( $args = array(), $echo = false ) {
166
		$output = AUI_Component_Input::textarea( $args );
167
168
		if ( $echo ) {
169
			echo $output;
170
		}else{
171
			return $output;
172
		}
173
	}
174
175
	/**
176
	 * Render and return a bootstrap button component.
177
	 *
178
	 * @since 1.0.0
179
	 *
180
	 * @param array $args The function arguments.
181
	 * @param bool  $echo If we should return or echo.
182
	 *
183
	 * @return string The rendered component.
184
	 */
185
	public function button( $args = array(), $echo = false ) {
186
		$output = AUI_Component_Button::get( $args );
187
188
		if ( $echo ) {
189
			echo $output;
190
		}else{
191
			return $output;
192
		}
193
	}
194
195
	/**
196
	 * Render and return a bootstrap button component.
197
	 *
198
	 * @since 1.0.0
199
	 *
200
	 * @param array $args The function arguments.
201
	 * @param bool  $echo If we should return or echo.
202
	 *
203
	 * @return string The rendered component.
204
	 */
205
	public function badge( $args = array(), $echo = false ) {
206
		$defaults = array(
207
			'class' => 'badge badge-primary align-middle',
208
		);
209
210
		// maybe set type.
211
		if ( empty( $args['href'] ) ) {
212
			$defaults['type'] = 'badge';
213
		}
214
215
		/**
216
		 * Parse incoming $args into an array and merge it with $defaults
217
		 */
218
		$args = wp_parse_args( $args, $defaults );
219
220
		$output = AUI_Component_Button::get( $args );
221
222
		if ( $echo ) {
223
			echo $output;
224
		}else{
225
			return $output;
226
		}
227
	}
228
229
	/**
230
	 * Render and return a bootstrap dropdown component.
231
	 *
232
	 * @since 1.0.0
233
	 *
234
	 * @param array $args The function arguments.
235
	 * @param bool  $echo If we should return or echo.
236
	 *
237
	 * @return string The rendered component.
238
	 */
239
	public function dropdown( $args = array(), $echo = false ) {
240
		$output = AUI_Component_Dropdown::get( $args );
241
242
		if ( $echo ) {
243
			echo $output;
244
		}else{
245
			return $output;
246
		}
247
	}
248
249
	/**
250
	 * Render and return a bootstrap select component.
251
	 *
252
	 * @since 1.0.0
253
	 *
254
	 * @param array $args The function arguments.
255
	 * @param bool  $echo If we should return or echo.
256
	 *
257
	 * @return string The rendered component.
258
	 */
259
	public function select( $args = array(), $echo = false ) {
260
		$output = AUI_Component_Input::select( $args );
261
262
		if ( $echo ) {
263
			echo $output;
264
		}else{
265
			return $output;
266
		}
267
	}
268
269
	/**
270
	 * Render and return a bootstrap radio component.
271
	 *
272
	 * @since 1.0.0
273
	 *
274
	 * @param array $args The function arguments.
275
	 * @param bool  $echo If we should return or echo.
276
	 *
277
	 * @return string The rendered component.
278
	 */
279
	public function radio( $args = array(), $echo = false ) {
280
		$output = AUI_Component_Input::radio( $args );
281
282
		if ( $echo ) {
283
			echo $output;
284
		}else{
285
			return $output;
286
		}
287
	}
288
289
	/**
290
	 * Render and return a bootstrap pagination component.
291
	 *
292
	 * @since 1.0.0
293
	 *
294
	 * @param array $args The function arguments.
295
	 * @param bool  $echo If we should return or echo.
296
	 *
297
	 * @return string The rendered component.
298
	 */
299
	public function pagination( $args = array(), $echo = false ) {
300
		$output = AUI_Component_Pagination::get( $args );
301
302
		if ( $echo ) {
303
			echo $output;
304
		}else{
305
			return $output;
306
		}
307
	}
308
309
}