<?php
/*I have added it through action so that it is flexible to the developer to adapt change*/
add_action('salientthemes_customizer_add_setting_control','salientthemes_customizer_add_setting_control_callback', 12, 5);

if ( ! function_exists( 'salientthemes_customizer_add_setting_control_callback' ) ) :
    /**
     * Function to add customizer setting and controls
     *
     * @access public
     * @since 1.0.0
     *
     * @param object $salientthemes_customizer_wp_customize
     * @param string $salientthemes_customizer_customizer_name common name for all setting and controls
     * @param array $salientthemes_customizer_basic_control_types
     * @param string $salientthemes_customizer_setting_control_key
     * @param array $salientthemes_customizer_settings_control
     * @return void
     *
     */
    function salientthemes_customizer_add_setting_control_callback( $salientthemes_customizer_wp_customize, $salientthemes_customizer_customizer_name, $salientthemes_customizer_basic_control_types, $salientthemes_customizer_setting_control_key, $salientthemes_customizer_settings_control ){
        $salientthemes_customizer_wp_customize->add_setting( esc_attr( $salientthemes_customizer_customizer_name.'['.$salientthemes_customizer_setting_control_key.']' ), $salientthemes_customizer_settings_control['setting']);

        $salientthemes_customizer_control_field_type = $salientthemes_customizer_settings_control['control']['type'];

        /*check if basic control types*/
        if ( in_array( $salientthemes_customizer_control_field_type, $salientthemes_customizer_basic_control_types ) ) {
            $salientthemes_customizer_wp_customize->add_control( esc_attr( $salientthemes_customizer_customizer_name.'['.$salientthemes_customizer_setting_control_key.']' ), $salientthemes_customizer_settings_control['control']);
        }
        else {
            /*Check for default WP_Customize_Custom_Control defined*/
            $salientthemes_customizer_Explode_Customize_Custom_Control_class_name = explode('_', strtolower( $salientthemes_customizer_control_field_type ));
            $salientthemes_customizer_Ucfirst_Customize_Custom_Control_class_name_array = array_map('ucfirst', $salientthemes_customizer_Explode_Customize_Custom_Control_class_name);
            $salientthemes_customizer_Implode_Customize_Custom_Control_class_name = implode('_', $salientthemes_customizer_Ucfirst_Customize_Custom_Control_class_name_array);

            $salientthemes_customizer_New_Customize_Custom_Control_class_name = 'WP_Customize_'.$salientthemes_customizer_Implode_Customize_Custom_Control_class_name.'_Control';
            $salientthemes_customizer_customizer_class_exist = false;
            if ( class_exists( $salientthemes_customizer_New_Customize_Custom_Control_class_name ) ) {
                $salientthemes_customizer_customizer_class_exist = true;
            }
            else{
                $salientthemes_customizer_New_Customize_Custom_Control_class_name = 'Salient_Customizer_'.$salientthemes_customizer_Implode_Customize_Custom_Control_class_name.'_Control';
                if ( class_exists( $salientthemes_customizer_New_Customize_Custom_Control_class_name ) ) {
                    $salientthemes_customizer_customizer_class_exist = true;
                }
            }
            if($salientthemes_customizer_customizer_class_exist){
                $salientthemes_customizer_wp_customize->add_control(
                    new $salientthemes_customizer_New_Customize_Custom_Control_class_name(
                        $salientthemes_customizer_wp_customize,
                        esc_attr( $salientthemes_customizer_customizer_name.'['.$salientthemes_customizer_setting_control_key.']'),
                        $salientthemes_customizer_settings_control['control']
                    )
                );
            }
            else {
                ?>

                  <script>
                    console.log('<?php echo esc_attr( $salientthemes_customizer_New_Customize_Custom_Control_class_name ). "not found. Please create it."?>');
                </script>

            <?php
            }

        }
    }
endif;