diff --git a/README.md b/README.md index 5e512c4..33fa8a9 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,16 @@ WP-Player 一个迷你歌曲播放器,支持多歌曲播放,支持使用虾 ## Demo参考 +### Update Ver 2.2.0 (2015-01-01) +1. 新增网易云音乐网址的调用(支持单音乐页面、专辑页面、艺人页面、精选集[即歌单]页面) +2. 因网易的接口一些特殊性,如在后台选择网站类型中没有出现网易音乐,则您当前的站点暂不支持网易云音乐。 +3. 其次要感谢牧风的音乐播放插件,作为参考实现了网易接口的思路。 +4. 在次还要感谢此文作者 https://github.com/yanunon/NeteaseCloudMusic/wiki/网易云音乐API分析 +5. 修正播放器请求容错处理方案。 +6. 去除短代码中 00:00 字样。 +7. 修正播放器样式问题。 +8. 如想用 v2.1.0 老版本,可去 https://github.com/webjyh/WP-Player/releases 下载 + ### Update Ver 2.1.0 (2014-12-27) 1. 因发现最近播放器在解析虾米地址时,出现不稳定的情况,采用最新发现的虾米接口。 2. 新接口将是直接调取虾米网接口,速度更快且稳定。 diff --git a/css/wp-player-plugin.css b/css/wp-player-plugin.css index ae1b007..86c2a81 100644 --- a/css/wp-player-plugin.css +++ b/css/wp-player-plugin.css @@ -3,9 +3,9 @@ * @desc WP-Player MetaBox 样式 * @author M.J * @date 2014-12-19 - * @update 2014-12-27 + * @update 2015-01-01 * @URL http://webjyh.com - * @version 2.1.0 + * @version 2.2.0 */ .wp-player-wrap { padding: 10px; } .wp-player-wrap .wp-player-tabs { margin: 0px; border-bottom: 1px solid #EEE; height: 36px; } @@ -13,7 +13,8 @@ .wp-player-wrap .wp-player-tabs li a { display: inline-block; *zoom: 1; padding: 0px 15px; height: 36px; line-height:36px; color: #333; text-decoration: none; margin-right: 10px; border: 1px solid #FFF; border-bottom: 0; } .wp-player-wrap .wp-player-tabs li.current a { border-color: #EEE; background: #FFF; font-weight: bold; } .wp-player-wrap .wp-player-row { border: 1px solid #EEE; padding:10px 15px; border-top: none; } +.wp-player-wrap .wp-player-row .red { color: red; } .wp-player-wrap .wp-player-inner { display: none; } .wp-player-wrap .wp-player-inner.current { display: block; } -.wp-player-wrap .wp-player-text { height: 28px; width: 60%; } +.wp-player-wrap .wp-player-text { height: 28px; width: 50%; } .wp-player-wrap .wp-player-select { vertical-align: top; } \ No newline at end of file diff --git a/css/wp-player.css b/css/wp-player.css index 95c0a49..14e5b8a 100644 --- a/css/wp-player.css +++ b/css/wp-player.css @@ -3,10 +3,10 @@ * @desc WP-Player 样式表 * @author M.J * @date 2014-12-21 - * @update 2014-12-27 + * @update 2015-01-01 * @URL http://webjyh.com * @Github https://github.com/webjyh/WP-Player - * @version 2.1.0 + * @version 2.2.0 * */ .wp-player { overflow: hidden; background: #FFF; width: 350px; min-width: 300px; margin: 10px auto; border: 1px solid #DDD; box-shadow: 0px 0px 10px rgba(0,0,0, 0.15); box-sizing: content-box; } @@ -24,7 +24,7 @@ .wp-player .wp-player-box { height: 90px; } .wp-player .wp-player-thumb { position: relative; float: left; width: 90px; height: 90px; overflow: hidden; } -.wp-player .wp-player-thumb img { padding: 0; margin: 0; border-radius: 0; box-shadow: none; border: 0; display: block; width: 90px; height: 90px; vertical-align: top; } +.wp-player .wp-player-thumb img { padding: 0 !important; margin: 0!important; border-radius: 0!important; box-shadow: none!important; border: 0!important; display: block; width: 90px; height: 90px; vertical-align: top; } .wp-player .wp-player-playing { display: none; position: absolute; z-index: 2; top: 0; left: 0; width: 90px; height: 90px; background: #000; background: rgba(0, 0, 0, 0.4); filter: alpha(opacity=40); } .wp-player .wp-player-playing span { display: block; position: absolute; top: 50%; left: 50%; margin: -5px 0px 0px -5px; z-index: 3; width: 11px; height: 11px; background: url(../images/playing.gif) no-repeat; } .wp-player .wp-player-panel { margin-left: 100px; padding-top: 0px; padding-right: 10px; } @@ -48,10 +48,10 @@ .wp-player .wp-player-list { border-top: 1px solid #DDD; max-height: 301px; overflow: auto; } .wp-player .wp-player-list ul { margin: 0; padding: 0; border: 0; list-style: none; } -.wp-player .wp-player-list ul li { background:#FFF; margin: 0; padding: 0; border: 0; cursor: pointer; list-style: none; height: 22px; line-height: 22px; padding: 4px 0px; } +.wp-player .wp-player-list ul li { background:#FFF; margin: 0!important; padding: 0; border: 0!important; cursor: pointer; list-style: none; height: 22px; line-height: 22px; padding: 4px 0px; } .wp-player .wp-player-list ul li:after { content: ''; } .wp-player .wp-player-list ul li:before { content: ''; } -.wp-player .wp-player-list ul li a { outline: 0; border: 0; background:none; height: 22px; overflow: hidden; line-height: 22px; padding: 0px 10px; display: block; *zoom: 1; color: #333; border-left: 4px solid #FFF; text-decoration: none; } +.wp-player .wp-player-list ul li a { outline: 0!important; border: 0; background:none!important; height: 22px; overflow: hidden; line-height: 22px; padding: 0px 10px; display: block; *zoom: 1; color: #333; border-left: 4px solid #FFF; text-decoration: none; } .wp-player .wp-player-list ul li.odd { line-height: 22px; background: #f7f7f7; } .wp-player .wp-player-list ul li.odd a { line-height: 22px; border-color: #f7f7f7; } .wp-player .wp-player-list ul li.current a, diff --git a/include/metaboxes.php b/include/metaboxes.php index 3d7b996..fe305e4 100644 --- a/include/metaboxes.php +++ b/include/metaboxes.php @@ -12,9 +12,9 @@ */ function wp_player_create_meta_box() { - global $theme_name; - add_meta_box( 'wp-post-meta-boxes', 'WP-Player 播放器选项', 'wpPlayer_post_meta_boxes', 'post', 'normal', 'high' ); - add_meta_box( 'wp-page-meta-boxes', 'WP-Player 播放器选项', 'wpPlayer_post_meta_boxes', 'page', 'normal', 'high' ); + global $theme_name; + add_meta_box( 'wp-post-meta-boxes', 'WP-Player 播放器选项', 'wpPlayer_post_meta_boxes', 'post', 'normal', 'high' ); + add_meta_box( 'wp-page-meta-boxes', 'WP-Player 播放器选项', 'wpPlayer_post_meta_boxes', 'page', 'normal', 'high' ); } /** @@ -22,66 +22,78 @@ function wp_player_create_meta_box() { * function filterable to add options through child themes. */ function wp_player_meta_boxes( $val = true, $apply = false ) { - $arr1 = array( - 'mp3_xiami_type' => array( - 'name' => 'mp3_xiami_type', - 'type' => 'select', - 'options' => array( - 'song' => '单音乐页面', - 'album' => '专辑页面', - 'artist' => '艺人页面', - 'collect' => '精选集页面' - ), - 'output' => false - ), - 'mp3_xiami' => array( - 'name' => 'mp3_xiami', - 'type' => 'text', - 'description' => '即可填音乐写地址ID,也可填写虾米音乐网址 http://......', - 'button' => '获取音乐ID', - 'output' => false - ), - ); - - $arr2 = array( - 'mp3_title' => array( - 'name' => 'mp3_title', - 'type' => 'text', - 'title' => '歌曲名', - 'description' => '请填写歌曲名', - 'output' => true - ), - 'mp3_author' => array( - 'name' => 'mp3_author', - 'type' => 'text', - 'title' => '歌手名', - 'description' => '请填写歌手名', - 'output' => true - ), - 'mp3_address' => array( - 'name' => 'mp3_address', - 'type' => 'upload', - 'title' => '歌曲地址', - "description" => '在这里可以上传MP3文件,也可以自己填写MP3外链地址,默认请带上 http://', - 'output' => true - ), - 'mp3_thumb' => array( - 'name' => 'mp3_thumb', - 'type' => 'upload', - 'title' => '歌曲封面', - "description" => '上传封面,可以为空,WP-Player 将使用默认图', - 'output' => true - ) - ); - - $meta_boxes = $val ? $arr1 : $arr2; - - if ( $apply ){ - $meta_boxes = array(); - $meta_boxes = array_merge( $arr1, $arr2 ); - } - - return apply_filters( 'wp_player_meta_boxes', $meta_boxes ); + $arr1 = array( + 'wp_player_music_type' => array( + 'name' => 'wp_player_music_type', + 'type' => 'select', + 'options' => array( + 'xiami' => '虾米音乐' + ), + 'output' => false + ), + 'mp3_xiami_type' => array( + 'name' => 'mp3_xiami_type', + 'type' => 'select', + 'options' => array( + 'song' => '单音乐页面', + 'album' => '专辑页面', + 'artist' => '艺人页面', + 'collect' => '精选集页面' + ), + 'output' => false + ), + 'mp3_xiami' => array( + 'name' => 'mp3_xiami', + 'type' => 'text', + 'description' => '即可填音乐写地址ID,也可填写虾米音乐网址 http://......', + 'button' => '获取音乐ID', + 'output' => false + ) + ); + + $arr2 = array( + 'mp3_title' => array( + 'name' => 'mp3_title', + 'type' => 'text', + 'title' => '歌曲名', + 'description' => '请填写歌曲名', + 'output' => true + ), + 'mp3_author' => array( + 'name' => 'mp3_author', + 'type' => 'text', + 'title' => '歌手名', + 'description' => '请填写歌手名', + 'output' => true + ), + 'mp3_address' => array( + 'name' => 'mp3_address', + 'type' => 'upload', + 'title' => '歌曲地址', + "description" => '在这里可以上传MP3文件,也可以自己填写MP3外链地址,默认请带上 http://', + 'output' => true + ), + 'mp3_thumb' => array( + 'name' => 'mp3_thumb', + 'type' => 'upload', + 'title' => '歌曲封面', + "description" => '上传封面,可以为空,WP-Player 将使用默认图', + 'output' => true + ) + ); + + if ( function_exists('curl_init') ){ + $arr1['wp_player_music_type']['options']['netease'] = '网易音乐'; + } + + $meta_boxes = $val ? $arr1 : $arr2; + + if ( $apply ){ + $meta_boxes = array(); + $meta_boxes = array_merge( $arr1, $arr2 ); + } + + return apply_filters( 'wp_player_meta_boxes', $meta_boxes ); } @@ -109,33 +121,33 @@ function get_wp_player_metaBox( $val = true ){ */ function wpPlayer_post_meta_boxes() { - global $post; ?> -
- -
-
-

