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

	public static function execute( $params ) {
		// Set progress
		Lcm_Status::info( __( 'Making the final adjustments...', 'lc-migrator' ), Lcm_Stages::$STAGES['UPLOAD_TO_CLOUD'] );

		// Get archive CRC value
		$archive_crc_value = null;
		if ( isset( $params['archive_crc_value'] ) ) {
			$archive_crc_value = $params['archive_crc_value'];
		}

		// Open the archive file for writing
		$archive = new Lcm_Compressor( lcm_archive_path( $params ) );

		// Append EOF block
		$archive->close( true, $archive_crc_value );

		// Rename archive file
		if ( rename( lcm_archive_path( $params ), lcm_backup_path( $params ) ) ) {

			$blog_id = null;

			// Get subsite Blog ID
			if ( isset( $params['options']['sites'] ) && ( $sites = $params['options']['sites'] ) ) {
				if ( count( $sites ) === 1 ) {
					$blog_id = array_shift( $sites );
				}
			}

			// Set archive details
			$file = lcm_archive_name( $params );
			$link = lcm_backup_url( $params );
			$size = lcm_backup_size( $params );
			$name = lcm_site_name( $blog_id );

			// Set progress
			Lcm_Status::backup_created(
				sprintf(
					__( 'Backup created: %s Size: %s', 'lc-migrator' ),
					$name,
					$size
				),
				$link
			);
		}

		do_action( 'lcm_status_export_done', $params );

		if ( isset( $params['lcm_manual_backup'] ) ) {
			do_action( 'lcm_status_backup_created', $params );
		}

		return $params;
	}
}
