PluginProbe ʕ •ᴥ•ʔ
Download Manager / trunk
Download Manager vtrunk
3.3.62 3.3.61 3.3.60 3.3.59 3.3.58 3.3.57 3.3.56 trunk 2.1.3 2.3.0 2.5.96 2.5.97 2.6.2 2.6.96 2.8.3 2.9.99 3.0.4 3.1.05 3.1.07 3.1.08 3.1.11 3.1.12 3.1.14 3.1.17 3.1.18 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.2.04 3.2.13 3.2.14 3.2.16 3.2.18 3.2.19 3.2.21 3.2.22 3.2.23 3.2.24 3.2.25 3.2.27 3.2.28 3.2.29 3.2.30 3.2.31 3.2.32 3.2.33 3.2.34 3.2.35 3.2.37 3.2.38 3.2.39 3.2.40 3.2.41 3.2.42 3.2.43 3.2.44 3.2.45 3.2.46 3.2.47 3.2.48 3.2.49 3.2.50 3.2.51 3.2.52 3.2.53 3.2.54 3.2.55 3.2.56 3.2.57 3.2.58 3.2.59 3.2.60 3.2.61 3.2.63 3.2.64 3.2.65 3.2.66 3.2.67 3.2.68 3.2.69 3.2.70 3.2.71 3.2.72 3.2.73 3.2.74 3.2.75 3.2.76 3.2.77 3.2.78 3.2.79 3.2.80 3.2.81 3.2.82 3.2.83 3.2.84 3.2.85 3.2.86 3.2.87 3.2.88 3.2.89 3.2.90 3.2.91 3.2.92 3.2.93 3.2.94 3.2.95 3.2.96 3.2.97 3.2.98 3.2.99 3.3.00 3.3.01 3.3.02 3.3.03 3.3.04 3.3.05 3.3.06 3.3.07 3.3.08 3.3.09 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.15 3.3.16 3.3.17 3.3.18 3.3.19 3.3.20 3.3.21 3.3.22 3.3.23 3.3.24 3.3.25 3.3.26 3.3.27 3.3.28 3.3.29 3.3.30 3.3.31 3.3.32 3.3.33 3.3.34 3.3.35 3.3.36 3.3.37 3.3.38 3.3.39 3.3.40 3.3.41 3.3.42 3.3.43 3.3.44 3.3.45 3.3.46 3.3.47 3.3.48 3.3.49 3.3.50 3.3.51 3.3.52 3.3.53 3.3.54 3.3.55
download-manager / src / Category / CategoryController.php
download-manager / src / Category Last commit date
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>&nbsp; <i class='fa fa-angle-right'></i> &nbsp;" . implode("&nbsp; <i class='fa fa-angle-right'></i> &nbsp;", $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