Skip to content

Commit ff368c7

Browse files
author
vishalkakadiya
committed
Add unit tests for comment fields
1 parent c92eb48 commit ff368c7

File tree

5 files changed

+188
-101
lines changed

5 files changed

+188
-101
lines changed

inc/classes/class-comment-preview.php

+50-44
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,23 @@ class Comment_Preview {
1919
*/
2020
public function __construct() {
2121

22-
$this->setup_hooks();
22+
$this->_setup_hooks();
2323
}
2424

2525
/**
2626
* Initialize actions and filters.
2727
*/
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() {
3529

3630
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
3731

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 );
5933

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 );
6135

62-
$comment_fields['comment'] .= $field;
63-
}
36+
add_filter( 'comment_form_submit_button', array( $this, 'append_preview_button' ), 20 );
6437

65-
return $comment_fields;
38+
add_action( 'rest_api_init', array( $this, 'register_rest_route' ) );
6639
}
6740

6841
/**
@@ -93,22 +66,52 @@ public function enqueue_scripts() {
9366
}
9467
}
9568

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+
9696
/**
9797
* Append radio buttons to allow a commenter to format their comment in
9898
* either markdown or plain text.
9999
*
100-
* @param string $field HTML to output for the comment field.
100+
* @param string $fields HTML to output for the comment field.
101+
*
101102
* @return string Modified HTML.
102103
*/
103-
public function append_markdown_option( $field ) {
104+
public function append_markdown_option( $fields ) {
105+
106+
ob_start();
104107

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();
110113

111-
return $field . $append_field;
114+
return $fields . $markdown_option_field;
112115
}
113116

114117
/**
@@ -120,7 +123,10 @@ public function append_markdown_option( $field ) {
120123
*/
121124
public function append_preview_button( $submit_button = '' ) {
122125

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+
);
124130

125131
return $submit_button . $preview_button;
126132
}
@@ -146,7 +152,7 @@ public function register_rest_route() {
146152
*
147153
* @param \WP_REST_Request $request Full details about the request.
148154
*
149-
* @return \WP_REST_Response|array Response object.
155+
* @return array Response object.
150156
*/
151157
public function generate_preview( $request ) {
152158

@@ -182,7 +188,7 @@ public function generate_preview( $request ) {
182188
$comment = '';
183189
}
184190

185-
$response['comment'] = wpautop( $comment );
191+
$response['comment'] = $comment;
186192

187193
return $response;
188194
}

templates/markdown-option.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Template to add markdown option in comment's form.
4+
*
5+
* @package WP_Comment_Preview
6+
*/
7+
8+
?>
9+
10+
<div class="comment-form-markdown">
11+
12+
<input checked="checked" type="radio" id="format-markdown-radio" name="wp_comment_format" value="markdown">
13+
<label for="format-markdown-radio"><?php esc_html_e( 'Use', 'comment-preview' ); ?> <a href="https://commonmark.org/help/"><?php esc_html_e( 'markdown', 'comment-preview' ); ?></a></label>
14+
15+
<input type="radio" id="format-text-radio" name="wp_comment_format" value="text">
16+
<label for="format-text-radio"><?php esc_html_e( 'Use plain text', 'comment-preview' ); ?></label>
17+
18+
</div>

0 commit comments

Comments
 (0)