<?php
/**
 * Wellness Coach Lite Theme Customizer
 *
 * @package Wellness_Coach_Lite
 */

/**
 * Requiring customizer panels & sections
*/

$wellness_coach_lite_new_sections     = array( 'info', 'site', 'appearance', 'layout', 'home', 'general', 'elementor', 'footer' );

foreach( $wellness_coach_lite_new_sections as $n ){
    require get_template_directory() . '/inc/customizer/' . $n . '.php';
}

/**
 * Sanitization Functions
*/
require get_template_directory() . '/inc/customizer/sanitization-functions.php';

/**
 * Active Callbacks
*/
require get_template_directory() . '/inc/customizer/active-callback.php';

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function wellness_coach_lite_customize_preview_js() {
	wp_enqueue_script( 'wellness-coach-lite-customizer', get_template_directory_uri() . '/inc/js/customizer.js', array( 'customize-preview' ), WELLNESS_COACH_LITE_THEME_VERSION, true );
}
add_action( 'customize_preview_init', 'wellness_coach_lite_customize_preview_js' );

function wellness_coach_lite_customize_script(){
	$array = array(
        'flushFonts'        => wp_create_nonce( 'wellness-coach-lite-local-fonts-flush' ),
    );
    wp_enqueue_style( 'wellness-coach-lite-customize', get_template_directory_uri() . '/inc/css/customize.css', array(), WELLNESS_COACH_LITE_THEME_VERSION );
    wp_enqueue_script( 'wellness-coach-lite-customize', get_template_directory_uri() . '/inc/js/customize.js', array( 'jquery', 'customize-controls' ), WELLNESS_COACH_LITE_THEME_VERSION, true );
    wp_localize_script( 'wellness-coach-lite-customize', 'wellness_coach_lite_cdata', $array );

    wp_localize_script( 'wellness-coach-lite-repeater', 'wellness_coach_lite_customize',
		array(
			'nonce' => wp_create_nonce( 'wellness_coach_lite_customize_nonce' )
		)
	);
}
add_action( 'customize_controls_enqueue_scripts', 'wellness_coach_lite_customize_script' );

/*
 * Notifications in customizer
 */
require get_template_directory() . '/inc/customizer-plugin-recommend/customizer-notice/class-customizer-notice.php';

require get_template_directory() . '/inc/customizer-plugin-recommend/plugin-install/class-plugin-install-helper.php';

require get_template_directory() . '/inc/customizer-plugin-recommend/plugin-install/class-plugin-recommend.php';


/**
 * Initialize Wellness Coach Lite Customizer Notice
 *
 * @since 1.0.0
 */
if( ! function_exists( 'wellness_coach_lite_customizer_notice_init' ) ) {
	function wellness_coach_lite_customizer_notice_init() {
		$config_customizer = array(
			'recommended_plugins' => array(
				//change the slug for respective plugin recomendation
				'blossomthemes-toolkit' => array(
					'recommended' => true,
					'description' => sprintf(
						/* translators: %s: plugin name */
						esc_html__( 'If you want to take full advantage of the features this theme has to offer, please install and activate %s plugin.', 'wellness-coach-lite' ), '<strong>BlossomThemes Toolkit</strong>'
					),
				),
			),
			'recommended_plugins_title' => esc_html__( 'Recommended Plugin', 'wellness-coach-lite' ),
			'install_button_label'      => esc_html__( 'Install and Activate', 'wellness-coach-lite' ),
			'activate_button_label'     => esc_html__( 'Activate', 'wellness-coach-lite' ),
			'deactivate_button_label'   => esc_html__( 'Deactivate', 'wellness-coach-lite' ),
		);
		Wellness_Coach_Lite_Customizer_Notice::init( apply_filters( 'wellness_coach_lite_customizer_notice_array', $config_customizer ) );
    }
}
add_action( 'init', 'wellness_coach_lite_customizer_notice_init' );

/**
 * Reset font folder
 *
 * @access public
 * @return void
 */
function wellness_coach_lite_ajax_delete_fonts_folder() {
	// Check request.
	if ( ! check_ajax_referer( 'wellness-coach-lite-local-fonts-flush', 'nonce', false ) ) {
		wp_send_json_error( 'invalid_nonce' );
	}
	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_send_json_error( 'invalid_permissions' );
	}
	if ( class_exists( '\Wellness_Coach_Lite_WebFont_Loader' ) ) {
		$font_loader = new \Wellness_Coach_Lite_WebFont_Loader( '' );
		$removed = $font_loader->delete_fonts_folder();
		if ( ! $removed ) {
			wp_send_json_error( 'failed_to_flush' );
		}
		wp_send_json_success();
	}
	wp_send_json_error( 'no_font_loader' );
}
add_action( 'wp_ajax_wellness_coach_lite_flush_fonts_folder', 'wellness_coach_lite_ajax_delete_fonts_folder' );