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