Wordpress Media Uploader 查询附件参数 [英] Wordpress Media Uploader query-attachment parameter

查看:26
本文介绍了Wordpress Media Uploader 查询附件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在插件中使用媒体上传器.我需要将附加参数传递给查询附件请求.我尝试向库中添加一个参数,如下所示:

I'm trying to use the Media Uploader in a plugin. I need to pass additional parameters to the query-attachment request. I've tried to add a parameter to the Library like this:

        file_frame = wp.media.frames.file_frame = wp.media({
        title: "User's photo",
        button: {
            text: "Upload image",
        },
        library: {
            type: ['image/jpeg','image/png'],
     -->       additional_param: 'a value'

        },
        multiple: false  // Set to true to allow multiple files to be selected
    });

但是这个额外的参数似乎搞砸了新的附件上传过程——新上传的附件在下载结束时没有显示在网格中.

But this additional parameter seems to screw up the new attachment upload process - the new uploaded attachment isn't display in the grid at the download end.

是否有一种有效的方法可以将自定义参数添加到查询附件请求中?

Is there a valid way to add a custom parameter to the query-attachment request?

感谢您的回答

推荐答案

我挖了一点 un process,我发现传递附加参数的最佳方法是在查询调用期间添加它.为此,需要扩展几个类.

I dug a little bit un process and the best way to pass additional parameter I found is to add it during the query call. To do it, several classes add to be extended.

扩展查询 (wp.media.model.Query)同步查询的方法负责放置实际的 admin-ajax 调用来查询附件.同步方法有 3 个参数,最后一个 - 选项 - 包含传递给 http 请求的参数.扩展同步方法在调用父同步请求之前添加了附加参数.

Extending Query (wp.media.model.Query) The sync Query's method is responsible to place the actual admin-ajax call to query the attachments. The sync method has 3 arguments, the last one - options - contains the parameters passed to the http request. The extended sync method adds the additional parameter before calling the parent sync request.

// Extending the wp.media.query to add a parameter
var Query1;
Query1=wp.media.model.Query1 = oldQuery.extend({

  sync: function( method, model, options ) {
    options = options || {};
    options = _.extend(options, {'data':{'CadTest': '1'}});
    return oldQuery.prototype.sync.apply(this, arguments);
  }...

现在必须找到一种方法来实例化这个新类.这样做是调用 Query 的静态"或类方法"获取的附件类的重新查询方法.为了创建基于 Query1 的查询,必须复制 get 方法以创建一个 Query1 对象

Now the must find a way to have this new class instancied. This done is the requery method of the Attachements class which call the 'static' ou 'class method' get of Query. In order to create a query based on Query1, the get method has had to be duplicated creating an Query1 object

    get1: (function(){
    /**
     * @static
     * @type Array
     */
    var queries = [];

    /**
     * @returns {Query}
     */
    return function( props, options ) {
        var args     = {},
            orderby  = Query1.orderby,
            defaults = Query1.defaultProps,
            query,
            cache    = !! props.cache || _.isUndefined( props.cache );

        // Remove the `query` property. This isn't linked to a query,
        // this *is* the query.
        delete props.query;
        delete props.cache;

        // Fill default args.
        _.defaults( props, defaults );

        // Normalize the order.
        props.order = props.order.toUpperCase();
        if ( 'DESC' !== props.order && 'ASC' !== props.order ) {
            props.order = defaults.order.toUpperCase();
        }

        // Ensure we have a valid orderby value.
        if ( ! _.contains( orderby.allowed, props.orderby ) ) {
            props.orderby = defaults.orderby;
        }

        _.each( [ 'include', 'exclude' ], function( prop ) {
            if ( props[ prop ] && ! _.isArray( props[ prop ] ) ) {
                props[ prop ] = [ props[ prop ] ];
            }
        } );

        // Generate the query `args` object.
        // Correct any differing property names.
        _.each( props, function( value, prop ) {
            if ( _.isNull( value ) ) {
                return;
            }

            args[ Query1.propmap[ prop ] || prop ] = value;
        });

        // Fill any other default query args.
        _.defaults( args, Query1.defaultArgs );

        // `props.orderby` does not always map directly to `args.orderby`.
        // Substitute exceptions specified in orderby.keymap.
        args.orderby = orderby.valuemap[ props.orderby ] || props.orderby;

        // Search the query cache for a matching query.
        if ( cache ) {
            query = _.find( queries, function( query ) {
                return _.isEqual( query.args, args );
            });
        } else {
            queries = [];
        }

        // Otherwise, create a new query and add it to the cache.
        if ( ! query ) {
            query = new wp.media.model.Query1( [], _.extend( options || {}, {
                props: props,
                args:  args
            } ) );
            queries.push( query );
        }

        return query;
    };
}()),

注意get1是在extend()的第二个参数中创建的类方法

Note that get1 is a class method to create in the 2nd parameter of extend()

扩展附件 - 当 _requery 方法被调用时 - 在我们的例子中 - Attachments 集合被一个 Query 集合替换(这使得实际的 admin-ajax 调用来查询附件).在扩展的 _requery 方法中,创建 Query1 查询而不是常规 Query.

Extending Attachments - when the _requery method is called - in our case - the Attachments collection is replaced by a Query collection (which makes the actual admin-ajax call to query the attachments). In the extended _requery method, a Query1 query is created instead of a regular Query.

wp.media.model.Attachments1 = oldAttachments.extend({


  _requery: function( refresh ) {
    var props;
    if ( this.props.get('query') ) {
        props = this.props.toJSON();
        props.cache = ( true !== refresh );
        this.mirror( wp.media.model.Query1.get1( props ) );
    }
  },
});

最后在库创建过程中必须创建一个类型为 Attachment1 的对象

Finally an object of type Attachment1 must be created during the library creation

wp.media.query1 = function( props ) {
  return new wp.media.model.Attachments1( null, {
    props: _.extend( _.defaults( props || {}, { orderby: 'date' } ), { query: true } )
  });
};

// Extending the current media library frame to add a new tab
wp.media.view.MediaFrame.Post1 = oldMediaFrame.extend({

 initialize: function(){
    oldMediaFrame.prototype.initialize.apply(this, arguments);
    var options = this.options;
    this.states.add([
        new Library({
            id:         'inserts',
            title:      vja_params.title,
            priority:   20,
            toolbar:    'main-insert',
            filterable: 'all',
            multiple:   false,
            editable:   false,
            library:  wp.media.query1( _.defaults({
                type: 'image'
            }, options.library ) ),


            // Show the attachment display settings.
            displaySettings: true,
            // Update user settings when users adjust the
            // attachment display settings.
            displayUserSettings: true
        }),
   ]);
 },

附加参数添加在_POST数组中,可以在PHP代码中使用——即在query_attachments_args过滤器中

The additional parameter is added in the _POST array and can be used in PHP code - ie in the query_attachments_args filter

    public function query_attachments_args($args){
      if (isset($_POST[CadTest])) {
        do whatever you want...
      }
      return $args;
   }

这篇关于Wordpress Media Uploader 查询附件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