<?php

/**
 * Sanitize select field
 *
 * @param  string $dark_fitness_input   Selected input
 * @param  string $dark_fitness_setting Input setting
 */

function dark_fitness_sanitize_select( $dark_fitness_input, $dark_fitness_setting ) {
	// input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only.
	$dark_fitness_input = sanitize_key( $dark_fitness_input );

	// get the list of possible select options.
	$dark_fitness_choices = $dark_fitness_setting->manager->get_control( $dark_fitness_setting->id )->choices;

	// return input if valid or return default option.
	return ( array_key_exists( $dark_fitness_input, $dark_fitness_choices ) ? $dark_fitness_input : $dark_fitness_setting->default );
}

/**
 * Sanitize switch control
 *
 * @param  string   Switch value
 * @return integer  Sanitized value
 */
function dark_fitness_sanitize_switch( $dark_fitness_input ) {
	if ( true === $dark_fitness_input ) {
		return true;
	} else {
		return false;
	}
}

function dark_fitness_sanitize_image( $dark_fitness_image, $dark_fitness_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.
	$file = wp_check_filetype( $dark_fitness_image, $mimes );
	// If $dark_fitness_image has a valid mime_type, return it; otherwise, return the default.
	return ( $file['ext'] ? $dark_fitness_image : $dark_fitness_setting->default );
}

function dark_fitness_sanitize_google_fonts( $dark_fitness_input, $dark_fitness_setting ) {
	// Get list of choices from the control associated with the setting.
	$dark_fitness_choices = $dark_fitness_setting->manager->get_control( $dark_fitness_setting->id )->choices;

	// If the input is a valid key, return it; otherwise, return the default.
	return ( array_key_exists( $dark_fitness_input, $dark_fitness_choices ) ? $dark_fitness_input : $dark_fitness_setting->default );
}

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

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

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

function dark_fitness_sanitize_choices( $dark_fitness_input, $dark_fitness_setting ) {
    global $wp_customize; 
    $dark_fitness_control = $wp_customize->get_control( $dark_fitness_setting->id ); 
    if ( array_key_exists( $dark_fitness_input, $dark_fitness_control->choices ) ) {
        return $dark_fitness_input;
    } else {
        return $dark_fitness_setting->default;
    }
}

function dark_fitness_sanitize_range_value( $dark_fitness_number, $dark_fitness_setting ) {

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

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

	// Get minimum number in the range.
	$dark_fitness_min = ( isset( $dark_fitness_atts['min'] ) ? $dark_fitness_atts['min'] : $dark_fitness_number );

	// Get maximum number in the range.
	$dark_fitness_max = ( isset( $dark_fitness_atts['max'] ) ? $dark_fitness_atts['max'] : $dark_fitness_number );

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

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $dark_fitness_min <= $dark_fitness_number && $dark_fitness_number <= $dark_fitness_max && is_int( $dark_fitness_number / $dark_fitness_step ) ? $dark_fitness_number : $dark_fitness_setting->default );
}