PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.0
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.0, at includes/blocks/form-builder/validate-form-data.php

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