如题,给客户开发的一个小功能,输入关键词查询网站文章是否被百度收录,如果未收录则删除或移动到回收站;
支持两种查询方式:
1、百度直接查询是否收录,只支持少量查询,否则会触发百度安全机制;
2、通过第三方API查询,支持大批量查询,费用较低。API为link123站长工具。
其他功能说明:
1、支持输入关键词查询;
2、支持设置查询间隔时间,防止触发百度安全验证机制;
3、支持停止后继续查询功能;
4、异步分批次查询,百度直接查询时一次一个网址,通过API查询时一次10个网址;
上代码:
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
<?php add_action('admin_menu', function () { add_menu_page('删除百度未收录文章', '删除未收录', 'edit_themes', 'del-post-of-baidu-no-included', function () { ?> <div class="wrap"> <h2 class="wp-heading-inline">删除百度未收录文章</h2> <?php if($count = get_option('_wb_del_post_count',0)){ echo "<p>最后一次API查询删除的未收录文章数量为:{$count}篇.</p>"; } ?> <form> <table class="form-table"> <tbody> <tr class="form-field1"> <th scope="row"><label for="keyword">关键词 <span style="color:#e00"> *</span></label></th> <td> <input type="text" name="keyword" id="keyword" value="<?php echo get_option('_wb_del_posts_last_keyword'); ?>" placeholder="请输入关键词" required class="regular-text"/> </td> </tr> <tr class="form-field"> <th scope="row"><label for="action_type">处理方式</label></th> <td> <select name="action_type" id="action_type"> <option value="trash">移动到回收站</option> <option value="del">直接删除</option> </select> </td> </tr> <tr class="form-field1"> <th scope="row"><label for="interval_time">间隔时间</label></th> <td> <input type="number" name="interval_time" id="interval_time" value="2" class="regular-text" required/> 秒 <p>处理每条数据间隔时间, 这里主要是防止百度屏蔽查询. </p> </td> </tr> <tr class="form-field"> <th scope="row"><label for="query_type">查询引擎</label></th> <td> <select name="query_type" id="query_type"> <option value="baidu">百度查询</option> <option value="link113" <?php $query_type = get_option('_wb_del_posts_query_type'); selected('link113',$query_type); ?>>link113Api查询</option> </select> </td> </tr> <tr class="form-field1 link113"<?php echo $query_type != 'link113'? ' style="display:none"':''; ?>> <th scope="row"><label for="AccessKey">link113Api - AccessKey</label></th> <td> <input type="text" name="AccessKey" id="AccessKey" value="<?php echo get_option('link113_accesskey'); ?>" class="regular-text"/> <p>API接口申请地址: https://www.link113.com/</p> <p>API回调地址: <code id="api_return_url"><?php echo admin_url('admin-ajax.php?action=wb_api_return'); ?></code></p> <p>查询完毕后一段时间(视回调时间而定)刷新页面可显示删除未收录文章数量统计。</p> </td> </tr> <tr class="form-field1 link113"<?php echo $query_type != 'link113'? ' style="display:none"':''; ?>> <th scope="row"><label for="SecretKey">link113Api - SecretKey</label></th> <td> <input type="text" name="SecretKey" id="SecretKey" value="<?php echo get_option('link113_secretkey'); ?>" class="regular-text"/> </td> </tr> </tbody> </table> <p class="submit" style="float: left;"> <?php submit_button('提交处理', 'button button-primary wb_submit', 'wb_submit', false,['data-offset' => 0]); if($last_offset = get_option('_wb_del_posts_last_offset',0)){ submit_button('继续处理', 'button wb_submit', 'wb_submit', false,['data-offset' => $last_offset,'style' => 'margin-left:10px;']); } ?> <span class="spinner"></span> </p> </form> </div> <script type="text/javascript"> jQuery(document).ready(function ($) { var page_title = $("title").text(); $('body').on('change', '#query_type', function (e) { e.preventDefault(); if ($('#query_type').val() == "link113") { $('.link113').show(); } else { $('.link113').hide(); } }); // $('body').on('change', '#AccessKey', function (e) { // e.preventDefault(); // $('#api_return_url').html('<?php echo admin_url('admin-ajax.php?action=wb_api_return&key='); ?>'+$('#AccessKey').val()); // }); $('body').on('click', '.wb_submit', function (e) { e.preventDefault(); let keyword = $('#keyword').val(), query_type = $('#query_type').val(); if (keyword == '' || !keyword) { alert('请输入关键词'); return; } if (query_type == "link113" && ($('#AccessKey').val() == '' || $('#SecretKey').val() == '')) { alert('请输入Link113 的秘钥'); return; } if (confirm('确定要执行操作吗?') == false) { return false; } if($(this).attr('data-offset') > 0){ $(this).remove(); } let args = { action: "wb_del_posts_of_baidu_no_included_action", keyword: keyword, offset: $(this).attr('data-offset'), type: $('#action_type').val(), query_type: query_type, AccessKey: $('#AccessKey').val(), SecretKey: $('#SecretKey').val(), }; $('.wb_submit').prop('disabled', true).next('.spinner').addClass('is-active'); wb_action_post(args); }); function wb_action_post(args) { $.post(ajaxurl, args, function (data, status) { let response = (typeof data == 'object') ? data : JSON.parse(data); if (response.code != 0) { alert(response.msg); } else { if ($('#wb_notice').length < 1) { $('.wp-heading-inline').after('<div id="wb_notice" class="notice is-dismissible inline hidden"></div>'); } if ($('.wp-heading-inline').offset().top < $(window).scrollTop()) { $('html, body').animate({scrollTop: 0}, 800); } $('#wb_notice').html('<p><strong>' + response.msg + '</strong></p>') .removeClass('notice-success notice-error notice-info hidden') .addClass('notice-success').css('opacity', 0) .slideDown(200, function () { $(this).fadeTo(200, 1, function () { let _this = $(this); if (_this.find('button.notice-dismiss').length > 0) { return; } let button = $('<button type="button" class="notice-dismiss"><span class="screen-reader-text">忽略此提示。</span></button>'); button.on('click.wp-dismiss-notice', function (e) { e.preventDefault(); _this.fadeTo(100, 0, function () { _this.slideUp(100, function () { _this.remove(); }); }); }); _this.append(button); if ($('#TB_ajaxContent').length > 0) { $('#TB_ajaxContent').scrollTop(0); } }); }); if (response.done == 0) { $("title").text('('+response.offset+'/'+response.found_posts+') '+page_title); setTimeout(function () { args.offset = response.offset; wb_action_post(args); }, $('#interval_time').val() * 1000); } else { $("title").text('(完成) '+page_title); $('.wb_submit').prop('disabled', false).next('.spinner').removeClass('is-active'); } } }); return false; } }); </script> <?php }, '', 99); }); function wb_ajax_wb_del_posts_of_baidu_no_included_action() { $keyword = $_POST['keyword'] ?? wp_unslash($_POST['keyword']); $offset = $_POST['offset'] ? absint($_POST['offset']) : 0; $type = isset($_POST['offset']) && $_POST['offset'] == 'del' ? 'del' : 'trash'; $query_type = isset($_POST['query_type']) && $_POST['query_type'] == 'baidu' ? 'baidu' : 'link113'; //第三方接口 $AccessKey = $_POST['AccessKey'] ?? wp_unslash($_POST['AccessKey']); $SecretKey = $_POST['SecretKey'] ?? wp_unslash($_POST['SecretKey']); if ($query_type == 'link113' && $offset == 0) { update_option('link113_accesskey', $AccessKey, false); update_option('link113_secretkey', $SecretKey, false); update_option('_wb_del_posts_query_type','link113',false); update_option('_wb_del_posts_type', $type, false); update_option('_wb_del_post_count',0,false); }elseif($offset == 0){ update_option('_wb_del_posts_query_type','baidu',false); } $per_page = 1; if ($query_type == 'link113') { $per_page = 10; } $query = new WP_Query([ 'post_type' => 'any', 'post_status' => 'publish', 'order' => 'ASC', //'fields' => 'ids', 's' => $keyword, 'search_columns' => ['post_title', 'post_content'], 'posts_per_page' => $per_page, 'offset' => $offset ]); $msg = ''; $included = 0; if ($query->have_posts()) { foreach ($query->posts as $post) { $post_url = get_the_permalink($post); if ($query_type == 'link113') { $baidu_status = wb_link113_query($post->ID, $post_url); if ($baidu_status != 2) { sleep(10); wp_send_json(['code' => 0, 'offset' => $offset, 'done' => 0, 'found_posts' => $query->found_posts, 'msg' => '已处理' . $offset . '篇文章, 继续请求中,如需停止执行,请刷新页面,接口报错: ' . $baidu_status]); } } else { $baidu_status = wb_baidu_query($post_url); } switch ($baidu_status) { case 1: $status = '<span style="color:#009428"> [已收录]</span>'; break; case 2: $status = '<span style="color:#009428"> [已提交API查询]</span>'; break; case -1: $status = '<span style="color:#940005"> [百度请求错误]</span>'; break; default: $status = '<span style="color:#005694"> [未收录已删除]</span>'; break; } if ($baidu_status == 1) { $included++; } if ($baidu_status == 0) { if ($type == 'del') { wp_delete_post($post->ID); } if ($type == 'trash') { wp_trash_post($post->ID); } } $msg .= $post->post_title . ' [' . $post_url . '] ' . $status . '<br />'; if ($baidu_status == -1) { sleep(10); wp_send_json(['code' => 0, 'offset' => $offset, 'done' => 0, 'found_posts' => $query->found_posts, 'msg' => '已处理' . $offset . '篇文章, 由于频繁请求, 百度请求报错, 系统自动重试中, 如需停止请刷新页面. ']); exit; } } $count = count($query->posts); $number = $offset + $count; } else { $count = 0; $number = $offset + $count; } update_option('_wb_del_posts_last_offset',$offset,false); update_option('_wb_del_posts_last_keyword',$keyword,false); if ($count < $per_page) { update_option('_wb_del_posts_last_offset',0,false); update_option('_wb_del_posts_last_keyword','',false); wp_send_json(['code' => 0, 'offset' => $number, 'done' => 1, 'found_posts' => 0, 'msg' => '处理成功' . $number . '篇文章, 百度共收录' . $included . '篇文章。<br />' . $msg]); } else { wp_send_json(['code' => 0, 'offset' => $number, 'done' => 0, 'found_posts' => $query->found_posts, 'msg' => '处理中, 共需处理' . $query->found_posts . '篇文章, 本次处理了' . $number . '篇文章。<br />' . $msg]); } } function wb_ajax_nopriv_wb_api_return() { $type = get_option('_wb_del_posts_type'); $accesskey = get_option('link113_accesskey'); if (!$type || !$accesskey || !get_option('link113_secretkey')) { die('ERROR'); } $data = wp_unslash($_POST); file_put_contents(get_template_directory().'/api.txt',"\r\n".date('Y-m-d H:i:s')."-----".str_replace('\\','',json_encode($_POST,true)),FILE_APPEND); if(isset($data['result']) && $data['result'] == 0 && $data['item'] == 'baidu-check' &&strpos($data['url'],$data['id'])){ if ($type == 'del') { wp_delete_post(absint($data['id'])); } if ($type == 'trash') { wp_trash_post(absint($data['id'])); } update_option('_wb_del_post_count',(get_option('_wb_del_post_count',0)+1),false); } die('SUCCESS'); } add_action('wp_ajax_wb_del_posts_of_baidu_no_included_action', 'wb_ajax_wb_del_posts_of_baidu_no_included_action'); add_action('wp_ajax_nopriv_wb_api_return', 'wb_ajax_nopriv_wb_api_return'); //add_action('wp_ajax_wb_api_return', 'wb_ajax_nopriv_wb_api_return'); function wb_link113_query($post_id, $url) { $access_key = get_option('link113_accesskey'); $token = md5($access_key . get_option('link113_secretkey')); $api_url = 'https://openapi.link113.com/push'; $http = wp_remote_post($api_url, [ 'body' => [ 'id' => $post_id, 'url' => $url, 'item' => 'baidu-check', 'access_key' => $access_key, 'access_token' => $token, ] ],[ 'timeout' => 60, 'redirection' => 5, 'sslverify' => true ] ); //var_dump($http); if (wp_remote_retrieve_response_code($http) != 200) { return '返回状态非200'; } $body = trim(wp_remote_retrieve_body($http)); if (!$body) { return '返回内容为空'; } if ($response = json_decode($body, true)) { if (isset($response['code']) && $response['code'] == 1) return 2; if (isset($response['message'])) return 'code: '. $response['code'] .'message: '. $response['message']; } return '返回错误未知'; } /** * 通过百度查询网址是否被收录 * @param $url * @return int */ function wb_baidu_query($url) { @session_start(); $UserAgents = [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 *OPR*/44.0.2510.1218", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 *2345Explorer*/8.4.1.14855", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 *JuziBrowser*", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 *LBBROWSER*", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2441.400 *QQBrowser*/9.5.10632.400", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X *MetaSr* 1.0", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) *Maxthon*/5.0.2.2000 Chrome/47.0.2526.73 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 *UBrowser*/6.1.2107.204 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 *BIDUBrowser*/8.4 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 *TheWorld* 7", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 *YaBrowser*/17.3.1.840 *Yowser*/2.5 Safari/537.36", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36 *Qiyu*/2.1.0.0", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5", ]; $defaults = array( 'timeout' => 20, 'redirection' => 5, 'sslverify' => true, //'cookies' => $_SESSION['baidu_cookies'] ?? 'BAIDUID=8DBF0E8C0568607608B16282B62D7EBF:FG=1', 'headers' => [ 'Host' => 'www.baidu.com', 'Referer' => 'http://www.baidu.com', 'Content-Type' => 'application/x-www-form-urlencoded', 'Connection' => 'keep-alive', 'Accept' => 'text/html,application/xhtml+xml,application/xml', 'user-agent' => $UserAgents[array_rand($UserAgents)], //'Cookie' => $_SESSION['baidu_cookies'] ?? 'BAIDUID=8DBF0E8C0568607608B16282B62D7EBF:FG=1', ], ); $search_url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=' . urlencode($url); do { $http = wp_remote_get($search_url, $defaults); if (wp_remote_retrieve_response_code($http) != 200) { return -1; } $body = trim(wp_remote_retrieve_body($http)); if (!$body) { return -1; } if (preg_match('#提交网址.+?给我们#is', $body)) { break; } if (preg_match('#没有找到该URL#is', $body)) { break; } if (preg_match('#<title>百度安全验证</title>#is', $body)) { return -1; break; } if (preg_match('#没有找到与#i', $body)) { break; } return 1; } while (false); return 0; } |
将代码插入主题functions.php内,或者作为独立插件使用都可以。