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