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