<?php
/**
 * Classic Editor & Widgets Module
 *
 * Restores the classic post editor and/or the classic widgets screen.
 *
 * @package BlaminhorEssentials
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Classic Editor & Widgets module class
 */
class Blaminhor_Essentials_Module_Classic_Editor extends Blaminhor_Essentials_Module {

    /**
     * Module ID
     *
     * @var string
     */
    protected $id = 'classic-editor';

    /**
     * Module name
     *
     * @var string
     */
    protected $name = 'Classic Editor & Widgets';

    /**
     * Initialize module
     */
    protected function init() {
        $this->name        = __( 'Classic Editor & Widgets', 'blaminhor-essentials' );
        $this->description = __( 'Restores the classic post editor and/or the classic widgets screen.', 'blaminhor-essentials' );
    }

    /**
     * Get default settings
     *
     * @return array
     */
    protected function get_default_settings() {
        return array(
            'enable_classic_editor'     => true,
            'enable_classic_widgets'    => true,
            'remove_block_styles'       => true,
            'remove_admin_block_styles' => false,
        );
    }

    /**
     * Register hooks
     */
    protected function register_hooks() {
        // Classic Editor hooks.
        if ( $this->get_setting( 'enable_classic_editor', true ) ) {
            // Disable Gutenberg for posts.
            add_filter( 'use_block_editor_for_post', '__return_false', 10 );

            // Disable Gutenberg for post types.
            add_filter( 'use_block_editor_for_post_type', '__return_false', 10 );

            // Remove the "Try Gutenberg" dashboard widget.
            add_action( 'wp_dashboard_setup', array( $this, 'remove_gutenberg_dashboard_widget' ) );

            // Remove block styles from frontend if enabled.
            if ( $this->get_setting( 'remove_block_styles', true ) ) {
                add_action( 'wp_enqueue_scripts', array( $this, 'remove_block_styles' ), 100 );
            }

            // Remove block styles from admin if enabled.
            if ( $this->get_setting( 'remove_admin_block_styles', false ) ) {
                add_action( 'admin_enqueue_scripts', array( $this, 'remove_admin_block_styles' ), 100 );
            }
        }

        // Classic Widgets hooks.
        if ( $this->get_setting( 'enable_classic_widgets', true ) ) {
            add_filter( 'use_widgets_block_editor', '__return_false' );
            add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );
        }
    }

    /**
     * Remove "Try Gutenberg" dashboard widget
     */
    public function remove_gutenberg_dashboard_widget() {
        remove_meta_box( 'try_gutenberg_panel', 'dashboard', 'normal' );
    }

    /**
     * Remove block library styles from frontend
     */
    public function remove_block_styles() {
        wp_dequeue_style( 'wp-block-library' );
        wp_dequeue_style( 'wp-block-library-theme' );
        wp_dequeue_style( 'wc-blocks-style' );
        wp_dequeue_style( 'global-styles' );
    }

    /**
     * Remove block library styles from admin
     */
    public function remove_admin_block_styles() {
        wp_dequeue_style( 'wp-block-library' );
        wp_dequeue_style( 'wp-block-library-theme' );
    }

    /**
     * Render settings page
     */
    public function render_settings_page() {
        $enable_editor       = $this->get_setting( 'enable_classic_editor', true );
        $enable_widgets      = $this->get_setting( 'enable_classic_widgets', true );
        $remove_styles       = $this->get_setting( 'remove_block_styles', true );
        $remove_admin_styles = $this->get_setting( 'remove_admin_block_styles', false );
        ?>
        <div class="wrap blaminhor-essentials-wrap">
            <div class="blaminhor-essentials-header">
                <h1><?php esc_html_e( 'Classic Editor & Widgets', 'blaminhor-essentials' ); ?></h1>
                <p class="blaminhor-essentials-tagline"><?php esc_html_e( 'Restores the classic post editor and/or the classic widgets screen.', 'blaminhor-essentials' ); ?></p>
            </div>

            <div class="blaminhor-essentials-card">
                <form method="post">
                    <?php $this->render_nonce_field(); ?>

                    <h3><?php esc_html_e( 'Classic Editor', 'blaminhor-essentials' ); ?></h3>

                    <?php if ( $enable_editor ) : ?>
                        <div class="ap-status-banner ap-status-success">
                            <span class="dashicons dashicons-yes-alt"></span>
                            <span><?php esc_html_e( 'Classic Editor is active.', 'blaminhor-essentials' ); ?></span>
                        </div>
                    <?php else : ?>
                        <div class="ap-status-banner ap-status-warning">
                            <span class="dashicons dashicons-info"></span>
                            <span><?php esc_html_e( 'Classic Editor is disabled.', 'blaminhor-essentials' ); ?></span>
                        </div>
                    <?php endif; ?>

                    <table class="form-table">
                        <tr>
                            <th scope="row"><?php esc_html_e( 'Enable Classic Editor', 'blaminhor-essentials' ); ?></th>
                            <td>
                                <label>
                                    <input type="checkbox" name="enable_classic_editor" value="1" <?php checked( $enable_editor ); ?>>
                                    <?php esc_html_e( 'Disables the Block Editor (Gutenberg) and restores the traditional TinyMCE editor.', 'blaminhor-essentials' ); ?>
                                </label>
                            </td>
                        </tr>
                    </table>

                    <h3><?php esc_html_e( 'Performance', 'blaminhor-essentials' ); ?></h3>

                    <table class="form-table">
                        <tr>
                            <th scope="row"><?php esc_html_e( 'Frontend Styles', 'blaminhor-essentials' ); ?></th>
                            <td>
                                <label>
                                    <input type="checkbox" name="remove_block_styles" value="1" <?php checked( $remove_styles ); ?>>
                                    <?php esc_html_e( 'Remove Block Library CSS from frontend', 'blaminhor-essentials' ); ?>
                                </label>
                                <p class="description"><?php esc_html_e( 'Removes unused Gutenberg CSS files from your site\'s frontend for better performance.', 'blaminhor-essentials' ); ?></p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row"><?php esc_html_e( 'Admin Styles', 'blaminhor-essentials' ); ?></th>
                            <td>
                                <label>
                                    <input type="checkbox" name="remove_admin_block_styles" value="1" <?php checked( $remove_admin_styles ); ?>>
                                    <?php esc_html_e( 'Remove Block Library CSS from admin area', 'blaminhor-essentials' ); ?>
                                </label>
                                <p class="description"><?php esc_html_e( 'May cause minor styling issues in some admin pages. Enable only if needed.', 'blaminhor-essentials' ); ?></p>
                            </td>
                        </tr>
                    </table>

                    <hr>

                    <h3><?php esc_html_e( 'Classic Widgets', 'blaminhor-essentials' ); ?></h3>

                    <?php if ( $enable_widgets ) : ?>
                        <div class="ap-status-banner ap-status-success">
                            <span class="dashicons dashicons-yes-alt"></span>
                            <span><?php esc_html_e( 'Classic Widgets are active.', 'blaminhor-essentials' ); ?></span>
                        </div>
                    <?php else : ?>
                        <div class="ap-status-banner ap-status-warning">
                            <span class="dashicons dashicons-info"></span>
                            <span><?php esc_html_e( 'Classic Widgets are disabled.', 'blaminhor-essentials' ); ?></span>
                        </div>
                    <?php endif; ?>

                    <table class="form-table">
                        <tr>
                            <th scope="row"><?php esc_html_e( 'Enable Classic Widgets', 'blaminhor-essentials' ); ?></th>
                            <td>
                                <label>
                                    <input type="checkbox" name="enable_classic_widgets" value="1" <?php checked( $enable_widgets ); ?>>
                                    <?php esc_html_e( 'Disables the block-based Widgets editor and restores the classic Widgets screen.', 'blaminhor-essentials' ); ?>
                                </label>
                            </td>
                        </tr>
                    </table>

                    <p class="submit">
                        <button type="submit" class="button button-primary"><?php esc_html_e( 'Save Settings', 'blaminhor-essentials' ); ?></button>
                    </p>
                </form>
            </div>
        </div>
        <?php
    }

    /**
     * Process settings
     *
     * @param array $data Form data.
     */
    protected function process_settings( $data ) {
        $settings = array(
            'enable_classic_editor'     => isset( $data['enable_classic_editor'] ),
            'enable_classic_widgets'    => isset( $data['enable_classic_widgets'] ),
            'remove_block_styles'       => isset( $data['remove_block_styles'] ),
            'remove_admin_block_styles' => isset( $data['remove_admin_block_styles'] ),
        );

        $this->save_settings( $settings );

        add_action( 'admin_notices', function () {
            blaminhor_essentials_admin_notice( __( 'Settings saved.', 'blaminhor-essentials' ), 'success' );
        } );
    }
}
