| @@ -5,10 +5,8 @@ | ||
| 5 | 5 | namespace UserAccessManager\Config; |
| 6 | 6 | |
| 7 | 7 | use Exception; |
| 8 | 8 | use UserAccessManager\Cache\Cache; |
| 9 | -use UserAccessManager\Config\Parameter\ConfigParameter; | |
| 10 | -use UserAccessManager\Config\Parameter\ConfigParameterFactory; | |
| 11 | 9 | use UserAccessManager\Object\ObjectHandler; |
| 12 | 10 | use UserAccessManager\Wrapper\Wordpress; |
| 13 | 11 | |
| 14 | 12 | class MainConfig extends Config |
| @@ -16,8 +14,15 @@ | ||
| 16 | 14 | public const MAIN_CONFIG_KEY = 'uamAdminOptions'; |
| 17 | 15 | public const DEFAULT_TYPE = 'default'; |
| 18 | 16 | public const CACHE_PROVIDER_NONE = 'none'; |
| 19 | 17 | |
| 18 | + /** | |
| 19 | + * MainConfig constructor. | |
| 20 | + * @param Wordpress $wordpress | |
| 21 | + * @param ObjectHandler $objectHandler | |
| 22 | + * @param Cache $cache | |
| 23 | + * @param ConfigParameterFactory $configParameterFactory | |
| 24 | + */ | |
| 20 | 25 | public function __construct( |
| 21 | 26 | Wordpress $wordpress, |
| 22 | 27 | private ObjectHandler $objectHandler, |
| 23 | 28 | private Cache $cache, |
| @@ -28,69 +33,60 @@ | ||
| 28 | 33 | |
| 29 | 34 | /** |
| 30 | 35 | * @throws Exception |
| 31 | 36 | */ |
| 32 | - private function addBoolean(array &$configParameters, string $id, bool $defaultValue = false): void | |
| 37 | + private function addDefaultGeneralConfigParameters(array &$configParameters): void | |
| 33 | 38 | { |
| 34 | - $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, $defaultValue); | |
| 35 | - } | |
| 39 | + $id = 'redirect'; | |
| 40 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 41 | + $id, | |
| 42 | + 'false', | |
| 43 | + ['false', 'blog', 'login', 'custom_page', 'custom_url'] | |
| 44 | + ); | |
| 36 | 45 | |
| 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 | - } | |
| 46 | + $id = 'redirect_custom_page'; | |
| 47 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); | |
| 44 | 48 | |
| 45 | - /** | |
| 46 | - * @throws Exception | |
| 47 | - */ | |
| 48 | - private function addSelection( | |
| 49 | - array &$configParameters, | |
| 50 | - string $id, | |
| 51 | - string $defaultValue, | |
| 52 | - array $selections | |
| 53 | - ): void { | |
| 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'; | |
| 54 | 80 | $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 55 | 81 | $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 | 82 | 'administrator', |
| 88 | 83 | ['administrator', 'editor', 'author', 'contributor', 'subscriber'] |
| 89 | 84 | ); |
| 90 | - $this->addSelection( | |
| 91 | - $configParameters, | |
| 92 | - 'active_cache_provider', | |
| 85 | + | |
| 86 | + $id = 'active_cache_provider'; | |
| 87 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 88 | + $id, | |
| 93 | 89 | self::CACHE_PROVIDER_NONE, |
| 94 | 90 | array_merge([self::CACHE_PROVIDER_NONE], array_keys($this->cache->getRegisteredCacheProviders())) |
| 95 | 91 | ); |
| 96 | 92 | } |
| @@ -108,23 +104,44 @@ | ||
| 108 | 104 | continue; |
| 109 | 105 | } |
| 110 | 106 | |
| 111 | 107 | if ($postType !== self::DEFAULT_TYPE) { |
| 112 | - $this->addBoolean($configParameters, "{$postType}_use_default"); | |
| 108 | + $id = "{$postType}_use_default"; | |
| 109 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 113 | 110 | } |
| 114 | 111 | |
| 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", | |
| 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, | |
| 123 | 136 | TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_COMMENTS |
| 124 | 137 | ); |
| 125 | - $this->addBoolean($configParameters, "{$postType}_comments_locked"); | |
| 126 | - $this->addBoolean($configParameters, "show_{$postType}_content_before_more"); | |
| 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); | |
| 127 | 144 | } |
| 128 | 145 | } |
| 129 | 146 | |
| 130 | 147 | /** |
| @@ -136,12 +153,14 @@ | ||
| 136 | 153 | array_unshift($taxonomies, self::DEFAULT_TYPE); |
| 137 | 154 | |
| 138 | 155 | foreach ($taxonomies as $taxonomy) { |
| 139 | 156 | if ($taxonomy !== self::DEFAULT_TYPE) { |
| 140 | - $this->addBoolean($configParameters, "{$taxonomy}_use_default"); | |
| 157 | + $id = "{$taxonomy}_use_default"; | |
| 158 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 141 | 159 | } |
| 142 | 160 | |
| 143 | - $this->addBoolean($configParameters, "hide_empty_$taxonomy", true); | |
| 161 | + $id = 'hide_empty_' . $taxonomy; | |
| 162 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 144 | 163 | } |
| 145 | 164 | } |
| 146 | 165 | |
| 147 | 166 | /** |
| @@ -148,20 +167,69 @@ | ||
| 148 | 167 | * @throws Exception |
| 149 | 168 | */ |
| 150 | 169 | private function addDefaultFileConfigParameters(array &$configParameters): void |
| 151 | 170 | { |
| 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'); | |
| 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 | + ); | |
| 164 | 232 | } |
| 165 | 233 | |
| 166 | 234 | /** |
| 167 | 235 | * @return ConfigParameter[] |
| @@ -168,23 +236,23 @@ | ||
| 168 | 236 | * @throws Exception |
| 169 | 237 | */ |
| 170 | 238 | protected function getDefaultConfigParameters(): array |
| 171 | 239 | { |
| 172 | - if ($this->defaultConfigParameters !== []) { | |
| 173 | - return $this->defaultConfigParameters; | |
| 174 | - } | |
| 240 | + if ($this->defaultConfigParameters === []) { | |
| 241 | + /** | |
| 242 | + * @var ConfigParameter[] $configParameters | |
| 243 | + */ | |
| 244 | + $configParameters = []; | |
| 175 | 245 | |
| 176 | - /** | |
| 177 | - * @var ConfigParameter[] $configParameters | |
| 178 | - */ | |
| 179 | - $configParameters = []; | |
| 246 | + $this->addDefaultGeneralConfigParameters($configParameters); | |
| 247 | + $this->addDefaultPostConfigParameters($configParameters); | |
| 248 | + $this->addDefaultTaxonomyConfigParameters($configParameters); | |
| 249 | + $this->addDefaultFileConfigParameters($configParameters); | |
| 180 | 250 | |
| 181 | - $this->addDefaultGeneralConfigParameters($configParameters); | |
| 182 | - $this->addDefaultPostConfigParameters($configParameters); | |
| 183 | - $this->addDefaultTaxonomyConfigParameters($configParameters); | |
| 184 | - $this->addDefaultFileConfigParameters($configParameters); | |
| 251 | + $this->defaultConfigParameters = $configParameters; | |
| 252 | + } | |
| 185 | 253 | |
| 186 | - return $this->defaultConfigParameters = $configParameters; | |
| 254 | + return $this->defaultConfigParameters; | |
| 187 | 255 | } |
| 188 | 256 | |
| 189 | 257 | private function getObjectParameter(string $objectType, string $rawParameterName): ?ConfigParameter |
| 190 | 258 | { |
| @@ -189,25 +257,29 @@ | ||
| 189 | 257 | private function getObjectParameter(string $objectType, string $rawParameterName): ?ConfigParameter |
| 190 | 258 | { |
| 191 | 259 | $options = $this->getConfigParameters(); |
| 192 | 260 | $parameterName = sprintf($rawParameterName, $objectType); |
| 193 | - $useDefaultParameter = $options["{$objectType}_use_default"] ?? null; | |
| 194 | 261 | |
| 195 | - if (isset($options[$parameterName]) === false || $useDefaultParameter?->getValue() === true) { | |
| 262 | + if (isset($options[$parameterName]) === false | |
| 263 | + || isset($options["{$objectType}_use_default"]) === true | |
| 264 | + && $options["{$objectType}_use_default"]->getValue() === true | |
| 265 | + ) { | |
| 196 | 266 | $parameterName = sprintf($rawParameterName, self::DEFAULT_TYPE); |
| 197 | 267 | } |
| 198 | 268 | |
| 199 | - return $options[$parameterName] ?? null; | |
| 269 | + return (isset($options[$parameterName]) === true) ? $options[$parameterName] : null; | |
| 200 | 270 | } |
| 201 | 271 | |
| 202 | 272 | private function hideObject(string $objectType, string $parameterName): bool |
| 203 | 273 | { |
| 204 | - return $this->getObjectParameter($objectType, $parameterName)?->getValue() ?? true; | |
| 274 | + $parameter = $this->getObjectParameter($objectType, $parameterName); | |
| 275 | + return ($parameter !== null) ? $parameter->getValue() : true; | |
| 205 | 276 | } |
| 206 | 277 | |
| 207 | 278 | private function getObjectContent(string $objectType, string $parameterName): bool|string |
| 208 | 279 | { |
| 209 | - return $this->getObjectParameter($objectType, $parameterName)?->getValue() ?? ''; | |
| 280 | + $parameter = $this->getObjectParameter($objectType, $parameterName); | |
| 281 | + return ($parameter !== null) ? $parameter->getValue() : ''; | |
| 210 | 282 | } |
| 211 | 283 | |
| 212 | 284 | public function hidePostType(string $postType): bool |
| 213 | 285 | { |
| @@ -263,13 +335,8 @@ | ||
| 263 | 335 | { |
| 264 | 336 | return $this->getParameterValue('redirect_custom_url'); |
| 265 | 337 | } |
| 266 | 338 | |
| 267 | - public function appendRedirectToParameter(): bool | |
| 268 | - { | |
| 269 | - return (bool) $this->getParameterValue('append_redirect_to_parameter'); | |
| 270 | - } | |
| 271 | - | |
| 272 | 339 | public function lockRecursive(): bool |
| 273 | 340 | { |
| 274 | 341 | return (bool) $this->getParameterValue('lock_recursive'); |
| 275 | 342 | } |
| @@ -365,9 +432,10 @@ | ||
| 365 | 432 | } |
| 366 | 433 | |
| 367 | 434 | public function hideEmptyTaxonomy(string $taxonomy): bool |
| 368 | 435 | { |
| 369 | - return $this->getObjectParameter($taxonomy, 'hide_empty_%s')?->getValue() ?? false; | |
| 436 | + $parameter = $this->getObjectParameter($taxonomy, 'hide_empty_%s'); | |
| 437 | + return ($parameter !== null) ? $parameter->getValue() : false; | |
| 370 | 438 | } |
| 371 | 439 | |
| 372 | 440 | public function protectFeed(): bool |
| 373 | 441 | { |