';
// Set recusive result to empty string as default
$recursive_result = '';
// Check if this is not top level category
if ($cat->term_id !== 0) {
// Call function recursively
$recursive_result = self::traverse_term_tree($cat->term_id, $current_level, $result);
}
// Append recursive result to existing one
$result .= $recursive_result;
// !IMPORTANT if while recursive call pattern was not replaced (not last child)
// We need to replace it by ourselves first case is when this is top level category
// (recursive result is empty) and second is when recursive result is not empty in this case
// we found category that has children
$result = str_replace(self::PLACEHOLDER_HAS_CHILDREN, !empty($recursive_result) ? self::CLASS_HAS_CHILDREN : '', $result);
}
// Close tags
$result .= '
';
$result .= '
';
} else {
// Nothing for now
}
return $result;
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance)
{
if (is_active_widget(false, false, $this->id_base, true)) {
$this->init_widget($instance);
}
if (is_category()) {
$category = get_category(get_query_var('cat'));
$this->current_category_id = $category->cat_ID;
}
echo $args['before_widget'] . "\n";
if ($this->title) {
echo $args['before_title'] . $this->title . $args['after_title'];
}
// Put all output inside container wrapper
echo '
' . "\n";
// Output main part categories menu
echo self::traverse_term_tree();
// Close container tag
echo '