PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
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 / Form / LegacyConsumer / Commands / SetupFieldValidation.php

SetupFieldValidation.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/Form/LegacyConsumer/Commands/SetupFieldValidation.php

125 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Form\LegacyConsumer\Commands;
4
5 use Give\Form\LegacyConsumer\Actions\DetermineVisibilityForRequest;
6 use Give\Form\LegacyConsumer\Validators\FileUploadValidator;
7 use Give\Framework\FieldsAPI\Field;
8 use Give\Framework\FieldsAPI\File;
9 use Give\Framework\FieldsAPI\Group;
10 use Give\Framework\FieldsAPI\LegacyNodes\CheckboxGroup;
11 use Give\Framework\FieldsAPI\Text;
12 use Give\Framework\FieldsAPI\Types;
13
14 /**
15 * Setup field validation for custom fields on the checkout error check fields hook.
16 *
17 * @NOTE For custom field like file, we can not process validation on ajax required.
18 * This class validate these fields on donation form submission.
19 *
20 * @package Give\Form\LegacyConsumer\Commands
21 * @since 2.10.2
22 */
23 class SetupFieldValidation implements HookCommandInterface
24 {
25
26 /** @var int */
27 private $formId;
28
29 /**
30 * @since 2.10.2
31 *
32 * @param int $formId
33 */
34 public function __construct($formId)
35 {
36 $this->formId = $formId;
37 }
38
39 /**
40 * @since 2.10.2
41 * @since 2.14.0 Handle File field type and custom field type separately
42 *
43 * @param string $hook
44 *
45 * @void
46 */
47 public function __invoke($hook)
48 {
49 $collection = Group::make($hook);
50 do_action("give_fields_$hook", $collection, $this->formId);
51 $collection->walkFields([$this, 'validate']);
52 }
53
54 /**
55 * Validate the given field.
56 *
57 * @since 2.28.0 add shim for CheckboxGroup, only necessary for legacy FFM fields.
58 * @since 2.14.0 Add max length validation for input and textarea field
59 *
60 * @param Field|File|Text $field
61 *
62 * @void
63 */
64 public function validate(Field $field)
65 {
66 if( ! $this->isFieldVisible( $field ) ) {
67 return;
68 }
69
70 if ($field->getType() === Types::FILE) {
71 // Are we processing donation form validation on ajax?
72 if (isset($_POST['give_ajax'])) {
73 return;
74 }
75
76 $validator = new FileUploadValidator($field);
77 $validator();
78 } elseif (in_array($field->getType(), Types::all(), true) || $field->getType() === CheckboxGroup::TYPE) {
79 if ( $field->isRequired() ) {
80 if( ! isset($_POST[$field->getName()]) || empty($_POST[$field->getName()]) ) {
81 give_set_error(
82 "give-{$field->getName()}-required-field-missing",
83 $field->getRequiredError()['error_message']
84 );
85 }
86 }
87
88 if (
89 in_array($field->getType(), [Types::TEXT, Types::TEXTAREA]) &&
90 $field->getMaxLength() &&
91 isset($_POST[$field->getName()]) &&
92 strlen($_POST[$field->getName()]) > $field->getMaxLength()
93 ) {
94 give_set_error(
95 "give-{$field->getName()}-required-field-missing",
96 sprintf(
97 esc_html__('%1$s field value exceed allowed character limit.', 'give'),
98 $field->getName()
99 )
100 );
101 }
102 } else {
103 /**
104 * Use this action to validate custom field which does not exist in field api.
105 *
106 * @since 2.14.0
107 *
108 * @param Field $field
109 * @param int $formId
110 */
111 do_action('give_fields_legacy_consumer_validate_field', $field, $this->formId);
112 }
113 }
114
115 /**
116 * @param Field $field
117 * @return bool
118 */
119 protected function isFieldVisible(Field $field)
120 {
121 $determineVisibilityAction = new DetermineVisibilityForRequest( $field, $_POST );
122 return $determineVisibilityAction->__invoke();
123 }
124 }
125