<?php
/**
* Sanitize callback functions
*
* @package GridRead WordPress Theme
* @copyright Copyright (C) 2025 ThemesDNA
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
* @author ThemesDNA <themesdna@gmail.com>
*/

function gridread_sanitize_checkbox( $input ) {
    return ( ( isset( $input ) && ( true == $input ) ) ? true : false );
}

function gridread_sanitize_html( $input ) {
    return wp_kses_post( force_balance_tags( $input ) );
}

function gridread_sanitize_post_summaries_style( $input, $setting ) {
    $valid = array('grid','non-grid');
    if ( in_array( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function gridread_sanitize_posts_navigation_type( $input, $setting ) {
    $valid = array('normalnavi','numberednavi');
    if ( in_array( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function gridread_sanitize_email( $input, $setting ) {
    if ( '' != $input && is_email( $input ) ) {
        $input = sanitize_email( $input );
        return $input;
    } else {
        return $setting->default;
    }
}

function gridread_sanitize_logo_location( $input, $setting ) {
    $valid = array('beside-title','above-title');
    if ( in_array( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function gridread_sanitize_slider_display( $input, $setting ) {
    $valid = array(
            'front-page' => esc_html__( 'Home Page', 'gridread' ),
            'front-page-static' => esc_html__( 'Static Home Page', 'gridread' ),
            'blog-posts-index' => esc_html__( 'Blog Posts Index Page', 'gridread' ),
            'everywhere' => esc_html__( 'Everywhere', 'gridread' )
    );

    if ( array_key_exists( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function gridread_sanitize_positive_integer( $input, $setting ) {
    $input = absint( $input ); // Force the value into non-negative integer.
    return ( 0 < $input ) ? $input : $setting->default;
}

function gridread_sanitize_positive_float( $input, $setting ) {
    $input = (float) $input;
    return ( 0 < $input ) ? $input : $setting->default;
}

function gridread_sanitize_slider_post_type( $input, $setting ) {
    $valid = array('recentposts','catposts','tagposts');
    if ( in_array( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function gridread_sanitize_include_posts( $input, $setting ) {
    $include_posts_regex = '/^\d+(?:,\d+)*$/';
    $input = ( preg_match($include_posts_regex, $input) ) ? sanitize_text_field( $input ) : $setting->default;
    return $input;
}

function gridread_sanitize_exclude_posts( $input, $setting ) {
    $exclude_posts_regex = '/^\d+(?:,\d+)*$/';
    $input = ( preg_match($exclude_posts_regex, $input) ) ? sanitize_text_field( $input ) : $setting->default;
    return $input;
}

function gridread_sanitize_slider_post_orderby( $input, $setting ) {
    $valid = array('date','modified','comment_count');
    if ( in_array( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}

function gridread_sanitize_slider_post_order( $input, $setting ) {
    $valid = array('ASC','DESC');
    if ( in_array( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}