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

AUI_Component_Dropdown   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 32
c 1
b 0
f 1
dl 0
loc 58
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A get() 0 49 4
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * A component class for rendering a bootstrap dropdown.
9
 *
10
 * @since 1.0.0
11
 */
12
class AUI_Component_Dropdown {
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
		$defaults = array(
23
			'type'       => 'button',
24
			'href'       => '#',
25
			'class'      => 'btn btn-primary dropdown-toggle',
26
			'wrapper_class' => '',
27
			'dropdown_menu_class' => '',
28
			'id'         => '',
29
			'title'      => '',
30
			'value'      => '',
31
			'content'    => '',
32
			'icon'       => '',
33
			'hover_content' => '',
34
			'hover_icon'    => '',
35
			'data-toggle'   => 'dropdown',
36
			'aria-haspopup' => 'true',
37
			'aria-expanded' => 'false',
38
			'dropdown_menu'          => '', // unescaped html menu (non-preferred way)
39
			'dropdown_items'          => array(), // array of AUI calls
40
41
		);
42
43
		/**
44
		 * Parse incoming $args into an array and merge it with $defaults
45
		 */
46
		$args   = wp_parse_args( $args, $defaults );
47
		$output = '';
48
		if ( ! empty( $args['type'] ) ) {
49
			// wrapper open
50
			$output .= '<div class="dropdown '.AUI_Component_Helper::esc_classes($args['wrapper_class']).'">';
51
52
			// button part
53
			$output .= aui()->button($args);
54
55
			// dropdown-menu
56
			if(!empty($args['dropdown_menu'])){
57
				$output .= $args['dropdown_menu'];
58
			}elseif(!empty($args['dropdown_items'])){
59
				$output .= '<div class="dropdown-menu '.AUI_Component_Helper::esc_classes($args['dropdown_menu_class']).'" aria-labelledby="'.sanitize_html_class($args['id']).'">';
60
				$output .= aui()->render($args['dropdown_items']);
61
				$output .= '</div>';
62
			}
63
64
			// wrapper close
65
			$output .= '</div>';
66
67
		}
68
69
		return $output;
70
	}
71
72
}