| 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 |
*/ |
| 15 |
|
| 16 |
declare(strict_types=1); |
| 17 |
|
| 18 |
namespace UserAccessManager\Controller; |
| 19 |
|
| 20 |
use UserAccessManager\Config\WordpressConfig; |
| 21 |
use UserAccessManager\Controller\Backend\BackendController; |
| 22 |
use UserAccessManager\Wrapper\Php; |
| 23 |
use UserAccessManager\Wrapper\Wordpress; |
| 24 |
|
| 25 |
/** |
| 26 |
* Class Controller |
| 27 |
* |
| 28 |
* @package UserAccessManager\Controller |
| 29 |
*/ |
| 30 |
abstract class Controller |
| 31 |
{ |
| 32 |
use BaseControllerTrait { |
| 33 |
render as traitRender; |
| 34 |
} |
| 35 |
|
| 36 |
public const ACTION_PARAMETER = 'uam_action'; |
| 37 |
public const ACTION_SUFFIX = 'Action'; |
| 38 |
|
| 39 |
protected ?string $updateMessage = null; |
| 40 |
|
| 41 |
public function __construct( |
| 42 |
protected Php $php, |
| 43 |
protected Wordpress $wordpress, |
| 44 |
protected WordpressConfig $wordpressConfig |
| 45 |
) { |
| 46 |
} |
| 47 |
|
| 48 |
protected function getPhp(): Php |
| 49 |
{ |
| 50 |
return $this->php; |
| 51 |
} |
| 52 |
|
| 53 |
protected function getWordpressConfig(): WordpressConfig |
| 54 |
{ |
| 55 |
return $this->wordpressConfig; |
| 56 |
} |
| 57 |
|
| 58 |
public function createNonceField(string $name): string |
| 59 |
{ |
| 60 |
return $this->wordpress->getNonceField($name, $name.'Nonce'); |
| 61 |
} |
| 62 |
|
| 63 |
public function getNonce(string $name): string |
| 64 |
{ |
| 65 |
return $this->wordpress->createNonce($name); |
| 66 |
} |
| 67 |
|
| 68 |
protected function verifyNonce(string $name): void |
| 69 |
{ |
| 70 |
$nonce = $this->getRequestParameter($name.'Nonce'); |
| 71 |
|
| 72 |
if ($this->wordpress->verifyNonce($nonce, $name) === false) { |
| 73 |
$this->wordpress->wpDie(TXT_UAM_NONCE_FAILURE_MESSAGE, TXT_UAM_NONCE_FAILURE_TITLE, ['response' => 401]); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
protected function setUpdateMessage(string $message): void |
| 78 |
{ |
| 79 |
$this->updateMessage = $message; |
| 80 |
} |
| 81 |
|
| 82 |
protected function addErrorMessage(string $message): void |
| 83 |
{ |
| 84 |
if (isset($_SESSION[BackendController::UAM_ERRORS]) === false) { |
| 85 |
$_SESSION[BackendController::UAM_ERRORS] = []; |
| 86 |
} |
| 87 |
|
| 88 |
$_SESSION[BackendController::UAM_ERRORS][] = $message; |
| 89 |
} |
| 90 |
|
| 91 |
public function getUpdateMessage(): ?string |
| 92 |
{ |
| 93 |
return $this->updateMessage; |
| 94 |
} |
| 95 |
|
| 96 |
public function hasUpdateMessage(): bool |
| 97 |
{ |
| 98 |
return $this->updateMessage !== null; |
| 99 |
} |
| 100 |
|
| 101 |
protected function processAction(): void |
| 102 |
{ |
| 103 |
$postAction = (string) $this->getRequestParameter(self::ACTION_PARAMETER); |
| 104 |
$postActionSplit = explode('_', $postAction); |
| 105 |
$postAction = array_shift($postActionSplit); |
| 106 |
$postAction .= implode('', array_map('ucfirst', $postActionSplit)); |
| 107 |
$actionMethod = $postAction.self::ACTION_SUFFIX; |
| 108 |
|
| 109 |
if (method_exists($this, $actionMethod) === true) { |
| 110 |
$this->{$actionMethod}(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public function render(): void |
| 115 |
{ |
| 116 |
$this->processAction(); |
| 117 |
$this->traitRender(); |
| 118 |
} |
| 119 |
} |
| 120 |
|