PluginProbe
User Access Manager / 2.1.8
User Access Manager v2.1.8
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.8, at src/Controller/Backend/SettingsController.php

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