<?php

/**
 * Plugin Name:       Lockee
 * Plugin URI:        https://wordpress.lockee.fr/
 * Description:       Add Lockee locks to your WordPress site to gamify your content and engage your visitors in a completely autonomous way.
 * Version:           3.1.8
 * Author:            Nicolas Desmarets
 * Author URI:        https://ndev.fr/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       lockee
 * Domain Path:       /languages
 */

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

define('LOCKEE_VERSION', '3.1.8');

require_once plugin_dir_path(__FILE__) . 'inc/class-lockee-singleton.php';
require_once plugin_dir_path(__FILE__) . 'inc/class-lockee-core.php';
require_once plugin_dir_path(__FILE__) . 'inc/class-lockee-activator.php';
require_once plugin_dir_path(__FILE__) . 'inc/class-lockee-deactivator.php';
require_once plugin_dir_path(__FILE__) . 'inc/class-lockee-update.php';

function lockee_init()
{
  load_plugin_textdomain('lockee', false, dirname(plugin_basename(__FILE__)) . '/languages/');
  Lockee_Update::get_instance();
  return Lockee_Core::get_instance();
}

register_activation_hook(__FILE__, array('Lockee_Activator', 'activate'));
register_deactivation_hook(__FILE__, array('Lockee_Deactivator', 'deactivate'));

// Ajout du hook pour la mise à jour
add_action('upgrader_process_complete', function ($upgrader_object, $options) {
  if ($options['action'] === 'update' && $options['type'] === 'plugin') {
    foreach ($options['plugins'] as $plugin) {
      if ($plugin === plugin_basename(__FILE__)) {
        require_once plugin_dir_path(__FILE__) . 'inc/class-lockee-post-types.php';
        Lockee_Post_Types::maybe_migrate_lock_to_lockee_lock();
        Lockee_Post_Types::update_rewrite_rules_static();
        break;
      }
    }
  }
}, 10, 2);

// Empêcher le renommage des fichiers de traduction
add_filter('load_script_translation_file', function ($file, $handle, $domain) {
  if ($domain === 'lockee') {
    // Récupérer le chemin du fichier de traduction original
    $locale = determine_locale();
    $original_file = plugin_dir_path(__FILE__) . 'locales/' . $locale . '.json';

    if (file_exists($original_file)) {
      return $original_file;
    }
  }
  return $file;
}, 10, 3);

add_action('plugins_loaded', 'lockee_init');
