Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fine tuning comments rules #86

Open
Kwagga-Quagga opened this issue Oct 22, 2019 · 3 comments
Open

Fine tuning comments rules #86

Kwagga-Quagga opened this issue Oct 22, 2019 · 3 comments

Comments

@Kwagga-Quagga
Copy link

Kwagga-Quagga commented Oct 22, 2019

Hi,

My message is probably more of question regarding comment rights, or event the handling of comment's rights by comment-press

Let's set the basics :

By default comment are allowed on published post only
By default any user can publish comments on any post

That's very nice and usefull in the perspective of collaborative review of and discussion on a post.
However we may want to use comments or commentpress for other usage.

A plug-in to handle comment rights within a workflow

I've thus work on a simple wp-plugin.

the intended wordflow

  • a user (contributor) can "draft" a post and switch it to "pending" review status
  • any authorised user (basically admin and editor) can review the post by writing it as wordpress built-in comments
  • the initial contributor can read all the comments written on his own post ; and he, the contributor can edit his post accordingly
  • ultimately an authorised user (admin, editor) can publish the post, the post content will be visible publicly but the comments remain only visible to the post author and the authorised users

some pseudo code and code

The code, still in alpha version work's great on twenty* theme and some other I tried, but only some functionalities work natively on comment press while some other don't :

  • enable comment on draft and pending post for admin, editor, author and contributor
    • Aim : so that a reviewer (editor) can comment (review) a post submitted by a contributor ; at this stage both text and comment aren't public yet
    • Result : works
  • disable comments for any user but the author of the post (of selected custom post type) and authorized user (admin, editor)
    • Aim : restrict who can post comment so that only the reviewer (admin and editors) can post a review using comment
    • Result : do not work, the comment form (actually the whole comment area) is still displayed and any logged-in user can post a comment
  • hide comment for everyone but the author of the post (of the same selected custom post type), the author of the comment, and authorized user (admin, editor)
    • Aim : make comment private between the author and the reviewers (admin and editors)
    • Result : works

I assume commentpress override wp settings regarding comments, as do my plugin. But it also seems that commentpres overrides my plugin settings on this specific spot add_filter( 'comments_open', '__return_false' );, preventing my plug-in to work.

It would be great if you could direct me on how to fix this alpha code to work with comment press, and I would be glad if I could edit/fork this code as a comment press version of my very basic plug-in.

Any guidance would be greatly appreciated !

Regards


/**
 * Return true if user can comment on post preview
 */
function is_a_preview_commenter() {
  $roles = array('administrator', 'editor', 'author', 'contributor');   // change this roles according to your needs
  $allowed_roles = apply_filters('preview_comment_allowed_roles', $roles);
  $user = wp_get_current_user();
  $inrole = array_intersect($user->roles, $allowed_roles);
  return ! empty( $inrole );
} 

/**
 * Add a nonce field for comment form in post preview for allowed users
 */
function additional_comment_fields() {
  if ( is_preview() && is_a_preview_commenter() ) {
    $nonce = wp_create_nonce('comment_preview');
    echo '<input type="hidden" name="check_the_preview" value="' . $nonce . '" />';
  }
}
add_action( 'comment_form_logged_in_after', 'additional_comment_fields');

/**
 * On 'wp_loaded', when the current page is wp-comments-post.php, check if the request
 * comes from post preview, if so and also the current user is allowed replace 
 * $GLOBALS['wp_post_statuses']['pending'] with $GLOBALS['wp_post_statuses']['publish']
 */
function fake_public_pending() {
  global $pagenow;
  if ( $pagenow === 'wp-comments-post.php' && is_a_preview_commenter() ) {
    $nonce = filter_input(INPUT_POST, 'check_the_preview', FILTER_SANITIZE_STRING);
    if ( empty($nonce) || ! wp_verify_nonce($nonce, 'comment_preview') ) return;
    global $wp_post_statuses;
    // let WordPress believe all pending posts are published post
    $wp_post_statuses['pending'] = $wp_post_statuses['publish'];
  }
}
add_action('wp_loaded', 'fake_public_pending');
/**
 * After a comment is inserted, redirect page to post preview when request come from
 * post preview. Also check if current user role is one of the allowed
 */
