<?php
/**
 * Sanitization Functions
 *
 * @package Chic Lifestyle
 * 
 */
    
function chic_lifestyle_sanitize_category( $input ){
  $output = intval( $input );
  return $output;

}

function chic_lifestyle_sanitize_dropdown_pages( $page_id, $setting ) {
  // Ensure $input is an absolute integer.
  $page_id = absint( $page_id );
  
  // If $page_id is an ID of a published page, return it; otherwise, return the default.
  return ( 'publish' == get_post_status( $page_id ) ? $page_id : $setting->default );
}

if ( ! function_exists( 'chic_lifestyle_sanitize_checkbox' ) ) {
    function chic_lifestyle_sanitize_checkbox( $input ) {       
        // Returns true if checkbox is checked.
        return ( isset( $input ) && true == $input ) ? true : false;
    }
}

//file input sanitization function
function chic_lifestyle_sanitize_file( $file, $setting ) {
 
    //allowed file types
    $mimes = array(
        'jpg|jpeg|jpe' => 'image/jpeg',
        'gif'          => 'image/gif',
        'png'          => 'image/png',
        'webp'         => 'image/webp',
    );
     
    //check file type from file name
    $file_ext = wp_check_filetype( $file, $mimes );
     
    //if file has a valid mime type return it, otherwise return default
    return ( $file_ext['ext'] ? $file : $setting->default );
}

function chic_lifestyle_sanitize_array( $value ){    
    if ( is_array( $value ) ) {
    foreach ( $value as $key => $subvalue ) {
      $value[ $key ] = esc_attr( $subvalue );
    }
    return $value;
  }
  return esc_attr( $value );    
}

/**
 * Returns a sanitize callback for slider controls with a textual max value.
 *
 * @param string $max_value   The canonical value to store when max is selected.
 * @param int    $min         Minimum allowed numeric value.
 * @param int    $max         Maximum allowed numeric value.
 * @param array  $alt_values  Alternative values to accept as max (e.g. 'full').
 * @return callable
 */
function chic_lifestyle_sanitize_choices_with_max_value( $max_value, $min, $max, $alt_values = array() ) {
	return function( $input, $setting ) use ( $max_value, $min, $max, $alt_values ) {
		if ( $input === $max_value || in_array( $input, $alt_values, true ) ) {
			return $max_value;
		}
		$val = absint( $input );
		return ( $val >= $min && $val <= $max ) ? $val : $setting->default;
	};
}

function chic_lifestyle_sanitize_choices( $input, $setting ) {
    global $wp_customize;
 
    $control = $wp_customize->get_control( $setting->id );
 
    if ( array_key_exists( $input, $control->choices ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function chic_lifestyle_sanitize_select( $input, $setting ) {
  
  // Ensure input is a slug.
  $input = sanitize_key( $input );
  
  // Get list of choices from the control associated with the setting.
  $choices = $setting->manager->get_control( $setting->id )->choices;
  
  // If the input is a valid key, return it; otherwise, return the default.
  return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}


function chic_lifestyle_sanitize_google_fonts( $input, $setting ) {

  // Get list of choices from the control associated with the setting.
  $choices = $setting->manager->get_control( $setting->id )->choices;
  
  // If the input is a valid key, return it; otherwise, return the default.
  return ( array_key_exists( $input, $choices ) ? $input : $setting->default );

}

function chic_lifestyle_sanitize_hex_color( $color ) {
  if ( '' === $color ) {
    return '';
  }

  // 3 or 6 hex digits, or the empty string.
  if ( preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
    return $color;
  }

  return NULL;
}