子比评论区设置私密评论【已实装】

迭代版本:

  • 2024.09.25:第一版
  • 2024.09.26:第二版
  • 2024.10.01:第三版
  • 2024.10.02:第四版

魔改改进:

  1. 复选框和文字间增加一段距离
  2. 设置私密等与原主题保持一致
  3. 设置复选框与主题保持一致
  4. 将设置私密按钮移动到下拉工具栏中
  5. 优化设置私密评论逻辑

代码修改:

functions.php代码

//私密评论
function yxc_scripts(){
  if (!is_admin()) {
        $script = array(
            'smminjs' => 'https://cdn.catchyxc.com/wp-resource/js/private-comments.js',
        );
        foreach( $script as $k => $v ){
            wp_register_script( $k, $v, array(), '2.4.0', true);
        };

        wp_enqueue_script('smminjs');
        
        if (is_singular()) {
            wp_enqueue_script('smminjs');
        };

    }
}
add_action('wp_enqueue_scripts', 'yxc_scripts');

function yxc_private_message_hook( $comment_content , $comment ){
    $comment_ID = $comment->comment_ID;
    $parent_ID = $comment->comment_parent ? $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();
    $current_user = wp_get_current_user();
    $html = '<span style="color:#558E53"><i class="fa fa-lock fa-fw"></i>该评论为私密评论</span>';
    
    if ($is_private) {
        // 普通用户未登录,且没有输入评论名
        if (!is_user_logged_in() && $current_commenter['comment_author_email'] == '') {
            return $comment_content = $html;
        } 
        
        // 检查当前用户是否是评论的作者、原评论的作者或管理员
        if ($current_user->user_email == $email || 
            $current_user->user_email == $parent_email || 
            current_user_can('delete_user')) {
            return '【私密评论】 ' . $comment_content;
        }
        
        return $comment_content = $html;
    }
    
    return $comment_content;
}

add_filter('get_comment_text','yxc_private_message_hook',10,2);

function yxc_mark_private_message( $comment_id ){
    if ( $_POST['is-private'] ) {
        update_comment_meta($comment_id,'_private','true');
    }
}
add_action('comment_post', 'yxc_mark_private_message');
//将某条评论设为私密评论
add_action('wp_ajax_nopriv_mrhe_private', 'yxc_private');
add_action('wp_ajax_mrhe_private', 'yxc_private');
function yxc_private(){
  $comment_id = $_POST["p_id"];
   $action = $_POST["p_action"];
  if ( $action == 'set_private'){
     update_comment_meta($comment_id, '_private', 'true');
    }
    if ($action == 'del_private'){
        delete_comment_meta($comment_id, '_private','true');
    }
    echo '<i class="fa fa-check mr10 fa-fw" aria-hidden="true"></i>设置成功';
   die;
}
// //挂载到评论底部
// function yxc_footer_info_add_private($info, $comment) {
//     if ( current_user_can( 'manage_options' ) ) {
//         $comment_ID = $comment->comment_ID;
//         $i_private = get_comment_meta($comment_ID, '_private', true);
//         $flag = ''; // 初始化 $flag 为空字符串
//         if (empty($i_private)) {
//             $flag .= '  <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  data-actionp="set_private" data-idp="' . get_comment_id() . '" id="sp" class="sm"><span class="has_set_private">设为私密</span></a>';
//             $info = $info . $flag;
//         } else {
//             $flag .= '  <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  data-actionp="del_private" data-idp="' . get_comment_id() . '" id="sp" class="sm"><span class="has_set_private">删除私密</span></a>';
//             $info = $info . $flag;
//         }
//     }
//     return $info;
// }
// add_filter('comment_footer_info', 'yxc_footer_info_add_private', 99, 2);

js代码

$(function() {
	$(".links-card ul.list-inline img").each(function() {
		if ("" == $(this).attr("data-src")) {
			var a = "" + $(this).parent().attr("href");
			$(this).attr("src", a)
		}
	}), $(document).on("click", ".sm", function() {
		var a = $(this);
		if (a.hasClass("private_now")) return notyf("您之前已设置过私密评论", "warning"), !1;
		a.addClass("private_now");
		var t = a.data("idp"),
			e = a.data("actionp"),
			n = a.children(".has_set_private"),
			r = {
				action: "mrhe_private",
				p_id: t,
				p_action: e
			};
		return $.post("/wp-admin/admin-ajax.php", r, function(a) {
			n.html(a)
		}), !1
	})
});

/wp-content/themes/zibll/template/comments.php代码

<!--私密评论-->
									<label class="but c-blue pw-1em" data-placement="top" data-toggle="tooltip" title="你的评论仅评论双方可见。" style="margin-bottom: 0px; -moz-user-select: none; -ms-user-select: none; user-select: none; margin-right: 6px;">
                           			<input id="is-private-checkbox" name="is-private" type="checkbox" style= "margin: 1px 4px 1px 0; width: 12px; height: 12px; min-width: 0em;">私密</label>
图片[1] - 子比评论区设置私密评论【已实装】 - 云晓晨CatchYXC

/wp-content/themes/zibll/inc/functions/zib-comments-list.php代码

    $i_private = get_comment_meta($comment->comment_ID, '_private', true);
    if (current_user_can('manage_options')) {
        if (empty($i_private)) {
            $private_but = '<a href="javascript:;" rel="external nofollow" data-actionp="set_private" data-idp="' . $comment->comment_ID . '" class="sm"><span class="has_set_private"><i class="fa fa-lock mr10 fa-fw" aria-hidden="true"></i>设为私密</span></a>';
        } else {
            $private_but = '<a href="javascript:;" rel="external nofollow" data-actionp="del_private" data-idp="' . $comment->comment_ID . '" class="sm"><span class="has_set_private"><i class="fa fa-unlock mr10 fa-fw" aria-hidden="true"></i>设为公开</span></a>';
        }
        $lists .= '<li>' . $private_but . '</li>';
    }  
图片[2] - 子比评论区设置私密评论【已实装】 - 云晓晨CatchYXC

CSS代码

在后台主题设置[自定义CSS样式]添加下面CSS代码:

/*设置私密复选框样式*/
#is-private-checkbox::before {
    transition: transform 0.2s ease;
}

#is-private-checkbox:checked::before {
    transform: translateY(-50%) translateX(-5%) scale(0.5);
}

#is-private-checkbox:not(:checked)::before {
    transform: translateY(0) translateX(0) scale(1);
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容