<?php

// Suppression des mises à jour auto
// define('AUTOMATIC_UPDATER_DISABLED', true);

/**
 * PAGINATION ACTUALITES URL REWRITING
 */


function lp_pagination_rewrite_rule()
{
    add_rewrite_rule('(magazine|magazin|revista)/page/?([0-9]{1,})/?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
}
add_action('init', 'lp_pagination_rewrite_rule', 10, 0);

function lp_post_rewrite_rule()
{
    add_rewrite_rule('(magazine|magazin|revista)/([^/]+)/([^/]+)/?$', 'index.php?name=$matches[3]', 'top');
}
add_action('init', 'lp_post_rewrite_rule', 10, 0);

function lp_pagination_add_query_vars($query_vars)
{
    $query_vars[] = 'paged';
    return $query_vars;
}
add_filter('query_vars', 'lp_pagination_add_query_vars');


function lp_permalink_prefix($url, $post)
{
    // Vérifier si le post est de type article
    if ($post->post_type == 'post') {
        // Récupérer la catégorie principale du post
        $category = get_the_category($post->ID);
        if ($category) {
            $category_slug = $category[0]->slug;
            $url = get_category_link($category[0]->term_id) . $post->post_name . '/';
            // Ajouter le préfixe et la catégorie au permalien
            // $url = trailingslashit(home_url('/actualites/' . $category_slug . '/' . $post->post_name));
        }
    }
    return $url;
}
add_filter('post_link', 'lp_permalink_prefix', 10, 2);

function lp_redirect_post_permalink()
{
    // Vérifier si la requête concerne un article
    if (is_single()) {
        // Récupérer l'URL actuelle
        global $wp;
        $current_url = home_url(add_query_arg(array(), $wp->request));

        // Récupérer l'URL de l'article
        $post_permalink = get_permalink();

        // Comparer les URLs
        if ($current_url !== $post_permalink) {
            // Rediriger vers l'URL de l'article
            wp_redirect($post_permalink, 301);
            exit();
        }
    }
}
add_action('template_redirect', 'lp_redirect_post_permalink');


/* **************
** LANGUES **
************** */

load_theme_textdomain("laurentperrier", TEMPLATEPATH . '/languages');


/* ********************
** FIX ROOT PAGE - WPML BUG **
******************** */
function wpml_root_page()
{
    global $wp_query, $sitepress;
    $queriedId = $wp_query->get_queried_object_id();
    if ($queriedId > 0 && (isset($sitepress->ROOT_URL_PAGE_ID) && $queriedId == $sitepress->ROOT_URL_PAGE_ID)) {
        define('IS_ROOT_PAGE', true);
        include "lang.php";
        die();
    }
}
add_action('wp', 'wpml_root_page');


/* ********************
** GET COUNTRY VISITOR **
******************** */
function lpGetIPAddress()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // if ($ip == '::1') $ip = '';
    return $ip;
}

add_action('init', 'lp_set_country_ip_cookie');

function lp_set_country_ip_cookie()
{


    if (isset($_COOKIE['lp_country']) && !empty($_COOKIE['lp_country'])) {
        return $_COOKIE['lp_country'];
    }
    $ip = lpGetIPAddress();



    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.country.is/" . $ip);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $details = json_decode(curl_exec($ch));
        curl_close($ch);


        // $details = json_decode(file_get_contents("https://api.country.is/" . $ip));
        if (lpGetIPAddress() == '::1') {
            setcookie('lp_country', 'us', time() + 3600 * 2, '/');
            return 'us';
        } elseif (isset($details->country) && strlen($details->country) == 2) {
            $country = strtolower($details->country);
            if ($country == 'uk') $country = 'en';
            if ($country == 'gb') $country = 'en';
            setcookie('lp_country', $country, time() + 3600 * 2, '/');
            return $country;
        } else {

            return false;
        }
    } else {
        return false;
    }
}

