PluginProbe ʕ •ᴥ•ʔ
FAPI Member / 2.2.24
FAPI Member v2.2.24
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / src / Service / FormService.php
fapi-member / src / Service Last commit date
ApiService.php 11 months ago ElementService.php 11 months ago EmailService.php 11 months ago FormService.php 11 months ago LevelOrderService.php 11 months ago LevelService.php 11 months ago MembershipService.php 11 months ago RedirectService.php 11 months ago SanitizationService.php 11 months ago StatisticsService.php 11 months ago UserService.php 11 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