diff --git a/lib/API/Endpoints/AutomatedLatestContent.php b/lib/API/Endpoints/AutomatedLatestContent.php index 9caeac45e1..f45a59b90f 100644 --- a/lib/API/Endpoints/AutomatedLatestContent.php +++ b/lib/API/Endpoints/AutomatedLatestContent.php @@ -1,6 +1,7 @@ successResponse( - get_terms( - $taxonomies, + WPPosts::getTerms( array( + 'taxonomy' => $taxonomies, 'hide_empty' => false, 'search' => $search, 'number' => $limit, diff --git a/lib/Newsletter/AutomatedLatestContent.php b/lib/Newsletter/AutomatedLatestContent.php index 701e521584..c9abb0549a 100644 --- a/lib/Newsletter/AutomatedLatestContent.php +++ b/lib/Newsletter/AutomatedLatestContent.php @@ -79,18 +79,16 @@ class AutomatedLatestContent { function constructTaxonomiesQuery($args) { $taxonomies_query = array(); if(isset($args['terms']) && is_array($args['terms'])) { - // Add filtering by tags and categories - $tags = array(); - $categories = array(); + $taxonomies = array(); + // Categorize terms based on their taxonomies foreach($args['terms'] as $term) { - if($term['taxonomy'] === 'category') { - $categories[] = $term['id']; - } else if($term['taxonomy'] === 'post_tag') $tags[] = $term['id']; + $taxonomy = $term['taxonomy']; + if(!isset($taxonomies[$taxonomy])) { + $taxonomies[$taxonomy] = array(); + } + $taxonomies[$taxonomy][] = $term['id']; } - $taxonomies = array( - 'post_tag' => $tags, - 'category' => $categories - ); + foreach($taxonomies as $taxonomy => $terms) { if(!empty($terms)) { $tax = array( @@ -108,7 +106,6 @@ class AutomatedLatestContent { // 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; @@ -125,4 +122,4 @@ class AutomatedLatestContent { remove_action('posts_where', array($this, 'filterOutSentPosts')); } } -} \ No newline at end of file +} diff --git a/lib/WP/Posts.php b/lib/WP/Posts.php new file mode 100644 index 0000000000..b40400132a --- /dev/null +++ b/lib/WP/Posts.php @@ -0,0 +1,18 @@ +=')) { + return get_terms($args); + } else { + $taxonomy = $args['taxonomy']; + unset($args['taxonomy']); + return get_terms($taxonomy, $args); + } + } + +} diff --git a/tests/_support/Helper/WordPress.php b/tests/_support/Helper/WordPress.php index 86b2d478c9..60ca4675fb 100644 --- a/tests/_support/Helper/WordPress.php +++ b/tests/_support/Helper/WordPress.php @@ -36,6 +36,14 @@ function override($func, $args) { return call_user_func_array('\\' . $func, $args); } +function get_terms($key) { + return override(__FUNCTION__, func_get_args()); +} + +function bloginfo($key) { + return override(__FUNCTION__, func_get_args()); +} + function get_option($key) { return override(__FUNCTION__, func_get_args()); } diff --git a/tests/unit/Newsletter/AutomatedLatestContentTest.php b/tests/unit/Newsletter/AutomatedLatestContentTest.php new file mode 100644 index 0000000000..a66fd0c679 --- /dev/null +++ b/tests/unit/Newsletter/AutomatedLatestContentTest.php @@ -0,0 +1,63 @@ +alc = new AutomatedLatestContent(); + } + + function testItCategorizesTermsToTaxonomies() { + $args = array( + 'terms' => array( + array( + 'id' => 1, + 'taxonomy' => 'post_tag' + ), + array( + 'id' => 2, + 'taxonomy' => 'product_tag', + ), + array( + 'id' => 3, + 'taxonomy' => 'post_tag' + ) + ), + 'inclusionType' => 'include', + ); + + expect($this->alc->constructTaxonomiesQuery($args))->equals(array( + array( + 'taxonomy' => 'post_tag', + 'field' => 'id', + 'terms' => array(1, 3) + ), + array( + 'taxonomy' => 'product_tag', + 'field' => 'id', + 'terms' => array(2) + ), + 'relation' => 'OR' + )); + } + + function testItCanExcludeTaxonomies() { + $args = array( + 'terms' => array( + array( + 'id' => 7, + 'taxonomy' => 'post_tag' + ), + array( + 'id' => 8, + 'taxonomy' => 'post_tag' + ) + ), + 'inclusionType' => 'exclude', + ); + + $query = $this->alc->constructTaxonomiesQuery($args); + + expect($query[0]['operator'])->equals('NOT IN'); + expect($query['relation'])->equals('AND'); + } +} diff --git a/tests/unit/WP/PostsTest.php b/tests/unit/WP/PostsTest.php new file mode 100644 index 0000000000..7b4f0950eb --- /dev/null +++ b/tests/unit/WP/PostsTest.php @@ -0,0 +1,56 @@ + 'post_tags', + 'hide_empty' => true + ); + + WordPressHelper::interceptFunction('bloginfo', function($key) { + return '4.6.0'; + }); + + WordPressHelper::interceptFunction('get_terms', function($key) { + return array( + 'call check' => 'get_terms called', + 'arguments' => func_get_args() + ); + }); + + $result = Posts::getTerms($args); + expect($result['call check'])->equals('get_terms called'); + expect($result['arguments'][0])->equals($args); + } + + function testGetTermsPassesTaxonomyAsFirstArgumentInOldVersions() { + $args = array( + 'taxonomy' => 'post_tags', + 'hide_empty' => true + ); + + WordPressHelper::interceptFunction('bloginfo', function($key) { + return '4.4.0'; + }); + + WordPressHelper::interceptFunction('get_terms', function($key) { + return array( + 'call check' => 'get_terms called', + 'arguments' => func_get_args() + ); + }); + + $result = Posts::getTerms($args); + expect($result['call check'])->equals('get_terms called'); + expect($result['arguments'][0])->equals($args['taxonomy']); + expect($result['arguments'][1])->equals(array_diff_key($args, array('taxonomy' => ''))); + } + + function _afterStep() { + WordPressHelper::releaseAllFunctions(); + } +}