Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add SecurityChallenge Field to Fields API #7865

Merged
merged 4 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/DonationForms/DataTransferObjects/ValidationRouteData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Give\DonationForms\Models\DonationForm;
use Give\Framework\FieldsAPI\Actions\CreateValidatorFromFormFields;
use Give\Framework\FieldsAPI\Exceptions\NameCollisionException;
use Give\Framework\FieldsAPI\Field;
use Give\Framework\FieldsAPI\SecurityChallenge;
use Give\Framework\Http\Response\Types\JsonResponse;
use Give\Framework\Support\Contracts\Arrayable;
use WP_Error;
Expand Down Expand Up @@ -44,6 +46,7 @@ public static function fromRequest(array $requestData): self
* compares the request against the individual fields,
* their types and validation rules.
*
* @unreleased updated to exclude security challenge fields during pre-validation
* @since 3.22.0 added additional validation for form validity, added givewp_donation_form_fields_validated action
* @since 3.0.0
*
Expand All @@ -60,8 +63,8 @@ public function validate(): JsonResponse
throw new DonationFormForbidden();
}

$formFields = array_filter($form->schema()->getFields(), static function ($field) use ($request) {
return array_key_exists($field->getName(), $request);
$formFields = array_filter($form->schema()->getFields(), function ($field) use ($request) {
return array_key_exists($field->getName(), $request) && !$this->isSecurityChallengeField($field);
});

$validator = (new CreateValidatorFromFormFields())($formFields, $request);
Expand Down Expand Up @@ -135,4 +138,12 @@ public function toArray(): array
{
return get_object_vars($this);
}

/**
* @unreleased
*/
protected function isSecurityChallengeField(Field $field): bool
{
return is_subclass_of($field, SecurityChallenge::class);
}
}
16 changes: 16 additions & 0 deletions src/Framework/FieldsAPI/SecurityChallenge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Give\Framework\FieldsAPI;


/**
* Security challenge fields are a special snowflake.
* They can typically only be validated once on the server.
* Extending this abstract field will ensure that the field is not validated on the server
* before the form is fully submitted, avoiding pre-validation conflicting endpoints.
*
* @unreleased
*/
abstract class SecurityChallenge extends Field
{
}