function lp_get_country_ip()
{
    if (isset($_COOKIE['lp_country']) && !empty($_COOKIE['lp_country'])) {
        return $_COOKIE['lp_country'];
    } else {
        $ip = lpGetIPAddress();
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "https://api.country.is/" . $ip);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $details = json_decode(curl_exec($ch));
            curl_close($ch);
            // $details = json_decode(file_get_contents("https://api.country.is/".$ip));
            if (isset($details->country) && strlen($details->country) == 2) {
                $country = strtolower($details->country);
                if ($country == 'uk') $country = 'en';
                if ($country == 'gb') $country = 'en';
                return $country;
            } else {
                return false;
            }
        }
    }
    return false;
}


function is_display_buy_buttons()
{
    $my_current_lang = apply_filters('wpml_current_language', NULL);
    $country_ip = lp_get_country_ip();

//fr----en
    //echo ('<script>console.log("' . $country_ip . '----' . $my_current_lang . '")</script>');
    //if ($country_ip !== false && strtolower($country_ip) == strtolower($my_current_lang) && in_array(strtolower($my_current_lang), ['fr', 'en', 'uk', 'gb'])) {
    if ($country_ip !== false && in_array(strtolower($country_ip), ['fr', 'en', 'uk', 'gb'])) {
        return true;
    } else {
        //cas localhost
        if (lpGetIPAddress() == '::1') {
            return true;
        } else {
            return false;
        }
    }
}

/* **************
** JAVASCRIPTS **
************** */
function my_load_scripts($hook)
{
    wp_enqueue_script('scripts', get_template_directory_uri() . '/dist/app.js', array('jquery'), '1.1');
    wp_enqueue_script('gmap', get_template_directory_uri() . '/dist/storelocator.js', array('jquery'), '2.33');
    // wp_enqueue_script('modern-gmap', get_template_directory_uri() . '/dist/modern-storelocator_gpt-5.js', '0.1');

    wp_localize_script(
        'scripts',
        'lpscripts',
        array(
            'country' => lp_get_country_ip(),
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('lp-application-ajax-nonce'),
            'wpmlLang' => ICL_LANGUAGE_CODE,
        )
    );
    wp_register_style('styles', get_stylesheet_directory_uri() . '/dist/app.css', array(), '1.6', 'all');
    wp_enqueue_style('styles');
}
add_action('wp_enqueue_scripts', 'my_load_scripts');



function my_admin_load_scripts($hook)
{
    wp_register_style('admin-styles', get_stylesheet_directory_uri() . '/dist/admin.css');
    wp_enqueue_style('admin-styles');

    wp_enqueue_script('admin-scripts', get_template_directory_uri() . '/dist/app.js', array('jquery'), '1.1');
}
add_action('admin_enqueue_scripts', 'my_admin_load_scripts');


@ini_set('upload_max_size', '256M');
@ini_set('post_max_size', '256M');
@ini_set('max_execution_time', '800');


/* **********************
** WORDPRESS THUMBNAIL **
********************** */

add_theme_support('post-thumbnails');
add_image_size('home', 1400, 760, true);
add_image_size('page', 650, 0, false);
add_image_size('aside', 80, 50, true);
add_image_size('small', 300, 300, false);
add_image_size('lg', 1000, 10000, false);

if (!get_option("large_crop"))
    add_option("large_crop", "1");
else
    update_option("large_crop", "1");


/* ********************
** WORDPRESS SIDEBAR **
******************** */
// if (function_exists('register_sidebar')) {
//     register_sidebar(array(
//         'id' => 'sidebar-1',
//         'name' => 'sidebar',
//         'before_widget' => '<div class="bloc">',
//         'after_widget' => '</div>',
//         'before_title' => '<h3>',
//         'after_title' => '</h3>'
//     ));
// }


/* *****************
** WORDPRESS EXCERPT **
****************** */
function custom_excerpt_length($length)
{
    return 30;
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);


/* *****************
** WORDPRESS MENU **
****************** */
if (function_exists('register_nav_menus')) {
    register_nav_menus(
        array(
            'menu' => 'Primary menu',
            'footer' => 'Footer menu'
        )
    );
}
// remove container wp_nav
function my_wp_nav_menu_args($args = '')
{
    $args['container'] = false;
    return $args;
}
add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args');

