diff --git a/assets/js/src/newsletter_editor/components/wordpress.js b/assets/js/src/newsletter_editor/components/wordpress.js new file mode 100644 index 0000000000..3ea0bab53d --- /dev/null +++ b/assets/js/src/newsletter_editor/components/wordpress.js @@ -0,0 +1,63 @@ +define('newsletter_editor/components/wordpress', [ + 'newsletter_editor/App', + 'backbone', + 'backbone.marionette', + 'mailpoet', + 'ajax' + ], function(EditorApplication, Backbone, Marionette, MailPoet) { + + EditorApplication.module("components.wordpress", function(Module, App, Backbone, Marionette, $, _) { + "use strict"; + + var postTypesCache, + taxonomiesCache = [], + termsCache = []; + + Module.getPostTypes = function() { + if (!postTypesCache) { + postTypesCache = MailPoet.Ajax.post({ + endpoint: 'wordpress', + action: 'getPostTypes', + data: {}, + }).then(function(types) { + return _.values(types); + }); + } + + return postTypesCache; + }; + + Module.getTaxonomies = function(postType) { + if (!taxonomiesCache[postType]) { + taxonomiesCache[postType] = MailPoet.Ajax.post({ + endpoint: 'wordpress', + action: 'getTaxonomies', + data: { + postType: postType, + }, + }); + } + + return taxonomiesCache[postType]; + }; + + Module.getTerms = function(taxonomies) { + if (!termsCache[taxonomies]) { + termsCache[taxonomies] = MailPoet.Ajax.post({ + endpoint: 'wordpress', + action: 'getTerms', + data: { + taxonomies: taxonomies, + }, + }); + } + + return termsCache[taxonomies]; + }; + + App.on('start', function(options) { + // Prefetch post types + Module.getPostTypes(); + }); + }); +}); diff --git a/webpack.config.js b/webpack.config.js index 179f7dfe01..7f90b2105c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -84,6 +84,7 @@ config.push(_.extend({}, baseConfig, { 'newsletter_editor/components/content.js', 'newsletter_editor/components/heading.js', 'newsletter_editor/components/save.js', + 'newsletter_editor/components/wordpress.js', 'newsletter_editor/behaviors/BehaviorsLookup.js', 'newsletter_editor/behaviors/ColorPickerBehavior.js', 'newsletter_editor/behaviors/ContainerDropZoneBehavior.js',