<?php
/**
 * All-in-One WP Migration
 * Copyright (C) 2014-2018 ServMask Inc.
 *
 * Modified Copyright (C) 2025 Lead Connector
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

if ( ! defined( 'ABSPATH' ) ) {
	die( 'Well Sir You Cannot Jump Here...haha' );
}

class Lcm_Import_Done {

	public static function execute( $params ) {
		global $wp_rewrite;

		// Check multisite.json file
		if ( is_file( lcm_multisite_path( $params ) ) ) {

			// Read multisite.json file
			$handle = lcm_open( lcm_multisite_path( $params ), 'r' );

			// Parse multisite.json file
			$multisite = lcm_read( $handle, filesize( lcm_multisite_path( $params ) ) );
			$multisite = json_decode( $multisite, true );

			// Close handle
			lcm_close( $handle );

			// Activate WordPress plugins
			if ( isset( $multisite['Plugins'] ) && ( $plugins = $multisite['Plugins'] ) ) {
				lcm_activate_plugins( $plugins );
			}

			// Deactivate WordPress SSL plugins
			if ( ! is_ssl() ) {
				lcm_deactivate_plugins(
					array(
						lcm_discover_plugin_basename( 'really-simple-ssl/rlrsssl-really-simple-ssl.php' ),
						lcm_discover_plugin_basename( 'wordpress-https/wordpress-https.php' ),
						lcm_discover_plugin_basename( 'wp-force-ssl/wp-force-ssl.php' ),
						lcm_discover_plugin_basename( 'force-https-littlebizzy/force-https.php' ),
					)
				);

				lcm_woocommerce_force_ssl( false );
			}

			// Deactivate WordPress plugins
			lcm_deactivate_plugins(
				array(
					lcm_discover_plugin_basename( 'invisible-recaptcha/invisible-recaptcha.php' ),
					lcm_discover_plugin_basename( 'wps-hide-login/wps-hide-login.php' ),
					lcm_discover_plugin_basename( 'hide-my-wp/index.php' ),
					lcm_discover_plugin_basename( 'hide-my-wordpress/index.php' ),
					lcm_discover_plugin_basename( 'mycustomwidget/my_custom_widget.php' ),
					lcm_discover_plugin_basename( 'lockdown-wp-admin/lockdown-wp-admin.php' ),
					lcm_discover_plugin_basename( 'rename-wp-login/rename-wp-login.php' ),
					lcm_discover_plugin_basename( 'wp-simple-firewall/icwp-wpsf.php' ),
					lcm_discover_plugin_basename( 'join-my-multisite/joinmymultisite.php' ),
					lcm_discover_plugin_basename( 'multisite-clone-duplicator/multisite-clone-duplicator.php' ),
					lcm_discover_plugin_basename( 'wordpress-mu-domain-mapping/domain_mapping.php' ),
					lcm_discover_plugin_basename( 'wordpress-starter/siteground-wizard.php' ),
					lcm_discover_plugin_basename( 'pro-sites/pro-sites.php' ),
					lcm_discover_plugin_basename( 'wpide/WPide.php' ),
					lcm_discover_plugin_basename( 'page-optimize/page-optimize.php' ),
					lcm_discover_plugin_basename( 'update-services/update-services.php' ),
				)
			);

			// Deactivate Swift Optimizer rules
			lcm_deactivate_swift_optimizer_rules(
				array(
					lcm_discover_plugin_basename( 'all-in-one-wp-migration/all-in-one-wp-migration.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-azure-storage-extension/all-in-one-wp-migration-azure-storage-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-b2-extension/all-in-one-wp-migration-b2-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-backup/all-in-one-wp-migration-backup.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-box-extension/all-in-one-wp-migration-box-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-digitalocean-extension/all-in-one-wp-migration-digitalocean-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-direct-extension/all-in-one-wp-migration-direct-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-dropbox-extension/all-in-one-wp-migration-dropbox-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-file-extension/all-in-one-wp-migration-file-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-ftp-extension/all-in-one-wp-migration-ftp-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-gcloud-storage-extension/all-in-one-wp-migration-gcloud-storage-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-gdrive-extension/all-in-one-wp-migration-gdrive-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-glacier-extension/all-in-one-wp-migration-glacier-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-mega-extension/all-in-one-wp-migration-mega-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-multisite-extension/all-in-one-wp-migration-multisite-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-onedrive-extension/all-in-one-wp-migration-onedrive-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-pcloud-extension/all-in-one-wp-migration-pcloud-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-pro/all-in-one-wp-migration-pro.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-s3-client-extension/all-in-one-wp-migration-s3-client-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-s3-extension/all-in-one-wp-migration-s3-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-unlimited-extension/all-in-one-wp-migration-unlimited-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-url-extension/all-in-one-wp-migration-url-extension.php' ),
					lcm_discover_plugin_basename( 'all-in-one-wp-migration-webdav-extension/all-in-one-wp-migration-webdav-extension.php' ),
				)
			);

			// Deactivate Revolution Slider
			lcm_deactivate_revolution_slider( lcm_discover_plugin_basename( 'revslider/revslider.php' ) );

			// Deactivate Jetpack modules
			lcm_deactivate_jetpack_modules( array( 'photon', 'sso' ) );

			// Flush Elementor cache
			lcm_elementor_cache_flush();

			// Initial DB version
			lcm_initial_db_version();

		} else {

			// Check package.json file
			if ( is_file( lcm_package_path( $params ) ) ) {

				// Read package.json file
				$handle = lcm_open( lcm_package_path( $params ), 'r' );

				// Parse package.json file
				$package = lcm_read( $handle, filesize( lcm_package_path( $params ) ) );
				$package = json_decode( $package, true );

				// Close handle
				lcm_close( $handle );

				// Activate WordPress plugins
				if ( isset( $package['Plugins'] ) && ( $plugins = $package['Plugins'] ) ) {
					lcm_activate_plugins( $plugins );
				}

				// Activate WordPress template
				if ( isset( $package['Template'] ) && ( $template = $package['Template'] ) ) {
					lcm_activate_template( $template );
				}

				// Activate WordPress stylesheet
				if ( isset( $package['Stylesheet'] ) && ( $stylesheet = $package['Stylesheet'] ) ) {
					lcm_activate_stylesheet( $stylesheet );
				}

				// Deactivate WordPress SSL plugins
				if ( ! is_ssl() ) {
					lcm_deactivate_plugins(
						array(
							lcm_discover_plugin_basename( 'really-simple-ssl/rlrsssl-really-simple-ssl.php' ),
							lcm_discover_plugin_basename( 'wordpress-https/wordpress-https.php' ),
							lcm_discover_plugin_basename( 'wp-force-ssl/wp-force-ssl.php' ),
							lcm_discover_plugin_basename( 'force-https-littlebizzy/force-https.php' ),
						)
					);

					lcm_woocommerce_force_ssl( false );
				}

				// Deactivate WordPress plugins
				lcm_deactivate_plugins(
					array(
						lcm_discover_plugin_basename( 'invisible-recaptcha/invisible-recaptcha.php' ),
						lcm_discover_plugin_basename( 'wps-hide-login/wps-hide-login.php' ),
						lcm_discover_plugin_basename( 'hide-my-wp/index.php' ),
						lcm_discover_plugin_basename( 'hide-my-wordpress/index.php' ),
						lcm_discover_plugin_basename( 'mycustomwidget/my_custom_widget.php' ),
						lcm_discover_plugin_basename( 'lockdown-wp-admin/lockdown-wp-admin.php' ),
						lcm_discover_plugin_basename( 'rename-wp-login/rename-wp-login.php' ),
						lcm_discover_plugin_basename( 'wp-simple-firewall/icwp-wpsf.php' ),
						lcm_discover_plugin_basename( 'join-my-multisite/joinmymultisite.php' ),
						lcm_discover_plugin_basename( 'multisite-clone-duplicator/multisite-clone-duplicator.php' ),
						lcm_discover_plugin_basename( 'wordpress-mu-domain-mapping/domain_mapping.php' ),
						lcm_discover_plugin_basename( 'wordpress-starter/siteground-wizard.php' ),
						lcm_discover_plugin_basename( 'pro-sites/pro-sites.php' ),
						lcm_discover_plugin_basename( 'wpide/WPide.php' ),
						lcm_discover_plugin_basename( 'page-optimize/page-optimize.php' ),
						lcm_discover_plugin_basename( 'update-services/update-services.php' ),
					)
				);

				// Deactivate Swift Optimizer rules
				lcm_deactivate_swift_optimizer_rules(
					array(
						lcm_discover_plugin_basename( 'all-in-one-wp-migration/all-in-one-wp-migration.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-azure-storage-extension/all-in-one-wp-migration-azure-storage-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-b2-extension/all-in-one-wp-migration-b2-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-backup/all-in-one-wp-migration-backup.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-box-extension/all-in-one-wp-migration-box-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-digitalocean-extension/all-in-one-wp-migration-digitalocean-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-direct-extension/all-in-one-wp-migration-direct-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-dropbox-extension/all-in-one-wp-migration-dropbox-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-file-extension/all-in-one-wp-migration-file-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-ftp-extension/all-in-one-wp-migration-ftp-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-gcloud-storage-extension/all-in-one-wp-migration-gcloud-storage-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-gdrive-extension/all-in-one-wp-migration-gdrive-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-glacier-extension/all-in-one-wp-migration-glacier-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-mega-extension/all-in-one-wp-migration-mega-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-multisite-extension/all-in-one-wp-migration-multisite-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-onedrive-extension/all-in-one-wp-migration-onedrive-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-pcloud-extension/all-in-one-wp-migration-pcloud-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-pro/all-in-one-wp-migration-pro.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-s3-client-extension/all-in-one-wp-migration-s3-client-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-s3-extension/all-in-one-wp-migration-s3-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-unlimited-extension/all-in-one-wp-migration-unlimited-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-url-extension/all-in-one-wp-migration-url-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-webdav-extension/all-in-one-wp-migration-webdav-extension.php' ),
					)
				);

				// Deactivate Revolution Slider
				lcm_deactivate_revolution_slider( lcm_discover_plugin_basename( 'revslider/revslider.php' ) );

				// Deactivate Jetpack modules
				lcm_deactivate_jetpack_modules( array( 'photon', 'sso' ) );

				// Flush Elementor cache
				lcm_elementor_cache_flush();

				// Initial DB version
				lcm_initial_db_version();
			}
		}

		// Check blogs.json file
		if ( is_file( lcm_blogs_path( $params ) ) ) {

			// Read blogs.json file
			$handle = lcm_open( lcm_blogs_path( $params ), 'r' );

			// Parse blogs.json file
			$blogs = lcm_read( $handle, filesize( lcm_blogs_path( $params ) ) );
			$blogs = json_decode( $blogs, true );

			// Close handle
			lcm_close( $handle );

			// Loop over blogs
			foreach ( $blogs as $blog ) {

				// Activate WordPress plugins
				if ( isset( $blog['New']['Plugins'] ) && ( $plugins = $blog['New']['Plugins'] ) ) {
					lcm_activate_plugins( $plugins );
				}

				// Activate WordPress template
				if ( isset( $blog['New']['Template'] ) && ( $template = $blog['New']['Template'] ) ) {
					lcm_activate_template( $template );
				}

				// Activate WordPress stylesheet
				if ( isset( $blog['New']['Stylesheet'] ) && ( $stylesheet = $blog['New']['Stylesheet'] ) ) {
					lcm_activate_stylesheet( $stylesheet );
				}

				// Deactivate WordPress SSL plugins
				if ( ! is_ssl() ) {
					lcm_deactivate_plugins(
						array(
							lcm_discover_plugin_basename( 'really-simple-ssl/rlrsssl-really-simple-ssl.php' ),
							lcm_discover_plugin_basename( 'wordpress-https/wordpress-https.php' ),
							lcm_discover_plugin_basename( 'wp-force-ssl/wp-force-ssl.php' ),
							lcm_discover_plugin_basename( 'force-https-littlebizzy/force-https.php' ),
						)
					);

					lcm_woocommerce_force_ssl( false );
				}

				// Deactivate WordPress plugins
				lcm_deactivate_plugins(
					array(
						lcm_discover_plugin_basename( 'invisible-recaptcha/invisible-recaptcha.php' ),
						lcm_discover_plugin_basename( 'wps-hide-login/wps-hide-login.php' ),
						lcm_discover_plugin_basename( 'hide-my-wp/index.php' ),
						lcm_discover_plugin_basename( 'hide-my-wordpress/index.php' ),
						lcm_discover_plugin_basename( 'mycustomwidget/my_custom_widget.php' ),
						lcm_discover_plugin_basename( 'lockdown-wp-admin/lockdown-wp-admin.php' ),
						lcm_discover_plugin_basename( 'rename-wp-login/rename-wp-login.php' ),
						lcm_discover_plugin_basename( 'wp-simple-firewall/icwp-wpsf.php' ),
						lcm_discover_plugin_basename( 'join-my-multisite/joinmymultisite.php' ),
						lcm_discover_plugin_basename( 'multisite-clone-duplicator/multisite-clone-duplicator.php' ),
						lcm_discover_plugin_basename( 'wordpress-mu-domain-mapping/domain_mapping.php' ),
						lcm_discover_plugin_basename( 'wordpress-starter/siteground-wizard.php' ),
						lcm_discover_plugin_basename( 'pro-sites/pro-sites.php' ),
						lcm_discover_plugin_basename( 'wpide/WPide.php' ),
						lcm_discover_plugin_basename( 'page-optimize/page-optimize.php' ),
						lcm_discover_plugin_basename( 'update-services/update-services.php' ),
					)
				);

				// Deactivate Swift Optimizer rules
				lcm_deactivate_swift_optimizer_rules(
					array(
						lcm_discover_plugin_basename( 'all-in-one-wp-migration/all-in-one-wp-migration.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-azure-storage-extension/all-in-one-wp-migration-azure-storage-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-b2-extension/all-in-one-wp-migration-b2-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-backup/all-in-one-wp-migration-backup.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-box-extension/all-in-one-wp-migration-box-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-digitalocean-extension/all-in-one-wp-migration-digitalocean-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-direct-extension/all-in-one-wp-migration-direct-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-dropbox-extension/all-in-one-wp-migration-dropbox-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-file-extension/all-in-one-wp-migration-file-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-ftp-extension/all-in-one-wp-migration-ftp-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-gcloud-storage-extension/all-in-one-wp-migration-gcloud-storage-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-gdrive-extension/all-in-one-wp-migration-gdrive-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-glacier-extension/all-in-one-wp-migration-glacier-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-mega-extension/all-in-one-wp-migration-mega-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-multisite-extension/all-in-one-wp-migration-multisite-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-onedrive-extension/all-in-one-wp-migration-onedrive-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-pcloud-extension/all-in-one-wp-migration-pcloud-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-pro/all-in-one-wp-migration-pro.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-s3-client-extension/all-in-one-wp-migration-s3-client-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-s3-extension/all-in-one-wp-migration-s3-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-unlimited-extension/all-in-one-wp-migration-unlimited-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-url-extension/all-in-one-wp-migration-url-extension.php' ),
						lcm_discover_plugin_basename( 'all-in-one-wp-migration-webdav-extension/all-in-one-wp-migration-webdav-extension.php' ),
					)
				);

				// Deactivate Revolution Slider
				lcm_deactivate_revolution_slider( lcm_discover_plugin_basename( 'revslider/revslider.php' ) );

				// Deactivate Jetpack modules
				lcm_deactivate_jetpack_modules( array( 'photon', 'sso' ) );

				// Flush Elementor cache
				lcm_elementor_cache_flush();

				// Initial DB version
				lcm_initial_db_version();
			}
		}

		// Clear auth cookie (WP Cerber)
		if ( lcm_validate_plugin_basename( 'wp-cerber/wp-cerber.php' ) ) {
			wp_clear_auth_cookie();
		}

		$should_reset_permalinks = false;

		// Switch to default permalink structure
		if ( ( $should_reset_permalinks = lcm_should_reset_permalinks( $params ) ) ) {
			$wp_rewrite->set_permalink_structure( '' );
		}

		// Set progress
		Lcm_Status::done( __( 'Your site has been imported successfully!', 'lc-migrator' ) );

		do_action( 'lcm_status_import_done', $params );

		return $params;
	}
}
