|
| 1 | +<?php |
| 2 | +/* |
| 3 | +* Plugin Name: DPlayer for WordPress |
| 4 | +* Description: Wow, such a lovely HTML5 danmaku video player comes to WordPress |
| 5 | +* Version: 1.0 |
| 6 | +* Author: BlueCocoa(WordPress Plugins) |
| 7 | +* Author URI: https://blog.0xbbc.com/ |
| 8 | +* |
| 9 | +* Acknowledgement |
| 10 | +* Part of this work is done under Copy and paste programming :) |
| 11 | +* Thanks to https://github.com/volio/DPlayer-for-typecho |
| 12 | +*/ |
| 13 | + |
| 14 | +/* |
| 15 | +The Star And Thank Author License (SATA) |
| 16 | +
|
| 17 | +Copyright (c) 2016 DIYgod(i@html.love) |
| 18 | +
|
| 19 | +Project Url: https://github.com/DIYgod/DPlayer |
| 20 | +
|
| 21 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 22 | +of this software and associated documentation files (the "Software"), to deal |
| 23 | +in the Software without restriction, including without limitation the rights |
| 24 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 25 | +copies of the Software, and to permit persons to whom the Software is |
| 26 | +furnished to do so, subject to the following conditions: |
| 27 | +
|
| 28 | +The above copyright notice and this permission notice shall be included in |
| 29 | +all copies or substantial portions of the Software. |
| 30 | +
|
| 31 | +And wait, the most important, you shall star/+1/like the project(s) in project url |
| 32 | +section above first, and then thank the author(s) in Copyright section. |
| 33 | +
|
| 34 | +Here are some suggested ways: |
| 35 | +
|
| 36 | + - Email the authors a thank-you letter, and make friends with him/her/them. |
| 37 | + - Report bugs or issues. |
| 38 | + - Tell friends what a wonderful project this is. |
| 39 | + - And, sure, you can just express thanks in your mind without telling the world. |
| 40 | +
|
| 41 | +Contributors of this project by forking have the option to add his/her name and |
| 42 | +forked project url at copyright and project url sections, but shall not delete |
| 43 | +or modify anything else in these two sections. |
| 44 | +
|
| 45 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 46 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 47 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 48 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 49 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 50 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 51 | +THE SOFTWARE. |
| 52 | +*/ |
| 53 | + |
| 54 | +class DPlayer { |
| 55 | + static $add_script; |
| 56 | + |
| 57 | + public static function init() { |
| 58 | + add_action( 'wp_head', array( __CLASS__, 'ready') ); |
| 59 | + add_action( 'wp_footer', array( __CLASS__, 'add_script' ) ); |
| 60 | + add_shortcode( 'dplayer', array( __CLASS__, 'dplayer_load') ); |
| 61 | + } |
| 62 | + |
| 63 | + public static function ready() { |
| 64 | +?> |
| 65 | +<script>var dPlayers = [];var dPlayerOptions = [];</script>; |
| 66 | +<?php |
| 67 | + } |
| 68 | + |
| 69 | + public static function dplayer_load($atts = [], $content = null, $tag = '') { |
| 70 | + // normalize attribute keys, lowercase |
| 71 | + $atts = array_change_key_case((array)$atts, CASE_LOWER); |
| 72 | + |
| 73 | + $id = md5($_SERVER['HTTP_HOST'] . $atts['url']); |
| 74 | + $result = array( |
| 75 | + 'url' => $atts['url'] ? $atts['url'] : '', |
| 76 | + 'pic' => $atts['pic'] ? $atts['pic'] : '' |
| 77 | + ); |
| 78 | + if (empty($result)) return; |
| 79 | + |
| 80 | + $theme = $atts['theme']; |
| 81 | + if (!$theme) $theme = '#FADFA3'; |
| 82 | + |
| 83 | + $data = array( |
| 84 | + 'id' => $id, |
| 85 | + 'autoplay' => false, |
| 86 | + 'theme' => $theme |
| 87 | + ); |
| 88 | + |
| 89 | + $data['autoplay'] = ($atts['autoplay'] == 'true') ? true : false; |
| 90 | + |
| 91 | + $playerCode = '<div id="player'.$id.'" class="dplayer">'; |
| 92 | + $playerCode .= "</div>\n"; |
| 93 | + $data['video'] = $result; |
| 94 | + |
| 95 | + $danmaku = array( |
| 96 | + 'id' => md5($id), |
| 97 | + 'token' => md5(md5($id) . date('YmdH', time())), |
| 98 | + 'api' => '//danmaku.daoapp.io/dplayer/danmaku', |
| 99 | + ); |
| 100 | + $data['danmaku'] = ($atts['danmu'] != 'false') ? $danmaku : null; |
| 101 | + |
| 102 | + $js = json_encode($data); |
| 103 | + $playerCode .= <<<EOF |
| 104 | +<script>dPlayerOptions.push({$js});</script> |
| 105 | +EOF; |
| 106 | + echo $playerCode; |
| 107 | + } |
| 108 | + |
| 109 | + public static function add_script() { |
| 110 | + if (!self::$add_script) { |
| 111 | + wp_enqueue_script( 'dplayer', site_url('/wp-content/plugins/dplayer/js/DPlayer.min.js'), false, '1.0.9', false ); |
| 112 | + wp_enqueue_script( 'dplayer-hls', site_url('/wp-content/plugins/dplayer/js/plugins/hls.min.js'), false, '1.0.9', false ); |
| 113 | + wp_enqueue_script( 'init-dplayer', site_url('/wp-content/plugins/dplayer/js/init-dplayer.js'), false, '1.0.0', false ); |
| 114 | + self::$add_script = true; |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +DPlayer::init(); |
0 commit comments