-
Notifications
You must be signed in to change notification settings - Fork 10
/
class.dplayer.php
172 lines (148 loc) · 6.65 KB
/
class.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
<?php
/**
* DPlayer是一个可爱且支持弹幕的HTML5视频播放器,由DIYgod编写,项目地址:https://github.com/MoePlayer/DPlayer
*
* DPlayer Lite是一个简洁实用的HTML5视频播放器,由kn007基于DPlayer 1.17.1 5207d26修改而成,项目地址:https://github.com/kn007/DPlayer-Lite
*
* 调用DPlayerHandle类并进行初始化,可以很简便的在WordPress使用上DPlayer Lite及DPlayer(有限功能)
* 短代码调用方法详见博客文章:https://kn007.net/topics/wordpress-blog-use-new-html5-video-player-dplayer-lite/
*
* 支持原生[video]标签,前提是增加dplayer参数,并且视频地址使用src参数
*
* 此代码以MIT许可协议授权,作者kn007,写于2017年12月12日。Github地址:https://github.com/kn007/DPlayerHandle
**/
class DPlayerHandle {
protected $instance = 0;
protected $hls_enable = 0;
protected $flv_enable = 0;
protected $dash_enable = 0;
protected $webtorrent_enable = 0;
public function init() {
add_shortcode( 'dplayer', array( $this, 'dplayer_shortcode' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'shortcode_buttons' ), 99999 );
add_filter( 'wp_video_shortcode_override' , array( $this, 'override_wp_video_shortcode' ), 1, 2 );
wp_register_script( 'hlsjs', 'https://cdn.jsdelivr.net/npm/hls.js/dist/hls.min.js', array(), "0.12.4", true );
wp_register_script( 'flvjs', 'https://cdn.jsdelivr.net/npm/flv.js/dist/flv.min.js', array(), "1.5.0", true );
wp_register_script( 'dashjs', 'https://cdn.jsdelivr.net/npm/dashjs/dist/dash.all.min.js', array(), "3.0.0", true );
wp_register_script( 'webtorrent', 'https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js', array(), "0.107.16", true );
wp_register_script( 'dplayer', 'https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js', array(), "1.25.0", true );
}
protected function dplayer_extension( $name ) {
wp_dequeue_script( 'dplayer' );
switch ( $name ) {
case 'hlsjs':
wp_enqueue_script( 'hlsjs' );
$this->hls_enable++;
break;
case 'flvjs':
wp_enqueue_script( 'flvjs' );
$this->flv_enable++;
break;
case 'dashjs':
wp_enqueue_script( 'dashjs' );
$this->dash_enable++;
break;
case 'webtorrent':
wp_enqueue_script( 'webtorrent' );
$this->webtorrent_enable++;
break;
}
wp_enqueue_script( 'dplayer' );
}
public function dplayer_shortcode( $atts = array(), $content = '' ) {
if ( !is_singular() && !is_admin() ) return;
$atts = shortcode_atts(
array(
'autoplay' => 'false',
'theme' => '#b7daff',
'loop' => 'false',
'preload' => 'auto',
'src' => '',
'poster' => '',
'type' => 'auto',
'mutex' => 'true',
'iconsColor' => '#ffffff',
), $atts, 'dplayer_shortcode' );
$atts['autoplay'] = wp_validate_boolean( $atts['autoplay'] );
$atts['theme'] = esc_attr( $atts['theme'] );
$atts['loop'] = wp_validate_boolean( $atts['loop'] );
$atts['preload'] = esc_attr( $atts['preload'] );
$atts['src'] = esc_url_raw( $atts['src'] );
$atts['poster'] = esc_url_raw( $atts['poster'] );
$atts['type'] = strtolower( esc_attr( $atts['type'] ) );
$atts['mutex'] = wp_validate_boolean( $atts['mutex'] );
$atts['iconsColor'] = esc_attr( $atts['iconsColor'] );
if ( empty( $atts['src'] ) ) return;
$this->instance++;
( 1 === $this->instance ) && wp_enqueue_script( 'dplayer' );
( 0 === $this->hls_enable ) && ( $atts['type'] == 'hls' || preg_match( "/\.(m3u8)($|\?|\#)/", strtolower( $atts['src'] ) ) ) && $this->dplayer_extension( 'hlsjs' );
( 0 === $this->flv_enable ) && ( $atts['type'] == 'flv' || preg_match( "/\.(flv)($|\?|\#)/", strtolower( $atts['src'] ) ) ) && $this->dplayer_extension( 'flvjs' );
( 0 === $this->dash_enable ) && ( $atts['type'] == 'dash' || preg_match( "/\.(mpd)($|\?|\#)/", strtolower( $atts['src'] ) ) ) && $this->dplayer_extension( 'dashjs' );
( 0 === $this->webtorrent_enable ) && ( $atts['type'] == 'webtorrent' || preg_match( "/\.(torrent)($|\?|\#)/", strtolower( $atts['src'] ) ) || preg_match( "/(magnet:\?xt=urn:btih:)?(.{40})/", strtolower( $atts['src'] ) ) ) && $this->dplayer_extension( 'webtorrent' );
if ( empty( $atts['poster'] ) ) {
$output = sprintf( '<script>var dp%u=new DPlayer({container:document.getElementById("dplayer-%u"),autoplay:%b,theme:"%s",loop:%b,preload:"%s",video:{url:"%s",type:"%s"},mutex:%b,iconsColor:"%s"});</script>',
$this->instance,
$this->instance,
$atts['autoplay'],
$atts['theme'],
$atts['loop'],
$atts['preload'],
$atts['src'],
$atts['type'],
$atts['mutex'],
$atts['iconsColor']
);
} else {
$output = sprintf( '<script>var dp%u=new DPlayer({container:document.getElementById("dplayer-%u"),autoplay:%b,theme:"%s",loop:%b,preload:"%s",video:{url:"%s",pic:"%s",type:"%s"},mutex:%b,iconsColor:"%s"});</script>',
$this->instance,
$this->instance,
$atts['autoplay'],
$atts['theme'],
$atts['loop'],
$atts['preload'],
$atts['src'],
$atts['poster'],
$atts['type'],
$atts['mutex'],
$atts['iconsColor']
);
}
$html = sprintf( '<div id="dplayer-%u"></div>',
$this->instance
);
add_action( 'wp_footer', function () use ( $output ) {
echo ' ' . $output . "\n";
}, 99999 );
return $html;
}
public function shortcode_buttons() {
if ( wp_script_is( 'quicktags' ) ) {
echo "<script type=\"text/javascript\">QTags.addButton('add_dplayer', 'dplayer', '[dplayer src=\"\"]');</script>";
}
}
public function override_wp_video_shortcode( $html = '', $atts = array() ) {
if ( '' !== $html || empty( $atts['dplayer'] ) || empty( $atts['src'] ) ) return $html;
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) return $html;
$atts = shortcode_atts(
array(
'autoplay' => 'false',
'theme' => '#b7daff',
'loop' => 'false',
'preload' => 'auto',
'src' => '',
'poster' => '',
'type' => 'auto',
'mutex' => 'true',
'iconsColor' => '#ffffff',
), $atts, 'video' );
( $atts['loop'] == 'true' || $atts['loop'] == 'on' || $atts['loop'] == '1' ) And $atts['loop'] = 'true' Or $atts['loop'] = 'false';
( $atts['autoplay'] == 'true' || $atts['autoplay'] == 'on' || $atts['autoplay'] == '1' ) And $atts['autoplay'] = 'true' Or $atts['autoplay'] = 'false';
$video_attr_strings = array();
foreach ( $atts as $k => $v ) {
if ( $v == '' ) continue;
$video_attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
}
$html .= sprintf( '[dplayer %s]', join( ' ', $video_attr_strings ) );
return do_shortcode( $html );
}
}