PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.3
JetFormBuilder — Dynamic Blocks Form Builder v3.0.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 3 years ago button-types 3 years ago conditional-block 3 years ago exceptions 3 years ago modules 3 years ago render 3 years ago ssr-validation 3 years ago types 3 years ago validation-messages 3 years ago action-buttons-manager.php 3 years ago block-helper.php 3 years ago blocks-repository-base.php 3 years ago default-blocks-repository.php 3 years ago dynamic-value.php 3 years ago form-builder-blocks-repository.php 3 years ago manager.php 3 years ago switch-page-on-change.php 3 years ago validation.php 3 years ago
validation.php
277 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Blocks;
5
6
7 use Jet_Form_Builder\Blocks\Advanced_Rules\Match_Not_Regexp_Rule;
8 use Jet_Form_Builder\Blocks\Advanced_Rules\Match_Regexp_Rule;
9 use Jet_Form_Builder\Blocks\Advanced_Rules\Must_Contain_Characters_Rule;
10 use Jet_Form_Builder\Blocks\Advanced_Rules\Must_Equal_Rule;
11 use Jet_Form_Builder\Blocks\Advanced_Rules\Must_Not_Contain_Characters_Rule;
12 use Jet_Form_Builder\Blocks\Advanced_Rules\Server_Side_Rule;
13 use Jet_Form_Builder\Blocks\Ssr_Validation\Validation_Callbacks;
14 use Jet_Form_Builder\Blocks\Types\Base;
15 use Jet_Form_Builder\Blocks\Validation_Messages\Base_Message;
16 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Char_Max;
17 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Char_Min;
18 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Date_Max;
19 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Date_Min;
20 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Empty_Value;
21 use Jet_Form_Builder\Blocks\Validation_Messages\Is_File_Ext;
22 use Jet_Form_Builder\Blocks\Validation_Messages\Is_File_Size;
23 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Files_Max;
24 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Not_Complete_Mask;
25 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Not_Valid_Email;
26 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Not_Valid_Url;
27 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Number_Max;
28 use Jet_Form_Builder\Blocks\Validation_Messages\Is_Number_Min;
29 use Jet_Form_Builder\Classes\Arrayable\Array_Tools;
30 use Jet_Form_Builder\Classes\Arrayable\Arrayable;
31 use Jet_Form_Builder\Classes\Instance_Trait;
32 use Jet_Form_Builder\Classes\Tools;
33 use Jet_Form_Builder\Plugin;
34
35 /**
36 * @method static Validation instance()
37 *
38 * Class Validation_Messages
39 * @package Jet_Form_Builder\Blocks
40 */
41 class Validation implements Arrayable {
42
43 use Instance_Trait;
44
45 const FORMAT_ADVANCED = 'advanced';
46 const FORMAT_BROWSER = 'browser';
47 const HANDLE = 'jet-fb-advanced-reporting';
48
49 /**
50 * @var Base_Message[]
51 */
52 private $messages;
53 public $callbacks;
54 private $settings = array();
55 private $inline_messages = array();
56
57 public function __construct() {
58 $this->messages = apply_filters(
59 'jet-form-builder/validation-messages',
60 $this->get_messages()
61 );
62
63 $this->callbacks = new Validation_Callbacks();
64
65 add_filter(
66 'jet-form-builder/before-start-form',
67 array( $this, 'add_validation_messages_global' )
68 );
69 add_action(
70 'jet-form-builder/before-start-form-row',
71 array( $this, 'add_validation_block' )
72 );
73 add_action(
74 'wp_enqueue_scripts',
75 array( $this, 'register_scripts' )
76 );
77
78 /**
79 * @link https://github.com/Crocoblock/issues-tracker/issues/1542
80 */
81 add_action(
82 'jet_plugins/frontend/register_scripts',
83 array( $this, 'register_scripts' )
84 );
85 }
86
87 /**
88 * @return Base_Message[]
89 */
90 private function get_messages(): array {
91 return array(
92 new Is_Empty_Value(),
93 new Is_Number_Min(),
94 new Is_Number_Max(),
95 new Is_Char_Min(),
96 new Is_Char_Max(),
97 new Is_Not_Valid_Email(),
98 new Is_Not_Valid_Url(),
99 new Is_Not_Complete_Mask(),
100 new Is_Files_Max(),
101 new Is_File_Size(),
102 new Is_File_Ext(),
103 new Is_Date_Min(),
104 new Is_Date_Max(),
105 );
106 }
107
108 public function register_scripts() {
109 wp_register_script(
110 self::HANDLE,
111 Plugin::instance()->plugin_url( 'assets/js/frontend/advanced.reporting{min}.js' ),
112 array(
113 Manager::MAIN_SCRIPT_HANDLE,
114 ),
115 Plugin::instance()->get_version(),
116 true
117 );
118 }
119
120 public function add_validation_messages_global( string $markup, bool $force = false ): string {
121 $this->settings = $this->get_settings();
122 $form_id = jet_fb_live()->form_id;
123
124 if (
125 ( ! $this->is_advanced_form() && ! $force ) ||
126 in_array( $form_id, $this->inline_messages, true )
127 ) {
128 return $markup;
129 }
130
131 $data = Tools::encode_json( $this->settings );
132
133 wp_add_inline_script(
134 Manager::MAIN_SCRIPT_HANDLE,
135 "
136 window.JetFormsValidation = window.JetFormsValidation ?? {};
137 window.JetFormsValidation[ {$form_id} ] = $data;
138 "
139 );
140
141 add_action(
142 'wp_enqueue_scripts',
143 function () use ( $form_id, $data ) {
144 wp_add_inline_script(
145 Manager::MAIN_SCRIPT_HANDLE,
146 "
147 window.JetFormsValidation = window.JetFormsValidation ?? {};
148 window.JetFormsValidation[ {$form_id} ] = $data;
149 "
150 );
151 },
152 20
153 );
154
155 $this->inline_messages[] = $form_id;
156
157 return $markup;
158 }
159
160 public function add_validation_block( Base $block ) {
161 /**
162 * If in post meta enable Advanced validation
163 * or right in block settings
164 */
165 if ( ! $this->is_advanced( $block ) ) {
166 return;
167 }
168 wp_enqueue_script( self::HANDLE );
169 do_action( 'jet_plugins/frontend/register_script', self::HANDLE );
170
171 $this->add_validation_messages_global( '', true );
172
173 $type = $this->get_block_type( $block );
174 $rules = $block->block_attrs['validation']['rules'] ?? array();
175
176 $block->add_attribute( 'data-validation-type', $type ?: 'inherit' );
177
178 if ( ! empty( $rules ) ) {
179 $this->prepare_rules( $rules );
180
181 $block->add_attribute(
182 'data-validation-rules',
183 Tools::encode_json( $rules )
184 );
185 }
186
187 /**
188 * If advanced validation not enabled right in block settings
189 */
190 if ( self::FORMAT_ADVANCED !== $type ) {
191 return;
192 }
193
194 $messages = $block->block_attrs['validation']['messages'] ?? array();
195
196 if ( ! empty( $messages ) ) {
197 $block->add_attribute( 'data-validation-messages', Tools::encode_json( $messages ) );
198 }
199 }
200
201 public function get_settings(): array {
202 $validation = jet_form_builder()->post_type->get_validation( jet_fb_live()->form_id );
203
204 $response = array(
205 'type' => $validation['type'] ?? self::FORMAT_BROWSER,
206 'messages' => array(),
207 );
208
209 foreach ( $this->messages as $message ) {
210 $response['messages'][ $message->get_id() ] = (
211 $validation['messages'][ $message->get_id() ] ?? $message->get_initial()
212 );
213 }
214
215 return $response;
216 }
217
218 public function is_advanced( Base $block ): bool {
219 $type = $this->get_block_type( $block );
220
221 return $type
222 ? self::FORMAT_ADVANCED === $type
223 : $this->is_advanced_form();
224 }
225
226 public function is_advanced_form(): bool {
227 return self::FORMAT_ADVANCED === ( $this->settings['type'] ?? '' );
228 }
229
230 protected function get_block_type( Base $block ): string {
231 return $block->block_attrs['validation']['type'] ?? '';
232 }
233
234 public function formats(): array {
235 return array(
236 array(
237 'value' => self::FORMAT_BROWSER,
238 'label' => __( 'Default', 'jet-form-builder' ),
239 'title' => __( 'Browser native validation' ),
240 ),
241 array(
242 'value' => self::FORMAT_ADVANCED,
243 'label' => __( 'Advanced', 'jet-form-builder' ),
244 'title' => __( 'More flexible JetFormBuilder\'s validation', 'jet-form-builder' )
245 ),
246 );
247 }
248
249 public function rule_types(): array {
250 return Array_Tools::to_array(
251 array(
252 new Must_Equal_Rule(),
253 new Must_Contain_Characters_Rule(),
254 new Must_Not_Contain_Characters_Rule(),
255 new Match_Regexp_Rule(),
256 new Match_Not_Regexp_Rule(),
257 new Server_Side_Rule(),
258 )
259 );
260 }
261
262 public function to_array(): array {
263 return array(
264 'messages' => Array_Tools::to_array( $this->messages ),
265 'ssr_callbacks' => Array_Tools::to_array( $this->callbacks->get_items() ),
266 'formats' => $this->formats(),
267 'rule_types' => $this->rule_types(),
268 );
269 }
270
271 public function prepare_rules( array &$rules ) {
272 foreach ( $rules as &$rule ) {
273 $rule['value'] = jet_fb_parse_dynamic( $rule['value'] ?? '' );
274 }
275 }
276
277 }