<?php
/**
* Custom Functions.
*
* @package Vintuna
*/


if( !function_exists( 'vintuna_sanitize_sidebar_option' ) ) :

    // Sidebar Option Sanitize.
    function vintuna_sanitize_sidebar_option( $vintuna_input ){

        $vintuna_metabox_options = array( 'global-sidebar','left-sidebar','right-sidebar','no-sidebar' );
        if( in_array( $vintuna_input,$vintuna_metabox_options ) ){

            return $vintuna_input;

        }

        return;

    }

endif;

if( !function_exists( 'vintuna_sanitize_single_pagination_layout' ) ) :

    // Sidebar Option Sanitize.
    function vintuna_sanitize_single_pagination_layout( $vintuna_input ){

        $vintuna_single_pagination = array( 'no-navigation','norma-navigation','ajax-next-post-load' );
        if( in_array( $vintuna_input,$vintuna_single_pagination ) ){

            return $vintuna_input;

        }

        return;

    }

endif;

if( !function_exists( 'vintuna_sanitize_archive_layout' ) ) :

    // Sidebar Option Sanitize.
    function vintuna_sanitize_archive_layout( $vintuna_input ){

        $vintuna_archive_option = array( 'default','full','masonry' );
        if( in_array( $vintuna_input,$vintuna_archive_option ) ){

            return $vintuna_input;

        }

        return;

    }

endif;

if( !function_exists( 'vintuna_sanitize_single_post_layout' ) ) :

    // Single Layout Option Sanitize.
    function vintuna_sanitize_single_post_layout( $vintuna_input ){

        $vintuna_single_layout = array( 'layout-1','layout-2' );
        if( in_array( $vintuna_input,$vintuna_single_layout ) ){

            return $vintuna_input;

        }

        return;

    }

endif;

if ( ! function_exists( 'vintuna_sanitize_checkbox' ) ) :

	/**
	 * Sanitize checkbox.
	 */
	function vintuna_sanitize_checkbox( $vintuna_checked ) {

		return ( ( isset( $vintuna_checked ) && true === $vintuna_checked ) ? true : false );

	}

endif;


if ( ! function_exists( 'vintuna_sanitize_select' ) ) :

    /**
     * Sanitize select.
     */
    function vintuna_sanitize_select( $vintuna_input, $vintuna_setting ) {

        // Ensure input is a slug.
        $vintuna_input = sanitize_text_field( $vintuna_input );

        // Get list of choices from the control associated with the setting.
        $choices = $vintuna_setting->manager->get_control( $vintuna_setting->id )->choices;

        // If the input is a valid key, return it; otherwise, return the default.
        return ( array_key_exists( $vintuna_input, $choices ) ? $vintuna_input : $vintuna_setting->default );

    }

endif;

if ( ! function_exists( 'vintuna_sanitize_repeater' ) ) :
    
    /**
    * Sanitise Repeater Field
    */
    function vintuna_sanitize_repeater($input){
        $input_decoded = json_decode( $input, true );
        
        if(!empty($input_decoded)) {

            foreach ($input_decoded as $boxes => $box ){

                foreach ($box as $key => $value){

                    if( $key == 'section_ed' 
                        || $key == 'ed_tab' 
                        || $key == 'ed_arrows_carousel' 
                        || $key == 'ed_dots_carousel' 
                        || $key == 'ed_autoplay_carousel' 
                        || $key == 'ed_ribbon_bg'
                    ){

                        $input_decoded[$boxes][$key] = vintuna_sanitize_repeater_ed( $value );

                    }elseif( $key == 'home_section_type' ){

                        $input_decoded[$boxes][$key] = vintuna_sanitize_home_sections( $value );

                    }elseif( $key == 'ribbon_bg_color_schema' ){

                        $input_decoded[$boxes][$key] = vintuna_sanitize_ribbon_bg( $value );

                    }elseif( $key == 'category_color' ){

                        $input_decoded[$boxes][$key] = sanitize_hex_color( $value );

                    }elseif( $key == 'tiles_post_per_page' ){

                        $input_decoded[$boxes][$key] =  absint( $value );

                    }elseif( $key == 'advertise_image' || $key == 'advertise_link' ){

                         $input_decoded[$boxes][$key] = esc_url_raw( $value );

                    }elseif($key == 'section_category' || 
                            $key == 'section_post_tab_list_cat' ||
                            $key == 'section_post_cat_1' || 
                            $key == 'section_category_1' || 
                            $key == 'section_category_2' || 
                            $key == 'section_category_3' || 
                            $key == 'section_category_4' || 
                            $key == 'category'
                        ){

                        $input_decoded[$boxes][$key] =  vintuna_sanitize_category( $value );

                    }else{

                        $input_decoded[$boxes][$key] = sanitize_text_field( $value );

                    }
                    
                }

            }
           
            return json_encode($input_decoded);

        }

        return $input;
    }
endif;

/** Sanitize Enable Disable Checkbox **/
function vintuna_sanitize_repeater_ed( $input ) {

    $valid_keys = array('yes','no');
    if ( in_array( $input , $valid_keys ) ) {
        return $input;
    }
    return '';

}

function vintuna_sanitize_home_sections( $input ) {

    $home_sections = array(
        'default-banner-area' => esc_html__('Default Banner','vintuna'),
        'tiles-blocks' => esc_html__('Slider & Tiles Block','vintuna'),
        'video-slider' => esc_html__('Video Slider','vintuna'),
        'must-read' => esc_html__('Must Read','vintuna'),
        'banner-blocks-1' => esc_html__('Featured & Tab Block','vintuna'),
        'latest-posts-blocks' => esc_html__('Latest Posts Block','vintuna'),
        'slider-blocks' => esc_html__('Slider Block','vintuna'),
        'advertise-blocks' => esc_html__('Advertise Block','vintuna'),
        'home-widget-area' => esc_html__('Widgets Area Block','vintuna'),
        'you-may-like-blocks' => esc_html__('You May Like Block','vintuna'),

    );
    if ( array_key_exists( $input , $home_sections ) ) {
        return $input;
    }
    return '';

}

/** Sanitize Category **/
function vintuna_sanitize_category( $input ) {

   $vintuna_post_category_list = vintuna_post_category_list();
    if ( array_key_exists( $input , $vintuna_post_category_list ) ) {
        return $input;
    }
    return '';

}

function vintuna_sanitize_ribbon_bg( $input ) {

    $ribbon_bg = array( 
                    '1' =>  array(
                                    'title' =>  esc_html__( 'Blue', 'vintuna' ),
                                    'color' =>  '#3061ff',
                                ),
                    '2' =>  array(
                                    'title' =>  esc_html__( 'Orange', 'vintuna' ),
                                    'color' =>  '#fa9000',
                                ),
                    '3' =>  array(
                                    'title' =>  esc_html__( 'Royal Blue', 'vintuna' ),
                                    'color' =>  '#00167a',
                                ),
                    '4' =>  array(
                                    'title' =>  esc_html__( 'Pink', 'vintuna' ),
                                    'color' =>  '#ff2d55',
                                ),
                );

    if ( array_key_exists( $input , $ribbon_bg ) ) {
        return $input;
    }
    return '';

}