| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Controller\Frontend; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use stdClass; |
| 9 |
use UserAccessManager\Access\AccessHandler; |
| 10 |
use UserAccessManager\Config\MainConfig; |
| 11 |
use UserAccessManager\Config\WordpressConfig; |
| 12 |
use UserAccessManager\Object\ObjectHandler; |
| 13 |
use UserAccessManager\Object\ObjectMapHandler; |
| 14 |
use UserAccessManager\User\UserHandler; |
| 15 |
use UserAccessManager\UserGroup\UserGroupHandler; |
| 16 |
use UserAccessManager\UserGroup\UserGroupTypeException; |
| 17 |
use UserAccessManager\Util\Util; |
| 18 |
use UserAccessManager\Wrapper\Php; |
| 19 |
use UserAccessManager\Wrapper\Wordpress; |
| 20 |
use WP_Term; |
| 21 |
|
| 22 |
class TermController extends ContentController |
| 23 |
{ |
| 24 |
private array $visibleElementsCount = []; |
| 25 |
private ?array $postObjectHideConfig = null; |
| 26 |
|
| 27 |
public function __construct( |
| 28 |
Php $php, |
| 29 |
Wordpress $wordpress, |
| 30 |
WordpressConfig $wordpressConfig, |
| 31 |
MainConfig $mainConfig, |
| 32 |
Util $util, |
| 33 |
ObjectHandler $objectHandler, |
| 34 |
UserHandler $userHandler, |
| 35 |
UserGroupHandler $userGroupHandler, |
| 36 |
AccessHandler $accessHandler, |
| 37 |
private ObjectMapHandler $objectMapHandler |
| 38 |
) { |
| 39 |
parent::__construct( |
| 40 |
$php, |
| 41 |
$wordpress, |
| 42 |
$wordpressConfig, |
| 43 |
$mainConfig, |
| 44 |
$util, |
| 45 |
$objectHandler, |
| 46 |
$userHandler, |
| 47 |
$userGroupHandler, |
| 48 |
$accessHandler |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
private function getPostObjectHideConfig(): ?array |
| 53 |
{ |
| 54 |
if ($this->postObjectHideConfig === null) { |
| 55 |
$this->postObjectHideConfig = []; |
| 56 |
|
| 57 |
foreach ($this->objectHandler->getPostTypes() as $postType) { |
| 58 |
$this->postObjectHideConfig[$postType] = $this->mainConfig->hidePostType($postType); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $this->postObjectHideConfig; |
| 63 |
} |
| 64 |
|
| 65 |
private function getAllPostForTerm(string $termType, int|string|null $termId): array |
| 66 |
{ |
| 67 |
$fullTerms = [$termId => $termType]; |
| 68 |
$termTreeMap = $this->objectMapHandler->getTermTreeMap(); |
| 69 |
|
| 70 |
if (isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType]) === true |
| 71 |
&& isset($termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId]) === true |
| 72 |
) { |
| 73 |
$fullTerms += $termTreeMap[ObjectMapHandler::TREE_MAP_CHILDREN][$termType][$termId]; |
| 74 |
} |
| 75 |
|
| 76 |
$posts = []; |
| 77 |
$termPostMap = $this->objectMapHandler->getTermPostMap(); |
| 78 |
|
| 79 |
foreach ($fullTerms as $fullTermId => $fullTermType) { |
| 80 |
if (isset($termPostMap[$fullTermId]) === true) { |
| 81 |
$posts += $termPostMap[$fullTermId]; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $posts; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @throws UserGroupTypeException |
| 90 |
*/ |
| 91 |
private function getVisibleElementsCount(string $termType, int|string|null $termId): int |
| 92 |
{ |
| 93 |
$key = $termType . '|' . $termId; |
| 94 |
|
| 95 |
if (isset($this->visibleElementsCount[$key]) === false) { |
| 96 |
$count = 0; |
| 97 |
$posts = $this->getAllPostForTerm($termType, $termId); |
| 98 |
$postTypeHiddenMap = $this->getPostObjectHideConfig(); |
| 99 |
|
| 100 |
foreach ($posts as $postId => $postType) { |
| 101 |
if (isset($postTypeHiddenMap[$postType]) && $postTypeHiddenMap[$postType] === false |
| 102 |
|| $this->accessHandler->checkObjectAccess( |
| 103 |
ObjectHandler::GENERAL_POST_OBJECT_TYPE, |
| 104 |
$postId |
| 105 |
) === true |
| 106 |
) { |
| 107 |
$count++; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$this->visibleElementsCount[$key] = $count; |
| 112 |
} |
| 113 |
|
| 114 |
return $this->visibleElementsCount[$key]; |
| 115 |
} |
| 116 |
|
| 117 |
private function isCategoryEmpty(WP_Term $term): bool |
| 118 |
{ |
| 119 |
return $term->count <= 0 |
| 120 |
&& $this->wordpressConfig->atAdminPanel() === false |
| 121 |
&& $this->mainConfig->hideEmptyTaxonomy($term->taxonomy) === true; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @throws UserGroupTypeException |
| 126 |
*/ |
| 127 |
private function updateTermParent(WP_Term|stdClass $term): WP_Term|stdClass |
| 128 |
{ |
| 129 |
$currentTerm = $term; |
| 130 |
|
| 131 |
while ($currentTerm->parent != 0) { |
| 132 |
$currentTerm = $this->objectHandler->getTerm($currentTerm->parent); |
| 133 |
|
| 134 |
if ($currentTerm === false) { |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
$access = $this->accessHandler->checkObjectAccess( |
| 139 |
$currentTerm->taxonomy, |
| 140 |
$currentTerm->term_id |
| 141 |
); |
| 142 |
|
| 143 |
if ($access === true) { |
| 144 |
$term->parent = $currentTerm->term_id; |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $term; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @throws UserGroupTypeException |
| 154 |
* @throws Exception |
| 155 |
*/ |
| 156 |
private function processTerm(mixed $term, ?bool &$isEmpty = null): mixed |
| 157 |
{ |
| 158 |
$isEmpty = false; |
| 159 |
|
| 160 |
if (($term instanceof WP_Term) === false |
| 161 |
|| $this->objectHandler->isValidObjectType($term->taxonomy) === false |
| 162 |
) { |
| 163 |
return $term; |
| 164 |
} |
| 165 |
|
| 166 |
if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
$term->name .= $this->adminOutput((string) $term->taxonomy, $term->term_id, $term->name); |
| 171 |
$term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id); |
| 172 |
$isEmpty = $this->isCategoryEmpty($term); |
| 173 |
|
| 174 |
if ($this->mainConfig->lockRecursive() === false) { |
| 175 |
$term = $this->updateTermParent($term); |
| 176 |
} |
| 177 |
|
| 178 |
return $term; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @throws UserGroupTypeException |
| 183 |
*/ |
| 184 |
public function showTerm(mixed $term): mixed |
| 185 |
{ |
| 186 |
return $this->processTerm($term); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @param WP_Term[] $terms |
| 191 |
* @throws UserGroupTypeException |
| 192 |
*/ |
| 193 |
public function showTerms(array $terms = []): array |
| 194 |
{ |
| 195 |
foreach ($terms as $key => $term) { |
| 196 |
if (is_numeric($term) === true) { |
| 197 |
if ((int) $term === 0) { |
| 198 |
unset($terms[$key]); |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
$term = $this->objectHandler->getTerm($term); |
| 203 |
} |
| 204 |
|
| 205 |
$term = $this->processTerm($term, $isEmpty); |
| 206 |
|
| 207 |
if ($term === false || $isEmpty === true) { |
| 208 |
unset($terms[$key]); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return $terms; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* @throws UserGroupTypeException |
| 217 |
*/ |
| 218 |
private function processPostMenuItem(object $item): bool |
| 219 |
{ |
| 220 |
if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) { |
| 221 |
if ($this->removePostFromList($item->object) === true) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
if ($this->mainConfig->hidePostTypeTitle($item->object) === true) { |
| 226 |
$item->title = $this->mainConfig->getPostTypeTitle($item->object); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* @throws UserGroupTypeException |
| 235 |
*/ |
| 236 |
private function processTermMenuItem(mixed $item): bool |
| 237 |
{ |
| 238 |
$rawTerm = $this->objectHandler->getTerm($item->object_id); |
| 239 |
$term = $this->processTerm($rawTerm, $isEmpty); |
| 240 |
|
| 241 |
return !($term === false || $isEmpty === true); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @throws UserGroupTypeException |
| 246 |
*/ |
| 247 |
public function showCustomMenu(array $items): array |
| 248 |
{ |
| 249 |
$showItems = []; |
| 250 |
|
| 251 |
foreach ($items as $key => $item) { |
| 252 |
$item->title .= $this->adminOutput((string) $item->object, $item->object_id, $item->title); |
| 253 |
|
| 254 |
if ($this->objectHandler->isPostType($item->object) === true) { |
| 255 |
if ($this->processPostMenuItem($item) === false) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
} elseif ($this->objectHandler->isTaxonomy($item->object) === true |
| 259 |
&& $this->processTermMenuItem($item) === false |
| 260 |
) { |
| 261 |
continue; |
| 262 |
} |
| 263 |
|
| 264 |
$showItems[$key] = $item; |
| 265 |
} |
| 266 |
|
| 267 |
return $showItems; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* @throws UserGroupTypeException |
| 272 |
*/ |
| 273 |
public function getTermArguments(array $arguments): array |
| 274 |
{ |
| 275 |
$exclude = (isset($arguments['exclude']) === true) ? |
| 276 |
$this->wordpress->parseIdList($arguments['exclude']) : []; |
| 277 |
$arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms()); |
| 278 |
$arguments['exclude'] = array_unique($arguments['exclude']); |
| 279 |
|
| 280 |
return $arguments; |
| 281 |
} |
| 282 |
} |
| 283 |
|