<?php

function construction_engineer_sanitize_select( $construction_engineer_input, $construction_engineer_setting ) {
	$construction_engineer_input = sanitize_key( $construction_engineer_input );
	$construction_engineer_choices = $construction_engineer_setting->manager->get_control( $construction_engineer_setting->id )->choices;
	return ( array_key_exists( $construction_engineer_input, $construction_engineer_choices ) ? $construction_engineer_input : $construction_engineer_setting->default );
}

function construction_engineer_sanitize_switch( $construction_engineer_input ) {
	if ( true === $construction_engineer_input ) {
		return true;
	} else {
		return false;
	}
}

function construction_engineer_sanitize_google_fonts( $construction_engineer_input, $construction_engineer_setting ) {
    $construction_engineer_choices = $construction_engineer_setting->manager->get_control( $construction_engineer_setting->id )->choices;
    return ( array_key_exists( $construction_engineer_input, $construction_engineer_choices ) ? $construction_engineer_input : $construction_engineer_setting->default );
}

/**
 * Sanitize HTML input.
 *
 * @param string $construction_engineer_input HTML input to sanitize.
 * @return string Sanitized HTML.
 */
function construction_engineer_sanitize_html( $construction_engineer_input ) {
    return wp_kses_post( $construction_engineer_input );
}

/**
 * Sanitize URL input.
 *
 * @param string $construction_engineer_input URL input to sanitize.
 * @return string Sanitized URL.
 */
function construction_engineer_sanitize_url( $construction_engineer_input ) {
    return esc_url_raw( $construction_engineer_input );
}

// Sanitize Scroll Top Position
function construction_engineer_sanitize_scroll_top_position( $construction_engineer_input ) {
    $construction_engineer_valid_positions = array( 'bottom-right', 'bottom-left', 'bottom-center' );
    if ( in_array( $construction_engineer_input, $construction_engineer_valid_positions ) ) {
        return $construction_engineer_input;
    } else {
        return 'bottom-right'; // Default to bottom-right if invalid value
    }
}

function construction_engineer_sanitize_image( $construction_engineer_image, $construction_engineer_setting ) {
	/*
	* Array of valid image file types.
	*
	* The array includes image mime types that are included in wp_get_mime_types()
	*/
	$mimes = array(
		'jpg|jpeg|jpe' => 'image/jpeg',
		'gif'          => 'image/gif',
		'png'          => 'image/png',
		'bmp'          => 'image/bmp',
		'tif|tiff'     => 'image/tiff',
		'ico'          => 'image/x-icon',
		'svg'          => 'image/svg+xml',
	);
	// Return an array with file extension and mime_type.
	$construction_engineer_file = wp_check_filetype( $construction_engineer_image, $mimes );
	// If $construction_engineer_image has a valid mime_type, return it; otherwise, return the default.
	return ( $construction_engineer_file['ext'] ? $construction_engineer_image : $construction_engineer_setting->default );
}

function construction_engineer_sanitize_choices( $construction_engineer_input, $construction_engineer_setting ) {
    global $wp_customize; 
    $construction_engineer_control = $wp_customize->get_control( $construction_engineer_setting->id ); 
    if ( array_key_exists( $construction_engineer_input, $construction_engineer_control->choices ) ) {
        return $construction_engineer_input;
    } else {
        return $construction_engineer_setting->default;
    }
}

function construction_engineer_sanitize_range_value( $construction_engineer_number, $construction_engineer_setting ) {

	// Ensure input is an absolute integer.
	$construction_engineer_number = absint( $construction_engineer_number );

	// Get the input attributes associated with the setting.
	$construction_engineer_atts = $construction_engineer_setting->manager->get_control( $construction_engineer_setting->id )->input_attrs;

	// Get minimum number in the range.
	$construction_engineer_min = ( isset( $construction_engineer_atts['min'] ) ? $construction_engineer_atts['min'] : $construction_engineer_number );

	// Get maximum number in the range.
	$construction_engineer_max = ( isset( $construction_engineer_atts['max'] ) ? $construction_engineer_atts['max'] : $construction_engineer_number );

	// Get step.
	$construction_engineer_step = ( isset( $construction_engineer_atts['step'] ) ? $construction_engineer_atts['step'] : 1 );

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $construction_engineer_min <= $construction_engineer_number && $construction_engineer_number <= $construction_engineer_max && is_int( $construction_engineer_number / $construction_engineer_step ) ? $construction_engineer_number : $construction_engineer_setting->default );
}