<?php

/**
 * Plugin Name: Macymed Link Tracker
 * Description: Enregistre le nombre d'affichages de vos pages.
 * Author: Macymed
 * Plugin URI:        https://extensions.macymed.fr
 * Version:           1.2.0.0
 * Requires at least: 6.6.2
 * Requires PHP:      7.4
 * Author:            Macymed
 * Author URI:        https://macymed.fr
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       macymed-link-tracker
 * Domain Path:       /languages
 *
 * @package MacymedLinkTracker
 */

namespace Macymed\MacymedLinkTracker;

if (! function_exists('sanitize_exclusion')) {
    function sanitize_exclusion($input)
    {

        if (! is_array($input)) {
            $input = explode(PHP_EOL, $input);
        }

        $input = array_map(
            function ($item) {
                return trim(str_replace("\r", '', $item));
            },
            $input
        );

        return array_filter($input);
    }
}

define('MACYMED_LINK_TRACKER_VERSION', '1.0.0');
define('MACYMED_LINK_TRACKER_FILE', __FILE__);

// Support for site-level autoloading.
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}

if (! defined('ABSPATH')) {
    exit; // Sécurité
}


if (!defined('MACYMED_LINK_TRACKER_TABLE_NAME')) {
    define('MACYMED_LINK_TRACKER_TABLE_NAME', 'macymed_link_tracker');
}
if (!defined('MACYMED_LINK_TRACKER_CLICKS_TABLE_NAME')) {
    define('MACYMED_LINK_TRACKER_CLICKS_TABLE_NAME', 'macymed_link_tracker_history');
}
if (!defined('MACYMED_LINK_TRACKER_PAGE_VIEWS_TABLE_NAME')) {
    define('MACYMED_LINK_TRACKER_PAGE_VIEWS_TABLE_NAME', 'macymed_link_tracker_page_views');
}
if (!defined('MACYMED_LINK_TRACKER_AI_CACHE_TABLE_NAME')) {
    define('MACYMED_LINK_TRACKER_AI_CACHE_TABLE_NAME', 'macymed_link_tracker_ai_cache');
}


use Macymed\MacymedLinkTracker\Plugin\RegisterPlugin;
use Macymed\MacymedLinkTracker\Utils\Database;
use Macymed\MacymedLinkTracker\Utils\Utils;

class MacymedLinkTracker
{
    /**
     * Holds the class instance.
     *
     * @var MacymedLinkTracker $instance
     */
    private static $instance = null;

    /**
     * Return an instance of the class
     *
     * Return an instance of the MacymedLinkTracker Class.
     *
     * @since 1.0.0
     *
     * @return MacymedLinkTracker class instance.
     */
    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Class initializer.
     */
    public function plugins_loaded()
    {
        load_plugin_textdomain(
            'macymed-link-tracker',
            false,
            basename(__DIR__) . '/languages'
        );

        if (file_exists(__DIR__ . '/updater.php')) {
            require_once __DIR__ . '/updater.php';
            new MacymedLinkTrackerUpdater();
        }
        // Run plugin init code here.
        $plugin = RegisterPlugin::get_instance();
        $plugin->init();

        // Run plugin setup code here.

        add_action('init', [ $this, 'init' ]);
    }

    /**
     * Init all the things.
     */
    public function init()
    {

        add_rewrite_rule('^redirect_external/?$', 'index.php?macymed_tracker=1', 'top');
        add_rewrite_rule('^link_tracker_record/?$', 'index.php?macymed_tracker_record=1', 'top');
        
    }
}






add_action(
    'plugins_loaded',
    function () {
        $macymedExternalLink = MacymedLinkTracker::get_instance();
        $macymedExternalLink->plugins_loaded();
    }
);
// Fonction appelée à l'activation du plugin


// Hook pour enregistrer la fonction d'activation
register_activation_hook(
    __FILE__,
    function () {
        $default_settings = [
            // Exclusions
            'macymed_tracker_exclusion_user_agents'        => [
                'Lighthouse',
                'GTmetrix',
                'Google',
                'Pingdom',
                'bot',
                'spider',
                'PTST',
                'HeadlessChrome',
            ],
            'macymed_tracker_tracking_list_personal_data_to_track' => [
                // 'REMOTE_ADDR',
                'HTTP_USER_AGENT',
            ],
            'macymed_tracker_tracking_list_to_track' => [
                'internal',
                'external',
                'inbound'
            ],
            'macymed_tracker_exclusion_ips'                => [ '0.0.0.0', '127.0.0.1' ],
            'macymed_tracker_exclusion_get'                => [],
            'macymed_tracker_exclusion_protocols'          => [ 'tel', 'mailto', 'callto' ],
            'macymed_tracker_exclusion_domains'            => [ Utils::get_current_domain() ],
            'macymed_tracker_exclusion_domains_relative_path' => '1',
            'macymed_tracker_exclusion_internal_pages'     => '0',

            // Inclusions
            'macymed_tracker_inclusions'                   => [],

            // Page de redirection
            'macymed_tracker_redirect_page_return_button_text' => 'Retour',
            'macymed_tracker_redirect_page_display_return_button' => '1',
            'macymed_tracker_redirect_page_direct_button_text' => 'Redirection immédiate',
            'macymed_tracker_redirect_page_display_direct_button' => '1',
            'macymed_tracker_redirect_page_title'          => 'Redirection en cours...',
            'macymed_tracker_redirect_page_display_title'  => '1',
            'macymed_tracker_redirect_page_message'        => 'Vous allez être redirigé vers le site {{redirect_url}} dans {{delay}} secondes...',
            'macymed_tracker_redirect_page_delay'          => 5,
            'macymed_tracker_redirect_page_display_message' => '1',

            // Tracking
            'macymed_tracker_tracking_record_404'          => '1',
            'macymed_tracker_tracking_record_excluded_ips' => '0',
            'macymed_tracker_tracking_refresh'             => '0',
            'macymed_tracker_tracking_record_ip'           => '1',
            'macymed_tracker_tracking_record_user_agent'   => '1',
            'macymed_tracker_tracking_use_external_redirect_page' => '0',
            'macymed_tracker_tracking_use_external_javascript_page' => '1',
            
        ];

        foreach ($default_settings as $key => $value) {
            if (! get_option($key)) {
                add_option($key, $value);
            }
            // update_option($key, $value);
        }

        // Database::init();
    }
);
