<?php

include_once('includes/rewrites.php');
include_once('includes/ip.php');
include_once('includes/navwalker.php');
/* ***************************
** WORDPRESS AJAX REQUESTS **
*************************** */
include_once('includes/ajax_requests.php');

/* ***************************
** WORDPRESS ADMIN **
*************************** */
include_once('includes/custom_admin.php');

/* ***************************
** ACF BLOCKS **
*************************** */
include_once('includes/blocks.php');

include_once('includes/countries.php');
/* ***************************
** STORE LOCATOR **
*************************** */
//include_once('includes/stores.php');
include_once('includes/store_locator.php');

/* ***************************
** STRUCTURED DATAS **
*************************** */
include_once('includes/structured_datas.php');

/* ***************************
** CUSTOM POST TYPES **
*************************** */
include_once('includes/custom-post-types.php');

/* ***************************
** HELPERS **
*************************** */
include_once('includes/helpers.php');
include_once('includes/setup.php');
// Localized author archives removed

/* ***************************
** Inclure les fonctions de breadcrumb
*************************** */
require_once get_template_directory() . '/includes/breadcrumb.php';




/**
 * Cette fonction est appelée à chaque requête HTTP et permet de rediriger l'utilisateur vers la version de la langue par défaut du site si l'utilisateur n'est pas sur la version par défaut.
 *
 * @return void
 */

add_action('template_redirect', function () {
 	$uri = $_SERVER['REQUEST_URI'] ?? '/';
    if ($uri !== '/' && $uri !== '') {
        return;
    }

    if (function_exists('icl_get_languages')) {
        global $sitepress;
        $default_lang = $sitepress->get_default_language();
        $langs = $sitepress->get_active_languages();

        $browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '', 0, 2);
        $lang = isset($langs[$browser_lang]) ? $browser_lang : $default_lang;

        // Si pas déjà sur /fr/ ou /en/, rediriger
        if (!preg_match("#^/{$lang}(/|$)#", $_SERVER['REQUEST_URI'])) {
            wp_redirect(site_url("/{$lang}/"), 302);
            exit;
        }
    }
});