Fix ALC filtering for custom taxonomies and post types
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace MailPoet\API\Endpoints;
|
||||
use MailPoet\API\Endpoint as APIEndpoint;
|
||||
use MailPoet\WP\Posts as WPPosts;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -31,9 +32,9 @@ class AutomatedLatestContent extends APIEndpoint {
|
||||
$page = (isset($data['page'])) ? (int)$data['page'] : 1;
|
||||
|
||||
return $this->successResponse(
|
||||
get_terms(
|
||||
$taxonomies,
|
||||
WPPosts::getTerms(
|
||||
array(
|
||||
'taxonomy' => $taxonomies,
|
||||
'hide_empty' => false,
|
||||
'search' => $search,
|
||||
'number' => $limit,
|
||||
|
@ -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;
|
||||
|
18
lib/WP/Posts.php
Normal file
18
lib/WP/Posts.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace MailPoet\WP;
|
||||
|
||||
class Posts {
|
||||
|
||||
static function getTerms($args) {
|
||||
// Since WordPress 4.5.0 signature of get_terms changed to require
|
||||
// one argument array, where taxonomy is key of that array
|
||||
if(version_compare(bloginfo('version'), '4.5.0', '>=')) {
|
||||
return get_terms($args);
|
||||
} else {
|
||||
$taxonomy = $args['taxonomy'];
|
||||
unset($args['taxonomy']);
|
||||
return get_terms($taxonomy, $args);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
63
tests/unit/Newsletter/AutomatedLatestContentTest.php
Normal file
63
tests/unit/Newsletter/AutomatedLatestContentTest.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
use MailPoet\Newsletter\AutomatedLatestContent;
|
||||
|
||||
class AutomatedLatestContentTest extends MailPoetTest {
|
||||
function __construct() {
|
||||
$this->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');
|
||||
}
|
||||
}
|
56
tests/unit/WP/PostsTest.php
Normal file
56
tests/unit/WP/PostsTest.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
use Helper\WordPress as WordPressHelper;
|
||||
use MailPoet\WP\Posts;
|
||||
|
||||
class WPPostsTest extends MailPoetTest {
|
||||
|
||||
function testGetTermsProxiesCallToWordPress() {
|
||||
$args = array(
|
||||
'taxonomy' => '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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user