<?php

function aster_ebooks_sanitize_select( $aster_ebooks_input, $aster_ebooks_setting ) {
	$aster_ebooks_input = sanitize_key( $aster_ebooks_input );
	$aster_ebooks_choices = $aster_ebooks_setting->manager->get_control( $aster_ebooks_setting->id )->choices;
	return ( array_key_exists( $aster_ebooks_input, $aster_ebooks_choices ) ? $aster_ebooks_input : $aster_ebooks_setting->default );
}

function aster_ebooks_sanitize_switch( $aster_ebooks_input ) {
	if ( true === $aster_ebooks_input ) {
		return true;
	} else {
		return false;
	}
}

function aster_ebooks_sanitize_google_fonts( $aster_ebooks_input, $aster_ebooks_setting ) {
	$aster_ebooks_choices = $aster_ebooks_setting->manager->get_control( $aster_ebooks_setting->id )->choices;
	return ( array_key_exists( $aster_ebooks_input, $aster_ebooks_choices ) ? $aster_ebooks_input : $aster_ebooks_setting->default );
}
/**
 * Sanitize HTML input.
 *
 * @param string $aster_ebooks_input HTML input to sanitize.
 * @return string Sanitized HTML.
 */
function aster_ebooks_sanitize_html( $aster_ebooks_input ) {
    return wp_kses_post( $aster_ebooks_input );
}

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

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

function aster_ebooks_sanitize_choices( $aster_ebooks_input, $aster_ebooks_setting ) {
	global $wp_customize; 
	$aster_ebooks_control = $wp_customize->get_control( $aster_ebooks_setting->id ); 
	if ( array_key_exists( $aster_ebooks_input, $aster_ebooks_control->choices ) ) {
		return $aster_ebooks_input;
	} else {
		return $aster_ebooks_setting->default;
	}
}

function aster_ebooks_sanitize_range_value( $aster_ebooks_number, $aster_ebooks_setting ) {

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

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

	// Get minimum number in the range.
	$aster_ebooks_min = ( isset( $aster_ebooks_atts['min'] ) ? $aster_ebooks_atts['min'] : $aster_ebooks_number );

	// Get maximum number in the range.
	$aster_ebooks_max = ( isset( $aster_ebooks_atts['max'] ) ? $aster_ebooks_atts['max'] : $aster_ebooks_number );

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

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $aster_ebooks_min <= $aster_ebooks_number && $aster_ebooks_number <= $aster_ebooks_max && is_int( $aster_ebooks_number / $aster_ebooks_step ) ? $aster_ebooks_number : $aster_ebooks_setting->default );
}