<?php
/**
 * EVSE Charging Dock functions and definitions
 *
 * @package EVSE Charging Dock
 * @since 1.0
 */

if ( ! function_exists( 'evse_charging_dock_support' ) ) :
	function evse_charging_dock_support() {
		
		load_theme_textdomain( 'evse-charging-dock', get_template_directory() . '/languages' );

		add_theme_support( 'html5', array(
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		) );

		add_theme_support( 'custom-background', apply_filters( 'evse_charging_dock_custom_background', array(
            'default-color' => 'ffffff',
            'default-image' => '',
        )));
		
		add_theme_support( 'wp-block-styles' );

		add_editor_style( 'style.css' );

		define('EVSE_CHARGING_DOCK_BUY_NOW',__('https://www.themescarts.com/products/charging-station-wordpress-theme/','evse-charging-dock'));
		define('EVSE_CHARGING_DOCK_FOOTER_BUY_NOW',__('https://www.themescarts.com/products/evse-charging-dock/','evse-charging-dock'));
		define('EVSE_CHARGING_DOCK_BUNDLE_LINK',__('https://www.themescarts.com/products/wordpress-theme-bundle/','evse-charging-dock'));

	}
endif;
add_action( 'after_setup_theme', 'evse_charging_dock_support' );

/*-------------------------------------------------------------
 Enqueue Styles
--------------------------------------------------------------*/

if ( ! function_exists( 'evse_charging_dock_styles' ) ) :
	function evse_charging_dock_styles() {
		// Register theme stylesheet.
		wp_enqueue_style('evse-charging-dock-style', get_stylesheet_uri(), array(), wp_get_theme()->get('version') );
		wp_enqueue_style('owl-carousel-css', get_template_directory_uri(). '/assets/css/owl.carousel.css', array(), '2.3.4' );
		wp_enqueue_style('evse-charging-dock-style-blocks', get_template_directory_uri(). '/assets/css/blocks.css');
		wp_enqueue_style('evse-charging-dock-style-responsive', get_template_directory_uri(). '/assets/css/responsive.css');
		wp_style_add_data( 'evse-charging-dock-style', 'rtl', 'replace' );
		wp_enqueue_script( 'owl-carousel-js', get_theme_file_uri( '/assets/js/owl.carousel.js' ), array( 'jquery' ), '2.3.4', true );
		wp_enqueue_script( 'wow-js', get_theme_file_uri( '/assets/js/wow.js' ), array( 'jquery' ), wp_get_theme()->get('Version'), true );
		wp_enqueue_style( 'animate-css', get_template_directory_uri().'/assets/css/animate.css', array(), wp_get_theme()->get('Version') );
		wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/assets/font-awesome/css/all.css', array(), '7.0.0' );

		wp_enqueue_style( 'dashicons' );
	}
endif;
add_action( 'wp_enqueue_scripts', 'evse_charging_dock_styles' );

function evse_charging_dock_enqueue_admin_script($hook) {
    // Enqueue admin JS for notices
    wp_enqueue_script('evse-charging-dock-welcome-notice', get_template_directory_uri() . '/inc/evse-charging-dock-theme-info-page/js/evse-charging-dock-welcome-notice.js', array('jquery'), '', true);
    
    // Localize script to pass data to JavaScript
    wp_localize_script('evse-charging-dock-welcome-notice', 'evse_charging_dock_localize', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('evse_charging_dock_welcome_nonce'),
        'dismiss_nonce' => wp_create_nonce('evse_charging_dock_welcome_nonce'), // Nonce for dismissal
        'redirect_url' => admin_url('themes.php?page=evse-charging-dock-theme-info-page')
    ));
}
add_action('admin_enqueue_scripts', 'evse_charging_dock_enqueue_admin_script');

if (!function_exists('evse_charging_dock_enable_plugin_autoupdate')) {

    add_filter('auto_update_plugin', function ($update, $item) {
        if ($item->slug === 'siteready-coming-soon-under-construction') {
            return true;
        }
        return $update;
    }, 10, 2);

}

function evse_charging_dock_plugin_update_available($slug, $file) {
    $updates = get_site_transient('update_plugins');

    if (!isset($updates->response[$slug . '/' . $file])) {
        return false; // No update available
    }

    return $updates->response[$slug . '/' . $file];
}

add_action('activated_plugin', function ($plugin) {

    if ($plugin === 'essential-blocks/essential-blocks.php') {

        // Prevent their redirect flag if they use one
        delete_transient('eb_admin_redirect');
        delete_option('eb_setup_redirect');

        // Force your redirect immediately
        wp_safe_redirect(admin_url('admin.php?page=evse-charging-dock-theme-info-page'));
        exit;
    }

});

require get_template_directory() .'/inc/TGM/tgm.php';

// Add block patterns
require get_template_directory() . '/inc/block-patterns.php';

require_once get_theme_file_path( 'inc/evse-charging-dock-theme-info-page/templates/class-theme-notice.php' );

require_once get_theme_file_path( 'inc/evse-charging-dock-theme-info-page/class-theme-info.php' );

require_once get_theme_file_path( '/inc/customizer.php' );	
?>