Total Complexity | 67 |
Total Lines | 485 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AUI_Component_Helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AUI_Component_Helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class AUI_Component_Helper { |
||
13 | |||
14 | /** |
||
15 | * A component helper for generating a input name. |
||
16 | * |
||
17 | * @param $text |
||
18 | * @param $multiple bool If the name is set to be multiple but no brackets found then we add some. |
||
19 | * |
||
20 | * @return string |
||
21 | */ |
||
22 | public static function name( $text, $multiple = false ) { |
||
23 | $output = ''; |
||
24 | |||
25 | if ( $text ) { |
||
26 | $is_multiple = strpos( $text, '[' ) === false && $multiple ? '[]' : ''; |
||
27 | $output = ' name="' . esc_attr( $text ) . $is_multiple . '" '; |
||
28 | } |
||
29 | |||
30 | return $output; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * A component helper for generating a item id. |
||
35 | * |
||
36 | * @param $text string The text to be used as the value. |
||
37 | * |
||
38 | * @return string The sanitized item. |
||
39 | */ |
||
40 | public static function id( $text ) { |
||
41 | $output = ''; |
||
42 | |||
43 | if ( $text ) { |
||
44 | $output = ' id="' . sanitize_html_class( $text ) . '" '; |
||
45 | } |
||
46 | |||
47 | return $output; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * A component helper for generating a item title. |
||
52 | * |
||
53 | * @param $text string The text to be used as the value. |
||
54 | * |
||
55 | * @return string The sanitized item. |
||
56 | */ |
||
57 | public static function title( $text ) { |
||
58 | $output = ''; |
||
59 | |||
60 | if ( $text ) { |
||
61 | $output = ' title="' . esc_attr( $text ) . '" '; |
||
62 | } |
||
63 | |||
64 | return $output; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * A component helper for generating a item value. |
||
69 | * |
||
70 | * @param $text string The text to be used as the value. |
||
71 | * |
||
72 | * @return string The sanitized item. |
||
73 | */ |
||
74 | public static function value( $text ) { |
||
75 | $output = ''; |
||
76 | |||
77 | if ( $text !== null && $text !== false ) { |
||
78 | $output = ' value="' . esc_attr( wp_unslash( $text ) ) . '" '; |
||
|
|||
79 | } |
||
80 | |||
81 | return $output; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * A component helper for generating a item class attribute. |
||
86 | * |
||
87 | * @param $text string The text to be used as the value. |
||
88 | * |
||
89 | * @return string The sanitized item. |
||
90 | */ |
||
91 | public static function class_attr( $text ) { |
||
92 | $output = ''; |
||
93 | |||
94 | if ( $text ) { |
||
95 | $classes = self::esc_classes( $text ); |
||
96 | if ( ! empty( $classes ) ) { |
||
97 | $output = ' class="' . $classes . '" '; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return $output; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Escape a string of classes. |
||
106 | * |
||
107 | * @param $text |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public static function esc_classes( $text ) { |
||
112 | $output = ''; |
||
113 | |||
114 | if ( $text ) { |
||
115 | $classes = explode( " ", $text ); |
||
116 | $classes = array_map( "trim", $classes ); |
||
117 | $classes = array_map( "sanitize_html_class", $classes ); |
||
118 | if ( ! empty( $classes ) ) { |
||
119 | $output = implode( " ", $classes ); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return $output; |
||
124 | |||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param $args |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public static function data_attributes( $args ) { |
||
133 | $output = ''; |
||
134 | |||
135 | if ( ! empty( $args ) ) { |
||
136 | |||
137 | foreach ( $args as $key => $val ) { |
||
138 | if ( substr( $key, 0, 5 ) === "data-" ) { |
||
139 | $output .= ' ' . sanitize_html_class( $key ) . '="' . esc_attr( $val ) . '" '; |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | return $output; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param $args |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public static function aria_attributes( $args ) { |
||
153 | $output = ''; |
||
154 | |||
155 | if ( ! empty( $args ) ) { |
||
156 | |||
157 | foreach ( $args as $key => $val ) { |
||
158 | if ( substr( $key, 0, 5 ) === "aria-" ) { |
||
159 | $output .= ' ' . sanitize_html_class( $key ) . '="' . esc_attr( $val ) . '" '; |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | return $output; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Build a font awesome icon from a class. |
||
169 | * |
||
170 | * @param $class |
||
171 | * @param bool $space_after |
||
172 | * @param array $extra_attributes An array of extra attributes. |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public static function icon( $class, $space_after = false, $extra_attributes = array() ) { |
||
177 | $output = ''; |
||
178 | |||
179 | if ( $class ) { |
||
180 | $classes = self::esc_classes( $class ); |
||
181 | if ( ! empty( $classes ) ) { |
||
182 | $output = '<i class="' . $classes . '" '; |
||
183 | // extra attributes |
||
184 | if ( ! empty( $extra_attributes ) ) { |
||
185 | $output .= AUI_Component_Helper::extra_attributes( $extra_attributes ); |
||
186 | } |
||
187 | $output .= '></i>'; |
||
188 | if ( $space_after ) { |
||
189 | $output .= " "; |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | return $output; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param $args |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | public static function extra_attributes( $args ) { |
||
203 | $output = ''; |
||
204 | |||
205 | if ( ! empty( $args ) ) { |
||
206 | |||
207 | if ( is_array( $args ) ) { |
||
208 | foreach ( $args as $key => $val ) { |
||
209 | $output .= ' ' . sanitize_html_class( $key ) . '="' . esc_attr( $val ) . '" '; |
||
210 | } |
||
211 | } else { |
||
212 | $output .= ' ' . $args . ' '; |
||
213 | } |
||
214 | |||
215 | } |
||
216 | |||
217 | return $output; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param $args |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | public static function help_text( $text ) { |
||
226 | $output = ''; |
||
227 | |||
228 | if ( $text ) { |
||
229 | $output .= '<small class="form-text text-muted d-block">' . wp_kses_post( $text ) . '</small>'; |
||
230 | } |
||
231 | |||
232 | |||
233 | return $output; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Replace element require context with JS. |
||
238 | * |
||
239 | * @param $input |
||
240 | * |
||
241 | * @return string|void |
||
242 | */ |
||
243 | public static function element_require( $input ) { |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Navigates through an array, object, or scalar, and removes slashes from the values. |
||
262 | * |
||
263 | * @since 0.1.41 |
||
264 | * |
||
265 | * @param mixed $value The value to be stripped. |
||
266 | * @param array $input Input Field. |
||
267 | * |
||
268 | * @return mixed Stripped value. |
||
269 | */ |
||
270 | public static function sanitize_html_field( $value, $input = array() ) { |
||
271 | $original = $value; |
||
272 | |||
273 | if ( is_array( $value ) ) { |
||
274 | foreach ( $value as $index => $item ) { |
||
275 | $value[ $index ] = self::_sanitize_html_field( $value, $input ); |
||
276 | } |
||
277 | } elseif ( is_object( $value ) ) { |
||
278 | $object_vars = get_object_vars( $value ); |
||
279 | |||
280 | foreach ( $object_vars as $property_name => $property_value ) { |
||
281 | $value->$property_name = self::_sanitize_html_field( $property_value, $input ); |
||
282 | } |
||
283 | } else { |
||
284 | $value = self::_sanitize_html_field( $value, $input ); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Filters content and keeps only allowable HTML elements. |
||
289 | * |
||
290 | * @since 0.1.41 |
||
291 | * |
||
292 | * @param string|array $value Content to filter through kses. |
||
293 | * @param string|array $value Original content without filter. |
||
294 | * @param array $input Input Field. |
||
295 | */ |
||
296 | return apply_filters( 'ayecode_ui_sanitize_html_field', $value, $original, $input ); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Filters content and keeps only allowable HTML elements. |
||
301 | * |
||
302 | * This function makes sure that only the allowed HTML element names, attribute |
||
303 | * names and attribute values plus only sane HTML entities will occur in |
||
304 | * $string. You have to remove any slashes from PHP's magic quotes before you |
||
305 | * call this function. |
||
306 | * |
||
307 | * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news', |
||
308 | * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This |
||
309 | * covers all common link protocols, except for 'javascript' which should not |
||
310 | * be allowed for untrusted users. |
||
311 | * |
||
312 | * @since 0.1.41 |
||
313 | * |
||
314 | * @param string|array $value Content to filter through kses. |
||
315 | * @param array $input Input Field. |
||
316 | * |
||
317 | * @return string Filtered content with only allowed HTML elements. |
||
318 | */ |
||
319 | public static function _sanitize_html_field( $value, $input = array() ) { |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Returns an array of allowed HTML tags and attributes for a given context. |
||
339 | * |
||
340 | * @since 0.1.41 |
||
341 | * |
||
342 | * @param string|array $context The context for which to retrieve tags. Allowed values are 'post', |
||
343 | * 'strip', 'data', 'entities', or the name of a field filter such as |
||
344 | * 'pre_user_description'. |
||
345 | * @param array $input Input. |
||
346 | * |
||
347 | * @return array Array of allowed HTML tags and their allowed attributes. |
||
348 | */ |
||
349 | public static function kses_allowed_html( $context = 'post', $input = array() ) { |
||
384 | } |
||
385 | |||
386 | public static function get_column_class( $label_number = 2, $type = 'label' ) { |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Sanitizes a multiline string from user input or from the database. |
||
408 | * |
||
409 | * Emulate the WP native sanitize_textarea_field function in a %%variable%% safe way. |
||
410 | * |
||
411 | * @see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php for the original |
||
412 | * |
||
413 | * @since 0.1.66 |
||
414 | * |
||
415 | * @param string $str String to sanitize. |
||
416 | * @return string Sanitized string. |
||
417 | */ |
||
418 | public static function sanitize_textarea_field( $str ) { |
||
419 | $filtered = self::_sanitize_text_fields( $str, true ); |
||
420 | |||
421 | /** |
||
422 | * Filters a sanitized textarea field string. |
||
423 | * |
||
424 | * @see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/formatting.php |
||
425 | * |
||
426 | * @param string $filtered The sanitized string. |
||
427 | * @param string $str The string prior to being sanitized. |
||
428 | */ |
||
429 | return apply_filters( 'sanitize_textarea_field', $filtered, $str ); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Internal helper function to sanitize a string from user input or from the db. |
||
434 | * |
||
435 | * @since 0.1.66 |
||
436 | * @access private |
||
437 | * |
||
438 | * @param string $str String to sanitize. |
||
439 | * @param bool $keep_newlines Optional. Whether to keep newlines. Default: false. |
||
440 | * @return string Sanitized string. |
||
441 | */ |
||
442 | public static function _sanitize_text_fields( $str, $keep_newlines = false ) { |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Sanitize FontAwesome icon. |
||
483 | * |
||
484 | * @param string $icon Icon string. |
||
485 | * @return string Sanitized icon. |
||
486 | */ |
||
487 | public static function sanitize_fa_icon( $icon ) { |
||
497 | } |
||
498 | } |