66 *
77 * @package DPlayer
88 * @author Volio
9- * @version 1.0.4
10- * @link http ://github.com/volio/DPlayer-for-typecho
9+ * @version 1.1.0
10+ * @link https ://niconiconi.org
1111 */
1212class DPlayer_Plugin implements Typecho_Plugin_Interface
1313{
@@ -20,8 +20,8 @@ class DPlayer_Plugin implements Typecho_Plugin_Interface
2020 */
2121 public static function activate ()
2222 {
23- Typecho_Plugin::factory ('Widget_Abstract_Contents ' )->contentEx = ['DPlayer_Plugin ' , 'parsePlayer ' ];
24- Typecho_Plugin::factory ('Widget_Abstract_Contents ' )->excerptEx = ['DPlayer_Plugin ' , 'parsePlayer ' ];
23+ Typecho_Plugin::factory ('Widget_Abstract_Contents ' )->contentEx = ['DPlayer_Plugin ' , 'replacePlayer ' ];
24+ Typecho_Plugin::factory ('Widget_Abstract_Contents ' )->excerptEx = ['DPlayer_Plugin ' , 'replacePlayer ' ];
2525 Typecho_Plugin::factory ('Widget_Archive ' )->header = ['DPlayer_Plugin ' , 'playerHeader ' ];
2626 Typecho_Plugin::factory ('Widget_Archive ' )->footer = ['DPlayer_Plugin ' , 'playerFooter ' ];
2727 Typecho_Plugin::factory ('admin/write-post.php ' )->bottom = ['DPlayer_Plugin ' , 'addEditorButton ' ];
@@ -44,10 +44,8 @@ public static function deactivate()
4444 */
4545 public static function playerHeader ()
4646 {
47- $ url = Helper::options ()->pluginUrl . '/DPlayer ' ;
4847 echo <<<EOF
4948<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.css" />
50- <script>var dPlayerOptions = [];</script>
5149EOF ;
5250 }
5351
@@ -66,29 +64,26 @@ public static function playerFooter()
6664 }
6765 echo <<<EOF
6866<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js"></script>
69- <script type="text/javascript" src=" $ url/dist/util .js"></script>
67+ <script type="text/javascript" src=" $ url/assets/player .js"></script>
7068EOF ;
7169 }
7270
7371 /**
7472 * 内容标签替换
7573 *
76- * @param string $content
74+ * @param $text
7775 * @param $widget
78- * @param $lastResult
76+ * @param $last
7977 * @return string
8078 */
81- public static function parsePlayer ( $ content , $ widget , $ lastResult )
79+ public static function replacePlayer ( $ text , $ widget , $ last )
8280 {
83- $ content = empty ($ lastResult ) ? $ content : $ lastResult ;
81+ $ text = empty ($ last ) ? $ text : $ last ;
8482 if ($ widget instanceof Widget_Archive) {
85- if (false === strpos ($ content , '[ ' )) {
86- return $ content ;
87- }
8883 $ pattern = self ::get_shortcode_regex (['dplayer ' ]);
89- $ content = preg_replace_callback ("/ $ pattern/ " , ['DPlayer_Plugin ' , 'parseCallback ' ], $ content );
84+ $ text = preg_replace_callback ("/ $ pattern/ " , ['DPlayer_Plugin ' , 'parseCallback ' ], $ text );
9085 }
91- return $ content ;
86+ return $ text ;
9287 }
9388
9489 /**
@@ -114,87 +109,75 @@ public static function parseCallback($matches)
114109 }
115110 //还原转义后的html
116111 //[dplayer title="Test Abc" artist="haha" id="1234543"/]
117- $ attr = htmlspecialchars_decode ($ matches [3 ]);
112+ $ tag = htmlspecialchars_decode ($ matches [3 ]);
118113 //[dplayer]标签的属性,类型为array
119- $ atts = self ::shortcode_parse_atts ($ attr );
120- //播放器id
121- $ id = md5 ( isset ( $ atts [ ' url ' ]) ? $ atts [ ' url ' ] : ' default id ' );
114+ $ attrs = self ::shortcode_parse_atts ($ tag );
115+ return DPlayer_Plugin:: parsePlayer ( $ attrs );
116+ }
122117
118+ public static function parsePlayer ($ attrs )
119+ {
123120 //播放器设置
124- $ theme = Typecho_Widget::widget ('Widget_Options ' )->plugin ('DPlayer ' )->theme ;
121+ $ theme = Typecho_Widget::widget ('Widget_Options ' )->plugin ('DPlayer ' )->theme ?: ' #FADFA3 ' ;
125122 $ api = Typecho_Widget::widget ('Widget_Options ' )->plugin ('DPlayer ' )->api ;
126- if (!$ theme ) $ theme = '#FADFA3 ' ;
127-
128- //输出代码
129- $ playerCode = '<div id="player ' . $ id . '" class="dplayer"> ' ;
130- $ playerCode .= "</div> \n" ;
131123
132- $ video = array (
133- 'url ' => isset ($ atts ['url ' ]) ? $ atts ['url ' ] : '' ,
134- 'pic ' => isset ($ atts ['pic ' ]) ? $ atts ['pic ' ] : '' ,
135- 'type ' => isset ($ atts ['type ' ]) ? $ atts ['type ' ] : 'auto ' ,
136- 'thumbnails ' => isset ($ atts ['thumbnails ' ]) ? $ atts ['thumbnails ' ] : '' ,
137- );
138- //弹幕部分配置文件
139- $ subtitle = [
140- 'url ' => isset ($ atts ['subtitleurl ' ]) ? $ atts ['subtitleurl ' ] : '' ,
141- 'type ' => isset ($ atts ['subtitletype ' ]) ? $ atts ['subtitletype ' ] : 'webvtt ' ,
142- 'fontSize ' => isset ($ atts ['subtitlefontsize ' ]) ? $ atts ['subtitlefontsize ' ] : '25px ' ,
143- 'bottom ' => isset ($ atts ['subtitlebottom ' ]) ? $ atts ['subtitlebottom ' ] : '10% ' ,
144- 'color ' => isset ($ atts ['subtitlecolor ' ]) ? $ atts ['subtitlecolor ' ] : '#b7daff ' ,
145- ];
146- $ danmaku = [
147- 'id ' => $ id ,
148- 'api ' => $ api ,
149- 'maximum ' => isset ($ atts ['maximum ' ]) ? $ atts ['maximum ' ] : 1000 ,
150- 'addition ' => isset ($ atts ['addition ' ]) ? [$ atts ['addition ' ]] : null ,
151- 'user ' => isset ($ atts ['user ' ]) ? $ atts ['user ' ] : 'DIYgod ' ,
152- 'bottom ' => isset ($ atts ['bottom ' ]) ? $ atts ['bottom ' ] : '15% ' ,
153- 'unlimited ' => true ,
154- ];
155-
156- //播放器默认属性
157- $ data = [
158- 'id ' => $ id ,
124+ //播放器属性
125+ $ config = [
159126 'live ' => false ,
160- 'autoplay ' => false ,
161- 'theme ' => isset ($ atts ['theme ' ]) ? $ atts ['theme ' ] : ' #FADFA3 ' ,
162- 'loop ' => ( isset ($ atts ['loop ' ]) && $ atts ['loop ' ] == 'true ' ) ? true : false ,
163- 'screenshot ' => ( isset ($ atts ['screenshot ' ]) && $ atts ['screenshot ' ] == 'true ' ) ? true : false ,
127+ 'autoplay ' => isset ( $ attrs [ ' autoplay ' ]) && $ attrs [ ' autoplay ' ] == ' true ' ,
128+ 'theme ' => isset ($ attrs ['theme ' ]) ? $ attrs ['theme ' ] : $ theme ,
129+ 'loop ' => isset ($ attrs ['loop ' ]) && $ attrs ['loop ' ] == 'true ' ,
130+ 'screenshot ' => isset ($ attrs ['screenshot ' ]) && $ attrs ['screenshot ' ] == 'true ' ,
164131 'hotkey ' => true ,
165132 'preload ' => 'metadata ' ,
166- 'lang ' => isset ($ atts ['lang ' ]) ? $ atts ['lang ' ] : 'zh-cn ' ,
167- 'logo ' => isset ($ atts ['logo ' ]) ? $ atts ['logo ' ] : null ,
168- 'volume ' => isset ($ atts ['volume ' ]) ? $ atts ['volume ' ] : 0.7 ,
133+ 'lang ' => isset ($ attrs ['lang ' ]) ? $ attrs ['lang ' ] : 'zh-cn ' ,
134+ 'logo ' => isset ($ attrs ['logo ' ]) ? $ attrs ['logo ' ] : null ,
135+ 'volume ' => isset ($ attrs ['volume ' ]) ? $ attrs ['volume ' ] : 0.7 ,
169136 'mutex ' => true ,
137+ 'video ' => [
138+ 'url ' => isset ($ attrs ['url ' ]) ? $ attrs ['url ' ] : null ,
139+ 'pic ' => isset ($ attrs ['pic ' ]) ? $ attrs ['pic ' ] : null ,
140+ 'type ' => isset ($ attrs ['type ' ]) ? $ attrs ['type ' ] : 'auto ' ,
141+ 'thumbnails ' => isset ($ attrs ['thumbnails ' ]) ? $ attrs ['thumbnails ' ] : null ,
142+ ],
170143 ];
171- $ data ['video ' ] = $ video ;
172- $ data ['danmaku ' ] = (isset ($ atts ['danmu ' ]) && $ atts ['danmu ' ] == 'true ' ) ? $ danmaku : null ;
173- $ data ['subtitle ' ] = isset ($ atts ['subtitleurl ' ]) ? $ subtitle : null ;
174- $ data ['autoplay ' ] = (isset ($ atts ['autoplay ' ]) && $ atts ['autoplay ' ] == 'true ' ) ? true : false ;
175- $ data ['theme ' ] = isset ($ atts ['theme ' ]) ? $ atts ['theme ' ] : $ theme ;
176- //加入头部数组
177- $ js = json_encode ($ data );
178- $ playerCode .= <<<EOF
179- <script>dPlayerOptions.push( {$ js });</script>
180- EOF ;
181- return $ playerCode ;
144+ if (isset ($ attrs ['danmu ' ]) && $ attrs ['danmu ' ] == 'true ' ) {
145+ $ config ['danmaku ' ] = [
146+ 'id ' => md5 (isset ($ attrs ['url ' ]) ? $ attrs ['url ' ] : '' ),
147+ 'api ' => $ api ,
148+ 'maximum ' => isset ($ attrs ['maximum ' ]) ? $ attrs ['maximum ' ] : 1000 ,
149+ 'user ' => isset ($ attrs ['user ' ]) ? $ attrs ['user ' ] : 'DIYgod ' ,
150+ 'bottom ' => isset ($ attrs ['bottom ' ]) ? $ attrs ['bottom ' ] : '15% ' ,
151+ 'unlimited ' => true ,
152+ ];
153+ }
154+ if (isset ($ attrs ['subtitle ' ]) && $ attrs ['subtitle ' ] == 'true ' ) {
155+ $ config ['subtitle ' ] = [
156+ 'url ' => isset ($ attrs ['subtitleurl ' ]) ? $ attrs ['subtitleurl ' ] : null ,
157+ 'type ' => isset ($ attrs ['subtitletype ' ]) ? $ attrs ['subtitletype ' ] : 'webvtt ' ,
158+ 'fontSize ' => isset ($ attrs ['subtitlefontsize ' ]) ? $ attrs ['subtitlefontsize ' ] : '25px ' ,
159+ 'bottom ' => isset ($ attrs ['subtitlebottom ' ]) ? $ attrs ['subtitlebottom ' ] : '10% ' ,
160+ 'color ' => isset ($ attrs ['subtitlecolor ' ]) ? $ attrs ['subtitlecolor ' ] : '#b7daff ' ,
161+ ];
162+ }
163+ $ json = json_encode ($ config );
164+ return "<div class= \"dplayer \" data-config=' {$ json }'></div> " ;
182165 }
183166
184167 public static function addEditorButton ()
185168 {
186- $ dir = Helper::options ()->pluginUrl . '/DPlayer/dist /editor.js ' ;
169+ $ dir = Helper::options ()->pluginUrl . '/DPlayer/assets /editor.js ' ;
187170 echo "<script type= \"text/javascript \" src= \"{$ dir }\"></script> " ;
188171 }
189172
190173 public static function config (Typecho_Widget_Helper_Form $ form )
191174 {
192175 $ theme = new Typecho_Widget_Helper_Form_Element_Text (
193176 'theme ' , null , '#FADFA3 ' ,
194- _t ('默认主题颜色 ' ), _t ('播放器默认的主题颜色,如 #372e21、#75c、red、blue,该设定会被[dplayer]标签中的theme属性覆盖,默认为 #FADFA3 ' ));
177+ _t ('默认主题颜色 ' ), _t ('播放器默认的主题颜色,例如 #372e21、#75c、red、blue,该设定会被[dplayer]标签中的theme属性覆盖,默认为 #FADFA3 ' ));
195178 $ api = new Typecho_Widget_Helper_Form_Element_Text (
196- 'api ' , null , 'https://api.prprpr.me/dplayer/v3/ ' ,
197- _t ('弹幕服务器地址 ' ), _t ('用于保存视频弹幕,默认为 https://api.prprpr.me/dplayer/v3/ ' ));
179+ 'api ' , null , '' ,
180+ _t ('弹幕服务器地址 ' ), _t ('用于保存视频弹幕,例如 https://api.prprpr.me/dplayer/v3/ ' ));
198181 $ hls = new Typecho_Widget_Helper_Form_Element_Radio ('hls ' , array ('0 ' => _t ('不开启HLS支持 ' ), '1 ' => _t ('开启HLS支持 ' )), '0 ' , _t ('HLS支持 ' ), _t ("开启后可解析 m3u8 格式视频 " ));
199182 $ flv = new Typecho_Widget_Helper_Form_Element_Radio ('flv ' , array ('0 ' => _t ('不开启FLV支持 ' ), '1 ' => _t ('开启FLV支持 ' )), '0 ' , _t ('FLV支持 ' ), _t ("开启后可解析 flv 格式视频 " ));
200183 $ form ->addInput ($ theme );
0 commit comments