PluginProbe
User Access Manager / 2.1.3
User Access Manager v2.1.3
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 / Controller / Backend / SettingsController.php

SettingsController.php in User Access Manager 2.1.3, at src/Controller/Backend/SettingsController.php

600 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SettingsController.php
4 *
5 * The SettingsController 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\Controller\Backend;
16
17 use UserAccessManager\Cache\Cache;
18 use UserAccessManager\Config\ConfigParameter;
19 use UserAccessManager\Config\MainConfig;
20 use UserAccessManager\Config\WordpressConfig;
21 use UserAccessManager\Controller\Controller;
22 use UserAccessManager\File\FileHandler;
23 use UserAccessManager\Form\FormFactory;
24 use UserAccessManager\Form\FormHelper;
25 use UserAccessManager\Form\MultipleFormElementValue;
26 use UserAccessManager\Object\ObjectHandler;
27 use UserAccessManager\Wrapper\Php;
28 use UserAccessManager\Wrapper\Wordpress;
29
30 class SettingsController extends Controller
31 {
32 use ControllerTabNavigationTrait;
33
34 const GROUP_POST_TYPES = 'post_types';
35 const GROUP_TAXONOMIES = 'taxonomies';
36 const GROUP_FILES = 'file';
37 const SECTION_FILES = 'file';
38 const GROUP_AUTHOR = 'author';
39 const SECTION_AUTHOR = 'author';
40 const GROUP_CACHE = 'cache';
41 const GROUP_OTHER = 'other';
42 const SECTION_OTHER = 'other';
43
44 /**
45 * @var MainConfig
46 */
47 private $mainConfig;
48
49 /**
50 * @var string
51 */
52 protected $template = 'AdminSettings.php';
53
54 /**
55 * @var Cache
56 */
57 private $cache;
58
59 /**
60 * @var ObjectHandler
61 */
62 private $objectHandler;
63
64 /**
65 * @var FileHandler
66 */
67 private $fileHandler;
68
69 /**
70 * @var FormFactory
71 */
72 private $formFactory;
73
74 /**
75 * @var FormHelper
76 */
77 private $formHelper;
78
79 /**
80 * SettingsController constructor.
81 *
82 * @param Php $php
83 * @param Wordpress $wordpress
84 * @param WordpressConfig $wordpressConfig
85 * @param MainConfig $mainConfig
86 * @param Cache $cache
87 * @param ObjectHandler $objectHandler
88 * @param FileHandler $fileHandler
89 * @param FormFactory $formFactory
90 * @param FormHelper $formHelper
91 */
92 public function __construct(
93 Php $php,
94 Wordpress $wordpress,
95 WordpressConfig $wordpressConfig,
96 MainConfig $mainConfig,
97 Cache $cache,
98 ObjectHandler $objectHandler,
99 FileHandler $fileHandler,
100 FormFactory $formFactory,
101 FormHelper $formHelper
102 ) {
103 parent::__construct($php, $wordpress, $wordpressConfig);
104 $this->mainConfig = $mainConfig;
105 $this->cache = $cache;
106 $this->objectHandler = $objectHandler;
107 $this->fileHandler = $fileHandler;
108 $this->formFactory = $formFactory;
109 $this->formHelper = $formHelper;
110 }
111
112 /**
113 * Returns the tab groups.
114 *
115 * @return array
116 */
117 public function getTabGroups()
118 {
119 $activeCacheProvider = $this->mainConfig->getActiveCacheProvider();
120 $cacheProviderSections = [$activeCacheProvider];
121 $cacheProviders = $this->cache->getRegisteredCacheProviders();
122
123 foreach ($cacheProviders as $cacheProvider) {
124 if ($cacheProvider->getId() !== $activeCacheProvider) {
125 $cacheProviderSections[] = $cacheProvider->getId();
126 }
127 }
128
129 return [
130 self::GROUP_POST_TYPES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getPostTypes())),
131 self::GROUP_TAXONOMIES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getTaxonomies())),
132 self::GROUP_FILES => [self::SECTION_FILES],
133 self::GROUP_AUTHOR => [self::SECTION_AUTHOR],
134 self::GROUP_CACHE => $cacheProviderSections,
135 self::GROUP_OTHER => [self::SECTION_OTHER]
136 ];
137 }
138
139 /**
140 * Returns true if the server is a nginx server.
141 *
142 * @return bool
143 */
144 public function isNginx()
145 {
146 return $this->wordpress->isNginx();
147 }
148
149 /**
150 * Returns the pages.
151 *
152 * @return array
153 */
154 private function getPages()
155 {
156 $pages = $this->wordpress->getPages('sort_column=menu_order');
157 return is_array($pages) !== false ? $pages : [];
158 }
159
160 /**
161 * Returns the post types as object.
162 *
163 * @return \WP_Post_Type[]
164 */
165 private function getPostTypes()
166 {
167 return $this->wordpress->getPostTypes(['public' => true], 'objects');
168 }
169
170 /**
171 * Returns the taxonomies as objects.
172 *
173 * @return \WP_Taxonomy[]
174 */
175 private function getTaxonomies()
176 {
177 return $this->wordpress->getTaxonomies(['public' => true], 'objects');
178 }
179
180 /**
181 * @param string $key
182 * @param bool $description
183 *
184 * @return string
185 */
186 public function getText($key, $description = false)
187 {
188 return $this->formHelper->getText($key, $description);
189 }
190
191 /**
192 * @param string $key
193 *
194 * @return string
195 */
196 public function getGroupText($key)
197 {
198 return $this->getText($key);
199 }
200
201 /**
202 * @param $key
203 *
204 * @return string
205 */
206 public function getGroupSectionText($key)
207 {
208 return ($key === MainConfig::DEFAULT_TYPE) ?
209 TXT_UAM_SETTINGS_GROUP_SECTION_DEFAULT : $this->getObjectName($key);
210 }
211
212 /**
213 * Returns the object name.
214 *
215 * @param string $objectKey
216 *
217 * @return string
218 */
219 public function getObjectName($objectKey)
220 {
221 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
222 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
223
224 return (isset($objects[$objectKey]) === true) ? $objects[$objectKey]->labels->name : $objectKey;
225 }
226
227 /**
228 * Returns the post settings form.
229 *
230 * @param string $postType
231 *
232 * @return \UserAccessManager\Form\Form
233 */
234 private function getPostSettingsForm($postType = MainConfig::DEFAULT_TYPE)
235 {
236 $textarea = null;
237 $configParameters = $this->mainConfig->getConfigParameters();
238
239 if (isset($configParameters["{$postType}_content"]) === true) {
240 $configParameter = $configParameters["{$postType}_content"];
241 $textarea = $this->formFactory->createTextarea(
242 $configParameter->getId(),
243 $configParameter->getValue(),
244 $this->formHelper->getParameterText($configParameter, false, $postType),
245 $this->formHelper->getParameterText($configParameter, true, $postType)
246 );
247 }
248
249 $parameters = ($postType !== MainConfig::DEFAULT_TYPE) ? ["{$postType}_use_default"] : [];
250 $parameters = array_merge($parameters, [
251 "hide_{$postType}",
252 "hide_{$postType}_title",
253 "{$postType}_title",
254 $textarea,
255 "hide_{$postType}_comment",
256 "{$postType}_comment_content",
257 "{$postType}_comments_locked"
258 ]);
259
260 if ($postType === 'post') {
261 $parameters[] = "show_{$postType}_content_before_more";
262 }
263
264 return $this->formHelper->getSettingsForm($parameters, $postType);
265 }
266
267 /**
268 * Returns the taxonomy settings form.
269 *
270 * @param string $taxonomy
271 *
272 * @return \UserAccessManager\Form\Form
273 */
274 private function getTaxonomySettingsForm($taxonomy = MainConfig::DEFAULT_TYPE)
275 {
276 $parameters = ($taxonomy !== MainConfig::DEFAULT_TYPE) ? ["{$taxonomy}_use_default"] : [];
277 $parameters = array_merge($parameters, [
278 "hide_empty_{$taxonomy}"
279 ]);
280
281 return $this->formHelper->getSettingsForm($parameters, $taxonomy);
282 }
283
284 /**
285 * Creates and returns the locked file types element.
286 *
287 * @param ConfigParameter $parameter
288 * @param string $value
289 * @param string $label
290 *
291 * @return MultipleFormElementValue
292 */
293 private function createMultipleFromElement(ConfigParameter $parameter, $value, $label)
294 {
295 $selectedValue = $this->formFactory->createMultipleFormElementValue($value, $label);
296 $convertedParameter = $this->formHelper->convertConfigParameter($parameter);
297
298 if ($convertedParameter !== null) {
299 $selectedValue->setSubElement($convertedParameter);
300 }
301
302 return $selectedValue;
303 }
304
305 /**
306 * Returns the files settings form.
307 *
308 * @return \UserAccessManager\Form\Form
309 */
310 private function getFilesSettingsForm()
311 {
312 $parameters = [
313 'lock_file',
314 'download_type'
315 ];
316
317 $configParameters = $this->mainConfig->getConfigParameters();
318
319 if (isset($configParameters['lock_file_types']) === true) {
320 $values = [
321 $this->formFactory->createMultipleFormElementValue('all', TXT_UAM_ALL)
322 ];
323
324 if (isset($configParameters['locked_file_types']) === true) {
325 $values[] = $this->createMultipleFromElement(
326 $configParameters['locked_file_types'],
327 'selected',
328 TXT_UAM_LOCKED_FILE_TYPES
329 );
330 }
331
332 if ($this->wordpress->isNginx() === false
333 && isset($configParameters['not_locked_file_types']) === true
334 ) {
335 $values[] = $this->createMultipleFromElement(
336 $configParameters['not_locked_file_types'],
337 'not_selected',
338 TXT_UAM_NOT_LOCKED_FILE_TYPES
339 );
340 }
341
342 $configParameter = $configParameters['lock_file_types'];
343
344 $lockFileTypes = $this->formFactory->createRadio(
345 $configParameter->getId(),
346 $values,
347 $configParameter->getValue(),
348 TXT_UAM_LOCK_FILE_TYPES,
349 TXT_UAM_LOCK_FILE_TYPES_DESC
350 );
351 $parameters[] = $lockFileTypes;
352 }
353
354 $parameters[] = 'file_pass_type';
355
356 return $this->formHelper->getSettingsForm($parameters);
357 }
358
359 /**
360 * Returns the author settings form.
361 *
362 * @return \UserAccessManager\Form\Form
363 */
364 private function getAuthorSettingsForm()
365 {
366 $parameters = [
367 'authors_has_access_to_own',
368 'authors_can_add_posts_to_groups',
369 'full_access_role'
370 ];
371
372 return $this->formHelper->getSettingsForm($parameters);
373 }
374
375 /**
376 * Returns the author settings form.
377 *
378 * @return \UserAccessManager\Form\Form
379 */
380 private function getOtherSettingsForm()
381 {
382 $redirect = null;
383 $configParameters = $this->mainConfig->getConfigParameters();
384
385 if (isset($configParameters['redirect'])) {
386 $values = [
387 $this->formFactory->createMultipleFormElementValue('false', TXT_UAM_NO),
388 $this->formFactory->createMultipleFormElementValue('blog', TXT_UAM_REDIRECT_TO_BLOG),
389 ];
390
391 if (isset($configParameters['redirect_custom_page']) === true) {
392 $redirectCustomPage = $configParameters['redirect_custom_page'];
393 $redirectCustomPageValue = $this->formFactory->createMultipleFormElementValue(
394 'selected',
395 TXT_UAM_REDIRECT_TO_PAGE
396 );
397
398 $possibleValues = [];
399 $pages = $this->getPages();
400
401 foreach ($pages as $page) {
402 $possibleValues[] = $this->formFactory->createValueSetFromElementValue(
403 (int)$page->ID,
404 $page->post_title
405 );
406 }
407
408 $formElement = $this->formFactory->createSelect(
409 $redirectCustomPage->getId(),
410 $possibleValues,
411 (int)$redirectCustomPage->getValue()
412 );
413
414 $redirectCustomPageValue->setSubElement($formElement);
415 $values[] = $redirectCustomPageValue;
416 }
417
418 if (isset($configParameters['redirect_custom_url']) === true) {
419 $values[] = $this->createMultipleFromElement(
420 $configParameters['redirect_custom_url'],
421 'custom_url',
422 TXT_UAM_REDIRECT_TO_URL
423 );
424 }
425
426 $configParameter = $configParameters['redirect'];
427
428 $redirect = $this->formFactory->createRadio(
429 $configParameter->getId(),
430 $values,
431 $configParameter->getValue(),
432 TXT_UAM_REDIRECT,
433 TXT_UAM_REDIRECT_DESC
434 );
435 }
436
437 $parameters = [
438 'lock_recursive',
439 'protect_feed',
440 $redirect,
441 'blog_admin_hint',
442 'blog_admin_hint_text',
443 'show_assigned_groups'
444 ];
445
446 return $this->formHelper->getSettingsForm($parameters);
447 }
448
449 /**
450 * Returns the full settings from.
451 *
452 * @param array $types
453 * @param array $ignoredTypes
454 * @param Callable $formFunction
455 *
456 * @return array
457 */
458 private function getFullSettingsFrom(array $types, array $ignoredTypes, $formFunction)
459 {
460 $groupForms = [];
461 $groupForms[MainConfig::DEFAULT_TYPE] = $formFunction();
462
463 foreach ($ignoredTypes as $ignoredType) {
464 unset($types[$ignoredType]);
465 }
466
467 foreach ($types as $type => $typeObject) {
468 $groupForms[$type] = $formFunction($type);
469 }
470
471 return $groupForms;
472 }
473
474 /**
475 * Returns the full taxonomy post forms.
476 *
477 * @return array
478 */
479 private function getFullPostSettingsForm()
480 {
481 return $this->getFullSettingsFrom(
482 $this->getPostTypes(),
483 [ObjectHandler::ATTACHMENT_OBJECT_TYPE],
484 function ($type = MainConfig::DEFAULT_TYPE) {
485 return $this->getPostSettingsForm($type);
486 }
487 );
488 }
489
490 /**
491 * Returns the full taxonomy settings forms.
492 *
493 * @return array
494 */
495 private function getFullTaxonomySettingsForm()
496 {
497 return $this->getFullSettingsFrom(
498 $this->getTaxonomies(),
499 [ObjectHandler::POST_FORMAT_TYPE],
500 function ($type = MainConfig::DEFAULT_TYPE) {
501 return $this->getTaxonomySettingsForm($type);
502 }
503 );
504 }
505
506 /**
507 * Returns the full cache providers froms.
508 *
509 * @return array
510 */
511 private function getFullCacheProvidersFrom()
512 {
513 $groupForms = [];
514 $cacheProviders = $this->cache->getRegisteredCacheProviders();
515 $groupForms[MainConfig::CACHE_PROVIDER_NONE] = null;
516
517 foreach ($cacheProviders as $cacheProvider) {
518 $groupForms[$cacheProvider->getId()] = $this->formHelper->getSettingsFormByConfig(
519 $cacheProvider->getConfig()
520 );
521 }
522
523 return $groupForms;
524 }
525
526 /**
527 * Returns the current settings form.
528 *
529 * @return \UserAccessManager\Form\Form[]
530 */
531 public function getCurrentGroupForms()
532 {
533 $group = $this->getCurrentTabGroup();
534 $groupForms = [];
535
536 if ($group === self::GROUP_POST_TYPES) {
537 $groupForms = $this->getFullPostSettingsForm();
538 } elseif ($group === self::GROUP_TAXONOMIES) {
539 $groupForms = $this->getFullTaxonomySettingsForm();
540 } elseif ($group === self::GROUP_FILES) {
541 $groupForms = [self::SECTION_FILES => $this->getFilesSettingsForm()];
542 } elseif ($group === self::GROUP_AUTHOR) {
543 $groupForms = [self::SECTION_AUTHOR => $this->getAuthorSettingsForm()];
544 } elseif ($group === self::GROUP_CACHE) {
545 $groupForms = $this->getFullCacheProvidersFrom();
546 } elseif ($group === self::GROUP_OTHER) {
547 $groupForms = [self::SECTION_OTHER => $this->getOtherSettingsForm()];
548 }
549
550 return $groupForms;
551 }
552
553 /**
554 * Update settings action.
555 */
556 public function updateSettingsAction()
557 {
558 $this->verifyNonce('uamUpdateSettings');
559 $group = $this->getCurrentTabGroup();
560 $newConfigParameters = $this->getRequestParameter('config_parameters');
561
562 if ($group === self::GROUP_CACHE) {
563 $section = $this->getCurrentTabGroupSection();
564 $cacheProviders = $this->cache->getRegisteredCacheProviders();
565
566 if (isset($cacheProviders[$section]) === true) {
567 $cacheProviders[$section]->getConfig()->setConfigParameters($newConfigParameters);
568 $newConfigParameters = ['active_cache_provider' => $section];
569 } elseif ($section === MainConfig::CACHE_PROVIDER_NONE) {
570 $newConfigParameters = ['active_cache_provider' => $section];
571 }
572 }
573
574 $this->mainConfig->setConfigParameters($newConfigParameters);
575
576 if ($this->mainConfig->lockFile() === false) {
577 $this->fileHandler->deleteFileProtection();
578 } else {
579 $this->fileHandler->createFileProtection();
580 }
581
582 $this->wordpress->doAction('uam_update_options', $this->mainConfig);
583 $this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS);
584 }
585
586 /**
587 * Checks if the group is a post type.
588 *
589 * @param string $key
590 *
591 * @return bool
592 */
593 public function isPostTypeGroup($key)
594 {
595 $postTypes = $this->getPostTypes();
596
597 return isset($postTypes[$key]);
598 }
599 }
600