|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class to manage functions for comment preview. |
|
4
|
|
|
* |
|
5
|
|
|
* @package WP_Comment_Preview |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace CommentPreview\Inc; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class for comment preview functionality. |
|
12
|
|
|
* |
|
13
|
|
|
* @package CommentPreview\Inc |
|
14
|
|
|
*/ |
|
15
|
|
|
class Comment_Preview { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class constructor. |
|
19
|
|
|
*/ |
|
20
|
10 |
|
public function __construct() { |
|
21
|
|
|
|
|
22
|
10 |
|
$this->_setup_hooks(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Initialize actions and filters. |
|
27
|
|
|
*/ |
|
28
|
10 |
|
protected function _setup_hooks() { |
|
29
|
|
|
|
|
30
|
10 |
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
31
|
|
|
|
|
32
|
10 |
|
add_filter( 'comment_form_fields', array( $this, 'comment_form_fields' ), 20 ); |
|
33
|
|
|
|
|
34
|
10 |
|
add_filter( 'comment_form_field_comment', array( $this, 'append_markdown_option' ), 20 ); |
|
35
|
|
|
|
|
36
|
10 |
|
add_filter( 'comment_form_submit_button', array( $this, 'append_preview_button' ), 20 ); |
|
37
|
|
|
|
|
38
|
10 |
|
add_action( 'rest_api_init', array( $this, 'register_rest_route' ) ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Enqueue JavaScript for handling comment previews. |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function enqueue_scripts() { |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Filter to enable comment preview on custom post types. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array List of post types. |
|
50
|
|
|
*/ |
|
51
|
3 |
|
$post_types = apply_filters( 'wp_comment_preview_allowed_post_types', array( 'post' ) ); |
|
52
|
|
|
|
|
53
|
3 |
|
if ( is_singular( $post_types ) ) { |
|
54
|
|
|
|
|
55
|
2 |
|
wp_register_script( |
|
56
|
2 |
|
'wp-comment-preview', |
|
57
|
2 |
|
WP_COMMENT_PREVIEW_URL . '/assets/js/comment-preview.js', |
|
58
|
2 |
|
array(), |
|
59
|
2 |
|
'1.0.0', |
|
60
|
2 |
|
true |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
2 |
|
wp_localize_script( |
|
64
|
2 |
|
'wp-comment-preview', |
|
65
|
2 |
|
'commentPreviewData', |
|
66
|
|
|
array( |
|
67
|
2 |
|
'apiURL' => get_rest_url( null, 'wp_comment_preview/v1/' ), |
|
68
|
2 |
|
'nonce' => wp_create_nonce( 'wp_rest' ), |
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
2 |
|
wp_enqueue_script( 'wp-comment-preview' ); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Add custom markup in comment form. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $comment_fields Comment fields. |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function comment_form_fields( array $comment_fields = array() ) { |
|
84
|
|
|
|
|
85
|
1 |
|
ob_start(); |
|
86
|
|
|
|
|
87
|
|
|
// Get template file output. |
|
88
|
1 |
|
include WP_COMMENT_PREVIEW_PATH . 'templates/comment-preview.php'; |
|
89
|
|
|
|
|
90
|
|
|
// Save output and stop output buffering. |
|
91
|
1 |
|
$field = ob_get_clean(); |
|
92
|
|
|
|
|
93
|
1 |
|
if ( ! empty( $field ) ) { |
|
94
|
|
|
|
|
95
|
1 |
|
$comment_fields['comment'] = '<div id="preview-wrapper"></div>' . $comment_fields['comment']; |
|
96
|
|
|
|
|
97
|
1 |
|
$comment_fields['comment'] .= $field; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return $comment_fields; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Append radio buttons to allow a commenter to format their comment in |
|
105
|
|
|
* either markdown or plain text. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $fields HTML to output for the comment field. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string Modified HTML. |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function append_markdown_option( $fields ) { |
|
112
|
|
|
|
|
113
|
1 |
|
ob_start(); |
|
114
|
|
|
|
|
115
|
|
|
// Get template file output. |
|
116
|
1 |
|
include WP_COMMENT_PREVIEW_PATH . 'templates/markdown-option.php'; |
|
117
|
|
|
|
|
118
|
|
|
// Save output and stop output buffering. |
|
119
|
1 |
|
$markdown_option_field = ob_get_clean(); |
|
120
|
|
|
|
|
121
|
1 |
|
return $fields . $markdown_option_field; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Append a button to allow commenters to preview their comment. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $submit_button HTML to output for the submit button. |
|
128
|
|
|
* |
|
129
|
|
|
* @return string Modified HTML |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function append_preview_button( $submit_button = '' ) { |
|
132
|
|
|
|
|
133
|
1 |
|
$preview_button = sprintf( |
|
134
|
1 |
|
'<input name="preview" type="button" id="preview" class="submit" value="%1$s">', |
|
135
|
1 |
|
esc_html__( 'Preview', 'comment-preview' ) |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
1 |
|
return $submit_button . $preview_button; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Register the route for generating comment previews. |
|
143
|
|
|
*/ |
|
144
|
10 |
|
public function register_rest_route() { |
|
145
|
|
|
|
|
146
|
10 |
|
register_rest_route( |
|
147
|
10 |
|
'wp_comment_preview/v1', |
|
148
|
10 |
|
'preview', |
|
149
|
|
|
array( |
|
150
|
10 |
|
'methods' => \WP_REST_Server::CREATABLE, |
|
151
|
10 |
|
'callback' => array( $this, 'generate_preview' ), |
|
152
|
10 |
|
'permission_callback' => '__return_true', |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Processes a comment for previewing. |
|
159
|
|
|
* |
|
160
|
|
|
* @param \WP_REST_Request $request Full details about the request. |
|
161
|
|
|
* |
|
162
|
|
|
* @return array Response object. |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function generate_preview( $request ) { |
|
165
|
|
|
|
|
166
|
2 |
|
$response = array(); |
|
167
|
|
|
|
|
168
|
2 |
|
if ( ! empty( $request['author'] ) ) { |
|
169
|
2 |
|
$response['author'] = esc_html( $request['author'] ); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
2 |
|
$user_id = ( ( is_user_logged_in() ) ? get_current_user_id() : 0 ); |
|
173
|
|
|
|
|
174
|
2 |
|
if ( ! empty( $user_id ) && empty( $response['author'] ) ) { |
|
175
|
|
|
|
|
176
|
1 |
|
$user = get_userdata( $user_id ); |
|
177
|
|
|
|
|
178
|
1 |
|
if ( $user ) { |
|
179
|
|
|
|
|
180
|
1 |
|
$response['author'] = $user->data->display_name; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
2 |
|
$response['gravatar'] = get_avatar_url( $user_id, array( 'size' => 50 ) ); |
|
185
|
|
|
|
|
186
|
2 |
|
$response['date'] = current_time( get_option( 'date_format' ) . ' \a\t ' . get_option( 'time_format' ) ); |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
2 |
|
if ( ! empty( $request['comment'] ) && isset( $request['format'] ) ) { |
|
189
|
2 |
|
if ( 'text' === $request['format'] ) { |
|
190
|
1 |
|
$comment = wp_kses_data( $request['comment'] ); |
|
191
|
|
|
} else { |
|
192
|
2 |
|
$comment = apply_filters( 'pre_comment_content', $request['comment'] ); |
|
193
|
|
|
} |
|
194
|
|
|
} else { |
|
195
|
1 |
|
$comment = ''; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
2 |
|
$response['comment'] = $comment; |
|
199
|
|
|
|
|
200
|
2 |
|
return $response; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
|