-
Notifications
You must be signed in to change notification settings - Fork 24
/
dplayer.php
executable file
·223 lines (198 loc) · 9.79 KB
/
dplayer.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
<?php
/*
* Plugin Name: DPlayer for WordPress
* Description: Wow, such a lovely HTML5 danmaku video player comes to WordPress
* Version: 1.2.5
* Author: 0xBBC
* Author URI: https://blog.0xbbc.com/
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* Acknowledgement
* DPlayer by DIYgod
* https://github.com/MoePlayer/DPlayer
*
* And part of this work is done under Copy and paste programming :)
* Thanks to https://github.com/MoePlayer/DPlayer-Typecho
*/
require_once( dirname( __FILE__ ) . '/dplayer-admin.php' );
class DPlayer {
static $add_script;
public static function init() {
register_activation_hook( __FILE__, array( __CLASS__, 'dplayer_install' ) );
register_deactivation_hook( __FILE__, array( __CLASS__, 'dplayer_uninstall' ) );
add_action( 'wp_head', array( __CLASS__, 'ready') );
add_action( 'wp_footer', array( __CLASS__, 'add_script' ) );
add_shortcode( 'dplayer', array( __CLASS__, 'dplayer_load') );
add_filter( 'plugin_action_links', array( __CLASS__, 'dplayer_settings_link' ), 9, 2 );
}
public static function ready() {
?>
<script>var dPlayers = [];var dPlayerOptions = [];</script>
<?php
}
// registers default options
public static function dplayer_install() {
add_option( 'kblog_danmaku_url', '//danmaku.daoapp.io' );
add_option( 'kblog_danmaku_token', 'tokendemo' );
add_option( 'kblog_danmaku_dplayer_version', '1.25.0' );
add_option( 'kblog_danmaku_dplayer_version_check', '0' );
}
public static function dplayer_uninstall() {
delete_option( 'kblog_danmaku_url' );
delete_option( 'kblog_danmaku_token' );
delete_option( 'kblog_danmaku_dplayer_version' );
delete_option( 'kblog_danmaku_dplayer_version_check' );
}
public static function dplayer_bilibili_url_handler($bilibili_url) {
$aid = 0;
$page = 1;
$is_bilibili = false;
if (preg_match('/^[\d]+$/', $bilibili_url)) {
$aid = $bilibili_url;
$is_bilibili = true;
} else {
$parsed = parse_url($bilibili_url);
if ($parsed['host'] === 'www.bilibili.com') {
preg_match('/^\/video\/av([\d]+)(?:\/index_([\d]+)\.html)?/', $parsed['path'], $path_match);
if ($path_match) {
$is_bilibili = true;
$aid = $path_match[1];
$page = $path_match[2] == null ? 1 : $path_match[2];
preg_match('/^page=([\d]+)$/', $parsed['fragment'], $page_match);
if ($page_match) $page = $page_match[1];
}
}
}
if ($is_bilibili) {
if ($page == 1) {
$danmaku_url = stripslashes(get_option( 'kblog_danmaku_url', '' ));
return array('danma' => $danmaku_url.'bilibili?aid='.$aid, 'video' => $danmaku_url.'/video/bilibili?aid='.$aid);
} else {
$cid = -1;
$json_response = @json_decode(gzdecode(file_get_contents('http://www.bilibili.com/widget/getPageList?aid='.$aid)), true);
if ($json_response) {
foreach ($json_response as $page_info) {
if ($page_info['page'] == $page) {
$cid = $page_info['cid'];
break;
}
}
}
if ($cid != -1) {
return array('danma' => $danmaku_url.'bilibili?cid='.$cid, 'video' => $danmaku_url.'/video/bilibili?cid='.$cid);
}
}
}
return null;
}
public static function dplayer_load($atts, $content, $tag) {
if ($atts == null) $atts = [];
if ($tag == null) $tag = '';
// normalize attribute keys, lowercase
$atts = array_change_key_case((array)$atts, CASE_LOWER);
$bilibili_param = $atts['bilibili'] ? $atts['bilibili'] : '';
$id = md5($_SERVER['HTTP_HOST'] . $atts['url'] . $bilibili_param);
$data = array(
'id' => $id,
'live' => false,
'autoplay' => false,
'theme' => $atts['theme'] ? $atts['theme'] : '#FADFA3',
'loop' => false,
'screenshot' => false,
'hotkey' => true,
'preload' => 'metadata',
'lang' => $atts['lang'] ? $atts['lang'] : 'zh-cn',
'logo' => $atts['logo'] ? $atts['logo'] : null,
'volume' => $atts['volume'] ? $atts['volume'] : 0.7,
'mutex' => true,
);
if ($atts['autoplay']) $data['autoplay'] = ($atts['autoplay'] == 'true') ? true : false;
if ($atts['loop']) $data['loop'] = ($atts['loop'] == 'true') ? true : false;
if ($atts['screenshot']) $data['screenshot'] = ($atts['screenshot'] == 'true') ? true : false;
if ($atts['hotkey']) $data['hotkey'] = ($atts['hotkey'] == 'true') ? true : false;
if ($atts['preload']) $data['preload'] = (in_array($atts['preload'], array('auto', 'metadata', 'none')) == true) ? $atts['preload'] : 'metadata';
if ($atts['mutex']) $data['mutex'] = ($atts['mutex'] == 'false') ? false : true;
$video = array(
'url' => $atts['url'] ? $atts['url'] : '',
'pic' => $atts['pic'] ? $atts['pic'] : '',
'type' => $atts['type'] ? $atts['type'] : 'auto',
'thumbnails' => $atts['thumbnails'] ? $atts['thumbnails'] : '',
);
$subtitle = array(
'url' => $atts['subtitleurl'] ? $atts['subtitleurl'] : '',
'type' => $atts['subtitletype'] ? $atts['subtitletype'] : 'webvtt',
'fontSize' => $atts['subtitlefontsize'] ? $atts['subtitlefontsize'] : '25px',
'bottom' => $atts['subtitlebottom'] ? $atts['subtitlebottom'] : '10%',
'color' => $atts['subtitlecolor'] ? $atts['subtitlecolor'] : '#b7daff',
);
$danmaku = array(
'id' => md5($id),
'api' => get_option( 'kblog_danmaku_url', '' ),
'token' => get_option( 'kblog_danmaku_token', '' ),
'maximum' => $atts['maximum'] ? $atts['maximum'] : 1000,
'addition' => $atts['addition'] ? explode('-A-', $atts['addition']) : [],
'user' => $atts['user'] ? $atts['user'] : 'DIYgod',
'bottom' => $atts['bottom'] ? $atts['bottom'] : '15%',
'unlimited' => true,
);
if ($atts['unlimited']) $data['unlimited'] = ($atts['unlimited'] == 'true') ? true : false;
$playerCode = '<div id="player'.$id.'">';
$playerCode .= "</div>\n";
if ($bilibili_param) {
$bilibili_parsed = DPlayer::dplayer_bilibili_url_handler($bilibili_param);
$danmaku['addition'] = array($bilibili_parsed['danma']);
if ($danmaku['addition'] && empty($atts['url'])) {
$video['url'] = $bilibili_parsed['video'];
}
}
$data['video'] = $video;
$data['subtitle'] = $subtitle;
$data['danmaku'] = ($atts['danmu'] != 'false') ? $danmaku : null;
$js = json_encode($data);
$playerCode .= <<<EOF
<script>dPlayerOptions.push({$js});</script>
EOF;
return $playerCode;
}
public static function dplayer_settings_link( $links, $file ) {
if ( plugins_url('dplayer-admin.php', __FILE__) === $file && function_exists( 'admin_url' ) ) {
$settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=kblog-dplayer' ) ) . '">' . esc_html__( 'Settings' ) . '</a>';
array_unshift( $links, $settings_link );
}
return $links;
}
public static function add_script() {
if (!self::$add_script) {
if ( get_option( 'kblog_enable_flv' ) ) {
wp_enqueue_script( '0-dplayer-flv', plugins_url('dplayer/plugin/flv.min.js', __FILE__), false, '1.4.0', false );
}
if ( get_option( 'kblog_enable_hls' ) ) {
wp_enqueue_script( '0-dplayer-hls', plugins_url('dplayer/plugin/hls.min.js', __FILE__), false, '1.4.0', false );
}
$current_time = time();
$last_check = (int)get_option( 'kblog_danmaku_dplayer_version_check', '0' );
$dplayer_version = get_option( 'kblog_danmaku_dplayer_version', '1.25.0' );
if ($current_time - $last_check > 86400 /* 86400 = 60 * 60 * 24 i.e 24hrs */) {
$response = wp_remote_get( 'https://cdnjs.cat.net/ajax/libs/dplayer/package.json' );
if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$body = $response['body']; // use the content
$json_data = @json_decode($body, true);
$json_dplayer_version = @$json_data['version'];
if (preg_grep('/^[\d\.]+$/', $json_dplayer_version)) {
if (strcmp($dplayer_version, $json_dplayer_version) != 0) {
update_option( 'kblog_danmaku_dplayer_version', $json_dplayer_version );
$dplayer_version = $json_dplayer_version;
}
}
}
update_option( 'kblog_danmaku_dplayer_version_check', $current_time );
}
wp_enqueue_style( 'dplayer', esc_url("https://cdnjs.cat.net/ajax/libs/dplayer/$dplayer_version/DPlayer.min.css"), false, $dplayer_version, false );
wp_enqueue_script( 'dplayer', esc_url("https://cdnjs.cat.net/ajax/libs/dplayer/$dplayer_version/DPlayer.min.js"), false, $dplayer_version, false );
wp_enqueue_script( 'init-dplayer', plugins_url('dplayer/init-dplayer.js', __FILE__), false, '1.0.0', false );
self::$add_script = true;
}
}
};
DPlayer::init();