public function comments($count = 0, $order = 'wp', $type = 'comment', $status = 'approve', $CommentClass = 'Timber\\Comment')
{
global $overridden_cpage, $user_ID;
$overridden_cpage = false;
$commenter = wp_get_current_commenter();
$comment_author_email = $commenter['comment_author_email'];
$args = array('post_id' => $this->ID, 'status' => $status, 'order' => $order);
if ($count > 0) {
$args['number'] = $count;
}
if (strtolower($order) == 'wp' || strtolower($order) == 'wordpress') {
$args['order'] = get_option('comment_order');
}
if ($user_ID) {
$args['include_unapproved'] = array($user_ID);
} elseif (!empty($comment_author_email)) {
$args['include_unapproved'] = array($comment_author_email);
}
$comments = get_comments($args);
$timber_comments = array();
if ('' == get_query_var('cpage') && get_option('page_comments')) {
set_query_var('cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1);
$overridden_cpage = true;
}
foreach ($comments as $key => &$comment) {
$timber_comment = new $CommentClass($comment);
$timber_comments[$timber_comment->id] = $timber_comment;
}
// Build a flattened (depth=1) comment tree
$comments_tree = array();
foreach ($timber_comments as $key => $comment) {
if (!$comment->is_child()) {
continue;
}
$tree_element = $comment;
do {
$tree_element = $timber_comments[$tree_element->comment_parent];
} while ($tree_element->is_child());
$comments_tree[$tree_element->id][] = $comment->id;
}
// Add child comments to the relative "super parents"
foreach ($comments_tree as $comment_parent => $comment_children) {
foreach ($comment_children as $comment_child) {
$timber_comments[$comment_parent]->add_child($timber_comments[$comment_child]);
unset($timber_comments[$comment_child]);
}
}
$timber_comments = array_values($timber_comments);
return $timber_comments;
}