填写方法

-
    -
  1. 在虾米网打开喜欢的歌曲页面,复制歌曲页面的网址如:http://www.xiami.com/song/2078022......
  2. -
  3. 并将复制的网址填写到后面的表单内。音乐类型将根据网址自动做出选择。
  4. -
  5. 点击获取音乐ID按钮,此时音乐ID出现在表单中。
  6. -
  7. 将短代码 [player autoplay="1"] 填入您的文章内容中。
  8. -
  9. 短代码中 autoplay 表示是否自动播放;参数"0"表示否;"1"表示是;
  10. -
  11. 支持播放歌单:单音乐页面、专辑页面、艺人页面、精选集页面。
  12. -
  13. PS:建议使用网址来获取音乐ID。
  14. -
-
-
-
-
-
+ global $post; ?> +
+ +
+
+

填写方法

+
    +
  1. 请注意:如果您的选择音乐网站中出现了网易音乐,说明可以支持网易云音乐网址。
  2. +
  3. 在虾米网或网易云音乐打开喜欢的歌曲页面,复制歌曲页面的网址如:http://www.xiami.com/song/2078022......
  4. +
  5. 并将复制的网址填写到后面的表单内。音乐类型将根据网址自动做出选择。
  6. +
  7. 点击获取音乐ID按钮,此时音乐ID出现在表单中。
  8. +
  9. 将短代码 [player autoplay="1"] 填入您的文章内容中。
  10. +
  11. 短代码中 autoplay 表示是否自动播放;参数"0"表示否;"1"表示是;
  12. +
  13. 支持播放歌单:单音乐页面、专辑页面、艺人页面、精选集页面(即网易云音乐歌单)。
  14. +
  15. PS:建议使用网址来获取音乐ID。
  16. +
