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

if ( ! function_exists( 'vw_cloudport_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 vw_cloudport_setup() {

		load_theme_textdomain( 'vw-cloudport', get_template_directory() . '/languages' );

		// 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', 'vw_cloudport_setup' );

/**
 * Enqueue scripts and styles.
 */
function vw_cloudport_scripts() {
	wp_enqueue_style('vw-cloudport-style', get_stylesheet_uri(), array() );
	wp_enqueue_script( 'jquery-wow', get_template_directory_uri(). '/js/wow.js', array('jquery'),wp_get_theme()->get( 'Version' ) ,true );
	wp_enqueue_style( 'animate-css', get_template_directory_uri().'/css/animate.css', array(), wp_get_theme()->get('Version') );
	wp_enqueue_style( 'owl.carousel-style', get_template_directory_uri().'/css/owl.carousel.css', array(), wp_get_theme()->get('Version') );
	wp_enqueue_script( 'owl.carousel-js', get_template_directory_uri(). '/js/owl.carousel.js', array('jquery'),wp_get_theme()->get( 'Version' ) ,true );
	wp_enqueue_script( 'vw-cloudport-custom-scripts', get_template_directory_uri() . '/js/custom.js', array('jquery'),wp_get_theme()->get( 'Version' ) ,true );
	wp_style_add_data( 'vw-cloudport-style', 'rtl', 'replace' );
}
add_action( 'wp_enqueue_scripts', 'vw_cloudport_scripts' );

/**
 * Enqueue block editor style
 */
function vw_cloudport_block_editor_styles() {
	wp_enqueue_style( 'vw-cloudport-block-patterns-style-editor', get_theme_file_uri( '/css/block-editor.css' ), array(), wp_get_theme()->get('Version') );	
}
add_action( 'enqueue_block_editor_assets', 'vw_cloudport_block_editor_styles' );

function vw_cloudport_init_setup() {

	define('VW_CLOUDPORT_BUY_NOW',__('https://www.vwthemes.com/products/cloudport-computing-wordpress-theme','vw-cloudport'));
	define('VW_CLOUDPORT_SUPPORT',__('https://wordpress.org/support/theme/vw-cloudport/','vw-cloudport'));
	define('VW_CLOUDPORT_REVIEW',__('https://wordpress.org/support/theme/vw-cloudport/reviews/','vw-cloudport'));
	define('VW_CLOUDPORT_LIVE_DEMO',__('https://www.vwthemes.net/cloudport/','vw-cloudport'));
	define('VW_CLOUDPORT_PRO_DOC',__('https://vwthemesdemo.com/documentation/cloudport-pro/','vw-cloudport'));
	define('VW_CLOUDPORT_FREE_DOC',__('https://preview.vwthemesdemo.com/docs/vw-cloudport-free-doc/','vw-cloudport'));
	define('VW_CLOUDPORT_THEME_BUNDLE_BUY_NOW',__('https://www.vwthemes.com/products/wp-theme-bundle','vw-cloudport'));
	define('VW_CLOUDPORT_THEME_BUNDLE_DOC',__('https://preview.vwthemesdemo.com/docs/theme-bundle/','vw-cloudport'));

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

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

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

	/**
	 * notice
	 */
	require get_template_directory() . '/inc/core/activation-notice.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', 'vw_cloudport_init_setup' );

/* Enqueue admin-notice-script js */
add_action('admin_enqueue_scripts', function ($hook) {

    wp_enqueue_script('admin-notice-script', get_template_directory_uri() . '/inc/core/js/admin-notice-script.js', ['jquery'], null, true);
});

add_action('wp_ajax_check_plugin_activation', function () {
    ob_start();

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

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

    // Map plugin identifiers to their main files
    $vw_cloudport_plugin_map = [
        'ibtana'               		   => 'ibtana-visual-editor/plugin.php'
    ];

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

    if (!isset($vw_cloudport_plugin_map[$vw_cloudport_requested_plugin])) {
        ob_end_clean();
        wp_send_json_error(['message' => 'Invalid plugin']);
    }

    $vw_cloudport_plugin_file = $vw_cloudport_plugin_map[$vw_cloudport_requested_plugin];
    $vw_cloudport_is_active   = is_plugin_active($vw_cloudport_plugin_file);

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


function vw_cloudport_dismissed_notice() {
	 update_option( 'vw_cloudport_admin_notice', true );
    wp_die();
}
add_action( 'wp_ajax_vw_cloudport_dismissed_notice', 'vw_cloudport_dismissed_notice' );