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

AUI_Component_Helper   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 57
c 1
b 0
f 1
dl 0
loc 194
rs 9.92
wmc 31

10 Methods

Rating   Name   Duplication   Size   Complexity  
A data_attributes() 0 13 4
A name() 0 9 3
A icon() 0 14 4
A id() 0 8 2
A esc_classes() 0 13 3
A title() 0 8 2
A aria_attributes() 0 13 4
A extra_attributes() 0 11 4
A class_attr() 0 11 3
A value() 0 8 2
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * A class for helping render common component items.
9
 *
10
 * @since 1.0.0
11
 */
12
class AUI_Component_Helper {
13
14
	/**
15
	 * A component helper for generating a input name.
16
	 *
17
	 * @param $text
18
	 *
19
	 * @return string
20
	 */
21
	public static function name($text,$multiple = false){
22
		$output = '';
23
24
		if($text){
25
			$is_multiple = $multiple ? '[]' : '';
26
			$output = ' name="'.sanitize_html_class($text).$is_multiple.'" ';
27
		}
28
29
		return $output;
30
	}
31
32
	/**
33
	 * A component helper for generating a item id.
34
	 *
35
	 * @param $text string The text to be used as the value.
36
	 *
37
	 * @return string The sanitized item.
38
	 */
39
	public static function id($text){
40
		$output = '';
41
42
		if($text){
43
			$output = ' id="'.sanitize_html_class($text).'" ';
44
		}
45
46
		return $output;
47
	}
48
49
	/**
50
	 * A component helper for generating a item title.
51
	 *
52
	 * @param $text string The text to be used as the value.
53
	 *
54
	 * @return string The sanitized item.
55
	 */
56
	public static function title($text){
57
		$output = '';
58
59
		if($text){
60
			$output = ' title="'.esc_attr($text).'" ';
61
		}
62
63
		return $output;
64
	}
65
66
	/**
67
	 * A component helper for generating a item value.
68
	 *
69
	 * @param $text string The text to be used as the value.
70
	 *
71
	 * @return string The sanitized item.
72
	 */
73
	public static function value($text){
74
		$output = '';
75
76
		if($text){
77
			$output = ' value="'.sanitize_text_field($text).'" ';
78
		}
79
80
		return $output;
81
	}
82
83
	/**
84
	 * A component helper for generating a item class attribute.
85
	 *
86
	 * @param $text string The text to be used as the value.
87
	 *
88
	 * @return string The sanitized item.
89
	 */
90
	public static function class_attr($text){
91
		$output = '';
92
93
		if($text){
94
			$classes = self::esc_classes($text);
95
			if(!empty($classes)){
96
				$output = ' class="'.$classes.'" ';
97
			}
98
		}
99
100
		return $output;
101
	}
102
103
	/**
104
	 * Escape a string of classes.
105
	 *
106
	 * @param $text
107
	 *
108
	 * @return string
109
	 */
110
	public static function esc_classes($text){
111
		$output = '';
112
113
		if($text){
114
			$classes = explode(" ",$text);
115
			$classes = array_map("trim",$classes);
116
			$classes = array_map("sanitize_html_class",$classes);
117
			if(!empty($classes)){
118
				$output = implode(" ",$classes);
119
			}
120
		}
121
122
		return $output;
123
124
	}
125
126
	/**
127
	 * @param $args
128
	 *
129
	 * @return string
130
	 */
131
	public static function data_attributes($args){
132
		$output = '';
133
134
		if(!empty($args)){
135
136
			foreach($args as $key => $val){
137
				if(substr( $key, 0, 5 ) === "data-"){
138
					$output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" ';
139
				}
140
			}
141
		}
142
143
		return $output;
144
	}
145
146
	/**
147
	 * @param $args
148
	 *
149
	 * @return string
150
	 */
151
	public static function aria_attributes($args){
152
		$output = '';
153
154
		if(!empty($args)){
155
156
			foreach($args as $key => $val){
157
				if(substr( $key, 0, 5 ) === "aria-"){
158
					$output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" ';
159
				}
160
			}
161
		}
162
163
		return $output;
164
	}
165
166
	/**
167
	 * Build a font awesome icon from a class.
168
	 * 
169
	 * @param $class
170
	 * @param bool $space_after
171
	 *
172
	 * @return string
173
	 */
174
	public static function icon($class,$space_after = false){
175
		$output = '';
176
177
		if($class){
178
			$classes = self::esc_classes($class);
179
			if(!empty($classes)){
180
				$output = '<i class="'.$classes.'" ></i>';
181
				if($space_after){
182
					$output .= " ";
183
				}
184
			}
185
		}
186
187
		return $output;
188
	}
189
190
	/**
191
	 * @param $args
192
	 *
193
	 * @return string
194
	 */
195
	public static function extra_attributes($args){
196
		$output = '';
197
198
		if(!empty($args) && is_array($args) ){
199
200
			foreach($args as $key => $val){
201
				$output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" ';
202
			}
203
		}
204
205
		return $output;
206
	}
207
208
}