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

366 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
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Controller\Frontend;
19
20 use Exception;
21 use stdClass;
22 use UserAccessManager\Access\AccessHandler;
23 use UserAccessManager\Config\MainConfig;
24 use UserAccessManager\Config\WordpressConfig;
25 use UserAccessManager\Object\ObjectHandler;
26 use UserAccessManager\Object\ObjectMapHandler;
27 use UserAccessManager\User\UserHandler;
28 use UserAccessManager\UserGroup\UserGroupHandler;
29 use UserAccessManager\UserGroup\UserGroupTypeException;
30 use UserAccessManager\Util\Util;
31 use UserAccessManager\Wrapper\Php;
32 use UserAccessManager\Wrapper\Wordpress;
33 use WP_Post;
34 use WP_Term;
35
36 /**
37 * Class FrontendTermController
38 *
39 * @package UserAccessManager\Controller
40 */
41 class TermController extends ContentController
42 {
43 /**
44 * @var array
45 */
46 private $visibleElementsCount = [];
47
48 /**
49 * @var null|array
50 */
51 private $postObjectHideConfig = null;
52
53 /**
54 * TermController constructor.
55 * @param Php $php
56 * @param Wordpress $wordpress
57 * @param WordpressConfig $wordpressConfig
58 * @param MainConfig $mainConfig
59 * @param Util $util
60 * @param ObjectHandler $objectHandler
61 * @param ObjectMapHandler $objectMapHandler
62 * @param UserHandler $userHandler
63 * @param UserGroupHandler $userGroupHandler
64 * @param AccessHandler $accessHandler
65 */
66 public function __construct(
67 Php $php,
68 Wordpress $wordpress,
69 WordpressConfig $wordpressConfig,
70 MainConfig $mainConfig,
71 Util $util,
72 ObjectHandler $objectHandler,
73 ObjectMapHandler $objectMapHandler,
74 UserHandler $userHandler,
75 UserGroupHandler $userGroupHandler,
76 AccessHandler $accessHandler
77 ) {
78 parent::__construct(
79 $php,
80 $wordpress,
81 $wordpressConfig,
82 $mainConfig,
83 $util,
84 $objectHandler,
85 $userHandler,
86 $userGroupHandler,
87 $accessHandler
88 );
89 $this->objectMapHandler = $objectMapHandler;
90 }
91
92 /**
93 * Returns the post object hide config.
94 * @return array
95 */
96 private function getPostObjectHideConfig(): ?array
97 {
98 if ($this->postObjectHideConfig === null) {
99 $this->postObjectHideConfig = [];
100
101 foreach ($this->objectHandler->getPostTypes() as $postType) {
102 $this->postObjectHideConfig[$postType] = $this->mainConfig->hidePostType($postType);
103 }
104 }
105
106 return $this->postObjectHideConfig;
107 }
108
109 /**
110 * Returns all posts for the given term.
111 * @param string $termType
112 * @param int $termId
113 * @return array
114 */
115 private function getAllPostForTerm(string $termType, int $termId): array
116 {
117 $fullTerms = [$termId => $termType];
118 $termTreeMap = $this->objectMapHandler->getTermTreeMap();
119
120 if (isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType]) === true
121 && isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId]) === true
122 ) {
123 $fullTerms += $termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId];
124 }
125
126 $posts = [];
127 $termPostMap = $this->objectMapHandler->getTermPostMap();
128
129 foreach ($fullTerms as $fullTermId => $fullTermType) {
130 if (isset($termPostMap[$fullTermId]) === true) {
131 $posts += $termPostMap[$fullTermId];
132 }
133 }
134
135 return $posts;
136 }
137
138 /**
139 * Returns the post count for the term.
140 * @param string $termType
141 * @param int $termId
142 * @return int
143 * @throws UserGroupTypeException
144 */
145 private function getVisibleElementsCount(string $termType, int $termId): int
146 {
147 $key = $termType . '|' . $termId;
148
149 if (isset($this->visibleElementsCount[$key]) === false) {
150 $count = 0;
151 $posts = $this->getAllPostForTerm($termType, $termId);
152 $postTypeHiddenMap = $this->getPostObjectHideConfig();
153
154 foreach ($posts as $postId => $postType) {
155 if (isset($postTypeHiddenMap[$postType]) && $postTypeHiddenMap[$postType] === false
156 || $this->accessHandler->checkObjectAccess(
157 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
158 $postId
159 ) === true
160 ) {
161 $count++;
162 }
163 }
164
165 $this->visibleElementsCount[$key] = $count;
166 }
167
168 return $this->visibleElementsCount[$key];
169 }
170
171 /**
172 * Checks if the category is empty.
173 * @param $term
174 * @return bool
175 */
176 private function isCategoryEmpty($term): bool
177 {
178 return $term->count <= 0
179 && $this->wordpressConfig->atAdminPanel() === false
180 && $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true;
181 }
182
183 /**
184 * Updates the term parent.
185 * @param WP_Term $term
186 * @return WP_Term
187 * @throws UserGroupTypeException
188 */
189 private function updateTermParent(WP_Term $term): WP_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 * @param stdClass|WP_Term $term
217 * @param bool $isEmpty
218 * @return null|stdClass|WP_Term
219 * @throws UserGroupTypeException
220 * @throws Exception
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 * @param stdClass|WP_Term $term
250 * @return null|object
251 * @throws UserGroupTypeException
252 */
253 public function showTerm($term)
254 {
255 return $this->processTerm($term);
256 }
257
258 /**
259 * The function for the get_terms filter.
260 * @param array $terms The terms.
261 * @return array
262 * @throws UserGroupTypeException
263 */
264 public function showTerms($terms = []): array
265 {
266 foreach ($terms as $key => $term) {
267 if (is_numeric($term) === true) {
268 if ((int) $term === 0) {
269 unset($terms[$key]);
270 continue;
271 }
272
273 $term = $this->objectHandler->getTerm($term);
274 }
275
276 $term = $this->processTerm($term, $isEmpty);
277
278 if ($term === null || $isEmpty === true) {
279 unset($terms[$key]);
280 }
281 }
282
283 return $terms;
284 }
285
286 /**
287 * Processes a post menu item.
288 * @param object $item
289 * @return bool
290 * @throws UserGroupTypeException
291 */
292 private function processPostMenuItem(object $item): bool
293 {
294 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
295 if ($this->removePostFromList($item->object) === true) {
296 return false;
297 }
298
299 if ($this->mainConfig->hidePostTypeTitle($item->object) === true) {
300 $item->title = $this->mainConfig->getPostTypeTitle($item->object);
301 }
302 }
303
304 return true;
305 }
306
307 /**
308 * Processes a term menu item.
309 * @param mixed $item
310 * @return bool
311 * @throws UserGroupTypeException
312 */
313 private function processTermMenuItem(&$item): bool
314 {
315 $rawTerm = $this->objectHandler->getTerm($item->object_id);
316 $term = $this->processTerm($rawTerm, $isEmpty);
317
318 return !($term === false || $term === null || $isEmpty === true);
319 }
320
321 /**
322 * The function for the wp_get_nav_menu_items filter.
323 * @param array $items The menu item.
324 * @return array
325 * @throws UserGroupTypeException
326 */
327 public function showCustomMenu(array $items): array
328 {
329 $showItems = [];
330
331 foreach ($items as $key => $item) {
332 $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
333
334 if ($this->objectHandler->isPostType($item->object) === true) {
335 if ($this->processPostMenuItem($item) === false) {
336 continue;
337 }
338 } elseif ($this->objectHandler->isTaxonomy($item->object) === true
339 && $this->processTermMenuItem($item) === false
340 ) {
341 continue;
342 }
343
344 $showItems[$key] = $item;
345 }
346
347 return $showItems;
348 }
349
350 /**
351 * Sets the excluded terms as argument.
352 * @param array $arguments
353 * @return array
354 * @throws UserGroupTypeException
355 */
356 public function getTermArguments(array $arguments): array
357 {
358 $exclude = (isset($arguments['exclude']) === true) ?
359 $this->wordpress->parseIdList($arguments['exclude']) : [];
360 $arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms());
361 $arguments['exclude'] = array_unique($arguments['exclude']);
362
363 return $arguments;
364 }
365 }
366