7
7
8
8
declare ( strict_types = 1 );
9
9
10
+ require_once __DIR__ . '/class-verbum-block-utils.php ' ;
11
+
10
12
/**
11
13
* Verbum_Gutenberg_Editor is responsible for loading the Gutenberg editor for comments.
12
14
*
@@ -29,10 +31,11 @@ function () {
29
31
},
30
32
9999
31
33
);
34
+
32
35
add_filter ( 'init ' , array ( $ this , 'remove_strict_kses_filters ' ) );
33
- add_filter ( 'comment_text ' , array ( $ this , 'render_verbum_blocks ' ) );
34
- add_filter ( 'pre_comment_content ' , array ( $ this , 'remove_blocks ' ) );
35
36
add_filter ( 'wp_enqueue_scripts ' , array ( $ this , 'enqueue_assets ' ) );
37
+ add_filter ( 'comment_text ' , array ( \Verbum_Block_Utils::class, 'render_verbum_blocks ' ) );
38
+ add_filter ( 'pre_comment_content ' , array ( \Verbum_Block_Utils::class, 'remove_blocks ' ) );
36
39
}
37
40
38
41
/**
@@ -59,84 +62,4 @@ public function enqueue_assets() {
59
62
$ vbe_cache_buster
60
63
);
61
64
}
62
-
63
- /**
64
- * Render blocks in the comment content
65
- * Filters blocks that aren't allowed
66
- *
67
- * @param string $comment_content - Text of the comment.
68
- * @return string
69
- */
70
- public function render_verbum_blocks ( $ comment_content ) {
71
- if ( ! has_blocks ( $ comment_content ) ) {
72
- return $ comment_content ;
73
- }
74
-
75
- $ blocks = parse_blocks ( $ comment_content );
76
- $ comment_content = '' ;
77
-
78
- $ allowed_blocks = self ::get_allowed_blocks ();
79
- foreach ( $ blocks as $ block ) {
80
- if ( in_array ( $ block ['blockName ' ], $ allowed_blocks , true ) ) {
81
- $ comment_content .= render_block ( $ block );
82
- }
83
- }
84
-
85
- return $ comment_content ;
86
- }
87
-
88
- /**
89
- * Remove blocks that aren't allowed
90
- *
91
- * @param string $content - Text of the comment.
92
- * @return string
93
- */
94
- public function remove_blocks ( $ content ) {
95
- if ( ! has_blocks ( $ content ) ) {
96
- return $ content ;
97
- }
98
-
99
- $ allowed_blocks = self ::get_allowed_blocks ();
100
- // The block attributes come slashed and `parse_blocks` won't be able to parse them.
101
- $ content = wp_unslash ( $ content );
102
- $ blocks = parse_blocks ( $ content );
103
- $ output = '' ;
104
-
105
- foreach ( $ blocks as $ block ) {
106
- if ( in_array ( $ block ['blockName ' ], $ allowed_blocks , true ) ) {
107
- $ output .= serialize_block ( $ block );
108
- }
109
- }
110
-
111
- return ltrim ( $ output );
112
- }
113
-
114
- /**
115
- * Get a list of allowed blocks by looking at the allowed comment tags
116
- *
117
- * @return string[]
118
- */
119
- public static function get_allowed_blocks () {
120
- global $ allowedtags ;
121
-
122
- $ allowed_blocks = array ( 'core/paragraph ' , 'core/list ' , 'core/code ' , 'core/list-item ' , 'core/quote ' , 'core/image ' , 'core/embed ' );
123
- $ convert = array (
124
- 'blockquote ' => 'core/quote ' ,
125
- 'h1 ' => 'core/heading ' ,
126
- 'h2 ' => 'core/heading ' ,
127
- 'h3 ' => 'core/heading ' ,
128
- 'img ' => 'core/image ' ,
129
- 'ul ' => 'core/list ' ,
130
- 'ol ' => 'core/list ' ,
131
- 'pre ' => 'core/code ' ,
132
- );
133
-
134
- foreach ( array_keys ( $ allowedtags ) as $ tag ) {
135
- if ( isset ( $ convert [ $ tag ] ) ) {
136
- $ allowed_blocks [] = $ convert [ $ tag ];
137
- }
138
- }
139
-
140
- return $ allowed_blocks ;
141
- }
142
65
}
0 commit comments