// add wrapper for submenu
class WPSE_78121_Sublevel_Walker extends Walker_Nav_Menu
{
    function start_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<div class='sub-menu-wrap'><ul class='sub-menu'>\n";
    }
    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul></div>\n";
    }
}


add_filter('wp_nav_menu_args', 'my_wp_nav_menu_args');
// add wrapper for submenu
class WPSE_new_header_Walker extends Walker_Nav_Menu
{
    // function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
    // {
    //     global $wp_query;
    //     $indent = ($depth > 0 ? str_repeat("\t", $depth) : ''); // code indent

    //     // Depth-dependent classes.
    //     $depth_classes = array(
    //         ($depth == 0 ? 'main-menu-item' : 'sub-menu-item'),
    //         ($depth >= 2 ? 'sub-sub-menu-item' : ''),
    //         ($depth % 2 ? 'menu-item-odd' : 'menu-item-even'),
    //         'menu-item-depth-' . $depth
    //     );
    //     $depth_class_names = esc_attr(implode(' ', $depth_classes));

    //     // Passed classes.
    //     $classes = empty($item->classes) ? array() : (array) $item->classes;
    //     $class_names = esc_attr(implode(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item)));

    //     // Build HTML.
    //     $output .= $indent . '<li id="nav-menu-item-' . $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';

    //     // Link attributes.
    //     $attributes  = !empty($item->attr_title) ? ' title="'  . esc_attr($item->attr_title) . '"' : '';
    //     $attributes .= !empty($item->target)     ? ' target="' . esc_attr($item->target) . '"' : '';
    //     $attributes .= !empty($item->xfn)        ? ' rel="'    . esc_attr($item->xfn) . '"' : '';
    //     $attributes .= !empty($item->url)        ? ' href="'   . esc_attr($item->url) . '"' : '';
    //     $attributes .= 'data-collapse="collapse-' . $item->ID . '"';
    //     $attributes .= $depth == 0 ? 'data-toggle="collapse"' : '';
    //     $attributes .= '"class="menu-link ' . ($depth > 0 ? 'sub-menu-link' : 'main-menu-link') . '"';

    //     // Build HTML output and pass through the proper filter.
    //     $item_output = sprintf(
    //         '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
    //         $args->before,
    //         $attributes,
    //         $args->link_before,
    //         apply_filters('the_title', $item->title, $item->ID),
    //         $args->link_after,
    //         $args->after
    //     );
    //     $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    // }

    function start_lvl(&$output, $depth = 0, $args = array())
    {

        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<div class='sub-menu-wrap'><ul class='sub-menu'>\n";
    }

    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul></div>\n";
    }
}



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

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

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

/* ***************************
** STORE LOCATOR **
*************************** */
include_once('includes/countries.php');
//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');



/* ********************
** WORDPRESS GALLERY **
******************** */
add_filter('use_default_gallery_style', '__return_false');
// modify attr
remove_shortcode('gallery');
add_shortcode('gallery', 'my_own_gallery_shortcode');
function my_own_gallery_shortcode($attr)
{
    $attr['itemtag']      = 'div';
    $attr['icontag']      = 'span';
    $attr['captiontag']   = 'figcaption';
    $attr['size']         = 'square';
    $output = gallery_shortcode($attr);
    $output = preg_replace(array('/<br[^>]*>/', '/<span[^>]*>/', '/<\/span>/'), '', $output);
    return $output;
}



