| 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 4.16.0 added $isFinalSubmission (false here: per-step validation, not the final submission) |
| 81 |
* @since 3.22.0 |
| 82 |
* |
| 83 |
* @param array $validatedValues validated values in key value pairs |
| 84 |
* @param bool $isFinalSubmission whether this is the final donation submission |
| 85 |
*/ |
| 86 |
do_action('givewp_donation_form_fields_validated', $validatedValues, false); |
| 87 |
|
| 88 |
return new JsonResponse(['valid' => true]); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @since 3.0.0 |
| 93 |
*/ |
| 94 |
public function getRequestData(): array |
| 95 |
{ |
| 96 |
return $this->requestData; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* This loops over an array of errors in the specific FieldAPI format, |
| 101 |
* and converts them into a WP_Error object that is attached to the |
| 102 |
* exception and delivered back to the client via JSON. |
| 103 |
* |
| 104 |
* @since 3.0.0 |
| 105 |
* |
| 106 |
* @param array<string, string> $errors |
| 107 |
* |
| 108 |
* @throws DonationFormFieldErrorsException |
| 109 |
*/ |
| 110 |
private function throwDonationFormFieldErrorsException(array $errors): void |
| 111 |
{ |
| 112 |
$wpError = new WP_Error(); |
| 113 |
|
| 114 |
foreach ($errors as $id => $error) { |
| 115 |
$wpError->add($id, $error); |
| 116 |
} |
| 117 |
|
| 118 |
throw new DonationFormFieldErrorsException($wpError); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @since 4.14.0 update permission capability to use facade |
| 123 |
* @since 3.22.0 |
| 124 |
*/ |
| 125 |
private function isValidForm(DonationForm $form): bool |
| 126 |
{ |
| 127 |
if ($form->status->isTrash()) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
if (!$form->status->isPublished() && !UserPermissions::donationForms()->canEdit()) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @since 3.0.0 |
| 140 |
*/ |
| 141 |
public function toArray(): array |
| 142 |
{ |
| 143 |
return get_object_vars($this); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @since 4.1.0 |
| 148 |
*/ |
| 149 |
protected function isSecurityChallengeField(Field $field): bool |
| 150 |
{ |
| 151 |
return is_subclass_of($field, SecurityChallenge::class); |
| 152 |
} |
| 153 |
} |
| 154 |
|