Advertisement
pjeaje

Option to make WordPress comments private

Feb 1st, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.25 KB | None | 0 0
  1. /**
  2. * Make comments able to choose private or public...
  3. * as seen in http://wpquestions.com/question/showLoggedIn/id/10468
  4. *
  5. * TERMS: a 'comment' is a comment to post. a 'reply' is a comment to a comment..
  6. * scenarios:
  7. * 1. a comment can be public or private. checkbox is visible.
  8. * 2. a reply can be public or private based only on the following (no checkbox):
  9. * a. if comment is private, all reply are automatic private.
  10. * b. if comment is public, all reply are public.
  11. */
  12. function restrict_comments( $comments , $post_id ){
  13. global $post;
  14. $user = wp_get_current_user();
  15. if($post->post_author == $user->ID){
  16. return $comments;
  17. }
  18. foreach($comments as $comment){
  19. $is_private = is_comment_private($comment);
  20. if (!$is_private ) {$new_comments_array[] = $comment;continue;}
  21. if( $comment->user_id == $user->ID || $post->post_author == $comment->user_id ){
  22. if($post->post_author == $comment->user_id){
  23. if($comment->comment_parent > 0){
  24. $parent_comm = get_comment( $comment->comment_parent );
  25. if( ( $parent_comm->user_id == $user->ID ) ){
  26. $new_comments_array[] = $comment;
  27. }
  28. }elseif (!$is_private){
  29. $new_comments_array[] = $comment;
  30. }
  31. } else {
  32. $new_comments_array[] = $comment;
  33. }
  34. }
  35. }
  36. return $new_comments_array;
  37. }
  38. add_filter( 'comments_array' , 'restrict_comments' , 10, 2 );
  39. function is_comment_private($comment) {
  40. $is_private = (get_comment_meta( $comment->comment_ID, 'private', 'no' )=='yes')?true:false;
  41. if ($is_private && !($comment->comment_parent > 0)) {
  42. return true;
  43. } elseif ($comment->comment_parent > 0) {
  44. return is_comment_private(get_comment( $comment->comment_parent ));
  45. }
  46. return false;
  47. }
  48. add_action( 'comment_post', 'save_comment_meta_data' );
  49. function save_comment_meta_data( $comment_id ) {
  50. if ( !is_user_logged_in() ) {return;}
  51. if ( ( isset( $_POST['private'] ) ) && ( $_POST['private'] != '') )
  52. $private = wp_filter_nohtml_kses($_POST['private']);
  53. add_comment_meta( $comment_id, 'private', $private );
  54. }
  55. add_action( 'comment_form_logged_in_after', 'additional_fields' );
  56. add_action( 'comment_form_after_fields', 'additional_fields' );
  57. function additional_fields () {
  58. if ( !is_user_logged_in() ) {return;}
  59. echo '<p class="comment-form-private">'.
  60. '<label for="private"><input id="private" name="private" type="checkbox" value="yes" />' . __( 'Make this comment private' ) . '</label>'.
  61. '</p>';
  62. echo '<p class="comment-private-msg">Please note: Your reply will be private</p>';
  63. echo '<p class="comment-public-msg">Warning: Your reply will be public</p>';
  64. }
  65. add_filter( 'comment_class' , 'private_comment_class',99,3 );
  66. function private_comment_class( $classes, $class, $comment_id ){
  67. $is_private = is_comment_private(get_comment( $comment_id ));
  68. if($is_private) {
  69. $classes[] = 'private-comment';
  70. }
  71. return $classes ;
  72. }
  73. add_action('wp_head','add_css_style');
  74. function add_css_style(){
  75. ?>
  76.         <style>
  77.                 .comment .comment-form-private,
  78.                 .comment-private-msg,
  79.                 .comment.private-comment .comment-public-msg,
  80.                 .comment-public-msg {
  81.                         display: none;
  82.                 }
  83.                 .comment .comment-public-msg,
  84.                 .private-comment .comment-private-msg{
  85.                         display: block;
  86.                 }
  87.  
  88.         </style>
  89. <?php
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement