PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
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/Controller.php +106 -25 trunk2.2.21 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * Controller.php
4 + *
5 + * The Controller 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;
@@ -4,11 +17,17 @@
4 17
5 18 namespace UserAccessManager\Controller;
6 19
7 20 use UserAccessManager\Config\WordpressConfig;
21 +use UserAccessManager\Controller\Backend\BackendController;
8 22 use UserAccessManager\Wrapper\Php;
9 23 use UserAccessManager\Wrapper\Wordpress;
10 24
25 +/**
26 + * Class Controller
27 + *
28 + * @package UserAccessManager\Controller
29 + */
11 30 abstract class Controller
12 31 {
13 32 use BaseControllerTrait {
14 33 render as traitRender;
@@ -13,42 +32,85 @@
13 32 use BaseControllerTrait {
14 33 render as traitRender;
15 34 }
16 35
17 - public const ACTION_PARAMETER = 'uam_action';
18 - public const ACTION_SUFFIX = 'Action';
19 - public const UAM_ERRORS = 'UAM_ERRORS';
36 + const ACTION_PARAMETER = 'uam_action';
37 + const ACTION_SUFFIX = 'Action';
20 38
21 - protected ?string $updateMessage = null;
39 + /**
40 + * @var Php
41 + */
42 + protected $php;
22 43
23 - public function __construct(
24 - protected Php $php,
25 - protected Wordpress $wordpress,
26 - protected WordpressConfig $wordpressConfig
27 - ) {
44 + /**
45 + * @var WordpressConfig
46 + */
47 + protected $wordpressConfig;
48 +
49 + /**
50 + * @var Wordpress
51 + */
52 + protected $wordpress;
53 +
54 + /**
55 + * @var string
56 + */
57 + protected $updateMessage = null;
58 +
59 + /**
60 + * Controller constructor.
61 + * @param Php $php
62 + * @param Wordpress $wordpress
63 + * @param WordpressConfig $wordpressConfig
64 + */
65 + public function __construct(Php $php, Wordpress $wordpress, WordpressConfig $wordpressConfig)
66 + {
67 + $this->php = $php;
68 + $this->wordpress = $wordpress;
69 + $this->wordpressConfig = $wordpressConfig;
28 70 }
29 71
72 + /**
73 + * @return Php
74 + */
30 75 protected function getPhp(): Php
31 76 {
32 77 return $this->php;
33 78 }
34 79
80 + /**
81 + * @return WordpressConfig
82 + */
35 83 protected function getWordpressConfig(): WordpressConfig
36 84 {
37 85 return $this->wordpressConfig;
38 86 }
39 87
88 + /**
89 + * Returns the nonce field.
90 + * @param string $name
91 + * @return string
92 + */
40 93 public function createNonceField(string $name): string
41 94 {
42 95 return $this->wordpress->getNonceField($name, $name.'Nonce');
43 96 }
44 97
98 + /**
99 + * Returns the nonce.
100 + * @param string $name
101 + * @return string
102 + */
45 103 public function getNonce(string $name): string
46 104 {
47 105 return $this->wordpress->createNonce($name);
48 106 }
49 107
50 - protected function verifyNonce(string $name): void
108 + /**
109 + * Verifies the nonce and terminates the application if the nonce is wrong.
110 + * @param string $name
111 + */
112 + protected function verifyNonce(string $name)
51 113 {
52 114 $nonce = $this->getRequestParameter($name.'Nonce');
53 115
54 116 if ($this->wordpress->verifyNonce($nonce, $name) === false) {
@@ -55,49 +117,68 @@
55 117 $this->wordpress->wpDie(TXT_UAM_NONCE_FAILURE_MESSAGE, TXT_UAM_NONCE_FAILURE_TITLE, ['response' => 401]);
56 118 }
57 119 }
58 120
59 - protected function setUpdateMessage(string $message): void
121 + /**
122 + * Sets the update message.
123 + * @param string $message
124 + */
125 + protected function setUpdateMessage(string $message)
60 126 {
61 127 $this->updateMessage = $message;
62 128 }
63 129
64 - protected function addErrorMessage(string $message): void
130 + /**
131 + * Adds an error message.
132 + * @param string $message
133 + */
134 + protected function addErrorMessage(string $message)
65 135 {
66 - if (isset($_SESSION[self::UAM_ERRORS]) === false) {
67 - $_SESSION[self::UAM_ERRORS] = [];
136 + if (isset($_SESSION[BackendController::UAM_ERRORS]) === false) {
137 + $_SESSION[BackendController::UAM_ERRORS] = [];
68 138 }
69 139
70 - $_SESSION[self::UAM_ERRORS][] = $message;
140 + $_SESSION[BackendController::UAM_ERRORS][] = $message;
71 141 }
72 142
143 + /**
144 + * Returns the update message.
145 + * @return string
146 + */
73 147 public function getUpdateMessage(): ?string
74 148 {
75 149 return $this->updateMessage;
76 150 }
77 151
152 + /**
153 + * Returns true if a update message is set.
154 + * @return bool
155 + */
78 156 public function hasUpdateMessage(): bool
79 157 {
80 158 return $this->updateMessage !== null;
81 159 }
82 160
83 - private function toCamelCase(string $snakeCaseName): string
161 + /**
162 + * Process the action.
163 + */
164 + protected function processAction()
84 165 {
85 - $words = explode('_', $snakeCaseName);
86 - return array_shift($words).implode('', array_map('ucfirst', $words));
87 - }
166 + $postAction = (string) $this->getRequestParameter(self::ACTION_PARAMETER);
167 + $postActionSplit = explode('_', $postAction);
168 + $postAction = array_shift($postActionSplit);
169 + $postAction .= implode('', array_map('ucfirst', $postActionSplit));
170 + $actionMethod = $postAction.self::ACTION_SUFFIX;
88 171
89 - protected function processAction(): void
90 - {
91 - $requestedAction = (string) $this->getRequestParameter(self::ACTION_PARAMETER);
92 - $actionMethod = $this->toCamelCase($requestedAction).self::ACTION_SUFFIX;
93 -
94 172 if (method_exists($this, $actionMethod) === true) {
95 173 $this->{$actionMethod}();
96 174 }
97 175 }
98 176
99 - public function render(): void
177 + /**
178 + * Renders the given template
179 + */
180 + public function render()
100 181 {
101 182 $this->processAction();
102 183 $this->traitRender();
103 184 }