PluginProbe
果果推送 / trunk
果果推送 vtrunk
trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7
ggpush / includes / class-ggpush-api.php

class-ggpush-api.php in 果果推送 trunk, at includes/class-ggpush-api.php

536 lines 17.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * 推送核心处理类
5 */
6 class Ggpush_Api
7 {
8
9 /**
10 * 获取请求�
11 时时间
12 * @return int
13 */
14 public static function get_request_timeout()
15 {
16 return Ggpush_Plugin::get_option('push_timeout', 30);
17 }
18
19 /**
20 * 格式化推送平台
21 * @param int $record_platform
22 * @return string
23 */
24 public static function format_record_platform($record_platform)
25 {
26 switch ($record_platform) {
27 case 1:
28 return '百度';
29 case 2:
30 return '360';
31 case 3:
32 return '搜狗';
33 case 4:
34 return '头条';
35 case 5:
36 return '神马';
37 case 6:
38 return '�
39 ';
40 case 7:
41 return '谷歌';
42 case 8:
43 return 'IndexNow';
44 case 9:
45 return 'Yandex';
46 case 10:
47 return 'Seznam.cz';
48 }
49 return $record_platform;
50 }
51
52 /**
53 * 格式化推送方式
54 * @param int $record_mode
55 * @return string
56 */
57 public static function format_record_mode($record_mode)
58 {
59 switch ($record_mode) {
60 case 1:
61 return '普通收录';
62 case 2:
63 return '快速抓取';
64 case 3:
65 return 'Js提交';
66 case 4:
67 return 'Api提交';
68 case 5:
69 return 'IndexNow';
70 }
71 return $record_mode;
72 }
73
74 /**
75 * 格式化推送结果
76 * @param int $result_status
77 * @return string
78 */
79 public static function format_result_status($result_status)
80 {
81 switch ($result_status) {
82 case 1:
83 return '成功';
84 case 2:
85 return '失败';
86 case 3:
87 return '未知';
88 }
89 return $result_status;
90 }
91
92 /**
93 * 查询指定字段的文章数据
94 * @param string $field 查询字段
95 * @param int $num 查询数量
96 * @param int $type 1 最新 2 随机 3 伪随机
97 * @param int $offset 过滤多少条数据
98 * @return array
99 */
100 public static function get_post_data($field, $num, $type, $offset = 0)
101 {
102 global $wpdb;
103 $table_name = $wpdb->prefix . 'posts';
104 $order = 'DESC';
105 $orderby = 'ID';
106 $where = '';
107 if (2 == $type) {
108 $orderby = 'rand()';
109 $order = '';
110 } else if (3 == $type) {
111 $max_id = $wpdb->get_var('SELECT MAX(ID) FROM `' . $table_name . '` where `post_status` = "publish"');
112 $order = 'ASC';
113 if ($max_id > $num) {
114 $start_id = mt_rand(0, $max_id - $num);
115 $where = '`ID` > ' . $start_id . ' AND';
116 }
117 }
118 $sql = 'SELECT ' . $field . ' FROM `' . $table_name . '` WHERE ' . $where . ' `post_status` = %s ORDER BY ' . $orderby . ' ' . $order . ' LIMIT %d, %d';
119 $query = $wpdb->prepare(
120 $sql,
121 'publish',
122 $offset,
123 $num
124 );
125 $results = $wpdb->get_results($query, ARRAY_A);
126 $data = array();
127 if (!empty($results)) {
128 foreach ($results as $result) {
129 $data[] = $result;
130 }
131 }
132 return $data;
133 }
134
135 /**
136 * 获取推送文章网址
137 * @param int $num 推送数量
138 * @param int $type 1 最新 2 随机 3 伪随机
139 * @param int $offset 过滤多少条数据
140 * @return array
141 */
142 public static function get_post_url($num, $type, $offset = 0)
143 {
144 $data = self::get_post_data('ID', $num, $type, $offset);
145 $urls = array();
146 if (!empty($data)) {
147 foreach ($data as $v) {
148 $urls[] = get_permalink($v['ID']);
149 }
150 }
151 return $urls;
152 }
153
154 /**
155 * 获取推送文章网址和推送时间
156 * @param int $num 推送数据
157 * @param int $type 1 最新 2 随机 3 伪随机
158 * @return array
159 */
160 public static function get_post_url_and_date($num, $type)
161 {
162 $data = self::get_post_data('ID,post_date', $num, $type);
163 $result = array();
164 if (!empty($data)) {
165 foreach ($data as $v) {
166 $result[] = array(
167 'url' => get_permalink($v['ID']),
168 'date' => date('Y-m-d', strtotime($v['post_date']))
169 );
170 }
171 }
172 return $result;
173 }
174
175 /**
176 * 保存推送日志记录
177 * @param $data
178 * @return bool|int|mysqli_result|resource|null
179 */
180 public static function save_record($data)
181 {
182 if (empty($data)) {
183 return false;
184 }
185 // 不保存推送记录
186 $push_record = Ggpush_Plugin::get_option('push_record', 0);
187 if (1 == $push_record) {
188 return true;
189 }
190 global $wpdb;
191 $table_name = $wpdb->prefix . 'ggpush_records';
192 if (empty($data['record_date'])) {
193 $data['record_date'] = wp_date('Y-m-d H:i:s');
194 }
195 return $wpdb->insert($table_name, $data);
196 }
197
198 /**
199 * 链接多平台推送
200 * @param string|array $url
201 * @param array|string|int $platforms
202 * @return bool|array
203 */
204 public static function platform_push($url, $platforms)
205 {
206 if (empty($url) || empty($platforms)) {
207 Ggpush_Plugin::set_error('链接为空或推送平台为空');
208 return false;
209 }
210 if (is_string($url)) {
211 $url = array($url);
212 }
213 if (is_numeric($platforms)) {
214 $platforms = array($platforms);
215 }
216 $result = array();
217 foreach ($platforms as $platform) {
218 $result[$platform] = self::single_platform_push($url, $platform);
219 }
220 return $result;
221 }
222
223 /**
224 * 链接多平台推送
225 * @param string|array $url
226 * @param string|int $platform
227 * @return bool
228 */
229 public static function single_platform_push($url, $platform)
230 {
231 if (empty($url) || empty($platform)) {
232 Ggpush_Plugin::set_error('链接为空或推送平台为空');
233 return false;
234 }
235 if (is_string($url)) {
236 $url = array($url);
237 }
238 switch ($platform) {
239 case 1:
240 // 百度搜索引擎普通收录
241 return self::push_baidu($url);
242 case 2:
243 // 百度搜索引擎快速抓取
244 return self::push_baidu($url, true);
245 case 4:
246 // �
247 应搜索引擎
248 return self::push_bing($url);
249 case 7:
250 // IndexNow
251 return self::push_indexnow($url);
252 }
253 return false;
254 }
255
256 /**
257 * 百度站长平台推送
258 * @param array $urls 推送网址
259 * @param string $site 推送网站
260 * @param string $token 推送token
261 * @param string $type 为daily则为快速推送
262 * @return array|WP_Error
263 */
264 public static function baidu_push($urls, $site, $token, $type = '')
265 {
266 $params = array(
267 'site' => $site,
268 'token' => $token
269 );
270 if (!empty($type)) {
271 $params['type'] = $type;
272 }
273 $apiUrl = 'http://data.zz.baidu.com/urls?' . http_build_query($params);
274 return wp_remote_post($apiUrl, array(
275 'body' => implode("\n", $urls),
276 'sslverify' => false,
277 'timeout' => self::get_request_timeout(),
278 'headers' => array(
279 'Content-Type' => 'text/plain'
280 )
281 ));
282 }
283
284 /**
285 * 百度推送
286 * @param array $urls
287 * @param bool $daily
288 * @return bool|int|mysqli_result|resource|null
289 */
290 public static function push_baidu($urls, $daily = false)
291 {
292 $baidu_token = Ggpush_Plugin::get_option('baidu_token');
293 if (empty($baidu_token)) {
294 Ggpush_Plugin::set_error('未设置百度推送');
295 return false;
296 }
297 $response = self::baidu_push($urls, parse_url(get_home_url(), PHP_URL_HOST), $baidu_token, $daily ? 'fastcrawl' : '');
298 if (is_wp_error($response)) {
299 $record_result_error = $response->get_error_message();
300 $record_result_status = 2;
301 $record_result = '';
302 } else {
303 $record_result = wp_remote_retrieve_body($response);
304 $record_result_error = '';
305 if (!empty($record_result)) {
306 $tmp = json_decode($record_result, true);
307 if (!empty($tmp['success']) && $tmp['success'] > 0) {
308 $record_result_status = 1;
309 } else {
310 $record_result_status = 2;
311 if (!empty($tmp['message'])) {
312 $record_result_error = $tmp['message'];
313 } else {
314 if (!empty($tmp['not_same_site'])) {
315 $record_result_error = '推送网址错误';
316 }
317 }
318 }
319 } else {
320 $record_result_status = 2;
321 }
322 }
323 $data = array(
324 'record_platform' => '1',
325 'record_mode' => $daily ? '2' : '1',
326 'record_urls' => json_encode($urls),
327 'record_num' => count($urls),
328 'record_result' => $record_result,
329 'record_result_code' => (int)wp_remote_retrieve_response_code($response),
330 'record_result_status' => $record_result_status,
331 'record_result_error' => $record_result_error,
332 );
333 Ggpush_Plugin::set_error($record_result_error);
334 return self::save_record($data);
335 }
336
337 /**
338 * bing搜索引擎推送
339 * @param array $urls 推送网址
340 * @param string $site 推送站点
341 * @param string $token 推送apikey
342 * @return array|WP_Error
343 */
344 public static function bing_push($urls, $site, $token)
345 {
346 return wp_remote_post('https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=' . $token, array(
347 'body' => json_encode(array(
348 'siteUrl' => $site,
349 'urlList' => $urls
350 )),
351 'sslverify' => false,
352 'timeout' => self::get_request_timeout(),
353 'headers' => array(
354 'Content-Type' => 'application/json; charset=utf-8'
355 )
356 ));
357 }
358
359 /**
360 * 推送链接到bing
361 * @param array $urls
362 * @return bool|int|mysqli_result|resource|null
363 */
364 public static function push_bing($urls)
365 {
366 $bing_token = Ggpush_Plugin::get_option('bing_token');
367 if (empty($bing_token)) {
368 Ggpush_Plugin::set_error('未设置�
369 应推送');
370 return false;
371 }
372 $response = self::bing_push($urls, get_home_url(), $bing_token);
373 if (is_wp_error($response)) {
374 $record_result_error = $response->get_error_message();
375 $record_result_status = 2;
376 $record_result = '';
377 } else {
378 $record_result = wp_remote_retrieve_body($response);
379 $record_result_error = '';
380 if (!empty($record_result)) {
381 $tmp = json_decode($record_result, true);
382 if (is_array($tmp) && array_key_exists('d', $tmp) && $tmp['d'] === null) {
383 $record_result_status = 1;
384 } else {
385 $record_result_status = 2;
386 if (!empty($tmp['Message'])) {
387 $record_result_error = $tmp['Message'];
388 }
389 }
390 } else {
391 $record_result_status = 2;
392 }
393 }
394 $data = array(
395 'record_platform' => '6',
396 'record_mode' => '4',
397 'record_urls' => json_encode($urls),
398 'record_num' => count($urls),
399 'record_result' => $record_result,
400 'record_result_code' => (int)wp_remote_retrieve_response_code($response),
401 'record_result_status' => $record_result_status,
402 'record_result_error' => $record_result_error
403 );
404 Ggpush_Plugin::set_error($record_result_error);
405 return self::save_record($data);
406 }
407
408 /**
409 * indexnow推送
410 * @param array $urls 推送网址
411 * @param string $host 推送域名
412 * @param string $key 推送key
413 * @param string $keyLocation 推送key文本文件位置
414 * @param string $searchengine 推送的搜索引擎,api.indexnow.org、www.bing.com、yandex.com
415 * @return array|WP_Error
416 */
417 public static function index_now_push($urls, $host, $key, $keyLocation, $searchengine)
418 {
419 $data = array(
420 'host' => $host,
421 'key' => $key,
422 'keyLocation' => $keyLocation,
423 'urlList' => $urls
424 );
425 return wp_remote_post('https://' . $searchengine . '/indexnow', array(
426 'body' => json_encode($data),
427 'sslverify' => false,
428 'timeout' => self::get_request_timeout(),
429 'headers' => array(
430 'Host' => $searchengine,
431 'Content-Type' => 'application/json; charset=utf-8'
432 )
433 ));
434 }
435
436 /**
437 * 推送链接到indexnow
438 * @param array $urls
439 * @return bool
440 */
441 public static function push_indexnow($urls)
442 {
443 $indexnow_token = Ggpush_Plugin::get_option('indexnow_token');
444 if (empty($indexnow_token) || !file_exists(ABSPATH . $indexnow_token . '.txt')) {
445 Ggpush_Plugin::set_error('未设置IndexNow推送');
446 return false;
447 }
448 $keyLocation = get_home_url() . '/' . $indexnow_token . '.txt';
449 foreach (Ggpush_Plugin::get_option('indexnow_search_engine', array()) as $v) {
450 $host = 'api.indexnow.org';
451 $record_platform = 8;
452 switch ($v) {
453 case 2:
454 $record_platform = 6;
455 $host = 'www.bing.com';
456 break;
457 case 3:
458 $record_platform = 9;
459 $host = 'yandex.com';
460 break;
461 case 4:
462 $record_platform = 10;
463 $host = 'search.seznam.cz';
464 break;
465 }
466 $response = self::index_now_push($urls, parse_url(get_home_url(), PHP_URL_HOST), Ggpush_Plugin::get_option('indexnow_token'), $keyLocation, $host);
467 $record_result_code = (int)wp_remote_retrieve_response_code($response);
468 if (is_wp_error($response)) {
469 $record_result_error = $response->get_error_message();
470 $record_result_status = 2;
471 $record_result = '';
472 } else {
473 $record_result = wp_remote_retrieve_body($response);
474 $record_result_error = '';
475 if (!empty($record_result)) {
476 $record_result = trim(strip_tags($record_result));
477 $record_result_is_arr = json_decode($record_result, true);
478 if (!empty($record_result_is_arr['message'])) {
479 $record_result_error = $record_result_is_arr['message'];
480 }
481 }
482 if ($record_result_code === 200) {
483 $record_result_status = 1;
484 } else if ($record_result_code === 202) {
485 $record_result_status = 3;
486 } else {
487 $record_result_status = 2;
488 }
489 }
490 $data = array(
491 'record_platform' => $record_platform,
492 'record_mode' => '5',
493 'record_urls' => json_encode($urls),
494 'record_num' => count($urls),
495 'record_result' => $record_result,
496 'record_result_code' => $record_result_code,
497 'record_result_status' => $record_result_status,
498 'record_result_error' => $record_result_error
499 );
500 if (!empty($record_result_error)) {
501 Ggpush_Plugin::set_error($record_result_error);
502 }
503 self::save_record($data);
504 }
505 return true;
506 }
507
508 /**
509 * 更新indexnow密钥验证文件
510 * @param string $newfile 新的密钥文件
511 * @param string $oldfile 旧的密钥文件
512 * @return bool|int
513 */
514 public static function update_indexnow_keyfile($newfile, $oldfile = '')
515 {
516 if (!empty($oldfile) && file_exists(ABSPATH . $oldfile . '.txt')) {
517 @unlink(ABSPATH . $oldfile . '.txt');
518 }
519 if (!empty($newfile)) {
520 return file_put_contents(ABSPATH . $newfile . '.txt', $newfile);
521 }
522 return true;
523 }
524
525 /**
526 * 删除indexnow的密钥文件
527 * @return void
528 */
529 public static function delete_indexnow_keyfile()
530 {
531 global $ggpush_options;
532 if (!empty($ggpush_options['indexnow_token'])) {
533 @unlink(ABSPATH . $ggpush_options['indexnow_token'] . '.txt');
534 }
535 }
536 }