马春杰杰 Exit Reader Mode

[mcj]WordPress增加私密评论功能

更新:现在对私密评论也会自动变为私密:

//私密评论
function liao_private_message_hook( $comment_content , $comment){
    $comment_ID = $comment->comment_ID;
    $parent_ID = $comment->comment_parent;
    $parent_email = get_comment_author_email($parent_ID);
    $is_private = get_comment_meta($comment_ID,'_private',true);
    $email = $comment->comment_author_email;
	//echo "parent_email:".$parent_email;
    $current_commenter = wp_get_current_commenter();
	//echo "current_commenter:".$current_commenter['comment_author_email'];
	if ( $is_private ){
		$comment_content = '@私密@ ' . $comment_content;
		if ( $current_commenter['comment_author_email'] == $email || $parent_email == $current_commenter['comment_author_email'] || current_user_can('delete_user') ) 	return $comment_content;
		else return '<span style="color:#A6A6A6"><i class="fa fa-lock fa-fw"></i>该评论为私密评论</span>';	
	}else return $comment_content;

    //if ( $is_private ) $comment_content = '@私密@ ' . $comment_content;
   // if ( $current_commenter['comment_author_email'] == $email || $parent_email == $current_commenter['comment_author_email'] || current_user_can('delete_user') ) 	return $comment_content; //这一段确实只有登录了才会显示。
    //if ( $is_private ) return '<span style="color:#A6A6A6"><i class="fa fa-lock fa-fw"></i>该评论为私密评论</span>';
    //return $comment_content;
}
add_filter('get_comment_text','liao_private_message_hook',10,2);

function liao_mark_private_message( $comment_id ) {
    // 通过评论ID获取评论对象
    $comment = get_comment( $comment_id );

    // 初始化父评论是否为私密评论的标志为false
    $parent_is_private = false;

    // 获取父评论ID
    $parent_ID = $comment->comment_parent;

    // 如果存在父评论,检查父评论是否为私密评论
    if ($parent_ID != 0) {
        $parent_is_private = get_comment_meta($parent_ID, '_private', true) == 'true';
    }

    // 检查当前评论是否标记为私密或父评论是否为私密
    if ( isset($_POST['is-private']) || $parent_is_private ) {
        update_comment_meta($comment_id, '_private', 'true');
    }
}
add_action('comment_post', 'liao_mark_private_message', 10, 1);

 

打开 DUX主题的 functions.php 文件,在最后一个 ?> 的前面添加以下代码:

//私密评论
function liao_private_message_hook( $comment_content , $comment){
    $comment_ID = $comment->comment_ID;
    $parent_ID = $comment->comment_parent;
    $parent_email = get_comment_author_email($parent_ID);
    $is_private = get_comment_meta($comment_ID,'_private',true);
    $email = $comment->comment_author_email;
    $current_commenter = wp_get_current_commenter();
    if ( $is_private ) $comment_content = '#私密# ' . $comment_content;
    if ( $current_commenter['comment_author_email'] == $email || $parent_email == $current_commenter['comment_author_email'] || current_user_can('delete_user') ) return $comment_content;
    if ( $is_private ) return '<span style="color:#A6A6A6"><i class="fa fa-lock fa-fw"></i>该评论为私密评论</span>';
    return $comment_content;
}
add_filter('get_comment_text','liao_private_message_hook',10,2);
function liao_mark_private_message( $comment_id ){
    if ( $_POST['is-private'] ) {
        update_comment_meta($comment_id,'_private','true');
    }
}
add_action('comment_post', 'liao_mark_private_message');

然后打开评论配置文件,加入:

温馨提示: 此处内容需要 评论本文刷新本页 才能查看!

效果如下:

修复了个bug: