views
2 years ago
Category.php
2 years ago
CategoryController.php
5 years ago
Shortcodes.php
2 years ago
CategoryController.php
243 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Download manager category controller class |
| 4 | */ |
| 5 | namespace WPDM\Category; |
| 6 | |
| 7 | use WPDM\__\Template; |
| 8 | |
| 9 | class CategoryController |
| 10 | { |
| 11 | |
| 12 | private $cbTreeInit = 0; |
| 13 | public $shortcode; |
| 14 | |
| 15 | function __construct() |
| 16 | { |
| 17 | add_filter("template_include", [$this, 'wpdmproTemplates'], 9999); |
| 18 | $this->shortcode = new Shortcodes(); |
| 19 | } |
| 20 | |
| 21 | function hArray($skip_filter = 1, $parent = 0) |
| 22 | { |
| 23 | $terms = get_terms(array('taxonomy' => 'wpdmcategory', 'parent' => $parent, 'hide_empty' => false)); |
| 24 | $allterms = _get_term_hierarchy('wpdmcategory'); |
| 25 | $allcats = array(); |
| 26 | foreach ($terms as $term) { |
| 27 | $allcats[$term->term_id] = array('category' => $term, 'access' => maybe_unserialize(get_term_meta($term->term_id, '__wpdm_access')), 'childs' => array()); |
| 28 | $this->exploreChilds($term, $allcats[$term->term_id]['childs']); |
| 29 | } |
| 30 | |
| 31 | if ($skip_filter === 0){ |
| 32 | /** |
| 33 | * @deprecated use WPDM_Category_CategoryController_hArray instead |
| 34 | */ |
| 35 | $allcats = apply_filters("WPDM_libs_CategoryHandler_hArray", $allcats); |
| 36 | $allcats = apply_filters("WPDM_Category_CategoryController_hArray", $allcats); |
| 37 | } |
| 38 | |
| 39 | return $allcats; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Generates WordPress Download Manager category selector wit checkbox and ul/li |
| 44 | * @param $name |
| 45 | * @param array $selected |
| 46 | * @param array $extras |
| 47 | */ |
| 48 | function checkboxTree($name, $selected = array(), $extras = array()) |
| 49 | { |
| 50 | echo "<ul class='ptypes m-0 p-0' id='wpdmcat-tree'>"; |
| 51 | $parent = wpdm_valueof($extras, 'parent', ['validate' => 'int']); |
| 52 | $allcats = WPDM()->categories->hArray(0, $parent); |
| 53 | |
| 54 | /*$cparent = is_array($extras) && isset($extras['base_category']) ? $extras['base_category'] : 0; |
| 55 | if ($cparent !== 0) { |
| 56 | $cparent = get_term($cparent); |
| 57 | $cparent = $cparent->term_id; |
| 58 | echo "<input type='hidden' value='{$cparent}' name='cats[]' />"; |
| 59 | }*/ |
| 60 | if($parent > 0 && wpdm_valueof($extras, 'hide_parent', ['validate' => 'int', 'default' => 0]) == 0) { |
| 61 | $term = get_term($parent); |
| 62 | $value = wpdm_valueof($extras, 'value') === 'slug' ? $term->slug : $term->term_id; |
| 63 | ?> |
| 64 | <ul> |
| 65 | <li class="<?php echo wpdm_valueof($extras, 'liclass'); ?>"> |
| 66 | <label><input type="checkbox" <?php checked(1, in_array($value, $selected)); ?> |
| 67 | name="<?php echo $name; ?>[]" |
| 68 | class="<?php echo wpdm_valueof($extras, 'cbclass'); ?>" |
| 69 | value="<?php echo $value; ?>"> <?php echo $term->name; ?> </label> |
| 70 | <ul> |
| 71 | <?php |
| 72 | } |
| 73 | $this->checkboxList($name, $allcats, $selected, $extras); |
| 74 | if($parent > 0 && wpdm_valueof($extras, 'hide_parent', ['validate' => 'int', 'default' => 0]) == 0) echo "</li></ul>"; |
| 75 | echo "</ul>"; |
| 76 | } |
| 77 | |
| 78 | private function checkboxList($name, $allcats, $selected = array(), $extras = array()) |
| 79 | { |
| 80 | foreach ($allcats as $cat_id => $cat) { |
| 81 | |
| 82 | $value = wpdm_valueof($extras, 'value') === 'slug' ? $cat['category']->slug : $cat_id; |
| 83 | $category = $cat['category']; |
| 84 | ?> |
| 85 | <li class="<?php echo wpdm_valueof($extras, 'liclass'); ?>"> |
| 86 | <label><input type="checkbox" <?php checked(1, in_array($value, $selected)); ?> |
| 87 | name="<?php echo $name; ?>[]" |
| 88 | class="<?php echo wpdm_valueof($extras, 'cbclass'); ?>" |
| 89 | value="<?php echo $value; ?>"> <?php echo $category->name; ?> </label> |
| 90 | <?php |
| 91 | if (count($cat['childs']) > 0) { |
| 92 | echo "<ul id='wpdmcats-childof-{$cat_id}'>"; |
| 93 | $this->checkboxList($name, $cat['childs'], $selected, $extras); |
| 94 | echo "</ul>"; |
| 95 | } |
| 96 | ?> |
| 97 | </li> |
| 98 | <?php |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function exploreChilds($term, &$allcats) |
| 103 | { |
| 104 | $child_ids = get_term_children($term->term_id, 'wpdmcategory'); |
| 105 | if (count($child_ids) > 0) { |
| 106 | foreach ($child_ids as $child_id) { |
| 107 | $term = get_term($child_id); |
| 108 | $allcats[$child_id] = array('category' => $term, 'access' => maybe_unserialize(get_term_meta($child_id, '__wpdm_access')), 'childs' => array()); |
| 109 | $this->exploreChilds($term, $allcats[$child_id]['childs']); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function getAllowedRoles($term_id) |
| 115 | { |
| 116 | $roles = maybe_unserialize(get_term_meta($term_id, '__wpdm_access', true)); |
| 117 | if (!is_array($roles)) { |
| 118 | $MetaData = get_option("__wpdmcategory"); |
| 119 | $MetaData = maybe_unserialize($MetaData); |
| 120 | |
| 121 | $roles = maybe_unserialize(get_term_meta($term_id, '__wpdm_access', true)); |
| 122 | |
| 123 | if (!is_array($roles)) |
| 124 | $roles = isset($MetaData[$term_id], $MetaData[$term_id]['access']) && is_array($MetaData[$term_id]['access']) ? $MetaData[$term_id]['access'] : array(); |
| 125 | |
| 126 | $roles = apply_filters("WPDM_Category_CategoryController_getAllowedRoles", $roles, $term_id); |
| 127 | } |
| 128 | foreach ($roles as $index => $role) { |
| 129 | if (!is_string($roles[$index])) unset($roles[$index]); |
| 130 | } |
| 131 | return $roles; |
| 132 | } |
| 133 | |
| 134 | function parentRoles($cid) |
| 135 | { |
| 136 | if (!$cid) return array(); |
| 137 | $roles = array(); |
| 138 | $parents = self::categoryParents($cid, 0); |
| 139 | $MetaData = get_option("__wpdmcategory"); |
| 140 | $MetaData = maybe_unserialize($MetaData); |
| 141 | foreach ($parents as $catid) { |
| 142 | $croles = maybe_unserialize(get_term_meta($catid, '__wpdm_access', true)); |
| 143 | if (!is_array($roles)) |
| 144 | $croles = isset($MetaData[$catid], $MetaData[$catid]['access']) && is_array($MetaData[$catid]['access']) ? $MetaData[$catid]['access'] : array(); |
| 145 | $roles += $croles; |
| 146 | } |
| 147 | return array_unique($roles); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | public static function icon($term_id) |
| 152 | { |
| 153 | $icon = get_term_meta($term_id, '__wpdm_icon', true); |
| 154 | if ($icon == '') { |
| 155 | $MetaData = get_option("__wpdmcategory"); |
| 156 | $MetaData = maybe_unserialize($MetaData); |
| 157 | $icon = get_term_meta($term_id, '__wpdm_icon', true); |
| 158 | if ($icon == '') |
| 159 | $icon = isset($MetaData[$term_id]['icon']) ? $MetaData[$term_id]['icon'] : ''; |
| 160 | } |
| 161 | return $icon; |
| 162 | } |
| 163 | |
| 164 | public static function categoryParents($cid, $offset = 1) |
| 165 | { |
| 166 | $CategoryBreadcrumb = array(); |
| 167 | if ($cid > 0) { |
| 168 | $cat = get_term($cid, 'wpdmcategory'); |
| 169 | $parent = $cat->parent; |
| 170 | $CategoryParents[] = $cat->term_id; |
| 171 | while ($parent > 0) { |
| 172 | $cat = get_term($parent, 'wpdmcategory'); |
| 173 | $CategoryParents[] = $cat->term_id; |
| 174 | $parent = $cat->parent; |
| 175 | } |
| 176 | if ($offset) |
| 177 | array_pop($CategoryBreadcrumb); |
| 178 | $CategoryParents = array_reverse($CategoryParents); |
| 179 | } |
| 180 | |
| 181 | return $CategoryParents; |
| 182 | |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Check if current user has access to the given term |
| 187 | * @param $term_id |
| 188 | * @return bool |
| 189 | */ |
| 190 | public static function userHasAccess($term_id) |
| 191 | { |
| 192 | global $current_user; |
| 193 | $roles = maybe_unserialize(get_term_meta($term_id, '__wpdm_access', true)); |
| 194 | $roles = is_array($roles) ? $roles : array(); |
| 195 | if(in_array('guest', $roles)) return true; |
| 196 | $user_roles = is_array($current_user->roles) ? $current_user->roles : array(); |
| 197 | $has_role = array_intersect($roles, $user_roles); |
| 198 | $users = maybe_unserialize(get_term_meta($term_id, '__wpdm_user_access', true)); |
| 199 | $users = is_array($users) ? $users : array(); |
| 200 | if (count($has_role) > 0 || in_array($current_user->user_login, $users)) return true; |
| 201 | if(count($roles) === 0 && count($users) === 0) return true; |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | public static function categoryBreadcrumb($cid, $offset = 1) |
| 206 | { |
| 207 | $CategoryBreadcrumb = array(); |
| 208 | if ($cid > 0) { |
| 209 | $cat = get_term($cid, 'wpdmcategory'); |
| 210 | $parent = $cat->parent; |
| 211 | $CategoryBreadcrumb[] = "<a href='#' class='folder' data-cat='{$cat->term_id}'>{$cat->name}</a>"; |
| 212 | while ($parent > 0) { |
| 213 | $cat = get_term($parent, 'wpdmcategory'); |
| 214 | $CategoryBreadcrumb[] = "<a href='#' class='folder' data-cat='{$cat->term_id}'>{$cat->name}</a>"; |
| 215 | $parent = $cat->parent; |
| 216 | } |
| 217 | if ($offset) |
| 218 | array_pop($CategoryBreadcrumb); |
| 219 | $CategoryBreadcrumb = array_reverse($CategoryBreadcrumb); |
| 220 | } |
| 221 | echo "<a href='#' class='folder' data-cat='0'>Home</a> <i class='fa fa-angle-right'></i> " . implode(" <i class='fa fa-angle-right'></i> ", $CategoryBreadcrumb); |
| 222 | |
| 223 | } |
| 224 | |
| 225 | function wpdmproTemplates($template){ |
| 226 | $_template = basename($template); |
| 227 | $style_global = get_option('__wpdm_cpage_style', 'basic'); |
| 228 | $style = get_term_meta(get_queried_object_id(), '__wpdm_style', true); |
| 229 | $style = in_array($style, ['basic', 'ltpl']) ? $style : $style_global; |
| 230 | if($style === 'ltpl' && (is_tax('wpdmcategory') || is_post_type_archive('wpdmpro'))){ |
| 231 | $template = Template::locate("taxonomy-wpdmcategory.php", __DIR__.'/views'); |
| 232 | } |
| 233 | /*if($_template !== 'single-wpdmpro.php' && is_singular('wpdmpro')){ |
| 234 | $template = Template::locate("single-wpdmpro.php", WPDM_TPL_FALLBACK, WPDM_TPL_FALLBACK); |
| 235 | }*/ |
| 236 | return $template; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | } |
| 242 | |
| 243 |