| @@ -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,63 @@ | ||
| 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 | - } | |
| 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 { | |
| 39 | + $id = 'redirect'; | |
| 54 | 40 | $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 55 | 41 | $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 | 42 | 'false', |
| 70 | 43 | ['false', 'blog', 'login', 'custom_page', 'custom_url', 'origin'] |
| 71 | 44 | ); |
| 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', | |
| 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 = 'append_redirect_to_parameter'; | |
| 53 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 54 | + | |
| 55 | + $id = 'lock_recursive'; | |
| 56 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 57 | + | |
| 58 | + $id = 'authors_has_access_to_own'; | |
| 59 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 60 | + | |
| 61 | + $id = 'authors_can_add_posts_to_groups'; | |
| 62 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 63 | + | |
| 64 | + $id = 'blog_admin_hint'; | |
| 65 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 66 | + | |
| 67 | + $id = 'blog_admin_hint_text'; | |
| 68 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, '[L]'); | |
| 69 | + | |
| 70 | + $id = 'show_assigned_groups'; | |
| 71 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 72 | + | |
| 73 | + $id = 'hide_edit_link_on_no_access'; | |
| 74 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 75 | + | |
| 76 | + $id = 'extra_ip_header'; | |
| 77 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, 'HTTP_X_REAL_IP'); | |
| 78 | + | |
| 79 | + $id = 'protect_feed'; | |
| 80 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 81 | + | |
| 82 | + $id = 'full_access_role'; | |
| 83 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 84 | + $id, | |
| 87 | 85 | 'administrator', |
| 88 | 86 | ['administrator', 'editor', 'author', 'contributor', 'subscriber'] |
| 89 | 87 | ); |
| 90 | - $this->addSelection( | |
| 91 | - $configParameters, | |
| 92 | - 'active_cache_provider', | |
| 88 | + | |
| 89 | + $id = 'active_cache_provider'; | |
| 90 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 91 | + $id, | |
| 93 | 92 | self::CACHE_PROVIDER_NONE, |
| 94 | 93 | array_merge([self::CACHE_PROVIDER_NONE], array_keys($this->cache->getRegisteredCacheProviders())) |
| 95 | 94 | ); |
| 96 | 95 | } |
| @@ -108,23 +107,44 @@ | ||
| 108 | 107 | continue; |
| 109 | 108 | } |
| 110 | 109 | |
| 111 | 110 | if ($postType !== self::DEFAULT_TYPE) { |
| 112 | - $this->addBoolean($configParameters, "{$postType}_use_default"); | |
| 111 | + $id = "{$postType}_use_default"; | |
| 112 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 113 | 113 | } |
| 114 | 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", | |
| 115 | + $id = "hide_$postType"; | |
| 116 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 117 | + | |
| 118 | + $id = "hide_{$postType}_title"; | |
| 119 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 120 | + | |
| 121 | + $id = "{$postType}_title"; | |
| 122 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( | |
| 123 | + $id, | |
| 124 | + TXT_UAM_SETTING_DEFAULT_NO_RIGHTS | |
| 125 | + ); | |
| 126 | + | |
| 127 | + $id = "{$postType}_content"; | |
| 128 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( | |
| 129 | + $id, | |
| 130 | + TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_ENTRY | |
| 131 | + ); | |
| 132 | + | |
| 133 | + $id = "hide_{$postType}_comment"; | |
| 134 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 135 | + | |
| 136 | + $id = "{$postType}_comment_content"; | |
| 137 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( | |
| 138 | + $id, | |
| 123 | 139 | TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_COMMENTS |
| 124 | 140 | ); |
| 125 | - $this->addBoolean($configParameters, "{$postType}_comments_locked"); | |
| 126 | - $this->addBoolean($configParameters, "show_{$postType}_content_before_more"); | |
| 141 | + | |
| 142 | + $id = "{$postType}_comments_locked"; | |
| 143 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 144 | + | |
| 145 | + $id = "show_{$postType}_content_before_more"; | |
| 146 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 127 | 147 | } |
| 128 | 148 | } |
| 129 | 149 | |
| 130 | 150 | /** |
| @@ -136,12 +156,14 @@ | ||
| 136 | 156 | array_unshift($taxonomies, self::DEFAULT_TYPE); |
| 137 | 157 | |
| 138 | 158 | foreach ($taxonomies as $taxonomy) { |
| 139 | 159 | if ($taxonomy !== self::DEFAULT_TYPE) { |
| 140 | - $this->addBoolean($configParameters, "{$taxonomy}_use_default"); | |
| 160 | + $id = "{$taxonomy}_use_default"; | |
| 161 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 141 | 162 | } |
| 142 | 163 | |
| 143 | - $this->addBoolean($configParameters, "hide_empty_$taxonomy", true); | |
| 164 | + $id = 'hide_empty_' . $taxonomy; | |
| 165 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); | |
| 144 | 166 | } |
| 145 | 167 | } |
| 146 | 168 | |
| 147 | 169 | /** |
| @@ -148,20 +170,69 @@ | ||
| 148 | 170 | * @throws Exception |
| 149 | 171 | */ |
| 150 | 172 | private function addDefaultFileConfigParameters(array &$configParameters): void |
| 151 | 173 | { |
| 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'); | |
| 174 | + $id = 'lock_file'; | |
| 175 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 176 | + | |
| 177 | + $id = 'download_type'; | |
| 178 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 179 | + $id, | |
| 180 | + 'fopen', | |
| 181 | + ['xsendfile', 'fopen', 'normal'] | |
| 182 | + ); | |
| 183 | + | |
| 184 | + $id = 'inline_files'; | |
| 185 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, 'pdf'); | |
| 186 | + | |
| 187 | + $id = 'no_access_image_type'; | |
| 188 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 189 | + $id, | |
| 190 | + 'default', | |
| 191 | + ['default', 'custom'] | |
| 192 | + ); | |
| 193 | + | |
| 194 | + $id = 'custom_no_access_image'; | |
| 195 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); | |
| 196 | + | |
| 197 | + $id = 'use_custom_file_handling_file'; | |
| 198 | + $configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); | |
| 199 | + | |
| 200 | + $id = 'locked_directory_type'; | |
| 201 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 202 | + $id, | |
| 203 | + 'wordpress', | |
| 204 | + ['wordpress', 'all', 'custom'] | |
| 205 | + ); | |
| 206 | + | |
| 207 | + $id = 'custom_locked_directories'; | |
| 208 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); | |
| 209 | + | |
| 210 | + $id = 'file_pass_type'; | |
| 211 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 212 | + $id, | |
| 213 | + 'random', | |
| 214 | + ['random', 'user'] | |
| 215 | + ); | |
| 216 | + | |
| 217 | + $id = 'lock_file_types'; | |
| 218 | + $configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( | |
| 219 | + $id, | |
| 220 | + 'all', | |
| 221 | + ['all', 'selected', 'not_selected'] | |
| 222 | + ); | |
| 223 | + | |
| 224 | + $id = 'locked_file_types'; | |
| 225 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( | |
| 226 | + $id, | |
| 227 | + 'zip,rar,tar,gz' | |
| 228 | + ); | |
| 229 | + | |
| 230 | + $id = 'not_locked_file_types'; | |
| 231 | + $configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( | |
| 232 | + $id, | |
| 233 | + 'gif,jpg,jpeg,png' | |
| 234 | + ); | |
| 164 | 235 | } |
| 165 | 236 | |
| 166 | 237 | /** |
| 167 | 238 | * @return ConfigParameter[] |
| @@ -168,23 +239,23 @@ | ||
| 168 | 239 | * @throws Exception |
| 169 | 240 | */ |
| 170 | 241 | protected function getDefaultConfigParameters(): array |
| 171 | 242 | { |
| 172 | - if ($this->defaultConfigParameters !== []) { | |
| 173 | - return $this->defaultConfigParameters; | |
| 174 | - } | |
| 243 | + if ($this->defaultConfigParameters === []) { | |
| 244 | + /** | |
| 245 | + * @var ConfigParameter[] $configParameters | |
| 246 | + */ | |
| 247 | + $configParameters = []; | |
| 175 | 248 | |
| 176 | - /** | |
| 177 | - * @var ConfigParameter[] $configParameters | |
| 178 | - */ | |
| 179 | - $configParameters = []; | |
| 249 | + $this->addDefaultGeneralConfigParameters($configParameters); | |
| 250 | + $this->addDefaultPostConfigParameters($configParameters); | |
| 251 | + $this->addDefaultTaxonomyConfigParameters($configParameters); | |
| 252 | + $this->addDefaultFileConfigParameters($configParameters); | |
| 180 | 253 | |
| 181 | - $this->addDefaultGeneralConfigParameters($configParameters); | |
| 182 | - $this->addDefaultPostConfigParameters($configParameters); | |
| 183 | - $this->addDefaultTaxonomyConfigParameters($configParameters); | |
| 184 | - $this->addDefaultFileConfigParameters($configParameters); | |
| 254 | + $this->defaultConfigParameters = $configParameters; | |
| 255 | + } | |
| 185 | 256 | |
| 186 | - return $this->defaultConfigParameters = $configParameters; | |
| 257 | + return $this->defaultConfigParameters; | |
| 187 | 258 | } |
| 188 | 259 | |
| 189 | 260 | private function getObjectParameter(string $objectType, string $rawParameterName): ?ConfigParameter |
| 190 | 261 | { |
| @@ -189,25 +260,29 @@ | ||
| 189 | 260 | private function getObjectParameter(string $objectType, string $rawParameterName): ?ConfigParameter |
| 190 | 261 | { |
| 191 | 262 | $options = $this->getConfigParameters(); |
| 192 | 263 | $parameterName = sprintf($rawParameterName, $objectType); |
| 193 | - $useDefaultParameter = $options["{$objectType}_use_default"] ?? null; | |
| 194 | 264 | |
| 195 | - if (isset($options[$parameterName]) === false || $useDefaultParameter?->getValue() === true) { | |
| 265 | + if (isset($options[$parameterName]) === false | |
| 266 | + || isset($options["{$objectType}_use_default"]) === true | |
| 267 | + && $options["{$objectType}_use_default"]->getValue() === true | |
| 268 | + ) { | |
| 196 | 269 | $parameterName = sprintf($rawParameterName, self::DEFAULT_TYPE); |
| 197 | 270 | } |
| 198 | 271 | |
| 199 | - return $options[$parameterName] ?? null; | |
| 272 | + return (isset($options[$parameterName]) === true) ? $options[$parameterName] : null; | |
| 200 | 273 | } |
| 201 | 274 | |
| 202 | 275 | private function hideObject(string $objectType, string $parameterName): bool |
| 203 | 276 | { |
| 204 | - return $this->getObjectParameter($objectType, $parameterName)?->getValue() ?? true; | |
| 277 | + $parameter = $this->getObjectParameter($objectType, $parameterName); | |
| 278 | + return ($parameter !== null) ? $parameter->getValue() : true; | |
| 205 | 279 | } |
| 206 | 280 | |
| 207 | 281 | private function getObjectContent(string $objectType, string $parameterName): bool|string |
| 208 | 282 | { |
| 209 | - return $this->getObjectParameter($objectType, $parameterName)?->getValue() ?? ''; | |
| 283 | + $parameter = $this->getObjectParameter($objectType, $parameterName); | |
| 284 | + return ($parameter !== null) ? $parameter->getValue() : ''; | |
| 210 | 285 | } |
| 211 | 286 | |
| 212 | 287 | public function hidePostType(string $postType): bool |
| 213 | 288 | { |
| @@ -365,9 +440,10 @@ | ||
| 365 | 440 | } |
| 366 | 441 | |
| 367 | 442 | public function hideEmptyTaxonomy(string $taxonomy): bool |
| 368 | 443 | { |
| 369 | - return $this->getObjectParameter($taxonomy, 'hide_empty_%s')?->getValue() ?? false; | |
| 444 | + $parameter = $this->getObjectParameter($taxonomy, 'hide_empty_%s'); | |
| 445 | + return ($parameter !== null) ? $parameter->getValue() : false; | |
| 370 | 446 | } |
| 371 | 447 | |
| 372 | 448 | public function protectFeed(): bool |
| 373 | 449 | { |