PluginProbe
User Access Manager / 2.2.13
User Access Manager v2.2.13
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
← All changes | src/Controller/Frontend/TermController.php +100 -18 2.3.132.2.13 View file →
@@ -1,5 +1,18 @@
1 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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Controller\Frontend;
@@ -18,13 +31,38 @@
18 31 use UserAccessManager\Wrapper\Php;
19 32 use UserAccessManager\Wrapper\Wordpress;
20 33 use WP_Term;
21 34
35 +/**
36 + * Class FrontendTermController
37 + *
38 + * @package UserAccessManager\Controller
39 + */
22 40 class TermController extends ContentController
23 41 {
24 - private array $visibleElementsCount = [];
25 - private ?array $postObjectHideConfig = null;
42 + /**
43 + * @var array
44 + */
45 + private $visibleElementsCount = [];
26 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 + */
27 65 public function __construct(
28 66 Php $php,
29 67 Wordpress $wordpress,
30 68 WordpressConfig $wordpressConfig,
@@ -30,12 +68,12 @@
30 68 WordpressConfig $wordpressConfig,
31 69 MainConfig $mainConfig,
32 70 Util $util,
33 71 ObjectHandler $objectHandler,
72 + ObjectMapHandler $objectMapHandler,
34 73 UserHandler $userHandler,
35 74 UserGroupHandler $userGroupHandler,
36 - AccessHandler $accessHandler,
37 - private ObjectMapHandler $objectMapHandler
75 + AccessHandler $accessHandler
38 76 ) {
39 77 parent::__construct(
40 78 $php,
41 79 $wordpress,
@@ -46,10 +84,15 @@
46 84 $userHandler,
47 85 $userGroupHandler,
48 86 $accessHandler
49 87 );
88 + $this->objectMapHandler = $objectMapHandler;
50 89 }
51 90
91 + /**
92 + * Returns the post object hide config.
93 + * @return array
94 + */
52 95 private function getPostObjectHideConfig(): ?array
53 96 {
54 97 if ($this->postObjectHideConfig === null) {
55 98 $this->postObjectHideConfig = [];
@@ -61,9 +104,15 @@
61 104
62 105 return $this->postObjectHideConfig;
63 106 }
64 107
65 - private function getAllPostForTerm(string $termType, int|string|null $termId): array
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
66 115 {
67 116 $fullTerms = [$termId => $termType];
68 117 $termTreeMap = $this->objectMapHandler->getTermTreeMap();
69 118
@@ -85,11 +134,15 @@
85 134 return $posts;
86 135 }
87 136
88 137 /**
138 + * Returns the post count for the term.
139 + * @param string $termType
140 + * @param int|string $termId
141 + * @return int
89 142 * @throws UserGroupTypeException
90 143 */
91 - private function getVisibleElementsCount(string $termType, int|string|null $termId): int
144 + private function getVisibleElementsCount(string $termType, $termId): int
92 145 {
93 146 $key = $termType . '|' . $termId;
94 147
95 148 if (isset($this->visibleElementsCount[$key]) === false) {
@@ -113,9 +166,14 @@
113 166
114 167 return $this->visibleElementsCount[$key];
115 168 }
116 169
117 - private function isCategoryEmpty(WP_Term $term): bool
170 + /**
171 + * Checks if the category is empty.
172 + * @param $term
173 + * @return bool
174 + */
175 + private function isCategoryEmpty($term): bool
118 176 {
119 177 return $term->count <= 0
120 178 && $this->wordpressConfig->atAdminPanel() === false
121 179 && $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true;
@@ -121,11 +179,14 @@
121 179 && $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true;
122 180 }
123 181
124 182 /**
183 + * Updates the term parent.
184 + * @param WP_Term $term
185 + * @return WP_Term
125 186 * @throws UserGroupTypeException
126 187 */
127 - private function updateTermParent(WP_Term|stdClass $term): WP_Term|stdClass
188 + private function updateTermParent(WP_Term $term): WP_Term
128 189 {
129 190 $currentTerm = $term;
130 191
131 192 while ($currentTerm->parent != 0) {
@@ -149,12 +210,16 @@
149 210 return $term;
150 211 }
151 212
152 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
153 218 * @throws UserGroupTypeException
154 219 * @throws Exception
155 220 */
156 - private function processTerm(mixed $term, ?bool &$isEmpty = null): mixed
221 + private function processTerm($term, &$isEmpty = null)
157 222 {
158 223 $isEmpty = false;
159 224
160 225 if (($term instanceof WP_Term) === false
@@ -163,12 +228,12 @@
163 228 return $term;
164 229 }
165 230
166 231 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
167 - return false;
232 + return null;
168 233 }
169 234
170 - $term->name .= $this->adminOutput((string) $term->taxonomy, $term->term_id, $term->name);
235 + $term->name .= $this->adminOutput($term->taxonomy, $term->term_id, $term->name);
171 236 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
172 237 $isEmpty = $this->isCategoryEmpty($term);
173 238
174 239 if ($this->mainConfig->lockRecursive() === false) {
@@ -178,20 +243,25 @@
178 243 return $term;
179 244 }
180 245
181 246 /**
247 + * The function for the get_term filter.
248 + * @param stdClass|WP_Term $term
249 + * @return null|object
182 250 * @throws UserGroupTypeException
183 251 */
184 - public function showTerm(mixed $term): mixed
252 + public function showTerm($term)
185 253 {
186 254 return $this->processTerm($term);
187 255 }
188 256
189 257 /**
190 - * @param WP_Term[] $terms
258 + * The function for the get_terms filter.
259 + * @param array $terms The terms.
260 + * @return array
191 261 * @throws UserGroupTypeException
192 262 */
193 - public function showTerms(array $terms = []): array
263 + public function showTerms($terms = []): array
194 264 {
195 265 foreach ($terms as $key => $term) {
196 266 if (is_numeric($term) === true) {
197 267 if ((int) $term === 0) {
@@ -203,9 +273,9 @@
203 273 }
204 274
205 275 $term = $this->processTerm($term, $isEmpty);
206 276
207 - if ($term === false || $isEmpty === true) {
277 + if ($term === null || $isEmpty === true) {
208 278 unset($terms[$key]);
209 279 }
210 280 }
211 281
@@ -212,8 +282,11 @@
212 282 return $terms;
213 283 }
214 284
215 285 /**
286 + * Processes a post menu item.
287 + * @param object $item
288 + * @return bool
216 289 * @throws UserGroupTypeException
217 290 */
218 291 private function processPostMenuItem(object $item): bool
219 292 {
@@ -230,19 +303,25 @@
230 303 return true;
231 304 }
232 305
233 306 /**
307 + * Processes a term menu item.
308 + * @param mixed $item
309 + * @return bool
234 310 * @throws UserGroupTypeException
235 311 */
236 - private function processTermMenuItem(mixed $item): bool
312 + private function processTermMenuItem(&$item): bool
237 313 {
238 314 $rawTerm = $this->objectHandler->getTerm($item->object_id);
239 315 $term = $this->processTerm($rawTerm, $isEmpty);
240 316
241 - return !($term === false || $isEmpty === true);
317 + return !($term === false || $term === null || $isEmpty === true);
242 318 }
243 319
244 320 /**
321 + * The function for the wp_get_nav_menu_items filter.
322 + * @param array $items The menu item.
323 + * @return array
245 324 * @throws UserGroupTypeException
246 325 */
247 326 public function showCustomMenu(array $items): array
248 327 {
@@ -248,9 +327,9 @@
248 327 {
249 328 $showItems = [];
250 329
251 330 foreach ($items as $key => $item) {
252 - $item->title .= $this->adminOutput((string) $item->object, $item->object_id, $item->title);
331 + $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
253 332
254 333 if ($this->objectHandler->isPostType($item->object) === true) {
255 334 if ($this->processPostMenuItem($item) === false) {
256 335 continue;
@@ -267,8 +346,11 @@
267 346 return $showItems;
268 347 }
269 348
270 349 /**
350 + * Sets the excluded terms as argument.
351 + * @param array $arguments
352 + * @return array
271 353 * @throws UserGroupTypeException
272 354 */
273 355 public function getTermArguments(array $arguments): array
274 356 {