<?php
/**
 * Secretary Services functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Secretary Services
 */

if ( ! defined( 'SECRETARY_SERVICES_VERSION' ) ) {
	// Replace the version number of the theme on each release.
	define( 'SECRETARY_SERVICES_VERSION', wp_get_theme()->get( 'Version' ) );
}

if ( ! function_exists( 'secretary_services_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 for post thumbnails.
	 */
	function secretary_services_setup() {

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		add_theme_support( 'align-wide' );

		add_theme_support( 'woocommerce' );

		// Add support for block styles.
		add_theme_support( 'wp-block-styles' );

		// Enqueue editor styles.
		add_editor_style( 'style.css' );

		// Add support for core custom logo.
		add_theme_support(
			'custom-logo',
			array(
				'height'      => 192,
				'width'       => 192,
				'flex-width'  => true,
				'flex-height' => true,
			)
		);

		// Enqueue editor styles.
		// add_editor_style( 'style.css' );

		// Experimental support for adding blocks inside nav menus
		add_theme_support( 'block-nav-menus' );

		// Add support for experimental link color control.
		add_theme_support( 'experimental-link-color' );
	}
endif;
add_action( 'after_setup_theme', 'secretary_services_setup' );

/**
 * Enqueue scripts and styles.
 */
function secretary_services_scripts() {
	wp_enqueue_style('secretary-services-style', get_stylesheet_uri(), array() );
	wp_enqueue_script( 'jquery-wow', esc_url(get_template_directory_uri()) . '/js/wow.js', array('jquery') );
	wp_enqueue_style( 'animate-css', esc_url(get_template_directory_uri()).'/css/animate.css' );
	wp_enqueue_script( 'secretary-services-custom-scripts', get_template_directory_uri() . '/js/custom.js', array('jquery'),'' ,true );
	wp_style_add_data( 'secretary-services-style', 'rtl', 'replace' );
}
add_action( 'wp_enqueue_scripts', 'secretary_services_scripts' );

/**
 * Enqueue block editor style
 */
function secretary_services_block_editor_styles() {
	wp_enqueue_style( 'secretary-services-block-patterns-style-editor', get_theme_file_uri( '/css/block-editor.css' ), false, '1.0', 'all' );
}
add_action( 'enqueue_block_editor_assets', 'secretary_services_block_editor_styles' );

function secretary_services_init_setup() {
	define('SECRETARY_SERVICES_BUY_NOW',__('https://www.vwthemes.com/products/secretary-wordpress-theme','secretary-services'));
	define('SECRETARY_SERVICES_SUPPORT',__('https://WordPress.org/support/theme/secretary-services/','secretary-services'));
	define('SECRETARY_SERVICES_REVIEW',__('https://WordPress.org/support/theme/secretary-services/reviews/','secretary-services'));
	define('SECRETARY_SERVICES_LIVE_DEMO',__('https://www.vwthemes.net/secretary-services-pro/','secretary-services'));
	define('SECRETARY_SERVICES_PRO_DOC',__('https://preview.vwthemesdemo.com/docs/secretary-services-pro/','secretary-services'));
	define('SECRETARY_SERVICES_FREE_DOC',__('https://preview.vwthemesdemo.com/docs/free-secretary-services/','secretary-services'));
	define('SECRETARY_SERVICES_FAQ',__('https://www.vwthemes.com/faqs/','secretary-services'));
	define('SECRETARY_SERVICES_CHILD_THEME',__('https://developer.wordpress.org/themes/advanced-topics/child-themes/','secretary-services'));
	define('SECRETARY_SERVICES_CONTACT',__('https://www.vwthemes.com/contact/','secretary-services'));
	define('SECRETARY_SERVICES_THEME_BUNDLE_BUY_NOW',__('https://www.vwthemes.com/products/wp-theme-bundle','secretary-services'));
	define('SECRETARY_SERVICES_THEME_BUNDLE_DOC',__('https://preview.vwthemesdemo.com/docs/theme-bundle/','secretary-services'));

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

	/**
	 * TGM
	 */
	require_once get_template_directory() . '/inc/tgm/plugin-activation.php';

	/**
	 * notice
	 */
	require get_template_directory() . '/inc/core/activation-notice.php';

	/**
	 * Section Pro
	 */
	require get_template_directory() . '/inc/section-pro/customizer.php';

	/**
	 * Load core file.
	 */
	require_once get_template_directory() . '/inc/core/theme-info.php';
	require_once get_template_directory() . '/inc/core/template-functions.php';
}
add_action( 'after_setup_theme', 'secretary_services_init_setup' );	

/* Enqueue admin-notice-script js */
add_action('admin_enqueue_scripts', function ($hook) {
    //if ($hook !== 'appearance_page_secretary-services') return;

    wp_enqueue_script('admin-notice-script', get_template_directory_uri() . '/inc/core/js/admin-notice-script.js', ['jquery'], null, true);
    wp_localize_script('admin-notice-script', 'pluginInstallerData', [
        'ajaxurl'     => admin_url('admin-ajax.php'),
        'nonce'       => wp_create_nonce('install_plugin_nonce'), // Match this with PHP nonce check
        'redirectUrl' => admin_url('themes.php?page=secretary-services-info'),
    ]);
});

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
    $secretary_services_plugin_map = [
        'video_popup'          => 'wpzoom-video-popup-block/wpzoom-video-popup-block.php',
        'ibtana'               => 'ibtana-visual-editor/plugin.php',
    ];

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

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

    $secretary_services_plugin_file = $secretary_services_plugin_map[$secretary_services_requested_plugin];
    $secretary_services_is_active   = is_plugin_active($secretary_services_plugin_file);

    wp_send_json_success(['active' => $secretary_services_is_active]);
});

function secretary_services_dismissed_notice() {
		update_option( 'secretary_services_admin_notice', true );
    wp_die();
}
add_action( 'wp_ajax_secretary_services_dismissed_notice', 'secretary_services_dismissed_notice' );