PluginProbe
User Access Manager / 2.1.5
User Access Manager v2.1.5
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.5, at src/Controller/Frontend/TermController.php

441 lines 11.0 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\Controller\Controller;
21 use UserAccessManager\Object\ObjectHandler;
22 use UserAccessManager\Object\ObjectMapHandler;
23 use UserAccessManager\User\UserHandler;
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 Controller
34 {
35 use AdminOutputControllerTrait;
36
37 const POST_COUNTS_CACHE_KEY = 'WpPostCounts';
38
39 /**
40 * @var MainConfig
41 */
42 private $mainConfig;
43
44 /**
45 * @var Util
46 */
47 private $util;
48
49 /**
50 * @var ObjectHandler
51 */
52 private $objectHandler;
53
54 /**
55 * @var ObjectMapHandler
56 */
57 private $objectMapHandler;
58
59 /**
60 * @var UserHandler
61 */
62 private $userHandler;
63
64 /**
65 * @var AccessHandler
66 */
67 private $accessHandler;
68
69 /**
70 * @var array
71 */
72 private $visibleElementsCount = [];
73
74 /**
75 * @var null|array
76 */
77 private $postObjectHideConfig = null;
78
79 /**
80 * TermController constructor.
81 *
82 * @param Php $php
83 * @param Wordpress $wordpress
84 * @param WordpressConfig $wordpressConfig
85 * @param MainConfig $mainConfig
86 * @param Util $util
87 * @param ObjectHandler $objectHandler
88 * @param ObjectMapHandler $objectMapHandler
89 * @param UserHandler $userHandler
90 * @param AccessHandler $accessHandler
91 */
92 public function __construct(
93 Php $php,
94 Wordpress $wordpress,
95 WordpressConfig $wordpressConfig,
96 MainConfig $mainConfig,
97 Util $util,
98 ObjectHandler $objectHandler,
99 ObjectMapHandler $objectMapHandler,
100 UserHandler $userHandler,
101 AccessHandler $accessHandler
102 ) {
103 parent::__construct($php, $wordpress, $wordpressConfig);
104 $this->mainConfig = $mainConfig;
105 $this->util = $util;
106 $this->objectHandler = $objectHandler;
107 $this->objectMapHandler = $objectMapHandler;
108 $this->userHandler = $userHandler;
109 $this->accessHandler = $accessHandler;
110 }
111
112 /**
113 * @return MainConfig
114 */
115 protected function getMainConfig()
116 {
117 return $this->mainConfig;
118 }
119
120 /**
121 * @return Util
122 */
123 protected function getUtil()
124 {
125 return $this->util;
126 }
127
128 /**
129 * @return Wordpress
130 */
131 protected function getWordpress()
132 {
133 return $this->wordpress;
134 }
135
136 /**
137 * @return UserHandler
138 */
139 protected function getUserHandler()
140 {
141 return $this->userHandler;
142 }
143
144 /**
145 * @return AccessHandler
146 */
147 protected function getAccessHandler()
148 {
149 return $this->accessHandler;
150 }
151
152 /**
153 * Returns the post object hide config.
154 *
155 * @return array
156 */
157 private function getPostObjectHideConfig()
158 {
159 if ($this->postObjectHideConfig === null) {
160 $this->postObjectHideConfig = [];
161
162 foreach ($this->objectHandler->getPostTypes() as $postType) {
163 $this->postObjectHideConfig[$postType] = $this->mainConfig->hidePostType($postType);
164 }
165 }
166
167 return $this->postObjectHideConfig;
168 }
169
170 /**
171 * Returns all posts for the given term.
172 *
173 * @param string $termType
174 * @param int $termId
175 *
176 * @return array
177 */
178 private function getAllPostForTerm($termType, $termId)
179 {
180 $fullTerms = [$termId => $termType];
181 $termTreeMap = $this->objectMapHandler->getTermTreeMap();
182
183 if (isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType]) === true
184 && isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId]) === true
185 ) {
186 $fullTerms += $termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId];
187 }
188
189 $posts = [];
190 $termPostMap = $this->objectMapHandler->getTermPostMap();
191
192 foreach ($fullTerms as $fullTermId => $fullTermType) {
193 if (isset($termPostMap[$fullTermId]) === true) {
194 $posts += $termPostMap[$fullTermId];
195 }
196 }
197
198 return $posts;
199 }
200
201 /**
202 * Returns the post count for the term.
203 *
204 * @param string $termType
205 * @param int $termId
206 *
207 * @return int
208 */
209 private function getVisibleElementsCount($termType, $termId)
210 {
211 $key = $termType.'|'.$termId;
212
213 if (isset($this->visibleElementsCount[$key]) === false) {
214 $count = 0;
215 $posts = $this->getAllPostForTerm($termType, $termId);
216 $postTypeHiddenMap = $this->getPostObjectHideConfig();
217
218 foreach ($posts as $postId => $postType) {
219 if (isset($postTypeHiddenMap[$postType]) && $postTypeHiddenMap[$postType] === false
220 || $this->accessHandler->checkObjectAccess(
221 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
222 $postId
223 ) === true
224 ) {
225 $count++;
226 }
227 }
228
229 $this->visibleElementsCount[$key] = $count;
230 }
231
232 return $this->visibleElementsCount[$key];
233 }
234
235 /**
236 * Checks if the category is empty.
237 *
238 * @param $term
239 *
240 * @return bool
241 */
242 private function isCategoryEmpty($term)
243 {
244 return $term->count <= 0
245 && $this->wordpressConfig->atAdminPanel() === false
246 && $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true;
247 }
248
249 /**
250 * Updates the term parent.
251 *
252 * @param \WP_Term $term
253 *
254 * @return \WP_Term
255 */
256 private function updateTermParent(\WP_Term $term)
257 {
258 $currentTerm = $term;
259
260 while ($currentTerm->parent != 0) {
261 $currentTerm = $this->objectHandler->getTerm($currentTerm->parent);
262
263 if ($currentTerm === false) {
264 break;
265 }
266
267 $access = $this->accessHandler->checkObjectAccess(
268 $currentTerm->taxonomy,
269 $currentTerm->term_id
270 );
271
272 if ($access === true) {
273 $term->parent = $currentTerm->term_id;
274 break;
275 }
276 }
277
278 return $term;
279 }
280
281 /**
282 * Modifies the content of the term by the given settings.
283 *
284 * @param mixed $term
285 * @param bool $isEmpty
286 *
287 * @return null|\WP_Term
288 */
289 private function processTerm($term, &$isEmpty = null)
290 {
291 $isEmpty = false;
292
293 if (($term instanceof \WP_Term) === false
294 || $this->objectHandler->isValidObjectType($term->taxonomy) === false
295 ) {
296 return $term;
297 }
298
299 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
300 return null;
301 }
302
303 $term->name .= $this->adminOutput($term->taxonomy, $term->term_id, $term->name);
304 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
305 $isEmpty = $this->isCategoryEmpty($term);
306
307 if ($this->mainConfig->lockRecursive() === false) {
308 $term = $this->updateTermParent($term);
309 }
310
311 return $term;
312 }
313
314 /**
315 * The function for the get_term filter.
316 *
317 * @param \WP_Term $term
318 *
319 * @return null|object
320 */
321 public function showTerm($term)
322 {
323 return $this->processTerm($term);
324 }
325
326 /**
327 * The function for the get_terms filter.
328 *
329 * @param array $terms The terms.
330 *
331 * @return array
332 */
333 public function showTerms($terms = [])
334 {
335 foreach ($terms as $key => $term) {
336 if (is_numeric($term) === true) {
337 if ((int)$term === 0) {
338 unset($terms[$key]);
339 continue;
340 }
341
342 $term = $this->objectHandler->getTerm($term);
343 }
344
345 $term = $this->processTerm($term, $isEmpty);
346
347 if ($term === null || $isEmpty === true) {
348 unset($terms[$key]);
349 }
350 }
351
352 return $terms;
353 }
354
355 /**
356 * Processes a post menu item.
357 *
358 * @param \stdClass $item
359 *
360 * @return bool
361 */
362 private function processPostMenuItem(&$item)
363 {
364 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
365 if ($this->mainConfig->hidePostType($item->object) === true
366 || $this->wordpressConfig->atAdminPanel() === true
367 ) {
368 return false;
369 }
370
371 if ($this->mainConfig->hidePostTypeTitle($item->object) === true) {
372 $item->title = $this->mainConfig->getPostTypeTitle($item->object);
373 }
374 }
375
376 return true;
377 }
378
379 /**
380 * Processes a term menu item.
381 *
382 * @param \stdClass $item
383 *
384 * @return bool
385 */
386 private function processTermMenuItem(&$item)
387 {
388 $rawTerm = $this->objectHandler->getTerm($item->object_id);
389 $term = $this->processTerm($rawTerm, $isEmpty);
390
391 return !($term === false || $term === null || $isEmpty === true);
392 }
393
394 /**
395 * The function for the wp_get_nav_menu_items filter.
396 *
397 * @param array $items The menu item.
398 *
399 * @return array
400 */
401 public function showCustomMenu($items)
402 {
403 $showItems = [];
404
405 foreach ($items as $key => $item) {
406 $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
407
408 if ($this->objectHandler->isPostType($item->object) === true) {
409 if ($this->processPostMenuItem($item) === false) {
410 continue;
411 }
412 } elseif ($this->objectHandler->isTaxonomy($item->object) === true
413 && $this->processTermMenuItem($item) === false
414 ) {
415 continue;
416 }
417
418 $showItems[$key] = $item;
419 }
420
421 return $showItems;
422 }
423
424 /**
425 * Sets the excluded terms as argument.
426 *
427 * @param array $arguments
428 *
429 * @return array
430 */
431 public function getTermArguments(array $arguments)
432 {
433 $exclude = (isset($arguments['exclude']) === true) ?
434 $this->wordpress->parseIdList($arguments['exclude']) : [];
435 $arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms());
436 $arguments['exclude'] = array_unique($arguments['exclude']);
437
438 return $arguments;
439 }
440 }
441