| 1 |
<?php |
| 2 |
/** |
| 3 |
* Config.php |
| 4 |
* |
| 5 |
* The Config class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 10 |
* @copyright 2008-2017 Alexander Schneider |
| 11 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 12 |
* @version SVN: $id$ |
| 13 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 14 |
*/ |
| 15 |
namespace UserAccessManager\Config; |
| 16 |
|
| 17 |
use UserAccessManager\Cache\Cache; |
| 18 |
use UserAccessManager\Object\ObjectHandler; |
| 19 |
use UserAccessManager\Wrapper\Wordpress; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Config |
| 23 |
* |
| 24 |
* @package UserAccessManager\Config |
| 25 |
*/ |
| 26 |
class MainConfig extends Config |
| 27 |
{ |
| 28 |
const MAIN_CONFIG_KEY = 'uamAdminOptions'; |
| 29 |
const DEFAULT_TYPE = 'default'; |
| 30 |
const CACHE_PROVIDER_NONE = 'none'; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var ObjectHandler |
| 34 |
*/ |
| 35 |
private $objectHandler; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Cache |
| 39 |
*/ |
| 40 |
private $cache; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var ConfigParameterFactory |
| 44 |
*/ |
| 45 |
protected $configParameterFactory; |
| 46 |
|
| 47 |
/** |
| 48 |
* MainConfig constructor. |
| 49 |
* |
| 50 |
* @param Wordpress $wordpress |
| 51 |
* @param ObjectHandler $objectHandler |
| 52 |
* @param Cache $cache |
| 53 |
* @param ConfigParameterFactory $configParameterFactory |
| 54 |
*/ |
| 55 |
public function __construct( |
| 56 |
Wordpress $wordpress, |
| 57 |
ObjectHandler $objectHandler, |
| 58 |
Cache $cache, |
| 59 |
ConfigParameterFactory $configParameterFactory |
| 60 |
) { |
| 61 |
$this->objectHandler = $objectHandler; |
| 62 |
$this->cache = $cache; |
| 63 |
$this->configParameterFactory = $configParameterFactory; |
| 64 |
|
| 65 |
parent::__construct($wordpress, self::MAIN_CONFIG_KEY); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Adds the default general config parameters to the config parameters. |
| 70 |
* |
| 71 |
* @param array $configParameters |
| 72 |
*/ |
| 73 |
private function addDefaultGeneralConfigParameters(array &$configParameters) |
| 74 |
{ |
| 75 |
$id = 'redirect'; |
| 76 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 77 |
$id, |
| 78 |
'false', |
| 79 |
['false', 'custom_page', 'custom_url'] |
| 80 |
); |
| 81 |
|
| 82 |
$id = 'redirect_custom_page'; |
| 83 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 84 |
|
| 85 |
$id = 'redirect_custom_url'; |
| 86 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 87 |
|
| 88 |
$id = 'lock_recursive'; |
| 89 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 90 |
|
| 91 |
$id = 'authors_has_access_to_own'; |
| 92 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 93 |
|
| 94 |
$id = 'authors_can_add_posts_to_groups'; |
| 95 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 96 |
|
| 97 |
$id = 'blog_admin_hint'; |
| 98 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 99 |
|
| 100 |
$id = 'blog_admin_hint_text'; |
| 101 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, '[L]'); |
| 102 |
|
| 103 |
$id = 'show_assigned_groups'; |
| 104 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 105 |
|
| 106 |
$id = 'protect_feed'; |
| 107 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 108 |
|
| 109 |
$id = 'full_access_role'; |
| 110 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 111 |
$id, |
| 112 |
'administrator', |
| 113 |
['administrator', 'editor', 'author', 'contributor', 'subscriber'] |
| 114 |
); |
| 115 |
|
| 116 |
$id = 'active_cache_provider'; |
| 117 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 118 |
$id, |
| 119 |
self::CACHE_PROVIDER_NONE, |
| 120 |
array_merge([self::CACHE_PROVIDER_NONE], array_keys($this->cache->getRegisteredCacheProviders())) |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Adds the default post config parameters to the config parameters. |
| 126 |
* |
| 127 |
* @param array $configParameters |
| 128 |
*/ |
| 129 |
private function addDefaultPostConfigParameters(array &$configParameters) |
| 130 |
{ |
| 131 |
$postTypes = $this->objectHandler->getPostTypes(); |
| 132 |
array_unshift($postTypes, self::DEFAULT_TYPE); |
| 133 |
|
| 134 |
foreach ($postTypes as $postType) { |
| 135 |
if ($postType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) { |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
if ($postType !== self::DEFAULT_TYPE) { |
| 140 |
$id = "{$postType}_use_default"; |
| 141 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 142 |
} |
| 143 |
|
| 144 |
$id = "hide_{$postType}"; |
| 145 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 146 |
|
| 147 |
$id = "hide_{$postType}_title"; |
| 148 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 149 |
|
| 150 |
$id = "{$postType}_title"; |
| 151 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 152 |
$id, |
| 153 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS |
| 154 |
); |
| 155 |
|
| 156 |
$id = "{$postType}_content"; |
| 157 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 158 |
$id, |
| 159 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_ENTRY |
| 160 |
); |
| 161 |
|
| 162 |
$id = "hide_{$postType}_comment"; |
| 163 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 164 |
|
| 165 |
$id = "{$postType}_comment_content"; |
| 166 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 167 |
$id, |
| 168 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_COMMENTS |
| 169 |
); |
| 170 |
|
| 171 |
$id = "{$postType}_comments_locked"; |
| 172 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 173 |
|
| 174 |
if ($postType === 'post') { |
| 175 |
$id = "show_{$postType}_content_before_more"; |
| 176 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Adds the default taxonomy config parameters to the config parameters. |
| 183 |
* |
| 184 |
* @param array $configParameters |
| 185 |
*/ |
| 186 |
private function addDefaultTaxonomyConfigParameters(array &$configParameters) |
| 187 |
{ |
| 188 |
$taxonomies = $this->objectHandler->getTaxonomies(); |
| 189 |
array_unshift($taxonomies, self::DEFAULT_TYPE); |
| 190 |
|
| 191 |
foreach ($taxonomies as $taxonomy) { |
| 192 |
if ($taxonomy !== self::DEFAULT_TYPE) { |
| 193 |
$id = "{$taxonomy}_use_default"; |
| 194 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 195 |
} |
| 196 |
|
| 197 |
$id = 'hide_empty_'.$taxonomy; |
| 198 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Adds the default file config parameters to the config parameters. |
| 204 |
* |
| 205 |
* @param array $configParameters |
| 206 |
*/ |
| 207 |
private function addDefaultFileConfigParameters(array &$configParameters) |
| 208 |
{ |
| 209 |
$id = 'lock_file'; |
| 210 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 211 |
|
| 212 |
$id = 'file_pass_type'; |
| 213 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 214 |
$id, |
| 215 |
'random', |
| 216 |
['random', 'user'] |
| 217 |
); |
| 218 |
|
| 219 |
$id = 'download_type'; |
| 220 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 221 |
$id, |
| 222 |
'fopen', |
| 223 |
['fopen', 'normal'] |
| 224 |
); |
| 225 |
|
| 226 |
$id = 'lock_file_types'; |
| 227 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 228 |
$id, |
| 229 |
'all', |
| 230 |
['all', 'selected', 'not_selected'] |
| 231 |
); |
| 232 |
|
| 233 |
$id = 'locked_file_types'; |
| 234 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 235 |
$id, |
| 236 |
'zip,rar,tar,gz' |
| 237 |
); |
| 238 |
|
| 239 |
$id = 'not_locked_file_types'; |
| 240 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 241 |
$id, |
| 242 |
'gif,jpg,jpeg,png' |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the default config parameters settings |
| 248 |
* |
| 249 |
* @return ConfigParameter[] |
| 250 |
*/ |
| 251 |
protected function getDefaultConfigParameters() |
| 252 |
{ |
| 253 |
if ($this->defaultConfigParameters === []) { |
| 254 |
/** |
| 255 |
* @var ConfigParameter[] $configParameters |
| 256 |
*/ |
| 257 |
$configParameters = []; |
| 258 |
|
| 259 |
$this->addDefaultGeneralConfigParameters($configParameters); |
| 260 |
$this->addDefaultPostConfigParameters($configParameters); |
| 261 |
$this->addDefaultTaxonomyConfigParameters($configParameters); |
| 262 |
$this->addDefaultFileConfigParameters($configParameters); |
| 263 |
|
| 264 |
$this->defaultConfigParameters = $configParameters; |
| 265 |
} |
| 266 |
|
| 267 |
return $this->defaultConfigParameters; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Returns the object parameter name. |
| 272 |
* |
| 273 |
* @param string $objectType |
| 274 |
* @param string $rawParameterName |
| 275 |
* |
| 276 |
* @return ConfigParameter|null |
| 277 |
*/ |
| 278 |
private function getObjectParameter($objectType, $rawParameterName) |
| 279 |
{ |
| 280 |
$options = $this->getConfigParameters(); |
| 281 |
$parameterName = sprintf($rawParameterName, $objectType); |
| 282 |
|
| 283 |
if (isset($options[$parameterName]) === false |
| 284 |
|| isset($options["{$objectType}_use_default"]) === true |
| 285 |
&& $options["{$objectType}_use_default"]->getValue() === true |
| 286 |
) { |
| 287 |
$parameterName = sprintf($rawParameterName, self::DEFAULT_TYPE); |
| 288 |
} |
| 289 |
|
| 290 |
return (isset($options[$parameterName]) === true) ? $options[$parameterName] : null; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Returns the option value if the option exists otherwise true. |
| 295 |
* |
| 296 |
* @param string $objectType |
| 297 |
* @param string $parameterName |
| 298 |
* |
| 299 |
* @return bool |
| 300 |
*/ |
| 301 |
private function hideObject($objectType, $parameterName) |
| 302 |
{ |
| 303 |
$parameter = $this->getObjectParameter($objectType, $parameterName); |
| 304 |
return ($parameter !== null) ? $parameter->getValue() : true; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Returns the option value if the option exists otherwise an empty string. |
| 309 |
* |
| 310 |
* @param string $objectType |
| 311 |
* @param string $parameterName |
| 312 |
* |
| 313 |
* @return string |
| 314 |
*/ |
| 315 |
private function getObjectContent($objectType, $parameterName) |
| 316 |
{ |
| 317 |
$parameter = $this->getObjectParameter($objectType, $parameterName); |
| 318 |
return ($parameter !== null) ? $parameter->getValue() : ''; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* @param string $postType |
| 323 |
* |
| 324 |
* @return bool |
| 325 |
*/ |
| 326 |
public function hidePostType($postType) |
| 327 |
{ |
| 328 |
return $this->hideObject($postType, 'hide_%s'); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* @param string $postType |
| 333 |
* |
| 334 |
* @return bool |
| 335 |
*/ |
| 336 |
public function hidePostTypeTitle($postType) |
| 337 |
{ |
| 338 |
return $this->hideObject($postType, 'hide_%s_title'); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* @param string $postType |
| 343 |
* |
| 344 |
* @return bool |
| 345 |
*/ |
| 346 |
public function hidePostTypeComments($postType) |
| 347 |
{ |
| 348 |
return $this->hideObject($postType, 'hide_%s_comment'); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* @param string $postType |
| 353 |
* |
| 354 |
* @return bool |
| 355 |
*/ |
| 356 |
public function lockPostTypeComments($postType) |
| 357 |
{ |
| 358 |
return $this->hideObject($postType, '%s_comments_locked'); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* @param string $postType |
| 363 |
* |
| 364 |
* @return string |
| 365 |
*/ |
| 366 |
public function getPostTypeTitle($postType) |
| 367 |
{ |
| 368 |
return $this->getObjectContent($postType, '%s_title'); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* @param string $postType |
| 373 |
* |
| 374 |
* @return string |
| 375 |
*/ |
| 376 |
public function getPostTypeContent($postType) |
| 377 |
{ |
| 378 |
return $this->getObjectContent($postType, '%s_content'); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* @param string $postType |
| 383 |
* |
| 384 |
* @return string |
| 385 |
*/ |
| 386 |
public function getPostTypeCommentContent($postType) |
| 387 |
{ |
| 388 |
return $this->getObjectContent($postType, '%s_comment_content'); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* @return string |
| 393 |
*/ |
| 394 |
public function getRedirect() |
| 395 |
{ |
| 396 |
return $this->getParameterValue('redirect'); |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* @return string |
| 401 |
*/ |
| 402 |
public function getRedirectCustomPage() |
| 403 |
{ |
| 404 |
return $this->getParameterValue('redirect_custom_page'); |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* @return string |
| 409 |
*/ |
| 410 |
public function getRedirectCustomUrl() |
| 411 |
{ |
| 412 |
return $this->getParameterValue('redirect_custom_url'); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* @return bool |
| 417 |
*/ |
| 418 |
public function lockRecursive() |
| 419 |
{ |
| 420 |
return $this->getParameterValue('lock_recursive'); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* @return bool |
| 425 |
*/ |
| 426 |
public function authorsHasAccessToOwn() |
| 427 |
{ |
| 428 |
return $this->getParameterValue('authors_has_access_to_own'); |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* @return bool |
| 433 |
*/ |
| 434 |
public function authorsCanAddPostsToGroups() |
| 435 |
{ |
| 436 |
return $this->getParameterValue('authors_can_add_posts_to_groups'); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* @return bool |
| 441 |
*/ |
| 442 |
public function lockFile() |
| 443 |
{ |
| 444 |
return $this->getParameterValue('lock_file'); |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* @return string |
| 449 |
*/ |
| 450 |
public function getFilePassType() |
| 451 |
{ |
| 452 |
return $this->getParameterValue('file_pass_type'); |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* @return string |
| 457 |
*/ |
| 458 |
public function getLockFileTypes() |
| 459 |
{ |
| 460 |
return $this->getParameterValue('lock_file_types'); |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* @return string |
| 465 |
*/ |
| 466 |
public function getDownloadType() |
| 467 |
{ |
| 468 |
return $this->getParameterValue('download_type'); |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* @return string |
| 473 |
*/ |
| 474 |
public function getLockedFileTypes() |
| 475 |
{ |
| 476 |
return $this->getParameterValue('locked_file_types'); |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* @return string |
| 481 |
*/ |
| 482 |
public function getNotLockedFileTypes() |
| 483 |
{ |
| 484 |
return $this->getParameterValue('not_locked_file_types'); |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* @return bool |
| 489 |
*/ |
| 490 |
public function blogAdminHint() |
| 491 |
{ |
| 492 |
return $this->getParameterValue('blog_admin_hint'); |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* @return string |
| 497 |
*/ |
| 498 |
public function getBlogAdminHintText() |
| 499 |
{ |
| 500 |
return $this->getParameterValue('blog_admin_hint_text'); |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* @return string |
| 505 |
*/ |
| 506 |
public function showAssignedGroups() |
| 507 |
{ |
| 508 |
return $this->getParameterValue('show_assigned_groups'); |
| 509 |
} |
| 510 |
|
| 511 |
/** |
| 512 |
* @param string $taxonomy |
| 513 |
* |
| 514 |
* @return bool |
| 515 |
*/ |
| 516 |
public function hideEmptyTaxonomy($taxonomy) |
| 517 |
{ |
| 518 |
$parameter = $this->getObjectParameter($taxonomy, 'hide_empty_%s'); |
| 519 |
return ($parameter !== null) ? $parameter->getValue() : false; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* @return bool |
| 524 |
*/ |
| 525 |
public function protectFeed() |
| 526 |
{ |
| 527 |
return $this->getParameterValue('protect_feed'); |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* @return bool |
| 532 |
*/ |
| 533 |
public function showPostContentBeforeMore() |
| 534 |
{ |
| 535 |
return $this->getParameterValue('show_post_content_before_more'); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* @return string |
| 540 |
*/ |
| 541 |
public function getFullAccessRole() |
| 542 |
{ |
| 543 |
return $this->getParameterValue('full_access_role'); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* @return null|string |
| 548 |
*/ |
| 549 |
public function getActiveCacheProvider() |
| 550 |
{ |
| 551 |
return $this->getParameterValue('active_cache_provider'); |
| 552 |
} |
| 553 |
} |
| 554 |
|