<?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/>.
 */
namespace Lcmigrator\LcMigrator\Loader;

use Lcm_Directory;
use Lcmigrator\LcMigrator\Admin\LcMigrator_Admin;
use Lcmigrator\LcMigrator\Loader\LcMigrator_Loader;

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       http://example.com
 * @since      1.0.0
 *
 * @package    LC Migrator
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    LC Migrator
 * @author     Aman Vishwakarma
 */
class LcMigratorApp
{

    protected LcMigrator_Loader $loader;

    protected $plugin_name;

    /**
     * The current version of the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $version    The current version of the plugin.
     */
    protected $version;

    /**
     * Define the core functionality of the plugin.
     *
     * Set the plugin name and the plugin version that can be used throughout the plugin.
     * Load the dependencies, define the locale, and set the hooks for the admin area and
     * the public-facing side of the site.
     *
     * @since    1.0.0
     */
    public function __construct()
    {
        if (defined('LC_MIGRATOR_VERSION')) {
            $this->version = LC_MIGRATOR_VERSION;
        } else {
            $this->version = '1.0.0';
        }
        $this->plugin_name = LC_MIGRATOR_PLUGIN_NAME;

        $this->load_dependencies();
        // $this->set_locale();
        $this->define_admin_hooks();
        // $this->define_public_hooks();

    }

    /**
     * Load the required dependencies for this plugin.
     *
     * Include the following files that make up the plugin:
     *
     * - LeadConnector_Loader. Orchestrates the hooks of the plugin.
     * - LeadConnector_i18n. Defines internationalization functionality.
     * - LeadConnector_Admin. Defines all hooks for the admin area.
     * - LeadConnector_Public. Defines all hooks for the public side of the site.
     *
     * Create an instance of the loader which will be used to register the hooks
     * with WordPress.
     *
     * @since    1.0.0
     * @access   private
     */
    private function load_dependencies()
    {

        // /**
        //  * The class responsible for orchestrating the actions and filters of the
        //  * core plugin.
        //  */
        // require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-lc-loader.php';

        // /**
        //  * The class responsible for defining internationalization functionality
        //  * of the plugin.
        //  */
        // require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-lc-i18n.php';

        // /**
        //  * The class responsible for defining all actions that occur in the admin area.
        //  */
        // require_once plugin_dir_path(dirname(__FILE__)) . 'admin/class-lc-admin.php';

        // /**
        //  * The class responsible for defining all actions that occur in the public-facing
        //  * side of the site.
        //  */
        // require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-lc-public.php';

        $this->loader = new LcMigrator_Loader();

    }

    // /**
    //  * Define the locale for this plugin for internationalization.
    //  *
    //  * Uses the LeadConnector_i18n class in order to set the domain and to register the hook
    //  * with WordPress.
    //  *
    //  * @since    1.0.0
    //  * @access   private
    //  */
    // private function set_locale()
    // {

    //     $plugin_i18n = new LeadConnector_i18n();

    //     $this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');

    // }

    /**
     * Register all of the hooks related to the admin area functionality
     * of the plugin.
     *
     * @since    1.0.0
     * @access   private
     */
    private function define_admin_hooks()
    {

        $plugin_admin = new LcMigrator_Admin($this->get_plugin_name(), $this->get_version());
        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
        $this->loader->add_action('rest_api_init', $plugin_admin, 'admin_rest_api_init');

    }


    /**
     * Run the loader to execute all of the hooks with WordPress.
     *
     * @since    1.0.0
     */
    public function run()
    {
        $this->loader->run();
        $this->activate_main_actions();
    }

    public function activate_main_actions()
    {
        // activate main actions
		// add_action( 'admin_init', array( $this, 'router' ) );
		add_action( 'admin_init', array( $this, 'wp_importing' ), 5 );
		add_action( 'admin_init', array( $this, 'setup_backups_folder' ) );
		add_action( 'admin_init', array( $this, 'setup_storage_folder' ) );
		// add_action( 'admin_init', array( $this, 'setup_secret_key' ) );

        // Main Export Actions
        add_action( 'plugins_loaded', array( $this, 'lcm_commands' ), 10 );
    }

    public function setup_storage_folder() {
		$this->create_storage_folder( LC_MIGRATOR_STORAGE_PATH );
		$this->create_storage_htaccess( LC_MIGRATOR_STORAGE_HTACCESS );
	}

    public function create_storage_folder( $path ) {
		if ( ! Lcm_Directory::create( $path ) ) {
			error_log( 'Failed to create storage folder: ' . $path );
		}
	}

	public function create_storage_htaccess( $path ) {
		if ( ! Lcm_Directory::create( $path ) ) {
			error_log( 'Failed to create storage htaccess: ' . $path );
		}
	}

    public function wp_importing() {
		if ( isset( $_GET['lcm_import'] ) ) {
			if ( ! defined( 'WP_IMPORTING' ) ) {
				define( 'WP_IMPORTING', true );
			}
		}
	}

    public function setup_backups_folder() {
		$this->create_backups_folder( LC_MIGRATOR_BACKUPS_PATH );
		$this->create_backups_htaccess( LC_MIGRATOR_BACKUPS_HTACCESS );
	}

