CatPackages.php
4 years ago
Categories.php
4 years ago
ListPackages.php
4 years ago
NewDownloads.php
4 years ago
PackageInfo.php
4 years ago
Search.php
4 years ago
Tags.php
2 years ago
TopDownloads.php
4 years ago
WidgetController.php
2 years ago
Categories.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | use WPDM\Category\CategoryController; |
| 4 | |
| 5 | if (!defined("ABSPATH")) die("Shit happens!"); |
| 6 | if (!class_exists('WPDM_Categories')) { |
| 7 | class WPDM_Categories extends \WP_Widget |
| 8 | { |
| 9 | /** constructor */ |
| 10 | function __construct() |
| 11 | { |
| 12 | parent::__construct(false, 'WPDM Categories'); |
| 13 | } |
| 14 | |
| 15 | /** @see WP_Widget::widget */ |
| 16 | function widget($args, $instance) |
| 17 | { |
| 18 | extract($args); |
| 19 | $instance['title'] = isset($instance['title']) ? $instance['title'] : ''; |
| 20 | $title = apply_filters('widget_title', $instance['title']); |
| 21 | $parent = isset($instance['parent']) && $instance['parent'] > 0 ? intval($instance['parent']) : 0; |
| 22 | $style = isset($instance['style']) ? esc_attr($instance['style']) : 'flat'; |
| 23 | $toplevel = isset($instance['toplevel']) ? esc_attr($instance['toplevel']) : 0; |
| 24 | $category_page = isset($instance['category_page']) ? esc_attr($instance['category_page']) : 'showall'; |
| 25 | $hideon0 = isset($instance['hideon0']) ? esc_attr($instance['hideon0']) : 0; |
| 26 | |
| 27 | |
| 28 | $args = array( |
| 29 | 'orderby' => 'name', |
| 30 | 'order' => 'ASC', |
| 31 | 'hide_empty' => false, |
| 32 | 'exclude' => array(), |
| 33 | 'exclude_tree' => array(), |
| 34 | 'include' => array(), |
| 35 | 'number' => '', |
| 36 | 'fields' => 'all', |
| 37 | 'slug' => '', |
| 38 | 'parent' => '', |
| 39 | 'hierarchical' => ($style == 'flat' ? false : true), |
| 40 | 'child_of' => $parent, |
| 41 | 'childless' => false, |
| 42 | 'get' => '', |
| 43 | 'name__like' => '', |
| 44 | 'description__like' => '', |
| 45 | 'pad_counts' => false, |
| 46 | 'offset' => '', |
| 47 | 'search' => '', |
| 48 | 'cache_domain' => 'core' |
| 49 | ); |
| 50 | |
| 51 | $object = get_queried_object(); |
| 52 | if ($category_page == 'subs' && !isset($object->post_type)) |
| 53 | $args['parent'] = get_queried_object_id(); |
| 54 | |
| 55 | $terms = get_terms("wpdmcategory", $args); |
| 56 | |
| 57 | if ($hideon0 == 1 && count($terms) == 0) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | echo $before_widget; |
| 62 | if ($title) |
| 63 | echo $before_title . $title . $after_title; |
| 64 | |
| 65 | if ($style == 'flat') { |
| 66 | echo "<div class='w3eden'><div class='list-group'>"; |
| 67 | foreach ($terms as $term) { |
| 68 | if (($toplevel == 1 && ($term->parent == 0 || $term->parent === $parent)) || $toplevel == 0) |
| 69 | echo "<a href='" . get_term_link($term) . "' class='list-group-item d-flex justify-content-between align-items-center'>{$term->name}<span class='badge'>{$term->count}</span></a>\n"; |
| 70 | } |
| 71 | |
| 72 | echo "</div></div>\n"; |
| 73 | } else if ($style == 'icon') { |
| 74 | |
| 75 | |
| 76 | echo "<div class='wpdm-categories icon-list'>"; |
| 77 | foreach ($terms as $term) { |
| 78 | $icon = CategoryController::icon($term->term_id); |
| 79 | if ($icon == '') $icon = "https://cdn1.iconfinder.com/data/icons/gradient-android-apps/64/1-05-512.png"; |
| 80 | if (($toplevel == 1 && ($term->parent == 0 || $term->parent === $parent)) || $toplevel == 0) |
| 81 | echo "<div class='icon-cat'><a href='" . get_term_link($term) . "'><img src='{$icon}' class='cat-icon' /> {$term->name}</a></div>\n"; |
| 82 | } |
| 83 | echo "</div>"; |
| 84 | } else { |
| 85 | |
| 86 | function wpdm_categories_tree($parent = 0, $selected = array()) |
| 87 | { |
| 88 | $categories = get_terms('wpdmcategory', array('hide_empty' => 0, 'parent' => $parent)); |
| 89 | $checked = ""; |
| 90 | foreach ($categories as $category) { |
| 91 | if ($selected) { |
| 92 | foreach ($selected as $ptype) { |
| 93 | if ($ptype->term_id == $category->term_id) { |
| 94 | $checked = "checked='checked'"; |
| 95 | break; |
| 96 | } else $checked = ""; |
| 97 | } |
| 98 | } |
| 99 | echo '<li><a href="' . get_term_link($category) . '"> ' . $category->name . ' </a>'; |
| 100 | $termchildren = get_term_children($category->term_id, 'wpdmcategory'); |
| 101 | if ($termchildren) { |
| 102 | echo "<ul>"; |
| 103 | wpdm_categories_tree($category->term_id, $selected); |
| 104 | echo "</ul>"; |
| 105 | } |
| 106 | echo "</li>"; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | echo "<ul class='wpdm-categories'>"; |
| 111 | $cparent = $parent; |
| 112 | if ($cparent !== 0) { |
| 113 | $cparent = get_term_by('slug', $cparent, 'wpdmcategory'); |
| 114 | $cparent = $cparent->term_id; |
| 115 | } |
| 116 | wpdm_categories_tree($cparent, $terms); |
| 117 | echo "</ul>"; |
| 118 | } |
| 119 | echo $after_widget; ?> |
| 120 | <?php |
| 121 | } |
| 122 | |
| 123 | /** @see WP_Widget::update */ |
| 124 | function update($new_instance, $old_instance) |
| 125 | { |
| 126 | return $new_instance; |
| 127 | } |
| 128 | |
| 129 | /** @see WP_Widget::form */ |
| 130 | function form($instance) |
| 131 | { |
| 132 | $title = isset($instance['title']) ? esc_attr($instance['title']) : ""; |
| 133 | $parent = isset($instance['parent']) ? intval($instance['parent']) : 0; |
| 134 | $style = isset($instance['style']) ? esc_attr($instance['style']) : 'flat'; |
| 135 | $toplevel = isset($instance['toplevel']) ? esc_attr($instance['toplevel']) : 0; |
| 136 | $category_page = isset($instance['category_page']) ? esc_attr($instance['category_page']) : 0; |
| 137 | $hideon0 = isset($instance['hideon0']) ? esc_attr($instance['hideon0']) : 0; |
| 138 | ?> |
| 139 | <p> |
| 140 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
| 141 | <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" |
| 142 | name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>"/> |
| 143 | </p> |
| 144 | <p> |
| 145 | <label for="<?php echo $this->get_field_id('parent'); ?>"><?php _e('Parent:'); ?></label><br/> |
| 146 | <?php wpdm_dropdown_categories($this->get_field_name('parent'), $parent, $this->get_field_id('parent')); ?> |
| 147 | </p> |
| 148 | <p> |
| 149 | <label for="<?php echo $this->get_field_id('category_page'); ?>"><?php _e('On Category Page:', 'download-manger'); ?></label><br/> |
| 150 | <select id="<?php echo $this->get_field_id('category_page'); ?>" |
| 151 | name="<?php echo $this->get_field_name('category_page'); ?>"> |
| 152 | <option value="showall" <?php selected('showall', $category_page); ?>><?php echo __("Show all", "download-manager") ?></option> |
| 153 | <option value="subs" <?php selected('subs', $category_page); ?>><?php echo __("Show subcategories only", "download-manager") ?></option> |
| 154 | </select> |
| 155 | </p> |
| 156 | <p> |
| 157 | <label><?php _e('Style:'); ?></label><br/> |
| 158 | <label><input type="radio" |
| 159 | name="<?php echo $this->get_field_name('style'); ?>" <?php checked('flat', $style); ?> |
| 160 | value="flat"> Flat List</label><br/> |
| 161 | <label><input type="radio" |
| 162 | name="<?php echo $this->get_field_name('style'); ?>" <?php checked('tree', $style); ?> |
| 163 | value="tree"> Hierarchy List</label><br/> |
| 164 | <label><input type="radio" |
| 165 | name="<?php echo $this->get_field_name('style'); ?>" <?php checked('icon', $style); ?> |
| 166 | value="icon"> Icon List ( Flat )</label><br/> |
| 167 | <!-- label><input type="radio" name="<?php echo $this->get_field_name('style'); ?>" <?php checked('dropdown', $style); ?> value="dropdown"> Dropdown List</label></br --> |
| 168 | </p> |
| 169 | <p> |
| 170 | <label for="<?php echo $this->get_field_id('toplevel'); ?>"><input type="checkbox" |
| 171 | name="<?php echo $this->get_field_name('toplevel'); ?>" <?php checked('1', $toplevel); ?> |
| 172 | value="1"> <?php _e('Top Level Only', 'download-manager'); ?> |
| 173 | </label><br/> |
| 174 | </p> |
| 175 | <p> |
| 176 | <label for="<?php echo $this->get_field_id('hideon0'); ?>"><input type="checkbox" |
| 177 | name="<?php echo $this->get_field_name('hideon0'); ?>" <?php checked('1', $hideon0); ?> |
| 178 | value="1"> <?php _e('Hide widget when no category', 'download-manager'); ?> |
| 179 | </label><br/> |
| 180 | </p> |
| 181 | <?php |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | register_widget('WPDM_Categories'); |
| 188 | |
| 189 |