切换到媒体上传器中的库标签

时间:2015-02-22 作者:leemon

我正在开发一个插件,在媒体上传器中添加一个选项卡,通过oembed将外部视频添加到媒体库中。一切正常,但在通过新选项卡添加新的外部视频后,我需要切换到库选项卡。这是我正在使用的代码的一部分:

wp.media.controller.Custom = wp.media.controller.State.extend({

    initialize: function(){
        this.props = new Backbone.Model({ custom_data: \'\' });
        this.props.on( \'change:custom_data\', this.refresh, this );
    },

    refresh: function() {
        this.frame.toolbar.get().refresh();
    },

    customAction: function(){
        wp.media.post( \'add-oembed\', {
            url:     this.props.get(\'custom_data\'),
            post_id: wp.media.view.settings.post.id
        });

        this.frame.content.mode(\'browse\');

    }

});
生产线this.frame.content.mode(\'browse\') 应该切换到library选项卡,但我收到一条错误消息,上面说:

TypeError: this.collection is undefined

有什么想法吗?

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

经过数天的努力,通过记录不良的媒体模式源代码和使用the gist (谢谢,法比恩),我想出了一个解决方案:

JS:

wp.media.controller.Custom = wp.media.controller.State.extend({

    initialize: function(){
        this.props = new Backbone.Model({ custom_data: \'\' });
        this.props.on( \'change:custom_data\', this.refresh, this );
    },

    refresh: function() {
        this.frame.toolbar.get().refresh();
    },

    // called when the toolbar button is clicked
    customAction: function( controller ){
        // call the PHP function that inserts an oembed attachment to the database via AJAX
        wp.media.post( \'add-oembed\', {
            url:     this.props.get( \'custom_data\' ),
            post_id: wp.media.view.settings.post.id
        }).done( function( resp ) {
            // create an attachment model using the data from the AJAX response
            var attachment = wp.media.model.Attachment.create( resp );
            var edit = controller.state( \'insert\' );
            // add the attachment to the library model
            edit.get( \'library\' ).add( attachment );
        });

    }

});


wp.media.view.Toolbar.Custom = wp.media.view.Toolbar.extend({
    initialize: function() {
        _.defaults( this.options, {
            event: \'custom_event\',
            close: false,
            items: {
                custom_event: {
                    text: wp.media.view.l10n.customButton,
                    style: \'primary\',
                    priority: 80,
                    requires: false,
                    click: this.customAction
                }
            }
        });

        wp.media.view.Toolbar.prototype.initialize.apply( this, arguments );
    },

    refresh: function() {
        var custom_data = this.controller.state().props.get(\'custom_data\');
        this.get(\'custom_event\').model.set( \'disabled\', ! custom_data );
        wp.media.view.Toolbar.prototype.refresh.apply( this, arguments );
    },

    // triggered when the button is clicked
    customAction: function(){
        this.controller.state().customAction( this.controller );
        // switch to the library view
        this.controller.setState( \'insert\' );
    }

});      

PHP:

add_action(\'wp_ajax_add-oembed\', \'custom_add_oembed\');

function custom_add_oembed() {
    $url = $_POST[\'url\'];
    $post_ID = intval($_POST[\'post_id\']);

    if (!current_user_can( \'edit_post\', $post_ID ) ) {
        wp_send_json_error();
    }

    $oembed = new WP_oEmbed();
    $provider = $oembed->discover($url);
    if($provider === false && substr($url, 0, 5) === \'https\') {
        $url = str_replace(\'https\', \'http\', $url);
        $provider = $oembed->discover($url);
    }
    if($provider === false) {
        wp_send_json_error();
    }

    $response = $oembed->fetch($provider, $url);
    if( $response === false) {
        wp_send_json_error();
    }

    $my_post = array(
        \'post_parent\'   => $post_ID,
        \'post_title\'    => $response->title,
        \'post_content\'  => \'\',
        \'post_status\'   => \'inherit\',
        \'post_author\'   => get_current_user_id(),
        \'post_type\'     => \'attachment\',
        \'guid\'          => $url,
        \'post_mime_type\'=> \'oembed/\' . $response->provider_name
    );
    $attachment_id = wp_insert_post( $my_post );
    if( ! is_int($attachment_id) ) {
        wp_send_json_error();
    }

    if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) ) {
        wp_send_json_error();
    }

    wp_send_json_success( $attachment );

}

结束