register_rest_route('nv','get-taxonomies',{
methods: 'post',
callback(data,req) {
const {post_type,include} = data;
if (!post_type) {
return new NV_Error('参数错误');
}
var taxonomies = apply_filters('_nv_taxonomies',[]);
return taxonomies.filter(tax=>{
var result = [];
result.push(tax.post_type == post_type)
if (include) {
result.push( include.includes(tax.taxonomy) )
}
return result.length == 0 ? false : !result.includes(false);
})
},
// permission_callback: power=>power >= 10,
});
register_rest_route('nv','query-terms',{
methods: 'post',
callback(data,req) {
var terms = query_terms(data);
(Array.isArray(terms) ? terms : terms.data)
.forEach(term=>{
term.permalink = get_term_permalink(term);
})
return terms;
},
});
register_rest_route('nv','get-term',{
methods: 'post',
callback(data,req) {
var {id,slug} = data;
var term = get_term(id || slug);
if (is_nv_error(term)) {return term;}
return {
...term,
permalink: get_term_permalink(term),
metas: get_term_meta(term.id)
}
},
});
register_rest_route('nv','add-term',{
methods: 'post',
callback(data,req) {
const {name,slug,parentID,taxonomy,metas} = data;
if (!name || !taxonomy) {
return new NV_Error('参数错误');
}
var term_id = nv_insert_term(name, taxonomy, {parentID,slug});
if (is_nv_error(term_id)) {
return term_id;
}
if (metas) {
for(var key in metas) {
update_term_meta(term_id, key, metas[key]);
}
}
return {id: term_id};
},
permission_callback: power=>power >= 8,
});
register_rest_route('nv','update-term',{
methods: 'post',
callback(data,req) {
const {term,name,slug,parentID,taxonomy,metas} = data;
//其中,term可以是旧的名称、slug、id。建议使用id
if (!term || !taxonomy) {
return new NV_Error('参数错误');
}
var term_id = nv_update_term(term, taxonomy, {parentID,slug,name});
if (is_nv_error(term_id)) {
return term_id;
}
if (metas) {
for(var key in metas) {
update_term_meta(term_id, key, metas[key]);
}
}
return {id: term_id};
},
permission_callback: power=>power >= 8,
});
register_rest_route('nv','delete-terms',{
methods: 'post',
callback(data,req) {
const {terms, taxonomy } = data;
//其中,terms[term]可以是旧的名称、slug、id。建议使用id
if (!terms || !taxonomy) {
return new NV_Error('参数错误');
}
terms.forEach(term=>{
nv_delete_term(term, taxonomy);
})
return {};
},
permission_callback: power=>power >= 8,
});
register_rest_route('nv','get-taxonomy-editor-metas',{
methods: 'post',
callback(data,req) {
const {post_type, taxonomy} = data;
if (!post_type || !taxonomy) {
return new NV_Error('参数错误');
}
//读取自定义metabox
var meta_boxs = apply_filters('_nv_taxonomy_custom_metaboxes',[])
.filter( r=>r.post_types.includes(post_type) && r.taxonomies.includes(taxonomy) )
.sort((a,b)=>{
return (a.position || 10) - (b.position || 10);
})
.map( r=>({
slug: r.meta_box_slug,
component_url: r.component_url,
content: r.content, // 有content是使用metabox组件,没有就是自定义
}) )
// 对于允许层级关系的taxonomy类型。需要读取所有的内容(用于生成父、子树)。
var taxonomies = apply_filters('_nv_taxonomies',[]);
var current_taxonomy = taxonomies.filter(tax=>tax.post_type == post_type && tax.taxonomy == taxonomy)[0] || {opts:{}};
var term_tree;
if (current_taxonomy.opts.hierarchical) {
term_tree = query_terms({taxonomy}).map(r=>({ id: r.id, name: r.name, parent: r.parent}))
}
return {
meta_boxs,
term_tree
}
},
permission_callback: power=>power >= 6,
});