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