<?php
class WP_CKEditor5 {
    private static $instance = null;
    
    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
        add_action('admin_footer', [$this, 'initialize_ckeditor5']);
    }
    
    public function enqueue_scripts() {
        wp_enqueue_script('ckeditor5', 'https://cdn.ckeditor.com/ckeditor5/41.2.1/super-build/ckeditor.js', [], null, true);
    }
    
    public function initialize_ckeditor5() {
        ?>
        <script>
        document.addEventListener("DOMContentLoaded", function () {
            if (document.querySelector("textarea#editor")) {
                ClassicEditor.create(document.querySelector("textarea#editor"), {
                    extraPlugins: [Comments],
                    comments: {
                        enabled: true,
                    }
                }).catch(error => console.error(error));
            }
        });
        </script>
        <?php
    }
}

WP_CKEditor5::get_instance();
