-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextend-comment.php
More file actions
263 lines (215 loc) · 8.25 KB
/
extend-comment.php
File metadata and controls
263 lines (215 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
Plugin Name: Расширенные комментарии
Version: 1.2
Plugin URI: https://wp-kama.ru/?p=8342
Description: Плагин, добавляющий произвольные поля в форму комментариев.
Author: Campusboy
Author URI: https://wp-plus.ru/
*/
// Добавляем поля для незарегистрированных пользователей
add_filter( 'comment_form_default_fields', 'extend_comment_default_fields' );
function extend_comment_default_fields( $fields ) {
$fields['phone'] = '<p class="comment-form-phone reply-hidden">' .
'<label for="phone">' . __( 'Phone' ) . '</label>' .
'<input id="phone" name="phone" type="text" size="30"/></p>';
return $fields;
}
// Добавляем поля для всех пользователей
add_action( 'comment_form_logged_in_after', 'extend_comment_custom_fields' );
add_action( 'comment_form_after_fields', 'extend_comment_custom_fields' );
function extend_comment_custom_fields() {
echo '<p class="comment-form-title reply-hidden">' .
'<label for="title">' . __( 'Comment Title' ) . '</label>' .
'<input id="title" name="title" type="text" size="30"/></p>';
echo '<p class="comment-form-rating reply-hidden">' .
'<label for="rating">' . __( 'Rating' ) . '<span class="required">*</span></label>
<span class="commentratingbox">';
for ( $i = 1; $i <= 5; $i ++ ) {
echo '
<label class="commentrating">
<input type="radio" name="rating" id="rating" value="' . $i . '"/> ' . $i . '
</label>';
}
echo '</span></p>';
}
// Сохраняем поля
add_action( 'comment_post', 'save_extend_comment_meta_data' );
function save_extend_comment_meta_data( $comment_id ) {
if ( ! empty( $_POST['phone'] ) ) {
$phone = sanitize_text_field( $_POST['phone'] );
add_comment_meta( $comment_id, 'phone', $phone );
}
if ( ! empty( $_POST['title'] ) ) {
$title = sanitize_text_field( $_POST['title'] );
add_comment_meta( $comment_id, 'title', $title );
}
if ( ! empty( $_POST['rating'] ) ) {
$rating = intval( $_POST['rating'] );
add_comment_meta( $comment_id, 'rating', $rating );
}
}
// Проверяем, заполнено ли поле "Рейтинг"
add_filter( 'preprocess_comment', 'verify_extend_comment_meta_data' );
function verify_extend_comment_meta_data( $commentdata ) {
// Если это ответ на комментарий, то отменяем остальные проверки, так как ответному комментариию рейтинг не нужен
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] === 'replyto-comment' ) {
return $commentdata;
}
if ( empty( $_POST['rating'] ) || ! (int) $_POST['rating'] ) {
wp_die( __( 'Error: You did not add a rating. Hit the Back button on your Web browser and resubmit your comment with a rating.' ) );
}
return $commentdata;
}
// Отображение содержимого метаполей во фронт-энде
add_filter( 'comment_text', 'modify_extend_comment' );
function modify_extend_comment( $text ) {
if ( $commenttitle = get_comment_meta( get_comment_ID(), 'title', true ) ) {
$commenttitle = '<strong>' . esc_attr( $commenttitle ) . '</strong><br/>';
$text = $commenttitle . $text;
}
if ( $commentrating = get_comment_meta( get_comment_ID(), 'rating', true ) ) {
if ( ! function_exists( 'wp_star_rating' ) ) {
require_once ABSPATH . 'wp-admin/includes/template.php';
}
$commentrating = wp_star_rating( array(
'rating' => $commentrating,
'echo' => false
) );
$text = $text . $commentrating;
}
return $text;
}
// Добавляем шаблон рейтинга и его стили
add_action( 'wp_enqueue_scripts', 'check_count_extend_comments' );
function check_count_extend_comments() {
if ( is_singular() && comments_open() ) {
wp_enqueue_style( 'dashicons' );
$stars_css = '
.comment .reply-hidden { display: none }
.comment-form label.commentrating { display: inline-block }
.star-rating .star-full:before { content: "\f155"; }
.star-rating .star-half:before { content: "\f459"; }
.star-rating .star-empty:before { content: "\f154"; }
.star-rating .star {
color: #0074A2;
display: inline-block;
font-family: dashicons;
font-size: 20px;
font-style: normal;
font-weight: 400;
height: 20px;
line-height: 1;
text-align: center;
text-decoration: inherit;
vertical-align: top;
width: 20px;
}
';
wp_add_inline_style( 'dashicons', $stars_css );
}
}
// Добавляем новый метабокс на страницу редактирования комментария, если он не дочерний
add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
function extend_comment_add_meta_box( $comment ) {
if ( ! $comment->comment_parent ) {
add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
}
}
// Отображаем наши поля
function extend_comment_meta_box( $comment ) {
$phone = get_comment_meta( $comment->comment_ID, 'phone', true );
$title = get_comment_meta( $comment->comment_ID, 'title', true );
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );
wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
?>
<p>
<label for="phone"><?php _e( 'Phone' ); ?></label>
<input type="text" name="phone" value="<?php echo esc_attr( $phone ); ?>" class="widefat"/>
</p>
<p>
<label for="title"><?php _e( 'Comment Title' ); ?></label>
<input type="text" name="title" value="<?php echo esc_attr( $title ); ?>" class="widefat"/>
</p>
<p>
<label for="rating"><?php _e( 'Rating: ' ); ?></label>
<span class="commentratingbox">
<?php
for ( $i = 1; $i <= 5; $i ++ ) {
echo '
<span class="commentrating">
<input type="radio" name="rating" id="rating" value="' . $i . '" ' . checked( $i, $rating, 0 ) . '/>
</span>';
}
?>
</span>
</p>
<?php
}
// Сохраняем данные метаполей, заполенных в админке на странице редактирования комментария
add_action( 'edit_comment', 'extend_comment_edit_meta_data' );
function extend_comment_edit_meta_data( $comment_id ) {
if ( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) {
return;
}
if ( ! empty( $_POST['phone'] ) ) {
$phone = sanitize_text_field( $_POST['phone'] );
update_comment_meta( $comment_id, 'phone', $phone );
} else {
delete_comment_meta( $comment_id, 'phone' );
}
if ( ! empty( $_POST['title'] ) ) {
$title = sanitize_text_field( $_POST['title'] );
update_comment_meta( $comment_id, 'title', $title );
} else {
delete_comment_meta( $comment_id, 'title' );
}
if ( ! empty( $_POST['rating'] ) ) {
$rating = intval( $_POST['rating'] );
update_comment_meta( $comment_id, 'rating', $rating );
} else {
delete_comment_meta( $comment_id, 'rating' );
}
}
/**
* Возвращает HTML блок со средним рейтингом в виде звездочек для текущего поста.
*
* @see wp_star_rating()
*
* @return string
*/
function get_the_extend_comment_post_star_rating() {
if ( ! function_exists( 'wp_star_rating' ) ) {
require_once ABSPATH . 'wp-admin/includes/template.php';
}
$star_rating = wp_star_rating( array(
'rating' => get_the_extend_comment_post_rating(),
'echo' => false
) );
return $star_rating;
}
/**
* Возвращает средний рейтинг поста.
*
* @return string
*/
function get_the_extend_comment_post_rating() {
global $wpdb;
$rating = 0;
if ( is_singular() ) {
$post_id = (int) get_the_ID();
$rating = wp_cache_get( $post_id, 'post_rating' );
if ( false === $rating ) {
$rating = $wpdb->get_var( "
SELECT AVG(cm.meta_value) as avg
FROM $wpdb->commentmeta as cm
JOIN $wpdb->comments as c
ON cm.comment_id = c.comment_ID
WHERE c.comment_post_ID IN ($post_id) AND cm.meta_key = 'rating'
" );
$rating = $rating ?: 0;
wp_cache_add( $post_id, $rating, 'post_rating' );
}
}
return $rating;
}