function redirect_to_preview( $comment, $user ) {
  if ( ! is_a_preview_commenter() ) return;
  $nonce = filter_input(INPUT_POST, 'check_the_preview', FILTER_SANITIZE_STRING);
  if ( empty($nonce) || ! wp_verify_nonce($nonce, 'comment_preview') ) return;
  $link = get_permalink($comment->comment_post_ID);
  $url = add_query_arg( array('preview' => 'true'), $link );
  wp_safe_redirect("{$url}#comment-{$comment->comment_ID}", 302);
  exit();
}
add_action('set_comment_cookies', 'redirect_to_preview', 9999, 2 );




/*
Display comment to post author only, and editor and admin
*/

function restrict_visible_comments( $comments_query ) {
  if ( !is_singular( array( 'customPosttype1' , 'customPosttype2' )) ) {
        return; // restrict to certain post type
	 }
    if ( current_user_can( 'moderate_comments' ) ){
        return;  // moderators (admin and editors) can see all comments
	}
	if ( get_current_user_id() != get_queried_object()->post_author ){
        add_filter( 'comments_open', '__return_false' ); // disable comment to anyone but the post author
	  $comments_query->query_vars['user_id'] = get_current_user_id(); // hide the comment but to the post author and comment authors (ie user that can moderate comments)
        return;  
	}
    if ( get_current_user_id() == get_queried_object()->post_author ){
        return;  // the author of the post can see all comments
	}
    $comments_query->query_vars['user_id'] = get_current_user_id();
	
}
if ( !is_admin() )
    add_action( 'pre_get_comments', 'restrict_visible_comments' );

@christianwach
Copy link
Member

@Kwagga-Quagga This is an interesting idea. It's also quite a lot to digest (especially as you posted just before a school holiday) so please bear with me as I think through what you're trying to achieve.

Cheers, Christian

@Kwagga-Quagga
Copy link
Author

@Kwagga-Quagga This is an interesting idea. It's also quite a lot to digest (especially as you posted just before a school holiday) so please bear with me as I think through what you're trying to achieve.

Cheers, Christian

Hi

I slightly edited my post hoping it's easier to understand.

Regarding what I'm trying to achieve, you may think this way :

  • Person A write a text
  • Person B review the text : he write the review in the comments
  • Person A read the review and edit his text accordingly
  • Person B validate the text.

this seems to be a pretty classic (commentpress) workflow
What's different

  • Person A, a contributor, draft a text ; when he's done writing, he submit it as pending (this step do not require customization)
  • Person B, an editor, review the text : he writes his review in the comments, then he let PersonA know his text has been reviewed
    • here we need one customization : enable comment for post with draft and pending status
  • Person A read the review and edit his text accordingly (no need for customization)
  • Person B validate the text. (no need for customization)

--> now once the post is published, by default, both the post's content and the post's comments are visible to the public.

  • in some situation it's ok, so there is no need for further customization
  • but in some other situation it is not ok as we want the comment to remain private ; then we need extra customization i) hidding old comment to the public and ii) prevent anyone else to write a new comment

hope this helps

@christianwach
Copy link
Member

A few observations:

  1. The comment_form_logged_in_after filter doesn't exist in CommentPress. You could use commentpress_comment_form_pre_submit instead.

But it also seems that commentpress overrides my plugin settings on this specific spot add_filter( 'comments_open', '__return_false' );, preventing my plug-in to work.

  1. There's no such declaration in CommentPress, so I'm not clear what you mean.

  2. For obscure historical reasons, CommentPress doesn't use the existing built-in WordPress comment form - you will have to look at the code to see the filters that you can use to force the form to be hidden, e.g. commentpress_show_comment_form.

I think if you implement your code around the CommentPress filters and actions, then you should make progress. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants