-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
105 lines (92 loc) · 2.73 KB
/
Plugin.php
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
<?php
namespace TypechoPlugin\BarkPush;
use Typecho\Plugin\PluginInterface;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Form\Element\Text;
use Typecho\Widget\Helper\Form\Element\Radio;
use Widget\Options;
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 基于Bark的评论消息推送插件
*
* @package BarkPush
* @author chenai
* @version 1.0.0
* @link https://github.com/kindevil/BarkPush
*/
class Plugin implements PluginInterface{
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*/
public static function activate() {
\Typecho\Plugin::factory('Widget_Feedback')->finishComment = array('BarkPush_Plugin', 'render');
return '插件启用成功,请设置Bark服务器和Key';
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*/
public static function deactivate(){//插件禁用方法
return '禁用成功';
}
/**
* 获取插件配置面板
*
* @param Form $form 配置面板
*/
public static function config(Form $form){
$serverAddr = new Text('server',null,'','服务器地址');
$form->addInput($serverAddr);
$key = new Text('key',null,'','KEY');
$form->addInput($key);
$push = new Radio('push',
array(1=>_t('发送'),0=>_t('不发送')),0,_t('用户登录之后的评论否发送通知'),_t('选择“否”之后用户登录后台之后发出的评论不会推送通知到APP'));
$form->addInput($push);
}
/**
* 个人用户的配置面板
*
* @param Form $form
*/
public static function personalConfig(Form $form)
{
}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function render($comment){
$config = Options::alloc();
//判断用户是否登录,且插件是否设置为用户登录之后不推送通知
if (\Widget\User::alloc()->hasLogin() && $config->plugin('BarkPush')->push == 0) {
return "";
}
$server = $config->plugin('BarkPush')->server;
$server = $server.'/push';
$key = $config->plugin('BarkPush')->key;
$body = $comment->author." 在文章《".$comment->title."》评论说:".$comment->text;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $server,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"body": "'.$body.'",
"device_key": "'.$key.'",
"title": "评论通知",
"badge": 1,
"category": "评论通知",
"sound": "minuet.caf",
"icon": "https://kindevil.com/favicon.ico",
"group": "评论通知",
"url": "'.$comment->permalink.'"
}',
CURLOPT_HTTPHEADER => [
'Content-Type: application/json; charset=utf-8',
],
]);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
}