'text',
'name' => '',
'class' => '',
'wrap_class' => '',
'id' => '',
'placeholder' => '',
'title' => '',
'value' => '',
'required' => false,
'size' => '', // sm, lg, small, large
'clear_icon' => '', // true will show a clear icon, can't be used with input_group_right
'with_hidden' => false, // Append hidden field for single checkbox.
'label' => '',
'label_after' => false,
'label_class' => '',
'label_col' => '2',
'label_type' => '', // top, horizontal, empty = hidden
'label_force_left' => false, // used to force checkbox label left when using horizontal
// sets the label type, default: hidden. Options: hidden, top, horizontal, floating
'help_text' => '',
'validation_text' => '',
'validation_pattern' => '',
'no_wrap' => false,
'input_group_right' => '',
'input_group_left' => '',
'input_group_right_inside' => false,
// forces the input group inside the input
'input_group_left_inside' => false,
// forces the input group inside the input
'form_group_class' => '',
'step' => '',
'switch' => false,
// to show checkbox as a switch
'checked' => false,
// set a checkbox or radio as selected
'password_toggle' => true,
// toggle view/hide password
'element_require' => '',
// [%element_id%] == "1"
'extra_attributes' => array(),
// an array of extra attributes
'wrap_attributes' => array()
);
/**
* Parse incoming $args into an array and merge it with $defaults
*/
$args = wp_parse_args( $args, $defaults );
$output = '';
if ( ! empty( $args['type'] ) ) {
// hidden label option needs to be empty
$args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type'];
$type = sanitize_html_class( $args['type'] );
$help_text = '';
$label = '';
$label_after = $args['label_after'];
$label_args = array(
'title' => $args['label'],
'for' => $args['id'],
'class' => $args['label_class'] . " ",
'label_type' => $args['label_type'],
'label_col' => $args['label_col']
);
// floating labels need label after
if ( $args['label_type'] == 'floating' && $type != 'checkbox' ) {
$label_after = true;
$args['placeholder'] = ' '; // set the placeholder not empty so the floating label works.
}
// size
$size = '';
if ( $args['size'] == 'lg' || $args['size'] == 'large' ) {
$size = 'lg';
$args['class'] .= ' form-control-lg';
}elseif ( $args['size'] == 'sm' || $args['size'] == 'small' ) {
$size = 'sm';
$args['class'] .= ' form-control-sm';
}
// clear function
$clear_function = 'jQuery(this).parent().parent().find(\'input\').val(\'\');';
// Some special sauce for files
if ( $type == 'file' ) {
$label_after = true; // if type file we need the label after
$args['class'] .= ' custom-file-input ';
} elseif ( $type == 'checkbox' ) {
$label_after = true; // if type file we need the label after
$args['class'] .= $aui_bs5 ? ' form-check-input c-pointer ' : ' custom-control-input c-pointer ';
} elseif ( $type == 'datepicker' || $type == 'timepicker' ) {
$orig_type = $type;
$type = 'text';
$args['class'] .= ' bg-initial '; // @todo not sure why we have this?
$clear_function .= "jQuery(this).parent().parent().find('input[name=\'" . esc_attr( $args['name'] ) . "\']').trigger('change');";
$args['extra_attributes']['data-aui-init'] = 'flatpickr';
// Disable native datetime inputs.
$disable_mobile_attr = isset( $args['extra_attributes']['data-disable-mobile'] ) ? $args['extra_attributes']['data-disable-mobile'] : 'true';
$disable_mobile_attr = apply_filters( 'aui_flatpickr_disable_disable_mobile_attr', $disable_mobile_attr, $args );
$args['extra_attributes']['data-disable-mobile'] = $disable_mobile_attr;
// set a way to clear field if empty
if ( $args['input_group_right'] === '' && $args['clear_icon'] !== false ) {
$args['input_group_right_inside'] = true;
$args['clear_icon'] = true;
}
// enqueue the script
$aui_settings = AyeCode_UI_Settings::instance();
$aui_settings->enqueue_flatpickr();
} else if ( $type == 'iconpicker' ) {
$type = 'text';
// Validate FA icon.
$args['value'] = AUI_Component_Helper::sanitize_fa_icon( $args['value'], $args );
$args['extra_attributes']['data-aui-init'] = 'iconpicker';
$args['extra_attributes']['data-placement'] = 'bottomRight';
$args['input_group_right'] = '';
// Enqueue the script
$aui_settings = AyeCode_UI_Settings::instance();
$aui_settings->enqueue_iconpicker();
}
if ( $type == 'checkbox' && ( ( ! empty( $args['name'] ) && strpos( $args['name'], '[' ) === false ) || ! empty( $args['with_hidden'] ) ) ) {
$output .= '';
}
// allow clear icon
if ( $args['input_group_right'] === '' && $args['clear_icon'] ) {
$font_size = $size == 'sm' ? '1.3' : ( $size == 'lg' ? '1.65' : '1.5' );
$args['input_group_right_inside'] = true;
$align_class = $aui_bs5 ? ' h-100 py-0' : '';
$args['input_group_right'] = '' . ( $aui_bs5 ? '' : '×' ) . '';
}
// open/type
$output .= ' $output,
'class' => $aui_bs5 ? 'mb-3 custom-file' : 'form-group custom-file'
) );
} elseif ( $type == 'checkbox' ) {
$label_args['title'] = $args['label'];
$label_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'label' );
$label = !empty( $args['label_force_left'] ) ? self::label( $label_args, 'cb' ) : '
';
$switch_size_class = $args['switch'] && !is_bool( $args['switch'] ) ? ' custom-switch-'.esc_attr( $args['switch'] ) : '';
if ( $aui_bs5 ) {
$wrap_class = $args['switch'] ? 'form-check form-switch' . $switch_size_class : 'form-check';
}else{
$wrap_class = $args['switch'] ? 'custom-switch' . $switch_size_class : 'custom-checkbox' ;
}
if ( ! empty( $args['label_force_left'] ) ) {
$wrap_class .= $aui_bs5 ? '' : ' d-flex align-content-center';
$label = str_replace(array("form-check-label","custom-control-label"),"", self::label( $label_args, 'cb' ) );
}
$output = self::wrap( array(
'content' => $output,
'class' => $aui_bs5 ? $wrap_class : 'custom-control ' . $wrap_class
) );
if ( $args['label_type'] == 'horizontal' ) {
$input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' );
$output = $label . '' . $output . '
';
}
} elseif ( $type == 'password' && $args['password_toggle'] && ! $args['input_group_right'] ) {
// allow password field to toggle view
$args['input_group_right'] = '';
}
// input group wraps
if ( $args['input_group_left'] || $args['input_group_right'] ) {
$w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : '';
$group_size = $size == 'lg' ? ' input-group-lg' : '';
$group_size = !$group_size && $size == 'sm' ? ' input-group-sm' : $group_size;
if ( $args['input_group_left'] ) {
$output = self::wrap( array(
'content' => $output,
'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size,
'input_group_left' => $args['input_group_left'],
'input_group_left_inside' => $args['input_group_left_inside']
) );
}
if ( $args['input_group_right'] ) {
$output = self::wrap( array(
'content' => $output,
'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size,
'input_group_right' => $args['input_group_right'],
'input_group_right_inside' => $args['input_group_right_inside']
) );
}
}
if ( ! $label_after ) {
$output .= $help_text;
}
if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) {
$output = self::wrap( array(
'content' => $output,
'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' )
) );
}
if ( ! $label_after ) {
$output = $label . $output;
}
// wrap
if ( ! $args['no_wrap'] ) {
if ( ! empty( $args['form_group_class'] ) ) {
$fg_class = esc_attr( $args['form_group_class'] );
}else{
$fg_class = $aui_bs5 ? 'mb-3' : 'form-group';
}
$form_group_class = $args['label_type'] == 'floating' && $type != 'checkbox' ? 'form-label-group' : $fg_class;
$wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class;
$wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class;
$output = self::wrap( array(
'content' => $output,
'class' => $wrap_class,
'element_require' => $args['element_require'],
'argument_id' => $args['id'],
'wrap_attributes' => $args['wrap_attributes'],
) );
}
}
return $output;
}
public static function label( $args = array(), $type = '' ) {
global $aui_bs5;
$defaults = array(
'title' => 'div',
'for' => '',
'class' => '',
'label_type' => '', // empty = hidden, top, horizontal
'label_col' => '',
);
/**
* Parse incoming $args into an array and merge it with $defaults
*/
$args = wp_parse_args( $args, $defaults );
$output = '';
if ( $args['title'] ) {
// maybe hide labels //@todo set a global option for visibility class
if ( $type == 'file' || $type == 'checkbox' || $type == 'radio' || ! empty( $args['label_type'] ) ) {
$class = $args['class'];
} else {
$class = 'sr-only ' . ( $aui_bs5 ? 'visually-hidden ' : '' ) . $args['class'];
}
// maybe horizontal
if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) {
$class .= ' ' . AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ) . ' col-form-label ' . $type;
}
if ( $aui_bs5 ) {
$class .= ' form-label';
}
// open
$output .= '