| Total Complexity | 270 | 
| Total Lines | 1279 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like AUI_Component_Input 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_Input, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 12 | class AUI_Component_Input { | ||
| 13 | |||
| 14 | /** | ||
| 15 | * Build the component. | ||
| 16 | * | ||
| 17 | * @param array $args | ||
| 18 | * | ||
| 19 | * @return string The rendered component. | ||
| 20 | */ | ||
| 21 | 	public static function input( $args = array() ) { | ||
| 22 | global $aui_bs5; | ||
| 23 | |||
| 24 | $defaults = array( | ||
| 25 | 'type' => 'text', | ||
| 26 | 'name' => '', | ||
| 27 | 'class' => '', | ||
| 28 | 'wrap_class' => '', | ||
| 29 | 'id' => '', | ||
| 30 | 'placeholder' => '', | ||
| 31 | 'title' => '', | ||
| 32 | 'value' => '', | ||
| 33 | 'required' => false, | ||
| 34 | 'size' => '', // sm, lg, small, large | ||
| 35 | 'clear_icon' => '', // true will show a clear icon, can't be used with input_group_right | ||
| 36 | 'with_hidden' => false, // Append hidden field for single checkbox. | ||
| 37 | 'label' => '', | ||
| 38 | 'label_after' => false, | ||
| 39 | 'label_class' => '', | ||
| 40 | 'label_col' => '2', | ||
| 41 | 'label_type' => '', // top, horizontal, empty = hidden | ||
| 42 | 'label_force_left' => false, // used to force checkbox label left when using horizontal | ||
| 43 | // sets the label type, default: hidden. Options: hidden, top, horizontal, floating | ||
| 44 | 'help_text' => '', | ||
| 45 | 'validation_text' => '', | ||
| 46 | 'validation_pattern' => '', | ||
| 47 | 'no_wrap' => false, | ||
| 48 | 'input_group_right' => '', | ||
| 49 | 'input_group_left' => '', | ||
| 50 | 'input_group_right_inside' => false, | ||
| 51 | // forces the input group inside the input | ||
| 52 | 'input_group_left_inside' => false, | ||
| 53 | // forces the input group inside the input | ||
| 54 | 'form_group_class' => '', | ||
| 55 | 'step' => '', | ||
| 56 | 'switch' => false, | ||
| 57 | // to show checkbox as a switch | ||
| 58 | 'checked' => false, | ||
| 59 | // set a checkbox or radio as selected | ||
| 60 | 'password_toggle' => true, | ||
| 61 | // toggle view/hide password | ||
| 62 | 'element_require' => '', | ||
| 63 | // [%element_id%] == "1" | ||
| 64 | 'extra_attributes' => array(), | ||
| 65 | // an array of extra attributes | ||
| 66 | 'wrap_attributes' => array() | ||
| 67 | ); | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Parse incoming $args into an array and merge it with $defaults | ||
| 71 | */ | ||
| 72 | $args = wp_parse_args( $args, $defaults ); | ||
| 73 | $output = ''; | ||
| 74 | 		if ( ! empty( $args['type'] ) ) { | ||
| 75 | // hidden label option needs to be empty | ||
| 76 | $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; | ||
| 77 | |||
| 78 | $type = sanitize_html_class( $args['type'] ); | ||
| 79 | |||
| 80 | $help_text = ''; | ||
| 81 | $label = ''; | ||
| 82 | $label_after = $args['label_after']; | ||
| 83 | $label_args = array( | ||
| 84 | 'title' => $args['label'], | ||
| 85 | 'for' => $args['id'], | ||
| 86 | 'class' => $args['label_class'] . " ", | ||
| 87 | 'label_type' => $args['label_type'], | ||
| 88 | 'label_col' => $args['label_col'] | ||
| 89 | ); | ||
| 90 | |||
| 91 | // floating labels need label after | ||
| 92 | 			if ( $args['label_type'] == 'floating' && $type != 'checkbox' ) { | ||
| 93 | $label_after = true; | ||
| 94 | $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. | ||
| 95 | } | ||
| 96 | |||
| 97 | // size | ||
| 98 | $size = ''; | ||
| 99 | 			if ( $args['size'] == 'lg' || $args['size'] == 'large' ) { | ||
| 100 | $size = 'lg'; | ||
| 101 | $args['class'] .= ' form-control-lg'; | ||
| 102 | 			}elseif ( $args['size'] == 'sm' || $args['size'] == 'small' ) { | ||
| 103 | $size = 'sm'; | ||
| 104 | $args['class'] .= ' form-control-sm'; | ||
| 105 | } | ||
| 106 | |||
| 107 | // clear function | ||
| 108 | $clear_function = 'jQuery(this).parent().parent().find(\'input\').val(\'\');'; | ||
| 109 | |||
| 110 | // Some special sauce for files | ||
| 111 | 			if ( $type == 'file' ) { | ||
| 112 | $label_after = true; // if type file we need the label after | ||
| 113 | $args['class'] .= ' custom-file-input '; | ||
| 114 | 			} elseif ( $type == 'checkbox' ) { | ||
| 115 | $label_after = true; // if type file we need the label after | ||
| 116 | $args['class'] .= $aui_bs5 ? ' form-check-input' : ' custom-control-input '; | ||
| 117 | 			} elseif ( $type == 'datepicker' || $type == 'timepicker' ) { | ||
| 118 | $orig_type = $type; | ||
| 119 | $type = 'text'; | ||
| 120 | $args['class'] .= ' bg-initial '; // @todo not sure why we have this? | ||
| 121 | 				$clear_function .= "jQuery(this).parent().parent().find('input[name=\'" . esc_attr( $args['name'] ) . "\']').trigger('change');"; | ||
| 122 | |||
| 123 | $args['extra_attributes']['data-aui-init'] = 'flatpickr'; | ||
| 124 | |||
| 125 | // Disable native datetime inputs. | ||
| 126 | 				if ( ( $orig_type == 'timepicker' || ! empty( $args['extra_attributes']['data-enable-time'] ) ) && ! isset( $args['extra_attributes']['data-disable-mobile'] ) ) { | ||
| 127 | $args['extra_attributes']['data-disable-mobile'] = 'true'; | ||
| 128 | } | ||
| 129 | |||
| 130 | // set a way to clear field if empty | ||
| 131 | 				if ( $args['input_group_right'] === '' && $args['clear_icon'] !== false ) { | ||
| 132 | $args['input_group_right_inside'] = true; | ||
| 133 | $args['clear_icon'] = true; | ||
| 134 | } | ||
| 135 | |||
| 136 | // enqueue the script | ||
| 137 | $aui_settings = AyeCode_UI_Settings::instance(); | ||
| 138 | $aui_settings->enqueue_flatpickr(); | ||
| 139 | 			} elseif ( $type == 'iconpicker' ) { | ||
| 140 | $type = 'text'; | ||
| 141 | //$args['class'] .= ' aui-flatpickr bg-initial '; | ||
| 142 | // $args['class'] .= ' bg-initial '; | ||
| 143 | |||
| 144 | $args['extra_attributes']['data-aui-init'] = 'iconpicker'; | ||
| 145 | $args['extra_attributes']['data-placement'] = 'bottomRight'; | ||
| 146 | |||
| 147 | $args['input_group_right'] = '<span class="input-group-addon input-group-text c-pointer"></span>'; | ||
| 148 | // $args['input_group_right_inside'] = true; | ||
| 149 | // enqueue the script | ||
| 150 | $aui_settings = AyeCode_UI_Settings::instance(); | ||
| 151 | $aui_settings->enqueue_iconpicker(); | ||
| 152 | } | ||
| 153 | |||
| 154 | 			if ( $type == 'checkbox' && ( ( ! empty( $args['name'] ) && strpos( $args['name'], '[' ) === false ) || ! empty( $args['with_hidden'] ) ) ) { | ||
| 155 | $output .= '<input type="hidden" name="' . esc_attr( $args['name'] ) . '" value="0" />'; | ||
| 156 | } | ||
| 157 | |||
| 158 | // allow clear icon | ||
| 159 | 			if ( $args['input_group_right'] === '' && $args['clear_icon'] ) { | ||
| 160 | $font_size = $size == 'sm' ? '1.3' : ( $size == 'lg' ? '1.65' : '1.5' ); | ||
| 161 | $args['input_group_right_inside'] = true; | ||
| 162 | $align_class = $aui_bs5 ? ' h-100 py-0' : ''; | ||
| 163 | $args['input_group_right'] = '<span class="input-group-text aui-clear-input c-pointer bg-initial border-0 px-2 d-none ' . $align_class . '" onclick="' . $clear_function . '"><span style="font-size: '.$font_size.'rem" aria-hidden="true" class="close">×</span></span>'; | ||
| 164 | } | ||
| 165 | |||
| 166 | // open/type | ||
| 167 | $output .= '<input type="' . $type . '" '; | ||
| 168 | |||
| 169 | // name | ||
| 170 | 			if ( ! empty( $args['name'] ) ) { | ||
| 171 | $output .= ' name="' . esc_attr( $args['name'] ) . '" '; | ||
| 172 | } | ||
| 173 | |||
| 174 | // id | ||
| 175 | 			if ( ! empty( $args['id'] ) ) { | ||
| 176 | $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; | ||
| 177 | } | ||
| 178 | |||
| 179 | // placeholder | ||
| 180 | 			if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { | ||
| 181 | $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; | ||
| 182 | } | ||
| 183 | |||
| 184 | // title | ||
| 185 | 			if ( ! empty( $args['title'] ) ) { | ||
| 186 | $output .= ' title="' . esc_attr( $args['title'] ) . '" '; | ||
| 187 | } | ||
| 188 | |||
| 189 | // value | ||
| 190 | 			if ( ! empty( $args['value'] ) ) { | ||
| 191 | $output .= AUI_Component_Helper::value( $args['value'] ); | ||
| 192 | } | ||
| 193 | |||
| 194 | // checked, for radio and checkboxes | ||
| 195 | 			if ( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ) { | ||
| 196 | $output .= ' checked '; | ||
| 197 | } | ||
| 198 | |||
| 199 | // validation text | ||
| 200 | 			if ( ! empty( $args['validation_text'] ) ) { | ||
| 201 | $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; | ||
| 202 | 				$output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; | ||
| 203 | } | ||
| 204 | |||
| 205 | // validation_pattern | ||
| 206 | 			if ( ! empty( $args['validation_pattern'] ) ) { | ||
| 207 | $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; | ||
| 208 | } | ||
| 209 | |||
| 210 | // step (for numbers) | ||
| 211 | 			if ( ! empty( $args['step'] ) ) { | ||
| 212 | $output .= ' step="' . $args['step'] . '" '; | ||
| 213 | } | ||
| 214 | |||
| 215 | // required | ||
| 216 | 			if ( ! empty( $args['required'] ) ) { | ||
| 217 | $output .= ' required '; | ||
| 218 | } | ||
| 219 | |||
| 220 | // class | ||
| 221 | $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; | ||
| 222 | $output .= $aui_bs5 && $type == 'checkbox' ? ' class="' . $class . '" ' : ' class="form-control ' . $class . '" '; | ||
| 223 | |||
| 224 | // data-attributes | ||
| 225 | $output .= AUI_Component_Helper::data_attributes( $args ); | ||
| 226 | |||
| 227 | // extra attributes | ||
| 228 | 			if ( ! empty( $args['extra_attributes'] ) ) { | ||
| 229 | $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); | ||
| 230 | } | ||
| 231 | |||
| 232 | // close | ||
| 233 | $output .= ' >'; | ||
| 234 | |||
| 235 | // help text | ||
| 236 | 			if ( ! empty( $args['help_text'] ) ) { | ||
| 237 | $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); | ||
| 238 | } | ||
| 239 | |||
| 240 | // label | ||
| 241 | 			if ( ! empty( $args['label'] ) ) { | ||
| 242 | $label_base_class = ''; | ||
| 243 | 				if ( $type == 'file' ) { | ||
| 244 | $label_base_class = ' custom-file-label'; | ||
| 245 | 				} elseif ( $type == 'checkbox' ) { | ||
| 246 | 					if ( ! empty( $args['label_force_left'] ) ) { | ||
| 247 | $label_args['title'] = wp_kses_post( $args['help_text'] ); | ||
| 248 | $help_text = ''; | ||
| 249 | //$label_args['class'] .= ' d-inline '; | ||
| 250 | $args['wrap_class'] .= ' align-items-center '; | ||
| 251 | 					}else{ | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | $label_base_class = $aui_bs5 ? ' form-check-label' : ' custom-control-label'; | ||
| 256 | } | ||
| 257 | $label_args['class'] .= $label_base_class; | ||
| 258 | $temp_label_args = $label_args; | ||
| 259 | 				if(! empty( $args['label_force_left'] )){$temp_label_args['class'] = $label_base_class." text-muted";} | ||
| 260 | $label = self::label( $temp_label_args, $type ); | ||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | |||
| 266 | // set help text in the correct position | ||
| 267 | 			if ( $label_after ) { | ||
| 268 | $output .= $label . $help_text; | ||
| 269 | } | ||
| 270 | |||
| 271 | // some input types need a separate wrap | ||
| 272 | 			if ( $type == 'file' ) { | ||
| 273 | $output = self::wrap( array( | ||
| 274 | 'content' => $output, | ||
| 275 | 'class' => $aui_bs5 ? 'mb-3 custom-file' : 'form-group custom-file' | ||
| 276 | ) ); | ||
| 277 | 			} elseif ( $type == 'checkbox' ) { | ||
| 278 | |||
| 279 | $label_args['title'] = $args['label']; | ||
| 280 | $label_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ); | ||
| 281 | $label = !empty( $args['label_force_left'] ) ? self::label( $label_args, 'cb' ) : '<div class="' . $label_col . ' col-form-label"></div>'; | ||
| 282 | $switch_size_class = $args['switch'] && !is_bool( $args['switch'] ) ? ' custom-switch-'.esc_attr( $args['switch'] ) : ''; | ||
| 283 | 				if ( $aui_bs5 ) { | ||
| 284 | $wrap_class = $args['switch'] ? 'form-check form-switch' . $switch_size_class : 'form-check'; | ||
| 285 | 				}else{ | ||
| 286 | $wrap_class = $args['switch'] ? 'custom-switch' . $switch_size_class : 'custom-checkbox' ; | ||
| 287 | } | ||
| 288 | 				if ( ! empty( $args['label_force_left'] ) ) { | ||
| 289 | $wrap_class .= $aui_bs5 ? '' : ' d-flex align-content-center'; | ||
| 290 | 					$label = str_replace("form-check-label","", self::label( $label_args, 'cb' ) ); | ||
| 291 | } | ||
| 292 | $output = self::wrap( array( | ||
| 293 | 'content' => $output, | ||
| 294 | 'class' => $aui_bs5 ? $wrap_class : 'custom-control ' . $wrap_class | ||
| 295 | ) ); | ||
| 296 | |||
| 297 | 				if ( $args['label_type'] == 'horizontal' ) { | ||
| 298 | $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); | ||
| 299 | $output = $label . '<div class="' . $input_col . '">' . $output . '</div>'; | ||
| 300 | } | ||
| 301 | 			} elseif ( $type == 'password' && $args['password_toggle'] && ! $args['input_group_right'] ) { | ||
| 302 | |||
| 303 | |||
| 304 | // allow password field to toggle view | ||
| 305 | $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" | ||
| 306 | onclick="var $el = jQuery(this).find(\'i\');$el.toggleClass(\'fa-eye fa-eye-slash\'); | ||
| 307 | var $eli = jQuery(this).parent().parent().find(\'input\'); | ||
| 308 | if($el.hasClass(\'fa-eye\')) | ||
| 309 | {$eli.attr(\'type\',\'text\');} | ||
| 310 | else{$eli.attr(\'type\',\'password\');}" | ||
| 311 | ><i class="far fa-fw fa-eye-slash"></i></span>'; | ||
| 312 | } | ||
| 313 | |||
| 314 | // input group wraps | ||
| 315 | 			if ( $args['input_group_left'] || $args['input_group_right'] ) { | ||
| 316 | $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; | ||
| 317 | $group_size = $size == 'lg' ? ' input-group-lg' : ''; | ||
| 318 | $group_size = !$group_size && $size == 'sm' ? ' input-group-sm' : $group_size; | ||
| 319 | |||
| 320 | 				if ( $args['input_group_left'] ) { | ||
| 321 | $output = self::wrap( array( | ||
| 322 | 'content' => $output, | ||
| 323 | 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size, | ||
| 324 | 'input_group_left' => $args['input_group_left'], | ||
| 325 | 'input_group_left_inside' => $args['input_group_left_inside'] | ||
| 326 | ) ); | ||
| 327 | } | ||
| 328 | 				if ( $args['input_group_right'] ) { | ||
| 329 | $output = self::wrap( array( | ||
| 330 | 'content' => $output, | ||
| 331 | 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size, | ||
| 332 | 'input_group_right' => $args['input_group_right'], | ||
| 333 | 'input_group_right_inside' => $args['input_group_right_inside'] | ||
| 334 | ) ); | ||
| 335 | } | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | 			if ( ! $label_after ) { | ||
| 340 | $output .= $help_text; | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | 			if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { | ||
| 345 | $output = self::wrap( array( | ||
| 346 | 'content' => $output, | ||
| 347 | 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) | ||
| 348 | ) ); | ||
| 349 | } | ||
| 350 | |||
| 351 | 			if ( ! $label_after ) { | ||
| 352 | $output = $label . $output; | ||
| 353 | } | ||
| 354 | |||
| 355 | // wrap | ||
| 356 | 			if ( ! $args['no_wrap'] ) { | ||
| 357 | 				if ( ! empty( $args['form_group_class'] ) ) { | ||
| 358 | $fg_class = esc_attr( $args['form_group_class'] ); | ||
| 359 | 				}else{ | ||
| 360 | $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; | ||
| 361 | } | ||
| 362 | $form_group_class = $args['label_type'] == 'floating' && $type != 'checkbox' ? 'form-label-group' : $fg_class; | ||
| 363 | $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; | ||
| 364 | $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; | ||
| 365 | $output = self::wrap( array( | ||
| 366 | 'content' => $output, | ||
| 367 | 'class' => $wrap_class, | ||
| 368 | 'element_require' => $args['element_require'], | ||
| 369 | 'argument_id' => $args['id'], | ||
| 370 | 'wrap_attributes' => $args['wrap_attributes'], | ||
| 371 | ) ); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | return $output; | ||
| 376 | } | ||
| 377 | |||
| 378 | 	public static function label( $args = array(), $type = '' ) { | ||
| 440 | } | ||
| 441 | |||
| 442 | /** | ||
| 443 | * Wrap some content in a HTML wrapper. | ||
| 444 | * | ||
| 445 | * @param array $args | ||
| 446 | * | ||
| 447 | * @return string | ||
| 448 | */ | ||
| 449 | 	public static function wrap( $args = array() ) { | ||
| 527 | } | ||
| 528 | |||
| 529 | /** | ||
| 530 | * Build the component. | ||
| 531 | * | ||
| 532 | * @param array $args | ||
| 533 | * | ||
| 534 | * @return string The rendered component. | ||
| 535 | */ | ||
| 536 | 	public static function textarea( $args = array() ) { | ||
| 779 | } | ||
| 780 | |||
| 781 | /** | ||
| 782 | * Build the component. | ||
| 783 | * | ||
| 784 | * @param array $args | ||
| 785 | * | ||
| 786 | * @return string The rendered component. | ||
| 787 | */ | ||
| 788 | 	public static function select( $args = array() ) { | ||
| 789 | global $aui_bs5; | ||
| 790 | $defaults = array( | ||
| 791 | 'class' => '', | ||
| 792 | 'wrap_class' => '', | ||
| 793 | 'id' => '', | ||
| 794 | 'title' => '', | ||
| 795 | 'value' => '', | ||
| 796 | // can be an array or a string | ||
| 797 | 'required' => false, | ||
| 798 | 'label' => '', | ||
| 799 | 'label_after' => false, | ||
| 800 | 'label_type' => '', | ||
| 801 | 'label_col' => '', | ||
| 802 | // sets the label type, default: hidden. Options: hidden, top, horizontal, floating | ||
| 803 | 'label_class' => '', | ||
| 804 | 'help_text' => '', | ||
| 805 | 'placeholder' => '', | ||
| 806 | 'options' => array(), | ||
| 807 | // array or string | ||
| 808 | 'icon' => '', | ||
| 809 | 'multiple' => false, | ||
| 810 | 'select2' => false, | ||
| 811 | 'no_wrap' => false, | ||
| 812 | 'input_group_right' => '', | ||
| 813 | 'input_group_left' => '', | ||
| 814 | 'input_group_right_inside' => false, // forces the input group inside the input | ||
| 815 | 'input_group_left_inside' => false, // forces the input group inside the input | ||
| 816 | 'form_group_class' => '', | ||
| 817 | 'element_require' => '', | ||
| 818 | // [%element_id%] == "1" | ||
| 819 | 'extra_attributes' => array(), | ||
| 820 | // an array of extra attributes | ||
| 821 | 'wrap_attributes' => array(), | ||
| 822 | ); | ||
| 823 | |||
| 824 | /** | ||
| 825 | * Parse incoming $args into an array and merge it with $defaults | ||
| 826 | */ | ||
| 827 | $args = wp_parse_args( $args, $defaults ); | ||
| 828 | $output = ''; | ||
| 829 | |||
| 830 | // for now lets hide floating labels | ||
| 831 | 		if ( $args['label_type'] == 'floating' ) { | ||
| 832 | $args['label_type'] = 'hidden'; | ||
| 833 | } | ||
| 834 | |||
| 835 | // hidden label option needs to be empty | ||
| 836 | $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; | ||
| 837 | |||
| 838 | |||
| 839 | $label_after = $args['label_after']; | ||
| 840 | |||
| 841 | // floating labels need label after | ||
| 842 | 		if ( $args['label_type'] == 'floating' ) { | ||
| 843 | $label_after = true; | ||
| 844 | $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. | ||
| 845 | } | ||
| 846 | |||
| 847 | // Maybe setup select2 | ||
| 848 | $is_select2 = false; | ||
| 849 | 		if ( ! empty( $args['select2'] ) ) { | ||
| 850 | $args['class'] .= ' aui-select2'; | ||
| 851 | $is_select2 = true; | ||
| 852 | 		} elseif ( strpos( $args['class'], 'aui-select2' ) !== false ) { | ||
| 853 | $is_select2 = true; | ||
| 854 | } | ||
| 855 | |||
| 856 | // select2 tags | ||
| 857 | 		if ( ! empty( $args['select2'] ) && $args['select2'] === 'tags' ) { // triple equals needed here for some reason | ||
| 858 | $args['data-tags'] = 'true'; | ||
| 859 | $args['data-token-separators'] = "[',']"; | ||
| 860 | $args['multiple'] = true; | ||
| 861 | } | ||
| 862 | |||
| 863 | // select2 placeholder | ||
| 864 | 		if ( $is_select2 && isset( $args['placeholder'] ) && '' != $args['placeholder'] && empty( $args['data-placeholder'] ) ) { | ||
| 865 | $args['data-placeholder'] = esc_attr( $args['placeholder'] ); | ||
| 866 | $args['data-allow-clear'] = isset( $args['data-allow-clear'] ) ? (bool) $args['data-allow-clear'] : true; | ||
| 867 | } | ||
| 868 | |||
| 869 | // Set hidden input to save empty value for multiselect. | ||
| 870 | 		if ( ! empty( $args['multiple'] ) && ! empty( $args['name'] ) ) { | ||
| 871 | $output .= '<input type="hidden" ' . AUI_Component_Helper::name( $args['name'] ) . ' value="" data-ignore-rule/>'; | ||
| 872 | } | ||
| 873 | |||
| 874 | // open/type | ||
| 875 | $output .= '<select '; | ||
| 876 | |||
| 877 | // style | ||
| 878 | 		if ( $is_select2 && !($args['input_group_left'] || $args['input_group_right'])) { | ||
| 879 | $output .= " style='width:100%;' "; | ||
| 880 | } | ||
| 881 | |||
| 882 | // element require | ||
| 883 | 		if ( ! empty( $args['element_require'] ) ) { | ||
| 884 | $output .= AUI_Component_Helper::element_require( $args['element_require'] ); | ||
| 885 | $args['class'] .= " aui-conditional-field"; | ||
| 886 | } | ||
| 887 | |||
| 888 | // class | ||
| 889 | $class = ! empty( $args['class'] ) ? $args['class'] : ''; | ||
| 890 | $select_class = $aui_bs5 ? 'form-select ' : 'custom-select '; | ||
| 891 | $output .= AUI_Component_Helper::class_attr( $select_class . $class ); | ||
| 892 | |||
| 893 | // name | ||
| 894 | 		if ( ! empty( $args['name'] ) ) { | ||
| 895 | $output .= AUI_Component_Helper::name( $args['name'], $args['multiple'] ); | ||
| 896 | } | ||
| 897 | |||
| 898 | // id | ||
| 899 | 		if ( ! empty( $args['id'] ) ) { | ||
| 900 | $output .= AUI_Component_Helper::id( $args['id'] ); | ||
| 901 | } | ||
| 902 | |||
| 903 | // title | ||
| 904 | 		if ( ! empty( $args['title'] ) ) { | ||
| 905 | $output .= AUI_Component_Helper::title( $args['title'] ); | ||
| 906 | } | ||
| 907 | |||
| 908 | // data-attributes | ||
| 909 | $output .= AUI_Component_Helper::data_attributes( $args ); | ||
| 910 | |||
| 911 | // aria-attributes | ||
| 912 | $output .= AUI_Component_Helper::aria_attributes( $args ); | ||
| 913 | |||
| 914 | // extra attributes | ||
| 915 | 		if ( ! empty( $args['extra_attributes'] ) ) { | ||
| 916 | $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); | ||
| 917 | } | ||
| 918 | |||
| 919 | // required | ||
| 920 | 		if ( ! empty( $args['required'] ) ) { | ||
| 921 | $output .= ' required '; | ||
| 922 | } | ||
| 923 | |||
| 924 | // multiple | ||
| 925 | 		if ( ! empty( $args['multiple'] ) ) { | ||
| 926 | $output .= ' multiple '; | ||
| 927 | } | ||
| 928 | |||
| 929 | // close opening tag | ||
| 930 | $output .= ' >'; | ||
| 931 | |||
| 932 | // placeholder | ||
| 933 | 		if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] && ! $is_select2 ) { | ||
| 934 | $output .= '<option value="" disabled selected hidden>' . esc_attr( $args['placeholder'] ) . '</option>'; | ||
| 935 | 		} elseif ( $is_select2 && ! empty( $args['placeholder'] ) ) { | ||
| 936 | $output .= "<option></option>"; // select2 needs an empty select to fill the placeholder | ||
| 937 | } | ||
| 938 | |||
| 939 | // Options | ||
| 940 | 		if ( ! empty( $args['options'] ) ) { | ||
| 941 | |||
| 942 | 			if ( ! is_array( $args['options'] ) ) { | ||
| 943 | $output .= $args['options']; // not the preferred way but an option | ||
| 944 | 			} else { | ||
| 945 | 				foreach ( $args['options'] as $val => $name ) { | ||
| 946 | $selected = ''; | ||
| 947 | 					if ( is_array( $name ) ) { | ||
| 948 | 						if ( isset( $name['optgroup'] ) && ( $name['optgroup'] == 'start' || $name['optgroup'] == 'end' ) ) { | ||
| 949 | $option_label = isset( $name['label'] ) ? $name['label'] : ''; | ||
| 950 | |||
| 951 | $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr( $option_label ) . '">' : '</optgroup>'; | ||
| 952 | 						} else { | ||
| 953 | $option_label = isset( $name['label'] ) ? $name['label'] : ''; | ||
| 954 | $option_value = isset( $name['value'] ) ? $name['value'] : ''; | ||
| 955 | $extra_attributes = !empty($name['extra_attributes']) ? AUI_Component_Helper::extra_attributes( $name['extra_attributes'] ) : ''; | ||
| 956 | 							if ( ! empty( $args['multiple'] ) && ! empty( $args['value'] ) && is_array( $args['value'] ) ) { | ||
| 957 | $selected = in_array( $option_value, stripslashes_deep( $args['value'] ) ) ? "selected" : ""; | ||
|  | |||
| 958 | 							} elseif ( ! empty( $args['value'] ) ) { | ||
| 959 | $selected = selected( $option_value, stripslashes_deep( $args['value'] ), false ); | ||
| 960 | 							} elseif ( empty( $args['value'] ) && $args['value'] === $option_value ) { | ||
| 961 | $selected = selected( $option_value, $args['value'], false ); | ||
| 962 | } | ||
| 963 | |||
| 964 | $output .= '<option value="' . esc_attr( $option_value ) . '" ' . $selected . ' '.$extra_attributes .'>' . $option_label . '</option>'; | ||
| 965 | } | ||
| 966 | 					} else { | ||
| 967 | 						if ( ! empty( $args['value'] ) ) { | ||
| 968 | 							if ( is_array( $args['value'] ) ) { | ||
| 969 | $selected = in_array( $val, $args['value'] ) ? 'selected="selected"' : ''; | ||
| 970 | 							} elseif ( ! empty( $args['value'] ) ) { | ||
| 971 | $selected = selected( $args['value'], $val, false ); | ||
| 972 | } | ||
| 973 | 						} elseif ( $args['value'] === $val ) { | ||
| 974 | $selected = selected( $args['value'], $val, false ); | ||
| 975 | } | ||
| 976 | $output .= '<option value="' . esc_attr( $val ) . '" ' . $selected . '>' . esc_attr( $name ) . '</option>'; | ||
| 977 | } | ||
| 978 | } | ||
| 979 | } | ||
| 980 | |||
| 981 | } | ||
| 982 | |||
| 983 | // closing tag | ||
| 984 | $output .= '</select>'; | ||
| 985 | |||
| 986 | $label = ''; | ||
| 987 | $help_text = ''; | ||
| 988 | // label | ||
| 989 | 		if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { | ||
| 990 | 		} elseif ( ! empty( $args['label'] ) && ! $label_after ) { | ||
| 991 | $label_args = array( | ||
| 992 | 'title' => $args['label'], | ||
| 993 | 'for' => $args['id'], | ||
| 994 | 'class' => $args['label_class'] . " ", | ||
| 995 | 'label_type' => $args['label_type'], | ||
| 996 | 'label_col' => $args['label_col'] | ||
| 997 | ); | ||
| 998 | $label = self::label( $label_args ); | ||
| 999 | } | ||
| 1000 | |||
| 1001 | // help text | ||
| 1002 | 		if ( ! empty( $args['help_text'] ) ) { | ||
| 1003 | $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | // input group wraps | ||
| 1007 | 		if ( $args['input_group_left'] || $args['input_group_right'] ) { | ||
| 1008 | $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; | ||
| 1009 | 			if ( $args['input_group_left'] ) { | ||
| 1010 | $output = self::wrap( array( | ||
| 1011 | 'content' => $output, | ||
| 1012 | 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', | ||
| 1013 | 'input_group_left' => $args['input_group_left'], | ||
| 1014 | 'input_group_left_inside' => $args['input_group_left_inside'] | ||
| 1015 | ) ); | ||
| 1016 | } | ||
| 1017 | 			if ( $args['input_group_right'] ) { | ||
| 1018 | $output = self::wrap( array( | ||
| 1019 | 'content' => $output, | ||
| 1020 | 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', | ||
| 1021 | 'input_group_right' => $args['input_group_right'], | ||
| 1022 | 'input_group_right_inside' => $args['input_group_right_inside'] | ||
| 1023 | ) ); | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | } | ||
| 1027 | |||
| 1028 | 		if ( ! $label_after ) { | ||
| 1029 | $output .= $help_text; | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | |||
| 1033 | 		if ( $args['label_type'] == 'horizontal' ) { | ||
| 1034 | $output = self::wrap( array( | ||
| 1035 | 'content' => $output, | ||
| 1036 | 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) | ||
| 1037 | ) ); | ||
| 1038 | } | ||
| 1039 | |||
| 1040 | 		if ( ! $label_after ) { | ||
| 1041 | $output = $label . $output; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | // maybe horizontal label | ||
| 1045 | //		if ( $args['label_type'] == 'horizontal' ) { | ||
| 1046 | // $output .= '</div>'; | ||
| 1047 | // } | ||
| 1048 | |||
| 1049 | |||
| 1050 | // wrap | ||
| 1051 | 		if ( ! $args['no_wrap'] ) { | ||
| 1052 | 			if ( ! empty( $args['form_group_class'] ) ) { | ||
| 1053 | $fg_class = esc_attr( $args['form_group_class'] ); | ||
| 1054 | 			}else{ | ||
| 1055 | $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; | ||
| 1056 | } | ||
| 1057 | $wrap_class = $args['label_type'] == 'horizontal' ? $fg_class . ' row' : $fg_class; | ||
| 1058 | $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; | ||
| 1059 | $output = self::wrap( array( | ||
| 1060 | 'content' => $output, | ||
| 1061 | 'class' => $wrap_class, | ||
| 1062 | 'element_require' => $args['element_require'], | ||
| 1063 | 'argument_id' => $args['id'], | ||
| 1064 | 'wrap_attributes' => $args['wrap_attributes'], | ||
| 1065 | ) ); | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | |||
| 1069 | return $output; | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | /** | ||
| 1073 | * Build the component. | ||
| 1074 | * | ||
| 1075 | * @param array $args | ||
| 1076 | * | ||
| 1077 | * @return string The rendered component. | ||
| 1078 | */ | ||
| 1079 | 	public static function radio( $args = array() ) { | ||
| 1080 | global $aui_bs5; | ||
| 1081 | |||
| 1082 | $defaults = array( | ||
| 1083 | 'class' => '', | ||
| 1084 | 'wrap_class' => '', | ||
| 1085 | 'id' => '', | ||
| 1086 | 'title' => '', | ||
| 1087 | 'horizontal' => false, | ||
| 1088 | // sets the lable horizontal | ||
| 1089 | 'value' => '', | ||
| 1090 | 'label' => '', | ||
| 1091 | 'label_class' => '', | ||
| 1092 | 'label_type' => '', | ||
| 1093 | 'label_col' => '', | ||
| 1094 | // sets the label type, default: hidden. Options: hidden, top, horizontal, floating | ||
| 1095 | 'help_text' => '', | ||
| 1096 | 'inline' => true, | ||
| 1097 | 'required' => false, | ||
| 1098 | 'options' => array(), | ||
| 1099 | 'icon' => '', | ||
| 1100 | 'no_wrap' => false, | ||
| 1101 | 'element_require' => '', | ||
| 1102 | // [%element_id%] == "1" | ||
| 1103 | 'extra_attributes' => array(), | ||
| 1104 | // an array of extra attributes | ||
| 1105 | 'wrap_attributes' => array() | ||
| 1106 | ); | ||
| 1107 | |||
| 1108 | /** | ||
| 1109 | * Parse incoming $args into an array and merge it with $defaults | ||
| 1110 | */ | ||
| 1111 | $args = wp_parse_args( $args, $defaults ); | ||
| 1112 | |||
| 1113 | // for now lets use horizontal for floating | ||
| 1114 | 		if ( $args['label_type'] == 'floating' ) { | ||
| 1115 | $args['label_type'] = 'horizontal'; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | $label_args = array( | ||
| 1119 | 'title' => $args['label'], | ||
| 1120 | 'class' => $args['label_class'] . " pt-0 ", | ||
| 1121 | 'label_type' => $args['label_type'], | ||
| 1122 | 'label_col' => $args['label_col'] | ||
| 1123 | ); | ||
| 1124 | |||
| 1125 | $output = ''; | ||
| 1126 | |||
| 1127 | |||
| 1128 | // label before | ||
| 1129 | 		if ( ! empty( $args['label'] ) ) { | ||
| 1130 | $output .= self::label( $label_args, 'radio' ); | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | // maybe horizontal label | ||
| 1134 | 		if ( $args['label_type'] == 'horizontal' ) { | ||
| 1135 | $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); | ||
| 1136 | $output .= '<div class="' . $input_col . '">'; | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | 		if ( ! empty( $args['options'] ) ) { | ||
| 1140 | $count = 0; | ||
| 1141 | 			foreach ( $args['options'] as $value => $label ) { | ||
| 1142 | $option_args = $args; | ||
| 1143 | $option_args['value'] = $value; | ||
| 1144 | $option_args['label'] = $label; | ||
| 1145 | $option_args['checked'] = $value == $args['value'] ? true : false; | ||
| 1146 | $output .= self::radio_option( $option_args, $count ); | ||
| 1147 | $count ++; | ||
| 1148 | } | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | // help text | ||
| 1152 | $help_text = ! empty( $args['help_text'] ) ? AUI_Component_Helper::help_text( $args['help_text'] ) : ''; | ||
| 1153 | $output .= $help_text; | ||
| 1154 | |||
| 1155 | // maybe horizontal label | ||
| 1156 | 		if ( $args['label_type'] == 'horizontal' ) { | ||
| 1157 | $output .= '</div>'; | ||
| 1158 | } | ||
| 1159 | |||
| 1160 | // wrap | ||
| 1161 | $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; | ||
| 1162 | $wrap_class = $args['label_type'] == 'horizontal' ? $fg_class . ' row' : $fg_class; | ||
| 1163 | $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; | ||
| 1164 | $output = self::wrap( array( | ||
| 1165 | 'content' => $output, | ||
| 1166 | 'class' => $wrap_class, | ||
| 1167 | 'element_require' => $args['element_require'], | ||
| 1168 | 'argument_id' => $args['id'], | ||
| 1169 | 'wrap_attributes' => $args['wrap_attributes'], | ||
| 1170 | ) ); | ||
| 1171 | |||
| 1172 | |||
| 1173 | return $output; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | /** | ||
| 1177 | * Build the component. | ||
| 1178 | * | ||
| 1179 | * @param array $args | ||
| 1180 | * | ||
| 1181 | * @return string The rendered component. | ||
| 1182 | */ | ||
| 1183 | 	public static function radio_option( $args = array(), $count = '' ) { | ||
| 1291 | } | ||
| 1292 | |||
| 1293 | } |