PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.4.1
JetFormBuilder — Dynamic Blocks Form Builder v1.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 / includes / request / parser-manager.php
jetformbuilder / includes / request Last commit date
fields 4 years ago field-data-parser.php 4 years ago parser-manager.php 4 years ago request-handler.php 4 years ago
parser-manager.php
192 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Request;
5
6 use Jet_Form_Builder\Classes\Instance_Trait;
7 use Jet_Form_Builder\Exceptions\Parse_Exception;
8 use Jet_Form_Builder\Exceptions\Request_Exception;
9 use Jet_Form_Builder\Plugin;
10 use Jet_Form_Builder\Request\Fields;
11
12 /**
13 * @method static Parser_Manager instance()
14 *
15 * Class Parser_Manager
16 * @package Jet_Form_Builder\Request
17 */
18 class Parser_Manager {
19
20 const EMPTY_BLOCK_ERROR = '0';
21 const NOT_FIELD_HAS_INNER = '1';
22 const FIELD_HAS_INNER = '2';
23 const IS_CONDITIONAL = '3';
24
25 private $_parsers;
26 private $response;
27 public $inside_conditional = false;
28
29 use Instance_Trait;
30
31 private function __construct() {
32 $this->register_request_parsers();
33 }
34
35 private function register_request_parsers() {
36 $parsers = apply_filters(
37 'jet-form-builder/parsers-request/register',
38 array(
39 new Fields\Date_Field_Parser(),
40 new Fields\Repeater_Field_Parser(),
41 new Fields\Wysiwyg_Field_Parser(),
42 new Fields\Text_Field_Parser(),
43 new Fields\Repeater_Field_Parser(),
44 new Fields\Media_Field_Parser(),
45 new Fields\Datetime_Field_Parser(),
46 )
47 );
48
49 foreach ( $parsers as $parser ) {
50 $this->_parsers[ $parser->type() ] = $parser;
51 }
52 }
53
54 public function get_values_fields( $fields, $request, $inside_conditional = false ) {
55 $response = array();
56 $this->get_values_fields_recursive( $response, $fields, $request, $inside_conditional );
57
58 return $response;
59 }
60
61 public function get_values_fields_recursive( &$output, $fields, $request, $inside_conditional ) {
62 foreach ( $fields as $field ) {
63 try {
64 $this->get_value_from_field( $output, $field, $request, $inside_conditional );
65
66 } catch ( Parse_Exception $exception ) {
67 switch ( $exception->getMessage() ) {
68
69 case self::IS_CONDITIONAL:
70 $this->get_values_fields_recursive(
71 $output,
72 $exception->get_inner(),
73 $request,
74 true
75 );
76 break;
77
78 case self::NOT_FIELD_HAS_INNER:
79 $this->get_values_fields_recursive(
80 $output,
81 $exception->get_inner(),
82 $request,
83 $inside_conditional
84 );
85 break;
86 }
87 }
88 }
89 }
90
91
92 /**
93 * @param $output
94 * @param $field
95 *
96 * @param $request
97 * @param $inside_conditional
98 *
99 * @throws Parse_Exception
100 */
101 public function get_value_from_field( &$output, $field, $request, $inside_conditional ) {
102 if ( empty( $field['blockName'] ) ) {
103 throw new Parse_Exception( self::EMPTY_BLOCK_ERROR );
104 }
105
106 if ( ! empty( $field['innerBlocks'] ) ) {
107 if ( strpos( $field['blockName'], 'conditional-block' ) ) {
108 throw new Parse_Exception( self::IS_CONDITIONAL, $field['innerBlocks'] );
109 }
110 if ( ! $this->isset_parser_by_block_name( $field['blockName'] ) ) {
111 throw new Parse_Exception( self::NOT_FIELD_HAS_INNER, $field['innerBlocks'] );
112 }
113 }
114
115 $settings = $field['attrs'];
116 $name = $settings['name'] ?? 'field_name';
117
118 try {
119 $output[ $name ] = $this->get_parsed_value( $field, $request, $name, $inside_conditional );
120 } catch ( Parse_Exception $exception ) {
121 $output = array_merge( $output, $exception->get_inner() );
122 }
123 }
124
125 public function get_parsed_value( $field, $request, $name, $inside_conditional ) {
126
127 if ( ! $this->is_field_visible( $field['attrs'] ) ) {
128 return null;
129 }
130 $value = $request[ $name ] ?? '';
131 $parser = $this->get_parser_by_block_name( $field['blockName'] );
132
133 if ( $parser instanceof Field_Data_Parser ) {
134 $parser->init( $value, $field, $inside_conditional );
135
136 return $parser->response();
137 }
138
139 return $value;
140 }
141
142 private function isset_parser_by_block_name( $block_name ) {
143 $type = Plugin::instance()->form->field_name( $block_name );
144
145 return isset( $this->_parsers[ $type ] );
146 }
147
148 private function get_parser_by_block_name( $block_name ) {
149 $type = Plugin::instance()->form->field_name( $block_name );
150
151 return $this->get_parser( $type );
152 }
153
154 private function get_parser( $type ) {
155 return isset( $this->_parsers[ $type ] ) ? clone $this->_parsers[ $type ] : false;
156 }
157
158 /**
159 * Returns true if field is visible
160 *
161 * @param array $field [description]
162 *
163 * @return boolean [description]
164 */
165 public function is_field_visible( $field = array() ) {
166
167 // For backward compatibility and hidden fields.
168 if ( empty( $field['visibility'] ) ) {
169 return true;
170 }
171
172 // If is visible for all - show field.
173 if ( 'all' === $field['visibility'] ) {
174 return true;
175 }
176
177 // If is visible for logged in users and user is logged in - show field.
178 if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) {
179 return true;
180 }
181
182 // If is visible for not logged in users and user is not logged in - show field.
183 if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) {
184 return true;
185 }
186
187 return false;
188
189 }
190
191 }
192