<?php

use MDirectorNewsletter\includes\Mdirector_Newsletter;
use MDirectorNewsletter\includes\Mdirector_Newsletter_Activator;
use MDirectorNewsletter\includes\Mdirector_Newsletter_Deactivator;
use MDirectorNewsletter\includes\Mdirector_Newsletter_Utils;

/**
 *
 * @link              https://mdirector.com
 * @since             1.0.0
 * @package           MDirector_Newsletter
 *
 * @wordpress-plugin
 * Plugin Name:       emBlue Newsletter
 * Plugin URI:        https://help.embluemail.com/integraciones/nuevo-plugin-de-wordpress-para-email-marketing-en-emblue/
 * Description:       Official emBlue plugin for WordPress. Add emBlue sign-up forms to your WordPress site and automate your newsletters.
 * Version:           1.1.0
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * Author:            emBlue
 * Author URI:        https://www.embluemail.com/
 * License:           GPL-2.0
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       emblue-newsletter
 * Domain Path:       /languages
 */

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

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

global $wp_version;

define('MDIRECTOR_MIN_WP_VERSION', '4.0.0');
define('MDIRECTOR_NEWSLETTER', 'mdirector-newsletter');
define('MDIRECTOR_NEWSLETTER_VERSION', '1.1.0');
define('MDIRECTOR_NEWSLETTER_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('MDIRECTOR_TEMPLATES_PATH', plugin_dir_path(__FILE__) . 'templates/');
define('MDIRECTOR_LOGS_PATH', plugin_dir_path(__FILE__) . 'log/');
define('MDIRECTOR_NEWSLETTER_PLUGIN_URL', plugins_url('/', __FILE__));
define('MDIRECTOR_NEWSLETTER_PLUGIN_FILE', __FILE__);
define('MDIRECTOR_CURRENT_WP_VERSION', $wp_version);

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require MDIRECTOR_NEWSLETTER_PLUGIN_DIR . 'includes/class-mdirector-newsletter.php';

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-mdirector-newsletter-activator.php
 */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- WP activation hook naming convention.
function activate_mdirector_newsletter()
{
    require_once MDIRECTOR_NEWSLETTER_PLUGIN_DIR . 'includes/class-mdirector-newsletter-activator.php';
    Mdirector_Newsletter_Activator::activate();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-mdirector-newsletter-deactivator.php
 */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- WP deactivation hook naming convention.
function deactivate_mdirector_newsletter()
{
    require_once MDIRECTOR_NEWSLETTER_PLUGIN_DIR . 'includes/class-mdirector-newsletter-deactivator.php';
    Mdirector_Newsletter_Deactivator::deactivate();
}

register_activation_hook(__FILE__, 'activate_mdirector_newsletter');
register_deactivation_hook(__FILE__, 'deactivate_mdirector_newsletter');

/**
 * Fallback de traducción para variantes de español (es_CO, es_AR, es_CL, etc.).
 * Si no existe un .mo para el locale exacto, redirige a es_ES automáticamente.
 */
add_filter('load_textdomain_mofile', function ($mofile, $domain) {
    if ($domain === 'emblue-newsletter' && !file_exists($mofile)) {
        $locale = determine_locale();
        if (strpos($locale, 'es_') === 0 && $locale !== 'es_ES') {
            $mofile = str_replace($locale, 'es_ES', $mofile);
        }
    }
    return $mofile;
}, 10, 2);

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- hooked to admin_notices, name follows plugin convention.
function mdirector_admin_notice()
{
    require_once MDIRECTOR_NEWSLETTER_PLUGIN_DIR . 'includes/class-mdirector-newsletter-utils.php';

    if (!get_option('mdirector-notice')) {
        echo '<div class="updated" style="padding: 10px;margin: 20px 0 0 2px;"><p>';
        echo esc_html__( 'PLUGIN-INSTALLED', 'emblue-newsletter' );
        echo ' <a href="admin.php?page=mdirector-newsletter&tab=welcome&mdirector_notice_ignore=0">';
        echo esc_html__( 'PLUGIN-CONFIGURE', 'emblue-newsletter' );
        echo '</a>';
        echo '</p></div>';
    }
}


// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- hooked to admin_init, name follows plugin convention.
function mdirector_notice_ignore()
{
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only GET param for dismissing a notice, no data mutation risk.
    if (isset($_GET['mdirector_notice_ignore']) && '0' === $_GET['mdirector_notice_ignore'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        update_option('mdirector-notice', 'true', true);
    }
}

add_action('admin_notices', 'mdirector_admin_notice');
add_action('admin_init', 'mdirector_notice_ignore');

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @throws \Twig\Error\LoaderError
 * @throws \Twig\Error\RuntimeError
 * @throws \Twig\Error\SyntaxError
 * @since    1.0.0
 */
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- bootstrap entry point, name follows plugin convention.
function run_mdirector_newsletter()
{
    $plugin = new Mdirector_Newsletter();
    $plugin->run();
}

run_mdirector_newsletter();
