diff --git a/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js b/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js index 8c99698ee7..87835ac05a 100644 --- a/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js +++ b/assets/js/src/newsletter_editor/blocks/automatedLatestContent.js @@ -51,9 +51,9 @@ define([ blocks: blocks, }).then(_.partial(this.refreshBlocks, models)); }, - refreshBlocks: function(models, renderedBlocks) { + refreshBlocks: function(models, response) { _.each( - _.zip(models, renderedBlocks), + _.zip(models, response.data), function(args) { var model = args[0], contents = args[1]; @@ -217,17 +217,19 @@ define([ }; }, transport: function(options, success, failure) { - var taxonomies, - promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) { - taxonomies = tax; + var taxonomies; + var promise = CommunicationComponent.getTaxonomies( + that.model.get('contentType') + ).then(function(response) { + taxonomies = response.data; // Fetch available terms based on the list of taxonomies already fetched var promise = CommunicationComponent.getTerms({ search: options.data.term, taxonomies: _.keys(taxonomies) - }).then(function(terms) { + }).then(function(response) { return { taxonomies: taxonomies, - terms: terms, + terms: response.data }; }); return promise; diff --git a/assets/js/src/newsletter_editor/blocks/posts.js b/assets/js/src/newsletter_editor/blocks/posts.js index 3062e803d1..f14c32d984 100644 --- a/assets/js/src/newsletter_editor/blocks/posts.js +++ b/assets/js/src/newsletter_editor/blocks/posts.js @@ -108,8 +108,8 @@ define([ }, fetchAvailablePosts: function() { var that = this; - CommunicationComponent.getPosts(this.toJSON()).done(function(posts) { - that.get('_availablePosts').reset(posts); + CommunicationComponent.getPosts(this.toJSON()).done(function(response) { + that.get('_availablePosts').reset(response.data); that.get('_selectedPosts').reset(); // Empty out the collection that.trigger('change:_availablePosts'); }).fail(function() { @@ -127,8 +127,8 @@ define([ return; } - CommunicationComponent.getTransformedPosts(data).done(function(posts) { - that.get('_transformedPosts').get('blocks').reset(posts, {parse: true}); + CommunicationComponent.getTransformedPosts(data).done(function(response) { + that.get('_transformedPosts').get('blocks').reset(response.data, {parse: true}); }).fail(function() { MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); }); @@ -143,8 +143,8 @@ define([ if (data.posts.length === 0) return; - CommunicationComponent.getTransformedPosts(data).done(function(posts) { - collection.add(posts, { at: index }); + CommunicationComponent.getTransformedPosts(data).done(function(response) { + collection.add(response.data, { at: index }); }).fail(function() { MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts')); }); @@ -302,17 +302,19 @@ define([ }; }, transport: function(options, success, failure) { - var taxonomies, - promise = CommunicationComponent.getTaxonomies(that.model.get('contentType')).then(function(tax) { - taxonomies = tax; + var taxonomies; + var promise = CommunicationComponent.getTaxonomies( + that.model.get('contentType') + ).then(function(response) { + taxonomies = response.data; // Fetch available terms based on the list of taxonomies already fetched var promise = CommunicationComponent.getTerms({ search: options.data.term, taxonomies: _.keys(taxonomies) - }).then(function(terms) { + }).then(function(response) { return { taxonomies: taxonomies, - terms: terms, + terms: response.data }; }); return promise; diff --git a/assets/js/src/newsletter_editor/components/communication.js b/assets/js/src/newsletter_editor/components/communication.js index 7792425f6e..439001f525 100644 --- a/assets/js/src/newsletter_editor/components/communication.js +++ b/assets/js/src/newsletter_editor/components/communication.js @@ -27,8 +27,8 @@ define([ return Module._cachedQuery({ action: 'getPostTypes', options: {}, - }).then(function(types) { - return _.values(types); + }).then(function(response) { + return _.values(response.data); }); }; diff --git a/lib/API/Endpoint.php b/lib/API/Endpoint.php index 4cc419c481..874514f6ea 100644 --- a/lib/API/Endpoint.php +++ b/lib/API/Endpoint.php @@ -8,7 +8,6 @@ abstract class Endpoint { function successResponse( $data = array(), $meta = array(), $status = Response::STATUS_OK ) { - return new SuccessResponse($data, $meta, $status); } diff --git a/lib/API/Endpoints/AutomatedLatestContent.php b/lib/API/Endpoints/AutomatedLatestContent.php index ffe9ee1bec..f866a91637 100644 --- a/lib/API/Endpoints/AutomatedLatestContent.php +++ b/lib/API/Endpoints/AutomatedLatestContent.php @@ -1,9 +1,11 @@ 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); } } diff --git a/lib/API/SuccessResponse.php b/lib/API/SuccessResponse.php index 251774c173..35c2134741 100644 --- a/lib/API/SuccessResponse.php +++ b/lib/API/SuccessResponse.php @@ -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 + ); } } \ No newline at end of file diff --git a/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js b/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js index 30baad156f..1b65febd1d 100644 --- a/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js +++ b/tests/javascript/newsletter_editor/blocks/automatedLatestContent.spec.js @@ -53,7 +53,7 @@ define([ mock1.expects('trigger').once().withArgs('refreshPosts', postsSet1); mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2); - model.refreshBlocks([block1, block2], [postsSet1, postsSet2]); + model.refreshBlocks([block1, block2], { data: [postsSet1, postsSet2] }); mock1.verify(); mock2.verify(); diff --git a/tests/javascript/newsletter_editor/components/communication.spec.js b/tests/javascript/newsletter_editor/components/communication.spec.js index d1d423a247..a2549e7615 100644 --- a/tests/javascript/newsletter_editor/components/communication.spec.js +++ b/tests/javascript/newsletter_editor/components/communication.spec.js @@ -12,8 +12,10 @@ define([ post: function() { var deferred = jQuery.Deferred(); deferred.resolve({ - 'post': 'val1', - 'page': 'val2', + data: { + 'post': 'val1', + 'page': 'val2', + } }); return deferred; }