1
votes

If user has permission "Contributor" in Admin Page can View the comments awaiting moderation but i need to disable this.

In user roles with "View comments Awaiting Moderation" its not present nothing.

How to disable the /wp-admin/edit-comments.php?comment_status=moderated for Contributor users?

enter image description here

4

4 Answers

1
votes

You could bypass it with javascript adding a script only for logged in users with contributor role removing comment rows with unapproved class from dom.

check if user is logged and is contributor:

if( is_user_logged_in() && current_user_can('contributor')) {

then add a script inline or enqueue a js file with the follwing code:

$('#the-comment-list tr.unapproved').remove();

check if the comments are visible in any other views and add the corresponding classes to the script to remove them form everywhere

/* edit */

vanilla js script:

var elem = document.getElementById("the-comment-list"); 

for (var i = 0; i < elem.childNodes.length; i++) {
   if (/\bunapproved/.test(elem.childNodes[i].className)) {
      elem.childNodes[i].parentNode.removeChild(elem.childNodes[i]);
   }        
}
1
votes

Modify the comments query

Here's one suggestion, to adjust the comment status for the get_comments() input arguments, with help of the pre_get_comments action:

add_action( 'pre_get_comments', function( \WP_Comment_Query $query )
{   
    // Only target edit-comments.php
    if( ! did_action( 'load-edit-comments.php' ) )
        return;

    // Only target users that can't publish posts
    if( current_user_can( 'publish_posts' ) )
        return;

    // Halt the query for pending comments      
    if( 'hold' === $query->query_vars['status'] )
        $query->query_vars['status'] = 'non-existent';

    // Remove pending comments from the 'all' or '' status view
    if( in_array( $query->query_vars['status'], [ 'all', '' ], true ) )
        $query->query_vars['status'] = 'approve';   
} );

where we target only the edit-comments.php page and modify the status to approve if it's empty or 'all' for users that can't publish posts. Here we assign the comment status to a non existent status to remove the list of pending comments.

It can be a little confusing all the various status values for pending comments, e.g.

hold, pending, moderated, '0'

depending if it's a label, a comment query variable or how it's stored in the database,

Modify the comments counts

All comments count here:

all

means the sum of the Approved + Pending comments counts.

When we change the comment query, like above, these comment status counts will not change. We might want to adjust that too.

Here's an example how we can adjust the comments counts via the wp_count_comments filter:

add_filter( 'wp_count_comments', 'wpse_count_comments', 10, 2 );

function wpse_count_comments( $counts, $post_id  )
{
    // Only target the backend
    if( !  is_admin() )
        return $counts;

    // Only target users that can't publish posts    
    if( current_user_can( 'publish_posts' ) )
        return $counts;

    // Avoid infinite loop before calling wp_count_comments()    
    remove_filter( current_filter(), __FUNCTION__ );    
    $counts = wp_count_comments( $counts, $post_id  );
    add_filter( current_filter(), __FUNCTION__, 10, 2 );

    // Subract 'moderated' count from 'all' count
    $counts->all = $counts->all - $counts->moderated;

    // Set 'moderated' count to zero
    $counts->moderated = 0;

    return $counts;
}

This will also remove the counting number, for users that can't publish posts, from the admin menu here:

pending

Modify the comment status links

Finally we might want to remove the status link for pending comments, for users that can't publish posts:

add_filter( 'comment_status_links', function( $status )
{
    if( ! current_user_can( 'publish_posts' ) )
        unset( $status['moderated'] );
    return $status;
} );

So it will become:

all

Hope it helps!

0
votes

I have found this semi-solution, this code hide comment from comment moderation list, but it remove all comment also moderated comment, its ok for now =)

https://wordpress.stackexchange.com/questions/167250/prevent-contributor-to-show-comment-list

function filter_comments_by_contributor( $all_comments ) {
    // get the current logged in user
    $current_user = wp_get_current_user();

    if ( 0 == $current_user->ID ) {
        // Not logged in.
        return $all_comments;
    } else {
        // Logged in.

        // check if the logged-in user is a contributor
        if ( in_array( 'contributor', (array) $current_user->roles ) ) {

            // check if the user is on wp-admin backend,
            $screen = get_current_screen();
            if ( ! empty( $screen ) && 'edit-comments' == $screen->id ) {

                // get all posts by that contributor
                $args              = array(
                    'author'         => $current_user->ID,
                    'posts_per_page' => - 1,
                    'fields'         => 'ids'
                );
                $contributor_posts = get_posts( $args );

                // unset the comments given on posts other than his/her.
                foreach ( $all_comments as $key => $value ) {
                    if ( ! in_array( $value->comment_post_ID, $contributor_posts ) ) {
                        unset( $all_comments[ $key ] );
                    }
                }
            }

            return $all_comments;
        } else {
            return $all_comments;
        }
    }
}

if( is_user_logged_in() && !current_user_can('manage_options')) {   
    add_filter( 'the_comments', 'filter_comment_by_contributor' );
}
0
votes

I recommend you to use user role editor to have a full control on users roles in general and add some exceptions to specific users.