<?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_Mu_Plugins {

	public static function execute( $params ) {

		// Set progress
		Lcm_Status::info( __( 'Activating mu-plugins...', 'lc-migrator' ) );

		// Set decryption password
		$decryption_password = null;
		if ( isset( $params['decryption_password'] ) ) {
			$decryption_password = $params['decryption_password'];
		}

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

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

		// Close handle
		lcm_close( $handle );

		// Get compression type
		$compression_type = null;
		if ( ! empty( $config['Compression']['Enabled'] ) ) {
			$compression_type = $config['Compression']['Type'];
		}

		// List of must-use plugins
		$exclude_files = array(
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_ENDURANCE_PAGE_CACHE_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_ENDURANCE_PHP_EDGE_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_ENDURANCE_BROWSER_CACHE_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_GD_SYSTEM_PLUGIN_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_WP_STACK_CACHE_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_WP_COMSH_LOADER_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_WP_COMSH_HELPER_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_WP_ENGINE_SYSTEM_PLUGIN_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_WPE_SIGN_ON_PLUGIN_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_WP_ENGINE_SECURITY_AUDITOR_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_WP_CERBER_SECURITY_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_SQLITE_DATABASE_INTEGRATION_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_SQLITE_DATABASE_ZERO_NAME,
			LC_MIGRATOR_MUPLUGINS_NAME . DIRECTORY_SEPARATOR . LC_MIGRATOR_EOS_DEACTIVATE_PLUGINS_NAME,
		);

		// Open the archive file for reading
		$archive = new Lcm_Extractor( lcm_archive_path( $params ), $decryption_password, $compression_type );

		// Unpack mu-plugins files
		$archive->extract_by_files_array( WP_CONTENT_DIR, array( LC_MIGRATOR_MUPLUGINS_NAME ), $exclude_files );

		// Close the archive file
		$archive->close();

		// Set progress
		Lcm_Status::info( __( 'Mu-plugins activated.', 'lc-migrator' ) );

		return $params;
	}
}
