/
wechat-manager-functions.php
308 lines (287 loc) · 9.9 KB
/
wechat-manager-functions.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
if (!function_exists('get_post_excerpt')) {
//获取日志摘要
function get_post_excerpt($post)
{
$post_excerpt = strip_tags($post->post_excerpt);
if (!$post_excerpt) {
$post_excerpt = mb_substr(trim(strip_tags($post->post_content)), 0, 120);
}
return $post_excerpt;
}
}
if (!function_exists('get_post_first_image')) {
function get_post_first_image($post_content)
{
preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode($post_content), $matches);
if ($matches) {
return $matches[1][0];
} else {
return false;
}
}
}
// 判断当前用户操作是否在微信内置浏览器中
if (!function_exists('is_weixin')) {
function is_weixin()
{
if (isset($_SERVER['HTTP_USER_AGENT'])) {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Windows Phone') !== false) {
return true;
}
}
return false;
}
}
//获取博客文章
function wm_query_posts($q, $s = "")
{
global $wp_query;
$articles = array();
$query_base = array(
'ignore_sticky_posts' => true,
'posts_per_page' => POSTNUM,
'post_status' => 'publish',
);
if (empty($s)) {
switch ($q) {
case "c7":
$query_more = array(
"order" => "DESC",
"orderby" => "comment_count",
'date_query' => array(
array(
'after' => '1 week ago',
),
),
);
break;
case "c30":
$query_more = array(
"order" => "DESC",
"orderby" => "comment_count",
'date_query' => array(
array(
'after' => '1 month ago',
),
),
);
break;
case "c365":
$query_more = array(
"order" => "DESC",
"orderby" => "comment_count",
'date_query' => array(
array(
'after' => '1 year ago',
),
),
);
break;
case "c":
$query_more = array(
"order" => "DESC",
"orderby" => "comment_count",
);
break;
case "n":
$query_more = array(
"order" => "DESC",
"orderby" => "date",
);
break;
case "r":
$query_more = array(
"orderby" => "rand",
);
break;
default:
$query_more = array();
break;
}
} else {
$query_more = array(
's' => $s,
);
}
$weixin_query_array = array_merge($query_base, $query_more);
$weixin_query_array = apply_filters('weixin_query', $weixin_query_array);
$wp_query->query($weixin_query_array);
if (have_posts()) {
while (have_posts()) {
the_post();
global $post;
$title = get_the_title();
$excerpt = get_post_excerpt($post);
$thumbnail_id = get_post_thumbnail_id($post->ID);
if ($thumbnail_id) {
$thumb = wp_get_attachment_image_src($thumbnail_id, 'full');
$thumb = $thumb[0];
} else {
$thumb = get_post_first_image($post->post_content);
}
global $wm_thumb;
if (empty($thumb) && !empty($wm_thumb)) {
$thumb = $wm_thumb;
}
$link = get_permalink();
$articles[] = array($title, $excerpt, $link, $thumb);
}
}
return $articles;
}
function send_post(WeChat $object, $type = '', $value = '')
{
$articles = wm_query_posts($type, $value);
if (empty($articles)) {
$no_post = wm_get_setting('no_post') ?: '暂无相关文章';
$object->sendText($no_post);
}
foreach ($articles as $v) {
$object->addNews($v['0'], $v['1'], $v['2'], $v['3']);
}
$object->sendNews();
}
function wm_query_posts_by_ids($ids)
{
global $wp_query;
$articles = array();
$query_base = array(
'ignore_sticky_posts' => true,
'posts_per_page' => POSTNUM,
'post_status' => 'publish',
);
$idArr = explode(',', $ids);
$query_base['post__in'] = $idArr;
$weixin_query_array = apply_filters('weixin_query', $query_base);
$wp_query->query($weixin_query_array);
if (have_posts()) {
while (have_posts()) {
the_post();
global $post;
$title = get_the_title();
$excerpt = get_post_excerpt($post);
$thumbnail_id = get_post_thumbnail_id($post->ID);
if ($thumbnail_id) {
$thumb = wp_get_attachment_image_src($thumbnail_id, 'full');
$thumb = $thumb[0];
} else {
$thumb = get_post_first_image($post->post_content);
}
global $wm_thumb;
if (empty($thumb) && !empty($wm_thumb)) {
$thumb = $wm_thumb;
}
$link = get_permalink();
$articles[] = array($title, $excerpt, $link, $thumb);
}
}
return $articles;
}
function send_post_by_ids(Wechat $object, $ids)
{
$articles = wm_query_posts_by_ids($ids);
if (empty($articles)) {
$no_post = wm_get_setting('no_post') ?: '暂无相关文章';
$object->sendText($no_post);
}
foreach ($articles as $v) {
$object->addNews($v['0'], $v['1'], $v['2'], $v['3']);
}
$object->sendNews();
}
//英汉互译
function wm_translate($q)
{
/*global $wm_bdak;
if(empty($wm_bdak)){
$content = "请先在后台配置 百度API密钥 后使用";
return $content;
}
$url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id={$wm_bdak}&q={$value}&from=auto&to=auto";
$content = json_decode(file_get_contents($url));
$content = $content->trans_result;
$content = $content[0]->dst;
return $content;*/
global $wm_translate_appid, $wm_translate_key;
if (empty($wm_translate_appid) || empty($wm_translate_key)) {
$content = "请先在后台配置 百度翻译APPID和KEY 后使用";
return $content;
}
if (empty($q)) return false;
// $q = filter_allowed_words($q);
$salt = rand(1, 999);
$sign = md5($wm_translate_appid . $q . $salt . $wm_translate_key);
$url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?q=' . $q . '&appid=' . $wm_translate_appid . '&salt=' . $salt . '&from=auto&to=auto&sign=' . $sign;
$result = json_decode(file_get_contents($url), true);
$dst = $result['trans_result'][0]['dst'];
return $dst ? $dst : false;
}
//天气查询
function wm_weather($city)
{
global $wm_bdak;
if (empty($wm_bdak)) {
$content = "请先在后台配置 百度API密钥 后使用";
return $content;
}
$json = json_decode(file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location={$city}&output=json&ak={$wm_bdak}"));
$result = $json->results[0]->weather_data;
if (!is_array($result)) {
$content = "找不到与 {$city} 有关的天气信息,请换一个关键词试试";
return $content;
}
for ($i = 0; $i < 4; $i++) {
$date = $result[$i]->date;
$array[$i]['pic'] = $result[$i]->dayPictureUrl;
$weather = $result[$i]->weather;
$wind = $result[$i]->wind;
$temp = $result[$i]->temperature;
$array[$i]['title'] = $date . " 天气:" . $weather . " 温度:" . $temp . " 风力:" . $wind;
if ($i == 0) {
$array[$i]['pic'] = WECHAT_MANAGER_STATIC . "/include/weather.jpg";
$array[$i]['title'] = $city . " " . $array[$i]['title'];
}
}
return $array;
}
function wm_get_setting($opt)
{
$options = wm_get_options();
return $options[$opt] ? $options[$opt] : null;
}
function wm_get_options()
{
if (!$options = wp_cache_get('options_setting', 'wm_options')) {
$options = get_option('wm_options');
wp_cache_add('options_setting', $options, 'wm_options');
}
return $options;
}
function wm_get_access_token()
{
if (wm_get_setting('appid') && wm_get_setting('appsecret')) {
$wm_access_token = get_transient('wm_access_token');
if ($wm_access_token === false) {
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . wm_get_setting('appid') . '&secret=' . wm_get_setting('appsecret');
$wm_access_token = wp_remote_get($url, array('sslverify' => false));
if (is_wp_error($wm_access_token)) {
echo '<div class="wrap"><div class="updated"><p>' . $wm_access_token->get_error_code() . ':' . $wm_access_token->get_error_message() . '</p></div></div>';
exit;
}
$wm_access_token = json_decode($wm_access_token['body'], true);
if (isset($wm_access_token['access_token'])) {
set_transient('wm_access_token', $wm_access_token['access_token'], $wm_access_token['expires_in']);
return $wm_access_token['access_token'];
} else {
echo '<div class="wrap"><div class="updated"><p>错误代码:' . $wm_access_token['errcode'] . ',信息:' . $wm_access_token['errmsg'] . '</p></div></div>';
return;
}
} else {
return $wm_access_token;
}
} else {
echo '<div class="wrap"><div class="updated"><p>请先设置 AppID 与 AppSecret</p></div></div>';
return;
}
}