Automattic / blocks-everywhere

Gutenberg block editor in WordPress comments, admin pages, bbPress, and BuddyPress.
GNU General Public License v3.0
77 stars 17 forks source link

Gutenberg for Term Description #178

Open goaround opened 1 year ago

goaround commented 1 year ago

As suggested here https://github.com/Automattic/blocks-everywhere/issues/20 I tried to add support for the term description.

It works, but it is still wip because I need your help:

  1. (Done) As the term description is only in the backend, I would propose supporting all blocks, not just a limited set.
  2. I think it would be helpful if Gutenberg uses the full page with. Currently, the edit form limited to max-width: 800px. I would propose to override it with 100%.
  3. (Works now) In the past, I used https://github.com/sheabunge/visual-term-description-editor to have at least the classic editor for the term description. But if there is an old description, it breaks because it has to be converted to blocks first. This is disabled, but I don't know how to enable it again for the term description.
1ucay commented 4 months ago

With image upload and all blocks

<?php

namespace Automattic\Blocks_Everywhere\Handler;
use WP_Block_Type;
use WP_Block_Type_Registry;

class Terms extends Handler {
    /**
     * Constructor
     */

    private $enabled_blocks;

    public function __construct() {
        parent::__construct();

        /* Only users with the "publish_posts" capability can use this feature */
        if ( current_user_can( 'publish_posts' ) ) {

            /* Remove the filters which disallow HTML in term descriptions */
            remove_filter( 'pre_term_description', 'wp_filter_kses' );
            remove_filter( 'term_description', 'wp_kses_data' );

            /* Add filters to disallow unsafe HTML tags */
            if ( ! current_user_can( 'unfiltered_html' ) ) {
                add_filter( 'pre_term_description', 'wp_kses_post' );
                add_filter( 'term_description', 'wp_kses_post' );
            }

            add_action( 'current_screen', function() {

                $this->enabled_blocks = array_values( array_map( function( $block ) {
                    return $block->name;
                }, WP_Block_Type_Registry::get_instance()->get_all_registered() ) );

                $this->enabled_blocks[] = 'blocks-everywhere/support-content';

                /* Loop through the taxonomies, adding actions */
                $taxonomies = get_taxonomies( [
                    'show_ui' => true,
                    'public' => true,
                ] );
                foreach ( $taxonomies as $taxonomy ) {
                    add_action( $taxonomy . '_edit_form_fields', [ $this, 'add_to_terms_edit' ], 1, 2 );
                    add_action( $taxonomy . '_add_form_fields', [ $this, 'add_to_terms_add' ], 1, 1 );
                }
            } );

        }

        add_filter( 'pre_term_description', [ $this, 'remove_blocks' ] );

        // Ensure blocks are processed when displaying
        add_filter(
            'term_description',
            function( $content ) {
                return $this->do_blocks( $content, 'term_description' );
            },
            8
        );

        add_filter( 'blocks_everywhere_editor_settings', [ $this, 'settings' ], 1, 1 );
        add_filter( 'blocks_everywhere_allowed_blocks', [ $this, 'allowed_blocks' ], 1, 1 );

        add_filter( 'body_class', [ $this, 'body_class' ] );

    }

    public function body_class( $classes ) {
        $classes[] = 'gutenberg-support';

        $can_upload = false;
        if ( isset( $this->settings['editor']['hasUploadPermissions'] ) && $this->settings['editor']['hasUploadPermissions'] ) {
            $can_upload = true;
        }

        if ( $can_upload ) {
            $classes[] = 'gutenberg-support-upload';
        }

        return $classes;
    }

    public function can_show_admin_editor( $hook ) {
        return false; //$hook === 'term.php';
    }

    public function get_editor_type() {
        return 'core';
    }

    public function allowed_blocks( $blocks ) {
        return $this->enabled_blocks;
    }

    public function settings( $settings ) {

        $settings['iso']['moreMenu'] = array(
            'editor' => true,
            'fullscreen' => true,
            'preview' => true,
            'topToolbar' => true
        );

        $settings['iso']['sidebar'] = array(
            'inserter'  => false,
            'inspector' => false
        );

        $settings['editor']['hasUploadPermissions'] = true;
        $settings['editor']['reusableBlocks'] = true;

        return $settings;
    }

    /**
     * Get the HTML that the editor uses on the page
     *
     * @return void
     */
    public function add_to_terms_edit() {
        $this->load_editor( '#description' );
    }

    /**
     * Get the HTML that the editor uses on the page
     *
     * @return void
     */
    public function add_to_terms_add() {
        $this->load_editor( '#tag-description' );
    }

    public function wp_editor_settings( $settings ) {
        $settings['tinymce'] = false;
        $settings['quicktags'] = true;
        return $settings;
    }
}
goaround commented 4 months ago

@1ucay nice work! I updated my PR. Now it works fine and I think it could be merged @johngodley

isuke01 commented 3 weeks ago

OH This would be cool