<?php
/*
* Plugin-Name: Product Designer App
* Plugin-URI: https://productdesignerapp.com
* Author: productdesignerapp
* Uninstall
* NOTE: All datas saved by PDA plugin will remove after delete plugin.
* Version: 1.1.2
*/
if (!defined('ABSPATH'))
{
    exit;
}
if (!defined('WP_UNINSTALL_PLUGIN'))
{
	exit;
}

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

if(!function_exists('pdapp_delete_folder'))
{
	function pdapp_delete_folder($dirPath)
	{
		global $wp_filesystem;
		if(is_dir($dirPath))
		{
			$wp_filesystem->delete($dirPath, true);
		}
	}
}

if (!function_exists('pdapp_uninstall'))
{
	function pdapp_uninstall()
	{
		global $wpdb;
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_PRODUCTS));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_ARTWORKS));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_TEMPLATES));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_PRINTS));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_SETTINGS));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_VALUES));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_DESIGNS));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_WOO_PRODUCTS));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_ORDERS));
		$wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", PDAPP_DB_ORDER_DETAIL));

		$pageIDs = $wpdb->get_results($wpdb->prepare("SELECT ID FROM %i WHERE post_content LIKE %s", [$wpdb->posts, "%[pdapp-studio-page]%"]));
		if($pageIDs)
		{
			foreach($pageIDs as $page)
			{
				$wpdb->delete($wpdb->posts, ['ID'=>$page->ID], ['%d']);
			}
		}

		pdapp_delete_folder(PDAPP_UPLOAD_DIR);
	}
}

pdapp_uninstall();
?>