<?php
/**
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              https://www.leadconnector.com
 * @since             1.0.0
 * @package           LeadConnector WP Migrator
 *
 * Copyright 2025 Lead Connector
 *
 * @wordpress-plugin
 * Plugin Name:       LeadConnector WP Migrator
 * Description:       This plugin helps you to easily migrate your data from one site to another.
 * Version:           1.1.5
 * Author:            LeadConnector
 * Author URI:        https://www.leadconnectorhq.com
 * License:           GPL-3.0+
 * License URI:       http://www.gnu.org/licenses/gpl-3.0.txt
 * Text Domain:       leadconnector-wp-migrator
 * Domain Path:       /languages
 */

// Autoload dependencies
require_once __DIR__ . '/vendor/autoload.php';

use Lcmigrator\LcMigrator\Loader\LcMigratorApp;
use Lcmigrator\LcMigrator\Logger\Logger;
use Lcmigrator\LcMigrator\CliRegister\Register;

/**
 * Define plugin constants
 * Start at version 1.0.0 and use SemVer - https://semver.org
 */
if (file_exists(__DIR__ . '/config.php')) {
    require_once __DIR__ . '/config.php';
} else {
    // Core plugin settings
    define('LC_MIGRATOR_VERSION', '1.0.0');
    define('LC_MIGRATOR_PLUGIN_NAME', 'LC Easy Migrator');
    define('LC_MIGRATOR_OPTION_NAME', 'lc_migrator_options');

    // Logger configuration
    define('LC_MIGRATOR_ENABLE_LOGGING', true);
    define('LC_MIGRATOR_ENABLE_WP_DEBUG', true);
    define('LOGGER_LOG_INFO_LOGS', true);

    // API configuration
    define('LC_MIGRATOR_OAUTH_CLIENT_ID', '67a47f6009f1ed914c264efe-m6t4qp22');
    define('LC_MIGRATOR_SERVICES_BASE_URL', 'https://staging.services.leadconnectorhq.com/');
    define('LEAD_CONNECTOR_BASE_URL', 'https://rest.leadconnectorhq.com/');
}

// Path configurations
define('LC_MIGRATOR_LOG_PATH', dirname(__DIR__) . '/logs/lc_migrator.log');
define('LC_MIGRATOR_PLUGIN_BASENAME', basename(dirname(__FILE__)) . '/' . basename(__FILE__));
define('LC_MIGRATOR_PATH', dirname(__FILE__));

if (!defined('LC_SECURE_ACCESS_PUBLIC_KEY_PATH')) {
    define('LC_SECURE_ACCESS_PUBLIC_KEY_PATH', LC_MIGRATOR_PATH . '/src/SecureAccess/keys/secure_access.pub');
}

if (!defined('LC_QA_MODE')) {
    define('LC_QA_MODE', false);
}

define('LC_MIGRATOR_STORAGE_PATH', LC_MIGRATOR_PATH . DIRECTORY_SEPARATOR . 'backup-files');

// WordPress content directory configuration
if (!defined('WP_CONTENT_DIR')) {
    define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
}

// Backup path configuration
if (!defined('LC_MIGRATOR_DEFAULT_BACKUPS_PATH')) {
    define('LC_MIGRATOR_DEFAULT_BACKUPS_PATH', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'lc-migrator-backups');
}

define('LC_MIGRATOR_BACKUPS_PATH', get_option(LC_MIGRATOR_BACKUPS_PATH_OPTION, LC_MIGRATOR_DEFAULT_BACKUPS_PATH));

// Plugin base directory configuration
if (defined('LC_MIGRATOR_PLUGIN_BASENAME')) {
    define('LC_MIGRATOR_PLUGIN_BASEDIR', dirname(LC_MIGRATOR_PLUGIN_BASENAME));
} else {
    define('LC_MIGRATOR_PLUGIN_BASEDIR', 'lc-migrator');
}

define('LC_MIGRATOR_SOURCE_PATH', LC_MIGRATOR_PATH . DIRECTORY_SEPARATOR . 'src');
define('LC_MIGRATOR_BACKUPS_HTACCESS', LC_MIGRATOR_BACKUPS_PATH . DIRECTORY_SEPARATOR . '.htaccess');
define('LC_MIGRATOR_STORAGE_HTACCESS', LC_MIGRATOR_STORAGE_PATH . DIRECTORY_SEPARATOR . '.htaccess');
define('LCM_MIGRATOR_GCLOUD_STORAGE_FILE_CHUNK_SIZE', 1024 * 1024 * 10); // 1MB for faster uploads
define('LC_MIGRATOR_THEMES_FUNCTIONS_NAME', 'themes' . DIRECTORY_SEPARATOR . 'functions.php');
define('LC_MIGRATOR_ELEMENTOR_CSS_NAME', 'uploads' . DIRECTORY_SEPARATOR . 'elementor' . DIRECTORY_SEPARATOR . 'css');
define('LC_MIGRATOR_CIVICRM_UPLOADS_NAME', 'uploads' . DIRECTORY_SEPARATOR . 'civicrm');
define('LC_MIGRATOR_LESS_CACHE_EXTENSION', '.less.cache');
define('LC_MIGRATOR_SQLITE_DATABASE_EXTENSION', '.sqlite');
require_once LC_MIGRATOR_PATH . DIRECTORY_SEPARATOR . 'loader' . DIRECTORY_SEPARATOR . 'loader.php';

/**
 * Initialize static logger on plugins_loaded
 */
add_action('plugins_loaded', function () {
    $logger = Logger::getInstance();
});


// Login Code Filter Fix Here 
/**
 * Add custom query variables to WordPress
 */
function lead_connector_migrator_add_custom_query_vars($vars)
{
    $vars[] = 'code';
    $vars[] = 'lc_code';
    // Add any additional custom parameters here
    return $vars;
}
add_filter('query_vars', 'lead_connector_migrator_add_custom_query_vars');

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-lc-activator.php
 */
function activate_lead_connector_migrator()
{
    // Register custom query vars and flush rewrite rules
    lead_connector_migrator_add_custom_query_vars(array());
    flush_rewrite_rules();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-lc-deactivator.php
 */
function deactivate_lead_connector_migrator()
{
    flush_rewrite_rules();
}

register_activation_hook(__FILE__, 'activate_lead_connector_migrator');
register_deactivation_hook(__FILE__, 'deactivate_lead_connector_migrator');

// Login Code Filter Fix End Here 

/**
 * Register WP CLI commands if WP CLI is available
 */
if (defined('WP_CLI') && WP_CLI) {
    Register::register();
}

// Initialize and run the plugin
$leadMigrator = new LcMigratorApp();
$leadMigrator->run();
