Skip to content

Commit

Permalink
feat: add xiami support
Browse files Browse the repository at this point in the history
  • Loading branch information
bigfa committed Mar 31, 2016
1 parent d41fd2f commit 3e20ebb
Show file tree
Hide file tree
Showing 18 changed files with 480 additions and 23 deletions.
36 changes: 35 additions & 1 deletion functions/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,40 @@ function nm_player(){
add_action('admin_menu', 'nm_menu');

function nm_menu() {
add_options_page('网易云音乐设置', '网易云音乐设置', 'manage_options', basename(__FILE__), 'nm_setting_page');
add_menu_page( '网易云音乐', '网易云音乐', 'manage_options', 'neteasemusic', 'nm_setting_page' );
add_submenu_page( 'neteasemusic', '设置', '设置', 'manage_options', 'neteasemusic', 'nm_setting_page' );
//add_submenu_page( 'neteasemusic', '自定义歌单', '自定义歌单', 'manage_options', 'neteasemusic-list', 'nm_list_setting' );
//add_submenu_page( 'neteasemusic', '帮助', '帮助', 'manage_options', 'neteasemusic-help', 'nm_setting_page' );
add_action( 'admin_init', 'nm_setting_group');
}

add_action('wp_ajax_nm_get','nm_get_callback');

function nm_get_callback(){
echo json_encode(array('data'=>get_option('nm_pr_list')));
exit;
}


add_action('wp_ajax_nm_add','nm_add_callback');

function nm_add_callback(){
global $nmjson;
$id = $_POST['url'];
//delete_option('nm_pr_list');
$lists = get_option('nm_pr_list') ? get_option('nm_pr_list') : array();
$album = $nmjson->netease_album($id);

$name = $album['album_title'];
$img = $album['album_cover'];
$ab = array('title' => $name ,'img' => $img );
$lists[] = $ab;

update_option( 'nm_pr_list',$lists);
header('Content-type: application/json');
echo json_encode(array('title'=>$name,'img'=>$album['album_cover']));
exit;
}

add_action( 'wp_ajax_nopriv_nmjson', 'nmjson_callback' );
add_action( 'wp_ajax_nmjson', 'nmjson_callback' );
Expand Down Expand Up @@ -176,6 +206,10 @@ function nm_setting_group() {
register_setting( 'nm_setting_group', 'nm_setting' );
}

function nm_list_setting(){
@include 'nm-list.php';
}

function nm_setting_page(){
@include 'nm-setting.php';
}
Expand Down
75 changes: 69 additions & 6 deletions functions/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,90 @@ function nms_like_btn($id) {
}

function nm_single_playform( $id , $instance , $cover , $title , $artist , $duration ) {
return '<div id="nm-player-' . $instance . '" class="nmsingle-container"><div class="nmsingle-cover" style="background-image:url(' . $cover . '?param=148x148)"><span class="nms-play-btn fxfont nm-play" data-index=' . $instance . '></span></div><div class="nmsingle-info"><div class="nmplayer-top"><span class="nmplayer-title">' . $title . ' - ' . $artist . '</span><span class="nmsingle-playtime"><span class="current-time">--:--</span> / <span class="duration">' . nm_format_time($duration) . '</span></span></div><div class="nmplayer-mid"><div class="nmplayer-control" data-index=' . $instance . '><span class="fxfont nm-previous"></span><span class="fxfont nm-next"></span><span class="nm-mute fxfont"></span></div><div class="nmsingle-lrc">(*+﹏+*)</div></div><div class="nmsingle-process" data-index="' . $instance . '"><div class="nmsingle-process-bar"></div></div></div></div>';
return '<div id="nm-player-' . $instance . '" class="nmsingle-container"><div class="nmsingle-cover" style="background-image:url(' . $cover . '?param=148x148)"><span class="nms-play-btn fxfont nm-play" data-index=' . $instance . '></span></div><div class="nmsingle-info"><div class="nmplayer-top"><span class="nmplayer-title">' . $title . ' - ' . $artist . '</span><span class="nmsingle-playtime"><span class="current-time">--:--</span> / <span class="duration">' . nm_format_time($duration) . '</span></span></div><div class="nmplayer-mid"><div class="nmplayer-control" data-index=' . $instance . '><span class="fxfont nm-previous"></span><span class="fxfont nm-next"></span><span class="nm-mute fxfont"></span><span class="list-triggle">列表</span></div><div class="nmsingle-lrc">(*+﹏+*)</div></div><div class="nmsingle-process" data-index="' . $instance . '"><div class="nmsingle-process-bar"></div></div></div></div>';
}

wp_embed_register_handler( 'neteasemusicalbum', '#http:\/\/music\.163\.com\/\#\/(\w+)\?id=(\d+)#i', 'wp_embed_handler_neteasemusicalbum' );

wp_embed_register_handler( 'xiami','#http:\/\/www\.xiami\.com\/(\w+)\/(\d+)#i','wp_embed_handler_xiami');

function wp_embed_handler_neteasemusicalbum( $matches, $attr, $url, $rawattr ) {
function wp_embed_handler_xiami( $matches, $attr, $url, $rawattr ){
if(! is_singular() ) return $url;
$type = $matches[1];
wp_enqueue_style('nms');
wp_enqueue_script('nm');
wp_enqueue_script('nms');
$id = $matches[2];
global $nmjson;
static $instance = 0;
global $instance;

$instance = $instance ? $instance : 0;

if( $instance === 0) $html ='<script>var playlist = []</script><div id="nm_jplayer" style="display:none"></div>';
switch ($type) {
case 'album':
$data = $nmjson->xiami_album($id);
$songs = $data['songs'];
$html .= nm_single_playform( $data['album_id'] , $instance , $data['album_cover'] , $data['album_title'] , $data['album_author'] , '' );
$class = nm_get_setting('listopen') ? '' : ' hide';

$html .= '<div class="nms-list' . $class . '" id="nm-list-' . $instance . '" data-index="' . $instance . '">';

foreach ($songs as $key => $song) {
$html .= '<div class="nms-list-item">' . $song['title'] . ' - ' . $song['artist'] . '<span class="song-time">' . nm_format_time( $song['duration'] ) . '</span></div>';
}
$html .= '</div>';
$html .= '<script>playlist.push(' . json_encode($songs). ');</script>';
break;

case 'song':
$data = $nmjson->xiami_song($id);
$html .= nm_single_playform( $data['id'] , $instance , $data['cover'] , $data['title'] , $data['artist'] , $data['duration'] );
$html .= '<script>playlist.push(' . json_encode(array($data)). ');</script>';
break;

case 'collect':
$data = $nmjson->xiami_collect($id);
$songs = $data['songs'];

$html .= nm_single_playform( $data['collect_id'] , $instance , $data['collect_cover'] , $data['collect_title'] , $data['collect_author'] , '' );
$class = nm_get_setting('listopen') ? '' : ' hide';

$html .= '<div class="nms-list' . $class . '" id="nm-list-' . $instance . '" data-index="' . $instance . '">';

foreach ($songs as $key => $song) {
$html .= '<div class="nms-list-item"><span class="song-info">' . $song['title'] . ' - ' . $song['artist'] . '</span></div>';
}
$html .= '</div>';
$html .= '<script>playlist.push(' . json_encode($songs). ');</script>';
break;
default:
return $url;
break;
}
$instance++;
return apply_filters( 'embed_forbes', $html, $matches, $attr, $url, $rawattr );
}

function wp_embed_handler_neteasemusicalbum( $matches, $attr, $url, $rawattr ) {
if(! is_singular() ) return $url;
$type = $matches[1];
wp_enqueue_style('nms');
wp_enqueue_script('nm');
wp_enqueue_script('nms');
$id = $matches[2];
global $nmjson;
global $instance;
$instance = $instance ? $instance : 0;
if( $instance === 0) $html ='<script>var playlist = []</script><div id="nm_jplayer" style="display:none"></div>';
switch ($type) {
case 'album':
$data = $nmjson->netease_album($id);
$songs = $data['songs'];
$html .= nm_single_playform( $data['album_id'] , $instance , $data['album_cover'] , $data['album_title'] , $data['album_author'] , '' );
$html .= '<div class="nms-list" id="nm-list-' . $instance . '" data-index="' . $instance . '">';
$class = nm_get_setting('listopen') ? '' : ' hide';

$html .= '<div class="nms-list' . $class . '" id="nm-list-' . $instance . '" data-index="' . $instance . '">';
foreach ($songs as $key => $song) {
$html .= '<div class="nms-list-item">' . $song['title'] . ' - ' . $song['artist'] . '<span class="song-time">' . nm_format_time( $song['duration'] ) . '</span></div>';
}
Expand All @@ -73,9 +134,11 @@ function wp_embed_handler_neteasemusicalbum( $matches, $attr, $url, $rawattr ) {
case 'playlist':
$data = $nmjson->netease_playlist($id);
$songs = $data['songs'];
//var_dump($songs);

$html .= nm_single_playform( $data['collect_id'] , $instance , $data['collect_cover'] , $data['collect_title'] , $data['collect_author'] , '' );
$html .= '<div class="nms-list" id="nm-list-' . $instance . '" data-index="' . $instance . '">';
$class = nm_get_setting('listopen') ? '' : ' hide';

$html .= '<div class="nms-list' . $class . '" id="nm-list-' . $instance . '" data-index="' . $instance . '">';
foreach ($songs as $key => $song) {
$html .= '<div class="nms-list-item"><span class="song-info">' . $song['title'] . ' - ' . $song['artist'] . '</span><span class="song-time">' . nm_format_time( $song['duration'] ) . '</span></div>';
}
Expand Down
14 changes: 14 additions & 0 deletions functions/nm-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div><h2>自定义歌单</h2><br>
<form id="nm-form">
<input id="ssfgg" name="url" type="text">
<input type="submit" value="添加专辑">
</form>
<div id="nm-private-list" class="nm-p-lists">
<div v-for="item in items" class="nm-p-item">
<img src="{{ item.img }}" width=150 height=150>
<div class="item-title">{{ item.title }}</div>
<button v-on:click="greet">Greet</button>
</div>
</div>
</div>
9 changes: 9 additions & 0 deletions functions/nm-setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@
<p class="description">显示歌曲热门评论。</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="<?php echo nm_setting_key('listopen');?>">展开列表</label></th>
<td>
<label for="<?php echo nm_setting_key('listopen');?>">
<input type="checkbox" name="<?php echo nm_setting_key('listopen');?>" id="listopen" value="1" <?php if(nm_get_setting("listopen")) echo 'checked="checked"';?>>
</label>
<p class="description">默认展开歌曲列表。</p>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="<?php echo nm_setting_key('tworow');?>">歌曲列表三列</label></th>
<td>
Expand Down
Loading

0 comments on commit 3e20ebb

Please sign in to comment.