function getIDFromString($str)
{
    $id = strtolower(preg_replace('/\s+/', '-', $str));
    $unwanted_array = array(
        'Š' => 'S',
        'š' => 's',
        'Ž' => 'Z',
        'ž' => 'z',
        'À' => 'A',
        'Á' => 'A',
        'Â' => 'A',
        'Ã' => 'A',
        'Ä' => 'A',
        'Å' => 'A',
        'Æ' => 'A',
        'Ç' => 'C',
        'È' => 'E',
        'É' => 'E',
        'Ê' => 'E',
        'Ë' => 'E',
        'Ì' => 'I',
        'Í' => 'I',
        'Î' => 'I',
        'Ï' => 'I',
        'Ñ' => 'N',
        'Ò' => 'O',
        'Ó' => 'O',
        'Ô' => 'O',
        'Õ' => 'O',
        'Ö' => 'O',
        'Ø' => 'O',
        'Ù' => 'U',
        'Ú' => 'U',
        'Û' => 'U',
        'Ü' => 'U',
        'Ý' => 'Y',
        'Þ' => 'B',
        'ß' => 'Ss',
        'à' => 'a',
        'á' => 'a',
        'â' => 'a',
        'ã' => 'a',
        'ä' => 'a',
        'å' => 'a',
        'æ' => 'a',
        'ç' => 'c',
        'è' => 'e',
        'é' => 'e',
        'ê' => 'e',
        'ë' => 'e',
        'ì' => 'i',
        'í' => 'i',
        'î' => 'i',
        'ï' => 'i',
        'ð' => 'o',
        'ñ' => 'n',
        'ò' => 'o',
        'ó' => 'o',
        'ô' => 'o',
        'õ' => 'o',
        'ö' => 'o',
        'ø' => 'o',
        'ù' => 'u',
        'ú' => 'u',
        'û' => 'u',
        'ý' => 'y',
        'þ' => 'b',
        'ÿ' => 'y'
    );
    $id = strtr($id, $unwanted_array);
    $id = preg_replace('/[^a-zA-Z0-9-_\.]/', '', $id);
    return $id;
}

/**
 * Responsive Image Helper Function
 *
 * @param string $image_id the id of the image (from ACF or similar)
 * @param string $image_size the size of the thumbnail image or custom image size
 * @param string $max_width the max width this image will be shown to build the sizes attribute
 */

function acf_responsive_image($image_id, $image_size, $max_width, $srcset = true)
{

    // check the image ID is not blank
    if ($image_id != '') {
        // set the default src image size
        $image_src = wp_get_attachment_image_url($image_id, $image_size);
        // set the srcset with various image sizes
        if ($srcset) {
            $image_srcset = wp_get_attachment_image_srcset($image_id, $image_size);
            echo 'src="' . $image_src . '"  srcset="' . $image_srcset . '" sizes="(max-width: ' . $max_width . ') 100vw, ' . $max_width . '"';
        } else {
            echo 'src="' . $image_src . '"';
        }
        // generate the markup for the responsive image
    } else {
        echo 'src="pixel.gif"';
    }
}



/* Autoriser les fichiers SVG */
function wpc_mime_types($mimes)
{
    $mimes['svg'] = 'image/svg xml';
    return $mimes;
}
add_filter('upload_mimes', 'wpc_mime_types');



@ini_set('upload_max_size', '64M');
@ini_set('post_max_size', '64M');
@ini_set('max_execution_time', '300');


$my_current_lang = apply_filters('wpml_current_language', NULL);


add_filter('wpseo_meta_author', '__return_false');
add_filter('wpseo_meta_twitter:data1', '__return_false');
add_filter('wpseo_meta_twitter:label1', '__return_false');



function custom_tinymce_buttons($buttons)
{
    array_push($buttons, 'alignjustify'); // Ajoute le bouton "Justifier"
    array_push($buttons, 'underline'); // Ajoute le bouton "Underline"
    array_push($buttons, 'styleselect'); // Ajoute le bouton "Styles"
    // array_push($buttons, 'fontsizeselect'); // Ajoute le bouton "Taille de police"
    // array_push($buttons, 'fontselect'); // Ajoute le bouton "Police"


    return $buttons;
}
add_filter('mce_buttons', 'custom_tinymce_buttons');

// add_filter('tiny_mce_before_init', function ($init) {
//     $init['fontsize_formats'] = '14pt 16pt 18pt 20pt 22pt 24pt 28pt 32pt';
//     return $init;
// });
// add_filter('tiny_mce_before_init', function ($init) {
//     $init['font_formats'] = 'Raleway=Raleway, sans-serif;SackersGothic=SackersGothic, sans-serif;MrsEavesItalic=MrsEavesItalic, sans-serif;BodoniModa= \'Bodoni Moda\', serif;';
//     return $init;
// });


