public function terms($tax = '', $merge = true, $TermClass = '')
{
$taxonomies = array();
$TermClass = $TermClass ?: $this->TermClass;
if (is_string($merge) && class_exists($merge)) {
$TermClass = $merge;
}
if (is_array($tax)) {
$taxonomies = $tax;
}
if (is_string($tax)) {
if (in_array($tax, array('all', 'any', ''))) {
$taxonomies = get_object_taxonomies($this->post_type);
} else {
$taxonomies = array($tax);
}
}
$term_class_objects = array();
foreach ($taxonomies as $taxonomy) {
if (in_array($taxonomy, array('tag', 'tags'))) {
$taxonomy = 'post_tag';
}
if ($taxonomy == 'categories') {
$taxonomy = 'category';
}
$terms = wp_get_post_terms($this->ID, $taxonomy);
if (is_wp_error($terms)) {
/* @var $terms WP_Error */
Helper::error_log("Error retrieving terms for taxonomy '{$taxonomy}' on a post in timber-post.php");
Helper::error_log('tax = ' . print_r($tax, true));
Helper::error_log('WP_Error: ' . $terms->get_error_message());
return $term_class_objects;
}
// map over array of wordpress terms, and transform them into instances of the TermClass
$terms = array_map(function ($term) use($TermClass, $taxonomy) {
return call_user_func(array($TermClass, 'from'), $term->term_id, $taxonomy);
}, $terms);
if ($merge && is_array($terms)) {
$term_class_objects = array_merge($term_class_objects, $terms);
} else {
if (count($terms)) {
$term_class_objects[$taxonomy] = $terms;
}
}
}
return $term_class_objects;
}