Merge pull request #581 from mailpoet/api_uniform_b

updated ALC endpoint + nl editor + js tests
This commit is contained in:
Tautvidas Sipavičius
2016-08-08 15:00:26 +03:00
committed by GitHub
7 changed files with 104 additions and 61 deletions

View File

@ -217,8 +217,10 @@ define([
};
},
transport: function(options, success, failure) {
var taxonomies,
promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) {
var taxonomies;
var promise = CommunicationComponent.getTaxonomies(
that.model.get('contentType')
).then(function(tax) {
taxonomies = tax;
// Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({
@ -227,7 +229,7 @@ define([
}).then(function(terms) {
return {
taxonomies: taxonomies,
terms: terms,
terms: terms
};
});
return promise;

View File

@ -302,8 +302,10 @@ define([
};
},
transport: function(options, success, failure) {
var taxonomies,
promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) {
var taxonomies;
var promise = CommunicationComponent.getTaxonomies(
that.model.get('contentType')
).then(function(tax) {
taxonomies = tax;
// Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({
@ -312,7 +314,7 @@ define([
}).then(function(terms) {
return {
taxonomies: taxonomies,
terms: terms,
terms: terms
};
});
return promise;

View File

@ -27,8 +27,8 @@ define([
return Module._cachedQuery({
action: 'getPostTypes',
options: {},
}).then(function(types) {
return _.values(types);
}).then(function(response) {
return _.values(response.data);
});
};
@ -36,36 +36,46 @@ define([
return Module._cachedQuery({
action: 'getTaxonomies',
options: {
postType: postType,
},
postType: postType
}
}).then(function(response) {
return response.data;
});
};
Module.getTerms = function(options) {
return Module._cachedQuery({
action: 'getTerms',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getPosts = function(options) {
return Module._cachedQuery({
action: 'getPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getTransformedPosts = function(options) {
return Module._cachedQuery({
action: 'getTransformedPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getBulkTransformedPosts = function(options) {
return Module._query({
action: 'getBulkTransformedPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};

View File

@ -8,7 +8,6 @@ abstract class Endpoint {
function successResponse(
$data = array(), $meta = array(), $status = Response::STATUS_OK
) {
return new SuccessResponse($data, $meta, $status);
}

View File

@ -1,9 +1,11 @@
<?php
namespace MailPoet\API\Endpoints;
use \MailPoet\API\Endpoint as APIEndpoint;
use \MailPoet\API\Error as APIError;
if(!defined('ABSPATH')) exit;
class AutomatedLatestContent {
class AutomatedLatestContent extends APIEndpoint {
public $ALC;
function __construct() {
@ -11,46 +13,57 @@ class AutomatedLatestContent {
}
function getPostTypes() {
return get_post_types(array(), 'objects');
return $this->successResponse(
get_post_types(array(), 'objects')
);
}
function getTaxonomies($args) {
$post_type = (isset($args['postType'])) ? $args['postType'] : 'post';
return get_object_taxonomies($post_type, 'objects');
function getTaxonomies($data = array()) {
$post_type = (isset($data['postType'])) ? $data['postType'] : 'post';
return $this->successResponse(
get_object_taxonomies($post_type, 'objects')
);
}
function getTerms($args) {
$taxonomies = (isset($args['taxonomies'])) ? $args['taxonomies'] : array();
$search = (isset($args['search'])) ? $args['search'] : '';
$limit = (isset($args['limit'])) ? (int)$args['limit'] : 10;
$page = (isset($args['page'])) ? (int)$args['page'] : 1;
return get_terms(
$taxonomies,
array(
'hide_empty' => false,
'search' => $search,
'number' => $limit,
'offset' => $limit * ($page - 1)
function getTerms($data = array()) {
$taxonomies = (isset($data['taxonomies'])) ? $data['taxonomies'] : array();
$search = (isset($data['search'])) ? $data['search'] : '';
$limit = (isset($data['limit'])) ? (int)$data['limit'] : 10;
$page = (isset($data['page'])) ? (int)$data['page'] : 1;
return $this->successResponse(
get_terms(
$taxonomies,
array(
'hide_empty' => false,
'search' => $search,
'number' => $limit,
'offset' => $limit * ($page - 1)
)
)
);
}
function getPosts($args) {
return $this->ALC->getPosts($args);
function getPosts($data = array()) {
return $this->successResponse(
$this->ALC->getPosts($data)
);
}
function getTransformedPosts($args) {
$posts = $this->ALC->getPosts($args);
return $this->ALC->transformPosts($args, $posts);
function getTransformedPosts($data = array()) {
$posts = $this->ALC->getPosts($data);
return $this->successResponse(
$this->ALC->transformPosts($data, $posts)
);
}
function getBulkTransformedPosts($args) {
function getBulkTransformedPosts($data = array()) {
$alc = new \MailPoet\Newsletter\AutomatedLatestContent();
$used_posts = array();
$rendered_posts = array();
foreach($args['blocks'] as $block) {
foreach($data['blocks'] as $block) {
$posts = $alc->getPosts($block, $used_posts);
$rendered_posts[] = $alc->transformPosts($block, $posts);
@ -59,6 +72,6 @@ class AutomatedLatestContent {
}
}
return $rendered_posts;
return $this->successResponse($rendered_posts);
}
}

View File

@ -12,12 +12,8 @@ class SuccessResponse extends Response {
}
function getData() {
if(empty($this->data)) {
return false;
} else {
return array(
'data' => $this->data
);
}
return array(
'data' => $this->data
);
}
}

View File

@ -12,8 +12,10 @@ define([
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve({
'post': 'val1',
'page': 'val2',
data: {
'post': 'val1',
'page': 'val2',
}
});
return deferred;
}
@ -71,13 +73,17 @@ define([
expect(spy.args[0][0].data.postType).to.equal('post');
});
it('fetches post types from the server', function() {
it('fetches taxonomies from the server', function() {
var module = CommunicationInjector({
"mailpoet": {
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve({ 'category': 'val1' });
deferred.resolve({
data: {
'category': 'val1'
}
});
return deferred;
}
},
@ -108,13 +114,13 @@ define([
describe('getTerms', function() {
it('sends terms to endpoint', function() {
var spy,
post = function(params) {
var deferred = jQuery.Deferred();
deferred.resolve({});
return deferred;
},
module;
var spy;
var post = function(params) {
var deferred = jQuery.Deferred();
deferred.resolve({});
return deferred;
}
var module;
spy = sinon.spy(post);
module = CommunicationInjector({
"mailpoet": {
@ -136,7 +142,12 @@ define([
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve({ 'term1': 'term1val1', 'term2': 'term2val2' });
deferred.resolve({
data: {
'term1': 'term1val1',
'term2': 'term2val2'
}
});
return deferred;
}
},
@ -199,7 +210,12 @@ define([
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve([{post_title: 'title 1'}, {post_title: 'post title 2'}]);
deferred.resolve({
data: [
{post_title: 'title 1'},
{post_title: 'post title 2'}
]
});
return deferred;
}
},
@ -265,7 +281,12 @@ define([
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve([{type: 'text', text: 'something'}, {type: 'text', text: 'something else'}]);
deferred.resolve({
data: [
{type: 'text', text: 'something'},
{type: 'text', text: 'something else'}
]
});
return deferred;
}
},