+
+
+
+
+

'.$title.'

' : "\n"; - $html .= ''; - $html .= ''; - if ( $button ){ - $html .= "\n".''; - } - $html .= $output ? '

'."\n" : "\n"; + $html .= $output ? '

'.$title.'

' : "\n"; + $html .= ''; + $html .= ''; + if ( $button ){ + $html .= "\n".''; + } + $html .= $output ? '

'."\n" : "\n"; - echo $html; + echo $html; } /** @@ -162,18 +174,18 @@ function wp_player_get_meta_text_input( $args = array(), $value = false ) { */ function wp_player_get_meta_select( $args = array(), $value = false ) { - extract( $args ); + extract( $args ); - $html .= $output ? '

'.$title.'

' : "\n"; - $html .= ''; - $html .= $output ? '

'."\n" : "\n"; + $html .= $output ? '

'.$title.'

' : "\n"; + $html .= ''; + $html .= $output ? '

'."\n" : "\n"; - echo $html; + echo $html; } /** @@ -182,15 +194,15 @@ function wp_player_get_meta_select( $args = array(), $value = false ) { */ function wp_player_get_meta_upload( $args = array(), $value = false ) { - extract( $args ); + extract( $args ); - $html .= $output ? '

'.$title.'

' : "\n"; - $html .= ''."\n"; - $html .= ''; - $html .= ''; - $html .= $output ? '

'."\n" : "\n"; + $html .= $output ? '

'.$title.'

' : "\n"; + $html .= ''."\n"; + $html .= ''; + $html .= ''; + $html .= $output ? '

