| 1 |
<?php |
| 2 |
/** |
| 3 |
* Show a toggle with or without labels. |
| 4 |
* |
| 5 |
* @package Formidable |
| 6 |
* |
| 7 |
* @var string $id The HTML id. |
| 8 |
* @var string $name The HTML name. |
| 9 |
* @var array $args Pass args. |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
die( 'You are not allowed to call this page directly.' ); |
| 14 |
} |
| 15 |
?> |
| 16 |
<?php |
| 17 |
$div_class = isset( $args['div_class'] ) ? $args['div_class'] : false; |
| 18 |
$show_labels = isset( $args['show_labels'] ) ? $args['show_labels'] : false; |
| 19 |
$off_label = isset( $args['off_label'] ) ? $args['off_label'] : ''; |
| 20 |
$on_label = isset( $args['on_label'] ) ? $args['on_label'] : 1; |
| 21 |
$value = isset( $args['value'] ) ? $args['value'] : $on_label; |
| 22 |
$checked = isset( $args['checked'] ) && ( true === $args['checked'] || false !== strpos( $args['checked'], 'checked="checked"' ) ); |
| 23 |
$disabled = ! empty( $args['disabled'] ); |
| 24 |
$aria_checked = $checked ? 'true' : 'false'; |
| 25 |
$input_html = isset( $args['input_html'] ) ? $args['input_html'] : array(); |
| 26 |
|
| 27 |
$use_container = false; |
| 28 |
|
| 29 |
$div_params = array( |
| 30 |
// This is important when the default style is !important as Pro styling may cause conflicts. |
| 31 |
// It uses --toggle-on-color so just set the variable. |
| 32 |
'style' => '--toggle-on-color:var(--primary-color);', |
| 33 |
); |
| 34 |
if ( $div_class ) { |
| 35 |
$use_container = true; |
| 36 |
$div_params['class'] = $div_class; |
| 37 |
} |
| 38 |
|
| 39 |
if ( strpos( $name, '[' ) === false ) { |
| 40 |
$name .= '[]'; |
| 41 |
} |
| 42 |
|
| 43 |
if ( $use_container ) { |
| 44 |
?> |
| 45 |
<div <?php FrmAppHelper::array_to_html_params( $div_params, true ); ?>> |
| 46 |
<?php |
| 47 |
$div_params = array(); |
| 48 |
} |
| 49 |
?> |
| 50 |
<label class="frm_toggle_block" <?php FrmAppHelper::array_to_html_params( $div_params, true ); ?>> |
| 51 |
<?php if ( $show_labels && $off_label ) { ?> |
| 52 |
<span class="frm_off_label frm_toggle_opt"><?php echo esc_html( $off_label ); ?></span> |
| 53 |
<?php } ?> |
| 54 |
|
| 55 |
<input type="checkbox" name="<?php echo esc_attr( $name ); ?>" id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $value ); ?>" |
| 56 |
<?php checked( $checked, true ); ?> |
| 57 |
<?php if ( $show_labels && $off_label ) { ?> |
| 58 |
data-off="<?php echo esc_attr( $off_label ); ?>" |
| 59 |
<?php } ?> |
| 60 |
<?php if ( $disabled ) { ?> |
| 61 |
disabled |
| 62 |
<?php } ?> |
| 63 |
<?php |
| 64 |
if ( is_array( $input_html ) ) { |
| 65 |
FrmAppHelper::array_to_html_params( $input_html, true ); |
| 66 |
} else { |
| 67 |
_doing_it_wrong( '$args[input_html]', 'An array is required', '6.0' ); |
| 68 |
} |
| 69 |
?> |
| 70 |
/> |
| 71 |
|
| 72 |
<span class="frm_toggle" tabindex="0" role="switch" |
| 73 |
<?php echo ! empty( $args['aria-label-attr'] ) ? 'aria-label="' . esc_attr( $args['aria-label-attr'] ) . '"' : ''; ?> |
| 74 |
aria-labelledby="<?php echo esc_attr( $id ); ?>_label" |
| 75 |
aria-checked="<?php echo esc_attr( $aria_checked ); ?>" |
| 76 |
> |
| 77 |
<span class="frm_toggle_slider"></span> |
| 78 |
</span> |
| 79 |
|
| 80 |
<?php if ( $show_labels && $on_label != 1 ) { ?> |
| 81 |
<span class="frm_on_label frm_toggle_opt"><?php FrmAppHelper::kses_echo( $on_label, 'all' ); ?></span> |
| 82 |
<?php } ?> |
| 83 |
</label> |
| 84 |
<?php if ( $use_container ) { ?> |
| 85 |
</div> |
| 86 |
<?php } ?> |
| 87 |
|