PluginProbe
User Access Manager / 2.2.1
User Access Manager v2.2.1
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.1, at src/Config/MainConfig.php

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