Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Verbum_Gutenberg_Editor
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
2
 remove_strict_kses_filters
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_assets
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Verbum Gutenberg Editor
4 *
5 * @package automattic/jetpack-mu-plugins
6 */
7
8declare( strict_types = 1 );
9
10require_once __DIR__ . '/class-verbum-block-utils.php';
11require_once __DIR__ . '/class-verbum-asset-loader.php';
12
13/**
14 * Verbum_Gutenberg_Editor is responsible for loading the Gutenberg editor for comments.
15 *
16 * This loads the isolated editor, and sets up the editor to be used for Verbum_Comments.
17 *
18 * @see https://github.com/Automattic/isolated-block-editor
19 *
20 * @phan-constructor-used-for-side-effects
21 */
22class Verbum_Gutenberg_Editor {
23    /**
24     * Comment forms can appear anywhere (page, post, query loop, etc), there is no reliable way to determine if there are comments on the page,
25     * So we hook into `comment_form_before` and set this flag to true when a comment form is found.
26     *
27     * @var bool
28     */
29    public $should_enqueue_assets = false;
30
31    /**
32     * Class constructor
33     */
34    public function __construct() {
35        define( 'VERBUM_USING_GUTENBERG', true );
36
37        // Override the placeholder text
38        add_filter(
39            'write_your_story',
40            function () {
41                return __( 'Write a comment...', 'jetpack-mu-wpcom' );
42            },
43            9999
44        );
45
46        add_action(
47            'comment_form_before',
48            function () {
49                $this->should_enqueue_assets = true;
50            }
51        );
52
53        add_filter( 'init', array( $this, 'remove_strict_kses_filters' ) );
54        add_filter( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
55        add_filter( 'comment_text', array( \Verbum_Block_Utils::class, 'render_verbum_blocks' ) );
56        add_filter( 'pre_comment_content', array( \Verbum_Block_Utils::class, 'remove_blocks' ) );
57    }
58
59    /**
60     * Default KSES filters on wpcom only allow HTML for admins and people who can post "posts" to the blog they're commenting on.
61     * See: wp-includes/kses.php (this one adds the restrictions).
62     * See: wp-content/mu-plugins/misc.php (this one removes it, but only has_cap('publish_posts')).
63     */
64    public function remove_strict_kses_filters() {
65        // Allow HTML when blocks are enabled.
66        remove_filter( 'pre_comment_content', 'wp_filter_kses' );
67        add_filter( 'pre_comment_content', 'wp_filter_post_kses' );
68    }
69
70    /**
71     * Enqueue the assets for the Gutenberg editor
72     *
73     * In case the page is singular and has comment closed or front page with comments closed we avoid the enqueueing
74     */
75    public function enqueue_assets() {
76        if ( ! \Verbum_Block_Utils::should_show_verbum_comments() && ! $this->should_enqueue_assets ) {
77            return;
78        }
79
80        \Verbum_Asset_Loader::load_editor_supporting_assets(); // Editor itself is loaded dynamically
81    }
82}