<?php
/**
 * Plugin Name: Social Publisher by Calliope – Auto Post to 10+ Networks
 * Description: Auto-post WordPress content to 10+ social networks with AI-assisted copy, image tools, history, analytics, and sanitized logs.
 * Version: 2.0.1
 * Author: CalliopeWP
 * Author URI: https://pluginswordpress.calliope.com.ar/
 * Requires at least: 6.4
 * Requires PHP: 8.1
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: calliope-social-publisher
 * Domain Path: /languages
 */

if (!defined('ABSPATH')) {
    exit;
}

// Define plugin constants.
define('CALLIOPE_SP_PATH', plugin_dir_path(__FILE__));
define('CALLIOPE_SP_URL', plugin_dir_url(__FILE__));
define('CALLIOPE_SP_VERSION', '2.0.1');
if (!defined('CALLIOPE_SP_SCHEDULER_BRIDGE_VERSION')) { define('CALLIOPE_SP_SCHEDULER_BRIDGE_VERSION', 1); }

// Lightweight class autoloader.
// Load classes only when they are actually instantiated or referenced.
if (!function_exists('calliope_social_publisher_autoload')) {
    function calliope_social_publisher_autoload($class) {
        if (strpos($class, 'Calliope_SP_') !== 0) {
            return;
        }

        $relative = substr($class, strlen('Calliope_SP_'));
        if ($relative === '') {
            return;
        }

        $slug = strtolower(str_replace('_', '-', $relative));
        $file = CALLIOPE_SP_PATH . 'classes/class-' . $slug . '.php';

        if (is_readable($file)) {
            require_once $file;
        }
    }
}

spl_autoload_register('calliope_social_publisher_autoload');

// The extension bridge must register early; connection-state and provider classes autoload on demand.
require_once CALLIOPE_SP_PATH . 'classes/class-extensions.php';
Calliope_SP_Extensions::init();

// Persistent publication history is autoloaded only when storage is actually needed.
// Activation and admin upgrades keep the schema current without loading Analytics on normal front-end requests.
if (!function_exists('calliope_social_publisher_activate')) {
    function calliope_social_publisher_activate($network_wide = false) {
        Calliope_SP_History::activate((bool) $network_wide);
        Calliope_SP_Short_Link_Schema::activate((bool) $network_wide);
        Calliope_SP_Short_Link_Service::activate_rewrite((bool) $network_wide);
    }
}
register_activation_hook(__FILE__, 'calliope_social_publisher_activate');

if (!function_exists('calliope_social_publisher_maybe_upgrade_storage')) {
    function calliope_social_publisher_maybe_upgrade_storage() {
        if (!current_user_can('manage_options')) {
            return;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only routing check used only to defer schema work until the plugin screen is opened.
        $page = isset($_GET['page']) ? sanitize_key(wp_unslash((string) $_GET['page'])) : '';
        if ($page !== 'calliope-social-publisher') {
            return;
        }

        Calliope_SP_History::maybe_upgrade();
        Calliope_SP_Short_Link_Schema::maybe_upgrade();
        Calliope_SP_Short_Link_Service::maybe_flush_rewrite_rules();
    }
}
add_action('admin_init', 'calliope_social_publisher_maybe_upgrade_storage', 1);
if (!function_exists('calliope_social_publisher_initialize_new_site')) {
    function calliope_social_publisher_initialize_new_site($new_site) {
        Calliope_SP_History::initialize_new_site($new_site);
        Calliope_SP_Short_Link_Schema::initialize_new_site($new_site);
    }
}
add_action('wp_initialize_site', 'calliope_social_publisher_initialize_new_site', 20, 1);

// Public same-domain short-link route. No external shortening service is used.
add_action('init', ['Calliope_SP_Short_Link_Service', 'register_rewrite'], 5);
add_action('init', ['Calliope_SP_Short_Link_Service', 'maybe_flush_rewrite_rules'], 99);
add_filter('query_vars', ['Calliope_SP_Short_Link_Service', 'register_query_var']);
add_action('template_redirect', ['Calliope_SP_Short_Link_Service', 'handle_redirect'], 1);

// Load the main controller eagerly. The rest will be autoloaded on demand.
require_once CALLIOPE_SP_PATH . 'classes/class-master.php';

/**
 * Initialize the plugin.
 */
function calliope_social_publisher_init() {
    new Calliope_SP_Master();
}
add_action('plugins_loaded', 'calliope_social_publisher_init', 20);
