PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
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
← All changes | src/Controller/Backend/SettingsController.php +254 -102 2.3.16 → 2.1.11 View file →
@@ -1,11 +1,20 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * SettingsController.php
4 + *
5 + * The SettingsController class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
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 + */
5 15 namespace UserAccessManager\Controller\Backend;
6 16
7 -use Exception;
8 17 use UserAccessManager\Cache\Cache;
9 18 use UserAccessManager\Config\MainConfig;
10 19 use UserAccessManager\Config\WordpressConfig;
11 20 use UserAccessManager\Controller\Controller;
@@ -16,41 +25,89 @@
16 25 use UserAccessManager\Form\ValueSetFormElement;
17 26 use UserAccessManager\Object\ObjectHandler;
18 27 use UserAccessManager\Wrapper\Php;
19 28 use UserAccessManager\Wrapper\Wordpress;
20 -use WP_Post_Type;
21 -use WP_Taxonomy;
22 29
23 30 class SettingsController extends Controller
24 31 {
25 32 use ControllerTabNavigationTrait;
26 33
27 - public const GROUP_POST_TYPES = 'post_types';
28 - public const GROUP_TAXONOMIES = 'taxonomies';
29 - public const GROUP_FILES = 'file';
30 - public const SECTION_FILES = 'file';
31 - public const GROUP_AUTHOR = 'author';
32 - public const SECTION_AUTHOR = 'author';
33 - public const GROUP_CACHE = 'cache';
34 - public const GROUP_OTHER = 'other';
35 - public const SECTION_OTHER = 'other';
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';
36 43
37 - protected ?string $template = 'AdminSettings.php';
44 + /**
45 + * @var MainConfig
46 + */
47 + private $mainConfig;
38 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 + */
39 86 public function __construct(
40 87 Php $php,
41 88 Wordpress $wordpress,
42 89 WordpressConfig $wordpressConfig,
43 - private MainConfig $mainConfig,
44 - private Cache $cache,
45 - private FileHandler $fileHandler,
46 - private FormFactory $formFactory,
47 - private FormHelper $formHelper
90 + MainConfig $mainConfig,
91 + Cache $cache,
92 + FileHandler $fileHandler,
93 + FormFactory $formFactory,
94 + FormHelper $formHelper
48 95 ) {
49 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;
50 102 }
51 103
52 - public function getTabGroups(): array
104 + /**
105 + * Returns the tab groups.
106 + *
107 + * @return array
108 + */
109 + public function getTabGroups()
53 110 {
54 111 $activeCacheProvider = $this->mainConfig->getActiveCacheProvider();
55 112 $cacheProviderSections = [$activeCacheProvider];
56 113 $cacheProviders = $this->cache->getRegisteredCacheProviders();
@@ -60,12 +117,8 @@
60 117 $cacheProviderSections[] = $cacheProvider->getId();
61 118 }
62 119 }
63 120
64 - if (!in_array(MainConfig::CACHE_PROVIDER_NONE, $cacheProviderSections)) {
65 - $cacheProviderSections[] = MainConfig::CACHE_PROVIDER_NONE;
66 - }
67 -
68 121 return [
69 122 self::GROUP_POST_TYPES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getPostTypes())),
70 123 self::GROUP_TAXONOMIES => array_merge([MainConfig::DEFAULT_TYPE], array_keys($this->getTaxonomies())),
71 124 self::GROUP_FILES => [self::SECTION_FILES],
@@ -74,10 +127,14 @@
74 127 self::GROUP_OTHER => [self::SECTION_OTHER]
75 128 ];
76 129 }
77 130
78 -
79 - private function getPages(): array
131 + /**
132 + * Returns the pages.
133 + *
134 + * @return array
135 + */
136 + private function getPages()
80 137 {
81 138 $pages = $this->wordpress->getPages('sort_column=menu_order');
82 139 return is_array($pages) !== false ? $pages : [];
83 140 }
@@ -82,40 +139,67 @@
82 139 return is_array($pages) !== false ? $pages : [];
83 140 }
84 141
85 142 /**
86 - * @return WP_Post_Type[]
143 + * Returns the post types as object.
144 + *
145 + * @return \WP_Post_Type[]
87 146 */
88 - private function getPostTypes(): array
147 + private function getPostTypes()
89 148 {
90 149 return $this->wordpress->getPostTypes(['public' => true], 'objects');
91 150 }
92 151
93 152 /**
94 - * @return WP_Taxonomy[]
153 + * Returns the taxonomies as objects.
154 + *
155 + * @return \WP_Taxonomy[]
95 156 */
96 - private function getTaxonomies(): array
157 + private function getTaxonomies()
97 158 {
98 159 return $this->wordpress->getTaxonomies(['public' => true], 'objects');
99 160 }
100 161
101 - public function getText(string $key, bool $description = false): string
162 + /**
163 + * @param string $key
164 + * @param bool $description
165 + *
166 + * @return string
167 + */
168 + public function getText($key, $description = false)
102 169 {
103 170 return $this->formHelper->getText($key, $description);
104 171 }
105 172
106 - public function getGroupText(string $key): string
173 + /**
174 + * @param string $key
175 + *
176 + * @return string
177 + */
178 + public function getGroupText($key)
107 179 {
108 180 return $this->getText($key);
109 181 }
110 182
111 - public function getGroupSectionText(string $key): string
183 + /**
184 + * @param $key
185 + *
186 + * @return string
187 + */
188 + public function getGroupSectionText($key)
112 189 {
113 190 return ($key === MainConfig::DEFAULT_TYPE) ?
114 191 TXT_UAM_SETTINGS_GROUP_SECTION_DEFAULT : $this->getObjectName($key);
115 192 }
116 193
117 - public function getObjectName(string $objectKey): string
194 + /**
195 + * Returns the object name.
196 + *
197 + * @param string $objectKey
198 + *
199 + * @return string
200 + */
201 + public function getObjectName($objectKey)
118 202 {
119 203 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
120 204 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
121 205
@@ -122,11 +206,17 @@
122 206 return (isset($objects[$objectKey]) === true) ? $objects[$objectKey]->labels->name : $objectKey;
123 207 }
124 208
125 209 /**
126 - * @throws Exception
210 + * Returns the post settings form.
211 + *
212 + * @param string $postType
213 + *
214 + * @return \UserAccessManager\Form\Form
215 + *
216 + * @throws \Exception
127 217 */
128 - private function getPostSettingsForm(string $postType = MainConfig::DEFAULT_TYPE): Form
218 + private function getPostSettingsForm($postType = MainConfig::DEFAULT_TYPE)
129 219 {
130 220 $textarea = null;
131 221 $configParameters = $this->mainConfig->getConfigParameters();
132 222
@@ -141,9 +231,9 @@
141 231 }
142 232
143 233 $parameters = ($postType !== MainConfig::DEFAULT_TYPE) ? ["{$postType}_use_default"] : [];
144 234 $parameters = array_merge($parameters, [
145 - "hide_$postType",
235 + "hide_{$postType}",
146 236 "hide_{$postType}_title",
147 237 "{$postType}_title",
148 238 $textarea,
149 239 "hide_{$postType}_comment",
@@ -155,68 +245,76 @@
155 245 return $this->formHelper->getSettingsForm($parameters, $postType);
156 246 }
157 247
158 248 /**
159 - * @throws Exception
249 + * Returns the taxonomy settings form.
250 + *
251 + * @param string $taxonomy
252 + *
253 + * @return \UserAccessManager\Form\Form
254 + *
255 + * @throws \Exception
160 256 */
161 - private function getTaxonomySettingsForm(string $taxonomy = MainConfig::DEFAULT_TYPE): Form
257 + private function getTaxonomySettingsForm($taxonomy = MainConfig::DEFAULT_TYPE)
162 258 {
163 259 $parameters = ($taxonomy !== MainConfig::DEFAULT_TYPE) ? ["{$taxonomy}_use_default"] : [];
164 260 $parameters = array_merge($parameters, [
165 - "hide_empty_$taxonomy"
261 + "hide_empty_{$taxonomy}"
166 262 ]);
167 263
168 264 return $this->formHelper->getSettingsForm($parameters, $taxonomy);
169 265 }
170 266
171 - private function isXSendFileAvailable(): bool
267 + /**
268 + * Checks if x send file is available.
269 + *
270 + * @return bool
271 + */
272 + private function isXSendFileAvailable()
172 273 {
173 - // On nginx, X-Accel-Redirect is always available — no HTTP test needed.
174 - if ($this->wordpress->isNginx()) {
175 - return true;
176 - }
177 -
178 - $content = $this->php->fileGetContents($this->wordpress->getSiteUrl() . '?testXSendFile');
274 + $content = file_get_contents($this->wordpress->getSiteUrl().'?testXSendFile');
179 275 $this->fileHandler->removeXSendFileTestFile();
180 276
181 277 return ($content === 'success');
182 278 }
183 279
184 - private function configureXSendFileOption(Form $form): void
280 + /**
281 + * Disables the xSendFileOption
282 + *
283 + * @param Form $form
284 + */
285 + private function disableXSendFileOption(Form $form)
185 286 {
186 287 $formElements = $form->getElements();
187 288
188 - if (isset($formElements['download_type']) === false) {
189 - return;
190 - }
289 + if (isset($formElements['download_type']) === true) {
290 + /** @var ValueSetFormElement $downloadType */
291 + $downloadType = $formElements['download_type'];
292 + $possibleValues = $downloadType->getPossibleValues();
191 293
192 - /** @var ValueSetFormElement $downloadType */
193 - $downloadType = $formElements['download_type'];
194 - $possibleValues = $downloadType->getPossibleValues();
195 -
196 - if (isset($possibleValues['xsendfile']) === false) {
197 - return;
294 + if (isset($possibleValues['xsendfile']) === true) {
295 + $possibleValues['xsendfile']->markDisabled();
296 + }
198 297 }
199 -
200 - if ($this->wordpress->isNginx() === true) {
201 - // X-Accel-Redirect is always available on nginx — just relabel the option.
202 - $possibleValues['xsendfile']->setLabel(TXT_UAM_DOWNLOAD_TYPE_XACCELREDIRECT);
203 - } elseif ($this->isXSendFileAvailable() === false) {
204 - $possibleValues['xsendfile']->markDisabled();
205 - }
206 298 }
207 299
208 - private function addLockFileTypes(array $configParameters, array &$parameters): void
300 + /**
301 + * Adds the lock file types config parameter to the parameters.
302 + *
303 + * @param array $configParameters
304 + * @param array $parameters
305 + */
306 + private function addLockFileTypes(array $configParameters, array &$parameters)
209 307 {
210 - if (isset($configParameters['lock_file_types']) === true) {
308 + if (isset($configParameters['lock_file_types']) === true
309 + && $this->wordpress->isNginx() === false
310 + ) {
211 311 $parameters['lock_file_types'] = [
212 312 'selected' => 'locked_file_types',
213 313 'not_selected' => 'not_locked_file_types'
214 314 ];
215 315
216 - if ($this->wordpress->isNginx() === false
217 - && $this->wordpress->gotModRewrite() === false
218 - ) {
316 + if ($this->wordpress->gotModRewrite() === false) {
219 317 $parameters[] = 'file_pass_type';
220 318 }
221 319 }
222 320 }
@@ -221,11 +319,15 @@
221 319 }
222 320 }
223 321
224 322 /**
225 - * @throws Exception
323 + * Returns the files settings form.
324 + *
325 + * @return \UserAccessManager\Form\Form
326 + *
327 + * @throws \Exception
226 328 */
227 - private function getFilesSettingsForm(): Form
329 + private function getFilesSettingsForm()
228 330 {
229 331 $fileProtectionFileName = $this->fileHandler->getFileProtectionFileName();
230 332 $fileContent = (file_exists($fileProtectionFileName) === true) ?
231 333 file_get_contents($fileProtectionFileName) : '';
@@ -250,17 +352,23 @@
250 352 $this->addLockFileTypes($configParameters, $parameters);
251 353
252 354 $form = $this->formHelper->getSettingsForm($parameters);
253 355
254 - $this->configureXSendFileOption($form);
356 + if ($this->isXSendFileAvailable() === false) {
357 + $this->disableXSendFileOption($form);
358 + }
255 359
256 360 return $form;
257 361 }
258 362
259 363 /**
260 - * @throws Exception
364 + * Returns the author settings form.
365 + *
366 + * @return \UserAccessManager\Form\Form
367 + *
368 + * @throws \Exception
261 369 */
262 - private function getAuthorSettingsForm(): Form
370 + private function getAuthorSettingsForm()
263 371 {
264 372 $parameters = [
265 373 'authors_has_access_to_own',
266 374 'authors_can_add_posts_to_groups',
@@ -269,9 +377,15 @@
269 377
270 378 return $this->formHelper->getSettingsForm($parameters);
271 379 }
272 380
273 - private function addCustomPageRedirectFormElement(array $configParameters, array &$values): void
381 + /**
382 + * Adds the custom page redirect from element.
383 + *
384 + * @param array $configParameters
385 + * @param array $values
386 + */
387 + private function addCustomPageRedirectFormElement(array $configParameters, array &$values)
274 388 {
275 389 if (isset($configParameters['redirect_custom_page']) === true) {
276 390 $redirectCustomPage = $configParameters['redirect_custom_page'];
277 391 $redirectCustomPageValue = $this->formFactory->createMultipleFormElementValue(
@@ -283,9 +397,9 @@
283 397 $pages = $this->getPages();
284 398
285 399 foreach ($pages as $page) {
286 400 $possibleValues[] = $this->formFactory->createValueSetFromElementValue(
287 - (int) $page->ID,
401 + (int)$page->ID,
288 402 $page->post_title
289 403 );
290 404 }
291 405
@@ -291,23 +405,28 @@
291 405
292 406 $formElement = $this->formFactory->createSelect(
293 407 $redirectCustomPage->getId(),
294 408 $possibleValues,
295 - (int) $redirectCustomPage->getValue()
409 + (int)$redirectCustomPage->getValue()
296 410 );
297 411
298 412 try {
299 413 $redirectCustomPageValue->setSubElement($formElement);
300 414 $values[] = $redirectCustomPageValue;
301 - } catch (Exception) {
415 + } catch (\Exception $exception) {
416 + // Do Nothing
302 417 }
303 418 }
304 419 }
305 420
306 421 /**
307 - * @throws Exception
422 + * Returns the author settings form.
423 + *
424 + * @return \UserAccessManager\Form\Form
425 + *
426 + * @throws \Exception
308 427 */
309 - private function getOtherSettingsForm(): Form
428 + private function getOtherSettingsForm()
310 429 {
311 430 $redirect = null;
312 431 $configParameters = $this->mainConfig->getConfigParameters();
313 432
@@ -314,10 +433,9 @@
314 433 if (isset($configParameters['redirect'])) {
315 434 $values = [
316 435 $this->formFactory->createMultipleFormElementValue('false', TXT_UAM_NO),
317 436 $this->formFactory->createMultipleFormElementValue('blog', TXT_UAM_REDIRECT_TO_BLOG),
318 - $this->formFactory->createMultipleFormElementValue('login', TXT_UAM_REDIRECT_TO_LOGIN),
319 - $this->formFactory->createMultipleFormElementValue('origin', TXT_UAM_REDIRECT_TO_ORIGIN)
437 + $this->formFactory->createMultipleFormElementValue('login', TXT_UAM_REDIRECT_TO_LOGIN)
320 438 ];
321 439
322 440 $this->addCustomPageRedirectFormElement($configParameters, $values);
323 441
@@ -327,9 +445,10 @@
327 445 'custom_url',
328 446 TXT_UAM_REDIRECT_TO_URL,
329 447 $configParameters['redirect_custom_url']
330 448 );
331 - } catch (Exception) {
449 + } catch (\Exception $exception) {
450 + // Do nothing.
332 451 }
333 452 }
334 453
335 454 $configParameter = $configParameters['redirect'];
@@ -346,20 +465,27 @@
346 465 $parameters = [
347 466 'lock_recursive',
348 467 'protect_feed',
349 468 $redirect,
350 - 'append_redirect_to_parameter',
351 469 'blog_admin_hint',
352 470 'blog_admin_hint_text',
353 471 'show_assigned_groups',
354 - 'hide_edit_link_on_no_access',
355 - 'extra_ip_header'
472 + 'hide_edit_link_on_no_access'
356 473 ];
357 474
358 475 return $this->formHelper->getSettingsForm($parameters);
359 476 }
360 477
361 - private function getFullSettingsFrom(array $types, array $ignoredTypes, callable $formFunction): array
478 + /**
479 + * Returns the full settings from.
480 + *
481 + * @param array $types
482 + * @param array $ignoredTypes
483 + * @param Callable $formFunction
484 + *
485 + * @return array
486 + */
487 + private function getFullSettingsFrom(array $types, array $ignoredTypes, $formFunction)
362 488 {
363 489 $groupForms = [];
364 490 $groupForms[MainConfig::DEFAULT_TYPE] = $formFunction();
365 491
@@ -374,11 +500,13 @@
374 500 return $groupForms;
375 501 }
376 502
377 503 /**
378 - * @throws Exception
504 + * Returns the full taxonomy post forms.
505 + *
506 + * @return array
379 507 */
380 - private function getFullPostSettingsForm(): array
508 + private function getFullPostSettingsForm()
381 509 {
382 510 return $this->getFullSettingsFrom(
383 511 $this->getPostTypes(),
384 512 [ObjectHandler::ATTACHMENT_OBJECT_TYPE],
@@ -388,11 +516,13 @@
388 516 );
389 517 }
390 518
391 519 /**
392 - * @throws Exception
520 + * Returns the full taxonomy settings forms.
521 + *
522 + * @return array
393 523 */
394 - private function getFullTaxonomySettingsForm(): array
524 + private function getFullTaxonomySettingsForm()
395 525 {
396 526 return $this->getFullSettingsFrom(
397 527 $this->getTaxonomies(),
398 528 [ObjectHandler::POST_FORMAT_TYPE],
@@ -402,11 +532,15 @@
402 532 );
403 533 }
404 534
405 535 /**
406 - * @throws Exception
536 + * Returns the full cache providers froms.
537 + *
538 + * @return array
539 + *
540 + * @throws \Exception
407 541 */
408 - private function getFullCacheProvidersForm(): array
542 + private function getFullCacheProvidersForm()
409 543 {
410 544 $groupForms = [];
411 545 $cacheProviders = $this->cache->getRegisteredCacheProviders();
412 546 $groupForms[MainConfig::CACHE_PROVIDER_NONE] = null;
@@ -420,13 +554,16 @@
420 554 return $groupForms;
421 555 }
422 556
423 557 /**
424 - * @return Form[]
558 + * Returns the current settings form.
559 + *
560 + * @return \UserAccessManager\Form\Form[]
425 561 */
426 - public function getCurrentGroupForms(): array
562 + public function getCurrentGroupForms()
427 563 {
428 564 $group = $this->getCurrentTabGroup();
565 + $groupForms = [];
429 566
430 567 try {
431 568 $formMap = [
432 569 self::GROUP_POST_TYPES => function () {
@@ -451,16 +588,21 @@
451 588
452 589 if (isset($formMap[$group]) === true) {
453 590 return $formMap[$group]();
454 591 }
455 - } catch (Exception $exception) {
592 + } catch (\Exception $exception) {
456 593 $this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage()));
457 594 }
458 595
459 - return [];
596 + return $groupForms;
460 597 }
461 598
462 - private function updateFileProtectionFile(array $configParameters): void
599 + /**
600 + * Updates the file handling file.
601 + *
602 + * @param array $configParameters
603 + */
604 + private function updateFileProtectionFile(array $configParameters)
463 605 {
464 606 $key = 'custom_file_handling_file';
465 607 $customFileHandlingFile = isset($configParameters[$key]) === true ? $configParameters[$key] : null;
466 608 unset($configParameters[$key]);
@@ -478,9 +620,12 @@
478 620 );
479 621 }
480 622 }
481 623
482 - public function updateSettingsAction(): void
624 + /**
625 + * Update settings action.
626 + */
627 + public function updateSettingsAction()
483 628 {
484 629 $this->verifyNonce('uamUpdateSettings');
485 630 $group = $this->getCurrentTabGroup();
486 631 $newConfigParameters = $this->getRequestParameter('config_parameters');
@@ -501,9 +646,16 @@
501 646 $this->wordpress->doAction('uam_update_options', $this->mainConfig);
502 647 $this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS);
503 648 }
504 649
505 - public function isPostTypeGroup(string $key): bool
650 + /**
651 + * Checks if the group is a post type.
652 + *
653 + * @param string $key
654 + *
655 + * @return bool
656 + */
657 + public function isPostTypeGroup($key)
506 658 {
507 659 $postTypes = $this->getPostTypes();
508 660
509 661 return isset($postTypes[$key]);