WordPress媒体上传器查询附件参数



我正在尝试在插件中使用媒体上传器。 我需要将其他参数传递给查询附件请求。 我尝试像这样向库添加一个参数:

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
});

但是这个附加参数似乎搞砸了新的附件上传过程 - 新上传的附件不会显示在下载端的网格中。

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

谢谢你的回答

我挖了一点 un 进程,传递我发现的其他参数的最佳方法是在查询调用期间添加它。 为此,添加多个类进行扩展。

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

// 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);
}...

现在必须找到一种方法来实例化这个新类。这是 Attachements 类的重新查询方法,它调用 'static' ou 'class method' get 的查询。为了创建基于 Query1 的查询,必须复制 get 方法以创建 Query1 对象

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(( 的第二个参数中创建的类方法

扩展附件 - 当调用_requery方法时 - 在我们的例子中 - 附件集合被替换为查询集合(它进行实际的 admin-ajax 调用来查询附件(。在扩展_requery方法中,将创建 Query1 查询而不是常规查询。

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 类型的对象

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 过滤器中

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

我用:

var post_type = 'my-cpt';
var real_ajax_url = wp.ajax.settings.url;
// For uploading.
window.wp.Uploader.defaults.multipart_params.post_type = post_type;
// For querying.
wp.ajax.settings.url = real_ajax_url + '?post_type=' + post_type;

然后收听$_GET['post_type']wp_ajax_query-attachments动作,并收听admin_init钩子上的'upload-attachment' !== $_POST['action']$_POST['post_type']设置为"my-cpt"。

并在完成对话框后取消设置/恢复值。

最新更新