| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Config; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\Cache\Cache; |
| 9 |
use UserAccessManager\Object\ObjectHandler; |
| 10 |
use UserAccessManager\Wrapper\Wordpress; |
| 11 |
|
| 12 |
class MainConfig extends Config |
| 13 |
{ |
| 14 |
public const MAIN_CONFIG_KEY = 'uamAdminOptions'; |
| 15 |
public const DEFAULT_TYPE = 'default'; |
| 16 |
public const CACHE_PROVIDER_NONE = 'none'; |
| 17 |
|
| 18 |
/** |
| 19 |
* MainConfig constructor. |
| 20 |
* @param Wordpress $wordpress |
| 21 |
* @param ObjectHandler $objectHandler |
| 22 |
* @param Cache $cache |
| 23 |
* @param ConfigParameterFactory $configParameterFactory |
| 24 |
*/ |
| 25 |
public function __construct( |
| 26 |
Wordpress $wordpress, |
| 27 |
private ObjectHandler $objectHandler, |
| 28 |
private Cache $cache, |
| 29 |
private ConfigParameterFactory $configParameterFactory |
| 30 |
) { |
| 31 |
parent::__construct($wordpress, self::MAIN_CONFIG_KEY); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @throws Exception |
| 36 |
*/ |
| 37 |
private function addDefaultGeneralConfigParameters(array &$configParameters): void |
| 38 |
{ |
| 39 |
$id = 'redirect'; |
| 40 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 41 |
$id, |
| 42 |
'false', |
| 43 |
['false', 'blog', 'login', 'custom_page', 'custom_url', 'origin'] |
| 44 |
); |
| 45 |
|
| 46 |
$id = 'redirect_custom_page'; |
| 47 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 48 |
|
| 49 |
$id = 'redirect_custom_url'; |
| 50 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 51 |
|
| 52 |
$id = 'lock_recursive'; |
| 53 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 54 |
|
| 55 |
$id = 'authors_has_access_to_own'; |
| 56 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 57 |
|
| 58 |
$id = 'authors_can_add_posts_to_groups'; |
| 59 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 60 |
|
| 61 |
$id = 'blog_admin_hint'; |
| 62 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 63 |
|
| 64 |
$id = 'blog_admin_hint_text'; |
| 65 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, '[L]'); |
| 66 |
|
| 67 |
$id = 'show_assigned_groups'; |
| 68 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 69 |
|
| 70 |
$id = 'hide_edit_link_on_no_access'; |
| 71 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 72 |
|
| 73 |
$id = 'extra_ip_header'; |
| 74 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, 'HTTP_X_REAL_IP'); |
| 75 |
|
| 76 |
$id = 'protect_feed'; |
| 77 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 78 |
|
| 79 |
$id = 'full_access_role'; |
| 80 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 81 |
$id, |
| 82 |
'administrator', |
| 83 |
['administrator', 'editor', 'author', 'contributor', 'subscriber'] |
| 84 |
); |
| 85 |
|
| 86 |
$id = 'active_cache_provider'; |
| 87 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 88 |
$id, |
| 89 |
self::CACHE_PROVIDER_NONE, |
| 90 |
array_merge([self::CACHE_PROVIDER_NONE], array_keys($this->cache->getRegisteredCacheProviders())) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @throws Exception |
| 96 |
*/ |
| 97 |
private function addDefaultPostConfigParameters(array &$configParameters): void |
| 98 |
{ |
| 99 |
$postTypes = $this->objectHandler->getPostTypes(); |
| 100 |
array_unshift($postTypes, self::DEFAULT_TYPE); |
| 101 |
|
| 102 |
foreach ($postTypes as $postType) { |
| 103 |
if ($postType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
if ($postType !== self::DEFAULT_TYPE) { |
| 108 |
$id = "{$postType}_use_default"; |
| 109 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 110 |
} |
| 111 |
|
| 112 |
$id = "hide_$postType"; |
| 113 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 114 |
|
| 115 |
$id = "hide_{$postType}_title"; |
| 116 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 117 |
|
| 118 |
$id = "{$postType}_title"; |
| 119 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 120 |
$id, |
| 121 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS |
| 122 |
); |
| 123 |
|
| 124 |
$id = "{$postType}_content"; |
| 125 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 126 |
$id, |
| 127 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_ENTRY |
| 128 |
); |
| 129 |
|
| 130 |
$id = "hide_{$postType}_comment"; |
| 131 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 132 |
|
| 133 |
$id = "{$postType}_comment_content"; |
| 134 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 135 |
$id, |
| 136 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_COMMENTS |
| 137 |
); |
| 138 |
|
| 139 |
$id = "{$postType}_comments_locked"; |
| 140 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 141 |
|
| 142 |
$id = "show_{$postType}_content_before_more"; |
| 143 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @throws Exception |
| 149 |
*/ |
| 150 |
private function addDefaultTaxonomyConfigParameters(array &$configParameters): void |
| 151 |
{ |
| 152 |
$taxonomies = $this->objectHandler->getTaxonomies(); |
| 153 |
array_unshift($taxonomies, self::DEFAULT_TYPE); |
| 154 |
|
| 155 |
foreach ($taxonomies as $taxonomy) { |
| 156 |
if ($taxonomy !== self::DEFAULT_TYPE) { |
| 157 |
$id = "{$taxonomy}_use_default"; |
| 158 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 159 |
} |
| 160 |
|
| 161 |
$id = 'hide_empty_' . $taxonomy; |
| 162 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @throws Exception |
| 168 |
*/ |
| 169 |
private function addDefaultFileConfigParameters(array &$configParameters): void |
| 170 |
{ |
| 171 |
$id = 'lock_file'; |
| 172 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 173 |
|
| 174 |
$id = 'download_type'; |
| 175 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 176 |
$id, |
| 177 |
'fopen', |
| 178 |
['xsendfile', 'fopen', 'normal'] |
| 179 |
); |
| 180 |
|
| 181 |
$id = 'inline_files'; |
| 182 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, 'pdf'); |
| 183 |
|
| 184 |
$id = 'no_access_image_type'; |
| 185 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 186 |
$id, |
| 187 |
'default', |
| 188 |
['default', 'custom'] |
| 189 |
); |
| 190 |
|
| 191 |
$id = 'custom_no_access_image'; |
| 192 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 193 |
|
| 194 |
$id = 'use_custom_file_handling_file'; |
| 195 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 196 |
|
| 197 |
$id = 'locked_directory_type'; |
| 198 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 199 |
$id, |
| 200 |
'wordpress', |
| 201 |
['wordpress', 'all', 'custom'] |
| 202 |
); |
| 203 |
|
| 204 |
$id = 'custom_locked_directories'; |
| 205 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 206 |
|
| 207 |
$id = 'file_pass_type'; |
| 208 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 209 |
$id, |
| 210 |
'random', |
| 211 |
['random', 'user'] |
| 212 |
); |
| 213 |
|
| 214 |
$id = 'lock_file_types'; |
| 215 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 216 |
$id, |
| 217 |
'all', |
| 218 |
['all', 'selected', 'not_selected'] |
| 219 |
); |
| 220 |
|
| 221 |
$id = 'locked_file_types'; |
| 222 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 223 |
$id, |
| 224 |
'zip,rar,tar,gz' |
| 225 |
); |
| 226 |
|
| 227 |
$id = 'not_locked_file_types'; |
| 228 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 229 |
$id, |
| 230 |
'gif,jpg,jpeg,png' |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @return ConfigParameter[] |
| 236 |
* @throws Exception |
| 237 |
*/ |
| 238 |
protected function getDefaultConfigParameters(): array |
| 239 |
{ |
| 240 |
if ($this->defaultConfigParameters === []) { |
| 241 |
/** |
| 242 |
* @var ConfigParameter[] $configParameters |
| 243 |
*/ |
| 244 |
$configParameters = []; |
| 245 |
|
| 246 |
$this->addDefaultGeneralConfigParameters($configParameters); |
| 247 |
$this->addDefaultPostConfigParameters($configParameters); |
| 248 |
$this->addDefaultTaxonomyConfigParameters($configParameters); |
| 249 |
$this->addDefaultFileConfigParameters($configParameters); |
| 250 |
|
| 251 |
$this->defaultConfigParameters = $configParameters; |
| 252 |
} |
| 253 |
|
| 254 |
return $this->defaultConfigParameters; |
| 255 |
} |
| 256 |
|
| 257 |
private function getObjectParameter(string $objectType, string $rawParameterName): ?ConfigParameter |
| 258 |
{ |
| 259 |
$options = $this->getConfigParameters(); |
| 260 |
$parameterName = sprintf($rawParameterName, $objectType); |
| 261 |
|
| 262 |
if (isset($options[$parameterName]) === false |
| 263 |
|| isset($options["{$objectType}_use_default"]) === true |
| 264 |
&& $options["{$objectType}_use_default"]->getValue() === true |
| 265 |
) { |
| 266 |
$parameterName = sprintf($rawParameterName, self::DEFAULT_TYPE); |
| 267 |
} |
| 268 |
|
| 269 |
return (isset($options[$parameterName]) === true) ? $options[$parameterName] : null; |
| 270 |
} |
| 271 |
|
| 272 |
private function hideObject(string $objectType, string $parameterName): bool |
| 273 |
{ |
| 274 |
$parameter = $this->getObjectParameter($objectType, $parameterName); |
| 275 |
return ($parameter !== null) ? $parameter->getValue() : true; |
| 276 |
} |
| 277 |
|
| 278 |
private function getObjectContent(string $objectType, string $parameterName): bool|string |
| 279 |
{ |
| 280 |
$parameter = $this->getObjectParameter($objectType, $parameterName); |
| 281 |
return ($parameter !== null) ? $parameter->getValue() : ''; |
| 282 |
} |
| 283 |
|
| 284 |
public function hidePostType(string $postType): bool |
| 285 |
{ |
| 286 |
return $this->hideObject($postType, 'hide_%s'); |
| 287 |
} |
| 288 |
|
| 289 |
public function hidePostTypeTitle(string $postType): bool |
| 290 |
{ |
| 291 |
return $this->hideObject($postType, 'hide_%s_title'); |
| 292 |
} |
| 293 |
|
| 294 |
public function hidePostTypeComments(string $postType): bool |
| 295 |
{ |
| 296 |
return $this->hideObject($postType, 'hide_%s_comment'); |
| 297 |
} |
| 298 |
|
| 299 |
public function lockPostTypeComments(string $postType): bool |
| 300 |
{ |
| 301 |
return $this->hideObject($postType, '%s_comments_locked'); |
| 302 |
} |
| 303 |
|
| 304 |
public function getPostTypeTitle(string $postType): string |
| 305 |
{ |
| 306 |
return $this->getObjectContent($postType, '%s_title'); |
| 307 |
} |
| 308 |
|
| 309 |
public function getPostTypeContent(string $postType): string |
| 310 |
{ |
| 311 |
return $this->getObjectContent($postType, '%s_content'); |
| 312 |
} |
| 313 |
|
| 314 |
public function getPostTypeCommentContent(string $postType): string |
| 315 |
{ |
| 316 |
return $this->getObjectContent($postType, '%s_comment_content'); |
| 317 |
} |
| 318 |
|
| 319 |
public function showPostTypeContentBeforeMore(string $postType): bool |
| 320 |
{ |
| 321 |
return (bool) $this->getObjectContent($postType, 'show_%s_content_before_more'); |
| 322 |
} |
| 323 |
|
| 324 |
public function getRedirect(): ?string |
| 325 |
{ |
| 326 |
return $this->getParameterValue('redirect'); |
| 327 |
} |
| 328 |
|
| 329 |
public function getRedirectCustomPage(): ?string |
| 330 |
{ |
| 331 |
return $this->getParameterValue('redirect_custom_page'); |
| 332 |
} |
| 333 |
|
| 334 |
public function getRedirectCustomUrl(): ?string |
| 335 |
{ |
| 336 |
return $this->getParameterValue('redirect_custom_url'); |
| 337 |
} |
| 338 |
|
| 339 |
public function lockRecursive(): bool |
| 340 |
{ |
| 341 |
return (bool) $this->getParameterValue('lock_recursive'); |
| 342 |
} |
| 343 |
|
| 344 |
public function authorsHasAccessToOwn(): bool |
| 345 |
{ |
| 346 |
return (bool) $this->getParameterValue('authors_has_access_to_own'); |
| 347 |
} |
| 348 |
|
| 349 |
public function authorsCanAddPostsToGroups(): bool |
| 350 |
{ |
| 351 |
return (bool) $this->getParameterValue('authors_can_add_posts_to_groups'); |
| 352 |
} |
| 353 |
|
| 354 |
public function lockFile(): bool |
| 355 |
{ |
| 356 |
return (bool) $this->getParameterValue('lock_file'); |
| 357 |
} |
| 358 |
|
| 359 |
public function getInlineFiles(): ?string |
| 360 |
{ |
| 361 |
return $this->getParameterValue('inline_files'); |
| 362 |
} |
| 363 |
|
| 364 |
public function getNoAccessImageType(): ?string |
| 365 |
{ |
| 366 |
return $this->getParameterValue('no_access_image_type'); |
| 367 |
} |
| 368 |
|
| 369 |
public function getCustomNoAccessImage(): ?string |
| 370 |
{ |
| 371 |
return $this->getParameterValue('custom_no_access_image'); |
| 372 |
} |
| 373 |
|
| 374 |
public function useCustomFileHandlingFile(): bool |
| 375 |
{ |
| 376 |
return (bool) $this->getParameterValue('use_custom_file_handling_file'); |
| 377 |
} |
| 378 |
|
| 379 |
public function getLockedDirectoryType(): ?string |
| 380 |
{ |
| 381 |
return $this->getParameterValue('locked_directory_type'); |
| 382 |
} |
| 383 |
|
| 384 |
public function getCustomLockedDirectories(): ?string |
| 385 |
{ |
| 386 |
return $this->getParameterValue('custom_locked_directories'); |
| 387 |
} |
| 388 |
|
| 389 |
public function getFilePassType(): ?string |
| 390 |
{ |
| 391 |
return $this->getParameterValue('file_pass_type'); |
| 392 |
} |
| 393 |
|
| 394 |
public function getDownloadType(): ?string |
| 395 |
{ |
| 396 |
return $this->getParameterValue('download_type'); |
| 397 |
} |
| 398 |
|
| 399 |
public function getLockedFileType(): ?string |
| 400 |
{ |
| 401 |
return $this->getParameterValue('lock_file_types'); |
| 402 |
} |
| 403 |
|
| 404 |
public function getLockedFiles(): ?string |
| 405 |
{ |
| 406 |
return $this->getParameterValue('locked_file_types'); |
| 407 |
} |
| 408 |
|
| 409 |
public function getNotLockedFiles(): ?string |
| 410 |
{ |
| 411 |
return $this->getParameterValue('not_locked_file_types'); |
| 412 |
} |
| 413 |
|
| 414 |
public function blogAdminHint(): bool |
| 415 |
{ |
| 416 |
return (bool) $this->getParameterValue('blog_admin_hint'); |
| 417 |
} |
| 418 |
|
| 419 |
public function getBlogAdminHintText(): ?string |
| 420 |
{ |
| 421 |
return $this->getParameterValue('blog_admin_hint_text'); |
| 422 |
} |
| 423 |
|
| 424 |
public function showAssignedGroups(): bool |
| 425 |
{ |
| 426 |
return (bool) $this->getParameterValue('show_assigned_groups'); |
| 427 |
} |
| 428 |
|
| 429 |
public function hideEditLinkOnNoAccess(): bool |
| 430 |
{ |
| 431 |
return (bool) $this->getParameterValue('hide_edit_link_on_no_access'); |
| 432 |
} |
| 433 |
|
| 434 |
public function hideEmptyTaxonomy(string $taxonomy): bool |
| 435 |
{ |
| 436 |
$parameter = $this->getObjectParameter($taxonomy, 'hide_empty_%s'); |
| 437 |
return ($parameter !== null) ? $parameter->getValue() : false; |
| 438 |
} |
| 439 |
|
| 440 |
public function protectFeed(): bool |
| 441 |
{ |
| 442 |
return (bool) $this->getParameterValue('protect_feed'); |
| 443 |
} |
| 444 |
|
| 445 |
public function getFullAccessRole(): ?string |
| 446 |
{ |
| 447 |
return $this->getParameterValue('full_access_role'); |
| 448 |
} |
| 449 |
|
| 450 |
public function getActiveCacheProvider(): ?string |
| 451 |
{ |
| 452 |
return $this->getParameterValue('active_cache_provider'); |
| 453 |
} |
| 454 |
|
| 455 |
public function getExtraIpHeader(): ?string |
| 456 |
{ |
| 457 |
return $this->getParameterValue('extra_ip_header'); |
| 458 |
} |
| 459 |
} |
| 460 |
|