// function add_br_to_tinymce($init)
// {
//     $init['forced_root_block'] = false; // Disable automatic <p> wrapping
//     $init['force_br_newlines'] = true; // Add <br> on Enter
//     $init['force_p_newlines'] = false; // Disable <p> on Enter
//     $init['cleanup'] = false;
//     return $init;
// }
// add_filter('tiny_mce_before_init', 'add_br_to_tinymce');

// function my_acf_add_local_field_groups()
// {
//     remove_filter('acf_the_content', 'wpautop');
// }
// add_action('acf/init', 'my_acf_add_local_field_groups');



function add_tailwind_colors_to_editor_palette()
{
    add_theme_support('editor-color-palette', array(
        array(
            'name'  => 'Gray',
            'slug'  => 'gray',
            'color' => '#E9E9E9',
        ),
        array(
            'name'  => 'Gray Medium',
            'slug'  => 'gray-medium',
            'color' => '#D2D2D2',
        ),
        array(
            'name'  => 'Gray Light',
            'slug'  => 'gray-light',
            'color' => '#F1F1F1',
        ),
        array(
            'name'  => 'Neutral',
            'slug'  => 'neutral',
            'color' => '#7f7f7f',
        ),
        array(
            'name'  => 'Beige',
            'slug'  => 'beige',
            'color' => '#FFF8F5',
        ),
        array(
            'name'  => 'Orange',
            'slug'  => 'orange',
            'color' => '#EC6726',
        ),
        array(
            'name'  => 'Red',
            'slug'  => 'red',
            'color' => '#E64E19',
        ),
        array(
            'name'  => 'Yellow',
            'slug'  => 'yellow',
            'color' => '#FDC300',
        ),
        array(
            'name'  => 'Pink',
            'slug'  => 'pink',
            'color' => '#ffceb4',
        ),
        array(
            'name'  => 'Gold',
            'slug'  => 'gold',
            'color' => '#CAA165',
        ),
        array(
            'name'  => 'Green 400',
            'slug'  => 'green-400',
            'color' => '#65936F',
        ),
        array(
            'name'  => 'Green 600',
            'slug'  => 'green-600',
            'color' => '#004747',
        ),
    ));
}
add_action('after_setup_theme', 'add_tailwind_colors_to_editor_palette');

function add_tailwind_colors_to_acf_palette()
{
?>
    <script type="text/javascript">
        (function($) {
            acf.add_filter('color_picker_args', function(args, $field) {
                args.palettes = [
                    '#000000', // Black
                    '#FFFFFF', // White
                    '#E9E9E9', // Gray
                    '#D2D2D2', // Gray Medium
                    '#F1F1F1', // Gray Light
                    '#7f7f7f', // Neutral
                    '#FFF8F5', // Beige
                    '#EC6726', // Orange
                    '#E64E19', // Red
                    '#FDC300', // Yellow
                    '#ffceb4', // Pink
                    '#CAA165', // Gold
                    '#65936F', // Green 400
                    '#004747' // Green 600
                ];
                return args;
            });
        })(jQuery);
    </script>
<?php
}
add_action('acf/input/admin_footer', 'add_tailwind_colors_to_acf_palette');

// function add_acf_tinymce_custom_styles($toolbars)
// {
//     if (isset($toolbars['Full'])) {
//         $toolbars['Full'][2][] = 'styleselect';
//     }
//     return $toolbars;
// }
// add_filter('acf/fields/wysiwyg/toolbars', 'add_acf_tinymce_custom_styles');

