| 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\ObjectHandler\ObjectHandler; |
| 18 |
use UserAccessManager\Wrapper\Wordpress; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Config |
| 22 |
* |
| 23 |
* @package UserAccessManager\Config |
| 24 |
*/ |
| 25 |
class Config |
| 26 |
{ |
| 27 |
const ADMIN_OPTIONS_NAME = 'uamAdminOptions'; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var Wordpress |
| 31 |
*/ |
| 32 |
private $wordpress; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var ObjectHandler |
| 36 |
*/ |
| 37 |
private $objectHandler; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var ConfigParameterFactory |
| 41 |
*/ |
| 42 |
private $configParameterFactory; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $baseFile; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var null|array |
| 51 |
*/ |
| 52 |
private $configParameters = null; |
| 53 |
|
| 54 |
/** |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
private $wpOptions = []; |
| 58 |
|
| 59 |
/** |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
private $mimeTypes = null; |
| 63 |
|
| 64 |
/** |
| 65 |
* Config constructor. |
| 66 |
* |
| 67 |
* @param Wordpress $wordpress |
| 68 |
* @param ObjectHandler $objectHandler |
| 69 |
* @param ConfigParameterFactory $configParameterFactory |
| 70 |
* @param String $baseFile |
| 71 |
*/ |
| 72 |
public function __construct( |
| 73 |
Wordpress $wordpress, |
| 74 |
ObjectHandler $objectHandler, |
| 75 |
ConfigParameterFactory $configParameterFactory, |
| 76 |
$baseFile |
| 77 |
) { |
| 78 |
$this->wordpress = $wordpress; |
| 79 |
$this->objectHandler = $objectHandler; |
| 80 |
$this->configParameterFactory = $configParameterFactory; |
| 81 |
$this->baseFile = $baseFile; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns the WordPress options. |
| 86 |
* |
| 87 |
* @param string $option |
| 88 |
* |
| 89 |
* @return mixed |
| 90 |
*/ |
| 91 |
public function getWpOption($option) |
| 92 |
{ |
| 93 |
if (!isset($this->wpOptions[$option]) === true) { |
| 94 |
$this->wpOptions[$option] = $this->wordpress->getOption($option); |
| 95 |
} |
| 96 |
|
| 97 |
return $this->wpOptions[$option]; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Flushes the config parameters. |
| 102 |
*/ |
| 103 |
public function flushConfigParameters() |
| 104 |
{ |
| 105 |
$this->configParameters = null; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns the current settings |
| 110 |
* |
| 111 |
* @return ConfigParameter[] |
| 112 |
*/ |
| 113 |
public function getConfigParameters() |
| 114 |
{ |
| 115 |
if ($this->configParameters === null) { |
| 116 |
/** |
| 117 |
* @var ConfigParameter[] $configParameters |
| 118 |
*/ |
| 119 |
$configParameters = []; |
| 120 |
|
| 121 |
$postTypes = $this->objectHandler->getPostTypes(); |
| 122 |
|
| 123 |
foreach ($postTypes as $postType) { |
| 124 |
if ($postType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
$id = "hide_{$postType}"; |
| 129 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 130 |
|
| 131 |
$id = "hide_{$postType}_title"; |
| 132 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 133 |
|
| 134 |
$id = "{$postType}_title"; |
| 135 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 136 |
$id, |
| 137 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS |
| 138 |
); |
| 139 |
|
| 140 |
$id = "{$postType}_content"; |
| 141 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 142 |
$id, |
| 143 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_ENTRY |
| 144 |
); |
| 145 |
|
| 146 |
$id = "hide_{$postType}_comment"; |
| 147 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 148 |
|
| 149 |
$id = "{$postType}_comment_content"; |
| 150 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 151 |
$id, |
| 152 |
TXT_UAM_SETTING_DEFAULT_NO_RIGHTS_FOR_COMMENTS |
| 153 |
); |
| 154 |
|
| 155 |
$id = "{$postType}_comments_locked"; |
| 156 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 157 |
|
| 158 |
if ($postType === 'post') { |
| 159 |
$id = "show_{$postType}_content_before_more"; |
| 160 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$id = 'redirect'; |
| 165 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 166 |
$id, |
| 167 |
'false', |
| 168 |
['false', 'custom_page', 'custom_url'] |
| 169 |
); |
| 170 |
|
| 171 |
$id = 'redirect_custom_page'; |
| 172 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 173 |
|
| 174 |
$id = 'redirect_custom_url'; |
| 175 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id); |
| 176 |
|
| 177 |
$id = 'lock_recursive'; |
| 178 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 179 |
|
| 180 |
$id = 'authors_has_access_to_own'; |
| 181 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 182 |
|
| 183 |
|
| 184 |
$id = 'authors_can_add_posts_to_groups'; |
| 185 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 186 |
|
| 187 |
$id = 'lock_file'; |
| 188 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id); |
| 189 |
|
| 190 |
$id = 'file_pass_type'; |
| 191 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 192 |
$id, |
| 193 |
'random', |
| 194 |
['random', 'user'] |
| 195 |
); |
| 196 |
|
| 197 |
$id = 'download_type'; |
| 198 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 199 |
$id, |
| 200 |
'fopen', |
| 201 |
['fopen', 'normal'] |
| 202 |
); |
| 203 |
|
| 204 |
$id = 'lock_file_types'; |
| 205 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 206 |
$id, |
| 207 |
'all', |
| 208 |
['all', 'selected', 'not_selected'] |
| 209 |
); |
| 210 |
|
| 211 |
$id = 'locked_file_types'; |
| 212 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 213 |
$id, |
| 214 |
'zip,rar,tar,gz' |
| 215 |
); |
| 216 |
|
| 217 |
$id = 'not_locked_file_types'; |
| 218 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter( |
| 219 |
$id, |
| 220 |
'gif,jpg,jpeg,png' |
| 221 |
); |
| 222 |
|
| 223 |
$id = 'blog_admin_hint'; |
| 224 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 225 |
|
| 226 |
$id = 'blog_admin_hint_text'; |
| 227 |
$configParameters[$id] = $this->configParameterFactory->createStringConfigParameter($id, '[L]'); |
| 228 |
|
| 229 |
$taxonomies = $this->objectHandler->getTaxonomies(); |
| 230 |
|
| 231 |
foreach ($taxonomies as $taxonomy) { |
| 232 |
$id = 'hide_empty_'.$taxonomy; |
| 233 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 234 |
} |
| 235 |
|
| 236 |
$id = 'protect_feed'; |
| 237 |
$configParameters[$id] = $this->configParameterFactory->createBooleanConfigParameter($id, true); |
| 238 |
|
| 239 |
$id = 'full_access_role'; |
| 240 |
$configParameters[$id] = $this->configParameterFactory->createSelectionConfigParameter( |
| 241 |
$id, |
| 242 |
'administrator', |
| 243 |
['administrator', 'editor', 'author', 'contributor', 'subscriber'] |
| 244 |
); |
| 245 |
|
| 246 |
$currentOptions = (array)$this->getWpOption(self::ADMIN_OPTIONS_NAME); |
| 247 |
|
| 248 |
foreach ($currentOptions as $key => $option) { |
| 249 |
if (isset($configParameters[$key])) { |
| 250 |
$configParameters[$key]->setValue($option); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
$this->configParameters = $configParameters; |
| 255 |
} |
| 256 |
|
| 257 |
return $this->configParameters; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Sets the new config parameters and saves them to the database. |
| 262 |
* |
| 263 |
* @param $rawParameters |
| 264 |
*/ |
| 265 |
public function setConfigParameters(array $rawParameters) |
| 266 |
{ |
| 267 |
$configParameters = $this->getConfigParameters(); |
| 268 |
|
| 269 |
foreach ($rawParameters as $key => $value) { |
| 270 |
if (isset($configParameters[$key]) === true) { |
| 271 |
$configParameters[$key]->setValue($value); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
$this->configParameters = $configParameters; |
| 276 |
|
| 277 |
$simpleConfigParameters = []; |
| 278 |
|
| 279 |
foreach ($configParameters as $parameter) { |
| 280 |
$simpleConfigParameters[$parameter->getId()] = $parameter->getValue(); |
| 281 |
} |
| 282 |
|
| 283 |
$this->wordpress->updateOption(self::ADMIN_OPTIONS_NAME, $simpleConfigParameters); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Returns the requested parameter value |
| 288 |
* |
| 289 |
* @param string $parameterName |
| 290 |
* |
| 291 |
* @return mixed |
| 292 |
* |
| 293 |
* @throws \Exception |
| 294 |
*/ |
| 295 |
private function getParameterValue($parameterName) |
| 296 |
{ |
| 297 |
$options = $this->getConfigParameters(); |
| 298 |
|
| 299 |
if (isset($options[$parameterName]) === false) { |
| 300 |
throw new \Exception("Unknown config parameter '{$parameterName}'."); |
| 301 |
} |
| 302 |
|
| 303 |
return $options[$parameterName]->getValue(); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Returns true if a user is at the admin panel. |
| 308 |
* |
| 309 |
* @return bool |
| 310 |
*/ |
| 311 |
public function atAdminPanel() |
| 312 |
{ |
| 313 |
return $this->wordpress->isAdmin(); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns true if permalinks are active otherwise false. |
| 318 |
* |
| 319 |
* @return bool |
| 320 |
*/ |
| 321 |
public function isPermalinksActive() |
| 322 |
{ |
| 323 |
$permalinkStructure = $this->getWpOption('permalink_structure'); |
| 324 |
return (empty($permalinkStructure) === false); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Returns the upload directory. |
| 329 |
* |
| 330 |
* @return null|string |
| 331 |
*/ |
| 332 |
public function getUploadDirectory() |
| 333 |
{ |
| 334 |
$wordpressUploadDir = $this->wordpress->getUploadDir(); |
| 335 |
|
| 336 |
if (empty($wordpressUploadDir['error'])) { |
| 337 |
return $wordpressUploadDir['basedir'].DIRECTORY_SEPARATOR; |
| 338 |
} |
| 339 |
|
| 340 |
return null; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Returns the full supported mine types. |
| 345 |
* |
| 346 |
* @return array |
| 347 |
*/ |
| 348 |
public function getMimeTypes() |
| 349 |
{ |
| 350 |
if ($this->mimeTypes === null) { |
| 351 |
$mimeTypes = $this->wordpress->getAllowedMimeTypes(); |
| 352 |
$fullMimeTypes = []; |
| 353 |
|
| 354 |
foreach ($mimeTypes as $extensions => $mineType) { |
| 355 |
$extensions = explode('|', $extensions); |
| 356 |
|
| 357 |
foreach ($extensions as $extension) { |
| 358 |
$fullMimeTypes[$extension] = $mineType; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
$this->mimeTypes = $fullMimeTypes; |
| 363 |
} |
| 364 |
|
| 365 |
return $this->mimeTypes; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Returns the module url path. |
| 370 |
* |
| 371 |
* @return string |
| 372 |
*/ |
| 373 |
public function getUrlPath() |
| 374 |
{ |
| 375 |
return $this->wordpress->pluginsUrl('', $this->baseFile).DIRECTORY_SEPARATOR; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Returns the module real path. |
| 380 |
* |
| 381 |
* @return string |
| 382 |
*/ |
| 383 |
public function getRealPath() |
| 384 |
{ |
| 385 |
$dirName = dirname($this->baseFile); |
| 386 |
|
| 387 |
return $this->wordpress->getPluginDir().DIRECTORY_SEPARATOR |
| 388 |
.$this->wordpress->pluginBasename($dirName).DIRECTORY_SEPARATOR; |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Returns the option value if the option exists otherwise true. |
| 393 |
* |
| 394 |
* @param string $parameterName |
| 395 |
* |
| 396 |
* @return bool |
| 397 |
*/ |
| 398 |
private function hideObject($parameterName) |
| 399 |
{ |
| 400 |
$options = $this->getConfigParameters(); |
| 401 |
|
| 402 |
if (isset($options[$parameterName]) === true) { |
| 403 |
return $options[$parameterName]->getValue(); |
| 404 |
} |
| 405 |
|
| 406 |
return true; |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* @param string $postType |
| 411 |
* |
| 412 |
* @return bool |
| 413 |
*/ |
| 414 |
public function hidePostType($postType) |
| 415 |
{ |
| 416 |
return $this->hideObject('hide_'.$postType); |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* @param string $postType |
| 421 |
* |
| 422 |
* @return bool |
| 423 |
*/ |
| 424 |
public function hidePostTypeTitle($postType) |
| 425 |
{ |
| 426 |
return $this->hideObject('hide_'.$postType.'_title'); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* @param string $postType |
| 431 |
* |
| 432 |
* @return bool |
| 433 |
*/ |
| 434 |
public function hidePostTypeComments($postType) |
| 435 |
{ |
| 436 |
return $this->hideObject($postType.'_comments_locked'); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* @param string $postType |
| 441 |
* |
| 442 |
* @return string |
| 443 |
*/ |
| 444 |
public function getPostTypeTitle($postType) |
| 445 |
{ |
| 446 |
return $this->getParameterValue($postType.'_title'); |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* @param string $postType |
| 451 |
* |
| 452 |
* @return string |
| 453 |
*/ |
| 454 |
public function getPostTypeContent($postType) |
| 455 |
{ |
| 456 |
return $this->getParameterValue($postType.'_content'); |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* @param string $postType |
| 461 |
* |
| 462 |
* @return string |
| 463 |
*/ |
| 464 |
public function getPostTypeCommentContent($postType) |
| 465 |
{ |
| 466 |
return $this->getParameterValue($postType.'_comment_content'); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* @return string |
| 471 |
*/ |
| 472 |
public function getRedirect() |
| 473 |
{ |
| 474 |
return $this->getParameterValue('redirect'); |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* @return string |
| 479 |
*/ |
| 480 |
public function getRedirectCustomPage() |
| 481 |
{ |
| 482 |
return $this->getParameterValue('redirect_custom_page'); |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* @return string |
| 487 |
*/ |
| 488 |
public function getRedirectCustomUrl() |
| 489 |
{ |
| 490 |
return $this->getParameterValue('redirect_custom_url'); |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* @return bool |
| 495 |
*/ |
| 496 |
public function lockRecursive() |
| 497 |
{ |
| 498 |
return $this->getParameterValue('lock_recursive'); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* @return bool |
| 503 |
*/ |
| 504 |
public function authorsHasAccessToOwn() |
| 505 |
{ |
| 506 |
return $this->getParameterValue('authors_has_access_to_own'); |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* @return bool |
| 511 |
*/ |
| 512 |
public function authorsCanAddPostsToGroups() |
| 513 |
{ |
| 514 |
return $this->getParameterValue('authors_can_add_posts_to_groups'); |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* @return bool |
| 519 |
*/ |
| 520 |
public function lockFile() |
| 521 |
{ |
| 522 |
return $this->getParameterValue('lock_file'); |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* @return string |
| 527 |
*/ |
| 528 |
public function getFilePassType() |
| 529 |
{ |
| 530 |
return $this->getParameterValue('file_pass_type'); |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* @return string |
| 535 |
*/ |
| 536 |
public function getLockFileTypes() |
| 537 |
{ |
| 538 |
return $this->getParameterValue('lock_file_types'); |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* @return string |
| 543 |
*/ |
| 544 |
public function getDownloadType() |
| 545 |
{ |
| 546 |
return $this->getParameterValue('download_type'); |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* @return string |
| 551 |
*/ |
| 552 |
public function getLockedFileTypes() |
| 553 |
{ |
| 554 |
return $this->getParameterValue('locked_file_types'); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* @return string |
| 559 |
*/ |
| 560 |
public function getNotLockedFileTypes() |
| 561 |
{ |
| 562 |
return $this->getParameterValue('not_locked_file_types'); |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* @return bool |
| 567 |
*/ |
| 568 |
public function blogAdminHint() |
| 569 |
{ |
| 570 |
return $this->getParameterValue('blog_admin_hint'); |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* @return string |
| 575 |
*/ |
| 576 |
public function getBlogAdminHintText() |
| 577 |
{ |
| 578 |
return $this->getParameterValue('blog_admin_hint_text'); |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* @param string $taxonomy |
| 583 |
* |
| 584 |
* @return bool |
| 585 |
*/ |
| 586 |
public function hideEmptyTaxonomy($taxonomy) |
| 587 |
{ |
| 588 |
return $this->hideObject('hide_empty_'.$taxonomy); |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* @return bool |
| 593 |
*/ |
| 594 |
public function protectFeed() |
| 595 |
{ |
| 596 |
return $this->getParameterValue('protect_feed'); |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* @return bool |
| 601 |
*/ |
| 602 |
public function showPostContentBeforeMore() |
| 603 |
{ |
| 604 |
return $this->getParameterValue('show_post_content_before_more'); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* @return string |
| 609 |
*/ |
| 610 |
public function getFullAccessRole() |
| 611 |
{ |
| 612 |
return $this->getParameterValue('full_access_role'); |
| 613 |
} |
| 614 |
} |
| 615 |
|