PluginProbe
User Access Manager / 2.1.4
User Access Manager v2.1.4
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.4, at src/Controller/Frontend/TermController.php

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