【问题标题】:WordPress Contributor Moderation CommentsWordPress 贡献者审核评论
【发布时间】:2017-06-13 14:23:51
【问题描述】:

如果用户在管理页面中拥有“贡献者”权限,则可以查看等待审核的 cmets,但我需要禁用此功能。

在具有“View cmets Awaiting Moderation”的用户角色中,它不存在任何内容。

如何为贡献者用户禁用 /wp-admin/edit-cmets.php?comment_status=moderated?

【问题讨论】:

    标签: wordpress comments user-permissions


    【解决方案1】:

    修改 cmets 查询

    这里有一个建议,在 pre_get_comments 操作的帮助下调整 get_comments() 输入参数的评论状态:

    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';   
    } );
    

    我们仅定位edit-comments.php 页面并将状态修改为approve(如果它为空)或'all'(对于无法发布帖子的用户)。 这里我们将评论状态分配为不存在状态以删除待处理的 cmets 列表。

    pending cmets 的所有各种状态值可能会有点混乱,例如

    hold, pending, moderated, '0'
    

    取决于它是标签、评论查询变量还是它在数据库中的存储方式,

    修改 cmets 计数

    所有 cmets 都在这里计算:

    表示Approved + Pending cmets 计数的总和。

    当我们像上面一样更改评论查询时,这些评论状态计数不会改变。我们可能也想调整它。

    这是一个示例,我们如何通过 wp_count_comments 过滤器调整 cmets 计数:

    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;
    }
    

    这也将从此处的管理菜单中删除无法发布帖子的用户的计数:

    修改评论状态链接

    最后,对于无法发布帖子的用户,我们可能希望删除 pending cmets 的状态链接:

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

    所以会变成:

    希望对你有帮助!

    【讨论】:

    • 哇,完美!谢谢!
    【解决方案2】:

    您可以使用 javascript 绕过它,仅为具有贡献者角色的登录用户添加脚本,从 dom 中删除具有未批准类的评论行。

    检查用户是否已登录并且是贡献者:

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

    然后使用以下代码添加内联脚本或将 js 文件排入队列:

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

    检查 cmets 在任何其他视图中是否可见,并将相应的类添加到脚本中以从任何地方删除它们

    /* 编辑 */

    原版 js 脚本:

    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]);
       }        
    }
    

    【讨论】:

    • 贡献者未配置此功能查看图片,贡献者无法审核 cmets 只能看到等待审核的 cmets,谢谢 =(
    • function.php function frontfooter() { echo ""; } if( is_user_logged_in() && current_user_can('contributor')) { add_action('admin_footer', 'frontfooter'); } 返回:$ 不是函数
    • 好的,它的工作很完美,但是如果我打开一个页面,会显示 cmets 并在 1 毫秒后被删除,慢动作可以捕获所有评论。
    • 肯定有一种方法可以在后端实现这一点,而不会显示未经批准的 cmets,但我想这可能是一个临时解决方案,直到您找到更好的解决方案。 ;)
    【解决方案3】:

    我找到了这个半解决方案,这段代码从评论审核列表中隐藏了评论,但它删除了所有评论和审核的评论,现在还可以 =)

    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' );
    }
    

    【讨论】:

      【解决方案4】:

      我建议您使用user role editor 来全面控制用户角色,并为特定用户添加一些例外。

      【讨论】:

        猜你喜欢
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-05
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多