<?php
/**
 * Summer Program functions and definitions
 *
 * @link https://developer.WordPress.org/themes/basics/theme-functions/
 *
 * @subpackage Summer Program
 * @since Summer Program 1.0
 */

if ( ! function_exists( 'summer_program_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which runs
 * before the init hook. The init hook is too late for some features, such as indicating
 * support post thumbnails.
 */
function summer_program_setup() {
	load_theme_textdomain( 'summer-program', get_template_directory() . '/languages' );
}
endif;
add_action( 'after_setup_theme', 'summer_program_setup' );

function summer_program_block_assets(){
	wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/assets/font-awesome/css/all.css', array(), '7.1.0' );
	wp_enqueue_style( 'animatecss', get_template_directory_uri() . '/assets/css/animate.css');
	wp_enqueue_style( 'owlcarousel-css', get_template_directory_uri() . '/assets/css/owl.carousel.css');
	wp_enqueue_style( 'summer-program-style', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) );
	wp_enqueue_script('wow-script', get_template_directory_uri() . '/assets/js/wow.js', array('jquery'));
	wp_enqueue_script('owlcarousel-js', get_template_directory_uri() . '/assets/js/owl.carousel.js', array('jquery'));
	wp_enqueue_script('summer-program-script', get_template_directory_uri() . '/assets/js/script.js', array('jquery'), '1.0.0', true);
	wp_style_add_data( 'summer-program-style', 'rtl', 'replace' );
}
add_action('enqueue_block_assets', 'summer_program_block_assets');

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

/**
 * Notice.
 */
require_once get_template_directory() . '/inc/notice/notice.php';

/**
 * Theme Info Page.
 */
require get_template_directory() . '/inc/addon.php';

/**
 * Customizer
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Demo import
 */
require get_template_directory() . '/inc/demo-content.php';

function summer_program_setup_theme() {
	if ( ! defined( 'SUMMER_PROGRAM_PREMIUM_PAGE' ) ) {
		define('SUMMER_PROGRAM_PREMIUM_PAGE',__('https://www.theclassictemplates.com/products/summer-camp-WordPress-theme','summer-program'));
	}
	if ( ! defined( 'SUMMER_PROGRAM_PRO_NAME' ) ) {
		define( 'SUMMER_PROGRAM_PRO_NAME', __( 'About Summer Program', 'summer-program' ));
	}
	if ( ! defined( 'SUMMER_PROGRAM_THEME_PAGE' ) ) {
		define('SUMMER_PROGRAM_THEME_PAGE',__('https://www.theclassictemplates.com/collections/best-WordPress-templates','summer-program'));
	}
	if ( ! defined( 'SUMMER_PROGRAM_SUPPORT' ) ) {
		define('SUMMER_PROGRAM_SUPPORT',__('https://WordPress.org/support/theme/summer-program/','summer-program'));
	}
	if ( ! defined( 'SUMMER_PROGRAM_REVIEW' ) ) {
		define('SUMMER_PROGRAM_REVIEW',__('https://WordPress.org/support/theme/summer-program/reviews/','summer-program'));
	}
	if ( ! defined( 'SUMMER_PROGRAM_PRO_DEMO' ) ) {
		define('SUMMER_PROGRAM_PRO_DEMO',__('https://live.theclassictemplates.com/summer-program/','summer-program'));
	}
	if ( ! defined( 'SUMMER_PROGRAM_THEME_DOCUMENTATION' ) ) {
		define('SUMMER_PROGRAM_THEME_DOCUMENTATION',__('https://live.theclassictemplates.com/demo/docs/summer-program-free/','summer-program'));
	}
	if ( ! defined( 'SUMMER_PROGRAM_BUNDLE_PAGE' ) ) {
		define('SUMMER_PROGRAM_BUNDLE_PAGE',__('https://www.theclassictemplates.com/products/WordPress-theme-bundle','summer-program'));
	}
}
add_action('after_setup_theme', 'summer_program_setup_theme');

function summer_program_enqueue_admin_script($hook) {
    // Enqueue admin JS for notices
    wp_enqueue_script('summer-program-welcome-notice', get_template_directory_uri() . '/inc/notice/notice.js', array('jquery'), '', true);
    
    // Localize script to pass data to JavaScript
wp_localize_script('summer-program-welcome-notice', 'summer_program_localize', array(
    'ajax_url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('summer_program_welcome_nonce'),
    'demo_nonce' => wp_create_nonce('summer_program_demo_nonce'), // added
    'dismiss_nonce' => wp_create_nonce('summer_program_welcome_nonce'), 
    'redirect_url' => admin_url('themes.php?page=summer-program')
));
}
add_action('admin_enqueue_scripts', 'summer_program_enqueue_admin_script');

add_action('wp_ajax_check_plugin_activation', function () {
    if (!isset($_POST['plugin']) || empty($_POST['plugin'])) {
        wp_send_json_error(['message' => 'Missing plugin identifier']);
    }

    include_once ABSPATH . 'wp-admin/includes/plugin.php';

    // Map plugin identifiers to their main files
    $summer_program_plugin_map = [
    	'woocommerce'                  => 'woocommerce/woocommerce.php',
        'yith-woocommerce-wishlist'    => 'yith-woocommerce-wishlist/plugin.php',
        'classic-blog-grid'            => 'classic-blog-grid/plugin.php'
    ];

    $summer_program_requested_plugin = sanitize_text_field($_POST['plugin']);

    if (!isset($summer_program_plugin_map[$summer_program_requested_plugin])) {
        wp_send_json_error(['message' => 'Invalid plugin']);
    }

    $summer_program_plugin_file = $summer_program_plugin_map[$summer_program_requested_plugin];
    $summer_program_is_active   = is_plugin_active($summer_program_plugin_file);

    wp_send_json_success(['active' => $summer_program_is_active]);
});
add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );

add_action( 'admin_init', function() {
  update_option( 'essential_blocks_quick_setup_shown', true );
  update_option( 'essential_blocks_user_type', 'old' );
  remove_submenu_page( 'admin.php', 'eb-quick-setup' );
});