| Total Complexity | 154 |
| Total Lines | 900 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 | $defaults = array( |
||
| 23 | 'type' => 'text', |
||
| 24 | 'name' => '', |
||
| 25 | 'class' => '', |
||
| 26 | 'id' => '', |
||
| 27 | 'placeholder'=> '', |
||
| 28 | 'title' => '', |
||
| 29 | 'value' => '', |
||
| 30 | 'required' => false, |
||
| 31 | 'label' => '', |
||
| 32 | 'label_after'=> false, |
||
| 33 | 'label_class'=> '', |
||
| 34 | 'label_type' => '', // sets the lable type, horizontal |
||
| 35 | 'help_text' => '', |
||
| 36 | 'validation_text' => '', |
||
| 37 | 'validation_pattern' => '', |
||
| 38 | 'no_wrap' => false, |
||
| 39 | 'input_group_right' => '', |
||
| 40 | 'input_group_left' => '', |
||
| 41 | 'input_group_right_inside' => false, // forces the input group inside the input |
||
| 42 | 'input_group_left_inside' => false, // forces the input group inside the input |
||
| 43 | 'step' => '', |
||
| 44 | 'switch' => false, // to show checkbox as a switch |
||
| 45 | 'checked' => false, // set a checkbox or radio as selected |
||
| 46 | 'password_toggle' => true, // toggle view/hide password |
||
| 47 | 'extra_attributes' => array() // an array of extra attributes |
||
| 48 | ); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Parse incoming $args into an array and merge it with $defaults |
||
| 52 | */ |
||
| 53 | $args = wp_parse_args( $args, $defaults ); |
||
| 54 | $output = ''; |
||
| 55 | if ( ! empty( $args['type'] ) ) { |
||
| 56 | $type = sanitize_html_class( $args['type'] ); |
||
| 57 | |||
| 58 | $help_text = ''; |
||
| 59 | $label = ''; |
||
| 60 | $label_after = $args['label_after']; |
||
| 61 | $label_args = array( |
||
| 62 | 'title'=> $args['label'], |
||
| 63 | 'for'=> $args['id'], |
||
| 64 | 'class' => $args['label_class']." ", |
||
| 65 | 'label_type' => $args['label_type'] |
||
| 66 | ); |
||
| 67 | |||
| 68 | // Some special sauce for files |
||
| 69 | if($type=='file' ){ |
||
| 70 | $label_after = true; // if type file we need the label after |
||
| 71 | $args['class'] .= ' custom-file-input '; |
||
| 72 | }elseif($type=='checkbox'){ |
||
| 73 | $label_after = true; // if type file we need the label after |
||
| 74 | $args['class'] .= ' custom-control-input '; |
||
| 75 | }elseif($type=='datepicker' || $type=='timepicker'){ |
||
| 76 | $type = 'text'; |
||
| 77 | $args['class'] .= ' aui-flatpickr bg-initial '; |
||
| 78 | |||
| 79 | // enqueue the script |
||
| 80 | $aui_settings = AyeCode_UI_Settings::instance(); |
||
| 81 | $aui_settings->enqueue_flatpickr(); |
||
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | // open/type |
||
| 86 | $output .= '<input type="' . $type . '" '; |
||
| 87 | |||
| 88 | // name |
||
| 89 | if(!empty($args['name'])){ |
||
| 90 | $output .= ' name="'.esc_attr($args['name']).'" '; |
||
| 91 | } |
||
| 92 | |||
| 93 | // id |
||
| 94 | if(!empty($args['id'])){ |
||
| 95 | $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
||
| 96 | } |
||
| 97 | |||
| 98 | // placeholder |
||
| 99 | if(!empty($args['placeholder'])){ |
||
| 100 | $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
||
| 101 | } |
||
| 102 | |||
| 103 | // title |
||
| 104 | if(!empty($args['title'])){ |
||
| 105 | $output .= ' title="'.esc_attr($args['title']).'" '; |
||
| 106 | } |
||
| 107 | |||
| 108 | // value |
||
| 109 | if(!empty($args['value'])){ |
||
| 110 | $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
||
| 111 | } |
||
| 112 | |||
| 113 | // checked, for radio and checkboxes |
||
| 114 | if( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ){ |
||
| 115 | $output .= ' checked '; |
||
| 116 | } |
||
| 117 | |||
| 118 | // validation text |
||
| 119 | if(!empty($args['validation_text'])){ |
||
| 120 | $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
||
| 121 | $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
||
| 122 | } |
||
| 123 | |||
| 124 | // validation_pattern |
||
| 125 | if(!empty($args['validation_pattern'])){ |
||
| 126 | $output .= ' pattern="'.$args['validation_pattern'].'" '; |
||
| 127 | } |
||
| 128 | |||
| 129 | // step (for numbers) |
||
| 130 | if(!empty($args['step'])){ |
||
| 131 | $output .= ' step="'.$args['step'].'" '; |
||
| 132 | } |
||
| 133 | |||
| 134 | // required |
||
| 135 | if(!empty($args['required'])){ |
||
| 136 | $output .= ' required '; |
||
| 137 | } |
||
| 138 | |||
| 139 | // class |
||
| 140 | $class = !empty($args['class']) ? $args['class'] : ''; |
||
| 141 | $output .= ' class="form-control '.$class.'" '; |
||
| 142 | |||
| 143 | // data-attributes |
||
| 144 | $output .= AUI_Component_Helper::data_attributes($args); |
||
| 145 | |||
| 146 | // extra attributes |
||
| 147 | if(!empty($args['extra_attributes'])){ |
||
| 148 | $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
||
| 149 | } |
||
| 150 | |||
| 151 | // close |
||
| 152 | $output .= ' >'; |
||
| 153 | |||
| 154 | |||
| 155 | // label |
||
| 156 | if(!empty($args['label'])){ |
||
| 157 | if($type == 'file'){$label_args['class'] .= 'custom-file-label';} |
||
| 158 | elseif($type == 'checkbox'){$label_args['class'] .= 'custom-control-label';} |
||
| 159 | $label = self::label( $label_args, $type ); |
||
| 160 | } |
||
| 161 | |||
| 162 | // help text |
||
| 163 | if(!empty($args['help_text'])){ |
||
| 164 | $help_text = AUI_Component_Helper::help_text($args['help_text']); |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | // set help text in the correct possition |
||
| 169 | if($label_after){ |
||
| 170 | $output .= $label . $help_text; |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | // some input types need a separate wrap |
||
| 178 | if($type == 'file') { |
||
| 179 | $output = self::wrap( array( |
||
| 180 | 'content' => $output, |
||
| 181 | 'class' => 'form-group custom-file' |
||
| 182 | ) ); |
||
| 183 | }elseif($type == 'checkbox'){ |
||
| 184 | $wrap_class = $args['switch'] ? 'custom-switch' : 'custom-checkbox'; |
||
| 185 | $output = self::wrap( array( |
||
| 186 | 'content' => $output, |
||
| 187 | 'class' => 'custom-control '.$wrap_class |
||
| 188 | ) ); |
||
| 189 | |||
| 190 | if($args['label_type']=='horizontal'){ |
||
| 191 | $output = '<div class="col-sm-2 col-form-label"></div><div class="col-sm-10">' . $output . '</div>'; |
||
| 192 | } |
||
| 193 | }elseif($type == 'password' && $args['password_toggle'] && !$args['input_group_right']){ |
||
| 194 | |||
| 195 | |||
| 196 | // allow password field to toggle view |
||
| 197 | $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
||
| 198 | onclick="var $el = jQuery(this).find(\'i\');$el.toggleClass(\'fa-eye fa-eye-slash\'); |
||
| 199 | var $eli = jQuery(this).parent().parent().find(\'input\'); |
||
| 200 | if($el.hasClass(\'fa-eye\')) |
||
| 201 | {$eli.attr(\'type\',\'text\');} |
||
| 202 | else{$eli.attr(\'type\',\'password\');}" |
||
| 203 | ><i class="far fa-fw fa-eye-slash"></i></span>'; |
||
| 204 | } |
||
| 205 | |||
| 206 | // input group wraps |
||
| 207 | if($args['input_group_left'] || $args['input_group_right']){ |
||
| 208 | $w100 = strpos($args['class'], 'w-100') !== false ? ' w-100' : ''; |
||
| 209 | if($args['input_group_left']){ |
||
| 210 | $output = self::wrap( array( |
||
| 211 | 'content' => $output, |
||
| 212 | 'class' => $args['input_group_left_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
||
| 213 | 'input_group_left' => $args['input_group_left'], |
||
| 214 | 'input_group_left_inside' => $args['input_group_left_inside'] |
||
| 215 | ) ); |
||
| 216 | } |
||
| 217 | if($args['input_group_right']){ |
||
| 218 | $output = self::wrap( array( |
||
| 219 | 'content' => $output, |
||
| 220 | 'class' => $args['input_group_right_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
||
| 221 | 'input_group_right' => $args['input_group_right'], |
||
| 222 | 'input_group_right_inside' => $args['input_group_right_inside'] |
||
| 223 | ) ); |
||
| 224 | } |
||
| 225 | |||
| 226 | // Labels need to be on the outside of the wrap |
||
| 227 | // $label = self::label( $label_args, $type ); |
||
| 228 | // $output = $label . str_replace($label,"",$output); |
||
| 229 | } |
||
| 230 | |||
| 231 | if(!$label_after){ |
||
| 232 | $output .= $help_text; |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
||
| 237 | $output = self::wrap( array( |
||
| 238 | 'content' => $output, |
||
| 239 | 'class' => 'col-sm-10', |
||
| 240 | ) ); |
||
| 241 | } |
||
| 242 | |||
| 243 | if(!$label_after){ |
||
| 244 | $output = $label . $output; |
||
| 245 | } |
||
| 246 | |||
| 247 | // // maybe horizontal label |
||
| 248 | // if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
||
| 249 | // $output .= '<div class="col-sm-10">'; |
||
| 250 | // } |
||
| 251 | // // maybe horizontal label |
||
| 252 | // if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
||
| 253 | // $output .= '</div>'; |
||
| 254 | // } |
||
| 255 | |||
| 256 | // wrap |
||
| 257 | if(!$args['no_wrap']){ |
||
| 258 | $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
||
| 259 | $output = self::wrap(array( |
||
| 260 | 'content' => $output, |
||
| 261 | 'class' => $wrap_class, |
||
| 262 | )); |
||
| 263 | } |
||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | } |
||
| 268 | |||
| 269 | return $output; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Build the component. |
||
| 274 | * |
||
| 275 | * @param array $args |
||
| 276 | * |
||
| 277 | * @return string The rendered component. |
||
| 278 | */ |
||
| 279 | public static function textarea($args = array()){ |
||
| 280 | $defaults = array( |
||
| 281 | 'name' => '', |
||
| 282 | 'class' => '', |
||
| 283 | 'id' => '', |
||
| 284 | 'placeholder'=> '', |
||
| 285 | 'title' => '', |
||
| 286 | 'value' => '', |
||
| 287 | 'required' => false, |
||
| 288 | 'label' => '', |
||
| 289 | 'label_class' => '', |
||
| 290 | 'label_type' => '', // sets the lable type, horizontal |
||
| 291 | 'help_text' => '', |
||
| 292 | 'validation_text' => '', |
||
| 293 | 'validation_pattern' => '', |
||
| 294 | 'no_wrap' => false, |
||
| 295 | 'rows' => '', |
||
| 296 | 'wysiwyg' => false, |
||
| 297 | ); |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Parse incoming $args into an array and merge it with $defaults |
||
| 301 | */ |
||
| 302 | $args = wp_parse_args( $args, $defaults ); |
||
| 303 | $output = ''; |
||
| 304 | |||
| 305 | // label |
||
| 306 | if(!empty($args['label']) && is_array($args['label'])){ |
||
| 307 | }elseif(!empty($args['label'])){ |
||
| 308 | $label_args = array( |
||
| 309 | 'title'=> $args['label'], |
||
| 310 | 'for'=> $args['id'], |
||
| 311 | 'class' => $args['label_class']." ", |
||
| 312 | 'label_type' => $args['label_type'] |
||
| 313 | ); |
||
| 314 | $output .= self::label( $label_args ); |
||
| 315 | } |
||
| 316 | |||
| 317 | // maybe horizontal label |
||
| 318 | if($args['label_type']=='horizontal'){ |
||
| 319 | $output .= '<div class="col-sm-10">'; |
||
| 320 | } |
||
| 321 | |||
| 322 | if(!empty($args['wysiwyg'])){ |
||
| 323 | ob_start(); |
||
| 324 | $content = $args['value']; |
||
| 325 | $editor_id = !empty($args['id']) ? sanitize_html_class($args['id']) : 'wp_editor'; |
||
| 326 | $settings = array( |
||
| 327 | 'textarea_rows' => !empty(absint($args['rows'])) ? absint($args['rows']) : 4, |
||
| 328 | 'quicktags' => false, |
||
| 329 | 'media_buttons' => false, |
||
| 330 | 'editor_class' => 'form-control', |
||
| 331 | 'textarea_name' => !empty($args['name']) ? sanitize_html_class($args['name']) : sanitize_html_class($args['id']), |
||
| 332 | 'teeny' => true, |
||
| 333 | ); |
||
| 334 | |||
| 335 | // maybe set settings if array |
||
| 336 | if(is_array($args['wysiwyg'])){ |
||
| 337 | $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
||
| 338 | } |
||
| 339 | |||
| 340 | wp_editor( $content, $editor_id, $settings ); |
||
| 341 | $output .= ob_get_clean(); |
||
| 342 | }else{ |
||
| 343 | |||
| 344 | // open |
||
| 345 | $output .= '<textarea '; |
||
| 346 | |||
| 347 | // name |
||
| 348 | if(!empty($args['name'])){ |
||
| 349 | $output .= ' name="'.sanitize_html_class($args['name']).'" '; |
||
| 350 | } |
||
| 351 | |||
| 352 | // id |
||
| 353 | if(!empty($args['id'])){ |
||
| 354 | $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
||
| 355 | } |
||
| 356 | |||
| 357 | // placeholder |
||
| 358 | if(!empty($args['placeholder'])){ |
||
| 359 | $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
||
| 360 | } |
||
| 361 | |||
| 362 | // title |
||
| 363 | if(!empty($args['title'])){ |
||
| 364 | $output .= ' title="'.esc_attr($args['title']).'" '; |
||
| 365 | } |
||
| 366 | |||
| 367 | // validation text |
||
| 368 | if(!empty($args['validation_text'])){ |
||
| 369 | $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
||
| 370 | $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
||
| 371 | } |
||
| 372 | |||
| 373 | // validation_pattern |
||
| 374 | if(!empty($args['validation_pattern'])){ |
||
| 375 | $output .= ' pattern="'.$args['validation_pattern'].'" '; |
||
| 376 | } |
||
| 377 | |||
| 378 | // required |
||
| 379 | if(!empty($args['required'])){ |
||
| 380 | $output .= ' required '; |
||
| 381 | } |
||
| 382 | |||
| 383 | // rows |
||
| 384 | if(!empty($args['rows'])){ |
||
| 385 | $output .= ' rows="'.absint($args['rows']).'" '; |
||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | // class |
||
| 390 | $class = !empty($args['class']) ? $args['class'] : ''; |
||
| 391 | $output .= ' class="form-control '.$class.'" '; |
||
| 392 | |||
| 393 | |||
| 394 | // close tag |
||
| 395 | $output .= ' >'; |
||
| 396 | |||
| 397 | // value |
||
| 398 | if(!empty($args['value'])){ |
||
| 399 | $output .= sanitize_textarea_field($args['value']); |
||
| 400 | } |
||
| 401 | |||
| 402 | // closing tag |
||
| 403 | $output .= '</textarea>'; |
||
| 404 | |||
| 405 | } |
||
| 406 | |||
| 407 | // help text |
||
| 408 | if(!empty($args['help_text'])){ |
||
| 409 | $output .= AUI_Component_Helper::help_text($args['help_text']); |
||
| 410 | } |
||
| 411 | |||
| 412 | // maybe horizontal label |
||
| 413 | if($args['label_type']=='horizontal'){ |
||
| 414 | $output .= '</div>'; |
||
| 415 | } |
||
| 416 | |||
| 417 | |||
| 418 | // wrap |
||
| 419 | if(!$args['no_wrap']){ |
||
| 420 | $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
||
| 421 | $output = self::wrap(array( |
||
| 422 | 'content' => $output, |
||
| 423 | 'class' => $wrap_class, |
||
| 424 | )); |
||
| 425 | } |
||
| 426 | |||
| 427 | |||
| 428 | return $output; |
||
| 429 | } |
||
| 430 | |||
| 431 | public static function label($args = array(), $type = ''){ |
||
| 488 | } |
||
| 489 | |||
| 490 | public static function wrap($args = array()){ |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Build the component. |
||
| 550 | * |
||
| 551 | * @param array $args |
||
| 552 | * |
||
| 553 | * @return string The rendered component. |
||
| 554 | */ |
||
| 555 | public static function select($args = array()){ |
||
| 556 | $defaults = array( |
||
| 557 | 'class' => '', |
||
| 558 | 'id' => '', |
||
| 559 | 'title' => '', |
||
| 560 | 'value' => '', // can be an array or a string |
||
| 561 | 'required' => false, |
||
| 562 | 'label' => '', |
||
| 563 | 'label_class' => '', |
||
| 564 | 'label_type' => '', // sets the lable type, horizontal |
||
| 565 | 'help_text' => '', |
||
| 566 | 'placeholder'=> '', |
||
| 567 | 'options' => array(), |
||
| 568 | 'icon' => '', |
||
| 569 | 'multiple' => false, |
||
| 570 | 'select2' => false, |
||
| 571 | 'no_wrap' => false, |
||
| 572 | 'extra_attributes' => array() // an array of extra attributes |
||
| 573 | ); |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Parse incoming $args into an array and merge it with $defaults |
||
| 577 | */ |
||
| 578 | $args = wp_parse_args( $args, $defaults ); |
||
| 579 | $output = ''; |
||
| 580 | |||
| 581 | // Maybe setup select2 |
||
| 582 | $is_select2 = false; |
||
| 583 | if(!empty($args['select2'])){ |
||
| 584 | $args['class'] .= ' aui-select2'; |
||
| 585 | $is_select2 = true; |
||
| 586 | }elseif( strpos($args['class'], 'aui-select2') !== false){ |
||
| 587 | $is_select2 = true; |
||
| 588 | } |
||
| 589 | |||
| 590 | // select2 tags |
||
| 591 | if( !empty($args['select2']) && $args['select2'] === 'tags'){ // triple equlas needed here for some reason |
||
| 592 | $args['data-tags'] = 'true'; |
||
| 593 | $args['data-token-separators'] = "[',']"; |
||
| 594 | $args['multiple'] = true; |
||
| 595 | } |
||
| 596 | |||
| 597 | // select2 placeholder |
||
| 598 | if($is_select2 && !empty($args['placeholder']) && empty($args['data-placeholder'])){ |
||
| 599 | $args['data-placeholder'] = esc_attr($args['placeholder']); |
||
| 600 | $args['data-allow-clear'] = empty($args['data-allow-clear']) ? true : esc_attr($args['data-allow-clear']); |
||
| 601 | } |
||
| 602 | |||
| 603 | // label |
||
| 604 | if(!empty($args['label']) && is_array($args['label'])){ |
||
| 605 | }elseif(!empty($args['label'])){ |
||
| 606 | $label_args = array( |
||
| 607 | 'title'=> $args['label'], |
||
| 608 | 'for'=> $args['id'], |
||
| 609 | 'class' => $args['label_class']." ", |
||
| 610 | 'label_type' => $args['label_type'] |
||
| 611 | ); |
||
| 612 | $output .= self::label($label_args); |
||
| 613 | } |
||
| 614 | |||
| 615 | // maybe horizontal label |
||
| 616 | if($args['label_type']=='horizontal'){ |
||
| 617 | $output .= '<div class="col-sm-10">'; |
||
| 618 | } |
||
| 619 | |||
| 620 | // open/type |
||
| 621 | $output .= '<select '; |
||
| 622 | |||
| 623 | // style |
||
| 624 | if($is_select2){ |
||
| 625 | $output .= " style='width:100%;' "; |
||
| 626 | } |
||
| 627 | |||
| 628 | // class |
||
| 629 | $class = !empty($args['class']) ? $args['class'] : ''; |
||
| 630 | $output .= AUI_Component_Helper::class_attr('custom-select '.$class); |
||
| 631 | |||
| 632 | // name |
||
| 633 | if(!empty($args['name'])){ |
||
| 634 | $output .= AUI_Component_Helper::name($args['name'],$args['multiple']); |
||
| 635 | } |
||
| 636 | |||
| 637 | // id |
||
| 638 | if(!empty($args['id'])){ |
||
| 639 | $output .= AUI_Component_Helper::id($args['id']); |
||
| 640 | } |
||
| 641 | |||
| 642 | // title |
||
| 643 | if(!empty($args['title'])){ |
||
| 644 | $output .= AUI_Component_Helper::title($args['title']); |
||
| 645 | } |
||
| 646 | |||
| 647 | // data-attributes |
||
| 648 | $output .= AUI_Component_Helper::data_attributes($args); |
||
| 649 | |||
| 650 | // aria-attributes |
||
| 651 | $output .= AUI_Component_Helper::aria_attributes($args); |
||
| 652 | |||
| 653 | // extra attributes |
||
| 654 | if(!empty($args['extra_attributes'])){ |
||
| 655 | $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
||
| 656 | } |
||
| 657 | |||
| 658 | // required |
||
| 659 | if(!empty($args['required'])){ |
||
| 660 | $output .= ' required '; |
||
| 661 | } |
||
| 662 | |||
| 663 | // multiple |
||
| 664 | if(!empty($args['multiple'])){ |
||
| 665 | $output .= ' multiple '; |
||
| 666 | } |
||
| 667 | |||
| 668 | // close opening tag |
||
| 669 | $output .= ' >'; |
||
| 670 | |||
| 671 | // placeholder |
||
| 672 | if(!empty($args['placeholder']) && !$is_select2){ |
||
| 673 | $output .= '<option value="" disabled selected hidden>'.esc_attr($args['placeholder']).'</option>'; |
||
| 674 | } |
||
| 675 | |||
| 676 | // Options |
||
| 677 | if(!empty($args['options'])){ |
||
| 678 | foreach($args['options'] as $val => $name){ |
||
| 679 | $selected = ''; |
||
| 680 | if(is_array($name)){ |
||
| 681 | if (isset($name['optgroup']) && ($name['optgroup'] == 'start' || $name['optgroup'] == 'end')) { |
||
| 682 | $option_label = isset($name['label']) ? $name['label'] : ''; |
||
| 683 | |||
| 684 | $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr($option_label) . '">' : '</optgroup>'; |
||
| 685 | } else { |
||
| 686 | $option_label = isset($name['label']) ? $name['label'] : ''; |
||
| 687 | $option_value = isset($name['value']) ? $name['value'] : ''; |
||
| 688 | if(!empty($args['multiple']) && !empty($args['value'])){ |
||
| 689 | $selected = in_array($option_value, stripslashes_deep($args['value'])) ? "selected" : ""; |
||
|
|
|||
| 690 | } elseif(!empty($args['value'])) { |
||
| 691 | $selected = selected($option_value,stripslashes_deep($args['value']), false); |
||
| 692 | } |
||
| 693 | |||
| 694 | $output .= '<option value="' . esc_attr($option_value) . '" ' . $selected . '>' . $option_label . '</option>'; |
||
| 695 | } |
||
| 696 | }else{ |
||
| 697 | if(!empty($args['value'])){ |
||
| 698 | if(is_array($args['value'])){ |
||
| 699 | $selected = in_array($val,$args['value']) ? 'selected="selected"' : ''; |
||
| 700 | } elseif(!empty($args['value'])) { |
||
| 701 | $selected = selected( $args['value'], $val, false); |
||
| 702 | } |
||
| 703 | } |
||
| 704 | $output .= '<option value="'.esc_attr($val).'" '.$selected.'>'.esc_attr($name).'</option>'; |
||
| 705 | } |
||
| 706 | } |
||
| 707 | |||
| 708 | } |
||
| 709 | |||
| 710 | // closing tag |
||
| 711 | $output .= '</select>'; |
||
| 712 | |||
| 713 | // help text |
||
| 714 | if(!empty($args['help_text'])){ |
||
| 715 | $output .= AUI_Component_Helper::help_text($args['help_text']); |
||
| 716 | } |
||
| 717 | |||
| 718 | // maybe horizontal label |
||
| 719 | if($args['label_type']=='horizontal'){ |
||
| 720 | $output .= '</div>'; |
||
| 721 | } |
||
| 722 | |||
| 723 | |||
| 724 | // wrap |
||
| 725 | if(!$args['no_wrap']){ |
||
| 726 | $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
||
| 727 | $output = self::wrap(array( |
||
| 728 | 'content' => $output, |
||
| 729 | 'class' => $wrap_class, |
||
| 730 | )); |
||
| 731 | } |
||
| 732 | |||
| 733 | |||
| 734 | return $output; |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Build the component. |
||
| 739 | * |
||
| 740 | * @param array $args |
||
| 741 | * |
||
| 742 | * @return string The rendered component. |
||
| 743 | */ |
||
| 744 | public static function radio($args = array()){ |
||
| 745 | $defaults = array( |
||
| 746 | 'class' => '', |
||
| 747 | 'id' => '', |
||
| 748 | 'title' => '', |
||
| 749 | 'horizontal' => false, // sets the lable horizontal |
||
| 750 | 'value' => '', |
||
| 751 | 'label' => '', |
||
| 752 | 'label_class'=> '', |
||
| 753 | 'label_type' => '', // sets the lable type, horizontal |
||
| 754 | 'inline' => true, |
||
| 755 | 'required' => false, |
||
| 756 | 'options' => array(), |
||
| 757 | 'icon' => '', |
||
| 758 | 'no_wrap' => false, |
||
| 759 | 'extra_attributes' => array() // an array of extra attributes |
||
| 760 | ); |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Parse incoming $args into an array and merge it with $defaults |
||
| 764 | */ |
||
| 765 | $args = wp_parse_args( $args, $defaults ); |
||
| 766 | |||
| 767 | $label_args = array( |
||
| 768 | 'title'=> $args['label'], |
||
| 769 | 'class' => $args['label_class']." pt-0 ", |
||
| 770 | 'label_type' => $args['label_type'] |
||
| 771 | ); |
||
| 772 | |||
| 773 | $output = ''; |
||
| 774 | |||
| 775 | |||
| 776 | |||
| 777 | // label before |
||
| 778 | if(!empty($args['label'])){ |
||
| 779 | $output .= self::label( $label_args, 'radio' ); |
||
| 780 | } |
||
| 781 | |||
| 782 | // maybe horizontal label |
||
| 783 | if($args['label_type']=='horizontal'){ |
||
| 784 | $output .= '<div class="col-sm-10">'; |
||
| 785 | } |
||
| 786 | |||
| 787 | if(!empty($args['options'])){ |
||
| 788 | $count = 0; |
||
| 789 | foreach($args['options'] as $value => $label){ |
||
| 790 | $option_args = $args; |
||
| 791 | $option_args['value'] = $value; |
||
| 792 | $option_args['label'] = $label; |
||
| 793 | $option_args['checked'] = $value == $args['value'] ? true : false; |
||
| 794 | $output .= self::radio_option($option_args,$count); |
||
| 795 | $count++; |
||
| 796 | } |
||
| 797 | } |
||
| 798 | |||
| 799 | // maybe horizontal label |
||
| 800 | if($args['label_type']=='horizontal'){ |
||
| 801 | $output .= '</div>'; |
||
| 802 | } |
||
| 803 | |||
| 804 | |||
| 805 | // wrap |
||
| 806 | $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
||
| 807 | $output = self::wrap(array( |
||
| 808 | 'content' => $output, |
||
| 809 | 'class' => $wrap_class, |
||
| 810 | )); |
||
| 811 | |||
| 812 | |||
| 813 | return $output; |
||
| 814 | } |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Build the component. |
||
| 818 | * |
||
| 819 | * @param array $args |
||
| 820 | * |
||
| 821 | * @return string The rendered component. |
||
| 822 | */ |
||
| 823 | public static function radio_option($args = array(),$count = ''){ |
||
| 912 | } |
||
| 913 | |||
| 914 | } |