    public function create_backups_folder( $path ) {
		if ( ! Lcm_Directory::create( $path ) ) {
			error_log( 'Failed to create backups folder: ' . $path );
		}
	}

    public function create_backups_htaccess( $path ) {
		if ( ! Lcm_Directory::create( $path ) ) {
			error_log( 'Failed to create backups htaccess: ' . $path );
		}
	}
    
    public function lcm_commands()
    {

        // All the export Filters
        add_filter( 'lcm_export', 'Lcm_Export_Init::execute', 5 );
		add_filter( 'lcm_export', 'Lcm_Export_Compatibility::execute', 10 );
		add_filter( 'lcm_export', 'Lcm_Export_Archive::execute', 30 );
		add_filter( 'lcm_export', 'Lcm_Export_Config::execute', 50 );
		add_filter( 'lcm_export', 'Lcm_Export_Config_File::execute', 60 );
		add_filter( 'lcm_export', 'Lcm_Export_Enumerate_Content::execute', 100 );
		add_filter( 'lcm_export', 'Lcm_Export_Enumerate_Media::execute', 110 );
		add_filter( 'lcm_export', 'Lcm_Export_Enumerate_Plugins::execute', 120 );
		add_filter( 'lcm_export', 'Lcm_Export_Enumerate_Themes::execute', 130 );
		add_filter( 'lcm_export', 'Lcm_Export_Enumerate_Tables::execute', 140 );
		add_filter( 'lcm_export', 'Lcm_Export_Content::execute', 150 );
		add_filter( 'lcm_export', 'Lcm_Export_Media::execute', 160 );
		add_filter( 'lcm_export', 'Lcm_Export_Plugins::execute', 170 );
		add_filter( 'lcm_export', 'Lcm_Export_Themes::execute', 180 );
		add_filter( 'lcm_export', 'Lcm_Export_Database::execute', 200 );
		add_filter( 'lcm_export', 'Lcm_Export_Database_File::execute', 220 );
        add_filter( 'lcm_export', 'Lcm_Export_Archive_Crc::execute', 240 );
		add_filter( 'lcm_export', 'Lcm_Export_Generate_Cloud_Url::execute', 250 );
		add_filter( 'lcm_export', 'Lcm_Export_Upload_To_Cloud::execute', 260 );
		add_filter( 'lcm_export', 'Lcm_Export_Clean::execute', 300 );
		add_filter( 'lcm_export', 'Lcm_Start_Server_Import::execute', 310 );

        // Add the import filters 
        add_filter( 'lcm_import', 'Lcm_Import_Compatibility::execute', 10 );
        add_filter( 'lcm_import', 'Lcm_URL_Import_Create::execute', 20 );
        add_filter( 'lcm_import', 'Lcm_URL_Import_Download::execute', 30 );
		add_filter( 'lcm_import', 'Lcm_Import_Validate::execute', 50 );
        add_filter( 'lcm_import', 'Lcm_Import_Validate_Crc::execute', 55 );
		add_filter( 'lcm_import', 'Lcm_Import_Check_Compression::execute', 70 );
		add_filter( 'lcm_import', 'Lcm_Import_Check_Encryption::execute', 75 );
		add_filter( 'lcm_import', 'Lcm_Import_Confirm::execute', 100 );
		add_filter( 'lcm_import', 'Lcm_Import_Blogs::execute', 150 );
		add_filter( 'lcm_import', 'Lcm_Import_Permalinks::execute', 170 );
		add_filter( 'lcm_import', 'Lcm_Import_Enumerate::execute', 200 );
		add_filter( 'lcm_import', 'Lcm_Import_Content::execute', 250 );
		add_filter( 'lcm_import', 'Lcm_Import_Mu_Plugins::execute', 270 );
		add_filter( 'lcm_import', 'Lcm_Import_Database_File::execute', 295 );
		add_filter( 'lcm_import', 'Lcm_Import_Database::execute', 300 );
		add_filter( 'lcm_import', 'Lcm_Import_Users::execute', 310 );
		add_filter( 'lcm_import', 'Lcm_Import_Options::execute', 330 );
		add_filter( 'lcm_import', 'Lcm_Import_Done::execute', 350 );
		add_filter( 'lcm_import', 'Lcm_Import_Clean::execute', 400 );
    }

    /**
     * The name of the plugin used to uniquely identify it within the context of
     * WordPress and to define internationalization functionality.
     *
     * @since     1.0.0
     * @return    string    The name of the plugin.
     */
    public function get_plugin_name()
    {
        return $this->plugin_name;
    }

    /**
     * The reference to the class that orchestrates the hooks with the plugin.
     *
     * @since     1.0.0
     * @return    Plugin_Name_Loader    Orchestrates the hooks of the plugin.
     */
    public function get_loader()
    {
        return $this->loader;
    }

    /**
     * Retrieve the version number of the plugin.
     *
     * @since     1.0.0
     * @return    string    The version number of the plugin.
     */
    public function get_version()
    {
        return $this->version;
    }

}