'."\n" : "\n"; - echo $html; + echo $html; } /** @@ -199,35 +211,35 @@ function wp_player_get_meta_upload( $args = array(), $value = false ) { */ function wp_player_save_meta_data( $post_id ) { - global $post; + global $post; - $meta_boxes = wp_player_meta_boxes( true, true ); + $meta_boxes = wp_player_meta_boxes( true, true ); - foreach ( $meta_boxes as $meta_box ){ - - if ( 'page' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ) ){ - return $post_id; - } + foreach ( $meta_boxes as $meta_box ){ + + if ( 'page' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ) ){ + return $post_id; + } - if ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) ){ - return $post_id; - } + if ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) ){ + return $post_id; + } - if ( !wp_verify_nonce( $_POST[$meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) ){ - return $post_id; - } + if ( !wp_verify_nonce( $_POST[$meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) ){ + return $post_id; + } - $data = stripslashes( $_POST[$meta_box['name']] ); + $data = stripslashes( $_POST[$meta_box['name']] ); - if ( get_post_meta( $post_id, $meta_box['name'] ) == '' ){ - add_post_meta( $post_id, $meta_box['name'], $data, true ); - } elseif ( $data != get_post_meta( $post_id, $meta_box['name'], true ) ){ - update_post_meta( $post_id, $meta_box['name'], $data ); - }elseif ( $data == '' ){ - delete_post_meta( $post_id, $meta_box['name'], get_post_meta( $post_id, $meta_box['name'], true ) ); - } + if ( get_post_meta( $post_id, $meta_box['name'] ) == '' ){ + add_post_meta( $post_id, $meta_box['name'], $data, true ); + } elseif ( $data != get_post_meta( $post_id, $meta_box['name'], true ) ){ + update_post_meta( $post_id, $meta_box['name'], $data ); + }elseif ( $data == '' ){ + delete_post_meta( $post_id, $meta_box['name'], get_post_meta( $post_id, $meta_box['name'], true ) ); + } - } + } } ?> \ No newline at end of file diff --git a/include/player.php b/include/player.php index c60500d..6d029db 100644 --- a/include/player.php +++ b/include/player.php @@ -4,158 +4,231 @@ */ if ( !class_exists( 'wp_player_plugin' ) ){ - $WP_PLAYER_VERSION = '2.1.0'; - - class wp_player_plugin { - - function __construct(){ - - //Include MetaBox - require_once('metaboxes.php'); - - add_action( "admin_menu", array( $this, 'options_menu' ) ); - add_filter( 'plugin_action_links', array( $this, 'wp_player_add_link' ), 10, 4 ); - add_action( 'wp_enqueue_scripts', array( $this, 'wp_player_scripts') ); - add_action( 'admin_print_styles', array( $this, 'wp_player_admin_css' ) ); - add_action( "admin_print_scripts", array( $this, 'wp_player_admin_head' ) ); - add_shortcode( 'player', array( $this, 'wp_player_shortcode' ) ); - - $this->options = get_option( 'wp_player_options' ); - $this->base_dir = WP_PLUGIN_URL.'/'. dirname( plugin_basename( dirname( __FILE__ ) ) ).'/'; - $this->admin_dir = site_url( '/wp-admin/options-general.php?page=player.php' ); - - } - - //Register Menu - function options_menu(){ - add_options_page( 'WP-Player 设置', 'WP-Player 设置', 'manage_options', basename( __FILE__ ), array( $this, 'printAdminPage' ) ); - add_action( 'admin_init', array( $this, 'wp_player_settings' )); - } - - //Register WP-Player Setting - function wp_player_settings() { - register_setting( 'wp_player_settings_group', 'wp_player_options' ); - } - - //Register setting submit - function wp_player_add_link( $action_links, $plugin_file, $plugin_data, $context ){ - if (strip_tags($plugin_data['Title']) == 'WP-Player') { - $wp_player_links = '设定'; - array_unshift( $action_links, $wp_player_links ); - } - return $action_links; - } - - //Include scripts - function wp_player_scripts(){ - global $WP_PLAYER_VERSION; - $options = $this->options; - wp_enqueue_style( 'wp-player', $this->base_dir . 'css/wp-player.css', array(), $WP_PLAYER_VERSION, 'screen' ); - if( is_array( $options ) && $options['jQuery'] == 'true' ){ - wp_enqueue_script( 'jquery' ); - } - wp_enqueue_script( 'wp-player-jplayer', $this->base_dir . 'js/soundmanager2.js', array(), $WP_PLAYER_VERSION, true ); - wp_enqueue_script( 'wp-player', $this->base_dir . 'js/wp-player.js', array(), $WP_PLAYER_VERSION, true ); - - wp_localize_script( 'wp-player', 'wp_player_params', - array( - 'swf' => $this->base_dir.'js/', - 'single' => ( is_single() || is_page() ) ? 'true' : 'false' - )); - } - - //add Admin WP-Player CSS - function wp_player_admin_css(){ - global $WP_PLAYER_VERSION; - wp_enqueue_style( 'wp-player-plugin', plugins_url( 'css/wp-player-plugin.css', dirname( __FILE__ ) ), array(), $WP_PLAYER_VERSION ); - } - - //add Admin Upload script - function wp_player_admin_head(){ - global $WP_PLAYER_VERSION; - wp_enqueue_script( 'jquery' ); - wp_enqueue_script('media-upload'); - wp_enqueue_script('thickbox'); - wp_enqueue_script( 'wp-plugin-uploader', plugins_url( 'js/plugin-uploader.js', dirname( __FILE__ ) ), array('jquery','media-upload','thickbox'), $WP_PLAYER_VERSION ); - } - - //add shortcode - function wp_player_shortcode( $atts ){ - global $post; - - extract( shortcode_atts( array( 'autoplay' => 0 ), $atts ) ); - - $type = get_post_meta( $post->ID, 'mp3_xiami_type', true ); - $xiami = get_post_meta( $post->ID, 'mp3_xiami', true ); - $title = get_post_meta( $post->ID, 'mp3_title', true ); - $author = get_post_meta( $post->ID, 'mp3_author', true ); - $file = get_post_meta( $post->ID, 'mp3_address', true ); - $thumb = get_post_meta( $post->ID, 'mp3_thumb', true ); - $options = $this->options; - - if ( empty( $thumb ) ){ - $thumb = $this->base_dir.'images/default.png'; - } - - return '
00:00
    '; - } - - //WP-Player Admin Option Page - function printAdminPage(){ - ?> -
    -

    WP-Player 设置


    -
    - - options; ?> - - - - - - - - - - - - - - - -
    - 文章中插入短代码:
    -
      -
    1. [player]
    2. -
    3. [player autoplay="0"]
    4. -
    -
    - 提供了MetaBox来填写参数
    -
      -
    1. 在虾米网打开喜欢的歌曲页面,复制歌曲页面的网址如:http://www.xiami.com/song/2078022......
    2. -
    3. 并将复制的网址填写到后面的表单内。音乐类型将根据网址自动做出选择。
    4. -
    5. 点击获取音乐ID按钮,此时音乐ID出现在表单中。
    6. -
    7. 将短代码 [player autoplay="1"] 填入您的文章内容中。
    8. -
    9. 短代码中 autoplay 表示是否自动播放;参数"0"表示否;"1"表示是;
    10. -
    11. 支持播放歌单:单音乐页面、专辑页面、艺人页面、精选集页面。
    12. -
    13. PS:建议使用网址来获取音乐ID。
    14. -
    -
    -
    - -
    -
    -
    -
    - -
    -
    -
    + $WP_PLAYER_VERSION = '2.2.0'; + + class wp_player_plugin { + + function __construct(){ + + //Include MetaBox + require_once('metaboxes.php'); + + add_action( "admin_menu", array( $this, 'options_menu' ) ); + add_filter( 'plugin_action_links', array( $this, 'wp_player_add_link' ), 10, 4 ); + add_action( 'wp_enqueue_scripts', array( $this, 'wp_player_scripts') ); + add_action( 'admin_print_styles', array( $this, 'wp_player_admin_css' ) ); + add_action( 'admin_print_scripts', array( $this, 'wp_player_admin_head' ) ); + add_action( 'wp_ajax_nopriv_wpplayer', array( $this, 'wp_player_netease' ) ); + add_action( 'wp_ajax_wpplayer', array( $this, 'wp_player_netease' ) ); + add_shortcode( 'player', array( $this, 'wp_player_shortcode' ) ); + + $this->options = get_option( 'wp_player_options' ); + $this->base_dir = WP_PLUGIN_URL.'/'. dirname( plugin_basename( dirname( __FILE__ ) ) ).'/'; + $this->admin_dir = site_url( '/wp-admin/options-general.php?page=player.php' ); + + } + + //Register Menu + function options_menu(){ + add_options_page( 'WP-Player 设置', 'WP-Player 设置', 'manage_options', basename( __FILE__ ), array( $this, 'printAdminPage' ) ); + add_action( 'admin_init', array( $this, 'wp_player_settings' )); + } + + //Register WP-Player Setting + function wp_player_settings() { + register_setting( 'wp_player_settings_group', 'wp_player_options' ); + } + + //Register setting submit + function wp_player_add_link( $action_links, $plugin_file, $plugin_data, $context ){ + if (strip_tags($plugin_data['Title']) == 'WP-Player') { + $wp_player_links = '设定'; + array_unshift( $action_links, $wp_player_links ); + } + return $action_links; + } + + //Include scripts + function wp_player_scripts(){ + global $WP_PLAYER_VERSION; + $options = $this->options; + wp_enqueue_style( 'wp-player', $this->base_dir . 'css/wp-player.css', array(), $WP_PLAYER_VERSION, 'screen' ); + if( is_array( $options ) && $options['jQuery'] == 'true' ){ + wp_enqueue_script( 'jquery' ); + } + wp_enqueue_script( 'wp-player-jplayer', $this->base_dir . 'js/soundmanager2.js', array(), $WP_PLAYER_VERSION, true ); + wp_enqueue_script( 'wp-player', $this->base_dir . 'js/wp-player.js', array(), $WP_PLAYER_VERSION, true ); + + wp_localize_script( 'wp-player', 'wp_player_params', + array( + 'swf' => $this->base_dir.'js/', + 'url' => admin_url().'admin-ajax.php', + 'nonce' => wp_create_nonce('wp-player'), + 'single' => ( is_single() || is_page() ) ? 'true' : 'false' + )); + } + + //add Admin WP-Player CSS + function wp_player_admin_css(){ + global $WP_PLAYER_VERSION; + wp_enqueue_style( 'wp-player-plugin', plugins_url( 'css/wp-player-plugin.css', dirname( __FILE__ ) ), array(), $WP_PLAYER_VERSION ); + } + + //add Admin Upload script + function wp_player_admin_head(){ + global $WP_PLAYER_VERSION; + wp_enqueue_script( 'jquery' ); + wp_enqueue_script('media-upload'); + wp_enqueue_script('thickbox'); + wp_enqueue_script( 'wp-plugin-uploader', plugins_url( 'js/plugin-uploader.js', dirname( __FILE__ ) ), array('jquery','media-upload','thickbox'), $WP_PLAYER_VERSION ); + } + + /** @name Get Netease Song + * + * @Author: Mufeng + * @URL: http://mufeng.me/hermit-for-wordpress.html + * + * @Author: yanunon + * @URL: https://github.com/yanunon/NeteaseCloudMusic/wiki/%E7%BD%91%E6%98%93%E4%BA%91%E9%9F%B3%E4%B9%90API%E5%88%86%E6%9E%90 + **/ + function wp_player_netease(){ + $type = $_GET['type']; + $id = intval($_GET['id']); + $nonce = $_SERVER['HTTP_NONCE']; + + if ( !wp_verify_nonce($nonce, "wp-player") || !function_exists('curl_init') ) { + $JSON = array('status' => false, 'message' => '非法请求'); + } else { + switch ( $type ) { + case 'song': $url = "http://music.163.com/api/song/detail/?ids=[$id]"; $key = 'songs'; break; + case 'album': $url = "http://music.163.com/api/album/$id?id=$id"; $key = 'album'; break; + case 'artist': $url = "http://music.163.com/api/artist/$id?id=$id"; $key = 'artist'; break; + case 'collect': $url = "http://music.163.com/api/playlist/detail?id=$id"; $key = 'result'; break; + default: $url = "http://music.163.com/api/song/detail/?ids=[$id]"; $key = 'songs'; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Cookie: appver=2.0.2' )); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); + curl_setopt($ch, CURLOPT_REFERER, 'http://music.163.com/;'); + $cexecute = curl_exec($ch); + curl_close($ch); + + if ( $cexecute ) { + $result = json_decode($cexecute, true); + if ( $result['code'] == 200 && $result[$key] ){ + $JSON['status'] = true; + $JSON['message'] = ""; + + switch ( $key ){ + case 'songs' : $data = $result[$key]; break; + case 'album' : $data = $result[$key]['songs']; break; + case 'artist' : $data = $result['hotSongs']; break; + case 'result' : $data = $result[$key]['tracks']; break; + default : $data = $result[$key]; break; + } + + foreach ( $data as $keys => $data ){ + $JSON['data']['trackList'][] = array( + 'title' => $data['name'], + 'album_name' => $data['album']['name'], + 'artist' => $data['artists'][0]['name'], + 'location' => $data['mp3Url'], + 'pic' => $data['album']['blurPicUrl'].'?param=90x90' + ); + } + + } + } else { + $JSON = array('status' => false, 'message' => '非法请求'); + } + + } + + header('Content-type: application/json'); + echo json_encode($JSON); + die(); + } + + //add shortcode + function wp_player_shortcode( $atts ){ + global $post; + + extract( shortcode_atts( array( 'autoplay' => 0 ), $atts ) ); + + $source = get_post_meta( $post->ID, 'wp_player_music_type', true ); + $type = get_post_meta( $post->ID, 'mp3_xiami_type', true ); + $xiami = get_post_meta( $post->ID, 'mp3_xiami', true ); + $title = get_post_meta( $post->ID, 'mp3_title', true ); + $author = get_post_meta( $post->ID, 'mp3_author', true ); + $file = get_post_meta( $post->ID, 'mp3_address', true ); + $thumb = get_post_meta( $post->ID, 'mp3_thumb', true ); + $options = $this->options; + + if ( empty( $thumb ) ) $thumb = $this->base_dir.'images/default.png'; + if ( empty( $source ) ) $source = 'xiami'; + + return '
      '; + } + + //WP-Player Admin Option Page + function printAdminPage(){ + ?> +
      +

      WP-Player 设置


      +
      + + options; ?> + + + + + + + + + + + + + + + +
      + 文章中插入短代码:
      +
        +
      1. [player]
      2. +
      3. [player autoplay="0"]
      4. +
      +
      + 提供了MetaBox来填写参数
      +
        +
      1. 在虾米网或网易云音乐打开喜欢的歌曲页面,复制歌曲页面的网址如:http://www.xiami.com/song/2078022......
      2. +
      3. 并将复制的网址填写到后面的表单内。音乐类型将根据网址自动做出选择。
      4. +
      5. 点击获取音乐ID按钮,此时音乐ID出现在表单中。
      6. +
      7. 将短代码 [player autoplay="1"] 填入您的文章内容中。
      8. +
      9. 短代码中 autoplay 表示是否自动播放;参数"0"表示否;"1"表示是;
      10. +
      11. 支持播放歌单:单音乐页面、专辑页面、艺人页面、精选集页面。
      12. +
      13. PS:建议使用网址来获取音乐ID。
      14. +
      +
      +
      + +
      +
      +
      +
      + +
      +
      +
      \ No newline at end of file diff --git a/js/plugin-uploader.js b/js/plugin-uploader.js index c13336c..96adc60 100644 --- a/js/plugin-uploader.js +++ b/js/plugin-uploader.js @@ -1,64 +1,78 @@ /** - * @name Wp-Player Admin JS - * @desc MetaBox JavaScript - * @depend jQuery - * @author M.J - * @date 2014-12-19 - * @update 2014-12-27 - * @URL http://webjyh.com - * @version 2.1.0 - * - */ +* @name Wp-Player Admin JS +* @desc MetaBox JavaScript +* @depend jQuery +* @author M.J +* @date 2014-12-19 +* @update 2015-01-01 +* @URL http://webjyh.com +* @version 2.2.0 +* +*/ jQuery(document).ready(function() { - - //wp-player upload dialog - var original_send_to_editor = window.send_to_editor; - jQuery('.WP-Player-File').on('click', function() { - formField = jQuery(this).prev().attr('id'); - tb_show('', 'media-upload.php?media-upload.php?type=image&TB_iframe=true'); - - window.send_to_editor = function(html){ - fileUrl = jQuery( html ).attr('href'); - jQuery( '#'+formField ).val( fileUrl ); - tb_remove(); - window.send_to_editor = original_send_to_editor; - } - return false; - }); - - //wp-player Tabs - jQuery('#wp-player-tabs > li').on('click', function(){ - var index = jQuery(this).index(); - jQuery(this).addClass('current').siblings().removeClass('current'); - jQuery('#wp-player-row > div').hide().eq(index).fadeIn(); - }); - - //get Xiami ID - jQuery('#wp_player_get_xiami_id').on('click', function(){ - var $elem = jQuery('#mp3_xiami'), - $select = jQuery('#mp3_xiami_type'), - $val = $elem.val(), - reg = /^http[s]?:\/\/\w*[\.]?xiami.com+\/(\w+)\/+(\d+).*$/, - xiami = {}; - if ( typeof $val === 'undefined' || $val == '' ){ - $elem.focus(); - } + //wp-player upload dialog + var original_send_to_editor = window.send_to_editor; + jQuery('.WP-Player-File').on('click', function() { + formField = jQuery(this).prev().attr('id'); + tb_show('', 'media-upload.php?media-upload.php?type=image&TB_iframe=true'); + + window.send_to_editor = function(html){ + fileUrl = jQuery( html ).attr('href'); + jQuery( '#'+formField ).val( fileUrl ); + tb_remove(); + window.send_to_editor = original_send_to_editor; + } + return false; + }); - var result = $val.match(reg); - if (jQuery.isArray( result )){ - xiami['type'] = result[1]; - xiami['id'] = result[2]; - } else { - if (!jQuery.isNumeric($val)){ - alert('获取虾米音乐ID失败!') - } - } + //wp-player Tabs + jQuery('#wp-player-tabs > li').on('click', function() { + var index = jQuery(this).index(); + jQuery(this).addClass('current').siblings().removeClass('current'); + jQuery('#wp-player-row > div').hide().eq(index).fadeIn(); + }); - if ( jQuery.isArray( result ) && xiami['type'] && xiami['id'] ){ - $elem.val( xiami['id'] ); - $select.children('option').prop('selected', false); - jQuery('#mp3_xiami_type').find('option[value='+xiami['type']+']').prop('selected', true); - } - }); + //get Music ID + jQuery('#wp_player_get_xiami_id').on('click', function() { + var $type = jQuery('#wp_player_music_type'), + $len = $type.find('option').length, + $elem = jQuery('#mp3_xiami'), + $select = jQuery('#mp3_xiami_type'), + $val = $elem.val(), + regs = [/^http[s]?:\/\/\w*[\.]?xiami.com+\/(\w+)\/+(\d+).*$/, /^http:\/\/music.163.com\/#.*\/{1}(.+)\?id=(\d+)$/], + typeArr = ['xiami', 'netease'], + result = {}, + mark = null; + + if (typeof $val === 'undefined' || $val == '') { + $elem.focus(); + } + + var reg = ($val.indexOf('163.com') > -1 && $len > 1) ? mark = 1 : mark = 0, + row = $val.match(regs[mark]); + + if (mark == 0 && !row && !jQuery.isNumeric($val)){ + alert('您的当前站点只支持虾米音乐网址'); + return false; + } + + if (jQuery.isArray(row)) { + result['type'] = row[1]; + result['id'] = row[2]; + } else { + if (!jQuery.isNumeric($val)){ + alert('获取音乐ID失败!') + } + } + + if ( jQuery.isArray( row ) && result['type'] && result['id'] ){ + if ( result['type'] == 'playlist' ) result['type'] = 'collect'; + $elem.val( result['id'] ); + $type.children('option').prop('selected', false); + $select.children('option').prop('selected', false); + $type.find('option[value='+typeArr[mark]+']').prop('selected', true); + $select.find('option[value='+result['type']+']').prop('selected', true); + } + }); }); \ No newline at end of file diff --git a/js/wp-player.js b/js/wp-player.js index 2542373..36b944f 100644 --- a/js/wp-player.js +++ b/js/wp-player.js @@ -4,77 +4,99 @@ * @depend jQuery, SoundManager2 * @author M.J * @date 2014-12-21 - * @update 2014-12-27 + * @update 2015-01-01 * @URL http://webjyh.com * @Github https://github.com/webjyh/WP-Player * @reutn {jQuery} - * @version 2.1.0 + * @version 2.2.0 * */ -~function($, soundManager){ +~function($, soundManager) { - var WPPlayer = function(elem, options){ + var WPPlayer = function(elem, options) { soundManager.setup({ url: options.swf, debugMode: false }); this.index = 0; + this.url = options.url, + this.nonce = options.nonce, this.IE6 = !-[1,] && !window.XMLHttpRequest; this.single = options.single; - this.getDOM( $(elem) ).getAttr().init(); + this.getDOM($(elem)).getAttr().init(); }; WPPlayer.prototype = { // 初始化 - init: function(){ + init: function() { var attr = this.attr, DOM = this.DOM; - + + DOM.time.text('00:00'); DOM.title.text('Loading...'); DOM.author.text('Loading...'); - - ( typeof attr.xiami !== 'undefined' && $.isNumeric( attr.xiami ) ) ? this.xiamiAction() : this.localAction(); + + //各类型操作 + if (!attr.xiami) { + this.localAction(); + } else { + (attr.source == 'xiami') ? this.xiamiAction() : this.neteaseAction(); + } + }, + + // 网易云音乐操作 + neteaseAction: function() { + var type = this.attr.type, + id = this.attr.xiami, + _this = this; + $.ajax({ url: this.url, type: 'get', headers: { nonce: this.nonce }, data: { action: 'wpplayer', type: type, id: id } }) + .done(function(json) { + if (json.status && json.data.trackList){ + _this.data = json.data.trackList; + _this.createList().createSound().addEvent(); + } + }); }, // 虾米类型操作 - xiamiAction: function(){ - var type = this.getXiamiType( this.attr.type ), + xiamiAction: function() { + var type = this.getXiamiType(this.attr.type), xiami = this.attr.xiami, _this = this, url = 'http://www.xiami.com/song/playlist/id/'+xiami+'/type/'+type+'/cat/json?callback=?'; - $.getJSON( url, function(json){ - if ( json.status && json.data.trackList ){ - - for (var i=0, length = json.data.trackList.length; i 0 && data.data.length > 0 ){ + $.getJSON('http://wpplayer.sinaapp.com/?callback=?', { act: type, id: xiami }, function(data) { + if (data.code > 0 && data.data.length) { _this.data = data.data; _this.createList().createSound().addEvent(); } - }) + }); }, // 本地上传操作 - localAction: function(){ + localAction: function() { if (typeof this.attr.address === 'undefined') return false; var data = { title: this.attr.title, @@ -87,14 +109,14 @@ }, // 创建音乐列表 - createList: function(){ + createList: function() { var i = 0, tpl = '', DOM = this.DOM, - data = this.data; + data = this.data, len = data.length; - for ( ; i 100 ? '100%' : position.toFixed(5) + '%'; - if ( _this.timeReady ) { + if (_this.timeReady) { pre = '-'; count = Math.floor((this.duration - this.position) / 1000); minute = _this.formatNumber( Math.floor( count / 60 ) ); @@ -154,17 +176,17 @@ } DOM.playbar.width(playbar); - DOM.time.text( pre + minute +':'+ second ); + DOM.time.text(pre + minute +':'+ second); }, - whileloading: function(){ - var seekbar = this.bytesTotal ? ( this.bytesLoaded / this.bytesTotal ) * 100 : 100; + whileloading: function() { + var seekbar = this.bytesTotal ? (this.bytesLoaded / this.bytesTotal) * 100 : 100; DOM.seekbar.width(seekbar+'%'); } }); _this.soundEvent(); - if ( typeof val !== 'undefined' || autoplay ) _this.sound.play(); + if (typeof val !== 'undefined' || autoplay) _this.sound.play(); }); @@ -172,26 +194,26 @@ }, //播放器事件 - addEvent: function(){ + addEvent: function() { var DOM = this.DOM, _this = this; //showList - DOM.wrap.on('click', 'div.wp-player-list-btn', function(){ + DOM.wrap.on('click', 'div.wp-player-list-btn', function() { var has = $(this).hasClass('wp-player-open'); $(this)[has ? 'removeClass' : 'addClass']('wp-player-open'); DOM.list.stop(true,true)[has ? 'slideDown': 'slideUp']('fast'); }); // list select - DOM.list.on('click', 'li', function(){ - var index = parseInt( $(this).attr('data-index'), 10), + DOM.list.on('click', 'li', function() { + var index = parseInt($(this).attr('data-index'), 10), has = $(this).hasClass('current') && _this.sound.playState > 0; - ( index < 0 || index > _this.data.length-1 ) ? _this.index = 0 : _this.index = index; + (index < 0 || index > _this.data.length-1) ? _this.index = 0 : _this.index = index; - if ( has && !_this.sound.paused ){ + if (has && !_this.sound.paused) { _this.sound.pause(); - } else if ( has && _this.sound.paused ){ + } else if (has && _this.sound.paused) { _this.sound.resume() } else { _this.reset().setList().createSound(_this.index); @@ -202,59 +224,59 @@ }, // SoundManage Event - soundEvent: function(){ + soundEvent: function() { var DOM = this.DOM, _this = this; //sound play - DOM.seekbar.off().on('click', function(event){ _this.seekbar(event) }); - DOM.play.off().on('click', function(){ _this.play() }); - DOM.stop.off().on('click', function(){ _this.stop() }); + DOM.seekbar.off().on('click', function(event) { _this.seekbar(event) }); + DOM.play.off().on('click', function() { _this.play() }); + DOM.stop.off().on('click', function() { _this.stop() }); //prev, next - if ( this.data.length > 2 ){ - DOM.previous.off().on('click', function(){ _this.prevSound() }); - DOM.next.off().on('click', function(){ _this.nextSound() }); + if (this.data.length > 2) { + DOM.previous.off().on('click', function() { _this.prevSound() }); + DOM.next.off().on('click', function() { _this.nextSound() }); } }, // 播放进度 Event - seekbar: function(event){ + seekbar: function(event) { var DOM = this.DOM, _x = event.offsetX ? event.offsetX : (event.clientX - DOM.progress.offset().left).toFixed(0); - var offsetX = ( _x / DOM.progress.width() ) * this.sound.duration; - if ( offsetX < 0 ) offsetX = 0; - if ( offsetX > this.sound.duration ) offsetX = this.sound.duration; + var offsetX = (_x / DOM.progress.width()) * this.sound.duration; + if (offsetX < 0) offsetX = 0; + if (offsetX > this.sound.duration) offsetX = this.sound.duration; this.sound.setPosition(offsetX); }, //播放 Event - play: function(){ + play: function() { this.sound[this.sound.playState < 1 ? 'play' : 'resume'](); }, //暂停 Event - stop: function(){ + stop: function() { this.sound.pause(); }, // 上一首 Event - prevSound: function(){ + prevSound: function() { var minIndex = 0; - if ( --this.index < minIndex ) this.index = this.data.length-1; + if (--this.index < minIndex) this.index = this.data.length-1; this.reset().setList().createSound(this.index); }, // 下一首 Event - nextSound: function(){ + nextSound: function() { var maxIndex = this.data.length-1; - if ( ++this.index > maxIndex ) this.index = 0; + if (++this.index > maxIndex) this.index = 0; this.reset().setList().createSound(this.index); }, // 设置当前播放状态 - setPlay: function(){ + setPlay: function() { var DOM = this.DOM; DOM.playing.stop(true,true)[this.IE6 ? 'show' : 'fadeIn'](); DOM.play.hide(); @@ -263,7 +285,7 @@ }, // 设置当前暂停状态 - setStop: function(){ + setStop: function() { var DOM = this.DOM; DOM.playing.stop(true,true)[this.IE6 ? 'hide' : 'fadeOut'](); DOM.play.show(); @@ -272,7 +294,7 @@ }, // 重置播放器界面 - reset: function(){ + reset: function() { var DOM = this.DOM; this.setStop(); DOM.seekbar.width(0); @@ -281,20 +303,20 @@ }, // 设置列表选中 - setList: function(){ + setList: function() { var DOM = this.DOM; DOM.list.find('li').removeClass('current').eq(this.index).addClass('current'); return this; }, // 获取播放器DOM - getDOM: function($elem){ + getDOM: function($elem) { var elem = $elem[0].getElementsByTagName('*'), DOM = {}; DOM['wrap'] = $elem; - for (var i = 0; i < elem.length; i++){ - if ( elem[i].className.indexOf('wp-player') > -1 ){ + for (var i = 0; i < elem.length; i++) { + if (elem[i].className.indexOf('wp-player') > -1 ) { var name = elem[i].className.replace('wp-player', '').replace(/-/g, ''); DOM[name] = $(elem[i]); } @@ -305,18 +327,15 @@ }, // 格式化时间 - formatNumber: function(val){ - var str = val < 0 ? '0' : val.toString(); - if (str.length > 1){ - return str; - } - return '0' + str; + formatNumber: function(val) { + return val.toString().length < 2 ? '0' + val : val; }, // 获取播放器必须的属性 - getAttr: function(){ + getAttr: function() { var DOM = this.DOM; this.attr = { + source: DOM.wrap.attr('data-source'), type: DOM.wrap.attr('data-type'), xiami: DOM.wrap.attr('data-xiami'), title: DOM.wrap.attr('data-title'), @@ -329,9 +348,9 @@ }, // 虾米类型转换 - getXiamiType: function( val ){ + getXiamiType: function(val) { var type; - switch ( val ){ + switch (val) { case 'song': type = 0; break; case 'album': type = 1; break; case 'artist': type = 2; break; @@ -343,7 +362,7 @@ // 虾米地址转换 // 参照 http://www.blackglory.me/xiami-getlocation-implementation-of-php-and-javascript/ - getXiamiLocation: function( str ){ + getXiamiLocation: function(str) { try { var a1 = parseInt(str.charAt(0)), a2 = str.substring(1), @@ -379,9 +398,9 @@ WPPlayer.template = '
    • {author}{serial}{title}
    • '; // 扩展 jQuery 对象 - $.fn.WPPlayer = function( options ){ + $.fn.WPPlayer = function(options) { return this.each(function(){ - new WPPlayer( this, options ); + new WPPlayer(this, options); }); }; diff --git a/wp-player.php b/wp-player.php index 5dd3c3b..4806af2 100644 --- a/wp-player.php +++ b/wp-player.php @@ -2,8 +2,8 @@ /* * Plugin Name: WP-Player * Plugin URI: http://webjyh.com/wp-player/ - * Description: 一个迷你歌曲播放器,支持多歌曲播放,支持使用虾米歌曲地址,也支持自定义上传音乐。 - * Version: 2.1.0 + * Description: 一个迷你歌曲播放器,支持多歌曲播放,支持使用虾米歌曲或网易云音乐歌曲地址,也支持自定义上传音乐。 + * Version: 2.2.0 * Author: M.J * Author URI: http://webjyh.com * License: GPLv2 or later