PluginProbe
User Access Manager / 2.1.3
User Access Manager v2.1.3
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.3, at src/Controller/Frontend/TermController.php

401 lines 10.4 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 protected $mainConfig;
43
44 /**
45 * @var Util
46 */
47 protected $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 protected $userHandler;
63
64 /**
65 * @var AccessHandler
66 */
67 protected $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 * Returns the post object hide config.
114 *
115 * @return array
116 */
117 private function getPostObjectHideConfig()
118 {
119 if ($this->postObjectHideConfig === null) {
120 $this->postObjectHideConfig = [];
121
122 foreach ($this->objectHandler->getPostTypes() as $postType) {
123 $this->postObjectHideConfig[$postType] = $this->mainConfig->hidePostType($postType);
124 }
125 }
126
127 return $this->postObjectHideConfig;
128 }
129
130 /**
131 * Returns all posts for the given term.
132 *
133 * @param string $termType
134 * @param int $termId
135 *
136 * @return array
137 */
138 private function getAllPostForTerm($termType, $termId)
139 {
140 $fullTerms = [$termId => $termType];
141 $termTreeMap = $this->objectMapHandler->getTermTreeMap();
142
143 if (isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType]) === true
144 && isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId]) === true
145 ) {
146 $fullTerms += $termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId];
147 }
148
149 $posts = [];
150 $termPostMap = $this->objectMapHandler->getTermPostMap();
151
152 foreach ($fullTerms as $fullTermId => $fullTermType) {
153 if (isset($termPostMap[$fullTermId]) === true) {
154 $posts += $termPostMap[$fullTermId];
155 }
156 }
157
158 return $posts;
159 }
160
161 /**
162 * Returns the post count for the term.
163 *
164 * @param string $termType
165 * @param int $termId
166 *
167 * @return int
168 */
169 private function getVisibleElementsCount($termType, $termId)
170 {
171 $key = $termType.'|'.$termId;
172
173 if (isset($this->visibleElementsCount[$key]) === false) {
174 $count = 0;
175 $posts = $this->getAllPostForTerm($termType, $termId);
176 $postTypeHiddenMap = $this->getPostObjectHideConfig();
177
178 foreach ($posts as $postId => $postType) {
179 if (isset($postTypeHiddenMap[$postType]) && $postTypeHiddenMap[$postType] === false
180 || $this->accessHandler->checkObjectAccess(
181 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
182 $postId
183 ) === true
184 ) {
185 $count++;
186 }
187 }
188
189 $this->visibleElementsCount[$key] = $count;
190 }
191
192 return $this->visibleElementsCount[$key];
193 }
194
195 /**
196 * Checks if the category is empty.
197 *
198 * @param $term
199 *
200 * @return bool
201 */
202 private function isCategoryEmpty($term)
203 {
204 return $term->count <= 0
205 && $this->wordpressConfig->atAdminPanel() === false
206 && $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true;
207 }
208
209 /**
210 * Updates the term parent.
211 *
212 * @param \WP_Term $term
213 *
214 * @return \WP_Term
215 */
216 private function updateTermParent(\WP_Term $term)
217 {
218 $currentTerm = $term;
219
220 while ($currentTerm->parent != 0) {
221 $currentTerm = $this->objectHandler->getTerm($currentTerm->parent);
222
223 if ($currentTerm === false) {
224 break;
225 }
226
227 $access = $this->accessHandler->checkObjectAccess(
228 $currentTerm->taxonomy,
229 $currentTerm->term_id
230 );
231
232 if ($access === true) {
233 $term->parent = $currentTerm->term_id;
234 break;
235 }
236 }
237
238 return $term;
239 }
240
241 /**
242 * Modifies the content of the term by the given settings.
243 *
244 * @param mixed $term
245 * @param bool $isEmpty
246 *
247 * @return null|\WP_Term
248 */
249 private function processTerm($term, &$isEmpty = null)
250 {
251 $isEmpty = false;
252
253 if (($term instanceof \WP_Term) === false
254 || $this->objectHandler->isValidObjectType($term->taxonomy) === false
255 ) {
256 return $term;
257 }
258
259 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
260 return null;
261 }
262
263 $term->name .= $this->adminOutput($term->taxonomy, $term->term_id, $term->name);
264 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
265 $isEmpty = $this->isCategoryEmpty($term);
266
267 if ($this->mainConfig->lockRecursive() === false) {
268 $term = $this->updateTermParent($term);
269 }
270
271 return $term;
272 }
273
274 /**
275 * The function for the get_term filter.
276 *
277 * @param \WP_Term $term
278 *
279 * @return null|object
280 */
281 public function showTerm($term)
282 {
283 return $this->processTerm($term);
284 }
285
286 /**
287 * The function for the get_terms filter.
288 *
289 * @param array $terms The terms.
290 *
291 * @return array
292 */
293 public function showTerms($terms = [])
294 {
295 foreach ($terms as $key => $term) {
296 if (is_numeric($term) === true) {
297 if ((int)$term === 0) {
298 unset($terms[$key]);
299 continue;
300 }
301
302 $term = $this->objectHandler->getTerm($term);
303 }
304
305 $term = $this->processTerm($term, $isEmpty);
306
307 if ($term === null || $isEmpty === true) {
308 unset($terms[$key]);
309 }
310 }
311
312 return $terms;
313 }
314
315 /**
316 * Processes a post menu item.
317 *
318 * @param \stdClass $item
319 *
320 * @return bool
321 */
322 private function processPostMenuItem(&$item)
323 {
324 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
325 if ($this->mainConfig->hidePostType($item->object) === true
326 || $this->wordpressConfig->atAdminPanel() === true
327 ) {
328 return false;
329 }
330
331 if ($this->mainConfig->hidePostTypeTitle($item->object) === true) {
332 $item->title = $this->mainConfig->getPostTypeTitle($item->object);
333 }
334 }
335
336 return true;
337 }
338
339 /**
340 * Processes a term menu item.
341 *
342 * @param \stdClass $item
343 *
344 * @return bool
345 */
346 private function processTermMenuItem(&$item)
347 {
348 $rawTerm = $this->objectHandler->getTerm($item->object_id);
349 $term = $this->processTerm($rawTerm, $isEmpty);
350
351 return !($term === false || $term === null || $isEmpty === true);
352 }
353
354 /**
355 * The function for the wp_get_nav_menu_items filter.
356 *
357 * @param array $items The menu item.
358 *
359 * @return array
360 */
361 public function showCustomMenu($items)
362 {
363 $showItems = [];
364
365 foreach ($items as $key => $item) {
366 $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
367
368 if ($this->objectHandler->isPostType($item->object) === true) {
369 if ($this->processPostMenuItem($item) === false) {
370 continue;
371 }
372 } elseif ($this->objectHandler->isTaxonomy($item->object) === true
373 && $this->processTermMenuItem($item) === false
374 ) {
375 continue;
376 }
377
378 $showItems[$key] = $item;
379 }
380
381 return $showItems;
382 }
383
384 /**
385 * Sets the excluded terms as argument.
386 *
387 * @param array $arguments
388 *
389 * @return array
390 */
391 public function getTermArguments(array $arguments)
392 {
393 $exclude = (isset($arguments['exclude']) === true) ?
394 $this->wordpress->parseIdList($arguments['exclude']) : [];
395 $arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms());
396 $arguments['exclude'] = array_unique($arguments['exclude']);
397
398 return $arguments;
399 }
400 }
401