PluginProbe
User Access Manager / 2.1.1
User Access Manager v2.1.1
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.1, at src/Controller/Frontend/TermController.php

399 lines 10.3 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 return $term;
255 }
256
257 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
258 return null;
259 }
260
261 $term->name .= $this->adminOutput($term->taxonomy, $term->term_id, $term->name);
262 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
263 $isEmpty = $this->isCategoryEmpty($term);
264
265 if ($this->mainConfig->lockRecursive() === false) {
266 $term = $this->updateTermParent($term);
267 }
268
269 return $term;
270 }
271
272 /**
273 * The function for the get_term filter.
274 *
275 * @param \WP_Term $term
276 *
277 * @return null|object
278 */
279 public function showTerm($term)
280 {
281 return $this->processTerm($term);
282 }
283
284 /**
285 * The function for the get_terms filter.
286 *
287 * @param array $terms The terms.
288 *
289 * @return array
290 */
291 public function showTerms($terms = [])
292 {
293 foreach ($terms as $key => $term) {
294 if (is_numeric($term) === true) {
295 if ((int)$term === 0) {
296 unset($terms[$key]);
297 continue;
298 }
299
300 $term = $this->objectHandler->getTerm($term);
301 }
302
303 $term = $this->processTerm($term, $isEmpty);
304
305 if ($term === null || $isEmpty === true) {
306 unset($terms[$key]);
307 }
308 }
309
310 return $terms;
311 }
312
313 /**
314 * Processes a post menu item.
315 *
316 * @param \stdClass $item
317 *
318 * @return bool
319 */
320 private function processPostMenuItem(&$item)
321 {
322 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
323 if ($this->mainConfig->hidePostType($item->object) === true
324 || $this->wordpressConfig->atAdminPanel() === true
325 ) {
326 return false;
327 }
328
329 if ($this->mainConfig->hidePostTypeTitle($item->object) === true) {
330 $item->title = $this->mainConfig->getPostTypeTitle($item->object);
331 }
332 }
333
334 return true;
335 }
336
337 /**
338 * Processes a term menu item.
339 *
340 * @param \stdClass $item
341 *
342 * @return bool
343 */
344 private function processTermMenuItem(&$item)
345 {
346 $rawTerm = $this->objectHandler->getTerm($item->object_id);
347 $term = $this->processTerm($rawTerm, $isEmpty);
348
349 return !($term === false || $term === null || $isEmpty === true);
350 }
351
352 /**
353 * The function for the wp_get_nav_menu_items filter.
354 *
355 * @param array $items The menu item.
356 *
357 * @return array
358 */
359 public function showCustomMenu($items)
360 {
361 $showItems = [];
362
363 foreach ($items as $key => $item) {
364 $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
365
366 if ($this->objectHandler->isPostType($item->object) === true) {
367 if ($this->processPostMenuItem($item) === false) {
368 continue;
369 }
370 } elseif ($this->objectHandler->isTaxonomy($item->object) === true
371 && $this->processTermMenuItem($item) === false
372 ) {
373 continue;
374 }
375
376 $showItems[$key] = $item;
377 }
378
379 return $showItems;
380 }
381
382 /**
383 * Sets the excluded terms as argument.
384 *
385 * @param array $arguments
386 *
387 * @return array
388 */
389 public function getTermArguments(array $arguments)
390 {
391 $exclude = (isset($arguments['exclude']) === true) ?
392 $this->wordpress->parseIdList($arguments['exclude']) : [];
393 $arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms());
394 $arguments['exclude'] = array_unique($arguments['exclude']);
395
396 return $arguments;
397 }
398 }
399