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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,8 +12,10 @@ define([
post: function() { post: function() {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({ deferred.resolve({
'post': 'val1', data: {
'page': 'val2', 'post': 'val1',
'page': 'val2',
}
}); });
return deferred; return deferred;
} }
@ -71,13 +73,17 @@ define([
expect(spy.args[0][0].data.postType).to.equal('post'); 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({ var module = CommunicationInjector({
"mailpoet": { "mailpoet": {
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({ 'category': 'val1' }); deferred.resolve({
data: {
'category': 'val1'
}
});
return deferred; return deferred;
} }
}, },
@ -108,13 +114,13 @@ define([
describe('getTerms', function() { describe('getTerms', function() {
it('sends terms to endpoint', function() { it('sends terms to endpoint', function() {
var spy, var spy;
post = function(params) { var post = function(params) {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({}); deferred.resolve({});
return deferred; return deferred;
}, }
module; var module;
spy = sinon.spy(post); spy = sinon.spy(post);
module = CommunicationInjector({ module = CommunicationInjector({
"mailpoet": { "mailpoet": {
@ -136,7 +142,12 @@ define([
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({ 'term1': 'term1val1', 'term2': 'term2val2' }); deferred.resolve({
data: {
'term1': 'term1val1',
'term2': 'term2val2'
}
});
return deferred; return deferred;
} }
}, },
@ -199,7 +210,12 @@ define([
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); 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; return deferred;
} }
}, },
@ -265,7 +281,12 @@ define([
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); 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; return deferred;
} }
}, },