Wordpress Playlist WPSE

时间:2020-01-22 作者:thejack99

我使用的插件来自https://github.com/birgire/wpse-playlist 到目前为止,我真的很喜欢它,但我希望任何人都能帮我修改一下代码,比如:原始代码:

[_playlist]
   [_track title="Ain\'t Misbehavin\'" src="//s.w.org/images/core/3.9/AintMisbehavin.mp3"]
   [_track title="Buddy Bolden\'s Blues" src="//s.w.org/images/core/3.9/JellyRollMorton-BuddyBoldensBlues.mp3"]
[/_playlist]
我想将其更改为:

[_playlist]
   http://s.w.org/images/core/3.9/AintMisbehavin.mp3
   http://s.w.org/images/core/3.9/JellyRollMorton-BuddyBoldensBlues.mp3
[/_playlist]

2 个回复
最合适的回答,由SO网友:Rup 整理而成

乍一看,你只需要改变一下get_tracks_from_content. 而不是通过do\\u短代码运行$content,

按换行符(或空格)拆分并丢弃空行或任何不是URL的内容,此时可能会调用strip\\u标记,在您留下的每个值上,将URL传递到track\\u快捷码中,如下所示[ "src" => $url ], 加上您要在此处设置的任何其他通用元数据,将结果串联起来,生成新的$内容,继续// Replace last comma 代码

SO网友:thejack99

我试过了,但没有成功,也许我在什么地方误解了。这是WPSE函数文件,请告诉我需要编辑的位置:

<?php

namespace birgire;

/**
 * Class Playlist
 */

