forked from sy-records/wp-baidu-textcensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-baidu-textcensor.php
More file actions
181 lines (170 loc) · 7.27 KB
/
wp-baidu-textcensor.php
File metadata and controls
181 lines (170 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/*
Plugin Name: Baidu TextCensor For Comments
Plugin URI: https://github.com/sy-records/wp-baidu-textcensor
Description: 基于百度文本内容审核技术来提供WordPress评论内容审核
Version: 1.0.4
Author: 沈唁
Author URI: https://qq52o.me
License: Apache 2.0
*/
// init plugin
add_action('admin_init', 'bdtc_submit_default_options');
function bdtc_submit_default_options()
{
// 获取选项
$default = get_option('BaiduTextCensor');
if ($default == '') {
// 设置默认数据
$default = array(
'app_id' => '',
'api_key' => '',
'secret_key' => '',
'check_me' => '',
'delete' => '',
);
//更新选项
update_option('BaiduTextCensor', $default);
}
}
// stop plugin
function bdtc_stop_option()
{
$option = get_option('BaiduTextCensor');
if ($option['delete']) {
delete_option("BaiduTextCensor");
}
}
register_deactivation_hook(__FILE__, 'bdtc_stop_option');
// setting plugin
add_action('admin_menu', 'bdtc_submit_menu');
function bdtc_submit_menu()
{
add_submenu_page('options-general.php', '评论内容审核设置', '评论内容审核设置', 'manage_options', 'Baidu_Text_Censor', 'bdtc_submit_options');
}
// add setting button
function bdtc_plugin_action_links($links, $file)
{
if ($file == plugin_basename(dirname(__FILE__) . '/wp-baidu-textcensor.php')) {
$links[] = '<a href="options-general.php?page=Baidu_Text_Censor">设置</a>';
}
return $links;
}
add_filter('plugin_action_links', 'bdtc_plugin_action_links', 10, 2);
// setting page
function bdtc_submit_options()
{
//保存数据
if (isset($_POST['bdtc_submit'])) {
if (!current_user_can('level_10')) {
echo '<div class="error" id="message"><p>暂无权限操作</p></div>';
return;
}
$nonce = $_REQUEST['_bdtc_nonce'];
if (!wp_verify_nonce($nonce, 'bdtcSubmit')) {
echo '<div class="error" id="message"><p>非法操作</p></div>';
return;
}
$app_id = sanitize_text_field($_POST['app_id']);
$api_key = sanitize_text_field($_POST['api_key']);
$secret_key = sanitize_text_field($_POST['secret_key']);
$check_me = isset($_POST['check_me']) ? sanitize_text_field($_POST['check_me']) : false;
$delete = isset($_POST['delete']) ? sanitize_text_field($_POST['delete']) : false;
$check_status = bdtc_submit_check($app_id, $api_key, $secret_key);
if ($check_status) {
echo '<div class="error" id="message"><p>获取Access Token失败,请检查参数</p></div>';
} else {
$bdtcOption = array(
'app_id' => $app_id,
'api_key' => $api_key,
'secret_key' => $secret_key,
'check_me' => $check_me,
'delete' => $delete,
);
$res = update_option('BaiduTextCensor', $bdtcOption);//更新选项
if ($res) {
$updated = '设置成功!';
} else {
$updated = '设置失败或未更新选项!';
}
echo '<div class="updated" id="message"><p>' . $updated . '</p></div>';
}
}
// //获取选项
$option = get_option('BaiduTextCensor');
$check_me = $option['check_me'] !== false ? 'checked="checked"' : '';
$delete = $option['delete'] !== false ? 'checked="checked"' : '';
echo '<div class="wrap">';
echo '<h2>评论内容审核设置</h2>';
echo '<form method="post">';
echo '<table class="form-table">';
echo '<tr valign="top">';
echo '<td><input class="all-options" type="hidden" name="_bdtc_nonce" value="' . wp_create_nonce('bdtcSubmit') . '"></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row">AppID</th>';
echo '<td><input class="all-options" type="text" name="app_id" value="' . $option['app_id'] . '" /></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row">API Key</th>';
echo '<td><input class="all-options" type="text" name="api_key" value="' . $option['api_key'] . '" /></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row">Secret Key</th>';
echo '<td><input class="all-options" type="text" name="secret_key" value="' . $option['secret_key'] . '" /></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row">是否过滤博主</th>';
echo '<td><label><input value="true" type="checkbox" name="check_me" ' . $check_me . '> 勾选后会跳过博主评论内容(需要是登录状态),不去验证。</label></td>';
echo '</tr>';
echo '<tr valign="top">';
echo '<th scope="row">是否删除配置信息</th>';
echo '<td><label><input value="true" type="checkbox" name="delete" ' . $delete . '> 勾选后停用插件时会删除保存的配置信息</label></td>';
echo '</tr>';
echo '</table>';
echo '<p class="submit">';
echo '<input type="submit" name="bdtc_submit" id="submit" class="button button-primary" value="保存更改" />';
echo '</p>';
echo '</form>';
echo '<p><strong>使用提示</strong>:<br>
1. AppID、API Key、Secret Key在百度 AI 控制台的 <a target="_blank" href="https://console.bce.baidu.com/ai/?fromai=1#/ai/antiporn/app/list">产品服务 / 内容审核 - 应用列表</a> 创建应用后获取;<br>
2. 百度有默认审核策略,如果误杀严重,请进入 <a target="_blank" href="https://ai.baidu.com/censoring#/strategylist">内容审核平台创建自定义规则</a> 进行修改策略;<br>
3. 如有问题请至 <a target="_blank" href="https://github.com/sy-records/wp-baidu-textcensor">Github</a> 查看使用说明或至沈唁志博客 <a target="_blank" href="https://qq52o.me/2720.html">插件发布页</a> 留言反馈。<br>
</p>';
echo '</div>';
}
function bdtc_submit_check($appId, $apiKey, $secretKey)
{
if (!empty($appId) && !empty($apiKey) && !empty($secretKey)) {
require_once dirname(__FILE__) . '/src/AipBase.php';
$client = new \Luffy\TextCensor\AipBase($appId, $apiKey, $secretKey);
$response = $client->auth();
if (isset($response['error']) || isset($response['error_description'])) {
return true;
}
}
return false;
}
// 登录态无需验证
if (!function_exists('is_user_logged_in')) {
require(ABSPATH . WPINC . '/pluggable.php');
}
// check comment
function bdtc_refused_comments($comment_data)
{
$option = get_option('BaiduTextCensor');
if ($option['check_me'] && !is_user_logged_in()) {
require_once dirname(__FILE__) . '/src/AipBase.php';
$client = new \Luffy\TextCensor\AipBase($option['app_id'], $option['api_key'], $option['secret_key']);
$res = $client->textCensorUserDefined($comment_data['comment_content']);
// 1.合规,2.不合规,3.疑似,4.审核失败
if ($res['conclusionType'] == 2) {
wp_die("评论内容" . $res['data'][0]['msg'] . ",请重新评论");
} elseif (in_array($res['conclusionType'], [3, 4])) {
// 疑似和失败的写数据库,人工审核
add_filter( 'pre_comment_approved' , '__return_zero');
}
}
return( $comment_data );
}
add_filter('preprocess_comment', 'bdtc_refused_comments');