PluginProbe
果果推送 / 0.0.4
果果推送 v0.0.4
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 果果推送 0.0.4, at includes/class-ggpush-api.php

533 lines 16.8 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 return $wpdb->insert($table_name, $data);
193 }
194
195 /**
196 * 链接多平台推送
197 * @param string|array $url
198 * @param array|string|int $platforms
199 * @return bool|array
200 */
201 public static function platform_push($url, $platforms)
202 {
203 if (empty($url) || empty($platforms)) {
204 Ggpush_Plugin::set_error('链接为空或推送平台为空');
205 return false;
206 }
207 if (is_string($url)) {
208 $url = array($url);
209 }
210 if (is_numeric($platforms)) {
211 $platforms = array($platforms);
212 }
213 $result = array();
214 foreach ($platforms as $platform) {
215 $result[$platform] = self::single_platform_push($url, $platform);
216 }
217 return $result;
218 }
219
220 /**
221 * 链接多平台推送
222 * @param string|array $url
223 * @param string|int $platform
224 * @return bool
225 */
226 public static function single_platform_push($url, $platform)
227 {
228 if (empty($url) || empty($platform)) {
229 Ggpush_Plugin::set_error('链接为空或推送平台为空');
230 return false;
231 }
232 if (is_string($url)) {
233 $url = array($url);
234 }
235 switch ($platform) {
236 case 1:
237 // 百度搜索引擎普通收录
238 return self::push_baidu($url);
239 case 2:
240 // 百度搜索引擎快速收录
241 return self::push_baidu($url, true);
242 case 4:
243 // �
244 应搜索引擎
245 return self::push_bing($url);
246 case 7:
247 // IndexNow
248 return self::push_indexnow($url);
249 }
250 return false;
251 }
252
253 /**
254 * 百度站长平台推送
255 * @param array $urls 推送网址
256 * @param string $site 推送网站
257 * @param string $token 推送token
258 * @param string $type 为daily则为快速推送
259 * @return array|WP_Error
260 */
261 public static function baidu_push($urls, $site, $token, $type = '')
262 {
263 $params = array(
264 'site' => $site,
265 'token' => $token
266 );
267 if (!empty($type)) {
268 $params['type'] = $type;
269 }
270 $apiUrl = 'http://data.zz.baidu.com/urls?' . http_build_query($params);
271 return wp_remote_post($apiUrl, array(
272 'body' => implode("\n", $urls),
273 'sslverify' => false,
274 'timeout' => self::get_request_timeout(),
275 'headers' => array(
276 'Content-Type' => 'text/plain'
277 )
278 ));
279 }
280
281 /**
282 * 百度推送
283 * @param array $urls
284 * @param bool $daily
285 * @return bool|int|mysqli_result|resource|null
286 */
287 public static function push_baidu($urls, $daily = false)
288 {
289 $baidu_token = Ggpush_Plugin::get_option('baidu_token');
290 if (empty($baidu_token)) {
291 Ggpush_Plugin::set_error('未设置百度推送');
292 return false;
293 }
294 $response = self::baidu_push($urls, parse_url(get_home_url(), PHP_URL_HOST), $baidu_token, $daily ? 'daily' : '');
295 if (is_wp_error($response)) {
296 $record_result_error = $response->get_error_message();
297 $record_result_status = 2;
298 $record_result = '';
299 } else {
300 $record_result = wp_remote_retrieve_body($response);
301 $record_result_error = '';
302 if (!empty($record_result)) {
303 $tmp = json_decode($record_result, true);
304 if (!empty($tmp['success']) && $tmp['success'] > 0) {
305 $record_result_status = 1;
306 } else {
307 $record_result_status = 2;
308 if (!empty($tmp['message'])) {
309 $record_result_error = $tmp['message'];
310 } else {
311 if (!empty($tmp['not_same_site'])) {
312 $record_result_error = '推送网址错误';
313 }
314 }
315 }
316 } else {
317 $record_result_status = 2;
318 }
319 }
320 $data = array(
321 'record_platform' => '1',
322 'record_mode' => $daily ? '2' : '1',
323 'record_urls' => json_encode($urls),
324 'record_num' => count($urls),
325 'record_result' => $record_result,
326 'record_result_code' => (int)wp_remote_retrieve_response_code($response),
327 'record_result_status' => $record_result_status,
328 'record_result_error' => $record_result_error,
329 );
330 Ggpush_Plugin::set_error($record_result_error);
331 return self::save_record($data);
332 }
333
334 /**
335 * bing搜索引擎推送
336 * @param array $urls 推送网址
337 * @param string $site 推送站点
338 * @param string $token 推送apikey
339 * @return array|WP_Error
340 */
341 public static function bing_push($urls, $site, $token)
342 {
343 return wp_remote_post('https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=' . $token, array(
344 'body' => json_encode(array(
345 'siteUrl' => $site,
346 'urlList' => $urls
347 )),
348 'sslverify' => false,
349 'timeout' => self::get_request_timeout(),
350 'headers' => array(
351 'Content-Type' => 'application/json; charset=utf-8'
352 )
353 ));
354 }
355
356 /**
357 * 推送链接到bing
358 * @param array $urls
359 * @return bool|int|mysqli_result|resource|null
360 */
361 public static function push_bing($urls)
362 {
363 $bing_token = Ggpush_Plugin::get_option('bing_token');
364 if (empty($bing_token)) {
365 Ggpush_Plugin::set_error('未设置�
366 应推送');
367 return false;
368 }
369 $response = self::bing_push($urls, get_home_url(), $bing_token);
370 if (is_wp_error($response)) {
371 $record_result_error = $response->get_error_message();
372 $record_result_status = 2;
373 $record_result = '';
374 } else {
375 $record_result = wp_remote_retrieve_body($response);
376 $record_result_error = '';
377 if (!empty($record_result)) {
378 $tmp = json_decode($record_result, true);
379 if (is_array($tmp) && array_key_exists('d', $tmp) && $tmp['d'] === null) {
380 $record_result_status = 1;
381 } else {
382 $record_result_status = 2;
383 if (!empty($tmp['Message'])) {
384 $record_result_error = $tmp['Message'];
385 }
386 }
387 } else {
388 $record_result_status = 2;
389 }
390 }
391 $data = array(
392 'record_platform' => '6',
393 'record_mode' => '4',
394 'record_urls' => json_encode($urls),
395 'record_num' => count($urls),
396 'record_result' => $record_result,
397 'record_result_code' => (int)wp_remote_retrieve_response_code($response),
398 'record_result_status' => $record_result_status,
399 'record_result_error' => $record_result_error
400 );
401 Ggpush_Plugin::set_error($record_result_error);
402 return self::save_record($data);
403 }
404
405 /**
406 * indexnow推送
407 * @param array $urls 推送网址
408 * @param string $host 推送域名
409 * @param string $key 推送key
410 * @param string $keyLocation 推送key文本文件位置
411 * @param string $searchengine 推送的搜索引擎,api.indexnow.org、www.bing.com、yandex.com
412 * @return array|WP_Error
413 */
414 public static function index_now_push($urls, $host, $key, $keyLocation, $searchengine)
415 {
416 $data = array(
417 'host' => $host,
418 'key' => $key,
419 'keyLocation' => $keyLocation,
420 'urlList' => $urls
421 );
422 return wp_remote_post('https://' . $searchengine . '/indexnow', array(
423 'body' => json_encode($data),
424 'sslverify' => false,
425 'timeout' => self::get_request_timeout(),
426 'headers' => array(
427 'Host' => $searchengine,
428 'Content-Type' => 'application/json; charset=utf-8'
429 )
430 ));
431 }
432
433 /**
434 * 推送链接到indexnow
435 * @param array $urls
436 * @return bool
437 */
438 public static function push_indexnow($urls)
439 {
440 $indexnow_token = Ggpush_Plugin::get_option('indexnow_token');
441 if (empty($indexnow_token) || !file_exists(ABSPATH . $indexnow_token . '.txt')) {
442 Ggpush_Plugin::set_error('未设置IndexNow推送');
443 return false;
444 }
445 $keyLocation = get_home_url() . '/' . $indexnow_token . '.txt';
446 foreach (Ggpush_Plugin::get_option('indexnow_search_engine', array()) as $v) {
447 $host = 'api.indexnow.org';
448 $record_platform = 8;
449 switch ($v) {
450 case 2:
451 $record_platform = 6;
452 $host = 'www.bing.com';
453 break;
454 case 3:
455 $record_platform = 9;
456 $host = 'yandex.com';
457 break;
458 case 4:
459 $record_platform = 10;
460 $host = 'search.seznam.cz';
461 break;
462 }
463 $response = self::index_now_push($urls, parse_url(get_home_url(), PHP_URL_HOST), Ggpush_Plugin::get_option('indexnow_token'), $keyLocation, $host);
464 $record_result_code = (int)wp_remote_retrieve_response_code($response);
465 if (is_wp_error($response)) {
466 $record_result_error = $response->get_error_message();
467 $record_result_status = 2;
468 $record_result = '';
469 } else {
470 $record_result = wp_remote_retrieve_body($response);
471 $record_result_error = '';
472 if (!empty($record_result)) {
473 $record_result = trim(strip_tags($record_result));
474 $record_result_is_arr = json_decode($record_result, true);
475 if (!empty($record_result_is_arr['message'])) {
476 $record_result_error = $record_result_is_arr['message'];
477 }
478 }
479 if ($record_result_code === 200) {
480 $record_result_status = 1;
481 } else if ($record_result_code === 202) {
482 $record_result_status = 3;
483 } else {
484 $record_result_status = 2;
485 }
486 }
487 $data = array(
488 'record_platform' => $record_platform,
489 'record_mode' => '5',
490 'record_urls' => json_encode($urls),
491 'record_num' => count($urls),
492 'record_result' => $record_result,
493 'record_result_code' => $record_result_code,
494 'record_result_status' => $record_result_status,
495 'record_result_error' => $record_result_error
496 );
497 if (!empty($record_result_error)) {
498 Ggpush_Plugin::set_error($record_result_error);
499 }
500 self::save_record($data);
501 }
502 return true;
503 }
504
505 /**
506 * 更新indexnow密钥验证文件
507 * @param string $newfile 新的密钥文件
508 * @param string $oldfile 旧的密钥文件
509 * @return bool|int
510 */
511 public static function update_indexnow_keyfile($newfile, $oldfile = '')
512 {
513 if (!empty($oldfile) && file_exists(ABSPATH . $oldfile . '.txt')) {
514 @unlink(ABSPATH . $oldfile . '.txt');
515 }
516 if (!empty($newfile)) {
517 return file_put_contents(ABSPATH . $newfile . '.txt', $newfile);
518 }
519 return true;
520 }
521
522 /**
523 * 删除indexnow的密钥文件
524 * @return void
525 */
526 public static function delete_indexnow_keyfile()
527 {
528 global $ggpush_options;
529 if (!empty($ggpush_options['indexnow_token'])) {
530 @unlink(ABSPATH . $ggpush_options['indexnow_token'] . '.txt');
531 }
532 }
533 }