<?php
defined('ABSPATH') || exit;

/**
 * Add Gutenberg support for custom post types
 */


function ornex_gutenberg_support()
    {


    // Add support for custom post types
    $post_types = array(
        'actualites',
        'agenda',
        'faq',
        'flashinfos',
        'galeries',
        'offres-emploi',
        'publications',
        'annuaire',
        'trombinoscope',
        'elus'
    );
    // Force Gutenberg editor for all post types
    add_filter('use_block_editor_for_post_type', function ($can_edit, $post_type) use ($post_types) {
        return in_array($post_type, $post_types) ? true : $can_edit;
        }, 100, 2);

    // Remove Classic Editor if installed
    // add_filter('classic_editor_enabled', function () {
    //     return false;
    //     });


    foreach ($post_types as $post_type) {
        // var_dump($post_type);
        if (post_type_exists($post_type)) {
            // Force Gutenberg editor support

            remove_post_type_support($post_type, 'custom-fields'); // Remove first to prevent conflicts

            // Add theme support for Gutenberg features
            add_post_type_support($post_type, array(
                'editor',             // Enables the Gutenberg editor
                'title',              // Enable title field
                'excerpt',            // Enable excerpt field
                'thumbnail',          // Enable featured image
                'align-wide',         // Enable wide and full alignments
                'custom-spacing',     // Enables custom spacing controls
                'custom-units',       // Enables custom units in spacing controls
                'editor-styles',      // Enable editor styles
                'responsive-embeds',  // Enable responsive embedded content
                'custom-line-height', // Enables custom line height
                'wp-block-styles',    // Enable block styles
                'custom-fields'       // Enable custom fields if needed
            ));

            // Register support for core block features
            add_theme_support('core-block-patterns');
            add_theme_support('wp-block-styles');
            add_theme_support('align-wide');

            // Ensure editor styles are loaded
            add_theme_support('editor-styles');

            // Force enable block editor
            add_filter("use_block_editor_for_post_type_{$post_type}", '__return_true', 100);
            }
        }
    }

// Run early to ensure it overrides other configurations
add_action('init', 'ornex_gutenberg_support', 9);


/**
 * Add Gutenberg editor styles
 */
function ornex_gutenberg_styles()
    {
    // Add support for block styles
    add_theme_support('wp-block-styles');
    // Add support for responsive embeds
    add_theme_support('responsive-embeds');

    // Add support for full and wide align images
    add_theme_support('align-wide');

    // Add editor styles
    add_theme_support('editor-styles');

    // Add custom editor style
    add_editor_style('resources/css/editor-style.css');
    }
add_action('after_setup_theme', 'ornex_gutenberg_styles');

/**
 * Add custom block categories
 */
function ornex_block_categories($categories, $post)
    {
    return array_merge(
        $categories,
        array(
            array(
                'slug' => 'ornex',
                'title' => __('Ornex', 'ornex'),
                'icon' => 'admin-site',
            ),
        )
    );
    }
add_filter('block_categories_all', 'ornex_block_categories', 10, 2);

/**
 * Add custom block patterns
 */
function ornex_register_block_patterns()
    {
    if (function_exists('register_block_pattern_category')) {
        register_block_pattern_category(
            'ornex',
            array('label' => __('Ornex', 'ornex'))
        );
        }
    }
add_action('init', 'ornex_register_block_patterns');

// Ensure Gutenberg block styles are loaded
add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style('wp-block-library');
    wp_enqueue_style('wp-block-library-theme');
    });