class Playlist
{
    protected $type     = \'\';
    protected $types    = array( \'audio\', \'video\' );
    protected $instance = 0;

    /**
     * Init - Register shortcodes
     */

    public function init()
    {
        add_shortcode( \'_playlist\',     array( $this, \'playlist_shortcode\'  ) );
        add_shortcode( \'_track\',        array( $this, \'track_shortcode\'     ) );

        // Deprecated:
        add_shortcode( \'wpse_playlist\', array( $this, \'playlist_shortcode\'  ) );
        add_shortcode( \'wpse_trac\',     array( $this, \'track_shortcode\'     ) );
    }


    /**
     * Callback for the [_playlist] shortcode
     *
     * @uses wp_validate_boolean() from WordPress 4.0
     */

    public function playlist_shortcode( $atts = array(), $content = \'\' ) 
    {        
        global $content_width;  // Theme dependent content width
        $this->instance++;      // Counter to activate the \'wp_playlist_scripts\' action only once

        $atts = shortcode_atts( 
            array(
                \'type\'           => \'audio\',
                \'style\'          => \'light\',
                \'tracklist\'      => \'true\',
                \'tracknumbers\'   => \'true\',
                \'images\'         => \'true\',     // Audio related
                \'artists\'        => \'true\',     // Audio related
                \'current\'        => \'true\',
        \'autoplay\'       => \'false\',
        \'class\'          => \'wpse-playlist\',
        \'width\'          => \'\',
        \'height\'         => \'\',
        \'outer\'          => \'20\',
        \'default_width\'  => \'640\',
        \'default_height\' => \'380\',
            ), 
            $atts, 
            \'wpse_playlist_shortcode\' 
        );

        // Autoplay:
        $autoplay = wp_validate_boolean( $atts[\'autoplay\'] ) ? \'autoplay="yes"\' : \'\';

        // Nested shortcode support:
        $this->type = ( in_array( $atts[\'type\'], $this->types, TRUE ) ) ? esc_attr( $atts[\'type\'] ) : \'audio\';

        // Enqueue default scripts and styles for the playlist.
        ( 1 === $this->instance ) && do_action( \'wp_playlist_scripts\', esc_attr( $atts[\'type\'] ), esc_attr( $atts[\'style\'] ) );

        //----------
        // Height & Width - Adjusted from the WordPress core
        //----------

        $width = esc_attr( $atts[\'width\'] );       
        if( empty( $width ) )
            $width = empty( $content_width ) 
                ? intval( $atts[\'default_width\'] ) 
                : ( $content_width - intval( $atts[\'outer\'] ) );

        $height = esc_attr( $atts[\'height\'] );       
        if( empty( $height ) && intval( $atts[\'default_height\'] ) > 0 )
            $height = empty( $content_width ) 
                ? intval( $atts[\'default_height\'] )
                : round( ( intval( $atts[\'default_height\'] ) * $width ) / intval( $atts[\'default_width\'] ) );

        //----------
        // Output
        //----------

        $html = \'\';

        // Start div container:
        $html .= sprintf( \'<div class="wp-playlist wp-%s-playlist wp-playlist-%s \' .  esc_attr( $atts[\'class\'] ) . \'">\', 
        $this->type, 
            esc_attr( $atts[\'style\'] )
        );

        // Current audio item:
        if( $atts[\'current\'] && \'audio\' === $this->type )
    {
            $html .= \'<div class="wp-playlist-current-item"></div>\';   
        }

        // Video player:                      
        if( \'video\' === $this->type )
        {
            $html .= sprintf( \'<video controls="controls" \' . $autoplay . \' preload="none" width="%s" height="%s"></video>\',
                $width,
                $height
            );
        }
        // Audio player:                      
        else
        {
            $html .= sprintf( \'<audio controls="controls" \' . $autoplay . \' preload="none" width="%s" style="visibility: hidden"></audio>\', 
                $width 
            );
        }

        // Next/Previous:
        $html .= \'<div class="wp-playlist-next"></div><div class="wp-playlist-prev"></div>\';

        // JSON 
        $html .= sprintf( \'
            <script class="wp-playlist-script" type="application/json">{
                "type":"%s",
                "tracklist":%s,
                "tracknumbers":%s,
                "images":%s,
                "artists":%s,
                "tracks":[%s]
            }</script>\', 
            esc_attr( $atts[\'type\'] ), 
            wp_validate_boolean( $atts[\'tracklist\'] ) ? \'true\' : \'false\', 
            wp_validate_boolean( $atts[\'tracknumbers\'] ) ? \'true\' : \'false\',  
            wp_validate_boolean( $atts[\'images\'] ) ? \'true\' : \'false\',
            wp_validate_boolean( $atts[\'artists\'] ) ? \'true\' : \'false\',
            $this->get_tracks_from_content( $content )
        );

        // Close div container:
        $html .= \'</div>\';

        return $html;
    }


    /**
     * Get tracks from the [_playlist] shortcode content string
     */

    private function get_tracks_from_content( $content )
    {
        // Get tracs:
    $content = strip_tags( nl2br( do_shortcode( $content ) ) );

        // Replace last comma:
        if( FALSE !== ( $pos = strrpos( $content, \',\' ) ) )
        {
            $content = substr_replace( $content, \'\', $pos, 1 );
        }

        return $content;
    }


    /**
     * Callback for the [_track] shortcode
     */

    public function track_shortcode( $atts = array(), $content = \'\' ) 
    {    

        $atts = shortcode_atts( 
            array(
                \'src\'                        => \'\',
                \'type\'                       => ( \'video\' === $this->type ) ? \'video/mp4\' : \'audio/mpeg\',
                \'title\'                      => \'\',
                \'caption\'                    => \'\',
                \'description\'                => \'\',
                \'image_src\'                  => sprintf( \'%s/wp-includes/images/media/%s.png\', get_site_url(), $this->type ),
                \'image_width\'                => \'48\',
                \'image_height\'               => \'64\',
                \'thumb_src\'                  => sprintf( \'%s/wp-includes/images/media/%s.png\', get_site_url(), $this->type ),
                \'thumb_width\'                => \'48\',
                \'thumb_height\'               => \'64\',
                \'meta_artist\'                => \'\',
                \'meta_album\'                 => \'\',
                \'meta_genre\'                 => \'\',
                \'meta_length_formatted\'      => \'\',
                \'dimensions_original_width\'  => \'300\',
                \'dimensions_original_height\' => \'200\',
                \'dimensions_resized_width\'   => \'600\',
                \'dimensions_resized_height\'  => \'400\',
            ), 
            $atts, 
            \'wpse_track_shortcode\' 
        );

        //----------
        // Data output:
        //----------
        $data[\'src\']                      = esc_url( $atts[\'src\'] );
        $data[\'title\']                    = sanitize_text_field( $atts[\'title\'] );
        $data[\'type\']                     = sanitize_text_field( $atts[\'type\'] );
        $data[\'caption\']                  = sanitize_text_field( $atts[\'caption\'] );
        $data[\'description\']              = sanitize_text_field( $atts[\'description\'] );
        $data[\'image\'][\'src\']             = esc_url( $atts[\'image_src\'] );
        $data[\'image\'][\'width\']           = intval( $atts[\'image_width\'] );
        $data[\'image\'][\'height\']          = intval( $atts[\'image_height\'] );
        $data[\'thumb\'][\'src\']             = esc_url( $atts[\'thumb_src\'] );
        $data[\'thumb\'][\'width\']           = intval( $atts[\'thumb_width\'] );
        $data[\'thumb\'][\'height\']          = intval( $atts[\'thumb_height\'] );
        $data[\'meta\'][\'length_formatted\'] = sanitize_text_field( $atts[\'meta_length_formatted\'] );

        // Video related:
        if( \'video\' === $this->type ) 
        {
            $data[\'dimensions\'][\'original\'][\'width\']  = sanitize_text_field( $atts[\'dimensions_original_width\'] );
            $data[\'dimensions\'][\'original\'][\'height\'] = sanitize_text_field( $atts[\'dimensions_original_height\'] );
            $data[\'dimensions\'][\'resized\'][\'width\']   = sanitize_text_field( $atts[\'dimensions_resized_width\'] );
            $data[\'dimensions\'][\'resized\'][\'height\']  = sanitize_text_field( $atts[\'dimensions_resized_height\'] );

        // Audio related:
        } else {
            $data[\'meta\'][\'artist\'] = sanitize_text_field( $atts[\'meta_artist\'] );
            $data[\'meta\'][\'album\']  = sanitize_text_field( $atts[\'meta_album\'] );
            $data[\'meta\'][\'genre\']  = sanitize_text_field( $atts[\'meta_genre\'] );
        }

        return json_encode( $data ) . \',\';      
    }

} // end class

相关推荐

如何使用JWPlayerPlayListid自定义读取其PlayList项?

在带有JW Player(v5)的Wordpress(3.8.1)和JW插件(v2.1.2)中,我需要读取JWPlayer Plugin.通常情况下JWPlayer Plugin 工作原理如下,通过短代码:[jwplayer playlistid=\"1234\"] 。。然后aVideo Player 将与其中的播放列表一起呈现在页面上。我想做什么(定制)那么现在我该怎么做呢。。if i want to know (or pull out) the items inside a Playlist