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-plugin.php

class-ggpush-plugin.php in 果果推送 0.0.4, at includes/class-ggpush-plugin.php

555 lines 17.2 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_Plugin
7 {
8 // 启用插件
9 public static function plugin_activation()
10 {
11 global $wpdb;
12 $table_name = $wpdb->prefix . 'ggpush_records';
13 $charset_collate = $wpdb->get_charset_collate();
14 $sql = <<<SQL
15 CREATE TABLE {$table_name} (
16 `record_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
17 `record_platform` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT '推送平台:1 百度,2 360,3 搜狗,4 头条,5 神马,6 bing,7 谷歌,8 indexnow,9 yandex,10 Seznam.cz',
18 `record_mode` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT '推送方式:1 普通收录,2 快速收录,3 js提交,4 api提交,5 indexnow',
19 `record_urls` LONGTEXT NULL DEFAULT NULL COMMENT '推送链接' COLLATE 'utf8mb4_general_ci',
20 `record_num` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT '推送链接数量',
21 `record_result` TEXT NULL DEFAULT NULL COMMENT '推送结果' COLLATE 'utf8mb4_general_ci',
22 `record_result_code` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '200' COMMENT '推送结果状态码',
23 `record_result_status` TINYINT(3) UNSIGNED NOT NULL DEFAULT '1' COMMENT '推送状态:1 成功,2 失败,3 未知',
24 `record_result_error` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '失败原因' COLLATE 'utf8mb4_general_ci',
25 `record_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '推送时间',
26 PRIMARY KEY (`record_id`) USING BTREE
27 ) {$charset_collate};
28 SQL;
29 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
30 // 如果表不存在才会执行创建
31 maybe_create_table($table_name, $sql);
32 // 创建默认�
33 �置
34 global $ggpush_options;
35 if (empty($ggpush_options)) {
36 add_option('ggpush_options', array(
37 'menu_position' => 100,
38 'push_timeout' => 30,
39 ));
40 }
41 }
42
43 // 删除插件执行的代码
44 public static function plugin_uninstall()
45 {
46 // 删除表
47 global $wpdb;
48 global $ggpush_options;
49 $ggpush_options = get_option('ggpush_options', array());
50 $table_name = $wpdb->prefix . 'ggpush_records';
51 $wpdb->query('DROP TABLE IF EXISTS `' . $table_name . '`');
52 // 删除IndexNow密钥文件
53 Ggpush_Api::delete_indexnow_keyfile();
54 // 删除�
55 �置
56 delete_option('ggpush_options');
57 }
58
59 // 禁用插件执行的代码
60 public static function plugin_deactivation()
61 {
62 // 插件已禁用,删除定时任务
63 Ggpush_Cron::update_cron(false);
64 }
65
66 // 初始化
67 public static function admin_init()
68 {
69 // 注册设置页面
70 if (!empty($_REQUEST['page'])) {
71 if ('ggpush-timing-push' === $_REQUEST['page']) {
72 // 定时推送
73 Ggpush_Task::init_page();
74 } else if ('ggpush-settings' === $_REQUEST['page']) {
75 // 推送设置
76 Ggpush_Settings::init_page();
77 } else if ('ggpush-assist-push' === $_REQUEST['page']) {
78 // �
79 助推送
80 Ggpush_Assist::init_page();
81 }
82 }
83 }
84
85 // 添加菜单
86 public static function admin_menu()
87 {
88 global $ggpush_options;
89 $position = null;
90 if (!empty($ggpush_options['menu_position'])) {
91 $position = intval($ggpush_options['menu_position']);
92 }
93 $push_record = 0;
94 if (!empty($ggpush_options['push_record'])) {
95 $push_record = intval($ggpush_options['push_record']);
96 }
97 // 父菜单
98 add_menu_page(
99 '果果推送',
100 '果果推送',
101 'manage_options',
102 '#ggpush',
103 null,
104 'dashicons-admin-links',
105 $position
106 );
107
108 // 推送记录页面
109 if (0 === $push_record) {
110 add_submenu_page(
111 '#ggpush',
112 '推送记录',
113 '推送记录',
114 'manage_options',
115 'ggpush-record',
116 array('Ggpush_Record', 'home')
117 );
118 }
119
120 // 推送设置
121 add_submenu_page(
122 '#ggpush',
123 '推送设置',
124 '推送设置',
125 'manage_options',
126 'ggpush-settings',
127 array('Ggpush_Settings', 'show_page')
128 );
129
130 // 定时推送
131 add_submenu_page(
132 '#ggpush',
133 '定时推送',
134 '定时推送',
135 'manage_options',
136 'ggpush-timing-push',
137 array('Ggpush_Task', 'show_page')
138 );
139
140 // �
141 助推送
142 add_submenu_page(
143 '#ggpush',
144 '�
145 助推送',
146 '�
147 助推送',
148 'manage_options',
149 'ggpush-assist-push',
150 array('Ggpush_Assist', 'show_page')
151 );
152
153 remove_submenu_page('#ggpush', '#ggpush');
154 }
155
156 /**
157 * 在插件页面添加设置链接
158 * @param $links
159 * @return mixed
160 */
161 public static function setups($links)
162 {
163 $business_link = '<a href="https://www.ggdoc.cn/plugin/1.html" target="_blank">商业版</a>';
164 array_unshift($links, $business_link);
165
166 $setups = '<a href="admin.php?page=ggpush-settings">设置</a>';
167 array_unshift($links, $setups);
168
169 return $links;
170 }
171
172 /**
173 * 表单输�
174 �框回调
175 * @param array $args 这数据就是add_settings_field方法中第6个参数($args)的数据
176 */
177 public static function field_callback($args)
178 {
179 // 表单的id或name字段
180 $id = $args['label_for'];
181 // 表单的名称
182 $input_name = 'ggpush_options[' . $id . ']';
183 // 获取表单选项中的值
184 global $ggpush_options;
185 // 表单的值
186 $input_value = isset($ggpush_options[$id]) ? $ggpush_options[$id] : '';
187 // 表单的类型
188 $form_type = isset($args['form_type']) ? $args['form_type'] : 'input';
189 // 输�
190 �表单说明
191 $form_desc = isset($args['form_desc']) ? $args['form_desc'] : '';
192 // 输�
193 �表单type
194 $type = isset($args['type']) ? $args['type'] : 'text';
195 // 输�
196 �表单placeholder
197 $form_placeholder = isset($args['form_placeholder']) ? $args['form_placeholder'] : '';
198 // 下拉框等选项值
199 $form_data = isset($args['form_data']) ? $args['form_data'] : array();
200 // 扩展form表单属性
201 $form_extend = isset($args['form_extend']) ? $args['form_extend'] : array();
202 switch ($form_type) {
203 case 'input':
204 self::generate_input(
205 array_merge(
206 array(
207 'id' => $id,
208 'type' => $type,
209 'placeholder' => $form_placeholder,
210 'name' => $input_name,
211 'value' => $input_value,
212 'class' => 'regular-text',
213 ),
214 $form_extend
215 ));
216 break;
217 case 'select':
218 self::generate_select(
219 array_merge(
220 array(
221 'id' => $id,
222 'placeholder' => $form_placeholder,
223 'name' => $input_name
224 ),
225 $form_extend
226 ),
227 $form_data,
228 $input_value
229 );
230 break;
231 case 'checkbox':
232 self::generate_checkbox(
233 array_merge(
234 array(
235 'name' => $input_name . '[]',
236 'input_name' => $input_name,
237 ),
238 $form_extend
239 ),
240 $form_data,
241 $input_value
242 );
243 break;
244 case 'textarea':
245 self::generate_textarea(
246 array_merge(
247 array(
248 'id' => $id,
249 'placeholder' => $form_placeholder,
250 'name' => $input_name,
251 'class' => 'large-text code',
252 'rows' => 5,
253 ),
254 $form_extend
255 ),
256 $input_value
257 );
258 break;
259 }
260 if (!empty($form_desc)) {
261 ?>
262 <p class="description"><?php echo esc_html($form_desc); ?></p>
263 <?php
264 }
265 }
266
267 /**
268 * 生成textarea表单
269 * @param array $form_data 标签上的属性数组
270 * @param string $value 默认值
271 * @return void
272 */
273 public static function generate_textarea($form_data, $value = '')
274 {
275 ?><textarea <?php
276 foreach ($form_data as $k => $v) {
277 echo esc_attr($k); ?>="<?php echo esc_attr($v); ?>" <?php
278 } ?>><?php echo esc_textarea($value); ?></textarea>
279 <?php
280 }
281
282 /**
283 * 生成checkbox表单
284 * @param array $form_data 标签上的属性数组
285 * @param array $checkboxs 下拉列表数据
286 * @param string|array $value 选中值,单个选中字符串,多个选中数组
287 * @return void
288 */
289 public static function generate_checkbox($form_data, $checkboxs, $value = '')
290 {
291 ?>
292 <fieldset>
293 <p>
294 <input type="hidden" name="<?php echo esc_attr($form_data['input_name']); ?>" value="">
295 <?php
296 $len = count($checkboxs);
297 foreach ($checkboxs as $k => $checkbox) {
298 $checked = '';
299 if (!empty($value)) {
300 if (is_array($value)) {
301 if (in_array($checkbox['value'], $value)) {
302 $checked = 'checked';
303 }
304 } else {
305 if ($checkbox['value'] == $value) {
306 $checked = 'checked';
307 }
308 }
309 }
310 ?>
311 <label>
312 <input type="checkbox" <?php checked($checked, 'checked'); ?><?php
313 foreach ($form_data as $k2 => $v2) {
314 echo esc_attr($k2); ?>="<?php echo esc_attr($v2); ?>" <?php
315 } ?> value="<?php echo esc_attr($checkbox['value']); ?>"
316 ><?php echo esc_html($checkbox['title']); ?>
317 </label>
318 <?php
319 if ($k < ($len - 1)) {
320 ?>
321 <br>
322 <?php
323 }
324 }
325 ?>
326 </p>
327 </fieldset>
328 <?php
329 }
330
331 /**
332 * 生成input表单
333 * @param array $form_data 标签上的属性数组
334 * @return void
335 */
336 public static function generate_input($form_data)
337 {
338 ?><input <?php
339 foreach ($form_data as $k => $v) {
340 echo esc_attr($k); ?>="<?php echo esc_attr($v); ?>" <?php
341 } ?>><?php
342 }
343
344 /**
345 * 生成select表单
346 * @param array $form_data 标签上的属性数组
347 * @param array $selects 下拉列表数据
348 * @param string|array $value 选中值,单个选中字符串,多个选中数组
349 * @return void
350 */
351 public static function generate_select($form_data, $selects, $value = '')
352 {
353 ?><select <?php
354 foreach ($form_data as $k => $v) {
355 echo esc_attr($k); ?>="<?php echo esc_attr($v); ?>" <?php
356 } ?>><?php
357 foreach ($selects as $select) {
358 $selected = '';
359 if (!empty($value)) {
360 if (is_array($value)) {
361 if (in_array($select['value'], $value)) {
362 $selected = 'selected';
363 }
364 } else {
365 if ($select['value'] == $value) {
366 $selected = 'selected';
367 }
368 }
369 }
370 ?>
371 <option <?php selected($selected, 'selected'); ?>
372 value="<?php echo esc_attr($select['value']); ?>"><?php echo esc_html($select['title']); ?></option>
373 <?php
374 }
375 ?>
376 </select>
377 <?php
378 }
379
380 /**
381 * 统一设置保存变量
382 * @param $input
383 * @return array
384 */
385 public static function sanitize($input)
386 {
387 global $ggpush_options;
388 if (empty($input)) {
389 return $ggpush_options;
390 }
391 // 更新indexnow密钥文件
392 if (isset($input['indexnow_token'])) {
393 Ggpush_Api::update_indexnow_keyfile($input['indexnow_token'], isset($ggpush_options['indexnow_token']) ? $ggpush_options['indexnow_token'] : '');
394 }
395 if (empty($ggpush_options)) {
396 return $input;
397 } else {
398 return array_merge($ggpush_options, $input);
399 }
400 }
401
402 /**
403 * 更新�
404 �置
405 * @param array $options
406 * @return void
407 */
408 public static function update_options($options)
409 {
410 global $ggpush_options;
411 $ggpush_options = self::sanitize($options);
412 update_option('ggpush_options', $ggpush_options);
413 }
414
415
416 /**
417 * 获取存储的设置值
418 * @param string $option 设置的键
419 * @param mixed $def_value 默认值
420 * @return mixed|string
421 */
422 public static function get_option($option, $def_value = '')
423 {
424 global $ggpush_options;
425 if (!empty($ggpush_options[$option])) {
426 return $ggpush_options[$option];
427 }
428 return $def_value;
429 }
430
431 /**
432 * 获取GET中的参数值
433 * @param string $name
434 * @param mixed $defValue
435 * @return mixed
436 */
437 public static function get($name, $defValue = '')
438 {
439 if (isset($_GET[$name])) {
440 return $_GET[$name];
441 }
442 return $defValue;
443 }
444
445 /**
446 * 设置上一次错误信息
447 * @param string $error
448 * @return void
449 */
450 public static function set_error($error)
451 {
452 global $ggpush_error;
453 $ggpush_error = $error;
454 }
455
456 /**
457 * 获取上一次的错误信息
458 * @return string
459 */
460 public static function get_error()
461 {
462 global $ggpush_error;
463 return $ggpush_error;
464 }
465
466 /**
467 * 添加JavaScript文件
468 * @return void
469 */
470 public static function admin_enqueue_scripts()
471 {
472 global $ggpush_options;
473 // 发布文章后推送
474 if (!empty($ggpush_options['publish_article_platform'])) {
475 self::push_after_post();
476 }
477 }
478
479 /**
480 * 发布文章后推送处理的js
481 * @return void
482 */
483 public static function push_after_post()
484 {
485 global $pagenow;
486 if (!empty($pagenow)) {
487 // 发布文章后推送
488 if ('post.php' === $pagenow) {
489 // 添加静态文件
490 wp_enqueue_script(
491 'ggpush-sync',
492 plugins_url('/js/ggpush_publish.min.js', GGPUSH_PLUGIN_FILE),
493 array('jquery'),
494 '0.0.4',
495 true
496 );
497 wp_localize_script(
498 'ggpush-sync',
499 'ggpush_obj',
500 array(
501 'ajax_url' => admin_url('admin-ajax.php'),
502 'nonce' => wp_create_nonce('ggpush'),
503 )
504 );
505 } else if ('post-new.php' === $pagenow) {
506 global $post;
507 $post_id = 0;
508 if (!empty($post->ID)) {
509 $post_id = $post->ID;
510 } else if (!empty($_GET['post'])) {
511 $post_id = intval($_GET['post']);
512 }
513 set_transient('ggpush_publish_post_id', $post_id);
514 }
515 }
516 }
517
518 /**
519 * 发布文章后推送
520 * @return void
521 */
522 public static function wp_ajax_ggpush_publish()
523 {
524 check_ajax_referer('ggpush');
525 global $ggpush_options;
526 if (empty($ggpush_options['publish_article_platform'])) {
527 wp_send_json(array(
528 'status' => 0,
529 'msg' => '未开启发布文章后推送',
530 ));
531 }
532 $post_id = get_transient('ggpush_publish_post_id');
533 if (empty($post_id)) {
534 wp_send_json(array(
535 'status' => 0,
536 'msg' => '非发布的新文章',
537 ));
538 }
539 if (!empty($post_id) && !wp_is_post_revision($post_id) && 'publish' === get_post_status($post_id)) {
540 // 推送链接
541 $post_url = get_permalink($post_id);
542 Ggpush_Api::platform_push($post_url, $ggpush_options['publish_article_platform']);
543 delete_transient('ggpush_publish_post_id');
544 wp_send_json(array(
545 'url' => $post_url,
546 'status' => 1,
547 'msg' => '链接已推送',
548 ));
549 }
550 wp_send_json(array(
551 'status' => 0,
552 'msg' => '链接推送失败',
553 ));
554 }
555 }