@@ -19,50 +19,23 @@ class Comment_Preview {
19
19
*/
20
20
public function __construct () {
21
21
22
- $ this ->setup_hooks ();
22
+ $ this ->_setup_hooks ();
23
23
}
24
24
25
25
/**
26
26
* Initialize actions and filters.
27
27
*/
28
- public function setup_hooks () {
29
-
30
- add_filter ( 'comment_form_submit_button ' , array ( $ this , 'append_preview_button ' ) );
31
-
32
- add_filter ( 'comment_form_field_comment ' , array ( $ this , 'append_markdown_option ' ), 20 );
33
-
34
- add_filter ( 'comment_form_fields ' , array ( $ this , 'comment_form_fields ' ), 20 );
28
+ protected function _setup_hooks () {
35
29
36
30
add_action ( 'wp_enqueue_scripts ' , array ( $ this , 'enqueue_scripts ' ) );
37
31
38
- add_action ( 'rest_api_init ' , array ( $ this , 'register_rest_route ' ) );
39
- }
40
-
41
- /**
42
- * Add custom markup in comment form.
43
- *
44
- * @param array $comment_fields Comment fields.
45
- *
46
- * @return mixed
47
- */
48
- public function comment_form_fields ( array $ comment_fields = array () ) {
49
-
50
- ob_start ();
51
-
52
- // Get template file output.
53
- include WP_COMMENT_PREVIEW_PATH . 'templates/comment-preview.php ' ;
54
-
55
- // Save output and stop output buffering.
56
- $ field = ob_get_clean ();
57
-
58
- if ( ! empty ( $ field ) ) {
32
+ add_filter ( 'comment_form_fields ' , array ( $ this , 'comment_form_fields ' ), 20 );
59
33
60
- $ comment_fields [ ' comment ' ] = ' <div id="preview-wrapper"></div> ' . $ comment_fields [ ' comment ' ] ;
34
+ add_filter ( ' comment_form_field_comment ' , array ( $ this , ' append_markdown_option ' ), 20 ) ;
61
35
62
- $ comment_fields ['comment ' ] .= $ field ;
63
- }
36
+ add_filter ( 'comment_form_submit_button ' , array ( $ this , 'append_preview_button ' ), 20 );
64
37
65
- return $ comment_fields ;
38
+ add_action ( ' rest_api_init ' , array ( $ this , ' register_rest_route ' ) ) ;
66
39
}
67
40
68
41
/**
@@ -93,22 +66,52 @@ public function enqueue_scripts() {
93
66
}
94
67
}
95
68
69
+ /**
70
+ * Add custom markup in comment form.
71
+ *
72
+ * @param array $comment_fields Comment fields.
73
+ *
74
+ * @return mixed
75
+ */
76
+ public function comment_form_fields ( array $ comment_fields = array () ) {
77
+
78
+ ob_start ();
79
+
80
+ // Get template file output.
81
+ include WP_COMMENT_PREVIEW_PATH . 'templates/comment-preview.php ' ;
82
+
83
+ // Save output and stop output buffering.
84
+ $ field = ob_get_clean ();
85
+
86
+ if ( ! empty ( $ field ) ) {
87
+
88
+ $ comment_fields ['comment ' ] = '<div id="preview-wrapper"></div> ' . $ comment_fields ['comment ' ];
89
+
90
+ $ comment_fields ['comment ' ] .= $ field ;
91
+ }
92
+
93
+ return $ comment_fields ;
94
+ }
95
+
96
96
/**
97
97
* Append radio buttons to allow a commenter to format their comment in
98
98
* either markdown or plain text.
99
99
*
100
- * @param string $field HTML to output for the comment field.
100
+ * @param string $fields HTML to output for the comment field.
101
+ *
101
102
* @return string Modified HTML.
102
103
*/
103
- public function append_markdown_option ( $ field ) {
104
+ public function append_markdown_option ( $ fields ) {
105
+
106
+ ob_start ();
104
107
105
- $ append_field = ' <div class="comment-form-markdown"> ' ;
106
- $ append_field .= ' <input checked="checked" type="radio" id="format- markdown-radio" name="wp_comment_format" value="markdown"> ' ;
107
- $ append_field .= ' <label for="format-markdown-radio">Use <a href="https://commonmark.org/help/">markdown</a>.</label> ' ;
108
- $ append_field .= ' <input type="radio" id="format-text-radio" name="wp_comment_format" value="text"> ' ;
109
- $ append_field .= ' <label for="format-text-radio">Use plain text.</label></div> ' ;
108
+ // Get template file output.
109
+ include WP_COMMENT_PREVIEW_PATH . ' templates/ markdown-option.php ' ;
110
+
111
+ // Save output and stop output buffering.
112
+ $ markdown_option_field = ob_get_clean () ;
110
113
111
- return $ field . $ append_field ;
114
+ return $ fields . $ markdown_option_field ;
112
115
}
113
116
114
117
/**
@@ -120,7 +123,10 @@ public function append_markdown_option( $field ) {
120
123
*/
121
124
public function append_preview_button ( $ submit_button = '' ) {
122
125
123
- $ preview_button = '<input name="preview" type="button" id="preview" class="submit" value="Preview"> ' ;
126
+ $ preview_button = sprintf (
127
+ '<input name="preview" type="button" id="preview" class="submit" value="%1$s"> ' ,
128
+ esc_html__ ( 'Preview ' , 'comment-preview ' )
129
+ );
124
130
125
131
return $ submit_button . $ preview_button ;
126
132
}
@@ -146,7 +152,7 @@ public function register_rest_route() {
146
152
*
147
153
* @param \WP_REST_Request $request Full details about the request.
148
154
*
149
- * @return \WP_REST_Response| array Response object.
155
+ * @return array Response object.
150
156
*/
151
157
public function generate_preview ( $ request ) {
152
158
@@ -182,7 +188,7 @@ public function generate_preview( $request ) {
182
188
$ comment = '' ;
183
189
}
184
190
185
- $ response ['comment ' ] = wpautop ( $ comment ) ;
191
+ $ response ['comment ' ] = $ comment ;
186
192
187
193
return $ response ;
188
194
}
0 commit comments