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