<?php
/**
 * Plugin Name:       Acadlix Importer
 * Description:       An addon plugin for Acadlix to import data from wp-pro-quiz.
 * Version:           1.0.0
 * Requires at least: 6.8
 * Tested up to:      6.9
 * Requires PHP:      8.2
 * Author:            Team Acadlix
 * Author URI:        https://acadlix.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       acadlix-importer
 * Domain Path:       /languages
 */


use Yuvayana\AcadlixImporter\Admin\Menu;
use Yuvayana\AcadlixImporter\Helper\Helper;
use Yuvayana\AcadlixImporter\Migrations\Migration;
use Yuvayana\AcadlixImporter\REST\Api;

defined('ABSPATH') || exit();

if (!defined('ACADLIXIMPORTER_PLUGIN_FILE')) {
  define('ACADLIXIMPORTER_PLUGIN_FILE', __FILE__);
  $acadlix_importer_default_headers = [
    'name' => 'Plugin Name',
    'version' => 'Version',
  ];

  $acadlix_importer_plugin_data = get_file_data(ACADLIXIMPORTER_PLUGIN_FILE, $acadlix_importer_default_headers);
  define('ACADLIXIMPORTER_VERSION', $acadlix_importer_plugin_data['version']);
  define('ACADLIXIMPORTER_PLUGIN_BASENAME', plugin_basename(ACADLIXIMPORTER_PLUGIN_FILE));
  define('ACADLIXIMPORTER_SLUG', 'acadlix-importer');
  define('ACADLIXIMPORTER_PLUGIN_DIR', plugin_dir_path(ACADLIXIMPORTER_PLUGIN_FILE));
}

if (!version_compare(PHP_VERSION, '8.2', '>=')) {
  add_action('admin_notices', function () {
    ?>
    <div class="notice notice-warning">
      <p><?php
      echo sprintf(
        /* translators: 1: Required PHP version, 2: Current PHP version */
        esc_html__('Acadlix Importer requires PHP %1$s or higher. You are running PHP %2$s.', 'acadlix-importer'),
        '8.2',
        PHP_VERSION
      );
      ?></p>
    </div>
    <?php
  });
  return;
}

if (!class_exists('AcadlixImporter')) {
  class AcadlixImporter
  {
    private static $_instance = null;

    public function __construct()
    {
      require 'bootstrap.php';

      register_activation_hook(__FILE__, [$this, 'activate']);
      register_deactivation_hook(__FILE__, [$this, 'deactivate']);


      add_action('wp_initialize_site', [$this, 'initialize_new_site'], 10, 1);
      add_action('wp_delete_site', [$this, 'acadlix_delete_site'], 10, 1);

      $this->init_plugin();
      add_action('init', [$this, 'init_plugin']);
      add_action('plugins_loaded', [$this, 'acadlix_importer_plugin_check_dependency_on_admin_init']);
    }

    protected function check_required_plugin_active()
    {
      if (!function_exists('is_plugin_active')) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
      }
      $acadlix_active = function_exists('acadlix') || is_plugin_active('acadlix/acadlix.php') || is_plugin_active('acadlix-pro/acadlix.php');
      return $acadlix_active;
    }

    public function init_plugin()
    {
      if (!$this->check_required_plugin_active()) {
        return;
      }
      Menu::instance();
      Api::instance();
    }

    public function activate($network_wide)
    {
      if (is_multisite() && $network_wide) {
        // Loop through all existing sites
        foreach (get_sites(['fields' => 'ids']) as $site_id) {
          switch_to_blog($site_id);
          $this->run_site_activation();
          restore_current_blog();
        }
      } else {
        $this->run_site_activation();
      }
    }

    public function initialize_new_site($site)
    {
      include_once ABSPATH . 'wp-admin/includes/plugin.php';
      if (!is_plugin_active_for_network(ACADLIX_PLUGIN_BASENAME)) {
        return;
      }

      switch_to_blog($site->blog_id);

      $this->run_site_activation();

      restore_current_blog();
    }

    private function run_site_activation()
    {
      if ($this->check_required_plugin_active()) {
        Migration::createTable();
      }

    }

    public function deactivate()
    {
    }


    public function acadlix_import_plugin_check_notice()
    {
      ?>
      <div class="notice notice-error is-dismissible">
        <p>
          <?php
          echo sprintf(
            /* translators: 1: Plugin name (Acadlix Importer), 2: Required plugin name (Acadlix Plugin) */
            esc_html__('%1$s requires the %2$s to be installed and active. Please activate it before using this plugin.', 'acadlix-importer'),
            '<strong>' . esc_html__('Acadlix Importer', 'acadlix-importer') . '</strong>',
            '<strong>' . esc_html__('Acadlix Plugin', 'acadlix-importer') . '</strong>'
          );
          ?>
        </p>
      </div>
      <?php
    }

    public function acadlix_importer_plugin_check_dependency_on_admin_init()
    {

      if (!$this->check_required_plugin_active()) {
        if (!function_exists('deactivate_plugins')) {
          require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        deactivate_plugins(ACADLIXIMPORTER_PLUGIN_BASENAME);
        add_action('admin_notices', [$this, 'acadlix_import_plugin_check_notice']);
        add_action('network_admin_notices', [$this, 'acadlix_import_plugin_check_notice']);
      }
    }

    public function uninstall()
    {
      if (is_multisite()) {
        foreach (get_sites(['fields' => 'ids']) as $site_id) {
          switch_to_blog($site_id);
          $this->run_site_uninstall();
          restore_current_blog();
        }
      } else {
        $this->run_site_uninstall();
      }
    }

    public function acadlix_delete_site($site)
    {
      $blog_id = (int) $site->id;
      switch_to_blog($blog_id);
      $this->run_site_uninstall();
      restore_current_blog();
    }

    private function run_site_uninstall()
    {
      Migration::removeTable();
      $options = Helper::instance()->acadlix_importer_options();
      if (count($options) > 0) {
        foreach ($options as $key => $value) {
          delete_option($key);
        }
      }
    }

    public static function instance()
    {
      if (is_null(self::$_instance)) {
        self::$_instance = new self();
      }

      return self::$_instance;
    }

  }
}

function acadlix_importer()
{
  return AcadlixImporter::instance();
}

acadlix_importer();
