diff --git a/assets/js/src/newsletter_editor/components/communication.js b/assets/js/src/newsletter_editor/components/communication.js index 2647474f4f..f3729514b9 100644 --- a/assets/js/src/newsletter_editor/components/communication.js +++ b/assets/js/src/newsletter_editor/components/communication.js @@ -9,7 +9,7 @@ define([ Module._query = function(args) { return MailPoet.Ajax.post({ - endpoint: 'wordpress', + endpoint: 'automatedLatestContent', action: args.action, data: args.options || {}, }); diff --git a/lib/Newsletter/AutomatedLatestContent.php b/lib/Newsletter/AutomatedLatestContent.php new file mode 100644 index 0000000000..d1a63dd85f --- /dev/null +++ b/lib/Newsletter/AutomatedLatestContent.php @@ -0,0 +1,69 @@ + (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['posts']) && is_array($args['posts'])) { + $parameters['post__in'] = $args['posts']; + } + $parameters['tax_query'] = $this->constructTaxonomiesQuery($args); + return get_posts($parameters); + } + + function transformPosts($args, $posts) { + $transformer = new Transformer($args); + return $transformer->transform($posts); + } + + function constructTaxonomiesQuery($args) { + $taxonomies_query = array(); + if(isset($args['terms']) && is_array($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 = array( + 'post_tag' => $tags, + 'category' => $categories + ); + foreach($taxonomies 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'; + return $taxonomies_query; + } + } + return $taxonomies_query; + } +} \ No newline at end of file diff --git a/lib/Newsletter/Renderer/Blocks/AutomatedLatestContent.php b/lib/Newsletter/Renderer/Blocks/AutomatedLatestContent.php index 445b4dce68..a7c4f038af 100644 --- a/lib/Newsletter/Renderer/Blocks/AutomatedLatestContent.php +++ b/lib/Newsletter/Renderer/Blocks/AutomatedLatestContent.php @@ -1,15 +1,11 @@ fetchWordPressPosts($element); - $transformed_posts = array('blocks' => $transformer->transform($posts)); + $ALC = new \MailPoet\Newsletter\AutomatedLatestContent(); + $posts = $ALC->getPosts($element); + $transformed_posts = array('blocks' => $ALC->transformPosts($element, $posts)); $renderer = new Renderer(); return $renderer->render($transformed_posts, $column_count); } diff --git a/lib/Router/AutomatedLatestContent.php b/lib/Router/AutomatedLatestContent.php new file mode 100644 index 0000000000..c6c189f8e0 --- /dev/null +++ b/lib/Router/AutomatedLatestContent.php @@ -0,0 +1,43 @@ +ALC = new \MailPoet\Newsletter\AutomatedLatestContent(); + } + + function getPostTypes() { + wp_send_json(get_post_types(array(), 'objects')); + } + + function getTaxonomies($args) { + $post_type = (isset($args['postType'])) ? $args['postType'] : 'post'; + wp_send_json(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; + wp_send_json(get_terms($taxonomies, array( + 'hide_empty' => false, + 'search' => $search, + 'number' => $limit, + 'offset' => $limit * ($page - 1), + ))); + } + + function getPosts($args) { + wp_send_json($this->ALC->getPosts($args)); + } + + function getTransformedPosts($args) { + $posts = $this->ALC->getPosts($args); + wp_send_json($this->ALC->transformPosts($args, $posts)); + } +} diff --git a/lib/Router/Wordpress.php b/lib/Router/Wordpress.php deleted file mode 100644 index 8805fd1c0a..0000000000 --- a/lib/Router/Wordpress.php +++ /dev/null @@ -1,106 +0,0 @@ - false, - 'search' => $search, - 'number' => $limit, - 'offset' => $limit * ($page - 1), - ))); - } - - function getPosts($args) { - wp_send_json($this->fetchWordPressPosts($args)); - } - - function getTransformedPosts($args) { - $posts = $this->fetchWordPressPosts($args); - - $transformer = new Transformer($args); - wp_send_json($transformer->transform($posts)); - } - - function fetchWordPressPosts($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['posts']) && is_array($args['posts'])) { - $parameters['post__in'] = $args['posts']; - } - - $parameters['tax_query'] = $this->constructTaxonomiesQuery($args); - - return get_posts($parameters); - } - - function constructTaxonomiesQuery($args) { - $taxonomies_query = array(); - - if (isset($args['terms']) && is_array($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 = array('post_tag' => $tags, 'category' => $categories); - foreach ($taxonomies 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'; - - return $taxonomies_query; - } - } - - return $taxonomies_query; - } -}