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