<?php
/**
 * Customizer: Sanitization Callbacks
 *
 * This file demonstrates how to define sanitization callback functions for various data types.
 * 
 * @package   Supermarket Shopping
 * @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 supermarket_shopping_sanitize_checkbox( $supermarket_shopping_checked ) {
	return ( ( isset( $supermarket_shopping_checked ) && true == $supermarket_shopping_checked ) ? true : false );
}

/* Sanitization Text*/
function supermarket_shopping_sanitize_text( $supermarket_shopping_text ) {
	return wp_filter_post_kses( $supermarket_shopping_text );
}

function supermarket_shopping_sanitize_choices( $supermarket_shopping_input, $supermarket_shopping_setting ) {
    global $wp_customize; 
    $supermarket_shopping_control = $wp_customize->get_control( $supermarket_shopping_setting->id ); 
    if ( array_key_exists( $supermarket_shopping_input, $supermarket_shopping_control->choices ) ) {
        return $supermarket_shopping_input;
    } else {
        return $supermarket_shopping_setting->default;
    }
}

function supermarket_shopping_sanitize_select( $supermarket_shopping_input, $supermarket_shopping_setting ) {
  $supermarket_shopping_input = sanitize_key( $supermarket_shopping_input );
  $supermarket_shopping_choices = $supermarket_shopping_setting->manager->get_control( $supermarket_shopping_setting->id )->choices;
  return ( array_key_exists( $supermarket_shopping_input, $supermarket_shopping_choices ) ? $supermarket_shopping_input : $supermarket_shopping_setting->default );
}

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

function supermarket_shopping_sanitize_phone_number( $supermarket_shopping_phone ) {
    return preg_replace( '/[^\d+]/', '', $supermarket_shopping_phone );
}

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

function supermarket_shopping_sanitize_copyright_position( $supermarket_shopping_input ) {
  $supermarket_shopping_valid = array( 'right', 'left', 'center' );

  if ( in_array( $supermarket_shopping_input, $supermarket_shopping_valid, true ) ) {
      return $supermarket_shopping_input;
  } else {
      return 'right';
  }
}