<?php
/**
* Plugin Name: Agency Ecommerce Platform
* Description: Multi-tenant e-commerce platform for agencies – Built on FluentCart
* Version: 1.0.0
* Author: Your Agency
*/

namespace AgencyEcommercePlatform;

defined(‘ABSPATH’) || exit;

final class AgencyEcommercePlatform
{
private static $instance = null;
private $services = [];

public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}

private function __construct()
{
$this->defineConstants();
$this->checkDependencies();
$this->init();
}

private function defineConstants()
{
define(‘AGENCY_ECOMMERCE_PLATFORM_VERSION’, ‘1.0.0’);
define(‘AGENCY_ECOMMERCE_PLATFORM_URL’, plugin_dir_url(__FILE__));
define(‘AGENCY_ECOMMERCE_PLATFORM_PATH’, plugin_dir_path(__FILE__));
define(‘AGENCY_ECOMMERCE_PLATFORM_PLUGIN_FILE’, __FILE__);
}

private function checkDependencies()
{
add_action(‘admin_init’, function() {
if (!defined(‘FLUENTCARTPATH’)) {
deactivate_plugins(plugin_basename(__FILE__));
add_action(‘admin_notices’, function() {
echo ‘<div class=”error”><p>Agency Ecommerce Platform requires FluentCart to be installed and activated.</p></div>’;
});
}
});
}

private function init()
{
// Register autoloader
spl_autoload_register([$this, ‘autoload’]);

// Register activation hook
register_activation_hook(__FILE__, [$this, ‘activate’]);

// Initialize services
add_action(‘plugins_loaded’, [$this, ‘boot’], 20);
}

public function autoload($class)
{
$prefix = ‘AgencyEcommercePlatform\\’;
$base_dir = __DIR__ . ‘/app/’;

$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}

$relative_class = substr($class, $len);
$file = $base_dir . str_replace(‘\\’, ‘/’, $relative_class) . ‘.php’;

if (file_exists($file)) {
require $file;
}
}

public function activate()
{
// Create merchant role
$this->createMerchantRole();

// Run migrations
$this->runMigrations();

// Set default options
update_option(‘agency_ecommerce_platform_version’, AGENCY_ECOMMERCE_PLATFORM_VERSION);
update_option(‘agency_ecommerce_platform_db_version’, ‘1.0.0’);
}

private function createMerchantRole()
{
add_role(‘merchant’, ‘Merchant’, [
‘read’ => true,
‘edit_posts’ => true,
‘delete_posts’ => false,
‘publish_posts’ => false,
‘upload_files’ => true,
]);
}

private function runMigrations()
{
$migrations = [
new Database\Migrations\CreateStoresTable(),
new Database\Migrations\CreateStoreDomainsTable(),
new Database\Migrations\CreateAgenciesTable()
];

foreach ($migrations as $migration) {
if (method_exists($migration, ‘up’)) {
$migration->up();
}
}
}

public function boot()
{
if (!defined(‘FLUENTCARTPATH’)) {
return;
}

// Initialize hooks
$this->initHooks();

// Initialize admin interface
$this->initAdmin();

// Initialize API routes
add_action(‘rest_api_init’, [$this, ‘initRoutes’]);
}

private function initHooks()
{
// Register admin menu
add_action(‘admin_menu’, [$this, ‘addAdminMenu’]);

// Register merchant onboarding AJAX
add_action(‘wp_ajax_create_merchant_store’, [$this, ‘handleCreateMerchantStore’]);
}

public function addAdminMenu()
{
add_menu_page(
‘Agency Platform’,
‘Agency Platform’,
‘manage_options’,
‘agency-platform’,
[$this, ‘renderAgencyDashboard’],
‘dashicons-store’,
30
);

add_submenu_page(
‘agency-platform’,
‘Stores’,
‘Stores’,
‘manage_options’,
‘agency-stores’,
[$this, ‘renderStoresPage’]
);

add_submenu_page(
‘agency-platform’,
‘Onboarding’,
‘Onboarding’,
‘manage_options’,
‘agency-onboarding’,
[$this, ‘renderOnboardingPage’]
);
}

public function renderAgencyDashboard()
{
echo ‘<div class=”wrap”><h1>Agency Dashboard</h1>’;
echo ‘<div id=”agency-platform-app”>Loading Agency Platform…</div>’;
echo ‘</div>’;
}

public function renderStoresPage()
{
echo ‘<div class=”wrap”><h1>Manage Stores</h1>’;
echo ‘<div id=”agency-stores-app”>Loading Stores…</div>’;
echo ‘</div>’;
}

public function renderOnboardingPage()
{
echo ‘<div class=”wrap”><h1>Merchant Onboarding</h1>’;
echo ‘<div id=”agency-onboarding-app”>Loading Onboarding Form…</div>’;
echo ‘</div>’;
}

public function handleCreateMerchantStore()
{
// Check nonce and permissions
check_ajax_referer(‘agency_platform_nonce’, ‘nonce’);

if (!current_user_can(‘manage_options’)) {
wp_die(‘Unauthorized’);
}

$agency_id = intval($_POST[‘agency_id’]);
$data = [
‘merchant_email’ => sanitize_email($_POST[‘merchant_email’]),
‘merchant_name’ => sanitize_text_field($_POST[‘merchant_name’]),
‘store_name’ => sanitize_text_field($_POST[‘store_name’]),
‘plan_type’ => sanitize_text_field($_POST[‘plan_type’] ?? ‘basic’)
];

$onboardingService = new App\Services\MerchantOnboardingService(
new App\Services\StoreService(),
new App\Services\AgencyService()
);

$result = $onboardingService->createMerchantStore($agency_id, $data);

wp_send_json($result);
}

public function initRoutes()
{
// REST API routes will be added here
}
}

// Initialize the plugin
AgencyEcommercePlatform::getInstance();