/
wechatManager.php
175 lines (164 loc) · 6.06 KB
/
wechatManager.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
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
<?php
/*
Plugin Name: Wechat Manager 微信公众平台管家
Plugin URI: http://9iphp.com/opensystem/wordpress/1043.html
Description: 通过简单配置,可在微信中获得博客的最新文章、热点文章,实现搜索功能,并且添加了天气查询与英汉互译两个小工具,后续会根据需求添加更多小工具
Version: 2.1
Author: Specs
Author URI: http://9iphp.com
*/
$siteurl = get_option('siteurl');
define('WECHAT_MANAGER_FOLDER', dirname(plugin_basename(__FILE__)));
define('WECHAT_MANAGER_STATIC', $siteurl . '/wp-content/plugins/' . WECHAT_MANAGER_FOLDER);
define('WECHAT_MANAGER_PLUGIN_URL', plugins_url('', __FILE__));
define('WECHAT_MANAGER_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . dirname(plugin_basename(__FILE__)));
define('WECHAT_MANAGER_PLUGIN_FILE', __FILE__);
include(WECHAT_MANAGER_PLUGIN_DIR . '/wechat-manager-class.php'); // 微信类库
include(WECHAT_MANAGER_PLUGIN_DIR . '/wechat-manager-functions.php'); //函数库
include(WECHAT_MANAGER_PLUGIN_DIR . '/wechat-manager-hook.php'); //
include(WECHAT_MANAGER_PLUGIN_DIR . '/wechat-manager-user.php');
include(WECHAT_MANAGER_PLUGIN_DIR . '/wechat-manager-option.php');
define("TOKEN", wm_get_setting('token')); //TOKEN值
define("POSTNUM", wm_get_setting('post_num')); //TOKEN值
$wm_thumb = wm_get_setting('thumb');
$wm_bdak = wm_get_setting('bd_key');
$wm_translate_appid = wm_get_setting('trans_appid');
$wm_translate_key = wm_get_setting('trans_key');
add_action('pre_get_posts', 'wm_preprocess', 4);
/**
* 预处理函数
*
* @param $wp_query
*/
function wm_preprocess($wp_query)
{
global $object;
if (!isset($object)) {
//创建一个WeChat类的实例, 回调函数名称为"onMessage",即消息处理函数
$object = new WeChat(TOKEN, "onMessage");
$object->process(); //处理消息
return;
}
}
/**
* 消息处理函数
*
* @param WeChat $object
* @param string $messageType
* @param string $content
* @param string $arg1
* @param string $arg2
*/
function onMessage(WeChat $object, $messageType, $content, $arg1, $arg2)
{
$user = new User($object->fromUser); //创建一个用户
//处理subscribe和unsubscribe消息
switch ($messageType) {
case "subscribe": //当用户关注
global $wm_welcome;
$object->addNews('感谢关注', "", "", "");
$object->addNews(wm_get_setting('welcome'), "", "", "");
$object->sendNews();
break;
case "unsubscribe": //当用户取消关注
$user->delete();
break;
case "text":
$keyword = strtolower(trim($content));
switch ($keyword) {
case wm_get_setting('post_new'):
send_post($object, 'n');
break;
case wm_get_setting('post_comment_week'):
send_post($object, 'c7');
break;
case wm_get_setting('post_comment_month'):
send_post($object, 'c30');
break;
case wm_get_setting('post_comment_year'):
send_post($object, 'c365');
break;
case wm_get_setting('post_comment_all'):
send_post($object, 'c');
break;
case wm_get_setting('post_rand'):
send_post($object, 'r');
break;
case wm_get_setting('card'):
$object->addNews('微信贺卡', "定制自己的贺卡发送给亲朋好友\n\n点击图文开始制作吧!", "http://9iphp.com/card/", WECHAT_MANAGER_STATIC . "/include/weixin-card.jpg");
$object->sendNews();
break;
case "help":
case "h":
case "?":
case "?":
$object->sendText(wm_get_setting('welcome'));
// $object->addNews('帮助信息',"","","");
// $object->addNews(wm_get_setting('welcome'),"","","");
// $object->sendNews();
break;
default:
switchFunc($object, $keyword);
break;
}
break;
default:
$object->sendText("暂无设置此功能,你可以到我们的<a href='http://wx.wsq.qq.com/177325859'>微社区</a>提交相关问题"); //否则,显示出错信息
}
}
/**
* 自定义回复
*
* @param WeChat $object
* @param string $keyword
*/
function switchFunc(WeChat $object, $keyword)
{
global $wpdb;
//关键词回复
$cacheKey = 'wechat-manager-custom-reply-key';
if (!$cacheVal = wp_cache_get($cacheKey)) {
$message_table = $wpdb->prefix . "wx_message";
$sql = 'SELECT * FROM ' . $message_table . ' ORDER BY id DESC';
$results = $wpdb->get_results($sql, ARRAY_A);
foreach ($results as $r) {
$cacheVal[$r['keyword']] = $r;
}
wp_cache_set($cacheKey, $cacheVal);
}
if ($cacheVal && in_array($keyword, array_keys($cacheVal))) {
$reply = $cacheVal[$keyword];
switch ($reply['type']) {
case 'post':
send_post_by_ids($object, $reply['content']);
break;
default:
$object->sendText(stripslashes($reply['content']));
break;
}
}
$matches = explode("@", $keyword);
$key = $matches[0];
$value = $matches[1];
switch ($key) {
case "翻译":
$content = wm_translate($value);
$object->sendText($content);
break;
case "天气":
$content = wm_weather($value);
if (!is_array($content)) {
$object->sendText($content);
} else {
foreach ($content as $v) {
$object->addNews($v['title'], "", "", $v['pic']);
}
$object->sendNews();
}
break;
default:
send_post($object, '', $key);
break;
}
}
?>