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

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


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

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


/* ********************
** FIX ROOT PAGE - WPML BUG **
// ******************** */
// function wpml_root_page()
// {
//     global $wp_query, $sitepress;
//     // Safety checks
//     if (!isset($sitepress) || !isset($sitepress->ROOT_URL_PAGE_ID)) {
//         return;
//     }
//     $queriedId = (int) $wp_query->get_queried_object_id();
//     $rootId    = (int) $sitepress->ROOT_URL_PAGE_ID;

//     // Only act on the actual front page/root page
//     if ($queriedId > 0 && $queriedId === $rootId && function_exists('is_front_page') && is_front_page()) {
//         define('IS_ROOT_PAGE', true);
//         $lang_file = get_template_directory() . '/includes/lang.php';
//         if (file_exists($lang_file)) {
//             include $lang_file;
//             // Preserve original behavior when file exists
//             die();
//         }
//         // If the file is missing, do not terminate rendering; fallback to normal flow
//         return;
//     }
// }
// add_action('wp', 'wpml_root_page');

/* **************
** 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.7', '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.2');
}
add_action('admin_enqueue_scripts', 'my_admin_load_scripts');


/* **********************
** 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 EXCERPT **
****************** */
function custom_excerpt_length($length)
{
    return 30;
}
add_filter('excerpt_length', 'custom_excerpt_length', 999);



/* 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_twitter:data1', '__return_false');
add_filter('wpseo_meta_twitter:label1', '__return_false');

add_action('after_setup_theme', function () {
    add_theme_support('title-tag');
});


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');




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_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 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);


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;
});
