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