<?php
/**
 * Plugin Name: List2Play Servers
 * Plugin URI: https://list2play.net
 * Description: Display List2Play game servers on your WordPress site with beautiful, customizable layouts. Select which servers to display via admin panel, use shortcode, widget, or Gutenberg block.
 * Version: 1.0.6
 * Author: List2Play
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: list2play-servers
 * Requires at least: 5.8
 * Requires PHP: 7.4
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

// Define plugin constants
define('LIST2PLAY_SERVERS_VERSION', '1.0.6');
define('LIST2PLAY_SERVERS_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('LIST2PLAY_SERVERS_PLUGIN_URL', plugin_dir_url(__FILE__));
define('LIST2PLAY_SERVERS_PLUGIN_BASENAME', plugin_basename(__FILE__));

// Include main files
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-activator.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-deactivator.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-api.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-shortcode.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-admin.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-renderer.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-styles.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-widget.php';
require_once LIST2PLAY_SERVERS_PLUGIN_DIR . 'includes/class-list2play-block.php';

// Register activation and deactivation hooks (must be before class instantiation)
register_activation_hook(__FILE__, ['List2Play_Servers_Activator', 'activate']);
register_deactivation_hook(__FILE__, ['List2Play_Servers_Deactivator', 'deactivate']);

/**
 * Migrate stored API base URL from list2play.com to list2play.net (existing installs).
 */
add_action('plugins_loaded', static function (): void {
    $url = (string) get_option('list2play_api_base_url', '');
    if ($url !== '' && strpos($url, 'list2play.com') !== false) {
        update_option('list2play_api_base_url', str_replace('list2play.com', 'list2play.net', $url));
    }
}, 0);

/**
 * Main plugin class
 */
class List2Play_Servers {
    
    protected $api;
    protected $shortcode;
    protected $admin;
    protected $renderer;
    protected $styles;
    
    public function __construct() {
        $this->api = new List2Play_Servers_API();
        $this->renderer = new List2Play_Servers_Renderer($this->api);
        $this->styles = new List2Play_Servers_Styles();
        $this->shortcode = new List2Play_Servers_Shortcode($this->api, $this->renderer);
        
        if (is_admin()) {
            $this->admin = new List2Play_Servers_Admin($this->api);
        }
    }
    
    public function run() {
        // Initialize hooks
        add_action('init', [$this, 'init']);
        add_action('wp_enqueue_scripts', [$this->styles, 'enqueue_styles']);
        add_action('widgets_init', [$this, 'register_widget']);
    }
    
    public function init() {
        // Text domain is automatically loaded by WordPress.org for hosted plugins
        // Only load manually if plugin is not hosted on WordPress.org
        // load_plugin_textdomain('list2play-servers', false, dirname(LIST2PLAY_SERVERS_PLUGIN_BASENAME) . '/languages');
        
        // Register Gutenberg block
        if (function_exists('register_block_type')) {
            $block = new List2Play_Servers_Block($this->api, $this->renderer);
            $block->register();
        }
    }
    
    public function register_widget() {
        register_widget('List2Play_Servers_Widget');
    }
}

// Initialize plugin
function list2play_servers() {
    static $instance = null;
    if (null === $instance) {
        $instance = new List2Play_Servers();
        $instance->run();
    }
    return $instance;
}

// Start the plugin
list2play_servers();