function add_acf_tinymce_custom_style_formats($mceInit)
{
    $style_formats = array(
        array(
            'title' => 'Bannière',
            'block' => 'div',
            'classes' => 'wysiwyg_banniere',
            'wrapper' => true,
        ),
        array(
            'title' => 'titre de section',
            'block' => 'div',
            'classes' => 'wysiwyg_section-title',
            'wrapper' => true,
        ),
        array(
            'title' => 'sur-titre de section',
            'block' => 'div',
            'classes' => 'wysiwyg_section-uptitle',
            'wrapper' => true,
        ),
        array(
            'title' => 'sous-titre de section',
            'block' => 'div',
            'classes' => 'wysiwyg_section-subtitle',
            'wrapper' => true,
        ),
        array(
            'title' => 'intro de section',
            'block' => 'div',
            'classes' => 'wysiwyg_section-intro',
            'wrapper' => true,
        ),
        array(
            'title' => 'texte de section',
            'block' => 'p',
            'classes' => 'wysiwyg_section-text',
            'wrapper' => true,
        ),
    );
    $mceInit['style_formats'] = json_encode($style_formats);
    return $mceInit;
}
add_filter('tiny_mce_before_init', 'add_acf_tinymce_custom_style_formats');

// function rename_tinymce_formats($translation, $text, $domain)
// {
//     if ($text === 'Formats') {
//         return 'Style Laurent Perrier';
//     }
//     return $translation;
// }
// add_filter('gettext', 'rename_tinymce_formats', 10, 3);


function custom_tinymce_styles($settings)
{
    $settings['content_css'] = get_template_directory_uri() . '/dist/app.css'; // Charge le CSS du thème
    return $settings;
}
add_filter('tiny_mce_before_init', 'custom_tinymce_styles');

show_admin_bar(false);



// Renomme le menu "Posts" par "Magazine"
function custom_menu_labels($labels)
{
    $labels->name = __('Magazine', 'laurentperrier');
    $labels->singular_name = __('Article', 'laurentperrier');
    $labels->menu_name = __('Magazine', 'laurentperrier'); // Important pour le menu admin
    $labels->all_items = __('Tous les Articles', 'laurentperrier'); // Important pour le sous-menu
    $labels->add_new = __('Ajouter', 'laurentperrier');
    $labels->add_new_item = __('Ajouter un Article', 'laurentperrier');
    $labels->edit_item = __('Éditer l\'Article', 'laurentperrier');
    $labels->new_item = __('Nouvel Article', 'laurentperrier');
    $labels->view_item = __('Voir l\'Article', 'laurentperrier');
    $labels->view_items = __('Voir les Articles', 'laurentperrier');
    $labels->search_items = __('Rechercher un Article', 'laurentperrier');
    $labels->not_found = __('Aucun article trouvé', 'laurentperrier');
    $labels->not_found_in_trash = __('Aucun article trouvé dans la corbeille', 'laurentperrier');
    return $labels;
}
add_filter('post_type_labels_post', 'custom_menu_labels');

// Change l'icône du menu Magazine (Posts)
function change_magazine_menu_icon()
{
    global $menu;
    // Trouve le menu des posts et change son icône
    foreach ($menu as $key => $value) {
        if ($value[2] == 'edit.php') {
            $menu[$key][6] = 'dashicons-book-alt'; // Icône de magazine/livre
            break;
        }
    }
}
add_action('admin_menu', 'change_magazine_menu_icon');


add_filter('rest_url', function ($url) {
    return preg_replace('/^http:/i', 'https:', $url);
});

// add_filter('acf/settings/save_json', function ($path) {
//     return get_stylesheet_directory() . '/acf-json';
// });
// add_filter('acf/settings/load_json', function ($paths) {
//     unset($paths[0]);
//     $paths[] = get_stylesheet_directory() . '/acf-json';
//     return $paths;
// });

// add_action('acf/init', function () {
//     if (function_exists('acf_get_field_groups')) {
//         $groups = acf_get_field_groups();

//         foreach ($groups as $group) {
//             $local = acf_get_local_field_group($group['key']);
//             $db = acf_get_field_group($group['ID']);

//             // Si le JSON est plus récent → mettre à jour
//             if ($local && $db && $local['modified'] > $db['modified']) {
//                 acf_import_field_group($local);
//             }
//         }
//     }
// });
