| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 191 |
| Code Lines | 101 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | 'validation_text' => '', |
||
| 35 | 'validation_pattern' => '', |
||
| 36 | 'no_wrap' => false, |
||
| 37 | 'input_group_right' => '', |
||
| 38 | 'input_group_left' => '', |
||
| 39 | 'input_group_right_inside' => false, // forces the input group inside the input |
||
| 40 | 'input_group_left_inside' => false, // forces the input group inside the input |
||
| 41 | 'step' => '', |
||
| 42 | 'switch' => false, // to show checkbox as a switch |
||
| 43 | 'checked' => false, // set a checkbox or radio as selected |
||
| 44 | 'password_toggle' => true, // toggle view/hide password |
||
| 45 | 'extra_attributes' => array() // an array of extra attributes |
||
| 46 | ); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Parse incoming $args into an array and merge it with $defaults |
||
| 50 | */ |
||
| 51 | $args = wp_parse_args( $args, $defaults ); |
||
| 52 | $output = ''; |
||
| 53 | if ( ! empty( $args['type'] ) ) { |
||
| 54 | $type = sanitize_html_class( $args['type'] ); |
||
| 55 | $label_args = array('title'=>$args['label'],'for'=>$args['id'],'class' => $args['label_class']." "); |
||
| 56 | |||
| 57 | // Some special sauce for files |
||
| 58 | if($type=='file' ){ |
||
| 59 | $args['label_after'] = true; // if type file we need the label after |
||
| 60 | $args['class'] .= ' custom-file-input '; |
||
| 61 | }elseif($type=='checkbox'){ |
||
| 62 | $args['label_after'] = true; // if type file we need the label after |
||
| 63 | $args['class'] .= ' custom-control-input '; |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | // label before |
||
| 68 | if(!empty($args['label']) && !$args['label_after']){ |
||
| 69 | if($type == 'file'){$label_args['class'] .= 'custom-file-label';} |
||
| 70 | $output .= self::label( $label_args, $type ); |
||
| 71 | } |
||
| 72 | |||
| 73 | // open/type |
||
| 74 | $output .= '<input type="' . $type . '" '; |
||
| 75 | |||
| 76 | // name |
||
| 77 | if(!empty($args['name'])){ |
||
| 78 | $output .= ' name="'.sanitize_html_class($args['name']).'" '; |
||
| 79 | } |
||
| 80 | |||
| 81 | // id |
||
| 82 | if(!empty($args['id'])){ |
||
| 83 | $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
||
| 84 | } |
||
| 85 | |||
| 86 | // placeholder |
||
| 87 | if(!empty($args['placeholder'])){ |
||
| 88 | $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
||
| 89 | } |
||
| 90 | |||
| 91 | // title |
||
| 92 | if(!empty($args['title'])){ |
||
| 93 | $output .= ' title="'.esc_attr($args['title']).'" '; |
||
| 94 | } |
||
| 95 | |||
| 96 | // value |
||
| 97 | if(!empty($args['value'])){ |
||
| 98 | $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
||
| 99 | } |
||
| 100 | |||
| 101 | // checked, for radio and checkboxes |
||
| 102 | if( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ){ |
||
| 103 | $output .= ' checked '; |
||
| 104 | } |
||
| 105 | |||
| 106 | // validation text |
||
| 107 | if(!empty($args['validation_text'])){ |
||
| 108 | $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
||
| 109 | $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
||
| 110 | } |
||
| 111 | |||
| 112 | // validation_pattern |
||
| 113 | if(!empty($args['validation_pattern'])){ |
||
| 114 | $output .= ' pattern="'.$args['validation_pattern'].'" '; |
||
| 115 | } |
||
| 116 | |||
| 117 | // step (for numbers) |
||
| 118 | if(!empty($args['step'])){ |
||
| 119 | $output .= ' step="'.$args['step'].'" '; |
||
| 120 | } |
||
| 121 | |||
| 122 | // required |
||
| 123 | if(!empty($args['required'])){ |
||
| 124 | $output .= ' required '; |
||
| 125 | } |
||
| 126 | |||
| 127 | // class |
||
| 128 | $class = !empty($args['class']) ? $args['class'] : ''; |
||
| 129 | $output .= ' class="form-control '.$class.'" '; |
||
| 130 | |||
| 131 | // data-attributes |
||
| 132 | $output .= AUI_Component_Helper::data_attributes($args); |
||
| 133 | |||
| 134 | // extra attributes |
||
| 135 | if(!empty($args['extra_attributes'])){ |
||
| 136 | $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
||
| 137 | } |
||
| 138 | |||
| 139 | // close |
||
| 140 | $output .= ' >'; |
||
| 141 | |||
| 142 | // label after |
||
| 143 | if(!empty($args['label']) && $args['label_after']){ |
||
| 144 | if($type == 'file'){$label_args['class'] .= 'custom-file-label';} |
||
| 145 | elseif($type == 'checkbox'){$label_args['class'] .= 'custom-control-label';} |
||
| 146 | $output .= self::label( $label_args, $type ); |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | // some input types need a separate wrap |
||
| 151 | if($type == 'file') { |
||
| 152 | $output = self::wrap( array( |
||
| 153 | 'content' => $output, |
||
| 154 | 'class' => 'form-group custom-file' |
||
| 155 | ) ); |
||
| 156 | }elseif($type == 'checkbox'){ |
||
| 157 | $wrap_class = $args['switch'] ? 'custom-switch' : 'custom-checkbox'; |
||
| 158 | $output = self::wrap( array( |
||
| 159 | 'content' => $output, |
||
| 160 | 'class' => 'custom-control '.$wrap_class |
||
| 161 | ) ); |
||
| 162 | }elseif($type == 'password' && $args['password_toggle'] && !$args['input_group_right']){ |
||
| 163 | |||
| 164 | |||
| 165 | // allow password field to toggle view |
||
| 166 | $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
||
| 167 | onclick="var $el = jQuery(this).find(\'i\');$el.toggleClass(\'fa-eye fa-eye-slash\'); |
||
| 168 | var $eli = jQuery(this).parent().parent().find(\'input\'); |
||
| 169 | if($el.hasClass(\'fa-eye\')) |
||
| 170 | {$eli.attr(\'type\',\'text\');} |
||
| 171 | else{$eli.attr(\'type\',\'password\');}" |
||
| 172 | ><i class="far fa-fw fa-eye-slash"></i></span>'; |
||
| 173 | } |
||
| 174 | |||
| 175 | // input group wraps |
||
| 176 | if($args['input_group_left'] || $args['input_group_right']){ |
||
| 177 | $w100 = strpos($args['class'], 'w-100') !== false ? ' w-100' : ''; |
||
| 178 | if($args['input_group_left']){ |
||
| 179 | $output = self::wrap( array( |
||
| 180 | 'content' => $output, |
||
| 181 | 'class' => $args['input_group_left_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
||
| 182 | 'input_group_left' => $args['input_group_left'], |
||
| 183 | 'input_group_left_inside' => $args['input_group_left_inside'] |
||
| 184 | ) ); |
||
| 185 | } |
||
| 186 | if($args['input_group_right']){ |
||
| 187 | $output = self::wrap( array( |
||
| 188 | 'content' => $output, |
||
| 189 | 'class' => $args['input_group_right_inside'] ? 'input-group-inside'.$w100 : 'input-group', |
||
| 190 | 'input_group_right' => $args['input_group_right'], |
||
| 191 | 'input_group_right_inside' => $args['input_group_right_inside'] |
||
| 192 | ) ); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Labels need to be on the outside of the wrap |
||
| 196 | $label = self::label( $label_args, $type ); |
||
| 197 | $output = $label . str_replace($label,"",$output); |
||
| 198 | } |
||
| 199 | |||
| 200 | // wrap |
||
| 201 | if(!$args['no_wrap']){ |
||
| 202 | $output = self::wrap(array( |
||
| 203 | 'content' => $output, |
||
| 204 | )); |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | } |
||
| 210 | |||
| 211 | return $output; |
||
| 212 | } |
||
| 614 | } |