<?php

function gamers_studio_sanitize_select( $gamers_studio_input, $gamers_studio_setting ) {
	$gamers_studio_input = sanitize_key( $gamers_studio_input );
	$gamers_studio_choices = $gamers_studio_setting->manager->get_control( $gamers_studio_setting->id )->choices;
	return ( array_key_exists( $gamers_studio_input, $gamers_studio_choices ) ? $gamers_studio_input : $gamers_studio_setting->default );
}

function gamers_studio_sanitize_switch( $gamers_studio_input ) {
	if ( true === $gamers_studio_input ) {
		return true;
	} else {
		return false;
	}
}

function gamers_studio_sanitize_google_fonts( $gamers_studio_input, $gamers_studio_setting ) {
	$gamers_studio_choices = $gamers_studio_setting->manager->get_control( $gamers_studio_setting->id )->choices;
	return ( array_key_exists( $gamers_studio_input, $gamers_studio_choices ) ? $gamers_studio_input : $gamers_studio_setting->default );
}

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

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

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

function gamers_studio_sanitize_image( $gamers_studio_image, $gamers_studio_setting ) {
	/*
	* Array of valid image file types.
	*
	* The array includes image mime types that are included in wp_get_mime_types()
	*/
	$gamers_studio_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.
	$file = wp_check_filetype( $gamers_studio_image, $gamers_studio_mimes );
	// If $gamers_studio_image has a valid mime_type, return it; otherwise, return the default.
	return ( $file['ext'] ? $gamers_studio_image : $gamers_studio_setting->default );
}

function gamers_studio_sanitize_choices( $gamers_studio_input, $gamers_studio_setting ) {
    global $wp_customize; 
    $control = $wp_customize->get_control( $gamers_studio_setting->id ); 
    if ( array_key_exists( $gamers_studio_input, $control->choices ) ) {
        return $gamers_studio_input;
    } else {
        return $gamers_studio_setting->default;
    }
}

function gamers_studio_sanitize_range_value( $gamers_studio_number, $gamers_studio_setting ) {

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

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

	// Get minimum number in the range.
	$gamers_studio_min = ( isset( $gamers_studio_atts['min'] ) ? $gamers_studio_atts['min'] : $gamers_studio_number );

	// Get maximum number in the range.
	$gamers_studio_max = ( isset( $gamers_studio_atts['max'] ) ? $gamers_studio_atts['max'] : $gamers_studio_number );

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

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $gamers_studio_min <= $gamers_studio_number && $gamers_studio_number <= $gamers_studio_max && is_int( $gamers_studio_number / $gamers_studio_step ) ? $gamers_studio_number : $gamers_studio_setting->default );
}

function gamers_studio_sanitize_color_or_gradient( $gamers_studio_value ) {
    if ( preg_match( '/^#[a-f0-9]{6}$/i', $gamers_studio_value ) || preg_match( '/^linear-gradient\((.+)\)$/i', $gamers_studio_value ) ) {
        return $gamers_studio_value;
    }
    return '';
}