public static function parseCallback($matches)
{
/*
$mathes array
* 1 - An extra [ to allow for escaping shortcodes with double [[]]
* 2 - The shortcode name
* 3 - The shortcode argument list
* 4 - The self closing /
* 5 - The content of a shortcode when it wraps some content.
* 6 - An extra ] to allow for escaping shortcodes with double [[]]
*/
// allow [[player]] syntax for escaping the tag
if ($matches[1] == '[' && $matches[6] == ']') {
return substr($matches[0], 1, -1);
}
//播放器id
$id = self::getUniqueId();
//还原转义后的html
//[player title="Test Abc" artist="haha" id="1234543"/]
$attr = htmlspecialchars_decode($matches[3]);
//[player]标签的属性,类型为array
$atts = self::shortcode_parse_atts($attr);
//开始解析音乐地址
$result = array();
//解析[player]标签内id和url属性
if (isset($atts['url']) || isset($atts['id'])) {
$r = self::parse($matches[5], $atts);
if ($r) {
$result = array_merge($result, $r);
}
}
//解析[player][/player]内部的[mp3]标签
if ($matches[4] != '/' && $matches[5]) {
//获取正则
$regex = self::get_shortcode_regex(array('mp3'));
//过滤html标签并还原转义后的字符
$content = htmlspecialchars_decode(strip_tags($matches[5]));
//开始解析
if (false !== strpos($content, '[') && preg_match_all("/{$regex}/", $content, $all)) {
foreach ($all[0] as $k => $v) {
$a = self::shortcode_parse_atts($all[3][$k]);
//获取所有music信息
$r = self::parse(trim($all[5][$k]), $a);
if ($r) {
$result = array_merge($result, $r);
}
}
}
}
//删除id避免与后面的id属性冲突
if (isset($atts['id'])) {
unset($atts['id']);
}
//没有歌曲时候直接返回空值避免出错
if (empty($result)) {
return '';
}
//主题颜色
$theme = Typecho_Widget::widget('Widget_Options')->plugin('APlayer')->maintheme;
if (!$theme) {
$theme = '#e6d0b2';
}
//只允许一个播放器播放
$mutex = Typecho_Widget::widget('Widget_Options')->plugin('APlayer')->mutex;
if ($mutex == "false") {
$mutex = false;
}
if ($mutex == "true") {
$mutex = true;
}
//Audio preload
$preload = Typecho_Widget::widget('Widget_Options')->plugin('APlayer')->preload;
if (!$preload || $preload == "false") {
$preload = false;
}
//播放器默认属性
$data = array('id' => $id, 'autoplay' => false, 'theme' => $theme, 'mutex' => $mutex, 'preload' => $preload);
//设置播放器属性
if (!empty($atts)) {
foreach ($atts as $k => $att) {
$data[$k] = $atts[$k];
}
}
//默认有歌词就显示
if (!isset($data['showlrc'])) {
foreach ($result as $v) {
if ($v['lyric']) {
$data['showlrc'] = true;
break;
}
}
}
//自动播放
$data['autoplay'] = (bool) $data['autoplay'] && $data['autoplay'] !== 'false';
//歌词
$data['showlrc'] = isset($data['showlrc']) && (bool) $data['showlrc'] && $data['showlrc'] !== 'false' ? 1 : 0;
//输出代码
$playerCode = '<div id="player' . $id . '" class="aplayer">
';
//nolyric
$nolyric = Typecho_Widget::widget('Widget_Options')->plugin('APlayer')->nolyric;
if (!$nolyric) {
$nolyric = '找不到歌词';
}
//歌词
if (!empty($result)) {
foreach ($result as $k => $v) {
//歌词不存在的时候输出'no lyric'
$result[$k]['lrc'] = $v['lyric'] ? $v['lyric'] : "[00:00.00]{$nolyric}\n[99:00.00] ";
unset($result[$k]['cover']);
unset($result[$k]['lyric']);
unset($result[$k]['artist']);
}
}
$playerCode .= "</div>\n";
//开始添加歌曲列表,若只有一首歌则解析成单曲播放器,否则解析为列表播放器
if (count($result) === 1) {
$result = array_shift($result);
}
$data['music'] = $result;
//加入头部数组
$js = json_encode($data);
$playerCode .= <<<EOF
<script>APlayerOptions.push({$js});</script>
EOF;
return $playerCode;
}