<?php
/**
 * Customizer: Sanitization Callbacks
 *
 * This file demonstrates how to define sanitization callback functions for various data types.
 * 
 * @package   Automotive Services
 * @copyright Copyright (c) 2015, WordPress Theme Review Team
 * @license   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer)
 */

function automotive_services_sanitize_checkbox( $automotive_services_checked ) {
	return ( ( isset( $automotive_services_checked ) && true == $automotive_services_checked ) ? true : false );
}

/* Sanitization Text*/
function automotive_services_sanitize_text( $automotive_services_text ) {
	return wp_filter_post_kses( $automotive_services_text );
}

function automotive_services_sanitize_choices( $automotive_services_input, $automotive_services_setting ) {
    global $wp_customize; 
    $control = $wp_customize->get_control( $automotive_services_setting->id ); 
    if ( array_key_exists( $automotive_services_input, $control->choices ) ) {
        return $automotive_services_input;
    } else {
        return $automotive_services_setting->default;
    }
}

// Sanitization callback function for numeric input
function automotive_services_sanitize_numeric_input($automotive_services_input) {
    // Remove any non-numeric characters
    return preg_replace('/[^0-9]/', '', $automotive_services_input);
}

// Sanitization callback function for logo width
function automotive_services_sanitize_logo_width($automotive_services_input) {
    $automotive_services_input = absint($automotive_services_input); // Convert to integer
    // Ensure the value is between 1 and 150
    return ($automotive_services_input >= 1 && $automotive_services_input <= 300) ? $automotive_services_input : 150; // Default to 270 if out of range
}

function automotive_services_sanitize_copyright_position( $automotive_services_input ) {
    $valid = array( 'right', 'left', 'center' );

    if ( in_array( $automotive_services_input, $valid, true ) ) {
        return $automotive_services_input;
    } else {
        return 'right';
    }
}

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