This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsharing-page.php
389 lines (351 loc) · 15 KB
/
sharing-page.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
if (!defined('ABSPATH')) {
exit();
}
require_once 'wechat.php';
class Bosima_WeChat_Page_Sharing_Page
{
private static $initiated = false;
public static function init()
{
if (!self::$initiated) {
self::$initiated = true;
require_once(BOSIMA_WECHAT_PAGE_SHARING__PLUGIN_DIR . 'sharing-ajax.php');
Bosima_WeChat_Page_Sharing_Ajax::init();
}
}
/**
* 获取文章的缩略图
*/
private static function get_post_img_url($post_content, $config)
{
$post_thumbnail = '';
if (empty($config->sharing_img_option)) {
$config->sharing_img_option = "0";
}
if ($config->sharing_img_option == "1" && has_post_thumbnail()) {
$post_thumbnail = get_the_post_thumbnail_url(null, 'thumbnail');
}
if ($config->sharing_img_option == "0" || empty($post_thumbnail)) {
$output = preg_match_all('/<img[^>]+?src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches);
if (isset($matches[1][0]) && !empty($matches[1][0])) {
$post_thumbnail = $matches[1][0];
}
}
if (empty($post_thumbnail)) {
$post_thumbnail = plugins_url('', __FILE__) . '/images/random/article' . rand(1, 5) . '.jpg';
}
return $post_thumbnail;
}
/**
* 判断字符串结尾
*/
public static function endWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
/**
* 获取当前页面Url
*/
private static function curPageURL()
{
$current_url = 'http://';
$current_port = '';
if ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) {
$current_url = 'https://';
if ($_SERVER['SERVER_PORT'] != '443') {
$current_port = $_SERVER['SERVER_PORT'];
}
}
if ($current_url == 'http://' && $_SERVER['SERVER_PORT'] != '80') {
$current_port = $_SERVER['SERVER_PORT'];
}
$host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
$current_url .= $host. $_SERVER['REQUEST_URI'];
return $current_url;
}
/**
* 输出微信分享配置JS
*/
public static function render_config_js($config)
{
global $post, $posts;
global $wp;
$share_title = '';
$default_share_title = get_bloginfo('name');
$share_img_url = '';
$share_desc = '';
$default_share_desc = "想知道【" . get_bloginfo('name') . "】的更多内容吗?现在就点我吧。";
$default_img_url = plugins_url('', __FILE__) . '/images/random/article' . rand(1, 5) . '.jpg';
$use_icon_as_share_img = '0';
$site_lang = explode('_', get_locale())[0];
if (is_single()) {
$default_share_title = $post->post_title;
$share_img_url = Bosima_WeChat_Page_Sharing_Page::get_post_img_url($post->post_content, $config);
$default_share_desc = wp_strip_all_tags($post->post_excerpt);
$description_template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description'),
'{post_title}' => $post->post_title,
'{post_excerpt}' => $default_share_desc
);
if (!empty($config->sharing_single_description)) {
$share_desc = strtr($config->sharing_single_description, $description_template_vars);
}
$title_template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description'),
'{post_title}' => $post->post_title,
);
if (!empty($config->sharing_single_title)) {
$share_title = strtr($config->sharing_single_title, $title_template_vars);
}
} elseif (is_page()) {
$template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description'),
'{post_title}' => $post->post_title
);
$share_img_url = Bosima_WeChat_Page_Sharing_Page::get_post_img_url($post->post_content, $config);
$default_share_title = $post->post_title . ' | ' . get_bloginfo('name');
$default_share_desc = "想知道【" . $post->post_title . "】的更多内容吗?现在就点我吧。";
if (!empty($config->sharing_page_description)) {
$share_desc = strtr($config->sharing_page_description, $template_vars);
}
if (!empty($config->sharing_page_title)) {
$share_title = strtr($config->sharing_page_title, $template_vars);
}
} elseif (is_home() || is_front_page()) {
$template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description')
);
$default_share_desc = "这是【" . get_bloginfo('name') . "】的官方网站,请注意收藏。";
$default_share_title = get_bloginfo('name') . ' | ' . get_bloginfo('description');
if (!empty($config->sharing_home_title)) {
$share_title = strtr($config->sharing_home_title, $template_vars);
}
if (!empty($config->sharing_home_description)) {
$share_desc = strtr($config->sharing_home_description, $template_vars);
}
if (!empty($config->sharing_home_img_useicon)) {
$use_icon_as_share_img = $config->sharing_home_img_useicon;
}
} elseif (is_category()) {
$cate_name = single_cat_title('', false);
$template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description'),
'{category_name}' => $cate_name
);
$default_share_desc = "想知道【" . $cate_name . "】的更多内容吗?现在就点我吧。";
$default_share_title = $cate_name . ' | ' . get_bloginfo('name');
$category_description = wp_strip_all_tags(category_description());
if (!empty($category_description)) {
$share_desc = $category_description;
}
if (!empty($config->sharing_category_title)) {
$share_title = strtr($config->sharing_category_title, $template_vars);
}
} elseif (is_tag()) {
$tag_con = single_term_title('', false);
$tag_con = htmlspecialchars($tag_con);
$template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description'),
'{tag_con}' => $tag_con
);
$default_share_title = $tag_con . ' | ' . get_bloginfo('name');
$default_share_desc = "想知道【" . $tag_con . "】的更多内容吗?现在就点我吧。";
if (!empty($config->sharing_tag_description)) {
$share_desc = strtr($config->sharing_tag_description, $template_vars);
}
if (!empty($config->sharing_tag_title)) {
$share_title = strtr($config->sharing_tag_title, $template_vars);
}
} elseif (is_archive()) {
$m = get_query_var('m');
$year = get_query_var('year');
$monthnum = get_query_var('monthnum');
$day = get_query_var('day');
$archive_date = '';
if (!empty($m)) {
$my_year = substr($m, 0, 4);
$my_month = $wp_locale->get_month(substr($m, 4, 2));
$my_day = intval(substr($m, 6, 2));
if ($site_lang == "zh") {
$archive_date = $my_year . '年' . ($my_month ? $my_month . '月' : '') . ($my_day ? $my_day . '日' : '');
} else {
$archive_date = $my_year . '/' . ($my_month ? $my_month . '/' : '') . ($my_day ? $my_day : '');
}
}
if (!empty($year)) {
$archive_date = $year . '年';
if ($site_lang != "zh") {
$archive_date = $year;
}
if (!empty($monthnum)) {
if ($site_lang != "zh") {
$archive_date .= '/' . $monthnum;
} else {
$archive_date .= $monthnum . '月';
}
}
if (!empty($day)) {
if ($site_lang != "zh") {
$archive_date .= '/' . zeroise($day, 2);
} else {
$archive_date .= zeroise($day, 2) . '日';
}
}
}
$template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description'),
'{archive_date}' => $archive_date
);
$default_share_title = $archive_date . ' | ' . get_bloginfo('name');
$default_share_desc = "想知道" . $archive_date . "都有什么吗?现在就点我吧。";
if (!empty($config->sharing_archive_description)) {
$share_desc = strtr($config->sharing_archive_description, $template_vars);
}
if (!empty($config->sharing_archive_title)) {
$share_title = strtr($config->sharing_archive_title, $template_vars);
}
} elseif (is_search()) {
$search = get_query_var('s');
$search_con = strip_tags($search);
$template_vars = array(
'{blog_name}' => get_bloginfo('name'),
'{blog_sub_name}' => get_bloginfo('description'),
'{search_con}' => $search_con
);
$default_share_title = $search_con . ' | ' . get_bloginfo('name');
$default_share_desc = "想知道【" . $search_con . "】的更多内容吗?点我开始搜索吧。";
if (!empty($config->sharing_search_description)) {
$share_desc = strtr($config->sharing_search_description, $template_vars);
}
if (!empty($config->sharing_search_title)) {
$share_title = strtr($config->sharing_search_title, $template_vars);
}
}
// todo:support custom post type
// https://codex.wordpress.org/Post_Types#Custom_Post_Types
?>
var use_icon_as_share_img = "<?php echo $use_icon_as_share_img ?>";
var default_img_url='<?php echo $default_img_url ?>';
var share_img_url='<?php echo $share_img_url ?>';
var share_desc="<?php echo str_replace("\"","\\\"",$share_desc) ?>";
var default_share_desc="<?php echo str_replace("\"","\\\"",$default_share_desc) ?>";
var share_title="<?php echo str_replace("\"","\\\"",$share_title) ?>";
var default_share_title="<?php echo str_replace("\"","\\\"",$default_share_title) ?>";
var current_url=location.href.split('#')[0];
if(share_title==""){
share_title=jQuery("head title").text();
}
if(share_title==""){
share_title=default_share_title;
}
if(use_icon_as_share_img=="1"){
var icons = jQuery("head link[rel='shortcut icon']");
if(icons!=null&&icons.length>0){
share_img_url=jQuery(icons[0]).attr('href');
}
}
if(share_img_url==''){
var imgs = document.getElementsByTagName("img");
for(var i=0;i<imgs.length;i++){
if(imgs[i].width>=100&&imgs[i].height>=100){
share_img_url=imgs[i].src;
break;
}
}
if(share_img_url==''){
share_img_url=default_img_url;
}
}
if(share_desc==''){
var meta = document.getElementsByTagName('meta');
for(i in meta){
if(typeof meta[i].name!="undefined" && meta[i].name.toLowerCase()=="description"){
share_desc = meta[i].content;
break;
}
}
}
if(share_desc==''){
share_desc=default_share_desc;
}
wx.config({
debug: false,
appId: weChatJsSign.appId,
timestamp: weChatJsSign.timestamp,
nonceStr: weChatJsSign.nonceStr,
signature: weChatJsSign.signature,
jsApiList: [
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareWeibo'
]
});
wx.ready(function () {
wx.updateTimelineShareData({
title: share_title,
link: current_url,
imgUrl: share_img_url
});
wx.updateAppMessageShareData({
title: share_title,
desc: share_desc,
link: current_url,
imgUrl: share_img_url
});
wx.onMenuShareWeibo({
title: share_title,
desc: share_desc,
link: current_url,
imgUrl: share_img_url
});
});
<?php
}
/**
* 输出引用必须的Js
*/
public static function render_ref_js(){
wp_enqueue_script('jquery');
wp_enqueue_script('weixin-js', 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js');
}
/**
* 输出微信Js
*/
public static function render_js()
{
$weChat = Bosima_WeChat::getInstance();
$config = $weChat->getWeChatConfig();
?>
<script>
jQuery(document).ready(function(){
<?php
$ajax_url = admin_url('admin-ajax.php');
?>
var weChatJsSign = '';
var data={
action:'getWeChatJsSign',
cur_url:encodeURIComponent(location.href.split('#')[0])
}
jQuery.get('<?php echo $ajax_url; ?>', data, function(response) {
weChatJsSign = response;
if(weChatJsSign){
<?php Bosima_WeChat_Page_Sharing_Page::render_config_js($config); ?>
}
},'json');
});
</script>
<?php
}
}