PluginProbe
User Access Manager / 2.2.19
User Access Manager v2.2.19
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Config / MainConfig.php

MainConfig.php in User Access Manager 2.2.19, at src/Config/MainConfig.php

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