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


if( !function_exists( 'newsnation_sanitize_sidebar_option' ) ) :

    // Sidebar Option Sanitize.
    function newsnation_sanitize_sidebar_option( $newsnation_input ){

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

            return $newsnation_input;

        }

        return;

    }

endif;

if( !function_exists( 'newsnation_sanitize_single_pagination_layout' ) ) :

    // Sidebar Option Sanitize.
    function newsnation_sanitize_single_pagination_layout( $newsnation_input ){

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

            return $newsnation_input;

        }

        return;

    }

endif;

if( !function_exists( 'newsnation_sanitize_archive_layout' ) ) :

    // Sidebar Option Sanitize.
    function newsnation_sanitize_archive_layout( $newsnation_input ){

        $newsnation_archive_option = array( 'default','full','masonry' );
        if( in_array( $newsnation_input,$newsnation_archive_option ) ){

            return $newsnation_input;

        }

        return;

    }

endif;

if( !function_exists( 'newsnation_sanitize_single_post_layout' ) ) :

    // Single Layout Option Sanitize.
    function newsnation_sanitize_single_post_layout( $newsnation_input ){

        $newsnation_single_layout = array( 'layout-1','layout-2' );
        if( in_array( $newsnation_input,$newsnation_single_layout ) ){

            return $newsnation_input;

        }

        return;

    }

endif;

if ( ! function_exists( 'newsnation_sanitize_checkbox' ) ) :

	/**
	 * Sanitize checkbox.
	 */
	function newsnation_sanitize_checkbox( $newsnation_checked ) {

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

	}

endif;


if ( ! function_exists( 'newsnation_sanitize_select' ) ) :

    /**
     * Sanitize select.
     */
    function newsnation_sanitize_select( $newsnation_input, $newsnation_setting ) {

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

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

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

    }

endif;

if ( ! function_exists( 'newsnation_sanitize_repeater' ) ) :
    
    /**
    * Sanitise Repeater Field
    */
    function newsnation_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] = newsnation_sanitize_repeater_ed( $value );

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

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

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

                        $input_decoded[$boxes][$key] = newsnation_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] =  newsnation_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 newsnation_sanitize_repeater_ed( $input ) {

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

}

function newsnation_sanitize_home_sections( $input ) {

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

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

}

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

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

}

function newsnation_sanitize_ribbon_bg( $input ) {

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

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

}