← All changes
|
includes/blocks/form-builder/validate-form-data.php
+59
-10
2.9.1
→
2.14.0
View file →
| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | 2 | namespace ABlocks\Blocks\FormBuilder; |
| 4 | 3 | |
| 5 | 4 | use ABlocks\Blocks\FormBuilder\Actions\Interfaces\FormSubmissionAction; |
| 6 | 5 | |
| @@ -27,11 +26,11 @@ | ||
| 27 | 26 | |
| 28 | 27 | /** @var $filtered_data */ |
| 29 | 28 | private ?array $filtered_data = []; |
| 30 | 29 | /** @var $errors */ |
| 31 | - private ?array $errors = []; | |
| 30 | + public ?array $errors = []; | |
| 32 | 31 | /** @var $messages */ |
| 33 | - private ?array $messages = [ | |
| 32 | + public ?array $messages = [ | |
| 34 | 33 | 'default' => [], |
| 35 | 34 | ]; |
| 36 | 35 | /** @var $output */ |
| 37 | 36 | private ?array $output = []; |
| @@ -38,8 +37,11 @@ | ||
| 38 | 37 | |
| 39 | 38 | /** @var $form_info */ |
| 40 | 39 | public ?array $form_info = []; |
| 41 | 40 | |
| 41 | + /** @var $files */ | |
| 42 | + public ?array $files = []; | |
| 43 | + | |
| 42 | 44 | /** |
| 43 | 45 | * Constructor |
| 44 | 46 | * |
| 45 | 47 | * @param array $block_data |
| @@ -47,12 +49,12 @@ | ||
| 47 | 49 | */ |
| 48 | 50 | public function __construct( ?array $block_data, ?array $input_data ) { |
| 49 | 51 | $this->block_data = $block_data; |
| 50 | 52 | $this->input_data = $input_data; |
| 51 | - | |
| 52 | - $this->is_form_data_empty = count( $input_data ) === 0; | |
| 53 | - | |
| 53 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 54 | + $this->is_form_data_empty = ( count( $input_data ) + count( $_FILES ) ) === 0; | |
| 54 | 55 | $this->filter_data_by_whitelisted_key(); |
| 56 | + $this->filtered_data = apply_filters( 'ablocks/form_builder/filtered_data', $this->filtered_data, $this->block_data ); | |
| 55 | 57 | |
| 56 | 58 | $this->form_info = [ |
| 57 | 59 | 'info' => [ |
| 58 | 60 | 'type' => sanitize_text_field( $block_data['parentAttributes']['formType'] ?? null ), |
| @@ -69,8 +71,17 @@ | ||
| 69 | 71 | $this->set_error_message( __( 'Form is empty.', 'ablocks' ) ); |
| 70 | 72 | break; |
| 71 | 73 | } |
| 72 | 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 | + | |
| 73 | 84 | /** |
| 74 | 85 | * @method filter_data_by_whitelisted_key |
| 75 | 86 | * @return array |
| 76 | 87 | */ |
| @@ -77,14 +88,17 @@ | ||
| 77 | 88 | private function filter_data_by_whitelisted_key(): array { |
| 78 | 89 | if ( ( $this->block_data['innerBlocks'][0]['blockName'] ?? '' ) === 'ablocks/form-multi-step' ) { |
| 79 | 90 | |
| 80 | 91 | foreach ( $this->block_data['innerBlocks'][0]['innerBlocks'] ?? [] as $block ) { |
| 81 | - foreach ( $block['innerBlocks'] as ['attributes' => $attr] ) { | |
| 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'] ); | |
| 82 | 94 | $name = $attr['name'] ?? false; |
| 83 | 95 | // check current input name exists in $all_fields |
| 84 | 96 | if ( $name && array_key_exists( $name, $this->input_data ) ) { |
| 85 | 97 | $this->filtered_data[ $name ] = [ |
| 86 | - 'value' => sanitize_text_field( $this->input_data[ $name ] ), // sanitize value | |
| 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 ] ), | |
| 87 | 101 | ]; |
| 88 | 102 | } |
| 89 | 103 | } |
| 90 | 104 | } |
| @@ -93,14 +107,17 @@ | ||
| 93 | 107 | ! $this->is_form_data_empty |
| 94 | 108 | ) { |
| 95 | 109 | $this->filtered_data = []; |
| 96 | 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'] ); | |
| 97 | 112 | $name = $input_element_data['attributes']['name'] ?? false; |
| 98 | 113 | [ 'placeholder' ] ?? false; |
| 99 | 114 | // check current input name exists in $all_fields |
| 100 | 115 | if ( $name && array_key_exists( $name, $this->input_data ) ) { |
| 101 | 116 | $this->filtered_data[ $name ] = [ |
| 102 | - 'value' => sanitize_text_field( $this->input_data[ $name ] ), // sanitize value | |
| 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 ] ), | |
| 103 | 120 | ]; |
| 104 | 121 | } |
| 105 | 122 | } |
| 106 | 123 | }//end if |
| @@ -165,9 +182,9 @@ | ||
| 165 | 182 | public function set_message( string $msg, string $key = 'default' ) { |
| 166 | 183 | if ( in_array( $msg, array_key_exists( $key, $this->messages ) ? $this->messages[ $key ] : [], true ) ) { |
| 167 | 184 | return; |
| 168 | 185 | } |
| 169 | - $this->messages[ $key ][] = sanitize_text_field( $msg ); | |
| 186 | + $this->messages[ $key ][] = sanitize_text_field( $this->apply_vars( $msg ) ); | |
| 170 | 187 | } |
| 171 | 188 | /** |
| 172 | 189 | * Check msg |
| 173 | 190 | * |
| @@ -175,8 +192,40 @@ | ||
| 175 | 192 | */ |
| 176 | 193 | public function has_message() { |
| 177 | 194 | return count( $this->messages ) > 0; |
| 178 | 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 | + | |
| 179 | 228 | /** |
| 180 | 229 | * Get output as array |
| 181 | 230 | * |
| 182 | 231 | * @return string |