<?php
/**
 * Plugin Name: Ansico Diagnosekoder
 * Plugin URI: https://ansico.dk/Ansico/Ansico-diagnosekoder
 * Description: Gutenberg-blokke og backend-import til søgning i ICD-10 diagnosekoder fra en uploadet tekstfil.
 * Version: 1.0.0
 * Author: Andreas Andersen (Ansico)
 * Author URI: https://ansico.dk
 * License: GPL-3.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain: ansico-diagnosekoder
 */

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

define('ANSICO_DIAGNOSEKODER_VERSION', '1.0.0');
define('ANSICO_DIAGNOSEKODER_PAGE_LINKS_META', 'ansico_diagnosekoder_page_links');
define('ANSICO_DIAGNOSEKODER_PAGE_FAVORITES_META', 'ansico_diagnosekoder_page_favorites');
define('ANSICO_DIAGNOSEKODER_OPTION_RESULTS_URL', 'ansico_diagnosekoder_results_url');
define('ANSICO_DIAGNOSEKODER_PLUGIN_FILE', __FILE__);
define('ANSICO_DIAGNOSEKODER_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('ANSICO_DIAGNOSEKODER_PLUGIN_URL', plugin_dir_url(__FILE__));
define('ANSICO_DIAGNOSEKODER_OPTION_DATA', 'ansico_diagnosekoder_data');
define('ANSICO_DIAGNOSEKODER_OPTION_META', 'ansico_diagnosekoder_meta');

require_once ANSICO_DIAGNOSEKODER_PLUGIN_DIR . 'includes/class-ansico-diagnosekoder-parser.php';
require_once ANSICO_DIAGNOSEKODER_PLUGIN_DIR . 'includes/class-ansico-diagnosekoder-admin.php';
require_once ANSICO_DIAGNOSEKODER_PLUGIN_DIR . 'includes/class-ansico-diagnosekoder-rest.php';
require_once ANSICO_DIAGNOSEKODER_PLUGIN_DIR . 'includes/class-ansico-diagnosekoder-block.php';

Ansico_Diagnosekoder_Admin::init();
Ansico_Diagnosekoder_REST::init();
Ansico_Diagnosekoder_Block::init();

add_action('template_redirect', function () {
    $results_url = (string) get_option(ANSICO_DIAGNOSEKODER_OPTION_RESULTS_URL, '');
    $results_page_id = $results_url !== '' ? absint(url_to_postid($results_url)) : 0;
    $current_page_id = function_exists('get_queried_object_id') ? absint(get_queried_object_id()) : 0;

    if (($results_page_id && $current_page_id === $results_page_id) || isset($_GET['q'])) {
        nocache_headers();
    }
});

register_activation_hook(__FILE__, function () {
    if (false === get_option(ANSICO_DIAGNOSEKODER_OPTION_DATA, false)) {
        add_option(ANSICO_DIAGNOSEKODER_OPTION_DATA, []);
    }

    if (false === get_option(ANSICO_DIAGNOSEKODER_OPTION_META, false)) {
        add_option(ANSICO_DIAGNOSEKODER_OPTION_META, []);
    }
});
