PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.5.3
JetFormBuilder — Dynamic Blocks Form Builder v3.6.5.3
3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 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 All 131 releases
jetformbuilder / modules / validation / rules-controller.php

rules-controller.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.5.3, at modules/validation/rules-controller.php

85 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace JFB_Modules\Validation;
5
6 // If this file is called directly, abort.
7 if ( ! defined( 'WPINC' ) ) {
8 die;
9 }
10
11 use JFB_Modules\Validation\Advanced_Rules;
12 use Jet_Form_Builder\Exceptions\Repository_Exception;
13 use JFB_Components\Repository\Interfaces\Repository_Pattern_Interface;
14 use JFB_Components\Repository\Repository_Pattern_Trait;
15 use JFB_Modules\Block_Parsers\Field_Data_Parser;
16
17 class Rules_Controller implements Repository_Pattern_Interface {
18
19 use Repository_Pattern_Trait;
20
21 public function __construct() {
22 $this->rep_install();
23 }
24
25 public function rep_instances(): array {
26 return array(
27 new Advanced_Rules\Must_Equal_Rule(),
28 new Advanced_Rules\Must_Contain_Characters_Rule(),
29 new Advanced_Rules\Must_Not_Contain_Characters_Rule(),
30 new Advanced_Rules\Match_Regexp_Rule(),
31 new Advanced_Rules\Match_Not_Regexp_Rule(),
32 new Advanced_Rules\Server_Side_Rule(),
33 );
34 }
35
36 public function validate_block( Field_Data_Parser $parser ) {
37 $validation = $parser->get_setting( 'validation' );
38 $rules = $validation['rules'] ?? array();
39
40 $this->prepare_rules( $rules );
41
42 foreach ( $rules as $index => $rule_attrs ) {
43 try {
44 $rule = $this->get_item( $rule_attrs['type'] ?? '' );
45 } catch ( Repository_Exception $exception ) {
46 continue;
47 }
48 $rule_attrs['_rule_index'] = $index;
49 $rule->set_settings( $rule_attrs );
50 $rule->validate_field( $parser );
51 }
52 }
53
54 public function get_ssr(): Advanced_Rules\Server_Side_Rule {
55 try {
56 /** @var Advanced_Rules\Server_Side_Rule $rule */
57 $rule = $this->get_item( Advanced_Rules\Server_Side_Rule::class );
58 } catch ( Repository_Exception $exception ) {
59 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
60 wp_die( $exception->getMessage() );
61 }
62
63 return $rule;
64 }
65
66 public function prepare_rules( array &$rules ) {
67 foreach ( $rules as &$rule ) {
68 // Rules live in the form's own blocks (post_content), writable only
69 // with `edit_post` - never taken from the request, even on the REST
70 // validation route, which reloads them via Block_Helper::get_blocks_by_post().
71 $rule['value'] = jet_fb_parse_dynamic_trusted( $rule['value'] ?? '' );
72 }
73 }
74
75 /**
76 * @param string $name
77 *
78 * @return Advanced_Rules\Rule
79 * @throws Repository_Exception
80 */
81 public function get_item( string $name ): Advanced_Rules\Rule {
82 return $this->rep_get_item( $name );
83 }
84 }
85