<?php

function aster_travel_blog_sanitize_select( $aster_travel_blog_input, $aster_travel_blog_setting ) {
	$aster_travel_blog_input = sanitize_key( $aster_travel_blog_input );
	$aster_travel_blog_choices = $aster_travel_blog_setting->manager->get_control( $aster_travel_blog_setting->id )->choices;
	return ( array_key_exists( $aster_travel_blog_input, $aster_travel_blog_choices ) ? $aster_travel_blog_input : $aster_travel_blog_setting->default );
}

function aster_travel_blog_sanitize_switch( $aster_travel_blog_input ) {
	if ( true === $aster_travel_blog_input ) {
		return true;
	} else {
		return false;
	}
}

function aster_travel_blog_sanitize_google_fonts( $aster_travel_blog_input, $aster_travel_blog_setting ) {
	$aster_travel_blog_choices = $aster_travel_blog_setting->manager->get_control( $aster_travel_blog_setting->id )->choices;
	return ( array_key_exists( $aster_travel_blog_input, $aster_travel_blog_choices ) ? $aster_travel_blog_input : $aster_travel_blog_setting->default );
}

function aster_travel_blog_sanitize_choices( $aster_travel_blog_input, $aster_travel_blog_setting ) {
    global $wp_customize;
    $control = $wp_customize->get_control( $aster_travel_blog_setting->id );
    if ( array_key_exists( $aster_travel_blog_input, $control->choices ) ) {
        return $aster_travel_blog_input;
    } else {
        return $aster_travel_blog_setting->default;
    }
}

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

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

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

function aster_travel_blog_sanitize_range_value( $aster_travel_blog_number, $aster_travel_blog_setting ) {

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

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

	// Get minimum number in the range.
	$aster_travel_blog_min = ( isset( $aster_travel_blog_atts['min'] ) ? $aster_travel_blog_atts['min'] : $aster_travel_blog_number );

	// Get maximum number in the range.
	$aster_travel_blog_max = ( isset( $aster_travel_blog_atts['max'] ) ? $aster_travel_blog_atts['max'] : $aster_travel_blog_number );

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

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $aster_travel_blog_min <= $aster_travel_blog_number && $aster_travel_blog_number <= $aster_travel_blog_max && is_int( $aster_travel_blog_number / $aster_travel_blog_step ) ? $aster_travel_blog_number : $aster_travel_blog_setting->default );
}