PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.0.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.0.1
3.6.6.1 3.6.6 3.6.5.4 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 All 134 releases
jetformbuilder / compatibility / jet-engine / jet-engine.php

jet-engine.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.0.1, at compatibility/jet-engine/jet-engine.php

508 lines 14.0 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_Compatibility\Jet_Engine;
5
6 use Jet_Engine\Query_Builder\Queries\Base_Query;
7 use Jet_Form_Builder\Actions\Methods\Object_Properties_Collection;
8 use Jet_Form_Builder\Exceptions\Repository_Exception;
9 use JFB_Compatibility\Jet_Engine\Blocks\Check_Mark\Block_Asset;
10 use JFB_Compatibility\Jet_Engine\Compatibility\Bricks\Bricks;
11 use JFB_Compatibility\Jet_Engine\Compatibility\Elementor\Elementor;
12 use JFB_Modules\Option_Field\Blocks\Checkbox;
13 use JFB_Modules\Option_Field\Blocks\Radio;
14 use Jet_Form_Builder\Classes\Builder_Helper;
15 use JFB_Compatibility\Jet_Engine\Actions\Update_Options;
16 use JFB_Compatibility\Jet_Engine\Generators\Get_From_Field;
17 use JFB_Compatibility\Jet_Engine\Generators\Get_From_Je_Query;
18 use JFB_Compatibility\Jet_Engine\Methods\Post_Modification\Post_Je_Relation_Property;
19 use JFB_Compatibility\Jet_Engine\Option_Query\Inner_Module;
20 use JFB_Compatibility\Jet_Engine\Preset_Sources\Preset_Source_Options_Page;
21 use JFB_Compatibility\Jet_Engine\Preset_Sources\Preset_User;
22 use JFB_Components\Compatibility\Base_Compat_Dir_Trait;
23 use JFB_Components\Compatibility\Base_Compat_Handle_Trait;
24 use JFB_Components\Compatibility\Base_Compat_Url_Trait;
25 use JFB_Components\Module\Base_Module_After_Install_It;
26 use JFB_Components\Module\Base_Module_Dir_It;
27 use JFB_Components\Module\Base_Module_Handle_It;
28 use JFB_Components\Module\Base_Module_It;
29 use JFB_Components\Module\Base_Module_Url_It;
30 use Jet_Form_Builder\Blocks\Module;
31 use Jet_Engine\Modules\Custom_Content_Types;
32
33 // If this file is called directly, abort.
34 if ( ! defined( 'WPINC' ) ) {
35 die;
36 }
37
38 class Jet_Engine implements
39 Base_Module_It,
40 Base_Module_Handle_It,
41 Base_Module_Dir_It,
42 Base_Module_Url_It,
43 Base_Module_After_Install_It {
44
45 use Base_Compat_Handle_Trait;
46 use Base_Compat_Url_Trait;
47 use Base_Compat_Dir_Trait;
48
49 private $has_custom_template;
50
51 /**
52 * @var Inner_Module|null
53 */
54 private $option_query;
55 private $option_type = 'checkbox';
56
57 /**
58 * @var Elementor
59 */
60 private $elementor;
61
62 /**
63 * @var Bricks
64 */
65 private $bricks;
66
67 public function rep_item_id() {
68 return 'jet-engine';
69 }
70
71 public function condition(): bool {
72 return function_exists( 'jet_engine' );
73 }
74
75 public function on_install() {
76 $this->option_query = new Inner_Module();
77 $this->elementor = new Elementor();
78 $this->bricks = new Bricks();
79 }
80
81 public function on_uninstall() {
82 $this->option_query = null;
83 $this->elementor = null;
84 $this->bricks = null;
85 }
86
87 public function init_hooks() {
88 add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
89 add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_scripts' ) );
90 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_admin_assets' ) );
91 add_action( 'jet-form-builder/actions/register', array( $this, 'add_actions' ) );
92
93 add_filter(
94 'jet-form-builder/preset/source-types',
95 array( $this, 'add_sources' )
96 );
97 add_filter(
98 'jet-form-builder/forms/options-generators',
99 array( $this, 'add_generators' )
100 );
101 add_filter(
102 'jet-form-builder/custom-template-object',
103 array( $this, 'get_custom_object_for_template' ),
104 10,
105 3
106 );
107 add_action(
108 'jet-form-builder/generators/get_from_query/setup',
109 array( $this, 'add_cct_type_arg_to_block_args' ),
110 10,
111 2
112 );
113 add_filter(
114 'jet-form-builder/blocks/items',
115 array( $this, 'add_blocks' ),
116 0
117 );
118 add_action(
119 'init',
120 array( $this, 'register_listing_related_blocks' )
121 );
122 add_filter(
123 'jet-form-builder/render/checkbox-field/option',
124 array( $this, 'on_render_field_checkbox' ),
125 10,
126 4
127 );
128 add_filter(
129 'jet-form-builder/render/radio-field/option',
130 array( $this, 'on_render_field_radio' ),
131 10,
132 4
133 );
134 add_filter(
135 'render_block_jet-forms/checkbox-field',
136 array( $this, 'reset_post_data_after_render_options' )
137 );
138 add_filter(
139 'render_block_jet-forms/radio-field',
140 array( $this, 'reset_post_data_after_render_options' )
141 );
142
143 if ( jet_engine()->relations ) {
144 add_filter(
145 'jet-form-builder/post-modifier/object-properties',
146 array( $this, 'add_post_properties' )
147 );
148 }
149
150 $this->option_query->init_hooks();
151
152 if ( jet_form_builder()->has_compat( 'elementor' ) ) {
153 $this->elementor->init_hooks();
154 }
155
156 if ( jet_form_builder()->has_compat( 'bricks' ) ) {
157 $this->bricks->init_hooks();
158 }
159
160 /**
161 * @see https://github.com/Crocoblock/issues-tracker/issues/12555
162 */
163 add_action( 'jet-engine/rest-api/init-endpoints', array( $this, 'rewrite_map_location_data_endpoint' ), 99 );
164
165 // Register JetEngine macros for auto-update feature
166 add_action( 'jet-engine/register-macros', array( $this, 'register_macros' ) );
167 add_filter( 'jet-engine/listings/macros-list', array( $this, 'add_relation_macro_source_to_macro_args' ), 999 );
168 add_filter( 'jet-engine/relations/sources-list', array( $this, 'add_relation_macro_source' ) );
169 add_filter( 'jet-engine/relations/object-id-by-source/jfbuf_field', array( $this, 'resolve_relation_macro_source' ), 10, 2 );
170 }
171
172 public function remove_hooks() {
173 remove_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
174 remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_scripts' ) );
175
176 remove_filter(
177 'jet-form-builder/preset/source-types',
178 array( $this, 'add_sources' )
179 );
180 remove_filter(
181 'jet-form-builder/forms/options-generators',
182 array( $this, 'add_generators' )
183 );
184 remove_filter(
185 'jet-form-builder/custom-template-object',
186 array( $this, 'get_custom_object_for_template' )
187 );
188 remove_action(
189 'jet-form-builder/generators/get_from_query/setup',
190 array( $this, 'add_cct_type_arg_to_block_args' )
191 );
192 remove_filter(
193 'jet-form-builder/blocks/items',
194 array( $this, 'add_blocks' ),
195 0
196 );
197 remove_filter(
198 'jet-form-builder/render/checkbox-field/option',
199 array( $this, 'on_render_field_option' )
200 );
201 remove_filter(
202 'jet-form-builder/render/radio-field/option',
203 array( $this, 'on_render_field_option' )
204 );
205 remove_filter(
206 'render_block_jet-forms/checkbox-field',
207 array( $this, 'reset_post_data_after_render_options' )
208 );
209 remove_filter(
210 'render_block_jet-forms/radio-field',
211 array( $this, 'reset_post_data_after_render_options' )
212 );
213
214 remove_action(
215 'jet-form-builder/actions/register',
216 array( $this, 'add_actions' )
217 );
218
219 if ( jet_engine()->relations ) {
220 remove_filter(
221 'jet-form-builder/post-modifier/object-properties',
222 array( $this, 'add_post_properties' )
223 );
224 }
225
226 $this->option_query->remove_hooks();
227
228 /**
229 * @see https://github.com/Crocoblock/issues-tracker/issues/12555
230 */
231 remove_action( 'jet-engine/rest-api/init-endpoints', array( $this, 'rewrite_map_location_data_endpoint' ), 99 );
232
233 remove_action( 'jet-engine/register-macros', array( $this, 'register_macros' ) );
234 remove_filter( 'jet-engine/listings/macros-list', array( $this, 'add_relation_macro_source_to_macro_args' ), 999 );
235 remove_filter( 'jet-engine/relations/sources-list', array( $this, 'add_relation_macro_source' ) );
236 remove_filter( 'jet-engine/relations/object-id-by-source/jfbuf_field', array( $this, 'resolve_relation_macro_source' ), 10 );
237 }
238
239 /**
240 * If map fields endpoint are regsitered - rewrite location data endpoint permission_callback.
241 * This is required to make it public to use with map field search autocomplete.
242 *
243 * @see https://github.com/Crocoblock/issues-tracker/issues/12555
244 */
245 public function rewrite_map_location_data_endpoint( $api_manager ) {
246 if ( class_exists( '\Jet_Engine\Modules\Maps_Listings\Get_Map_Location_Data' ) ) {
247 require_once $this->get_dir( 'map-field/get-map-location-data-endpoint.php' );
248 $api_manager->register_endpoint( new Map_Field\Get_Map_Location_Data_Endpoint() );
249 }
250 }
251
252 /**
253 * Register JetEngine macros for auto-update feature.
254 *
255 * Registers macro that allows JetEngine Query Builder to access form field values
256 * during cascading field updates (auto-update feature).
257 */
258 public function register_macros() {
259 require_once $this->get_dir( 'macros/base-field-context-macro.php' );
260 require_once $this->get_dir( 'macros/auto-update-field-value.php' );
261
262 new Macros\Auto_Update_Field_Value();
263
264 if ( function_exists( 'jet_apb' ) ) {
265 require_once $this->get_dir( 'macros/auto-update-appointment-provider.php' );
266 require_once $this->get_dir( 'macros/auto-update-appointment-service.php' );
267
268 new Macros\Auto_Update_Appointment_Provider();
269 new Macros\Auto_Update_Appointment_Service();
270 }
271 }
272
273 public function add_relation_macro_source_to_macro_args( array $macros ): array {
274 foreach ( $macros as $key => $macro ) {
275 if ( empty( $macro['args']['rel_object_var']['condition']['rel_object_from'] ) ) {
276 continue;
277 }
278
279 $macros[ $key ]['args']['rel_object_var']['condition']['rel_object_from'][] = 'jfbuf_field';
280 }
281
282 return $macros;
283 }
284
285 public function add_relation_macro_source( array $sources ): array {
286 $sources['jfbuf_field'] = __( 'JFB Auto-Update - Form field', 'jet-form-builder' );
287
288 return $sources;
289 }
290
291 public function resolve_relation_macro_source( $object_id, $form_field = '' ) {
292 if ( empty( $form_field ) ) {
293 return false;
294 }
295
296 $request_key = 'jfb_update_related_' . $form_field;
297
298 if (
299 isset( $GLOBALS['jfb_generator_context'] ) &&
300 is_array( $GLOBALS['jfb_generator_context'] ) &&
301 array_key_exists( $form_field, $GLOBALS['jfb_generator_context'] )
302 ) {
303 return $GLOBALS['jfb_generator_context'][ $form_field ];
304 }
305
306 if ( isset( $_REQUEST[ $request_key ] ) ) {
307 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
308 return $_REQUEST[ $request_key ];
309 }
310
311 if ( isset( $GLOBALS[ $request_key ] ) ) {
312 return $GLOBALS[ $request_key ];
313 }
314
315 if ( function_exists( 'jet_fb_context' ) ) {
316 $request_data = jet_fb_context()->resolve_request();
317
318 if ( array_key_exists( $form_field, $request_data ) ) {
319 return $request_data[ $form_field ];
320 }
321 }
322
323 return false;
324 }
325
326 public function register_scripts() {
327 $script_asset = require_once $this->get_dir( 'assets/build/frontend/listing.options.asset.php' );
328
329 if ( true === $script_asset ) {
330 return;
331 }
332
333 array_push(
334 $script_asset['dependencies'],
335 Module::MAIN_SCRIPT_HANDLE
336 );
337
338 wp_register_script(
339 Module::LISTING_OPTIONS_HANDLE,
340 $this->get_url( 'assets/build/frontend/listing.options.js' ),
341 $script_asset['dependencies'],
342 $script_asset['version'],
343 true
344 );
345 }
346
347 public function enqueue_admin_assets() {
348 $script_asset = require_once $this->get_dir( 'assets/build/editor.asset.php' );
349 $handle = $this->get_handle( 'editor' );
350
351 wp_enqueue_script(
352 $handle,
353 $this->get_url( 'assets/build/editor.js' ),
354 $script_asset['dependencies'],
355 $script_asset['version'],
356 true
357 );
358 }
359
360 public function add_sources( array $sources ): array {
361 array_push(
362 $sources,
363 new Preset_Source_Options_Page(),
364 new Preset_User()
365 );
366
367 return $sources;
368 }
369
370 public function add_generators( array $generators ): array {
371 array_push(
372 $generators,
373 new Get_From_Field(),
374 new Get_From_Je_Query()
375 );
376
377 return $generators;
378 }
379
380 public function add_blocks( array $blocks ): array {
381 array_push(
382 $blocks,
383 new Blocks\Map_Field\Block_Type()
384 );
385
386 return $blocks;
387 }
388
389 public function register_listing_related_blocks() {
390 register_block_type(
391 $this->get_dir( 'blocks/check-mark' )
392 );
393
394 ( new Block_Asset() )->init_hooks();
395 }
396
397 public function add_actions( \Jet_Form_Builder\Actions\Manager $manager ) {
398 $manager->register_action_type( new Update_Options() );
399 }
400
401 public function add_post_properties(
402 Object_Properties_Collection $collection
403 ): Object_Properties_Collection {
404 return $collection->add( new Post_Je_Relation_Property() );
405 }
406
407 public function on_render_field_checkbox( string $item, $value, $option, $render ): string {
408 $this->option_type = 'checkbox';
409
410 return $this->on_render_field_option( $item, $value, $option, $render );
411 }
412
413 public function on_render_field_radio( string $item, $value, $option, $render ): string {
414 $this->option_type = 'radio';
415
416 return $this->on_render_field_option( $item, $value, $option, $render );
417 }
418
419 /**
420 * @param string $item
421 * @param $value
422 * @param $option
423 * @param Checkbox\Block_Render|Radio\Block_Render $render
424 *
425 * @return string
426 */
427 public function on_render_field_option( string $item, $value, $option, $render ): string {
428 $template = '';
429
430 if ( ! empty( $render->args['custom_item_template'] ) ) {
431 $template = ( new Builder_Helper() )->get_custom_template(
432 $option['object_id'] ?? $value,
433 $render->args
434 );
435 }
436
437 if ( $template ) {
438 $this->has_custom_template = true;
439 }
440
441 return ( $template . $item );
442 }
443
444 public function reset_post_data_after_render_options( string $content ): string {
445 if ( $this->has_custom_template ) {
446 wp_reset_postdata();
447 }
448
449 $this->has_custom_template = null;
450
451 return $content;
452 }
453
454 public function add_cct_type_arg_to_block_args( Get_From_Je_Query $generator, Base_Query $query ) {
455 if ( 'custom-content-type' !== $query->query_type ) {
456 return;
457 }
458
459 try {
460 $block = $generator->get_block();
461 } catch ( \Throwable $throwable ) {
462 return;
463 }
464
465 if ( ! $block ) {
466 return;
467 }
468
469 $block->block_attrs['je_generator_content_type'] = (
470 $query->final_query['content_type'] ?? ''
471 );
472 }
473
474 public function get_custom_object_for_template( $data_object, $object_id, $args ) {
475 switch ( $args['je_generator_query_type'] ?? false ) {
476 case 'users':
477 return get_user_by( 'ID', $object_id );
478 case 'terms':
479 return get_term( $object_id );
480 case 'posts':
481 return get_post( $object_id );
482 case 'custom-content-type':
483 /** @var Custom_Content_Types\Factory $cct_factory */
484 $cct_factory = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types(
485 $args['je_generator_content_type']
486 );
487
488 if ( ! is_object( $cct_factory ) ) {
489 return false;
490 }
491
492 return (object) $cct_factory->get_db()->get_item( $object_id );
493 default:
494 return $data_object;
495 }
496 }
497
498 /**
499 * Only for the internal usage.
500 *
501 * @return string
502 */
503 public function get_option_type(): string {
504 return $this->option_type;
505 }
506
507 }
508