| 1 |
<?php |
| 2 |
|
| 3 |
namespace Jet_Form_Builder\Admin; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Conditions\Condition_Manager as Action_Condition_Manager; |
| 6 |
use Jet_Form_Builder\Admin\Pages\Pages_Manager; |
| 7 |
use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager; |
| 8 |
use Jet_Form_Builder\Blocks\Validation; |
| 9 |
use Jet_Form_Builder\Classes\Arguments\Form_Arguments; |
| 10 |
use Jet_Form_Builder\Classes\Http\Utm_Url; |
| 11 |
use Jet_Form_Builder\Classes\Tools; |
| 12 |
use Jet_Form_Builder\Gateways\Gateway_Manager; |
| 13 |
use Jet_Form_Builder\Plugin; |
| 14 |
use Jet_Form_Builder\Blocks\Conditional_Block\Condition_Manager as Block_Condition_Manager; |
| 15 |
use Jet_Form_Builder\Post_Meta\Messages_Meta; |
| 16 |
|
| 17 |
/** |
| 18 |
* Form editor class |
| 19 |
* Thanks Tom J Nowell for initial editor idea and inspiration! |
| 20 |
*/ |
| 21 |
|
| 22 |
// If this file is called directly, abort. |
| 23 |
if ( ! defined( 'WPINC' ) ) { |
| 24 |
die; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Define Editor class |
| 29 |
*/ |
| 30 |
class Editor { |
| 31 |
|
| 32 |
const EDITOR_HANDLE = 'jet-form-builder-editor'; |
| 33 |
const EDITOR_PACKAGE_HANDLE = 'jet-form-builder-editor-package'; |
| 34 |
|
| 35 |
public function __construct() { |
| 36 |
add_action( 'enqueue_block_editor_assets', array( $this, 'admin_assets' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register admin assets |
| 41 |
* |
| 42 |
* @return void [type] [description] |
| 43 |
*/ |
| 44 |
public function admin_assets() { |
| 45 |
if ( jet_form_builder()->post_type->is_form_editor ) { |
| 46 |
$this->enqueue_assets(); |
| 47 |
} else { |
| 48 |
$this->enqueue_form_assets(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns taxonomies list for the config |
| 54 |
* |
| 55 |
* @return [type] [description] |
| 56 |
*/ |
| 57 |
public function get_taxonomies_list() { |
| 58 |
|
| 59 |
$taxonomies = get_taxonomies( array(), 'objects' ); |
| 60 |
|
| 61 |
$result = array(); |
| 62 |
|
| 63 |
foreach ( $taxonomies as $tax ) { |
| 64 |
$result[] = array( |
| 65 |
'value' => $tax->name, |
| 66 |
'label' => sprintf( '%1$s (%2$s)', $tax->label, $tax->name ), |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
return $result; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
public function get_preset_config() { |
| 75 |
return apply_filters( |
| 76 |
'jet-form-builder/editor/preset-config', |
| 77 |
array( |
| 78 |
'global_fields' => array( |
| 79 |
array( |
| 80 |
'name' => 'from', |
| 81 |
'label' => __( 'Source:', 'jet-form-builder' ), |
| 82 |
'type' => 'select', |
| 83 |
'options' => Tools::with_placeholder( |
| 84 |
array( |
| 85 |
array( |
| 86 |
'value' => 'post', |
| 87 |
'label' => __( 'Post', 'jet-form-builder' ), |
| 88 |
), |
| 89 |
array( |
| 90 |
'value' => 'user', |
| 91 |
'label' => __( 'User', 'jet-form-builder' ), |
| 92 |
), |
| 93 |
array( |
| 94 |
'value' => 'query_var', |
| 95 |
'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 96 |
), |
| 97 |
) |
| 98 |
), |
| 99 |
), |
| 100 |
array( |
| 101 |
'name' => 'post_from', |
| 102 |
'label' => __( 'Get post ID from:', 'jet-form-builder' ), |
| 103 |
'type' => 'select', |
| 104 |
'options' => Tools::with_placeholder( |
| 105 |
array( |
| 106 |
array( |
| 107 |
'value' => 'current_post', |
| 108 |
'label' => __( 'Current post', 'jet-form-builder' ), |
| 109 |
), |
| 110 |
array( |
| 111 |
'value' => 'query_var', |
| 112 |
'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 113 |
), |
| 114 |
) |
| 115 |
), |
| 116 |
'condition' => array( |
| 117 |
'field' => 'from', |
| 118 |
'value' => 'post', |
| 119 |
), |
| 120 |
), |
| 121 |
array( |
| 122 |
'name' => 'user_from', |
| 123 |
'label' => __( 'Get user ID from:', 'jet-form-builder' ), |
| 124 |
'type' => 'select', |
| 125 |
'options' => Tools::with_placeholder( |
| 126 |
array( |
| 127 |
array( |
| 128 |
'value' => 'current_user', |
| 129 |
'label' => __( 'Current user', 'jet-form-builder' ), |
| 130 |
), |
| 131 |
array( |
| 132 |
'value' => 'queried_user', |
| 133 |
'label' => __( 'Queried user', 'jet-form-builder' ), |
| 134 |
), |
| 135 |
array( |
| 136 |
'value' => 'query_var', |
| 137 |
'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 138 |
), |
| 139 |
) |
| 140 |
), |
| 141 |
'condition' => array( |
| 142 |
'field' => 'from', |
| 143 |
'value' => 'user', |
| 144 |
), |
| 145 |
), |
| 146 |
array( |
| 147 |
'name' => 'query_var', |
| 148 |
'label' => __( 'Query variable name:', 'jet-form-builder' ), |
| 149 |
'type' => 'text', |
| 150 |
'custom_condition' => 'query_var', |
| 151 |
'position' => 'dynamic', |
| 152 |
), |
| 153 |
), |
| 154 |
'map_fields' => array( |
| 155 |
array( |
| 156 |
'name' => 'key', |
| 157 |
'label' => __( 'Query variable key', 'jet-form-builder' ), |
| 158 |
'type' => 'text', |
| 159 |
'position' => 'general', |
| 160 |
'parent_condition' => array( |
| 161 |
'field' => 'from', |
| 162 |
'value' => 'query_var', |
| 163 |
), |
| 164 |
), |
| 165 |
array( |
| 166 |
'name' => 'prop', |
| 167 |
'label' => __( 'Post property', 'jet-form-builder' ), |
| 168 |
'type' => 'select', |
| 169 |
'options' => Tools::with_placeholder( |
| 170 |
array( |
| 171 |
array( |
| 172 |
'value' => 'ID', |
| 173 |
'label' => __( 'Post ID', 'jet-form-builder' ), |
| 174 |
), |
| 175 |
array( |
| 176 |
'value' => 'post_title', |
| 177 |
'label' => __( 'Post Title', 'jet-form-builder' ), |
| 178 |
), |
| 179 |
array( |
| 180 |
'value' => 'post_content', |
| 181 |
'label' => __( 'Post Content', 'jet-form-builder' ), |
| 182 |
), |
| 183 |
array( |
| 184 |
'value' => 'post_status', |
| 185 |
'label' => __( 'Post Status', 'jet-form-builder' ), |
| 186 |
), |
| 187 |
array( |
| 188 |
'value' => 'post_author', |
| 189 |
'label' => __( 'Post Author', 'jet-form-builder' ), |
| 190 |
), |
| 191 |
array( |
| 192 |
'value' => 'post_excerpt', |
| 193 |
'label' => __( 'Post Excerpt', 'jet-form-builder' ), |
| 194 |
), |
| 195 |
array( |
| 196 |
'value' => 'post_date', |
| 197 |
'label' => __( 'Post Date', 'jet-form-builder' ), |
| 198 |
), |
| 199 |
array( |
| 200 |
'value' => 'post_date_gmt', |
| 201 |
'label' => __( 'Post Date GMT', 'jet-form-builder' ), |
| 202 |
), |
| 203 |
array( |
| 204 |
'value' => 'post_thumb', |
| 205 |
'label' => __( 'Post Thumbnail', 'jet-form-builder' ), |
| 206 |
), |
| 207 |
array( |
| 208 |
'value' => 'post_meta', |
| 209 |
'label' => __( 'Post Meta', 'jet-form-builder' ), |
| 210 |
), |
| 211 |
array( |
| 212 |
'value' => 'post_terms', |
| 213 |
'label' => __( 'Post Terms', 'jet-form-builder' ), |
| 214 |
), |
| 215 |
) |
| 216 |
), |
| 217 |
'parent_condition' => array( |
| 218 |
'field' => 'from', |
| 219 |
'value' => 'post', |
| 220 |
), |
| 221 |
), |
| 222 |
array( |
| 223 |
'name' => 'key', |
| 224 |
'label' => __( 'Taxonomy', 'jet-form-builder' ), |
| 225 |
'type' => 'select', |
| 226 |
'options' => Tools::with_placeholder( $this->get_taxonomies_list() ), |
| 227 |
'parent_condition' => array( |
| 228 |
'field' => 'from', |
| 229 |
'value' => 'post', |
| 230 |
), |
| 231 |
'condition' => array( |
| 232 |
'field' => 'prop', |
| 233 |
'value' => 'post_terms', |
| 234 |
), |
| 235 |
), |
| 236 |
array( |
| 237 |
'name' => 'key', |
| 238 |
'label' => __( 'Meta field key', 'jet-form-builder' ), |
| 239 |
'type' => 'text', |
| 240 |
'parent_condition' => array( |
| 241 |
'field' => 'from', |
| 242 |
'value' => 'post', |
| 243 |
), |
| 244 |
'condition' => array( |
| 245 |
'field' => 'prop', |
| 246 |
'value' => 'post_meta', |
| 247 |
), |
| 248 |
), |
| 249 |
array( |
| 250 |
'name' => 'prop', |
| 251 |
'label' => __( 'User field', 'jet-form-builder' ), |
| 252 |
'type' => 'select', |
| 253 |
'options' => Tools::with_placeholder( |
| 254 |
array( |
| 255 |
array( |
| 256 |
'value' => 'ID', |
| 257 |
'label' => __( 'User ID', 'jet-form-builder' ), |
| 258 |
), |
| 259 |
array( |
| 260 |
'value' => 'user_login', |
| 261 |
'label' => __( 'User Login', 'jet-form-builder' ), |
| 262 |
), |
| 263 |
array( |
| 264 |
'value' => 'user_email', |
| 265 |
'label' => __( 'Email', 'jet-form-builder' ), |
| 266 |
), |
| 267 |
array( |
| 268 |
'value' => 'password', |
| 269 |
'label' => __( 'Password', 'jet-form-builder' ), |
| 270 |
), |
| 271 |
array( |
| 272 |
'value' => 'first_name', |
| 273 |
'label' => __( 'First Name', 'jet-form-builder' ), |
| 274 |
), |
| 275 |
array( |
| 276 |
'value' => 'last_name', |
| 277 |
'label' => __( 'Last Name', 'jet-form-builder' ), |
| 278 |
), |
| 279 |
array( |
| 280 |
'value' => 'user_url', |
| 281 |
'label' => __( 'User URL', 'jet-form-builder' ), |
| 282 |
), |
| 283 |
array( |
| 284 |
'value' => 'user_meta', |
| 285 |
'label' => __( 'User Meta', 'jet-form-builder' ), |
| 286 |
), |
| 287 |
) |
| 288 |
), |
| 289 |
'parent_condition' => array( |
| 290 |
'field' => 'from', |
| 291 |
'value' => 'user', |
| 292 |
), |
| 293 |
), |
| 294 |
array( |
| 295 |
'name' => 'key', |
| 296 |
'label' => __( 'Meta field key', 'jet-form-builder' ), |
| 297 |
'type' => 'text', |
| 298 |
'parent_condition' => array( |
| 299 |
'field' => 'from', |
| 300 |
'value' => 'user', |
| 301 |
), |
| 302 |
'condition' => array( |
| 303 |
'field' => 'prop', |
| 304 |
'value' => 'user_meta', |
| 305 |
), |
| 306 |
), |
| 307 |
), |
| 308 |
) |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Enqueue editor assets |
| 314 |
* |
| 315 |
* @return void |
| 316 |
*/ |
| 317 |
public function enqueue_assets() { |
| 318 |
do_action( 'jet-form-builder/editor-package/before', $this, self::EDITOR_PACKAGE_HANDLE ); |
| 319 |
|
| 320 |
wp_enqueue_script( |
| 321 |
self::EDITOR_PACKAGE_HANDLE, |
| 322 |
Plugin::instance()->plugin_url( 'assets/js/editor/package{min}.js' ), |
| 323 |
array( |
| 324 |
'wp-editor', |
| 325 |
'wp-core-data', |
| 326 |
'wp-data', |
| 327 |
'wp-block-library', |
| 328 |
'wp-format-library', |
| 329 |
'wp-api-fetch', |
| 330 |
), |
| 331 |
JET_FORM_BUILDER_VERSION, |
| 332 |
true |
| 333 |
); |
| 334 |
|
| 335 |
wp_localize_script( |
| 336 |
self::EDITOR_PACKAGE_HANDLE, |
| 337 |
'jetFormEvents', |
| 338 |
jet_fb_events()->to_array() |
| 339 |
); |
| 340 |
|
| 341 |
wp_localize_script( |
| 342 |
self::EDITOR_PACKAGE_HANDLE, |
| 343 |
'jetFormValidation', |
| 344 |
Validation::instance()->to_array() |
| 345 |
); |
| 346 |
|
| 347 |
wp_localize_script( |
| 348 |
self::EDITOR_PACKAGE_HANDLE, |
| 349 |
'jetFormBlockConditions', |
| 350 |
Block_Condition_Manager::instance()->to_array() |
| 351 |
); |
| 352 |
|
| 353 |
wp_set_script_translations( |
| 354 |
self::EDITOR_PACKAGE_HANDLE, |
| 355 |
'jet-form-builder', |
| 356 |
Plugin::instance()->plugin_dir( 'languages' ) |
| 357 |
); |
| 358 |
|
| 359 |
do_action( 'jet-form-builder/editor-assets/before', $this, self::EDITOR_HANDLE ); |
| 360 |
|
| 361 |
wp_enqueue_script( |
| 362 |
self::EDITOR_HANDLE, |
| 363 |
Plugin::instance()->plugin_url( 'assets/js/editor/form.builder{min}.js' ), |
| 364 |
array(), |
| 365 |
JET_FORM_BUILDER_VERSION, |
| 366 |
true |
| 367 |
); |
| 368 |
|
| 369 |
wp_enqueue_style( |
| 370 |
self::EDITOR_HANDLE, |
| 371 |
JET_FORM_BUILDER_URL . 'assets/css/editor.css', |
| 372 |
array( |
| 373 |
'media', |
| 374 |
'l10n', |
| 375 |
'buttons', |
| 376 |
'wp-edit-blocks', |
| 377 |
'wp-editor', |
| 378 |
), |
| 379 |
JET_FORM_BUILDER_VERSION, |
| 380 |
'all' |
| 381 |
); |
| 382 |
|
| 383 |
$conditions_settings = ( new Action_Condition_Manager() )->get_settings(); |
| 384 |
|
| 385 |
/** @var Messages_Meta $messages_meta */ |
| 386 |
$messages_meta = jet_form_builder()->post_type->get_meta( Messages_Meta::class ); |
| 387 |
|
| 388 |
$utm = new Utm_Url( 'wp-admin/editor-jet-form' ); |
| 389 |
$addons = JET_FORM_BUILDER_SITE . '/addons/'; |
| 390 |
$pricing = JET_FORM_BUILDER_SITE . '/pricing/'; |
| 391 |
|
| 392 |
wp_localize_script( |
| 393 |
self::EDITOR_PACKAGE_HANDLE, |
| 394 |
'JetFormEditorData', |
| 395 |
apply_filters( |
| 396 |
'jet-form-builder/editor/config', |
| 397 |
array( |
| 398 |
'presetConfig' => $this->get_preset_config(), |
| 399 |
'messagesDefault' => $messages_meta->messages(), |
| 400 |
'gateways' => Gateway_Manager::instance()->editor_data(), |
| 401 |
'helpForRepeaters' => $this->get_help_for_repeaters(), |
| 402 |
'global_settings' => Tab_Handler_Manager::instance()->all(), |
| 403 |
'global_settings_url' => Pages_Manager::instance()->get_stable_url( 'jfb-settings' ), |
| 404 |
'jetEngineVersion' => Tools::get_jet_engine_version(), |
| 405 |
'actionConditionSettings' => $conditions_settings, |
| 406 |
'argumentsSource' => Form_Arguments::get_options(), |
| 407 |
'utmLinks' => array( |
| 408 |
'allProActions' => $utm->set_campaign( 'pro-actions' )->add_query( $addons ), |
| 409 |
'limitResponses' => $utm->set_campaign( 'responses-pricing' )->add_query( $pricing ), |
| 410 |
'scheduleForm' => $utm->set_campaign( 'schedule-pricing' )->add_query( $pricing ), |
| 411 |
), |
| 412 |
'isActivePro' => jet_form_builder()->addons_manager->is_active(), |
| 413 |
) |
| 414 |
) |
| 415 |
); |
| 416 |
|
| 417 |
do_action( 'jet-form-builder/editor-assets/after', $this, self::EDITOR_HANDLE ); |
| 418 |
} |
| 419 |
|
| 420 |
private function get_help_for_repeaters() { |
| 421 |
return array( |
| 422 |
'conditional_block' => array( |
| 423 |
'label' => __( 'With many conditions for the block, they are checked with the AND operator', 'jet-form-builder' ), |
| 424 |
), |
| 425 |
'conditional_block_or' => array( |
| 426 |
'label' => __( 'With many conditions for the block, they are checked with the OR operator', 'jet-form-builder' ), |
| 427 |
), |
| 428 |
'conditional_action' => array( |
| 429 |
'label' => __( 'With many conditions for the action, they are checked with the AND operator', 'jet-form-builder' ), |
| 430 |
), |
| 431 |
'conditional_action_or' => array( |
| 432 |
'label' => __( 'With many conditions for the action, they are checked with the OR operator', 'jet-form-builder' ), |
| 433 |
), |
| 434 |
); |
| 435 |
} |
| 436 |
|
| 437 |
public function enqueue_form_assets() { |
| 438 |
|
| 439 |
$handle = 'jet-form-builder/form'; |
| 440 |
|
| 441 |
do_action( 'jet-form-builder/other-editor-assets/before', $this, $handle ); |
| 442 |
|
| 443 |
wp_register_script( |
| 444 |
$handle, |
| 445 |
Plugin::instance()->plugin_url( 'assets/js/editor/default.builder{min}.js' ), |
| 446 |
array( |
| 447 |
'wp-core-data', |
| 448 |
'wp-data', |
| 449 |
'wp-block-library', |
| 450 |
'wp-format-library', |
| 451 |
'wp-api-fetch', |
| 452 |
), |
| 453 |
JET_FORM_BUILDER_VERSION, |
| 454 |
true |
| 455 |
); |
| 456 |
|
| 457 |
wp_register_style( |
| 458 |
'jet-form-builder-others', |
| 459 |
Plugin::instance()->plugin_url( 'assets/css/frontend.css' ), |
| 460 |
array(), |
| 461 |
Plugin::instance()->get_version() |
| 462 |
); |
| 463 |
|
| 464 |
do_action( 'jet-form-builder/other-editor-assets/after', $this, $handle ); |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
} |
| 469 |
|