__('This widget allows to display categories hierarchy in an accordion style', 'text_domain'),) // Args ); } /** * Init widget (styles, scripts) * Enqueue plugin style-file */ public function init_widget($instance) { $this->parse_instance($instance); wp_register_style('accordion-category-style', plugins_url('css/accordion-category-style.css', __FILE__)); wp_enqueue_style('accordion-category-style'); // Register the script like this for a plugin: wp_enqueue_script('jquery'); wp_register_script('accordion-category-script', plugins_url('js/accordion-categories.js', __FILE__), array('jquery'),"1.0.0", true); wp_enqueue_script('accordion-category-script'); $script_params = [ self::AUTO_GENERATE_CSS => $this->auto_generate_css, self::AUTO_OVERFLOW_PADDING => $this->auto_overflow_padding, self::SHOW_CATEGORY_ICON => $this->show_category_icon, self::SHOW_EMPTY_CATEGORIES => $this->show_empty_categories, self::SHOW_POST_COUNT => $this->show_post_count, self::OPTION_NESTING_TYPE => $this->nesting_type, self::TITLE => $this->title ]; wp_localize_script('accordion-category-script', 'accordionMenuParams', $script_params); } public function traverse_term_tree($parent = 0, $current_level = 0, &$prevous_result = '') { $result = ''; $args = array( 'hide_empty' => ($this->show_empty_categories ? '0' : '1'), 'orderby' => 'name', 'order' => 'ASC', 'taxonomy' => $this->term_type, 'pad_counts' => 1 ); $categories = get_categories($args); $next = wp_list_filter($categories, array('parent' => $parent)); if ($next) { // Increase the current depth level $level_class = sprintf(self::PATTERN_LEVEL_CLASS, $current_level); // Create new list // 1. Add general list class // 2. Add level class // 3. If this is the root list add is-root class to make it visible $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 '
'; echo $args['after_widget'] . "\n"; } private function parse_instance($instance) { $this->title = sanitize_text_field(isset($instance[self::TITLE]) ? $instance[self::TITLE] : ''); $this->show_post_count = isset($instance[self::SHOW_POST_COUNT]) ? (bool)$instance[self::SHOW_POST_COUNT] : true; $this->show_empty_categories = isset($instance[self::SHOW_EMPTY_CATEGORIES]) ? (bool)$instance[self::SHOW_EMPTY_CATEGORIES] : true; $this->show_category_icon = isset($instance[self::SHOW_CATEGORY_ICON]) ? (bool)$instance[self::SHOW_CATEGORY_ICON] : true; $this->nesting_type = isset($instance[self::OPTION_NESTING_TYPE]) ? $instance[self::OPTION_NESTING_TYPE] : self::$NESTING_TYPES[self::INDEX_NESTING_TEXT_INDENT]; $this->auto_generate_css = isset($instance[self::AUTO_GENERATE_CSS]) ? (bool)$instance[self::AUTO_GENERATE_CSS] : true; $this->auto_overflow_padding = isset($instance[self::AUTO_OVERFLOW_PADDING]) ? (bool)$instance[self::AUTO_OVERFLOW_PADDING] : true; $this->default_category_icon = sanitize_text_field(isset($instance[self::OPTION_DEFAULT_CATEGORY_ICON]) ? $instance[self::OPTION_DEFAULT_CATEGORY_ICON] : ""); } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form($instance) { $this->parse_instance($instance); ?>

show_empty_categories); ?> />
show_post_count); ?> />
show_category_icon); ?> />
auto_generate_css); ?> />
auto_overflow_padding); ?> />