<?php
/*
* Plugin Name: Product Designer App
* Description: The best online design solution for printing business - Free version
* Author: productdesignerapp
* Version: 1.1.2
* Author URI: https://productdesignerapp.com
* License: GPLv3
* Requires at least: 4.9
* Tested up to: 6.9
* Requires PHP: 7.0
* Requires PHP Architecture: 64 bits
* Requires Plugins: woocommerce
* WC requires at least: 4.0
*/

if (!defined('ABSPATH'))
{
    exit;
}

if (defined('WP_INSTALLING') && WP_INSTALLING) 
{
    return false;
}

if (!defined('PDAPP_PLUGIN_FILE'))
{
	define('PDAPP_PLUGIN_FILE', __FILE__);
}

if (!defined('PDAPP_PLUGIN_DIR'))
{
	define('PDAPP_PLUGIN_DIR', plugin_dir_path(__FILE__));
}

if (!defined('PDAPP_PLUGIN_URL'))
{
    define('PDAPP_PLUGIN_URL', plugin_dir_url(__FILE__));
}
	
require_once(PDAPP_PLUGIN_DIR .'config.php');

if(!class_exists('PDAPP_Main')) 
{
	class PDAPP_Main 
	{
		public function __construct()
		{
			require_once(PDAPP_PLUGIN_DIR .'includes/productdesignerapp-install.php');
			require_once(PDAPP_PLUGIN_DIR .'includes/productdesignerapp-cronjob.php');
			register_deactivation_hook(PDAPP_PLUGIN_FILE, 'pdapp_deactivate');
			
			global $wp_version;
			$pdapp_block = false;
			if(version_compare(PHP_VERSION, PDAPP_PHP_VERSION, '<'))
			{
				$pdapp_block = true;
				add_action('admin_notices', 'pdapp_php_version_failed');
			}else if(version_compare($wp_version, PDAPP_WP_VERSION, '<'))
			{
				$pdapp_block = true;
				add_action('admin_notices', 'pdapp_wp_version_install');
			}else if(defined('PDAPP_ACTIVATED'))
			{
				$pdapp_block = true;
				add_action('admin_notices', 'pdapp_other_version_installed');
			}else if(!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))))
			{
				$pdapp_block = true;
				add_action('admin_notices', 'pdapp_woocommerce_failed');
			}

			if($pdapp_block)
			{
				if(is_admin())
				{
					require PDAPP_ADMIN_PLUGIN_DIR .'includes/class-productdesignerapp-block.php';
				}
				return false;
			}
			
			define('PDAPP_ACTIVATED', true);
			
			$pdappInstall = new PDAPP_Install();
			if($pdappInstall->pdapp_check_table(PDAPP_DB_SETTINGS))
			{
				require_once(PDAPP_PLUGIN_DIR .'includes/productdesignerapp-helpers.php');
				require_once(PDAPP_PLUGIN_DIR .'includes/productdesignerapp-functions.php');
				
				$action = pdapp_get('action');
				$admin = pdapp_get('admin');
				if($action != '' && $admin === '1')
				{
					require PDAPP_ADMIN_PLUGIN_DIR .'includes/productdesignerapp-admin-ajax.php';
				}else if($action != '' && $admin === '0')
				{
					require PDAPP_PLUGIN_DIR .'includes/productdesignerapp-front-ajax.php';
				}else if(is_admin())
				{
					require PDAPP_ADMIN_PLUGIN_DIR .'class-productdesignerapp-admin.php';
				}else
				{
					require PDAPP_PLUGIN_DIR .'includes/productdesignerapp-front-functions.php';
				}

				$uploadPrint = PDAPP_UPLOAD_DIR .'prints';
				if(!is_dir($uploadPrint))
				{
					pdapp_move_files(PDAPP_PLUGIN_DIR .'datas/uploads/prints', $uploadPrint);
				}

				$uploadProduct = PDAPP_UPLOAD_DIR .'products';
				if(!is_dir($uploadProduct))
				{
					pdapp_move_files(PDAPP_PLUGIN_DIR .'datas/uploads/products', $uploadProduct);
				}
			}
		}
	}

	if(!function_exists('pdapp_woocommerce_failed'))
	{
		function pdapp_woocommerce_failed()
		{
			$html_message = sprintf('<div class="error">%s</div>', wpautop('PDA requires WooCommerce version '. PDAPP_WOO_VERSION .' or higher.'));
			echo wp_kses_post($html_message);
		}
	}

	if(!function_exists('pdapp_other_version_installed'))
	{
		function pdapp_other_version_installed()
		{
			$html_message = sprintf('<div class="error">%s</div>', wpautop('You are using two instances of PDA plugin at same time, please deactive one of them and try active again.'));
			echo wp_kses_post($html_message);
		}
	}

	if(!function_exists('pdapp_wp_version_install'))
	{
		function pdapp_wp_version_install()
		{
			$html_message = sprintf('<div class="error">%s</div>', wpautop('PDA requires WordPress version '. PDAPP_WP_VERSION .'+. Because you are using an earlier version, Please update your wordpress to lastest version.'));
			echo wp_kses_post($html_message);
		}
	}

	if(!function_exists('pdapp_php_version_failed'))
	{
		function pdapp_php_version_failed()
		{
			$html_message = sprintf('<div class="error">%s</div>', wpautop(sprintf('PDA requires PHP version '. PDAPP_PHP_VERSION .'+, Current PHP version: %s', PHP_VERSION)));
			echo wp_kses_post($html_message);
		}
	}

	if(!function_exists('pdapp_deactivate'))
	{
		function pdapp_deactivate()
		{
			wp_clear_scheduled_hook('pdapp_cronjob_event');
		}
	}
}

new PDAPP_Main();
?>