PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.3
JetFormBuilder — Dynamic Blocks Form Builder v3.1.3
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / blocks / validation.php
jetformbuilder / includes / blocks Last commit date
advanced-rules 2 years ago button-types 2 years ago conditional-block 2 years ago exceptions 2 years ago modules 2 years ago render 2 years ago ssr-validation 2 years ago types 2 years ago validation-messages 2 years ago action-buttons-manager.php 2 years ago block-helper.php 2 years ago blocks-repository-base.php 2 years ago default-blocks-repository.php 2 years ago dynamic-value.php 2 years ago form-builder-blocks-repository.php 2 years ago module.php 2 years ago native-block-wrapper-attributes.php 2 years ago validation.php 2 years ago
validation.php
281 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Blocks;
5
6 use Jet_Form_Builder\Blocks\Advanced_Rules\Match_Not_Regexp_Rule;
7 use Jet_Form_Builder\Blocks\Advanced_Rules\Match_Regexp_Rule;
8 use Jet_Form_Builder\Blocks\Advanced_Rules\Must_Contain_Characters_Rule;
9 use Jet_Form_Builder\Blocks\Advanced_Rules\Must_Equal_Rule;
10 use Jet_Form_Builder\Blocks\Advanced_Rules\Must_Not_Contain_Characters_Rule;
11 use Jet_Form_Builder\Blocks\Advanced_Rules\Server_Side_Rule;
12 use Jet_Form_Builder\Blocks\Ssr_Validation\Validation_Callbacks;
13 use Jet_Form_Builder\Blocks\Types\Base;
14 use Jet_Form_Builder\Blocks\Validation_Messages\Base_Message;
15 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Char_Max;
16 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Char_Min;
17 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Date_Max;
18 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Date_Min;
19 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Empty_Value;
20 use Jet_Form_Builder\Blocks\Validation_Messages\Is_File_Ext;
21 use Jet_Form_Builder\Blocks\Validation_Messages\Is_File_Size;
22 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Files_Max;
23 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Not_Complete_Mask;
24 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Not_Valid_Email;
25 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Not_Valid_Url;
26 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Number_Max;
27 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Number_Min;
28 use Jet_Form_Builder\Classes\Arrayable\Array_Tools;
29 use Jet_Form_Builder\Classes\Arrayable\Arrayable;
30 use Jet_Form_Builder\Classes\Instance_Trait;
31 use Jet_Form_Builder\Classes\Tools;
32 use Jet_Form_Builder\Plugin;
33
34 // If this file is called directly, abort.
35 if ( ! defined( 'WPINC' ) ) {
36 die;
37 }
38
39 /**
40 * @method static Validation instance()
41 *
42 * Class Validation_Messages
43 * @package Jet_Form_Builder\Blocks
44 */
45 class Validation implements Arrayable {
46
47 use Instance_Trait;
48
49 const FORMAT_ADVANCED = 'advanced';
50 const FORMAT_BROWSER = 'browser';
51 const HANDLE = 'jet-fb-advanced-reporting';
52
53 /**
54 * @var Base_Message[]
55 */
56 private $messages;
57 public $callbacks;
58 private $settings = array();
59 private $inline_messages = array();
60
61 public function __construct() {
62 $this->messages = apply_filters(
63 'jet-form-builder/validation-messages',
64 $this->get_messages()
65 );
66
67 $this->callbacks = new Validation_Callbacks();
68
69 add_filter(
70 'jet-form-builder/before-start-form',
71 array( $this, 'add_validation_messages_global' )
72 );
73 add_action(
74 'jet-form-builder/before-start-form-row',
75 array( $this, 'add_validation_block' )
76 );
77 add_action(
78 'wp_enqueue_scripts',
79 array( $this, 'register_scripts' )
80 );
81
82 /**
83 * @link https://github.com/Crocoblock/issues-tracker/issues/1542
84 */
85 add_action(
86 'jet_plugins/frontend/register_scripts',
87 array( $this, 'register_scripts' )
88 );
89 }
90
91 /**
92 * @return Base_Message[]
93 */
94 private function get_messages(): array {
95 return array(
96 new Is_Empty_Value(),
97 new Is_Number_Min(),
98 new Is_Number_Max(),
99 new Is_Char_Min(),
100 new Is_Char_Max(),
101 new Is_Not_Valid_Email(),
102 new Is_Not_Valid_Url(),
103 new Is_Not_Complete_Mask(),
104 new Is_Files_Max(),
105 new Is_File_Size(),
106 new Is_File_Ext(),
107 new Is_Date_Min(),
108 new Is_Date_Max(),
109 );
110 }
111
112 public function register_scripts() {
113 wp_register_script(
114 self::HANDLE,
115 Plugin::instance()->plugin_url( 'assets/js/frontend/advanced.reporting.js' ),
116 array(
117 Manager::MAIN_SCRIPT_HANDLE,
118 ),
119 Plugin::instance()->get_version(),
120 true
121 );
122 }
123
124 public function add_validation_messages_global( string $markup, bool $force = false ): string {
125 $this->settings = $this->get_settings();
126 $form_id = jet_fb_live()->form_id;
127
128 if (
129 ( ! $this->is_advanced_form() && ! $force ) ||
130 in_array( $form_id, $this->inline_messages, true )
131 ) {
132 return $markup;
133 }
134
135 $data = Tools::encode_json( $this->settings );
136
137 wp_add_inline_script(
138 Manager::MAIN_SCRIPT_HANDLE,
139 "
140 window.JetFormsValidation = window.JetFormsValidation ?? {};
141 window.JetFormsValidation[ {$form_id} ] = $data;
142 "
143 );
144
145 add_action(
146 'wp_enqueue_scripts',
147 function () use ( $form_id, $data ) {
148 wp_add_inline_script(
149 Manager::MAIN_SCRIPT_HANDLE,
150 "
151 window.JetFormsValidation = window.JetFormsValidation ?? {};
152 window.JetFormsValidation[ {$form_id} ] = $data;
153 "
154 );
155 },
156 20
157 );
158
159 $this->inline_messages[] = $form_id;
160
161 return $markup;
162 }
163
164 public function add_validation_block( Base $block ) {
165 /**
166 * If in post meta enable Advanced validation
167 * or right in block settings
168 */
169 if ( ! $this->is_advanced( $block ) ) {
170 return;
171 }
172 wp_enqueue_script( self::HANDLE );
173
174 $this->add_validation_messages_global( '', true );
175
176 $type = $this->get_block_type( $block );
177 $rules = $block->block_attrs['validation']['rules'] ?? array();
178
179 $block->add_attribute( 'data-validation-type', $type ?: 'inherit' );
180
181 if ( ! empty( $rules ) ) {
182 $this->prepare_rules( $rules );
183
184 $block->add_attribute(
185 'data-validation-rules',
186 Tools::encode_json( $rules )
187 );
188 }
189
190 /**
191 * If advanced validation not enabled right in block settings
192 */
193 if ( self::FORMAT_ADVANCED !== $type ) {
194 return;
195 }
196
197 $messages = $block->block_attrs['validation']['messages'] ?? array();
198
199 if ( ! empty( $messages ) ) {
200 $block->add_attribute( 'data-validation-messages', Tools::encode_json( $messages ) );
201 }
202 }
203
204 public function get_settings(): array {
205 $validation = jet_form_builder()->post_type->get_validation( jet_fb_live()->form_id );
206
207 $response = array(
208 'type' => $validation['type'] ?? self::FORMAT_BROWSER,
209 'messages' => array(),
210 );
211
212 foreach ( $this->messages as $message ) {
213 $response['messages'][ $message->get_id() ] = (
214 $validation['messages'][ $message->get_id() ] ?? $message->get_initial()
215 );
216 }
217
218 return $response;
219 }
220
221 public function is_advanced( Base $block ): bool {
222 $type = $this->get_block_type( $block );
223
224 return $type
225 ? self::FORMAT_ADVANCED === $type
226 : $this->is_advanced_form();
227 }
228
229 public function is_advanced_form(): bool {
230 return self::FORMAT_ADVANCED === ( $this->settings['type'] ?? '' );
231 }
232
233 protected function get_block_type( Base $block ): string {
234 return $block->block_attrs['validation']['type'] ?? '';
235 }
236
237 public function formats(): array {
238 return array(
239 array(
240 'value' => self::FORMAT_BROWSER,
241 'label' => __( 'Default', 'jet-form-builder' ),
242 'title' => __( 'Browser native validation', 'jet-form-builder' ),
243 ),
244 array(
245 'value' => self::FORMAT_ADVANCED,
246 'label' => __( 'Advanced', 'jet-form-builder' ),
247 'title' => __( 'More flexible JetFormBuilder\'s validation', 'jet-form-builder' ),
248 ),
249 );
250 }
251
252 public function rule_types(): array {
253 return Array_Tools::to_array(
254 array(
255 new Must_Equal_Rule(),
256 new Must_Contain_Characters_Rule(),
257 new Must_Not_Contain_Characters_Rule(),
258 new Match_Regexp_Rule(),
259 new Match_Not_Regexp_Rule(),
260 new Server_Side_Rule(),
261 )
262 );
263 }
264
265 public function to_array(): array {
266 return array(
267 'messages' => Array_Tools::to_array( $this->messages ),
268 'ssr_callbacks' => Array_Tools::to_array( $this->callbacks->get_items() ),
269 'formats' => $this->formats(),
270 'rule_types' => $this->rule_types(),
271 );
272 }
273
274 public function prepare_rules( array &$rules ) {
275 foreach ( $rules as &$rule ) {
276 $rule['value'] = jet_fb_parse_dynamic( $rule['value'] ?? '' );
277 }
278 }
279
280 }
281