<?php


require_once trailingslashit( STORE_EXPERT_THEME_DIR ). 'inc/customize/controls/custom-controls.php';


/**
* Dropdown Menu List for customizer
*/
if ( !function_exists( 'store_expert_customizer_menulist' ) ) {
    // Function
    function store_expert_customizer_menulist() {
        $menus = wp_get_nav_menus();

        $menu_list['none'] = esc_html__(' -- Choose Menu -- ', 'store-expert');
        foreach ($menus as $menu) {
            $menu_list[$menu->slug] = $menu->name;
        }

        return $menu_list;
    }

}

if ( !function_exists( 'store_expert_customizer_category_list' ) ) {
    function store_expert_customizer_category_list(){
        $cats = get_categories();

        $categories['none'] = esc_html__(' -- Choose Category -- ', 'store-expert');
        foreach($cats as $cat){
            $categories[$cat->term_id] = $cat->name;
        }
        return $categories;
    }
}

if ( !function_exists( 'store_expert_customizer_posts_list' ) ) {
    function store_expert_customizer_posts_list(){
        global $post;
        $args = array( 'numberposts' => -1);
        $list = get_posts($args);

        $posts_list['none'] = esc_html__(' -- Choose Post -- ', 'store-expert');
        foreach($list as $lists){
            $posts_list[$lists->ID] = $lists->post_title;
        }
        return $posts_list;
    }
}

if ( !function_exists( 'store_expert_customizer_woo_category_list' ) ) {
    function store_expert_customizer_woo_category_list(){
        if ( !class_exists( 'WooCommerce' ) ) :
            $woo_categories['none'] = esc_html__( 'Please activate WooCommerce plugin', 'store-expert');
        endif;
            $woo_args = array( 'type' => 'product', 'taxonomy' => 'product_cat' );
            $woo_cats = get_categories( $woo_args );
            foreach($woo_cats as $woo_cat){
                $woo_categories[$woo_cat->slug] = $woo_cat->name;
            }
            return $woo_categories;
    }
}

/**
* Register customizer
*/
if ( !function_exists( 'store_expert_customize_register' ) ) {
    // Function starts
    function store_expert_customize_register( $wp_customize ) {

        /**
        * Register customizer sections
        */
        require get_template_directory() . '/inc/customize/options/general-settings.php';
        require get_template_directory() . '/inc/customize/options/header-settings.php';
        require get_template_directory() . '/inc/customize/options/home-settings.php';


        /**
        * Sanitize for Choices
        */
        function store_expert_sanitize_choices($input, $setting) {
            global $wp_customize;
            $control = $wp_customize->get_control($setting->id);

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

        /** Sanitize Text Field */
        function store_expert_sanitize_text($input) {
            return wp_kses_post(force_balance_tags($input));
        }



        // Sanitize for checkbox
        function store_expert_sanitize_checkbox( $input ) {
        // Boolean check 
        return ( ( isset( $input ) && true == $input ) ? true : false );
        }

        //Sanitize for Radio
        function store_expert_sanitize_radio( $input, $setting ){          
            //input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
            $input = sanitize_key($input);
            //get the list of possible radio box options 
            $choices = $setting->manager->get_control( $setting->id )->choices;
            //return input if valid or return default option
            return ( array_key_exists( $input, $choices ) ? $input : $setting->default );                
              
        }
        
    }
    add_action( 'customize_register', 'store_expert_customize_register' );
}