PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.5.2
JetFormBuilder — Dynamic Blocks Form Builder v3.5.2
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 / modules / validation / module.php
jetformbuilder / modules / validation Last commit date
advanced-rules 1 year ago handlers 11 months ago messages 2 years ago post-type 2 years ago rest-api 11 months ago ssr 1 year ago class-validation-handlers.php 11 months ago module.php 11 months ago rules-controller.php 2 years ago
module.php
384 lines
1 <?php
2
3
4 namespace JFB_Modules\Validation;
5
6 use Jet_Form_Builder\Admin\Editor;
7 use Jet_Form_Builder\Blocks\Types\Base;
8 use Jet_Form_Builder\Classes\Arrayable\Array_Tools;
9 use Jet_Form_Builder\Classes\Tools;
10 use Jet_Form_Builder\Exceptions\Repository_Exception;
11 use Jet_Form_Builder\Plugin;
12 use JFB_Components\Module\Base_Module_After_Install_It;
13 use JFB_Components\Module\Base_Module_Dir_It;
14 use JFB_Components\Module\Base_Module_Dir_Trait;
15 use JFB_Components\Module\Base_Module_Handle_It;
16 use JFB_Components\Module\Base_Module_Handle_Trait;
17 use JFB_Components\Module\Base_Module_It;
18 use JFB_Components\Module\Base_Module_Url_It;
19 use JFB_Components\Module\Base_Module_Url_Trait;
20 use JFB_Modules\Block_Parsers\Field_Data_Parser;
21 use JFB_Modules\Validation\Class_Validation_Handlers;
22
23 // If this file is called directly, abort.
24 if ( ! defined( 'WPINC' ) ) {
25 die;
26 }
27
28 final class Module implements
29 Base_Module_It,
30 Base_Module_Handle_It,
31 Base_Module_Url_It,
32 Base_Module_Dir_It,
33 Base_Module_After_Install_It {
34
35 use Base_Module_Url_Trait;
36 use Base_Module_Handle_Trait;
37 use Base_Module_Dir_Trait;
38
39 const FORMAT_ADVANCED = 'advanced';
40 const FORMAT_BROWSER = 'browser';
41 const HANDLE = 'jet-fb-advanced-reporting';
42
43 private $messages = array();
44 /**
45 * @var Rules_Controller
46 */
47 private $rules;
48 private $settings;
49 private $inline_messages = array();
50
51 public function rep_item_id() {
52 return 'validation';
53 }
54
55 public function condition(): bool {
56 return true;
57 }
58
59 /**
60 * @throws Repository_Exception
61 */
62 public function on_install() {
63 $handlers = new Class_Validation_Handlers();
64
65 foreach ( $handlers->get_handlers() as $handler ) {
66 if ( method_exists( $handler, 'init' ) ) {
67 $handler->init();
68 }
69 }
70
71 /** @var \JFB_Modules\Post_Type\Module $post_type */
72 $post_type = jet_form_builder()->module( 'post-type' );
73 $post_type->get_meta()->install( new Post_Type\Validation_Meta() );
74
75 /** @var \JFB_Modules\Rest_Api\Module $rest_api */
76 $rest_api = jet_form_builder()->module( 'rest-api' );
77 $rest_api->get_controller()->install( new Rest_Api\Rest_Validation_Endpoint() );
78 }
79
80 /**
81 * @throws Repository_Exception
82 */
83 public function on_uninstall() {
84 $this->rules = null;
85 $this->messages = array();
86
87 /** @var \JFB_Modules\Post_Type\Module $post_type */
88 $post_type = jet_form_builder()->module( 'post-type' );
89 $post_type->get_meta()->uninstall( Post_Type\Validation_Meta::class );
90
91 /** @var \JFB_Modules\Rest_Api\Module $rest_api */
92 $rest_api = jet_form_builder()->module( 'rest-api' );
93 $rest_api->get_controller()->uninstall( new Rest_Api\Rest_Validation_Endpoint() );
94 }
95
96 public function init_hooks() {
97 add_filter(
98 'jet-form-builder/before-start-form',
99 array( $this, 'add_validation_messages_global' )
100 );
101
102 add_action(
103 'jet-form-builder/before-start-form-row',
104 array( $this, 'add_validation_block' )
105 );
106 add_action(
107 'wp_enqueue_scripts',
108 array( $this, 'register_scripts' )
109 );
110
111 /**
112 * @link https://github.com/Crocoblock/issues-tracker/issues/1542
113 */
114 add_action(
115 'jet_plugins/frontend/register_scripts',
116 array( $this, 'register_scripts' )
117 );
118 add_action(
119 'jet-form-builder/validate-field',
120 array( $this, 'validate_block' ),
121 0
122 );
123 add_action(
124 'jet-form-builder/editor-assets/before',
125 array( $this, 'localize_editor_config' )
126 );
127 }
128
129 public function remove_hooks() {
130 remove_filter(
131 'jet-form-builder/before-start-form',
132 array( $this, 'add_validation_messages_global' )
133 );
134 remove_action(
135 'jet-form-builder/before-start-form-row',
136 array( $this, 'add_validation_block' )
137 );
138 remove_action(
139 'wp_enqueue_scripts',
140 array( $this, 'register_scripts' )
141 );
142
143 /**
144 * @link https://github.com/Crocoblock/issues-tracker/issues/1542
145 */
146 remove_action(
147 'jet_plugins/frontend/register_scripts',
148 array( $this, 'register_scripts' )
149 );
150 remove_action(
151 'jet-form-builder/validate-field',
152 array( $this, 'validate_block' ),
153 0
154 );
155 remove_action(
156 'jet-form-builder/editor-assets/before',
157 array( $this, 'localize_editor_config' )
158 );
159 }
160
161 public function register_scripts() {
162 $script_asset = require_once jet_form_builder()->plugin_dir(
163 'assets/build/frontend/advanced.reporting.asset.php'
164 );
165
166 if ( true === $script_asset ) {
167 return;
168 }
169
170 array_push(
171 $script_asset['dependencies'],
172 \Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE
173 );
174
175 wp_register_script(
176 self::HANDLE,
177 jet_form_builder()->plugin_url( 'assets/build/frontend/advanced.reporting.js' ),
178 $script_asset['dependencies'],
179 $script_asset['version'],
180 true
181 );
182 }
183
184 public function add_validation_messages_global( string $markup, bool $force = false ): string {
185 $this->settings = $this->get_settings();
186 $form_id = jet_fb_live()->form_id;
187
188 if (
189 ( ! $this->is_advanced_form() && ! $force ) ||
190 in_array( $form_id, $this->inline_messages, true )
191 ) {
192 return $markup;
193 }
194
195 $data = Tools::encode_json( $this->settings );
196
197 wp_enqueue_script( self::HANDLE );
198
199 wp_add_inline_script(
200 \Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE,
201 "
202 window.JetFormsValidation = window.JetFormsValidation ?? {};
203 window.JetFormsValidation[ {$form_id} ] = $data;
204 "
205 );
206
207 add_action(
208 'wp_enqueue_scripts',
209 function () use ( $form_id, $data ) {
210 wp_add_inline_script(
211 \Jet_Form_Builder\Blocks\Module::MAIN_SCRIPT_HANDLE,
212 "
213 window.JetFormsValidation = window.JetFormsValidation ?? {};
214 window.JetFormsValidation[ {$form_id} ] = $data;
215 "
216 );
217 },
218 20
219 );
220
221 $this->inline_messages[] = $form_id;
222
223 return $markup;
224 }
225
226 public function add_validation_block( Base $block ) {
227 /**
228 * If in post meta enable Advanced validation
229 * or right in block settings
230 */
231 if ( ! $this->is_advanced( $block->block_attrs ) ) {
232 return;
233 }
234
235 $this->add_validation_messages_global( '', true );
236
237 $type = $block->block_attrs['validation']['type'] ?? '';
238 $rules = $block->block_attrs['validation']['rules'] ?? array();
239
240 $block->add_attribute( 'data-validation-type', $type ?: 'inherit' );
241
242 if ( ! empty( $rules ) ) {
243 $this->get_rules()->prepare_rules( $rules );
244
245 $block->add_attribute(
246 'data-validation-rules',
247 Tools::encode_json( $rules )
248 );
249 }
250
251 /**
252 * If advanced validation not enabled right in block settings
253 */
254 if ( self::FORMAT_ADVANCED !== $type ) {
255 return;
256 }
257
258 $messages = $block->block_attrs['validation']['messages'] ?? array();
259
260 if ( ! empty( $messages ) ) {
261 $block->add_attribute( 'data-validation-messages', Tools::encode_json( $messages ) );
262 }
263 }
264
265 public function get_settings(): array {
266 /** @var \JFB_Modules\Post_Type\Module $module */
267 /** @noinspection PhpUnhandledExceptionInspection */
268 $module = jet_form_builder()->module( 'post-type' );
269 $validation = $module->query_meta( Post_Type\Validation_Meta::class );
270
271 $response = array(
272 'type' => $validation['type'] ?? self::FORMAT_BROWSER,
273 'messages' => array(),
274 );
275
276 $messages = $this->get_messages();
277
278 foreach ( $messages as $message ) {
279 $response['messages'][ $message->get_id() ] = (
280 $validation['messages'][ $message->get_id() ] ?? $message->get_initial()
281 );
282 }
283
284 return $response;
285 }
286
287 public function is_advanced( array $block_attrs ): bool {
288 $type = $block_attrs['validation']['type'] ?? '';
289
290 return $type
291 ? self::FORMAT_ADVANCED === $type
292 : $this->is_advanced_form();
293 }
294
295 public function is_advanced_form(): bool {
296 if ( is_null( $this->settings ) ) {
297 $this->settings = $this->get_settings();
298 }
299
300 return self::FORMAT_ADVANCED === ( $this->settings['type'] ?? '' );
301 }
302
303 public function formats(): array {
304 return array(
305 array(
306 'value' => self::FORMAT_BROWSER,
307 'label' => __( 'Default', 'jet-form-builder' ),
308 'title' => __( 'Browser native validation', 'jet-form-builder' ),
309 ),
310 array(
311 'value' => self::FORMAT_ADVANCED,
312 'label' => __( 'Advanced', 'jet-form-builder' ),
313 'title' => __( 'More flexible JetFormBuilder\'s validation', 'jet-form-builder' ),
314 ),
315 );
316 }
317
318 public function validate_block( Field_Data_Parser $parser ) {
319 if (
320 ! $this->is_advanced( $parser->get_settings() ) ||
321 ! $parser->get_value() ||
322 $parser->is_inside_conditional()
323 ) {
324 return;
325 }
326
327 $this->get_rules()->validate_block( $parser );
328 }
329
330 public function localize_editor_config() {
331 wp_localize_script(
332 Editor::EDITOR_PACKAGE_HANDLE,
333 'jetFormValidation',
334 array(
335 'messages' => Array_Tools::to_array( $this->get_messages() ),
336 'ssr_callbacks' => Array_Tools::to_array( $this->get_rules()->get_ssr()->get_callbacks() ),
337 'formats' => $this->formats(),
338 'rule_types' => Array_Tools::to_array( $this->get_rules()->rep_get_values() ),
339 )
340 );
341 }
342
343 /**
344 * @return Rules_Controller
345 */
346 public function get_rules(): Rules_Controller {
347 if ( ! is_null( $this->rules ) ) {
348 return $this->rules;
349 }
350
351 $this->rules = new Rules_Controller();
352
353 return $this->rules;
354 }
355
356 public function get_messages(): array {
357 if ( ! empty( $this->messages ) ) {
358 return $this->messages;
359 }
360
361 $this->messages = apply_filters(
362 'jet-form-builder/validation-messages',
363 array(
364 new Messages\Is_Empty_Value(),
365 new Messages\Is_Number_Min(),
366 new Messages\Is_Number_Max(),
367 new Messages\Is_Char_Min(),
368 new Messages\Is_Char_Max(),
369 new Messages\Is_Not_Valid_Email(),
370 new Messages\Is_Not_Valid_Url(),
371 new Messages\Is_Not_Complete_Mask(),
372 new Messages\Is_Files_Max(),
373 new Messages\Is_File_Size(),
374 new Messages\Is_File_Ext(),
375 new Messages\Is_Date_Min(),
376 new Messages\Is_Date_Max(),
377 )
378 );
379
380 return $this->messages;
381 }
382
383 }
384