PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / blocks / form-builder / validate-form-data.php

validate-form-data.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at includes/blocks/form-builder/validate-form-data.php

229 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocks\Blocks\FormBuilder;
4
5 use ABlocks\Blocks\FormBuilder\Actions\Interfaces\FormSubmissionAction;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10 /**
11 * @class Validateformdata
12 * Validate form data by whitelised key & sanitize input value
13 * Add & call action
14 */
15 final class ValidateFormData {
16
17
18 /** @var $is_form_data_empty */
19 private bool $is_form_data_empty;
20 /** @var $stop_action_exec */
21 public array $state_data = [];
22
23 /** @var $block_data */
24 private ?array $block_data;
25 /** @var $input_data */
26 private ?array $input_data;
27
28 /** @var $filtered_data */
29 private ?array $filtered_data = [];
30 /** @var $errors */
31 private ?array $errors = [];
32 /** @var $messages */
33 private ?array $messages = [
34 'default' => [],
35 ];
36 /** @var $output */
37 private ?array $output = [];
38
39 /** @var $form_info */
40 public ?array $form_info = [];
41
42 /**
43 * Constructor
44 *
45 * @param array $block_data
46 * @param array $input_data
47 */
48 public function __construct( ?array $block_data, ?array $input_data ) {
49 $this->block_data = $block_data;
50 $this->input_data = $input_data;
51
52 $this->is_form_data_empty = count( $input_data ) === 0;
53
54 $this->filter_data_by_whitelisted_key();
55
56 $this->form_info = [
57 'info' => [
58 'type' => sanitize_text_field( $block_data['parentAttributes']['formType'] ?? null ),
59 'postId' => sanitize_text_field( $block_data['parentAttributes']['postId'] ?? null ),
60 'email' => sanitize_email( $this->filtered_data['email']['value'] ?? null ),
61 'actions' => $block_data['parentAttributes']['formActions'] ?? [],
62 'config' => $block_data['parentAttributes'] ?? [],
63 ],
64 'data' => $this->filtered_data
65 ];
66
67 switch ( true ) {
68 case $this->is_form_data_empty || count( $this->filtered_data ) === 0:
69 $this->set_error_message( __( 'Form is empty.', 'ablocks' ) );
70 break;
71 }
72 }
73 /**
74 * @method filter_data_by_whitelisted_key
75 * @return array
76 */
77 private function filter_data_by_whitelisted_key(): array {
78 if ( ( $this->block_data['innerBlocks'][0]['blockName'] ?? '' ) === 'ablocks/form-multi-step' ) {
79
80 foreach ( $this->block_data['innerBlocks'][0]['innerBlocks'] ?? [] as $block ) {
81 foreach ( $block['innerBlocks'] as ['attributes' => $attr] ) {
82 $name = $attr['name'] ?? false;
83 // check current input name exists in $all_fields
84 if ( $name && array_key_exists( $name, $this->input_data ) ) {
85 $this->filtered_data[ $name ] = [
86 'value' => sanitize_text_field( $this->input_data[ $name ] ), // sanitize value
87 ];
88 }
89 }
90 }
91 } elseif (
92 isset( $this->block_data['innerBlocks'] ) &&
93 ! $this->is_form_data_empty
94 ) {
95 $this->filtered_data = [];
96 foreach ( $this->block_data['innerBlocks'] as $input_element_data ) {
97 $name = $input_element_data['attributes']['name'] ?? false;
98 [ 'placeholder' ] ?? false;
99 // check current input name exists in $all_fields
100 if ( $name && array_key_exists( $name, $this->input_data ) ) {
101 $this->filtered_data[ $name ] = [
102 'value' => sanitize_text_field( $this->input_data[ $name ] ), // sanitize value
103 ];
104 }
105 }
106 }//end if
107
108 return $this->filtered_data;
109 }
110 /**
111 * Get Filtered data
112 *
113 * @return void
114 */
115 // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
116 public function get_data() {
117 return $this->filtered_data;
118 }
119 /**
120 * Get errors as string
121 *
122 * @return string
123 */
124 public function get_error_message() {
125 return implode( "\n", $this->errors );
126 }
127 /**
128 * Set error
129 *
130 * @param string $error
131 * @return void
132 */
133 public function set_error_message( string $error ) {
134 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
135 if ( in_array( $error, $this->errors ) ) {
136 return;
137 }
138 $this->errors[] = sanitize_text_field( $error );
139 }
140 /**
141 * Check error
142 *
143 * @return bool
144 */
145 public function has_error() {
146 return count( $this->errors ) > 0;
147 }
148 /**
149 * Get msg as string
150 *
151 * @param string $key
152 * @return string
153 */
154 public function get_message( string $key = 'default' ) {
155 return implode( "\n", $this->messages[ $key ] );
156 }
157
158 /**
159 * Set msg
160 *
161 * @param string $msg
162 * @param string $key
163 * @return void
164 */
165 public function set_message( string $msg, string $key = 'default' ) {
166 if ( in_array( $msg, array_key_exists( $key, $this->messages ) ? $this->messages[ $key ] : [], true ) ) {
167 return;
168 }
169 $this->messages[ $key ][] = sanitize_text_field( $msg );
170 }
171 /**
172 * Check msg
173 *
174 * @return bool
175 */
176 public function has_message() {
177 return count( $this->messages ) > 0;
178 }
179 /**
180 * Get output as array
181 *
182 * @return string
183 */
184 public function get_output(): array {
185 return $this->output;
186 }
187 /**
188 * Set output
189 *
190 * @param string $key
191 * @param mixed $data
192 * @return void
193 */
194 public function set_output( string $key, $data ): void {
195 $this->output[ $key ] = $data;
196 }
197 /**
198 * Check output
199 *
200 * @return bool
201 */
202 public function has_output(): bool {
203 return count( $this->output ) > 0;
204 }
205 /**
206 * Add and call action
207 *
208 * @param array $classes
209 */
210 public function actions( array $classes = [] ) {
211 // check classes var is not empty & no error
212 if ( count( $classes ) && $this->has_error() === false ) {
213 // iterate
214 foreach ( $classes as $class ) {
215 // instantiate action class
216 if ( ! class_exists( $class ) ) {
217 continue;
218 }
219 $action_obj = new $class( $this );
220 // check for object is a instance of valid action class
221 if ( $action_obj instanceof FormSubmissionAction ) {
222 // call action
223 $action_obj->action();
224 }
225 }
226 }
227 }
228 }
229