-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-grade-comments.php
495 lines (432 loc) · 14.5 KB
/
wp-grade-comments.php
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/*
Plugin Name: WP Grade Comments
Version: 1.3.1
Description: Grades and private comments for WordPress blog posts. Built for the City Tech OpenLab.
Author: Boone Gorges
Author URI: http://boone.gorg.es
Plugin URI: http://openlab.citytech.cuny.edu
Text Domain: wp-grade-comments
Domain Path: /languages
*/
define( 'OLGC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'OLGC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
if ( is_admin() ) {
require OLGC_PLUGIN_DIR . '/includes/admin.php';
}
/**
* Load textdomain.
*
* @since 1.0.0
*/
function olgc_load_plugin_textdomain() {
load_plugin_textdomain( 'wp-grade-comments' );
}
add_action( 'init', 'olgc_load_plugin_textdomain' );
/**
* Markup for the checkboxes on the Leave a Comment section.
*
* @since 1.0.0
*/
function olgc_leave_comment_checkboxes() {
if ( ! olgc_is_instructor() ) {
return;
}
?>
<div class="olgc-checkboxes">
<label for="olgc-private-comment"><?php _e( 'Make this comment private.', 'wp-grade-comments' ) ?></label> <input type="checkbox" name="olgc-private-comment" id="olgc-private-comment" value="1" />
<br />
<label for="olgc-add-a-grade"><?php _e( 'Add a grade.', 'wp-grade-comments' ) ?></label> <input type="checkbox" name="olgc-add-a-grade" id="olgc-add-a-grade" value="1" />
<br />
</div>
<?php
}
add_action( 'comment_form_logged_in_after', 'olgc_leave_comment_checkboxes' );
/**
* Markup for the grade box on the Leave a Comment section.
*
* @since 1.0.0
*
* @param array $args Arguments from `comment_form()`.
* @return array
*/
function olgc_leave_comment_after_comment_fields( $args ) {
if ( ! olgc_is_instructor() ) {
return $args;
}
$args['comment_notes_after'] .= '
<div class="olgc-grade-entry">
<label for="olgc-grade">' . __( 'Grade:', 'wp-grade-comments' ) . '</label> <input type="text" maxlength="5" name="olgc-grade" id="olgc-grade" />
</div>
<div class="olgc-privacy-description">
' . __( 'NOTE: Private response and grade will only be visible to instructors and the post\'s author.', 'wp-grade-comments' ) . '
</div>' . wp_nonce_field( 'olgc-grade-entry-' . get_the_ID(), '_olgc_nonce', false, false );
return $args;
}
add_filter( 'comment_form_defaults', 'olgc_leave_comment_after_comment_fields', 1000 );
/**
* Catch and save values after comment submit.
*
* @since 1.0.0
*
* @param int $comment_id ID of the comment.
* @param WP_Comment $comment Comment object.
*/
function olgc_insert_comment( $comment_id, $comment ) {
// Private
$is_private = olgc_is_instructor() && ! empty( $_POST['olgc-private-comment'] );
if ( ! $is_private && ! empty( $comment->comment_parent ) ) {
$is_private = (bool) get_comment_meta( $comment->comment_parent, 'olgc_is_private', true );
}
if ( $is_private ) {
update_comment_meta( $comment_id, 'olgc_is_private', '1' );
}
if ( ! isset( $_POST['_olgc_nonce'] ) ) {
return;
}
// Grade
if ( olgc_is_instructor() && wp_verify_nonce( $_POST['_olgc_nonce'], 'olgc-grade-entry-' . $comment->comment_post_ID ) && ! empty( $_POST['olgc-add-a-grade'] ) && isset( $_POST['olgc-grade'] ) ) {
$grade = wp_unslash( $_POST['olgc-grade'] );
update_comment_meta( $comment_id, 'olgc_grade', $grade );
}
}
add_action( 'wp_insert_comment', 'olgc_insert_comment', 10, 2 );
/**
* Add 'Private' message, grade, and gloss to comment text.
*
* @since 1.0.0
*
* @param string $text Comment text.
* @param WP_Comment $comment Comment object.
* @return string
*/
function olgc_add_private_info_to_comment_text( $text, $comment ) {
global $pagenow;
// Grade has its own column on edit-comments.php.
$grade = '';
$grade_text = '';
if ( 'edit-comments.php' !== $pagenow && ( olgc_is_instructor() || olgc_is_author() ) ) {
$grade = get_comment_meta( $comment->comment_ID, 'olgc_grade', true );
if ( '' !== $grade ) {
$grade_text .= sprintf(
'<div class="olgc-grade-display olgc-grade-hidden">' .
'<span class="olgc-grade-label">%s</span> ' .
'<a href="#" class="olgc-show-grade olgc-grade-toggle">%s</a>' .
'<noscript>' .
'<span class="olgc-grade-value-noscript">%s</span>' .
'</noscript>' .
'<a href="#" class="olgc-hide-grade olgc-grade-toggle">%s</a>' .
'<span class="olgc-grade-value-script"><br />%s</span>' .
'</div>',
esc_html__( 'Grade (Private):', 'wp-grade-comments' ),
esc_html__( '(show)', 'wp-grade-comments' ),
esc_html( $grade ),
esc_html__( '(hide)', 'wp-grade-comments' ),
esc_html( $grade )
);
}
}
$is_private = get_comment_meta( $comment->comment_ID, 'olgc_is_private', true );
$comment_text = $text;
if ( $is_private ) {
$comment_text = sprintf(
'<div class="olgc-grade-display olgc-grade-hidden">' .
'<strong class="olgc-private-notice">%s</strong> ' .
'<a href="#" class="olgc-show-grade olgc-grade-toggle">%s</a>' .
'<noscript>' .
'<span class="olgc-grade-value-noscript">%s</span>' .
'</noscript>' .
'<a href="#" class="olgc-hide-grade olgc-grade-toggle">%s</a>' .
'<span class="olgc-grade-value-script"><br />%s</span>' .
'</div>',
esc_html__( 'Comment (Private):', 'wp-grade-comments' ),
esc_html__( '(show)', 'wp-grade-comments' ),
esc_html( $text ),
esc_html__( '(hide)', 'wp-grade-comments' ),
esc_html( $text )
);
}
$text = $comment_text . $grade_text;
$gloss = '';
if ( '' !== $grade && $is_private ) {
$gloss = __( 'NOTE: Private response and grade are visible only to instructors and to the post\'s author.', 'wp-grade-comments' );
} elseif ( $is_private ) {
$gloss = __( 'NOTE: Private response is visible only to instructors and to the post\'s author.', 'wp-grade-comments' );
}
if ( $gloss ) {
$text .= '<p class="olgc-privacy-description">' . $gloss . '</p>';
}
return $text;
}
add_filter( 'get_comment_text', 'olgc_add_private_info_to_comment_text', 100, 2 ); // Late to avoid kses
/**
* Add a "Private" label to the Reply button on reply comments.
*
* @since 1.0.0
*
* @param array $args Arguments passed to `comment_reply_link()`.
* @param WP_Comment $comment Comment object.
*/
function olgc_add_private_label_to_comment_reply_link( $args, $comment ) {
$is_private = get_comment_meta( $comment->comment_ID, 'olgc_is_private', true );
if ( $is_private ) {
$args['reply_text'] = '(Private) ' . $args['reply_text'];
$args['reply_to_text'] = '(Private) ' . $args['reply_to_text'];
}
return $args;
}
add_filter( 'comment_reply_link_args', 'olgc_add_private_label_to_comment_reply_link', 10, 2 );
/**
* Remove private comments via WP_Comment_Query query args.
*
* @since 1.2.0
*/
function olgc_remove_private_comments( WP_Comment_Query $comment_query ) {
$post_id = 0;
if ( ! empty( $comment_query->query_vars['post_id'] ) ) {
$post_id = $comment_query->query_vars['post_id'];
} elseif ( ! empty( $comment_query->query_vars['post_ID'] ) ) {
$post_id = $comment_query->query_vars['post_ID'];
}
// Unfiltered
if ( olgc_is_instructor() || olgc_is_author( $post_id ) ) {
return;
}
$pc_ids = olgc_get_inaccessible_comments( get_current_user_id(), $post_id );
if ( ! $pc_ids ) {
return;
}
$not__in = (array) $comment_query->query_vars['comment__not_in'];
$not__in = array_merge( $not__in, $pc_ids );
$comment_query->query_vars['comment__not_in'] = $not__in;
}
add_action( 'pre_get_comments', 'olgc_remove_private_comments' );
/**
* Filter comments out of comment feeds.
*
* @since 1.0.2
*
* @param string $where WHERE clause from comment feed query.
* @return string
*/
function olgc_filter_comments_from_feed( $where ) {
$pc_ids = olgc_get_inaccessible_comments( get_current_user_id(), get_queried_object_id() );
if ( $pc_ids ) {
$where .= ' AND comment_ID NOT IN (' . implode( ',', array_map( 'intval', $pc_ids ) ) . ')';
}
return $where;
}
add_filter( 'comment_feed_where', 'olgc_filter_comments_from_feed' );
/**
* Get inaccessible comments for a user.
*
* Optionally by post ID.
*
* @since 1.0.0
*
* @param int $user_id ID of the user.
* @param int $post_id Optional. ID of the post.
* @return array Array of comment IDs.
*/
function olgc_get_inaccessible_comments( $user_id, $post_id = 0 ) {
// Get a list of private comments
remove_action( 'pre_get_comments', 'olgc_remove_private_comments' );
$comment_args = array(
'meta_query' => array(
array(
'key' => 'olgc_is_private',
'value' => '1',
),
),
'status' => 'any',
);
if ( ! empty( $post_id ) ) {
$comment_args['post_id'] = $post_id;
}
$private_comments = get_comments( $comment_args );
add_action( 'pre_get_comments', 'olgc_remove_private_comments' );
// Filter out the ones that are written by the logged-in user, as well
// as those that are attached to a post that the user is the author of
$pc_ids = array();
foreach ( $private_comments as $private_comment ) {
if ( $user_id && ! empty( $private_comment->user_id ) && $user_id == $private_comment->user_id ) {
continue;
}
if ( $user_id ) {
$comment_post = get_post( $private_comment->comment_post_ID );
if ( $user_id == $comment_post->post_author ) {
continue;
}
}
// Comment authors should see private replies.
if ( ! empty( $private_comment->comment_parent ) ) {
$parent_id = (int) $private_comment->comment_parent;
$parent_comment = get_comment( $parent_id );
if ( $user_id == $parent_comment->user_id ) {
continue;
}
}
$pc_ids[] = $private_comment->comment_ID;
}
$pc_ids = wp_parse_id_list( $pc_ids );
return $pc_ids;
}
/**
* Filter comment count. Not cool.
*
* @since 1.0.0
*
* @param int $count Comment counte.
* @param int $post_id ID of the post.
* @return int
*/
function olgc_get_comments_number( $count, $post_id = 0 ) {
if ( empty( $post_id ) ) {
return $count;
}
$cquery = new WP_Comment_Query();
$comments_for_post = $cquery->query( array(
'post_id' => $post_id,
'count' => true,
) );
$count = $comments_for_post;
return $count;
}
add_filter( 'get_comments_number', 'olgc_get_comments_number', 10, 2 );
/**
* Enqueue assets.
*
* @since 1.0.0
*/
function olgc_enqueue_assets() {
wp_enqueue_style( 'wp-grade-comments', OLGC_PLUGIN_URL . 'assets/css/wp-grade-comments.css' );
wp_enqueue_script( 'wp-grade-comments', OLGC_PLUGIN_URL . 'assets/js/wp-grade-comments.js', array( 'jquery' ) );
}
add_action( 'comment_form_before', 'olgc_enqueue_assets' );
/**
* Is the current user the course instructor?
*
* @since 1.0.0
*
* @return bool
*/
function olgc_is_instructor() {
$is_admin = current_user_can( 'manage_options' );
/**
* Filters whether the current user is an "instructor" for the purposes of grade comments.
*
* @param bool $is_admin By default, `current_user_can( 'manage_options' )`.
*/
return apply_filters( 'olgc_is_instructor', $is_admin );
}
/**
* Is the current user the post author?
*
* @since 1.0.0
*
* @param int $post_id Optional. ID of the post. Defaults to current post ID.
* @return bool
*/
function olgc_is_author( $post_id = null ) {
if ( $post_id ) {
$post = get_post( $post_id );
} else {
$post = get_queried_object();
}
if ( ! is_a( $post, 'WP_Post' ) ) {
return false;
}
return is_user_logged_in() && get_current_user_id() == $post->post_author;
}
/**
* Prevent non-instructors from editing comments that are private or have grades.
*
* @since 1.0.2
*/
function olgc_prevent_edit_comment_for_olgc_comments( $caps, $cap, $user_id, $args ) {
if ( 'edit_comment' === $cap && ! olgc_is_instructor( $user_id ) ) {
$comment_id = $args[0];
$is_private = get_comment_meta( $comment_id, 'olgc_is_private', true );
$grade = get_comment_meta( $comment_id, 'olgc_grade', true );
if ( $is_private || $grade ) {
$caps = array( 'do_not_allow' );
}
}
return $caps;
}
add_filter( 'map_meta_cap', 'olgc_prevent_edit_comment_for_olgc_comments', 10, 4 );
/**
* Allows empty comments when a grade is attached.
*
* Works only in WP 5.0+.
*/
function olgc_allow_empty_comment( $allow, $commentdata ) {
// Only instructors can do this.
if ( ! olgc_is_instructor() ) {
return $allow;
}
return ! empty( $_POST['olgc-grade'] );
}
add_filter( 'allow_empty_comment', 'olgc_allow_empty_comment', 10, 2 );
/**
* Prevent private comments from appearing in BuddyPress activity streams.
*
* For now, we are going with the sledgehammer of deleting the comment altogether. In the
* future, we could use hide_sitewide.
*
* @since 1.0.0
*
* @param int $comment_id ID of the comment.
*/
function olgc_prevent_private_comments_from_creating_bp_activity_items( $comment_id ) {
$is_private = get_comment_meta( $comment_id, 'olgc_is_private', true );
if ( ! $is_private ) {
return;
}
if ( 'comment_post' === current_action() ) {
remove_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
remove_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
} elseif ( 'edit_comment' === current_action() ) {
remove_action( 'edit_comment', 'bp_blogs_record_comment', 10 );
remove_action( 'edit_comment', 'bp_activity_post_type_comment', 10 );
}
}
add_action( 'comment_post', 'olgc_prevent_private_comments_from_creating_bp_activity_items', 0 );
add_action( 'edit_comment', 'olgc_prevent_private_comments_from_creating_bp_activity_items', 0 );
/**
* Prevent private comments from appearing in BuddyPress activity streams.
*
* @since 1.2.0
*
* @param string $new_status New comment status.
* @param string $old_status Old comment status.
* @param WP_Comment $comment Comment object.
*/
function olgc_prevent_private_comments_from_creating_bp_activity_items_on_transition( $new_staus, $old_status, $comment ) {
$is_private = get_comment_meta( $comment->comment_ID, 'olgc_is_private', true );
if ( ! $is_private ) {
return;
}
remove_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10 );
}
add_action( 'transition_comment_status', 'olgc_prevent_private_comments_from_creating_bp_activity_items_on_transition', 0, 3 );
/**
* Add custom classes for private and grade comments.
*
* @since 1.4.0
*
* @param string[] $classes An array of comment classes.
* @param string $class A comma-separated list of additional classes added to the list.
* @param int $comment_id The comment id.
* @return string[] $classes An array of classes.
*/
function olgc_add_comment_classes( $classes, $class, $comment_id ) {
if ( get_comment_meta( $comment_id, 'olgc_is_private', true ) ) {
$classes[] = 'comment-is-private';
}
if ( get_comment_meta( $comment_id, 'olgc_grade', true ) ) {
$classes[] = 'comment-has-grade';
}
return $classes;
}
add_filter( 'comment_class', 'olgc_add_comment_classes', 10, 3 );