<?php
/**
 * Plugin Name: ELEX HelpDesk & Customer Support Ticket System
 * Plugin URI: https://elextensions.com/plugin/wsdesk-wordpress-helpdesk-plugin-free-version/
 * Description: Enhances your customer service and enables efficient handling of customer issues.
 * Version: 3.3.7
 * Author: ELEXtensions
 * Author URI: https://elextensions.com/
 * Text Domain: wsdesk
 * Domain Path: /lang
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! defined( 'WSDESK_PLUGIN_TYPE' ) ) {
	define( 'WSDESK_PLUGIN_TYPE', 'basic' );
}

if ( defined( 'EH_CRM_VERSION' ) ) {
	deactivate_plugins( plugin_basename( __FILE__ ) );
	$activated_plugins     = (array) get_option( 'active_plugins' );
	$wsdesk_plugins        = array(
		'wsdesk-premium/wsdesk.php' => "PREMIUM Version of WSDesk Plugin is activated. Please deactivate the PREMIUM Version of WSDesk before activating BASIC version.<br>Don't worry! Your ticket and settings data will be retained.<br>Go back to <a href='" . esc_html( admin_url( 'plugins.php' ) ) . "'>plugins page</a>",
		'wsdesk-basic/wsdesk.php'   => "BASIC Version of WSDesk Plugin is activated. Please deactivate the BASIC Version of WSDesk before activating PREMIUM version.<br>Don't worry! Your ticket and settings data will be retained.<br>Go back to <a href='" . esc_html( admin_url( 'plugins.php' ) ) . "'>plugins page</a>",
		'elex-helpdesk-customer-support-ticket-system/elex-wordpress-helpdesk.php' => "BASIC Version of WSDesk Plugin is activated. Please deactivate  the BASIC Version of WSDesk before activating PREMIUM version.<br>Don't worry! Your ticket and settings data will be retained.<br>Go back to <a href='" . esc_html( admin_url( 'plugins.php' ) ) . "'>plugins page</a>",
		'elex-wordpress-helpdesk/elex-wordpress-helpdesk.php' => "BASIC Version of WSDesk Plugin is activated. Please deactivate the BASIC Version of WSDesk before activating PREMIUM version.<br>Don't worry! Your ticket and settings data will be retained.<br>Go back to <a href='" . esc_html( admin_url( 'plugins.php' ) ) . "'>plugins page</a>",
		'wsdesk-helpdesk-customer-support-ticket-system/wsdesk.php' => "WooCommerce Version of WSDesk Plugin is activated. Please deactivate the WooCommerce Version of WSDesk before activating BASIC version.<br>Don't worry! Your ticket and settings data will be retained.<br>Go back to <a href='" . esc_html( admin_url( 'plugins.php' ) ) . "'>plugins page</a>",
	);
	$current_wsdesk_plugin = plugin_basename( __FILE__ );
	
	foreach ( $wsdesk_plugins as $wsdesk_plugin => $error_msg ) {
		
		if ( $current_wsdesk_plugin === $wsdesk_plugin ) {
			continue;
		}
		if ( in_array( $wsdesk_plugin, $activated_plugins, true ) ) {
			wp_die( wp_kses_post( $error_msg ) );
		}
	}
	return;
} else {

	if ( ! defined( 'EH_CRM_VERSION' ) ) {
		define( 'EH_CRM_VERSION', '3.1.2' );
	}
	if ( ! defined( 'EH_CRM_MAIN_URL' ) ) {
		define( 'EH_CRM_MAIN_URL', plugin_dir_url( __FILE__ ) );
	}
	if ( ! defined( 'EH_CRM_MAIN_PATH' ) ) {
		define( 'EH_CRM_MAIN_PATH', plugin_dir_path( __FILE__ ) );
	}
	if ( ! defined( 'EH_CRM_MAIN_IMG' ) ) {
		define( 'EH_CRM_MAIN_IMG', EH_CRM_MAIN_URL . 'assets/img/' );
	}
	if ( ! defined( 'EH_CRM_MAIN_CSS' ) ) {
		define( 'EH_CRM_MAIN_CSS', EH_CRM_MAIN_URL . 'assets/css/' );
	}
	if ( ! defined( 'EH_CRM_MAIN_JS' ) ) {
		define( 'EH_CRM_MAIN_JS', EH_CRM_MAIN_URL . 'assets/js/' );
	}
	if ( ! defined( 'EH_CRM_MAIN_VENDOR' ) ) {
		define( 'EH_CRM_MAIN_VENDOR', EH_CRM_MAIN_PATH . 'vendor/' );
	}
	if ( ! defined( 'EH_CRM_MAIN_VIEWS' ) ) {
		define( 'EH_CRM_MAIN_VIEWS', EH_CRM_MAIN_PATH . 'views/' );
	}
	if ( ! defined( 'EH_CRM_WOO_STATUS' ) ) {
		if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ), true ) ) {
			define( 'EH_CRM_WOO_STATUS', true );
		} else {
			define( 'EH_CRM_WOO_STATUS', false );
		}
	}
	if ( ! defined( 'EH_CRM_EDD_STATUS' ) ) {
		if ( in_array( 'easy-digital-downloads/easy-digital-downloads.php', get_option( 'active_plugins' ), true ) ) {
			define( 'EH_CRM_EDD_STATUS', true );
		} else {
			define( 'EH_CRM_EDD_STATUS', false );
		}
	}

	if ( ! defined( 'EH_CRM_PAY_FOR_SUPPORT_STATUS' ) ) {
		if ( in_array( 'wsdesk-pay-for-support/wsdesk-pay-for-support.php', get_option( 'active_plugins' ) ) ) {
			define( 'EH_CRM_PAY_FOR_SUPPORT_STATUS', true );
		} else {
			define( 'EH_CRM_PAY_FOR_SUPPORT_STATUS', false );
		}
	}

	if ( ! defined( 'EH_CRM_WSDESK_SIGNATURE_STATUS' ) ) {
		if ( in_array( 'wsdesk-agent-signature-add-on/wsdesk-agent-signature-add-on.php', get_option( 'active_plugins' ) ) ) {
			define( 'EH_CRM_WSDESK_SIGNATURE_STATUS', true );
		} else {
			define( 'EH_CRM_WSDESK_SIGNATURE_STATUS', false );
		}
	}
	if ( ! defined( 'EH_CRM_WSDESK_SMS_NOTIFICATION_STATUS' ) ) {
		if ( in_array( 'wsdesk-sms-notification-addon/wsdesk-sms-notification-addon.php', get_option( 'active_plugins' ) ) ) {
			define( 'EH_CRM_WSDESK_SMS_NOTIFICATION_STATUS', true );
		} else {
			define( 'EH_CRM_WSDESK_SMS_NOTIFICATION_STATUS', false );
		}
	}

	// review component
	if ( ! function_exists( 'get_plugin_data' ) ) {
		require_once  ABSPATH . 'wp-admin/includes/plugin.php';
	}
	include_once __DIR__ . '/review_and_troubleshoot_notify/review-and-troubleshoot-notify-class.php';
	$data                      = get_plugin_data( __FILE__ , false, false );
	$data['name']              = $data['Name'];
	$data['basename']          = plugin_basename( __FILE__ );
	$data['documentation_url'] = 'https://elextensions.com/knowledge-base/set-up-wsdesk-wordpress-helpdesk-plugin/';
	$data['support_url']       = 'https://wordpress.org/support/plugin/elex-helpdesk-customer-support-ticket-system/';
	$data['rating_url']        = 'https://elextensions.com/plugin/wsdesk-wordpress-helpdesk-plugin-free-version/#reviews';

	new \Elex_Review_Components( $data );

	require_once  ABSPATH . 'wp-admin/includes/plugin.php' ;
	require_once  __DIR__ . '/vendor/autoload.php' ;

	// Enter your plugin unique option name below $option_name variable
	add_action( 'admin_init', 'wsdesk_welcome' );
	register_activation_hook( __FILE__, 'eh_crm_install' );
	register_deactivation_hook( __FILE__, 'wsdesk_deactivate_work' );
	add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wsdesk_action_link' );
	add_filter( 'plugin_row_meta', 'wsdesk_plugin_row_meta', 10, 2 );
	add_action( 'plugins_loaded', 'eh_crm_update_function' );
	add_action( 'init', 'wsdesk_lang_loader' );
	add_action( 'init', 'register_woocommerce_redirect_url' );

	function register_woocommerce_redirect_url() {
		if ( ! is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
			return;
		}

		$login_redirect_url = isset( $_GET['redirect_to'] ) ? sanitize_text_field( $_GET['redirect_to'] ) : false;

		if ( ! $login_redirect_url ) {
			return;
		}

		add_filter(
			'woocommerce_login_redirect',
			function () use ( $login_redirect_url ) {
				return $login_redirect_url;
			}
		);
	}

	function wsdesk_lang_loader() {
		load_plugin_textdomain( 'wsdesk', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
	}
	require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-public-functions.php' ;

	function eh_crm_update_function() {
		global $base;
		if ( get_option( 'wsdesk_version_' . $base ) !== EH_CRM_VERSION ) {
			require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-install-functions.php' ;
			EH_CRM_Install::update_tables( $base );
		}
	}

	function wsdesk_deactivate_work() {
		$cron = new EH_CRM_Cron_Setup();
		$cron->crawler_schedule_terminate();
		delete_option( 'wsdesk_pack', '' );
	}

	function eh_crm_install( $network_wide ) {
		require_once  ABSPATH . 'wp-admin/includes/upgrade.php' ;
		require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-install-functions.php' ;
		global $wpdb;
		if ( is_multisite() ) {
			// Get all blogs in the network and activate plugin on each one
			$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
			foreach ( $blog_ids as $blog_id ) {
				switch_to_blog( $blog_id );
				if ( is_plugin_active( plugin_basename( __FILE__ ) ) || $network_wide ) {
					EH_CRM_Install::install_tables();
				}
				restore_current_blog();
			}
		} else {
			EH_CRM_Install::install_tables();
		}
		$shown = get_option( 'wsdesk_setup_wizard', 'not' );
		if ( 'not' === $shown ) {
			set_transient( '_wsdesk_welcome_screen_activation_redirect', true, 30 );
		}
	}

	function wsdesk_welcome() {
		if ( ! get_transient( '_wsdesk_welcome_screen_activation_redirect' ) && get_option( 'wsdesk_setup_wizard', 'not' ) == 'shown' ) {
			return;
		}
		delete_transient( '_wsdesk_welcome_screen_activation_redirect' );
		wp_safe_redirect(
			add_query_arg(
				array(
					'page'     => 'wsdesk-setup',
					'_wpnonce' => wp_create_nonce( 'wsdesk-setup' ),
				),
				admin_url( 'index.php' )
			)
		);
	}
	

	function wsdesk_wizard_includes() {
		if ( ! empty( $_GET['page'] ) ) {
			switch ( $_GET['page'] ) {
				case 'wsdesk-setup':
					include_once EH_CRM_MAIN_VIEWS . 'welcome/welcome.php';
					break;
				case 'wsdesk_print':
					include_once EH_CRM_MAIN_VIEWS . 'tickets/wsdesk_print.php';
					break;
			}
		}
	}

	function wsdesk_action_link( $links ) {
		$plugin_links = array(
			'<a href="' . admin_url( 'admin.php?page=wsdesk_tickets' ) . '">' . __( 'Tickets', 'wsdesk' ) . '</a>',
			'<a href="' . admin_url( 'admin.php?page=wsdesk_settings' ) . '">' . __( 'Settings', 'wsdesk' ) . '</a>',
		);
		array_push( $plugin_links, '<a onclick="return confirm(' . "'You are about to perform factory reset of WSDesk. This will delete all data permanently. This action is irreversible. This action can only be performed if you have \'DROP\' privilege for your database. Proceed?'" . ');" href="' . admin_url( 'admin.php?page=wsdesk_factory_reset' ) . '">' . __( 'Factory Reset', 'wsdesk' ) . '</a>' );
		if ( array_key_exists( 'deactivate', $links ) ) {
			$links['deactivate'] = str_replace( '<a', '<a class="wsdesk-deactivate-link"', $links['deactivate'] );
		}
		return array_merge( $plugin_links, $links );
	}

	function wsdesk_plugin_row_meta( $links, $file ) {
		if ( plugin_basename( __FILE__ ) === $file ) {
			$row_meta = array(
				'<a href="https://elextensions.com/set-up-wsdesk-wordpress-helpdesk-plugin/" target="_blank">' . __( 'Docs', 'wsdesk' ) . '</a>',
				'<a href="https://elextensions.com/support/" target="_blank">' . __( 'Support', 'wsdesk' ) . '</a>',
				'<a href="https://elextensions.com/wsdesk-wordpress-helpdesk-plugin-basic-version-changelog/" target="_blank">' . __( 'Changelog', 'wsdesk' ) . '</a>',
			);
			return array_merge( $links, $row_meta );
		}
		return (array) $links;
	}

	function eh_crm_run() {
		if ( ! class_exists( 'CRM_Init_Handler' ) ) {
			if ( ! defined( 'EH_CRM_WOO_VENDOR' ) ) {
				$wsdesk_logged_user = wp_get_current_user();
				$vendor_roles       = eh_crm_get_settingsmeta( 0, 'woo_vendor_roles' );
				if ( empty( $vendor_roles ) ) {
					$vendor_roles = array();
				}
				$result = array_intersect( $vendor_roles, $wsdesk_logged_user->roles );
				if ( ! empty( $result ) ) {
					define( 'EH_CRM_WOO_VENDOR', $wsdesk_logged_user->ID );
				} else {
					define( 'EH_CRM_WOO_VENDOR', false );
				}
			}
			require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-init-handler.php' ;
			require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-js-translation.php' ;
			require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-ajax-functions.php' ;
			require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-settings-handler.php' ;
			require_once  EH_CRM_MAIN_PATH . 'includes/Mailbox.php' ;
			require_once  EH_CRM_MAIN_PATH . 'includes/IncomingMail.php' ;
			require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-archive-ajax-functions.php' ;
			new EH_CRM_Init_Handler();
			$selected_views = eh_crm_get_settings( array( 'slug' => 'labels_view' ) );
			if ( empty( $selected_views ) ) {
				$views_settings = array(
					'labels_view' => array(
						'title'  => 'Labels',
						'filter' => 'yes',
						'type'   => 'view',
						'vendor' => '',
					),
					'agents_view' => array(
						'title'  => 'Agents',
						'filter' => 'yes',
						'type'   => 'view',
						'vendor' => '',
					),
					'tags_view'   => array(
						'title'  => 'Tags',
						'filter' => 'yes',
						'type'   => 'view',
						'vendor' => '',
					),
					'users_view'  => array(
						'title'  => 'Users',
						'filter' => 'yes',
						'type'   => 'view',
						'vendor' => '',
					),
				);
				foreach ( $views_settings as $key => $value ) {
					eh_crm_insert_settings( $value, null, $key );
				}
				eh_crm_update_settingsmeta( 0, 'selected_views', array( 'labels_view', 'agents_view', 'tags_view', 'users_view' ) );
			}
			$wsdesk_powered_by_status = eh_crm_get_settingsmeta( '0', 'wsdesk_powered_by_status' );
			if ( 'disable' === $wsdesk_powered_by_status ) {
				define( 'WSDESK_POWERED_SUPPORT', true );
				define( 'WSDESK_POWERED_EMAIL', true );
			}
		}
	}
	add_action( 'init', 'eh_crm_run', 99 );
	add_action( 'init', 'wsdesk_wizard_includes', 100 );
	require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-email-oauth.php' ;
	require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-cron-setup.php' ;
	new EH_CRM_Cron_Setup();

	if ( isset( $_GET['page'] ) ) { //code for factory reset
		if ( is_admin() ) {
			if ( 'wsdesk_factory_reset' === $_GET['page'] ) {

				require_once  EH_CRM_MAIN_PATH . 'includes/class-crm-init-handler.php' ;

				deactivate_plugins( plugin_basename( __FILE__ ) );

				EH_CRM_Init_Handler::factory_reset();

				header( 'Location: ' . admin_url( 'plugins.php' ) );
				exit;
			}
		}
	}
	if ( is_plugin_active( 'wsdesk-premium/wsdesk.php' ) ) {
		$debug_status = eh_crm_get_settingsmeta( 0, 'wsdesk_debug_status' );
		if ( 'enable' === $debug_status ) {
			eh_crm_db_debug_error_log( ' ------------- WSDesk Debug Mode Started ------------- ' );
			eh_crm_db_debug_error_log( ' ------------- Tickets Table Started ------------- ' );
			eh_crm_db_collation( 'wsdesk_tickets' );
			eh_crm_db_debug_error_log( ' ------------- Tickets Table Ended ------------- ' );
			eh_crm_db_debug_error_log( ' ------------- Settings Table Started ------------- ' );
			eh_crm_db_collation( 'wsdesk_settings' );
			eh_crm_db_debug_error_log( ' ------------- Settings Table Ended ------------- ' );
			eh_crm_db_debug_error_log( ' ------------- Ticketsmeta Table Started ------------- ' );
			eh_crm_db_collation( 'wsdesk_ticketsmeta' );
			eh_crm_db_debug_error_log( ' ------------- Ticketsmeta Ticketsmeta Ended ------------- ' );
			eh_crm_db_debug_error_log( ' ------------- Settingsmeta Table Started ------------- ' );
			eh_crm_db_collation( 'wsdesk_settingsmeta' );
			eh_crm_db_debug_error_log( ' ------------- Settingsmeta Table Ended ------------- ' );
			eh_crm_db_debug_error_log( ' ------------- WSDesk Debug Mode Ended ------------- ' );
		}
	}
	add_action( 'admin_bar_menu', 'elex_wsdesk_admin_bar', 900 );

	function elex_wsdesk_admin_bar( $wp_admin_bar ) {
		$admin_bar_args = array(
			'id'     => 'wsdesk_tickets',
			'parent' => 'site-name',
			'href'   => admin_url( 'admin.php?page=wsdesk_tickets' ),
			'title'  => __( 'WSDesk Tickets', 'wsdesk' ),
		);
		$wp_admin_bar->add_node( $admin_bar_args );
	}

	//Enqueue the Dashicons script
	add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' );
	function load_dashicons_front_end() {
		wp_enqueue_style( 'dashicons' );
	}
}
