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