PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
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 / DonateFormRouteData.php

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

170 lines 4.5 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\CreateValidatorFromForm;
9 use Give\Framework\FieldsAPI\Exceptions\NameCollisionException;
10 use Give\Framework\Permissions\Facades\UserPermissions;
11 use Give\Framework\Support\Contracts\Arrayable;
12 use WP_Error;
13
14 /**
15 * @since 3.0.0
16 */
17 class DonateFormRouteData implements Arrayable
18 {
19 /**
20 * @var string
21 */
22 public $gatewayId;
23 /**
24 * @var array
25 */
26 private $requestData;
27 /**
28 * @var int
29 */
30 public $formId;
31 /**
32 * @var string
33 */
34 public $originUrl;
35 /**
36 * @var string|null
37 */
38 public $embedId;
39 /**
40 * @var bool
41 */
42 public $isEmbed;
43
44 /**
45 * Convert data from request into DTO
46 *
47 * @since 3.0.0
48 */
49 public static function fromRequest(array $requestData): self
50 {
51 $self = new self();
52 $self->formId = (int)$requestData['formId'];
53 $self->gatewayId = $requestData['gatewayId'];
54 $self->originUrl = $requestData['originUrl'];
55 $self->isEmbed = filter_var($requestData['isEmbed'], FILTER_VALIDATE_BOOLEAN);
56 $self->embedId = $self->isEmbed ? $requestData['embedId'] : null;
57 $self->requestData = $requestData;
58
59 return $self;
60 }
61
62 /**
63 * This method loops over the form schema to
64 * compares the request against the individual fields,
65 * their types and validation rules.
66 *
67 * @since 3.22.0 added givewp_donation_form_fields_validated action
68 * @since 3.14.0 Added form status validation
69 * @since 3.0.0
70 *
71 * @throws DonationFormFieldErrorsException|NameCollisionException|DonationFormForbidden
72 */
73 public function validated(): DonateControllerData
74 {
75 $request = $this->getRequestData();
76 $validData = new DonateControllerData();
77
78 /** @var DonationForm $form */
79 $form = DonationForm::find($this->formId);
80
81 if (!$form || !$this->isValidForm($form)) {
82 throw new DonationFormForbidden();
83 }
84
85 $validator = (new CreateValidatorFromForm())($form->schema(), $request);
86
87 if ($validator->fails()) {
88 $this->throwDonationFormFieldErrorsException($validator->errors());
89 }
90
91 $validatedValues = $validator->validated();
92
93 /**
94 * @since 4.16.0 added $isFinalSubmission (true here: the final donation submission)
95 * @since 3.22.0
96 *
97 * @param array $data validated values in key value pairs
98 * @param bool $isFinalSubmission whether this is the final donation submission
99 */
100 do_action('givewp_donation_form_fields_validated', $validatedValues, true);
101
102 foreach ($validatedValues as $fieldId => $value) {
103 $validData->{$fieldId} = $value;
104 }
105
106 $validData->formTitle = $form->title;
107 $validData->wpUserId = get_current_user_id();
108 $validData->originUrl = $this->originUrl;
109 $validData->embedId = $this->embedId;
110 $validData->isEmbed = $this->isEmbed;
111
112 return $validData;
113 }
114
115 /**
116 * @since 3.0.0
117 */
118 public function getRequestData(): array
119 {
120 return $this->requestData;
121 }
122
123 /**
124 * This loops over an array of errors in the specific FieldAPI format,
125 * and converts them into a WP_Error object that is attached to the
126 * exception and delivered back to the client via JSON.
127 *
128 * @since 3.0.0
129 *
130 * @param array<string, string> $errors
131 *
132 * @throws DonationFormFieldErrorsException
133 */
134 private function throwDonationFormFieldErrorsException(array $errors)
135 {
136 $wpError = new WP_Error();
137
138 foreach ($errors as $id => $error) {
139 $wpError->add($id, $error);
140 }
141
142 throw new DonationFormFieldErrorsException($wpError);
143 }
144
145 /**
146 * @since 3.0.0
147 */
148 public function toArray(): array
149 {
150 return get_object_vars($this);
151 }
152
153 /**
154 * @since 4.14.0.0 update permission capability to use facade
155 * @since 3.14.0
156 */
157 private function isValidForm(DonationForm $form): bool
158 {
159 if ($form->status->isTrash()) {
160 return false;
161 }
162
163 if (!$form->status->isPublished() && !UserPermissions::donationForms()->canEdit()) {
164 return false;
165 }
166
167 return true;
168 }
169 }
170