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

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