PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.0
JetFormBuilder — Dynamic Blocks Form Builder v3.1.0
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 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 / presets / sources / base-source.php
jetformbuilder / includes / presets / sources Last commit date
base-source.php 2 years ago preset-source-post.php 2 years ago preset-source-query-var.php 2 years ago preset-source-user.php 2 years ago
base-source.php
294 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Presets\Sources;
5
6 use Jet_Form_Builder\Blocks\Types\Base;
7 use Jet_Form_Builder\Classes\Macros_Parser;
8 use Jet_Form_Builder\Exceptions\Parse_Exception;
9 use Jet_Form_Builder\Exceptions\Preset_Exception;
10 use Jet_Form_Builder\Presets\Preset_Manager;
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 abstract class Base_Source {
18
19 protected $fields_map;
20 protected $field_data = array();
21 protected $field_args;
22 protected $preset_data;
23 protected $field = '__condition__';
24 protected $prop;
25 private $src;
26
27 protected $permission;
28
29 const FUNC_PREFIX = 'source__';
30
31 abstract public function query_source();
32
33 abstract public function get_id();
34
35 public function condition(): bool {
36 return true;
37 }
38
39 /**
40 * @param $fields_map
41 * @param $preset_data
42 * @param $args
43 *
44 * @return $this
45 * @throws Preset_Exception
46 */
47 public function init_source( $fields_map, $preset_data, $args ): Base_Source {
48 $this->field_args = $args;
49 $this->field = $args['name'] ?? '';
50 $this->fields_map = $fields_map;
51 $this->preset_data = $preset_data;
52 $this->field_data = $this->get_field_data();
53 $this->prop = $this->get_prop();
54
55 return $this;
56 }
57
58 public function after_init(): Base_Source {
59 return $this;
60 }
61
62 public function after_register() {
63 }
64
65 public function on_sanitize(): bool {
66 return true;
67 }
68
69 public function is_need_prop() {
70 return true;
71 }
72
73 /**
74 * @return mixed
75 * @throws Preset_Exception
76 */
77 public function maybe_query_source() {
78 if ( $this->prop ) {
79 $this->src = $this->query_source();
80
81 return $this;
82 }
83
84 throw new Preset_Exception( 'Empty `prop` in ' . get_class( $this ), $this->field_data );
85 }
86
87 /**
88 * @return mixed
89 * @throws Preset_Exception
90 */
91 protected function get_field_data() {
92 if ( $this->has_field_in_map() ) {
93 return $this->fields_map[ $this->field ];
94 }
95
96 throw new Preset_Exception(
97 "Empty `fields_map['{$this->field}']` in " . get_class( $this ),
98 $this->fields_map
99 );
100 }
101
102 public function has_field_in_map() {
103 return ( isset( $this->fields_map[ $this->field ]['prop'] ) || isset( $this->fields_map[ $this->field ]['key'] ) );
104 }
105
106 /**
107 * @return mixed
108 */
109 public function src() {
110 return $this->src;
111 }
112
113 /**
114 * @return mixed
115 * @throws Preset_Exception
116 */
117 public function safe_src() {
118 $this->throw_if_preset_not_available();
119
120 return $this->src();
121 }
122
123 /**
124 * @return bool
125 * @throws Preset_Exception
126 */
127 protected function can_get_preset() {
128 return ( ! empty( $this->src() ) && ! is_wp_error( $this->src() ) );
129 }
130
131 /**
132 * @return bool
133 * @throws Preset_Exception
134 */
135 protected function has_permission(): bool {
136 if ( is_null( $this->permission ) ) {
137 $this->permission = apply_filters( 'jet-form-builder/preset-sanitize', $this->can_get_preset(), $this );
138 }
139
140 return $this->permission;
141 }
142
143 /**
144 * @throws Preset_Exception
145 */
146 final protected function throw_if_preset_not_available() {
147 if ( ! $this->has_permission() ) {
148 throw new Preset_Exception( static::class . '::can_get_preset return FALSE' );
149 }
150 }
151
152
153 protected function get_prop() {
154 if ( ! $this->is_need_prop() ) {
155 return true;
156 }
157
158 return ( ! empty( $this->field_data['prop'] ) ? $this->field_data['prop'] : false );
159 }
160
161 /**
162 * @return false|mixed
163 * @throws Preset_Exception
164 */
165 public function get_result_on_prop() {
166 if ( ! $this->is_need_prop() ) {
167 return $this->src();
168 }
169
170 $extra = $this->get_extra_fields();
171
172 if ( empty( $extra ) ) {
173 return $this->get_current_value();
174 }
175
176 $value = array();
177
178 foreach ( $extra as $name => $field ) {
179 $this->before_query_extra_field( $field );
180
181 $value[ $name ] = $this->get_current_value();
182 }
183
184 return $value;
185 }
186
187 protected function before_query_extra_field( $field ) {
188 $this->field_data['key'] = $field;
189 }
190
191 /**
192 * @return false|mixed
193 * @throws Preset_Exception
194 */
195 private function get_current_value() {
196 $func_name = self::FUNC_PREFIX . $this->prop;
197
198 if ( is_callable( array( $this, $func_name ) ) ) {
199 return call_user_func( array( $this, $func_name ) );
200 }
201
202 return $this->default_prop( $this->prop );
203 }
204
205 private function get_extra_fields(): array {
206 try {
207 $extra = $this->get_field_object()->get_extra_fields( $this );
208 } catch ( Preset_Exception $exception ) {
209 return array();
210 }
211
212 $parser = ( new Macros_Parser() )->set_replacements(
213 array(
214 'key' => $this->field_data['key'] ?? '',
215 'prop' => $this->prop,
216 )
217 );
218
219 foreach ( $extra as $index => $field ) {
220 $extra[ $index ] = $parser->parse_macros( $field );
221 }
222
223 return $extra;
224 }
225
226 /**
227 * @param string $prop
228 *
229 * @return mixed
230 * @throws Preset_Exception
231 */
232 public function default_prop( string $prop ) {
233 $source = $this->src;
234
235 if ( isset( $source->$prop ) ) {
236 return $source->$prop;
237 } elseif ( isset( $source->data ) && isset( $source->data->$prop ) ) {
238 return $source->data->$prop;
239 }
240
241 if ( ! is_object( $source ) ) {
242 throw new Preset_Exception( "Source isn't object" );
243 }
244
245 throw new Preset_Exception( "Can't get value from " . get_class( $source ) );
246 }
247
248
249 /**
250 * @return mixed
251 * @throws Preset_Exception
252 */
253 final public function result() {
254 $this->throw_if_preset_not_available();
255
256 return $this->parse_result_value( $this->get_result_on_prop() );
257 }
258
259 public function parse_result_value( $value ) {
260 if ( ! isset( $this->field_args['type'] ) ) {
261 return $value;
262 }
263
264 return Preset_Manager::instance()->prepare_result( $this->field_args['type'], $value );
265 }
266
267
268 /**
269 * @return Base
270 * @throws Preset_Exception
271 */
272 public function get_field_object(): Base {
273 $type = $this->field_args['type'] ?? false;
274 $block = jet_form_builder()->blocks->get_field_by_name( $type );
275
276 if ( ! $block ) {
277 throw new Preset_Exception( 'Undefined block_type: ' . $type, $this->field_args );
278 }
279
280 $block->block_attrs = $this->field_args;
281
282 return $block;
283 }
284
285 /**
286 * @return mixed
287 * @throws Preset_Exception
288 */
289 public function get_expected_format() {
290 return $this->get_field_object()->expected_preset_type()[0] ?? 'raw';
291 }
292
293 }
294