<?php

/**
 * This template displaying switch addon item.
 * This template can be overridden by copying it to yourtheme/eopfw-templates/epofw-switch.php
 * NOTE, on occasion Extra Product Options for WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @package Extra_Product_Options_For_WooCommerce/Templates
 * @version 4.1
 */
defined( 'ABSPATH' ) || exit;
$epofw_args = array();
$epofw_field_type = ( isset( $args['epofwtwp_args']['attr_data']['type'] ) ? $args['epofwtwp_args']['attr_data']['type'] : 'switch' );
$epofw_sanitize_attr_data = array();
// Get switch settings.
$epofw_switch_layout = 'default';
$epofw_switch_animation = '';
if ( !empty( $args['epofwtwp_args']['attr_data'] ) ) {
    // Set default value to "Yes" for switch field - this ensures a value is submitted when checked.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
    $args['epofwtwp_args']['attr_data']['value'] = ( !empty( $args['epofwtwp_args']['attr_data']['value'] ) ? $args['epofwtwp_args']['attr_data']['value'] : '' );
    foreach ( $args['epofwtwp_args']['attr_data'] as $epofw_attr_name => $epofw_attr_value ) {
        $epofw_sanitize_attr_data['input'][$epofw_attr_name] = true;
        if ( 'name' === $epofw_attr_name ) {
            if ( strpos( $epofw_attr_value, 'epofw_field_' ) === false ) {
                $epofw_attr_value = 'epofw_field_' . $epofw_attr_value;
            }
            /**
             * Apply filters for switch name field property.
             *
             * @since 4.1
             */
            $epofw_args['name'] = apply_filters( 'epofw_field_property_' . $epofw_field_type . '_' . $epofw_attr_name, $epofw_attr_value . '[value][]', $args );
        } elseif ( 'type' === $epofw_attr_name ) {
            /**
             * Apply filters for checkboxgroup type field property.
             *
             * @since 4.1
             */
            $epofw_args[$epofw_attr_name] = apply_filters( 'epofw_field_property_' . $epofw_field_type . '_' . $epofw_attr_name, 'checkbox', $args );
        } elseif ( 'class' === $epofw_attr_name ) {
            $epofw_args['class'] = $epofw_attr_value . ' epofw-switch-input';
        } elseif ( 'value' === $epofw_attr_name ) {
            // Use "Yes" as default value when empty - ensures price calculation works when switch is checked.
            $epofw_args['value'] = ( !empty( $epofw_attr_value ) ? $epofw_attr_value : __( 'Yes', 'extra-product-options-for-woocommerce' ) );
            if ( 'on' === $epofw_attr_value ) {
                $epofw_args['checked'] = true;
                $epofw_sanitize_attr_data['input']['checked'] = true;
                $epofw_sanitize_attr_data['input']['value'] = true;
            }
        } elseif ( !empty( $epofw_attr_value ) ) {
            /**
             * Apply filters for switch field property.
             *
             * @since 4.1
             */
            $epofw_args[$epofw_attr_name] = apply_filters( 'epofw_field_property_' . $epofw_field_type . '_' . $epofw_attr_name, $epofw_attr_value, $args );
        }
    }
}
$epofw_check_hop = ( isset( $args['epofwtwp_args']['fields_data']['common']['hop'] ) ? $args['epofwtwp_args']['fields_data']['common']['hop'] : '' );
$epofw_unique_field_slug = 'epofw_' . $epofw_field_type . '_field';
/**
 * Before addon field.
 *
 * @since 4.1
 */
do_action( 'epofw_before_addon_field_' . $epofw_field_type );
$epofw_style = '';
$epofw_switch_classes = array();
// Generate unique ID for switch input if not already set.
$epofw_switch_id = ( isset( $epofw_args['id'] ) ? $epofw_args['id'] : 'epofw_switch_' . wp_rand() );
if ( !isset( $epofw_args['id'] ) ) {
    $epofw_args['id'] = $epofw_switch_id;
}
?>
	<label class="epofw-switch-label" for="<?php 
echo esc_attr( $epofw_switch_id );
?>">
		<div class="epofw-switch-container <?php 
echo esc_attr( implode( ' ', $epofw_switch_classes ) );
?>" style="<?php 
echo esc_attr( $epofw_style );
?>">
			<?php 
$epofw_html = '<input ';
foreach ( $epofw_args as $epofw_attr_name => $epofw_attr_value ) {
    if ( !isset( $epofw_sanitize_attr_data['input'][$epofw_attr_name] ) ) {
        continue;
    }
    // Override id attribute with unique ID.
    if ( 'id' === $epofw_attr_name ) {
        $epofw_attr_value = $epofw_switch_id;
    }
    $epofw_html .= ' ' . sanitize_key( $epofw_attr_name ) . EPOFW_EQUAL . '"' . esc_attr( sanitize_text_field( $epofw_attr_value ) ) . '"';
}
$epofw_html .= '/>';
echo wp_kses( $epofw_html, $epofw_sanitize_attr_data );
?>
			<span class="epofw-switch-slider">
				<?php 
?>
			</span>
		</div>
	</label>
<?php 
/**
 * After addon field.
 *
 * @since 4.1
 */
do_action( 'epofw_after_addon_field_' . $epofw_field_type );