Add getPosts endpoint to fetch a list of posts for search
This commit is contained in:
@ -81,15 +81,14 @@ define([
|
|||||||
},
|
},
|
||||||
fetchAvailablePosts: function() {
|
fetchAvailablePosts: function() {
|
||||||
var that = this;
|
var that = this;
|
||||||
// TODO: Move this logic to new AJAX query format
|
WordpressComponent.getPosts(this.toJSON()).done(function(posts) {
|
||||||
//mailpoet_post_wpi('posts.php', this.toJSON(), function(response) {
|
console.log('Posts fetched', arguments);
|
||||||
//console.log('Posts fetched', arguments);
|
that.get('_availablePosts').reset(posts);
|
||||||
//that.get('_availablePosts').reset(response);
|
that.get('_selectedPosts').reset(); // Empty out the collection
|
||||||
//that.get('_selectedPosts').reset(); // Empty out the collection
|
that.trigger('change:_availablePosts');
|
||||||
//that.trigger('change:_availablePosts');
|
}).fail(function() {
|
||||||
//}, function() {
|
console.log('Posts fetchPosts error', arguments);
|
||||||
//console.log('Posts fetchPosts error', arguments);
|
});
|
||||||
//});
|
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Batch more changes during a specific time, instead of fetching
|
* Batch more changes during a specific time, instead of fetching
|
||||||
|
@ -7,8 +7,9 @@ define([
|
|||||||
|
|
||||||
var Module = {};
|
var Module = {};
|
||||||
var postTypesCache,
|
var postTypesCache,
|
||||||
taxonomiesCache = [],
|
taxonomiesCache = {},
|
||||||
termsCache = [];
|
termsCache = {},
|
||||||
|
postsCache = {};
|
||||||
|
|
||||||
Module.getPostTypes = function() {
|
Module.getPostTypes = function() {
|
||||||
if (!postTypesCache) {
|
if (!postTypesCache) {
|
||||||
@ -51,6 +52,19 @@ define([
|
|||||||
return termsCache[key];
|
return termsCache[key];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Module.getPosts = function(options) {
|
||||||
|
var key = JSON.stringify(options);
|
||||||
|
if (!postsCache[key]) {
|
||||||
|
postsCache[key] = MailPoet.Ajax.post({
|
||||||
|
endpoint: 'wordpress',
|
||||||
|
action: 'getPosts',
|
||||||
|
data: options || {},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return postsCache[key];
|
||||||
|
};
|
||||||
|
|
||||||
App.on('start', function(options) {
|
App.on('start', function(options) {
|
||||||
// Prefetch post types
|
// Prefetch post types
|
||||||
Module.getPostTypes();
|
Module.getPostTypes();
|
||||||
|
@ -29,4 +29,50 @@ class Wordpress {
|
|||||||
'offset' => $limit * ($page - 1),
|
'offset' => $limit * ($page - 1),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPosts($args) {
|
||||||
|
$parameters = array(
|
||||||
|
'posts_per_page' => (isset($args['amount'])) ? (int)$args['amount'] : 10,
|
||||||
|
'post_type' => (isset($args['contentType'])) ? $args['contentType'] : 'post',
|
||||||
|
'post_status' => (isset($args['postStatus'])) ? $args['postStatus'] : 'publish',
|
||||||
|
'orderby' => 'date',
|
||||||
|
'order' => ($args['sortBy'] === 'newest') ? 'DESC' : 'ASC',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isset($args['search'])) {
|
||||||
|
$parameters['s'] = $args['search'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($args['terms']) && is_array($args['terms']) && !empty($args['terms'])) {
|
||||||
|
// Add filtering by tags and categories
|
||||||
|
$tags = array();
|
||||||
|
$categories = array();
|
||||||
|
foreach($args['terms'] as $term) {
|
||||||
|
if ($term['taxonomy'] === 'category') $categories[] = $term['id'];
|
||||||
|
else if ($term['taxonomy'] === 'post_tag') $tags[] = $term['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$taxonomies_query = array();
|
||||||
|
foreach (array('post_tag' => $tags, 'category' => $categories) as $taxonomy => $terms) {
|
||||||
|
if (!empty($terms)) {
|
||||||
|
$tax = array(
|
||||||
|
'taxonomy' => $taxonomy,
|
||||||
|
'field' => 'id',
|
||||||
|
'terms' => $terms,
|
||||||
|
);
|
||||||
|
if ($args['inclusionType'] === 'exclude') $tax['operator'] = 'NOT IN';
|
||||||
|
$taxonomies_query[] = $tax;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($taxonomies_query)) {
|
||||||
|
// With exclusion we want to use 'AND', because we want posts that don't have excluded tags/categories
|
||||||
|
// But with inclusion we want to use 'OR', because we want posts that have any of the included tags/categories
|
||||||
|
$taxonomies_query['relation'] = ($args['inclusionType'] === 'exclude') ? 'AND' : 'OR';
|
||||||
|
$parameters['tax_query'] = $taxonomies_query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json(get_posts($parameters));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user