PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.15.5
GiveWP – Donation Plugin and Fundraising Platform v4.15.5
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / DonationForms / DataTransferObjects / ValidationRouteData.php

ValidationRouteData.php in GiveWP – Donation Plugin and Fundraising Platform 4.15.5, at src/DonationForms/DataTransferObjects/ValidationRouteData.php

152 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonationForms\DataTransferObjects;
4
5 use Give\DonationForms\Exceptions\DonationFormFieldErrorsException;
6 use Give\DonationForms\Exceptions\DonationFormForbidden;
7 use Give\DonationForms\Models\DonationForm;
8 use Give\Framework\FieldsAPI\Actions\CreateValidatorFromFormFields;
9 use Give\Framework\FieldsAPI\Exceptions\NameCollisionException;
10 use Give\Framework\FieldsAPI\Field;
11 use Give\Framework\FieldsAPI\SecurityChallenge;
12 use Give\Framework\Http\Response\Types\JsonResponse;
13 use Give\Framework\Support\Contracts\Arrayable;
14 use Give\Framework\Permissions\Facades\UserPermissions;
15 use WP_Error;
16
17 /**
18 * @since 3.0.0
19 */
20 class ValidationRouteData implements Arrayable
21 {
22 /**
23 * @var array
24 */
25 private $requestData;
26 /**
27 * @var int
28 */
29 public $formId;
30
31 /**
32 * Convert data from request into DTO
33 *
34 * @since 3.0.0
35 */
36 public static function fromRequest(array $requestData): self
37 {
38 $self = new self();
39 $self->formId = (int)$requestData['formId'];
40 $self->requestData = $requestData;
41
42 return $self;
43 }
44
45 /**
46 * This method loops over the form schema to
47 * compares the request against the individual fields,
48 * their types and validation rules.
49 *
50 * @since 4.1.0 updated to exclude security challenge fields during pre-validation
51 * @since 3.22.0 added additional validation for form validity, added givewp_donation_form_fields_validated action
52 * @since 3.0.0
53 *
54 * @throws DonationFormFieldErrorsException|NameCollisionException|DonationFormForbidden
55 */
56 public function validate(): JsonResponse
57 {
58 $request = $this->getRequestData();
59
60 /** @var DonationForm $form */
61 $form = DonationForm::find($this->formId);
62
63 if (!$form || !$this->isValidForm($form)) {
64 throw new DonationFormForbidden();
65 }
66
67 $formFields = array_filter($form->schema()->getFields(), function ($field) use ($request) {
68 return array_key_exists($field->getName(), $request) && !$this->isSecurityChallengeField($field);
69 });
70
71 $validator = (new CreateValidatorFromFormFields())($formFields, $request);
72
73 if ($validator->fails()) {
74 $this->throwDonationFormFieldErrorsException($validator->errors());
75 }
76
77 $validatedValues = $validator->validated();
78
79 /**
80 * @since 3.22.0
81 /**
82 * @param array $validatedValues validated values in key value pairs
83 */
84 do_action('givewp_donation_form_fields_validated', $validatedValues);
85
86 return new JsonResponse(['valid' => true]);
87 }
88
89 /**
90 * @since 3.0.0
91 */
92 public function getRequestData(): array
93 {
94 return $this->requestData;
95 }
96
97 /**
98 * This loops over an array of errors in the specific FieldAPI format,
99 * and converts them into a WP_Error object that is attached to the
100 * exception and delivered back to the client via JSON.
101 *
102 * @since 3.0.0
103 *
104 * @param array<string, string> $errors
105 *
106 * @throws DonationFormFieldErrorsException
107 */
108 private function throwDonationFormFieldErrorsException(array $errors): void
109 {
110 $wpError = new WP_Error();
111
112 foreach ($errors as $id => $error) {
113 $wpError->add($id, $error);
114 }
115
116 throw new DonationFormFieldErrorsException($wpError);
117 }
118
119 /**
120 * @since 4.14.0 update permission capability to use facade
121 * @since 3.22.0
122 */
123 private function isValidForm(DonationForm $form): bool
124 {
125 if ($form->status->isTrash()) {
126 return false;
127 }
128
129 if (!$form->status->isPublished() && !UserPermissions::donationForms()->canEdit()) {
130 return false;
131 }
132
133 return true;
134 }
135
136 /**
137 * @since 3.0.0
138 */
139 public function toArray(): array
140 {
141 return get_object_vars($this);
142 }
143
144 /**
145 * @since 4.1.0
146 */
147 protected function isSecurityChallengeField(Field $field): bool
148 {
149 return is_subclass_of($field, SecurityChallenge::class);
150 }
151 }
152