AddHoneyPotFieldToDonationForms.php
1 year ago
ConvertConsentBlockToFieldsApi.php
2 years ago
ConvertDonationAmountBlockToFieldsApi.php
2 years ago
ConvertDonationFormBlocksToFieldsApi.php
1 year ago
ConvertQueryDataToDonationForm.php
2 years ago
DispatchDonateControllerDonationCreatedListeners.php
1 year ago
DispatchDonateControllerSubscriptionCreatedListeners.php
2 years ago
GenerateAuthUrl.php
2 years ago
GenerateDonateRouteSignatureArgs.php
2 years ago
GenerateDonateRouteUrl.php
2 years ago
GenerateDonationConfirmationReceiptUrl.php
2 years ago
GenerateDonationConfirmationReceiptViewRouteUrl.php
2 years ago
GenerateDonationFormPreviewRouteUrl.php
2 years ago
GenerateDonationFormValidationRouteUrl.php
2 years ago
GenerateDonationFormViewRouteUrl.php
2 years ago
GetOrCreateDonor.php
2 years ago
PrintFormMetaTags.php
1 year ago
ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe.php
1 year ago
SanitizeDonationFormPreviewRequest.php
2 years ago
StoreBackwardsCompatibleFormMeta.php
2 years ago
AddHoneyPotFieldToDonationForms.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Actions; |
| 4 | |
| 5 | use Give\DonationForms\Rules\HoneyPotRule; |
| 6 | use Give\Framework\FieldsAPI\DonationForm; |
| 7 | use Give\Framework\FieldsAPI\Exceptions\EmptyNameException; |
| 8 | use Give\Framework\FieldsAPI\Honeypot; |
| 9 | |
| 10 | /** |
| 11 | * @since 3.16.2 |
| 12 | */ |
| 13 | class AddHoneyPotFieldToDonationForms |
| 14 | { |
| 15 | /** |
| 16 | * @since 3.17.0 added parameter $honeypotFieldName |
| 17 | * @since 3.16.2 |
| 18 | * @throws EmptyNameException |
| 19 | */ |
| 20 | public function __invoke(DonationForm $form, string $honeypotFieldName): void |
| 21 | { |
| 22 | $formNodes = $form->all(); |
| 23 | $lastSection = $form->count() ? $formNodes[$form->count() - 1] : null; |
| 24 | |
| 25 | if ($lastSection && is_null($form->getNodeByName($honeypotFieldName))) { |
| 26 | $field = Honeypot::make($honeypotFieldName) |
| 27 | ->label($this->generateLabelFromFieldName($honeypotFieldName)) |
| 28 | ->scope('honeypot') |
| 29 | ->showInAdmin(false) |
| 30 | ->showInReceipt(false) |
| 31 | ->rules(new HoneyPotRule()); |
| 32 | |
| 33 | $lastSection->append($field); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @since 3.17.0 |
| 39 | */ |
| 40 | private function generateLabelFromFieldName(string $honeypotFieldName): string |
| 41 | { |
| 42 | return ucwords(trim(implode(" ", preg_split("/(?=[A-Z])/", $honeypotFieldName)))); |
| 43 | } |
| 44 | } |
| 45 |