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