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