PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.3.2
JetFormBuilder — Dynamic Blocks Form Builder v1.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 / 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
188 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 $output[ $name ] = $this->get_parsed_value( $field, $request, $name, $inside_conditional );
119 }
120
121 public function get_parsed_value( $field, $request, $name, $inside_conditional ) {
122
123 if ( ! $this->is_field_visible( $field['attrs'] ) ) {
124 return null;
125 }
126 $value = $request[ $name ] ?? '';
127 $parser = $this->get_parser_by_block_name( $field['blockName'] );
128
129 if ( $parser instanceof Field_Data_Parser ) {
130 $parser->init( $value, $field, $inside_conditional );
131
132 return $parser->response();
133 }
134
135 return $value;
136 }
137
138 private function isset_parser_by_block_name( $block_name ) {
139 $type = Plugin::instance()->form->field_name( $block_name );
140
141 return isset( $this->_parsers[ $type ] );
142 }
143
144 private function get_parser_by_block_name( $block_name ) {
145 $type = Plugin::instance()->form->field_name( $block_name );
146
147 return $this->get_parser( $type );
148 }
149
150 private function get_parser( $type ) {
151 return isset( $this->_parsers[ $type ] ) ? clone $this->_parsers[ $type ] : false;
152 }
153
154 /**
155 * Returns true if field is visible
156 *
157 * @param array $field [description]
158 *
159 * @return boolean [description]
160 */
161 public function is_field_visible( $field = array() ) {
162
163 // For backward compatibility and hidden fields.
164 if ( empty( $field['visibility'] ) ) {
165 return true;
166 }
167
168 // If is visible for all - show field.
169 if ( 'all' === $field['visibility'] ) {
170 return true;
171 }
172
173 // If is visible for logged in users and user is logged in - show field.
174 if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) {
175 return true;
176 }
177
178 // If is visible for not logged in users and user is not logged in - show field.
179 if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) {
180 return true;
181 }
182
183 return false;
184
185 }
186
187 }
188