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