diff --git a/plogical/vhost.py b/plogical/vhost.py index a3f5df68e..990e9d7f9 100644 --- a/plogical/vhost.py +++ b/plogical/vhost.py @@ -25,7 +25,7 @@ from managePHP.phpManager import PHPManager from plogical.vhostConfs import vhostConfs from ApachController.ApacheVhosts import ApacheVhost try: - from websiteFunctions.models import Websites, ChildDomains, aliasDomains, DockerSites + from websiteFunctions.models import Websites, ChildDomains, aliasDomains, DockerSites, WPSites, WPStaging from databases.models import Databases except: pass @@ -404,6 +404,21 @@ class vhost: if ACLManager.FindIfChild() == 0: + ### Delete WordPress Sites and Staging Sites first + try: + wpSites = WPSites.objects.filter(owner=delWebsite) + for wpSite in wpSites: + # Delete any staging sites associated with this WP site + stagingSites = WPStaging.objects.filter(wpsite=wpSite) + for staging in stagingSites: + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted staging site record: {staging.id}") + # Delete the WP site itself + wpSite.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted WP site: {wpSite.id}") + except Exception as msg: + logging.CyberCPLogFileWriter.writeToFile(f"Error cleaning up WP/Staging sites: {str(msg)}") + ### Delete Docker Sites first before website deletion if os.path.exists('/home/docker/%s' % (virtualHostName)): @@ -497,6 +512,21 @@ class vhost: ## child check to make sure no database entires are being deleted from child server if ACLManager.FindIfChild() == 0: + ### Delete WordPress Sites and Staging Sites first + try: + wpSites = WPSites.objects.filter(owner=delWebsite) + for wpSite in wpSites: + # Delete any staging sites associated with this WP site + stagingSites = WPStaging.objects.filter(wpsite=wpSite) + for staging in stagingSites: + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted staging site record: {staging.id}") + # Delete the WP site itself + wpSite.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted WP site: {wpSite.id}") + except Exception as msg: + logging.CyberCPLogFileWriter.writeToFile(f"Error cleaning up WP/Staging sites: {str(msg)}") + for items in databases: mysqlUtilities.deleteDatabase(items.dbName, items.dbUser) diff --git a/plogical/virtualHostUtilities.py b/plogical/virtualHostUtilities.py index f0142db77..041c640d1 100644 --- a/plogical/virtualHostUtilities.py +++ b/plogical/virtualHostUtilities.py @@ -32,7 +32,7 @@ from ApachController.ApacheVhosts import ApacheVhost from managePHP.phpManager import PHPManager try: - from websiteFunctions.models import Websites, ChildDomains, aliasDomains + from websiteFunctions.models import Websites, ChildDomains, aliasDomains, WPSites, WPStaging from loginSystem.models import Administrator from packages.models import Package from CLManager.models import CLPackages @@ -598,6 +598,41 @@ local_name %s { 'This website already exists as child domain. [404]') return 0, "This website already exists as child domain." + # Check for orphaned staging site domain conflicts + try: + # Check if there are any WP sites with FinalURL matching this domain + conflicting_wp_sites = WPSites.objects.filter(FinalURL__icontains=virtualHostName) + for wp_site in conflicting_wp_sites: + # Check if the WP site's owner website still exists + try: + owner_website = wp_site.owner + if not Websites.objects.filter(id=owner_website.id).exists(): + # Orphaned WP site found, clean it up + wp_site.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned WP site: {wp_site.id} with URL: {wp_site.FinalURL}") + except: + # WP site owner is missing, delete it + wp_site.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned WP site: {wp_site.id} (missing owner)") + + # Check for orphaned staging sites + orphaned_staging = WPStaging.objects.filter(wpsite__FinalURL__icontains=virtualHostName) + for staging in orphaned_staging: + try: + # Check if the staging site's wpsite still exists and has valid owner + wpsite = staging.wpsite + owner_website = wpsite.owner + if not Websites.objects.filter(id=owner_website.id).exists(): + # Owner website doesn't exist, clean up staging + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned staging site: {staging.id}") + except: + # Staging site has invalid references, delete it + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned staging site: {staging.id} (invalid references)") + except Exception as e: + logging.CyberCPLogFileWriter.writeToFile(f"Error during staging site cleanup: {str(e)}") + ####### Limitations Check End logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Creating DNS records..,10') diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index eb9e60848..e04fe0a8d 100644 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -216,8 +216,21 @@ class WebsiteManager: if DeleteID != None: wstagingDelete = WPStaging.objects.get(pk=DeleteID, owner=WPobj) + + # Get the associated staging WPSites and Websites records + staging_wpsite = wstagingDelete.wpsite + staging_website = staging_wpsite.owner + + # Delete the WPStaging record first wstagingDelete.delete() + # Delete the staging WPSites record + staging_wpsite.delete() + + # Delete the staging Websites record and all associated data + from plogical.vhost import vhost + vhost.deleteVirtualHostConfigurations(staging_website.domain, staging_website.adminEmail, staging_website.package.packageName) + except BaseException as msg: da = str(msg)