PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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 +186 -57 2.3.132.2.2 View file →
@@ -1,5 +1,18 @@
1 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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Controller\Backend;
@@ -23,33 +36,81 @@
23 36 class SettingsController extends Controller
24 37 {
25 38 use ControllerTabNavigationTrait;
26 39
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';
40 + const GROUP_POST_TYPES = 'post_types';
41 + const GROUP_TAXONOMIES = 'taxonomies';
42 + const GROUP_FILES = 'file';
43 + const SECTION_FILES = 'file';
44 + const GROUP_AUTHOR = 'author';
45 + const SECTION_AUTHOR = 'author';
46 + const GROUP_CACHE = 'cache';
47 + const GROUP_OTHER = 'other';
48 + const SECTION_OTHER = 'other';
36 49
37 - protected ?string $template = 'AdminSettings.php';
50 + /**
51 + * @var MainConfig
52 + */
53 + private $mainConfig;
38 54
55 + /**
56 + * @var string
57 + */
58 + protected $template = 'AdminSettings.php';
59 +
60 + /**
61 + * @var Cache
62 + */
63 + private $cache;
64 +
65 + /**
66 + * @var FileHandler
67 + */
68 + private $fileHandler;
69 +
70 + /**
71 + * @var FormFactory
72 + */
73 + private $formFactory;
74 +
75 + /**
76 + * @var FormHelper
77 + */
78 + private $formHelper;
79 +
80 + /**
81 + * SettingsController constructor.
82 + * @param Php $php
83 + * @param Wordpress $wordpress
84 + * @param WordpressConfig $wordpressConfig
85 + * @param MainConfig $mainConfig
86 + * @param Cache $cache
87 + * @param FileHandler $fileHandler
88 + * @param FormFactory $formFactory
89 + * @param FormHelper $formHelper
90 + */
39 91 public function __construct(
40 92 Php $php,
41 93 Wordpress $wordpress,
42 94 WordpressConfig $wordpressConfig,
43 - private MainConfig $mainConfig,
44 - private Cache $cache,
45 - private FileHandler $fileHandler,
46 - private FormFactory $formFactory,
47 - private FormHelper $formHelper
95 + MainConfig $mainConfig,
96 + Cache $cache,
97 + FileHandler $fileHandler,
98 + FormFactory $formFactory,
99 + FormHelper $formHelper
48 100 ) {
49 101 parent::__construct($php, $wordpress, $wordpressConfig);
102 + $this->mainConfig = $mainConfig;
103 + $this->cache = $cache;
104 + $this->fileHandler = $fileHandler;
105 + $this->formFactory = $formFactory;
106 + $this->formHelper = $formHelper;
50 107 }
51 108
109 + /**
110 + * Returns the tab groups.
111 + * @return array
112 + */
52 113 public function getTabGroups(): array
53 114 {
54 115 $activeCacheProvider = $this->mainConfig->getActiveCacheProvider();
55 116 $cacheProviderSections = [$activeCacheProvider];
@@ -74,9 +135,12 @@
74 135 self::GROUP_OTHER => [self::SECTION_OTHER]
75 136 ];
76 137 }
77 138
78 -
139 + /**
140 + * Returns the pages.
141 + * @return array
142 + */
79 143 private function getPages(): array
80 144 {
81 145 $pages = $this->wordpress->getPages('sort_column=menu_order');
82 146 return is_array($pages) !== false ? $pages : [];
@@ -82,8 +146,9 @@
82 146 return is_array($pages) !== false ? $pages : [];
83 147 }
84 148
85 149 /**
150 + * Returns the post types as object.
86 151 * @return WP_Post_Type[]
87 152 */
88 153 private function getPostTypes(): array
89 154 {
@@ -90,8 +155,9 @@
90 155 return $this->wordpress->getPostTypes(['public' => true], 'objects');
91 156 }
92 157
93 158 /**
159 + * Returns the taxonomies as objects.
94 160 * @return WP_Taxonomy[]
95 161 */
96 162 private function getTaxonomies(): array
97 163 {
@@ -97,18 +163,31 @@
97 163 {
98 164 return $this->wordpress->getTaxonomies(['public' => true], 'objects');
99 165 }
100 166
101 - public function getText(string $key, bool $description = false): string
167 + /**
168 + * @param string $key
169 + * @param bool $description
170 + * @return string
171 + */
172 + public function getText(string $key, $description = false): string
102 173 {
103 174 return $this->formHelper->getText($key, $description);
104 175 }
105 176
177 + /**
178 + * @param string $key
179 + * @return string
180 + */
106 181 public function getGroupText(string $key): string
107 182 {
108 183 return $this->getText($key);
109 184 }
110 185
186 + /**
187 + * @param string $key
188 + * @return string
189 + */
111 190 public function getGroupSectionText(string $key): string
112 191 {
113 192 return ($key === MainConfig::DEFAULT_TYPE) ?
114 193 TXT_UAM_SETTINGS_GROUP_SECTION_DEFAULT : $this->getObjectName($key);
@@ -113,8 +192,13 @@
113 192 return ($key === MainConfig::DEFAULT_TYPE) ?
114 193 TXT_UAM_SETTINGS_GROUP_SECTION_DEFAULT : $this->getObjectName($key);
115 194 }
116 195
196 + /**
197 + * Returns the object name.
198 + * @param string $objectKey
199 + * @return string
200 + */
117 201 public function getObjectName(string $objectKey): string
118 202 {
119 203 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
120 204 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
@@ -122,11 +206,14 @@
122 206 return (isset($objects[$objectKey]) === true) ? $objects[$objectKey]->labels->name : $objectKey;
123 207 }
124 208
125 209 /**
210 + * Returns the post settings form.
211 + * @param string $postType
212 + * @return Form
126 213 * @throws Exception
127 214 */
128 - private function getPostSettingsForm(string $postType = MainConfig::DEFAULT_TYPE): Form
215 + private function getPostSettingsForm($postType = MainConfig::DEFAULT_TYPE): Form
129 216 {
130 217 $textarea = null;
131 218 $configParameters = $this->mainConfig->getConfigParameters();
132 219
@@ -141,9 +228,9 @@
141 228 }
142 229
143 230 $parameters = ($postType !== MainConfig::DEFAULT_TYPE) ? ["{$postType}_use_default"] : [];
144 231 $parameters = array_merge($parameters, [
145 - "hide_$postType",
232 + "hide_{$postType}",
146 233 "hide_{$postType}_title",
147 234 "{$postType}_title",
148 235 $textarea,
149 236 "hide_{$postType}_comment",
@@ -155,68 +242,70 @@
155 242 return $this->formHelper->getSettingsForm($parameters, $postType);
156 243 }
157 244
158 245 /**
246 + * Returns the taxonomy settings form.
247 + * @param string $taxonomy
248 + * @return Form
159 249 * @throws Exception
160 250 */
161 - private function getTaxonomySettingsForm(string $taxonomy = MainConfig::DEFAULT_TYPE): Form
251 + private function getTaxonomySettingsForm($taxonomy = MainConfig::DEFAULT_TYPE): Form
162 252 {
163 253 $parameters = ($taxonomy !== MainConfig::DEFAULT_TYPE) ? ["{$taxonomy}_use_default"] : [];
164 254 $parameters = array_merge($parameters, [
165 - "hide_empty_$taxonomy"
255 + "hide_empty_{$taxonomy}"
166 256 ]);
167 257
168 258 return $this->formHelper->getSettingsForm($parameters, $taxonomy);
169 259 }
170 260
261 + /**
262 + * Checks if x send file is available.
263 + * @return bool
264 + */
171 265 private function isXSendFileAvailable(): bool
172 266 {
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 = @file_get_contents($this->wordpress->getSiteUrl() . '?testXSendFile');
267 + $content = file_get_contents($this->wordpress->getSiteUrl() . '?testXSendFile');
179 268 $this->fileHandler->removeXSendFileTestFile();
180 269
181 270 return ($content === 'success');
182 271 }
183 272
184 - private function configureXSendFileOption(Form $form): void
273 + /**
274 + * Disables the xSendFileOption
275 + * @param Form $form
276 + */
277 + private function disableXSendFileOption(Form $form)
185 278 {
186 279 $formElements = $form->getElements();
187 280
188 - if (isset($formElements['download_type']) === false) {
189 - return;
190 - }
281 + if (isset($formElements['download_type']) === true) {
282 + /** @var ValueSetFormElement $downloadType */
283 + $downloadType = $formElements['download_type'];
284 + $possibleValues = $downloadType->getPossibleValues();
191 285
192 - /** @var ValueSetFormElement $downloadType */
193 - $downloadType = $formElements['download_type'];
194 - $possibleValues = $downloadType->getPossibleValues();
195 -
196 - if (isset($possibleValues['xsendfile']) === false) {
197 - return;
286 + if (isset($possibleValues['xsendfile']) === true) {
287 + $possibleValues['xsendfile']->markDisabled();
288 + }
198 289 }
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 290 }
207 291
208 - private function addLockFileTypes(array $configParameters, array &$parameters): void
292 + /**
293 + * Adds the lock file types config parameter to the parameters.
294 + * @param array $configParameters
295 + * @param array $parameters
296 + */
297 + private function addLockFileTypes(array $configParameters, array &$parameters)
209 298 {
210 - if (isset($configParameters['lock_file_types']) === true) {
299 + if (isset($configParameters['lock_file_types']) === true
300 + && $this->wordpress->isNginx() === false
301 + ) {
211 302 $parameters['lock_file_types'] = [
212 303 'selected' => 'locked_file_types',
213 304 'not_selected' => 'not_locked_file_types'
214 305 ];
215 306
216 - if ($this->wordpress->isNginx() === false
217 - && $this->wordpress->gotModRewrite() === false
218 - ) {
307 + if ($this->wordpress->gotModRewrite() === false) {
219 308 $parameters[] = 'file_pass_type';
220 309 }
221 310 }
222 311 }
@@ -221,8 +310,10 @@
221 310 }
222 311 }
223 312
224 313 /**
314 + * Returns the files settings form.
315 + * @return Form
225 316 * @throws Exception
226 317 */
227 318 private function getFilesSettingsForm(): Form
228 319 {
@@ -250,14 +341,18 @@
250 341 $this->addLockFileTypes($configParameters, $parameters);
251 342
252 343 $form = $this->formHelper->getSettingsForm($parameters);
253 344
254 - $this->configureXSendFileOption($form);
345 + if ($this->isXSendFileAvailable() === false) {
346 + $this->disableXSendFileOption($form);
347 + }
255 348
256 349 return $form;
257 350 }
258 351
259 352 /**
353 + * Returns the author settings form.
354 + * @return Form
260 355 * @throws Exception
261 356 */
262 357 private function getAuthorSettingsForm(): Form
263 358 {
@@ -269,9 +364,14 @@
269 364
270 365 return $this->formHelper->getSettingsForm($parameters);
271 366 }
272 367
273 - private function addCustomPageRedirectFormElement(array $configParameters, array &$values): void
368 + /**
369 + * Adds the custom page redirect from element.
370 + * @param array $configParameters
371 + * @param array $values
372 + */
373 + private function addCustomPageRedirectFormElement(array $configParameters, array &$values)
274 374 {
275 375 if (isset($configParameters['redirect_custom_page']) === true) {
276 376 $redirectCustomPage = $configParameters['redirect_custom_page'];
277 377 $redirectCustomPageValue = $this->formFactory->createMultipleFormElementValue(
@@ -297,14 +397,17 @@
297 397
298 398 try {
299 399 $redirectCustomPageValue->setSubElement($formElement);
300 400 $values[] = $redirectCustomPageValue;
301 - } catch (Exception) {
401 + } catch (Exception $exception) {
402 + // Do Nothing
302 403 }
303 404 }
304 405 }
305 406
306 407 /**
408 + * Returns the author settings form.
409 + * @return Form
307 410 * @throws Exception
308 411 */
309 412 private function getOtherSettingsForm(): Form
310 413 {
@@ -326,9 +429,10 @@
326 429 'custom_url',
327 430 TXT_UAM_REDIRECT_TO_URL,
328 431 $configParameters['redirect_custom_url']
329 432 );
330 - } catch (Exception) {
433 + } catch (Exception $exception) {
434 + // Do nothing.
331 435 }
332 436 }
333 437
334 438 $configParameter = $configParameters['redirect'];
@@ -348,15 +452,21 @@
348 452 $redirect,
349 453 'blog_admin_hint',
350 454 'blog_admin_hint_text',
351 455 'show_assigned_groups',
352 - 'hide_edit_link_on_no_access',
353 - 'extra_ip_header'
456 + 'hide_edit_link_on_no_access'
354 457 ];
355 458
356 459 return $this->formHelper->getSettingsForm($parameters);
357 460 }
358 461
462 + /**
463 + * Returns the full settings from.
464 + * @param array $types
465 + * @param array $ignoredTypes
466 + * @param Callable $formFunction
467 + * @return array
468 + */
359 469 private function getFullSettingsFrom(array $types, array $ignoredTypes, callable $formFunction): array
360 470 {
361 471 $groupForms = [];
362 472 $groupForms[MainConfig::DEFAULT_TYPE] = $formFunction();
@@ -372,8 +482,10 @@
372 482 return $groupForms;
373 483 }
374 484
375 485 /**
486 + * Returns the full taxonomy post forms.
487 + * @return array
376 488 * @throws Exception
377 489 */
378 490 private function getFullPostSettingsForm(): array
379 491 {
@@ -386,8 +498,10 @@
386 498 );
387 499 }
388 500
389 501 /**
502 + * Returns the full taxonomy settings forms.
503 + * @return array
390 504 * @throws Exception
391 505 */
392 506 private function getFullTaxonomySettingsForm(): array
393 507 {
@@ -400,8 +514,10 @@
400 514 );
401 515 }
402 516
403 517 /**
518 + * Returns the full cache providers froms.
519 + * @return array
404 520 * @throws Exception
405 521 */
406 522 private function getFullCacheProvidersForm(): array
407 523 {
@@ -418,8 +534,9 @@
418 534 return $groupForms;
419 535 }
420 536
421 537 /**
538 + * Returns the current settings form.
422 539 * @return Form[]
423 540 */
424 541 public function getCurrentGroupForms(): array
425 542 {
@@ -456,9 +573,13 @@
456 573
457 574 return [];
458 575 }
459 576
460 - private function updateFileProtectionFile(array $configParameters): void
577 + /**
578 + * Updates the file handling file.
579 + * @param array $configParameters
580 + */
581 + private function updateFileProtectionFile(array $configParameters)
461 582 {
462 583 $key = 'custom_file_handling_file';
463 584 $customFileHandlingFile = isset($configParameters[$key]) === true ? $configParameters[$key] : null;
464 585 unset($configParameters[$key]);
@@ -476,9 +597,12 @@
476 597 );
477 598 }
478 599 }
479 600
480 - public function updateSettingsAction(): void
601 + /**
602 + * Update settings action.
603 + */
604 + public function updateSettingsAction()
481 605 {
482 606 $this->verifyNonce('uamUpdateSettings');
483 607 $group = $this->getCurrentTabGroup();
484 608 $newConfigParameters = $this->getRequestParameter('config_parameters');
@@ -499,8 +623,13 @@
499 623 $this->wordpress->doAction('uam_update_options', $this->mainConfig);
500 624 $this->setUpdateMessage(TXT_UAM_UPDATE_SETTINGS);
501 625 }
502 626
627 + /**
628 + * Checks if the group is a post type.
629 + * @param string $key
630 + * @return bool
631 + */
503 632 public function isPostTypeGroup(string $key): bool
504 633 {
505 634 $postTypes = $this->getPostTypes();
506 635