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