Passed
Push — master ( c16653...06c25c )
by Brian
04:36
created

AUI_Component_Helper::element_require()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 2
nc 2
nop 1
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){
0 ignored issues
show
Unused Code introduced by
The parameter $multiple is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

21
	public static function name($text,/** @scrutinizer ignore-unused */ $multiple = false){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
		$output = '';
23
24
		if($text){
25
			$is_multiple = strpos($text, '[]') !== false ? '[]' : '';
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
	 * @param array $extra_attributes An array of extra attributes.
172
	 *
173
	 * @return string
174
	 */
175
	public static function icon($class,$space_after = false, $extra_attributes = array()){
176
		$output = '';
177
178
		if($class){
179
			$classes = self::esc_classes($class);
180
			if(!empty($classes)){
181
				$output = '<i class="'.$classes.'" ';
182
				// extra attributes
183
				if(!empty($extra_attributes)){
184
					$output .= AUI_Component_Helper::extra_attributes($extra_attributes);
185
				}
186
				$output .= '></i>';
187
				if($space_after){
188
					$output .= " ";
189
				}
190
			}
191
		}
192
193
		return $output;
194
	}
195
196
	/**
197
	 * @param $args
198
	 *
199
	 * @return string
200
	 */
201
	public static function extra_attributes($args){
202
		$output = '';
203
204
		if(!empty($args)){
205
206
			if( is_array($args) ){
207
				foreach($args as $key => $val){
208
					$output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" ';
209
				}
210
			}else{
211
				$output .= ' '.$args.' ';
212
			}
213
214
		}
215
216
		return $output;
217
	}
218
219
	/**
220
	 * @param $args
221
	 *
222
	 * @return string
223
	 */
224
	public static function help_text($text){
225
		$output = '';
226
227
		if($text){
228
			$output .= '<small class="form-text text-muted">'.wp_kses_post($text).'</small>';
229
		}
230
231
232
		return $output;
233
	}
234
235
	/**
236
	 * Replace element require context with JS.
237
	 *
238
	 * @param $input
239
	 *
240
	 * @return string|void
241
	 */
242
	public static function element_require( $input ) {
243
244
		$input = str_replace( "'", '"', $input );// we only want double quotes
245
246
		$output = esc_attr( str_replace( array( "[%", "%]", "%:checked]" ), array(
247
			"jQuery(form).find('[data-argument=\"",
248
			"\"]').find('input,select,textarea').val()",
249
			"\"]').find('input:checked').val()",
250
		), $input ) );
251
252
		if($output){
253
			$output = ' data-element-require="'.$output.'" ';
254
		}
255
256
		return $output;
257
	}
258
259
}