ApiService.php
7 months ago
ElementService.php
7 months ago
EmailService.php
7 months ago
FormService.php
7 months ago
LevelOrderService.php
7 months ago
LevelService.php
7 months ago
MembershipService.php
7 months ago
RedirectService.php
7 months ago
SanitizationService.php
7 months ago
StatisticsService.php
7 months ago
UserService.php
7 months ago
FormService.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Service; |
| 4 | |
| 5 | use FapiMember\Container\Container; |
| 6 | use FapiMember\Model\Enums\Types\FormValueType; |
| 7 | use FapiMember\Model\Enums\Types\RequestMethodType; |
| 8 | use FapiMember\Model\Enums\UserPermission; |
| 9 | use RuntimeException; |
| 10 | |
| 11 | class FormService |
| 12 | { |
| 13 | private SanitizationService $sanitizationService; |
| 14 | |
| 15 | public function __construct() |
| 16 | { |
| 17 | $this->sanitizationService = Container::get(SanitizationService::class); |
| 18 | } |
| 19 | |
| 20 | public function loadPostValue(string $key, string $sanitizer, mixed $default = null): mixed |
| 21 | { |
| 22 | return $this->loadFormValue(RequestMethodType::POST, $key, $sanitizer, $default); |
| 23 | } |
| 24 | |
| 25 | public function loadGetValue(string $key, string $sanitizer, mixed $default = null): mixed |
| 26 | { |
| 27 | return $this->loadFormValue(RequestMethodType::GET, $key, $sanitizer, $default); |
| 28 | } |
| 29 | |
| 30 | public function loadFormValue( |
| 31 | string $method, |
| 32 | string $key, |
| 33 | string $sanitizer, |
| 34 | mixed $default = null, |
| 35 | ): mixed |
| 36 | { |
| 37 | switch ($method) { |
| 38 | case RequestMethodType::GET: |
| 39 | $values = $_GET; |
| 40 | break; |
| 41 | case RequestMethodType::POST: |
| 42 | $values = $_POST; |
| 43 | break; |
| 44 | default: |
| 45 | throw new RuntimeException('Not implemented method.'); |
| 46 | } |
| 47 | |
| 48 | $rawValue = (isset($values[$key])) ? $values[$key] : $default; |
| 49 | |
| 50 | if ($rawValue === null && $sanitizer !== FormValueType::CHECKBOX) { |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | $sanitizerFunction = [$this->sanitizationService, $sanitizer]; |
| 55 | |
| 56 | if (!is_callable($sanitizerFunction)) { |
| 57 | throw new RuntimeException('Sanitizer should be callable.'); |
| 58 | } |
| 59 | |
| 60 | return $sanitizerFunction($rawValue, $default); |
| 61 | } |
| 62 | |
| 63 | public function verifyNonce($hook): void |
| 64 | { |
| 65 | $nonce = sprintf( 'fapi_member_%s_nonce', $hook ); |
| 66 | |
| 67 | if (!isset($_POST[$nonce]) |
| 68 | || !wp_verify_nonce($_POST[$nonce], $nonce) |
| 69 | ) { |
| 70 | wp_die(__('Zabezpečení formuláře neumožnilo zpracování, zkuste obnovit stránku a odeslat znovu.', 'fapi-member')); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public function verifyNonceAndCapability($hook): void |
| 75 | { |
| 76 | $this->verifyNonce($hook); |
| 77 | |
| 78 | if (!current_user_can(UserPermission::REQUIRED_CAPABILITY)) { |
| 79 | wp_die(__('Nemáte potřebná oprvánění.', 'fapi-member')); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |
| 84 |