PluginProbe
User Access Manager / trunk
User Access Manager vtrunk
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 / Content / TermController.php

TermController.php in User Access Manager trunk, at src/Controller/Frontend/Content/TermController.php

274 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Controller\Frontend\Content;
6
7 use Exception;
8 use stdClass;
9 use UserAccessManager\Access\AccessHandler;
10 use UserAccessManager\Config\MainConfig;
11 use UserAccessManager\Config\WordpressConfig;
12 use UserAccessManager\Object\ObjectHandler;
13 use UserAccessManager\Object\ObjectMapHandler;
14 use UserAccessManager\User\UserHandler;
15 use UserAccessManager\UserGroup\UserGroupHandler;
16 use UserAccessManager\UserGroup\UserGroupTypeException;
17 use UserAccessManager\Util\Util;
18 use UserAccessManager\Wrapper\Php;
19 use UserAccessManager\Wrapper\Wordpress;
20 use WP_Term;
21
22 class TermController extends ContentController
23 {
24 private array $visibleElementsCount = [];
25 private ?array $postObjectHideConfig = null;
26
27 public function __construct(
28 Php $php,
29 Wordpress $wordpress,
30 WordpressConfig $wordpressConfig,
31 MainConfig $mainConfig,
32 Util $util,
33 ObjectHandler $objectHandler,
34 UserHandler $userHandler,
35 UserGroupHandler $userGroupHandler,
36 AccessHandler $accessHandler,
37 private ObjectMapHandler $objectMapHandler
38 ) {
39 parent::__construct(
40 $php,
41 $wordpress,
42 $wordpressConfig,
43 $mainConfig,
44 $util,
45 $objectHandler,
46 $userHandler,
47 $userGroupHandler,
48 $accessHandler
49 );
50 }
51
52 private function getPostObjectHideConfig(): array
53 {
54 if ($this->postObjectHideConfig === null) {
55 $this->postObjectHideConfig = [];
56
57 foreach ($this->objectHandler->getPostTypes() as $postType) {
58 $this->postObjectHideConfig[$postType] = $this->mainConfig->hidePostType($postType);
59 }
60 }
61
62 return $this->postObjectHideConfig;
63 }
64
65 private function getAllPostsForTerm(string $termType, int|string|null $termId): array
66 {
67 $termTreeMap = $this->objectMapHandler->getTermTreeMap();
68 $childTerms = $termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId] ?? [];
69 $fullTerms = [$termId => $termType] + $childTerms;
70
71 $posts = [];
72 $termPostMap = $this->objectMapHandler->getTermPostMap();
73
74 foreach (array_keys($fullTerms) as $fullTermId) {
75 $posts += $termPostMap[$fullTermId] ?? [];
76 }
77
78 return $posts;
79 }
80
81 /**
82 * @throws UserGroupTypeException
83 */
84 private function getVisibleElementsCount(string $termType, int|string|null $termId): int
85 {
86 $key = $termType . '|' . $termId;
87
88 if (isset($this->visibleElementsCount[$key]) === true) {
89 return $this->visibleElementsCount[$key];
90 }
91
92 $count = 0;
93 $postTypeHiddenMap = $this->getPostObjectHideConfig();
94
95 foreach ($this->getAllPostsForTerm($termType, $termId) as $postId => $postType) {
96 $isPostTypeHidden = $postTypeHiddenMap[$postType] ?? true;
97
98 if ($isPostTypeHidden === false
99 || $this->accessHandler->checkObjectAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId) === true
100 ) {
101 $count++;
102 }
103 }
104
105 $this->visibleElementsCount[$key] = $count;
106
107 return $count;
108 }
109
110 private function isCategoryEmpty(WP_Term $term): bool
111 {
112 return $term->count <= 0
113 && $this->wordpressConfig->atAdminPanel() === false
114 && $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true;
115 }
116
117 /**
118 * @throws UserGroupTypeException
119 */
120 private function updateTermParent(WP_Term|stdClass $term): WP_Term|stdClass
121 {
122 $currentTerm = $term;
123
124 while ($currentTerm->parent != 0) {
125 $currentTerm = $this->objectHandler->getTerm($currentTerm->parent);
126
127 if ($currentTerm === false) {
128 break;
129 }
130
131 $access = $this->accessHandler->checkObjectAccess(
132 $currentTerm->taxonomy,
133 $currentTerm->term_id
134 );
135
136 if ($access === true) {
137 $term->parent = $currentTerm->term_id;
138 break;
139 }
140 }
141
142 return $term;
143 }
144
145 /**
146 * @throws UserGroupTypeException
147 * @throws Exception
148 */
149 private function processTerm(mixed $term, ?bool &$isEmpty = null): mixed
150 {
151 $isEmpty = false;
152
153 if (($term instanceof WP_Term) === false
154 || $this->objectHandler->isValidObjectType($term->taxonomy) === false
155 ) {
156 return $term;
157 }
158
159 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
160 return false;
161 }
162
163 $term->name .= $this->adminOutput((string) $term->taxonomy, $term->term_id, $term->name);
164 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
165 $isEmpty = $this->isCategoryEmpty($term);
166
167 if ($this->mainConfig->lockRecursive() === false) {
168 $term = $this->updateTermParent($term);
169 }
170
171 return $term;
172 }
173
174 /**
175 * @throws UserGroupTypeException
176 */
177 public function showTerm(mixed $term): mixed
178 {
179 return $this->processTerm($term);
180 }
181
182 /**
183 * @param WP_Term[] $terms
184 * @throws UserGroupTypeException
185 */
186 public function showTerms(array $terms = []): array
187 {
188 foreach ($terms as $key => $term) {
189 if (is_numeric($term) === true) {
190 if ((int) $term === 0) {
191 unset($terms[$key]);
192 continue;
193 }
194
195 $term = $this->objectHandler->getTerm($term);
196 }
197
198 $term = $this->processTerm($term, $isEmpty);
199
200 if ($term === false || $isEmpty === true) {
201 unset($terms[$key]);
202 }
203 }
204
205 return $terms;
206 }
207
208 /**
209 * @throws UserGroupTypeException
210 */
211 private function processPostMenuItem(object $item): bool
212 {
213 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
214 if ($this->removePostFromList($item->object) === true) {
215 return false;
216 }
217
218 if ($this->mainConfig->hidePostTypeTitle($item->object) === true) {
219 $item->title = $this->mainConfig->getPostTypeTitle($item->object);
220 }
221 }
222
223 return true;
224 }
225
226 /**
227 * @throws UserGroupTypeException
228 */
229 private function processTermMenuItem(object $item): bool
230 {
231 $term = $this->processTerm($this->objectHandler->getTerm($item->object_id), $isEmpty);
232
233 return $term !== false && $isEmpty !== true;
234 }
235
236 /**
237 * @throws UserGroupTypeException
238 */
239 public function showCustomMenu(array $items): array
240 {
241 $showItems = [];
242
243 foreach ($items as $key => $item) {
244 $item->title .= $this->adminOutput((string) $item->object, $item->object_id, $item->title);
245
246 if ($this->objectHandler->isPostType($item->object) === true) {
247 if ($this->processPostMenuItem($item) === false) {
248 continue;
249 }
250 } elseif ($this->objectHandler->isTaxonomy($item->object) === true
251 && $this->processTermMenuItem($item) === false
252 ) {
253 continue;
254 }
255
256 $showItems[$key] = $item;
257 }
258
259 return $showItems;
260 }
261
262 /**
263 * @throws UserGroupTypeException
264 */
265 public function getTermArguments(array $arguments): array
266 {
267 $exclude = (isset($arguments['exclude']) === true) ?
268 $this->wordpress->parseIdList($arguments['exclude']) : [];
269 $arguments['exclude'] = array_unique(array_merge($exclude, $this->accessHandler->getExcludedTerms()));
270
271 return $arguments;
272 }
273 }
274