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 / DonateFormRouteData.php

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

168 lines 4.3 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 3.22.0
95 *
96 * @param array $data validated values in key value pairs
97 */
98 do_action('givewp_donation_form_fields_validated', $validatedValues);
99
100 foreach ($validatedValues as $fieldId => $value) {
101 $validData->{$fieldId} = $value;
102 }
103
104 $validData->formTitle = $form->title;
105 $validData->wpUserId = get_current_user_id();
106 $validData->originUrl = $this->originUrl;
107 $validData->embedId = $this->embedId;
108 $validData->isEmbed = $this->isEmbed;
109
110 return $validData;
111 }
112
113 /**
114 * @since 3.0.0
115 */
116 public function getRequestData(): array
117 {
118 return $this->requestData;
119 }
120
121 /**
122 * This loops over an array of errors in the specific FieldAPI format,
123 * and converts them into a WP_Error object that is attached to the
124 * exception and delivered back to the client via JSON.
125 *
126 * @since 3.0.0
127 *
128 * @param array<string, string> $errors
129 *
130 * @throws DonationFormFieldErrorsException
131 */
132 private function throwDonationFormFieldErrorsException(array $errors)
133 {
134 $wpError = new WP_Error();
135
136 foreach ($errors as $id => $error) {
137 $wpError->add($id, $error);
138 }
139
140 throw new DonationFormFieldErrorsException($wpError);
141 }
142
143 /**
144 * @since 3.0.0
145 */
146 public function toArray(): array
147 {
148 return get_object_vars($this);
149 }
150
151 /**
152 * @since 4.14.0.0 update permission capability to use facade
153 * @since 3.14.0
154 */
155 private function isValidForm(DonationForm $form): bool
156 {
157 if ($form->status->isTrash()) {
158 return false;
159 }
160
161 if (!$form->status->isPublished() && !UserPermissions::donationForms()->canEdit()) {
162 return false;
163 }
164
165 return true;
166 }
167 }
168