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