PluginProbe
User Access Manager / 2.1.10
User Access Manager v2.1.10
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Controller / Frontend / TermController.php

TermController.php in User Access Manager 2.1.10, at src/Controller/Frontend/TermController.php

372 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * FrontendTermController.php
4 *
5 * The FrontendTermController class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @copyright 2008-2017 Alexander Schneider
11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 * @version SVN: $id$
13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 */
15 namespace UserAccessManager\Controller\Frontend;
16
17 use UserAccessManager\Access\AccessHandler;
18 use UserAccessManager\Config\MainConfig;
19 use UserAccessManager\Config\WordpressConfig;
20 use UserAccessManager\Object\ObjectHandler;
21 use UserAccessManager\Object\ObjectMapHandler;
22 use UserAccessManager\User\UserHandler;
23 use UserAccessManager\UserGroup\UserGroupHandler;
24 use UserAccessManager\Util\Util;
25 use UserAccessManager\Wrapper\Php;
26 use UserAccessManager\Wrapper\Wordpress;
27
28 /**
29 * Class FrontendTermController
30 *
31 * @package UserAccessManager\Controller
32 */
33 class TermController extends ContentController
34 {
35 /**
36 * @var array
37 */
38 private $visibleElementsCount = [];
39
40 /**
41 * @var null|array
42 */
43 private $postObjectHideConfig = null;
44
45 /**
46 * TermController constructor.
47 *
48 * @param Php $php
49 * @param Wordpress $wordpress
50 * @param WordpressConfig $wordpressConfig
51 * @param MainConfig $mainConfig
52 * @param Util $util
53 * @param ObjectHandler $objectHandler
54 * @param ObjectMapHandler $objectMapHandler
55 * @param UserHandler $userHandler
56 * @param UserGroupHandler $userGroupHandler
57 * @param AccessHandler $accessHandler
58 */
59 public function __construct(
60 Php $php,
61 Wordpress $wordpress,
62 WordpressConfig $wordpressConfig,
63 MainConfig $mainConfig,
64 Util $util,
65 ObjectHandler $objectHandler,
66 ObjectMapHandler $objectMapHandler,
67 UserHandler $userHandler,
68 UserGroupHandler $userGroupHandler,
69 AccessHandler $accessHandler
70 ) {
71 parent::__construct(
72 $php,
73 $wordpress,
74 $wordpressConfig,
75 $mainConfig,
76 $util,
77 $objectHandler,
78 $userHandler,
79 $userGroupHandler,
80 $accessHandler
81 );
82 $this->objectMapHandler = $objectMapHandler;
83 }
84
85 /**
86 * Returns the post object hide config.
87 *
88 * @return array
89 */
90 private function getPostObjectHideConfig()
91 {
92 if ($this->postObjectHideConfig === null) {
93 $this->postObjectHideConfig = [];
94
95 foreach ($this->objectHandler->getPostTypes() as $postType) {
96 $this->postObjectHideConfig[$postType] = $this->mainConfig->hidePostType($postType);
97 }
98 }
99
100 return $this->postObjectHideConfig;
101 }
102
103 /**
104 * Returns all posts for the given term.
105 *
106 * @param string $termType
107 * @param int $termId
108 *
109 * @return array
110 */
111 private function getAllPostForTerm($termType, $termId)
112 {
113 $fullTerms = [$termId => $termType];
114 $termTreeMap = $this->objectMapHandler->getTermTreeMap();
115
116 if (isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType]) === true
117 && isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId]) === true
118 ) {
119 $fullTerms += $termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId];
120 }
121
122 $posts = [];
123 $termPostMap = $this->objectMapHandler->getTermPostMap();
124
125 foreach ($fullTerms as $fullTermId => $fullTermType) {
126 if (isset($termPostMap[$fullTermId]) === true) {
127 $posts += $termPostMap[$fullTermId];
128 }
129 }
130
131 return $posts;
132 }
133
134 /**
135 * Returns the post count for the term.
136 *
137 * @param string $termType
138 * @param int $termId
139 *
140 * @return int
141 */
142 private function getVisibleElementsCount($termType, $termId)
143 {
144 $key = $termType.'|'.$termId;
145
146 if (isset($this->visibleElementsCount[$key]) === false) {
147 $count = 0;
148 $posts = $this->getAllPostForTerm($termType, $termId);
149 $postTypeHiddenMap = $this->getPostObjectHideConfig();
150
151 foreach ($posts as $postId => $postType) {
152 if (isset($postTypeHiddenMap[$postType]) && $postTypeHiddenMap[$postType] === false
153 || $this->accessHandler->checkObjectAccess(
154 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
155 $postId
156 ) === true
157 ) {
158 $count++;
159 }
160 }
161
162 $this->visibleElementsCount[$key] = $count;
163 }
164
165 return $this->visibleElementsCount[$key];
166 }
167
168 /**
169 * Checks if the category is empty.
170 *
171 * @param $term
172 *
173 * @return bool
174 */
175 private function isCategoryEmpty($term)
176 {
177 return $term->count <= 0
178 && $this->wordpressConfig->atAdminPanel() === false
179 && $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true;
180 }
181
182 /**
183 * Updates the term parent.
184 *
185 * @param \WP_Term $term
186 *
187 * @return \WP_Term
188 */
189 private function updateTermParent(\WP_Term $term)
190 {
191 $currentTerm = $term;
192
193 while ($currentTerm->parent != 0) {
194 $currentTerm = $this->objectHandler->getTerm($currentTerm->parent);
195
196 if ($currentTerm === false) {
197 break;
198 }
199
200 $access = $this->accessHandler->checkObjectAccess(
201 $currentTerm->taxonomy,
202 $currentTerm->term_id
203 );
204
205 if ($access === true) {
206 $term->parent = $currentTerm->term_id;
207 break;
208 }
209 }
210
211 return $term;
212 }
213
214 /**
215 * Modifies the content of the term by the given settings.
216 *
217 * @param mixed $term
218 * @param bool $isEmpty
219 *
220 * @return null|\WP_Term
221 */
222 private function processTerm($term, &$isEmpty = null)
223 {
224 $isEmpty = false;
225
226 if (($term instanceof \WP_Term) === false
227 || $this->objectHandler->isValidObjectType($term->taxonomy) === false
228 ) {
229 return $term;
230 }
231
232 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
233 return null;
234 }
235
236 $term->name .= $this->adminOutput($term->taxonomy, $term->term_id, $term->name);
237 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
238 $isEmpty = $this->isCategoryEmpty($term);
239
240 if ($this->mainConfig->lockRecursive() === false) {
241 $term = $this->updateTermParent($term);
242 }
243
244 return $term;
245 }
246
247 /**
248 * The function for the get_term filter.
249 *
250 * @param \WP_Term $term
251 *
252 * @return null|object
253 */
254 public function showTerm($term)
255 {
256 return $this->processTerm($term);
257 }
258
259 /**
260 * The function for the get_terms filter.
261 *
262 * @param array $terms The terms.
263 *
264 * @return array
265 */
266 public function showTerms($terms = [])
267 {
268 foreach ($terms as $key => $term) {
269 if (is_numeric($term) === true) {
270 if ((int)$term === 0) {
271 unset($terms[$key]);
272 continue;
273 }
274
275 $term = $this->objectHandler->getTerm($term);
276 }
277
278 $term = $this->processTerm($term, $isEmpty);
279
280 if ($term === null || $isEmpty === true) {
281 unset($terms[$key]);
282 }
283 }
284
285 return $terms;
286 }
287
288 /**
289 * Processes a post menu item.
290 *
291 * @param \stdClass $item
292 *
293 * @return bool
294 */
295 private function processPostMenuItem(&$item)
296 {
297 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
298 if ($this->removePostFromList($item->object) === true) {
299 return false;
300 }
301
302 if ($this->mainConfig->hidePostTypeTitle($item->object) === true) {
303 $item->title = $this->mainConfig->getPostTypeTitle($item->object);
304 }
305 }
306
307 return true;
308 }
309
310 /**
311 * Processes a term menu item.
312 *
313 * @param \stdClass $item
314 *
315 * @return bool
316 */
317 private function processTermMenuItem(&$item)
318 {
319 $rawTerm = $this->objectHandler->getTerm($item->object_id);
320 $term = $this->processTerm($rawTerm, $isEmpty);
321
322 return !($term === false || $term === null || $isEmpty === true);
323 }
324
325 /**
326 * The function for the wp_get_nav_menu_items filter.
327 *
328 * @param array $items The menu item.
329 *
330 * @return array
331 */
332 public function showCustomMenu($items)
333 {
334 $showItems = [];
335
336 foreach ($items as $key => $item) {
337 $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
338
339 if ($this->objectHandler->isPostType($item->object) === true) {
340 if ($this->processPostMenuItem($item) === false) {
341 continue;
342 }
343 } elseif ($this->objectHandler->isTaxonomy($item->object) === true
344 && $this->processTermMenuItem($item) === false
345 ) {
346 continue;
347 }
348
349 $showItems[$key] = $item;
350 }
351
352 return $showItems;
353 }
354
355 /**
356 * Sets the excluded terms as argument.
357 *
358 * @param array $arguments
359 *
360 * @return array
361 */
362 public function getTermArguments(array $arguments)
363 {
364 $exclude = (isset($arguments['exclude']) === true) ?
365 $this->wordpress->parseIdList($arguments['exclude']) : [];
366 $arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms());
367 $arguments['exclude'] = array_unique($arguments['exclude']);
368
369 return $arguments;
370 }
371 }
372