| @@ -1,564 +1,492 @@ | ||
| 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 | - array( | |
| 91 | - 'value' => 'term', | |
| 92 | - 'label' => __( 'Term', 'jet-form-builder' ), | |
| 93 | - ), | |
| 94 | - ) | |
| 95 | - ), | |
| 96 | - ), | |
| 97 | - array( | |
| 98 | - 'name' => 'post_from', | |
| 99 | - 'label' => __( 'Get post ID from:', 'jet-form-builder' ), | |
| 100 | - 'type' => 'select', | |
| 101 | - 'options' => Tools::with_placeholder( | |
| 102 | - array( | |
| 103 | - array( | |
| 104 | - 'value' => 'current_post', | |
| 105 | - 'label' => __( 'Current post', 'jet-form-builder' ), | |
| 106 | - ), | |
| 107 | - array( | |
| 108 | - 'value' => 'query_var', | |
| 109 | - 'label' => __( 'URL Query Variable', 'jet-form-builder' ), | |
| 110 | - ), | |
| 111 | - ) | |
| 112 | - ), | |
| 113 | - 'condition' => array( | |
| 114 | - 'field' => 'from', | |
| 115 | - 'value' => 'post', | |
| 116 | - ), | |
| 117 | - ), | |
| 118 | - array( | |
| 119 | - 'name' => 'user_from', | |
| 120 | - 'label' => __( 'Get user ID from:', 'jet-form-builder' ), | |
| 121 | - 'type' => 'select', | |
| 122 | - 'options' => Tools::with_placeholder( | |
| 123 | - array( | |
| 124 | - array( | |
| 125 | - 'value' => 'current_user', | |
| 126 | - 'label' => __( 'Current user', 'jet-form-builder' ), | |
| 127 | - ), | |
| 128 | - array( | |
| 129 | - 'value' => 'queried_user', | |
| 130 | - 'label' => __( 'Queried user', 'jet-form-builder' ), | |
| 131 | - ), | |
| 132 | - array( | |
| 133 | - 'value' => 'query_var', | |
| 134 | - 'label' => __( 'URL Query Variable', 'jet-form-builder' ), | |
| 135 | - ), | |
| 136 | - ) | |
| 137 | - ), | |
| 138 | - 'condition' => array( | |
| 139 | - 'field' => 'from', | |
| 140 | - 'value' => 'user', | |
| 141 | - ), | |
| 142 | - ), | |
| 143 | - array( | |
| 144 | - 'name' => 'term_from', | |
| 145 | - 'label' => __( 'Get term from:', 'jet-form-builder' ), | |
| 146 | - 'type' => 'select', | |
| 147 | - 'options' => Tools::with_placeholder( | |
| 148 | - array( | |
| 149 | - array( | |
| 150 | - 'value' => 'current_term', | |
| 151 | - 'label' => __( 'Current term', 'jet-form-builder' ), | |
| 152 | - ), | |
| 153 | - array( | |
| 154 | - 'value' => 'query_var', | |
| 155 | - 'label' => __( 'URL Query Variable', 'jet-form-builder' ), | |
| 156 | - ), | |
| 157 | - ) | |
| 158 | - ), | |
| 159 | - 'condition' => array( | |
| 160 | - 'field' => 'from', | |
| 161 | - 'value' => 'term', | |
| 162 | - ), | |
| 163 | - ), | |
| 164 | - array( | |
| 165 | - 'name' => 'term_taxonomy', | |
| 166 | - 'label' => __( 'Term taxonomy', 'jet-form-builder' ), | |
| 167 | - 'type' => 'select', | |
| 168 | - 'options' => Tools::with_placeholder( $this->get_taxonomies_list() ), | |
| 169 | - 'parent_condition' => array( | |
| 170 | - 'field' => 'from', | |
| 171 | - 'value' => 'term', | |
| 172 | - ), | |
| 173 | - 'condition' => array( | |
| 174 | - 'field' => 'term_from', | |
| 175 | - 'value' => 'query_var', | |
| 176 | - ), | |
| 177 | - ), | |
| 178 | - array( | |
| 179 | - 'name' => 'query_var', | |
| 180 | - 'label' => __( 'Query variable name:', 'jet-form-builder' ), | |
| 181 | - 'type' => 'text', | |
| 182 | - 'custom_condition' => 'query_var', | |
| 183 | - 'position' => 'dynamic', | |
| 184 | - ), | |
| 185 | - ), | |
| 186 | - 'map_fields' => array( | |
| 187 | - array( | |
| 188 | - 'name' => 'key', | |
| 189 | - 'label' => __( 'Query variable key', 'jet-form-builder' ), | |
| 190 | - 'type' => 'text', | |
| 191 | - 'position' => 'general', | |
| 192 | - 'parent_condition' => array( | |
| 193 | - 'field' => 'from', | |
| 194 | - 'value' => 'query_var', | |
| 195 | - ), | |
| 196 | - ), | |
| 197 | - array( | |
| 198 | - 'name' => 'prop', | |
| 199 | - 'label' => __( 'Post property', 'jet-form-builder' ), | |
| 200 | - 'type' => 'select', | |
| 201 | - 'options' => Tools::with_placeholder( | |
| 202 | - array( | |
| 203 | - array( | |
| 204 | - 'value' => 'ID', | |
| 205 | - 'label' => __( 'Post ID', 'jet-form-builder' ), | |
| 206 | - ), | |
| 207 | - array( | |
| 208 | - 'value' => 'post_title', | |
| 209 | - 'label' => __( 'Post Title', 'jet-form-builder' ), | |
| 210 | - ), | |
| 211 | - array( | |
| 212 | - 'value' => 'post_content', | |
| 213 | - 'label' => __( 'Post Content', 'jet-form-builder' ), | |
| 214 | - ), | |
| 215 | - array( | |
| 216 | - 'value' => 'post_status', | |
| 217 | - 'label' => __( 'Post Status', 'jet-form-builder' ), | |
| 218 | - ), | |
| 219 | - array( | |
| 220 | - 'value' => 'post_author', | |
| 221 | - 'label' => __( 'Post Author', 'jet-form-builder' ), | |
| 222 | - ), | |
| 223 | - array( | |
| 224 | - 'value' => 'post_excerpt', | |
| 225 | - 'label' => __( 'Post Excerpt', 'jet-form-builder' ), | |
| 226 | - ), | |
| 227 | - array( | |
| 228 | - 'value' => 'post_date', | |
| 229 | - 'label' => __( 'Post Date', 'jet-form-builder' ), | |
| 230 | - ), | |
| 231 | - array( | |
| 232 | - 'value' => 'post_date_gmt', | |
| 233 | - 'label' => __( 'Post Date GMT', 'jet-form-builder' ), | |
| 234 | - ), | |
| 235 | - array( | |
| 236 | - 'value' => 'post_thumb', | |
| 237 | - 'label' => __( 'Post Thumbnail', 'jet-form-builder' ), | |
| 238 | - ), | |
| 239 | - array( | |
| 240 | - 'value' => 'post_meta', | |
| 241 | - 'label' => __( 'Post Meta', 'jet-form-builder' ), | |
| 242 | - ), | |
| 243 | - array( | |
| 244 | - 'value' => 'post_terms', | |
| 245 | - 'label' => __( 'Post Terms', 'jet-form-builder' ), | |
| 246 | - ), | |
| 247 | - ) | |
| 248 | - ), | |
| 249 | - 'parent_condition' => array( | |
| 250 | - 'field' => 'from', | |
| 251 | - 'value' => 'post', | |
| 252 | - ), | |
| 253 | - ), | |
| 254 | - array( | |
| 255 | - 'name' => 'key', | |
| 256 | - 'label' => __( 'Taxonomy', 'jet-form-builder' ), | |
| 257 | - 'type' => 'select', | |
| 258 | - 'options' => Tools::with_placeholder( $this->get_taxonomies_list() ), | |
| 259 | - 'parent_condition' => array( | |
| 260 | - 'field' => 'from', | |
| 261 | - 'value' => 'post', | |
| 262 | - ), | |
| 263 | - 'condition' => array( | |
| 264 | - 'field' => 'prop', | |
| 265 | - 'value' => 'post_terms', | |
| 266 | - ), | |
| 267 | - ), | |
| 268 | - array( | |
| 269 | - 'name' => 'key', | |
| 270 | - 'label' => __( 'Meta field key', 'jet-form-builder' ), | |
| 271 | - 'type' => 'text', | |
| 272 | - 'parent_condition' => array( | |
| 273 | - 'field' => 'from', | |
| 274 | - 'value' => 'post', | |
| 275 | - ), | |
| 276 | - 'condition' => array( | |
| 277 | - 'field' => 'prop', | |
| 278 | - 'value' => 'post_meta', | |
| 279 | - ), | |
| 280 | - ), | |
| 281 | - array( | |
| 282 | - 'name' => 'prop', | |
| 283 | - 'label' => __( 'User field', 'jet-form-builder' ), | |
| 284 | - 'type' => 'select', | |
| 285 | - 'options' => Tools::with_placeholder( | |
| 286 | - array( | |
| 287 | - array( | |
| 288 | - 'value' => 'ID', | |
| 289 | - 'label' => __( 'User ID', 'jet-form-builder' ), | |
| 290 | - ), | |
| 291 | - array( | |
| 292 | - 'value' => 'user_login', | |
| 293 | - 'label' => __( 'User Login', 'jet-form-builder' ), | |
| 294 | - ), | |
| 295 | - array( | |
| 296 | - 'value' => 'user_email', | |
| 297 | - 'label' => __( 'Email', 'jet-form-builder' ), | |
| 298 | - ), | |
| 299 | - array( | |
| 300 | - 'value' => 'password', | |
| 301 | - 'label' => __( 'Password', 'jet-form-builder' ), | |
| 302 | - ), | |
| 303 | - array( | |
| 304 | - 'value' => 'first_name', | |
| 305 | - 'label' => __( 'First Name', 'jet-form-builder' ), | |
| 306 | - ), | |
| 307 | - array( | |
| 308 | - 'value' => 'last_name', | |
| 309 | - 'label' => __( 'Last Name', 'jet-form-builder' ), | |
| 310 | - ), | |
| 311 | - array( | |
| 312 | - 'value' => 'display_name', | |
| 313 | - 'label' => __( 'Display Name', 'jet-form-builder' ), | |
| 314 | - ), | |
| 315 | - array( | |
| 316 | - 'value' => 'user_url', | |
| 317 | - 'label' => __( 'User URL', 'jet-form-builder' ), | |
| 318 | - ), | |
| 319 | - array( | |
| 320 | - 'value' => 'user_meta', | |
| 321 | - 'label' => __( 'User Meta', 'jet-form-builder' ), | |
| 322 | - ), | |
| 323 | - ) | |
| 324 | - ), | |
| 325 | - 'parent_condition' => array( | |
| 326 | - 'field' => 'from', | |
| 327 | - 'value' => 'user', | |
| 328 | - ), | |
| 329 | - ), | |
| 330 | - array( | |
| 331 | - 'name' => 'key', | |
| 332 | - 'label' => __( 'Meta field key', 'jet-form-builder' ), | |
| 333 | - 'type' => 'text', | |
| 334 | - 'parent_condition' => array( | |
| 335 | - 'field' => 'from', | |
| 336 | - 'value' => 'user', | |
| 337 | - ), | |
| 338 | - 'condition' => array( | |
| 339 | - 'field' => 'prop', | |
| 340 | - 'value' => 'user_meta', | |
| 341 | - ), | |
| 342 | - ), | |
| 343 | - array( | |
| 344 | - 'name' => 'prop', | |
| 345 | - 'label' => __( 'Term property', 'jet-form-builder' ), | |
| 346 | - 'type' => 'select', | |
| 347 | - 'options' => Tools::with_placeholder( | |
| 348 | - array( | |
| 349 | - array( | |
| 350 | - 'value' => 'term_id', | |
| 351 | - 'label' => __( 'Term ID', 'jet-form-builder' ), | |
| 352 | - ), | |
| 353 | - array( | |
| 354 | - 'value' => 'name', | |
| 355 | - 'label' => __( 'Name', 'jet-form-builder' ), | |
| 356 | - ), | |
| 357 | - array( | |
| 358 | - 'value' => 'slug', | |
| 359 | - 'label' => __( 'Slug', 'jet-form-builder' ), | |
| 360 | - ), | |
| 361 | - array( | |
| 362 | - 'value' => 'parent', | |
| 363 | - 'label' => __( 'Parent', 'jet-form-builder' ), | |
| 364 | - ), | |
| 365 | - array( | |
| 366 | - 'value' => 'term_meta', | |
| 367 | - 'label' => __( 'Term Meta', 'jet-form-builder' ), | |
| 368 | - ), | |
| 369 | - ) | |
| 370 | - ), | |
| 371 | - 'parent_condition' => array( | |
| 372 | - 'field' => 'from', | |
| 373 | - 'value' => 'term', | |
| 374 | - ), | |
| 375 | - ), | |
| 376 | - array( | |
| 377 | - 'name' => 'key', | |
| 378 | - 'label' => __( 'Meta field key', 'jet-form-builder' ), | |
| 379 | - 'type' => 'text', | |
| 380 | - 'parent_condition' => array( | |
| 381 | - 'field' => 'from', | |
| 382 | - 'value' => 'term', | |
| 383 | - ), | |
| 384 | - 'condition' => array( | |
| 385 | - 'field' => 'prop', | |
| 386 | - 'value' => 'term_meta', | |
| 387 | - ), | |
| 388 | - ), | |
| 389 | - ), | |
| 390 | - ) | |
| 391 | - ); | |
| 392 | - } | |
| 393 | - | |
| 394 | - /** | |
| 395 | - * Enqueue editor assets | |
| 396 | - * | |
| 397 | - * @return void | |
| 398 | - * @throws Repository_Exception | |
| 399 | - */ | |
| 400 | - public function enqueue_assets() { | |
| 401 | - $script_asset = require_once jet_form_builder()->plugin_dir( 'assets/build/editor/package.asset.php' ); | |
| 402 | - | |
| 403 | - if ( true === $script_asset ) { | |
| 404 | - return; | |
| 405 | - } | |
| 406 | - | |
| 407 | - do_action( 'jet-form-builder/editor-package/before', $this, self::EDITOR_PACKAGE_HANDLE ); | |
| 408 | - | |
| 409 | - array_push( | |
| 410 | - $script_asset['dependencies'], | |
| 411 | - 'jet-fb-components', | |
| 412 | - 'jet-fb-data', | |
| 413 | - 'jet-fb-actions-v2', | |
| 414 | - 'jet-fb-blocks-v2-to-actions-v2' | |
| 415 | - ); | |
| 416 | - wp_enqueue_style( 'jet-fb-components' ); | |
| 417 | - | |
| 418 | - wp_enqueue_script( | |
| 419 | - self::EDITOR_PACKAGE_HANDLE, | |
| 420 | - Plugin::instance()->plugin_url( 'assets/build/editor/package.js' ), | |
| 421 | - $script_asset['dependencies'], | |
| 422 | - $script_asset['version'], | |
| 423 | - true | |
| 424 | - ); | |
| 425 | - | |
| 426 | - wp_localize_script( | |
| 427 | - self::EDITOR_PACKAGE_HANDLE, | |
| 428 | - 'jetFormEvents', | |
| 429 | - jet_fb_events()->to_array() | |
| 430 | - ); | |
| 431 | - | |
| 432 | - wp_localize_script( | |
| 433 | - self::EDITOR_PACKAGE_HANDLE, | |
| 434 | - 'jetFormBlockConditions', | |
| 435 | - Block_Condition_Manager::instance()->to_array() | |
| 436 | - ); | |
| 437 | - | |
| 438 | - wp_set_script_translations( | |
| 439 | - self::EDITOR_PACKAGE_HANDLE, | |
| 440 | - 'jet-form-builder', | |
| 441 | - Plugin::instance()->plugin_dir( 'languages' ) | |
| 442 | - ); | |
| 443 | - | |
| 444 | - $script_asset = require_once jet_form_builder()->plugin_dir( 'assets/build/editor/form.builder.asset.php' ); | |
| 445 | - | |
| 446 | - do_action( 'jet-form-builder/editor-assets/before', $this, self::EDITOR_HANDLE ); | |
| 447 | - | |
| 448 | - wp_enqueue_script( | |
| 449 | - self::EDITOR_HANDLE, | |
| 450 | - Plugin::instance()->plugin_url( 'assets/build/editor/form.builder.js' ), | |
| 451 | - $script_asset['dependencies'], | |
| 452 | - $script_asset['version'], | |
| 453 | - true | |
| 454 | - ); | |
| 455 | - | |
| 456 | - wp_enqueue_style( | |
| 457 | - self::EDITOR_HANDLE, | |
| 458 | - Plugin::instance()->plugin_url( 'assets/build/editor/form.builder.css' ), | |
| 459 | - array( | |
| 460 | - 'media', | |
| 461 | - 'l10n', | |
| 462 | - 'buttons', | |
| 463 | - 'wp-edit-blocks', | |
| 464 | - 'wp-editor', | |
| 465 | - ), | |
| 466 | - $script_asset['version'] | |
| 467 | - ); | |
| 468 | - | |
| 469 | - $conditions_settings = ( new Action_Condition_Manager() )->get_settings(); | |
| 470 | - $exclude_events = array( | |
| 471 | - 'redirect_to_page' => array( 'WC.ORDER.COMPLETE', 'WC.CHECKOUT.COMPLETE' ), | |
| 472 | - ); | |
| 473 | - | |
| 474 | - /** @var Module $post_type */ | |
| 475 | - /** @noinspection PhpUnhandledExceptionInspection */ | |
| 476 | - $post_type = jet_form_builder()->module( 'post-type' ); | |
| 477 | - | |
| 478 | - $utm = new Utm_Url( 'plugin' ); | |
| 479 | - $utm->set_medium( 'editor' ); | |
| 480 | - $utm->set_content( $utm->get_license_and_theme() ); | |
| 481 | - | |
| 482 | - $addons = JET_FORM_BUILDER_SITE . '/addons/'; | |
| 483 | - $pricing = JET_FORM_BUILDER_SITE . '/pricing/'; | |
| 484 | - | |
| 485 | - wp_localize_script( | |
| 486 | - self::EDITOR_PACKAGE_HANDLE, | |
| 487 | - 'JetFormEditorData', | |
| 488 | - apply_filters( | |
| 489 | - 'jet-form-builder/editor/config', | |
| 490 | - array( | |
| 491 | - 'presetConfig' => $this->get_preset_config(), | |
| 492 | - 'messagesDefault' => $post_type->get_messages(), | |
| 493 | - 'helpForRepeaters' => $this->get_help_for_repeaters(), | |
| 494 | - 'global_settings' => Tab_Handler_Manager::instance()->all(), | |
| 495 | - 'global_settings_url' => Pages_Manager::instance()->get_stable_url( 'jfb-settings' ), | |
| 496 | - 'jetEngineVersion' => Tools::get_jet_engine_version(), | |
| 497 | - 'actionConditionSettings' => $conditions_settings, | |
| 498 | - 'actionConditionExcludeEvents' => $exclude_events, | |
| 499 | - 'argumentsSource' => Form_Arguments::get_options(), | |
| 500 | - 'utmLinks' => array( | |
| 501 | - 'allProActions' => $utm->set_campaign( 'jetform-settings/pro-actions' )->add_query( $addons ), | |
| 502 | - 'limitResponses' => $utm->set_campaign( 'jetform-settings/limit-form-responses' )->add_query( $pricing ), | |
| 503 | - 'scheduleForm' => $utm->set_campaign( 'jetform-settings/schedule-forms' )->add_query( $pricing ), | |
| 504 | - 'userLoginPattern' => $utm->set_campaign( 'welcome-block/user-login' )->add_query( $addons . 'user-login/' ), | |
| 505 | - 'resetPassPattern' => $utm->set_campaign( 'welcome-block/reset-password' )->add_query( $addons . 'user-login/' ), | |
| 506 | - ), | |
| 507 | - 'isActivePro' => jet_form_builder()->addons_manager->is_active(), | |
| 508 | - 'assetsUrl' => jet_form_builder()->plugin_url( 'assets/' ), | |
| 509 | - ) | |
| 510 | - ) | |
| 511 | - ); | |
| 512 | - | |
| 513 | - do_action( 'jet-form-builder/editor-assets/after', $this, self::EDITOR_HANDLE ); | |
| 514 | - } | |
| 515 | - | |
| 516 | - private function get_help_for_repeaters() { | |
| 517 | - return array( | |
| 518 | - 'conditional_block' => array( | |
| 519 | - 'label' => __( 'With many conditions for the block, they are checked with the AND operator', 'jet-form-builder' ), | |
| 520 | - ), | |
| 521 | - 'conditional_block_or' => array( | |
| 522 | - 'label' => __( 'With many conditions for the block, they are checked with the OR operator', 'jet-form-builder' ), | |
| 523 | - ), | |
| 524 | - 'conditional_action' => array( | |
| 525 | - 'label' => __( 'With many conditions for the action, they are checked with the AND operator', 'jet-form-builder' ), | |
| 526 | - ), | |
| 527 | - 'conditional_action_or' => array( | |
| 528 | - 'label' => __( 'With many conditions for the action, they are checked with the OR operator', 'jet-form-builder' ), | |
| 529 | - ), | |
| 530 | - ); | |
| 531 | - } | |
| 532 | - | |
| 533 | - public function enqueue_form_assets() { | |
| 534 | - | |
| 535 | - $handle = 'jet-form-builder/form'; | |
| 536 | - | |
| 537 | - do_action( 'jet-form-builder/other-editor-assets/before', $this, $handle ); | |
| 538 | - | |
| 539 | - // todo: refactor with new way of assets loading | |
| 540 | - wp_register_script( | |
| 541 | - $handle, | |
| 542 | - Plugin::instance()->plugin_url( 'assets/build/editor/default.builder.js' ), | |
| 543 | - array( | |
| 544 | - 'wp-core-data', | |
| 545 | - 'wp-data', | |
| 546 | - 'wp-block-library', | |
| 547 | - 'wp-format-library', | |
| 548 | - 'wp-api-fetch', | |
| 549 | - ), | |
| 550 | - JET_FORM_BUILDER_VERSION, | |
| 551 | - true | |
| 552 | - ); | |
| 553 | - | |
| 554 | - wp_register_style( | |
| 555 | - 'jet-form-builder-others', | |
| 556 | - Plugin::instance()->plugin_url( 'assets/build/frontend/main.css' ), | |
| 557 | - array(), | |
| 558 | - Plugin::instance()->get_version() | |
| 559 | - ); | |
| 560 | - | |
| 561 | - do_action( 'jet-form-builder/other-editor-assets/after', $this, $handle ); | |
| 562 | - } | |
| 563 | - | |
| 564 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Jet_Form_Builder\Admin; | |
| 4 | + | |
| 5 | +use Jet_Form_Builder\Classes\Tools; | |
| 6 | +use Jet_Form_Builder\Gateways\Gateway_Manager; | |
| 7 | +use Jet_Form_Builder\Plugin; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Form editor class | |
| 11 | + * Thanks Tom J Nowell for initial editor idea and inspiration! | |
| 12 | + */ | |
| 13 | + | |
| 14 | +// If this file is called directly, abort. | |
| 15 | +if ( ! defined( 'WPINC' ) ) { | |
| 16 | + die; | |
| 17 | +} | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * Define Editor class | |
| 21 | + */ | |
| 22 | +class Editor { | |
| 23 | + | |
| 24 | + public static $index = 0; | |
| 25 | + | |
| 26 | + public $allowed_blocks = null; | |
| 27 | + public $action = null; | |
| 28 | + public $item_id = null; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Set up editor instatnce props | |
| 32 | + * | |
| 33 | + * @param array $config [description] | |
| 34 | + */ | |
| 35 | + public function __construct( $config = array() ) { | |
| 36 | + | |
| 37 | + if ( ! empty( $config['allowed_blocks'] ) ) { | |
| 38 | + $this->allowed_blocks = $config['allowed_blocks']; | |
| 39 | + } | |
| 40 | + | |
| 41 | + if ( ! empty( $config['action'] ) ) { | |
| 42 | + $this->action = $config['action']; | |
| 43 | + } | |
| 44 | + | |
| 45 | + if ( ! empty( $config['item_id'] ) ) { | |
| 46 | + $this->item_id = $config['item_id']; | |
| 47 | + } | |
| 48 | + | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * Returns action URL | |
| 53 | + * | |
| 54 | + * @return [type] [description] | |
| 55 | + */ | |
| 56 | + public function get_action() { | |
| 57 | + | |
| 58 | + if ( null === $this->action ) { | |
| 59 | + $this->action = apply_filters( 'jet-form-builder/editor/action', null ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + return $this->action; | |
| 63 | + | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Returns current item ID | |
| 68 | + * | |
| 69 | + * @return [type] [description] | |
| 70 | + */ | |
| 71 | + public function get_item_id() { | |
| 72 | + return $this->item_id; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Retuns allowed blocks list | |
| 77 | + * | |
| 78 | + * @return array | |
| 79 | + */ | |
| 80 | + public function get_allowed_blocks() { | |
| 81 | + | |
| 82 | + if ( empty( $this->allowed_blocks ) ) { | |
| 83 | + | |
| 84 | + $this->allowed_blocks = apply_filters( 'jet-form-builder/editor/allowed-blocks', array( | |
| 85 | + 'core/paragraph', | |
| 86 | + 'core/image', | |
| 87 | + 'core/heading', | |
| 88 | + 'core/verse', | |
| 89 | + 'core/freeform', | |
| 90 | + 'core/spacer', | |
| 91 | + 'core/subhead', | |
| 92 | + 'core/pullquote', | |
| 93 | + 'core/preformatted', | |
| 94 | + 'core/shortcode', | |
| 95 | + 'core/code', | |
| 96 | + 'core/quote', | |
| 97 | + 'core/list', | |
| 98 | + 'core/heading', | |
| 99 | + 'core/separator', | |
| 100 | + 'core/text-columns', | |
| 101 | + 'core/embed', | |
| 102 | + 'core-embed/youtube', | |
| 103 | + 'core-embed/twitter', | |
| 104 | + 'core-embed/vimeo', | |
| 105 | + 'core/columns', | |
| 106 | + 'core/column', | |
| 107 | + ) ); | |
| 108 | + | |
| 109 | + } | |
| 110 | + | |
| 111 | + return $this->allowed_blocks; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Returns taxonomies list for the config | |
| 116 | + * @return [type] [description] | |
| 117 | + */ | |
| 118 | + public function get_taxonomies_list() { | |
| 119 | + | |
| 120 | + $taxonomies = get_taxonomies( array(), 'objects' ); | |
| 121 | + | |
| 122 | + $result = array(); | |
| 123 | + | |
| 124 | + foreach ( $taxonomies as $tax ) { | |
| 125 | + $result[] = array( | |
| 126 | + 'value' => $tax->name, | |
| 127 | + 'label' => sprintf( '%1$s (%2$s)', $tax->label, $tax->name ), | |
| 128 | + ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + return $result; | |
| 132 | + | |
| 133 | + } | |
| 134 | + | |
| 135 | + public function get_preset_config() { | |
| 136 | + return apply_filters( 'jet-form-builder/editor/preset-config', array( | |
| 137 | + 'global_fields' => array( | |
| 138 | + array( | |
| 139 | + 'name' => 'from', | |
| 140 | + 'label' => __( 'Source:', 'jet-form-builder' ), | |
| 141 | + 'type' => 'select', | |
| 142 | + 'options' => Tools::with_placeholder( array( | |
| 143 | + array( | |
| 144 | + 'value' => 'post', | |
| 145 | + 'label' => __( 'Post', 'jet-form-builder' ), | |
| 146 | + ), | |
| 147 | + array( | |
| 148 | + 'value' => 'user', | |
| 149 | + 'label' => __( 'User', 'jet-form-builder' ), | |
| 150 | + ), | |
| 151 | + array( | |
| 152 | + 'value' => 'query_var', | |
| 153 | + 'label' => __( 'URL Query Variable', 'jet-form-builder' ), | |
| 154 | + ), | |
| 155 | + ) ), | |
| 156 | + ), | |
| 157 | + array( | |
| 158 | + 'name' => 'post_from', | |
| 159 | + 'label' => __( 'Get post ID from:', 'jet-form-builder' ), | |
| 160 | + 'type' => 'select', | |
| 161 | + 'options' => Tools::with_placeholder( array( | |
| 162 | + array( | |
| 163 | + 'value' => 'current_post', | |
| 164 | + 'label' => __( 'Current post', 'jet-form-builder' ), | |
| 165 | + ), | |
| 166 | + array( | |
| 167 | + 'value' => 'query_var', | |
| 168 | + 'label' => __( 'URL Query Variable', 'jet-form-builder' ), | |
| 169 | + ), | |
| 170 | + ) ), | |
| 171 | + 'condition' => array( | |
| 172 | + 'field' => 'from', | |
| 173 | + 'value' => 'post', | |
| 174 | + ), | |
| 175 | + ), | |
| 176 | + array( | |
| 177 | + 'name' => 'user_from', | |
| 178 | + 'label' => __( 'Get user ID from:', 'jet-form-builder' ), | |
| 179 | + 'type' => 'select', | |
| 180 | + 'options' => Tools::with_placeholder( array( | |
| 181 | + array( | |
| 182 | + 'value' => 'current_user', | |
| 183 | + 'label' => __( 'Current user', 'jet-form-builder' ), | |
| 184 | + ), | |
| 185 | + array( | |
| 186 | + 'value' => 'query_var', | |
| 187 | + 'label' => __( 'URL Query Variable', 'jet-form-builder' ), | |
| 188 | + ), | |
| 189 | + ) ), | |
| 190 | + 'condition' => array( | |
| 191 | + 'field' => 'from', | |
| 192 | + 'value' => 'user', | |
| 193 | + ), | |
| 194 | + ), | |
| 195 | + array( | |
| 196 | + 'name' => 'query_var', | |
| 197 | + 'label' => __( 'Query variable name:', 'jet-form-builder' ), | |
| 198 | + 'type' => 'text', | |
| 199 | + 'custom_condition' => 'query_var', | |
| 200 | + 'position' => 'dynamic' | |
| 201 | + ) | |
| 202 | + ), | |
| 203 | + 'map_fields' => array( | |
| 204 | + array( | |
| 205 | + 'name' => 'key', | |
| 206 | + 'label' => __( 'Query variable key', 'jet-form-builder' ), | |
| 207 | + 'type' => 'text', | |
| 208 | + 'position' => 'general', | |
| 209 | + 'parent_condition' => array( | |
| 210 | + 'field' => 'from', | |
| 211 | + 'value' => 'query_var' | |
| 212 | + ), | |
| 213 | + ), | |
| 214 | + array( | |
| 215 | + 'name' => 'prop', | |
| 216 | + 'label' => __( 'Post property', 'jet-form-builder' ), | |
| 217 | + 'type' => 'select', | |
| 218 | + 'options' => Tools::with_placeholder( array( | |
| 219 | + array( | |
| 220 | + 'value' => 'ID', | |
| 221 | + 'label' => __( 'Post ID', 'jet-form-builder' ), | |
| 222 | + ), | |
| 223 | + array( | |
| 224 | + 'value' => 'post_title', | |
| 225 | + 'label' => __( 'Post Title', 'jet-form-builder' ), | |
| 226 | + ), | |
| 227 | + array( | |
| 228 | + 'value' => 'post_content', | |
| 229 | + 'label' => __( 'Post Content', 'jet-form-builder' ), | |
| 230 | + ), | |
| 231 | + array( | |
| 232 | + 'value' => 'post_excerpt', | |
| 233 | + 'label' => __( 'Post Excerpt', 'jet-form-builder' ), | |
| 234 | + ), | |
| 235 | + array( | |
| 236 | + 'value' => 'post_date', | |
| 237 | + 'label' => __( 'Post Date', 'jet-form-builder' ) | |
| 238 | + ), | |
| 239 | + array( | |
| 240 | + 'value' => 'post_date_gmt', | |
| 241 | + 'label' => __( 'Post Date GMT', 'jet-form-builder' ) | |
| 242 | + ), | |
| 243 | + array( | |
| 244 | + 'value' => 'post_thumb', | |
| 245 | + 'label' => __( 'Post Thumbnail', 'jet-form-builder' ), | |
| 246 | + ), | |
| 247 | + array( | |
| 248 | + 'value' => 'post_meta', | |
| 249 | + 'label' => __( 'Post Meta', 'jet-form-builder' ), | |
| 250 | + ), | |
| 251 | + array( | |
| 252 | + 'value' => 'post_terms', | |
| 253 | + 'label' => __( 'Post Terms', 'jet-form-builder' ), | |
| 254 | + ), | |
| 255 | + ) ), | |
| 256 | + 'parent_condition' => array( | |
| 257 | + 'field' => 'from', | |
| 258 | + 'value' => 'post' | |
| 259 | + ), | |
| 260 | + ), | |
| 261 | + array( | |
| 262 | + 'name' => 'key', | |
| 263 | + 'label' => __( 'Taxonomy', 'jet-form-builder' ), | |
| 264 | + 'type' => 'select', | |
| 265 | + 'options' => Tools::with_placeholder( $this->get_taxonomies_list() ), | |
| 266 | + 'parent_condition' => array( | |
| 267 | + 'field' => 'from', | |
| 268 | + 'value' => 'post' | |
| 269 | + ), | |
| 270 | + 'condition' => array( | |
| 271 | + 'field' => 'prop', | |
| 272 | + 'value' => 'post_terms' | |
| 273 | + ), | |
| 274 | + ), | |
| 275 | + array( | |
| 276 | + 'name' => 'key', | |
| 277 | + 'label' => __( 'Meta field key', 'jet-form-builder' ), | |
| 278 | + 'type' => 'text', | |
| 279 | + 'parent_condition' => array( | |
| 280 | + 'field' => 'from', | |
| 281 | + 'value' => 'post' | |
| 282 | + ), | |
| 283 | + 'condition' => array( | |
| 284 | + 'field' => 'prop', | |
| 285 | + 'value' => 'post_meta' | |
| 286 | + ), | |
| 287 | + ), | |
| 288 | + array( | |
| 289 | + 'name' => 'prop', | |
| 290 | + 'label' => __( 'User field', 'jet-form-builder' ), | |
| 291 | + 'type' => 'select', | |
| 292 | + 'options' => Tools::with_placeholder( array( | |
| 293 | + array( | |
| 294 | + 'value' => 'ID', | |
| 295 | + 'label' => __( 'User ID', 'jet-form-builder' ), | |
| 296 | + ), | |
| 297 | + array( | |
| 298 | + 'value' => 'user_login', | |
| 299 | + 'label' => __( 'User Login', 'jet-form-builder' ), | |
| 300 | + ), | |
| 301 | + array( | |
| 302 | + 'value' => 'user_email', | |
| 303 | + 'label' => __( 'Email', 'jet-form-builder' ), | |
| 304 | + ), | |
| 305 | + array( | |
| 306 | + 'value' => 'password', | |
| 307 | + 'label' => __( 'Password', 'jet-form-builder' ), | |
| 308 | + ), | |
| 309 | + array( | |
| 310 | + 'value' => 'first_name', | |
| 311 | + 'label' => __( 'First Name', 'jet-form-builder' ), | |
| 312 | + ), | |
| 313 | + array( | |
| 314 | + 'value' => 'last_name', | |
| 315 | + 'label' => __( 'Last Name', 'jet-form-builder' ), | |
| 316 | + ), | |
| 317 | + array( | |
| 318 | + 'value' => 'user_url', | |
| 319 | + 'label' => __( 'User URL', 'jet-form-builder' ), | |
| 320 | + ), | |
| 321 | + array( | |
| 322 | + 'value' => 'user_meta', | |
| 323 | + 'label' => __( 'User Meta', 'jet-form-builder' ), | |
| 324 | + ), | |
| 325 | + ) ), | |
| 326 | + 'parent_condition' => array( | |
| 327 | + 'field' => 'from', | |
| 328 | + 'value' => 'user' | |
| 329 | + ), | |
| 330 | + ), | |
| 331 | + array( | |
| 332 | + 'name' => 'key', | |
| 333 | + 'label' => __( 'Meta field key', 'jet-form-builder' ), | |
| 334 | + 'type' => 'text', | |
| 335 | + 'parent_condition' => array( | |
| 336 | + 'field' => 'from', | |
| 337 | + 'value' => 'user' | |
| 338 | + ), | |
| 339 | + 'condition' => array( | |
| 340 | + 'field' => 'prop', | |
| 341 | + 'value' => 'user_meta' | |
| 342 | + ), | |
| 343 | + ), | |
| 344 | + ), | |
| 345 | + ) ); | |
| 346 | + } | |
| 347 | + | |
| 348 | + public function get_messages_default() { | |
| 349 | + return Plugin::instance()->post_type->get_messages_default(); | |
| 350 | + } | |
| 351 | + | |
| 352 | + public function get_recaptcha_labels() { | |
| 353 | + return array( | |
| 354 | + 'enabled' => __( 'Enable reCAPTCHA v3 form verification', 'jet-form-builder' ), | |
| 355 | + 'key' => __( 'Site Key:', 'jet-form-builder' ), | |
| 356 | + 'secret' => __( 'Secret Key:', 'jet-form-builder' ), | |
| 357 | + ); | |
| 358 | + } | |
| 359 | + | |
| 360 | + /** | |
| 361 | + * Enqueue editor assets | |
| 362 | + * | |
| 363 | + * @return void | |
| 364 | + */ | |
| 365 | + public function enqueue_assets() { | |
| 366 | + | |
| 367 | + $handle = 'jet-form-builder'; | |
| 368 | + | |
| 369 | + do_action( 'jet-form-builder/editor-assets/before', $this, $handle ); | |
| 370 | + | |
| 371 | + wp_enqueue_script( | |
| 372 | + $handle, | |
| 373 | + JET_FORM_BUILDER_URL . 'assets/js/editor.js', | |
| 374 | + array( | |
| 375 | + 'wp-editor', | |
| 376 | + 'wp-core-data', | |
| 377 | + 'wp-data', | |
| 378 | + 'wp-block-library', | |
| 379 | + 'wp-format-library', | |
| 380 | + 'wp-api-fetch', | |
| 381 | + ), | |
| 382 | + JET_FORM_BUILDER_VERSION, | |
| 383 | + true | |
| 384 | + ); | |
| 385 | + | |
| 386 | + wp_enqueue_style( | |
| 387 | + $handle, | |
| 388 | + JET_FORM_BUILDER_URL . 'assets/css/editor.css', | |
| 389 | + array( | |
| 390 | + 'media', | |
| 391 | + 'l10n', | |
| 392 | + 'buttons', | |
| 393 | + 'wp-edit-blocks', | |
| 394 | + 'wp-editor', | |
| 395 | + ), | |
| 396 | + JET_FORM_BUILDER_VERSION, | |
| 397 | + 'all' | |
| 398 | + ); | |
| 399 | + | |
| 400 | + | |
| 401 | + wp_localize_script( $handle, 'JetFormEditorData', array( | |
| 402 | + 'allowedBlocks' => $this->get_allowed_blocks(), | |
| 403 | + 'action' => $this->get_action(), | |
| 404 | + 'itemID' => $this->get_item_id(), | |
| 405 | + 'presetConfig' => $this->get_preset_config(), | |
| 406 | + 'messagesDefault' => $this->get_messages_default(), | |
| 407 | + 'recaptchaLabels' => $this->get_recaptcha_labels(), | |
| 408 | + 'gateways' => Gateway_Manager::instance()->editor_data(), | |
| 409 | + 'helpForRepeaters' => $this->get_help_for_repeaters(), | |
| 410 | + ) ); | |
| 411 | + | |
| 412 | + do_action( 'jet-form-builder/editor-assets/after', $this, $handle ); | |
| 413 | + } | |
| 414 | + | |
| 415 | + private function get_help_for_repeaters() { | |
| 416 | + return array( | |
| 417 | + 'conditional_block' => array( | |
| 418 | + 'label' => __( 'With many conditions for the block, they are checked with the AND operator', 'jet-form-builder' ) | |
| 419 | + ), | |
| 420 | + 'conditional_action' => array( | |
| 421 | + 'label' => __( 'With many conditions for the action, they are checked with the AND operator', 'jet-form-builder' ) | |
| 422 | + ), | |
| 423 | + ); | |
| 424 | + } | |
| 425 | + | |
| 426 | + public function enqueue_form_assets() { | |
| 427 | + | |
| 428 | + $handle = 'jet-form-builder/form'; | |
| 429 | + | |
| 430 | + do_action( 'jet-form-builder/other-editor-assets/before', $this, $handle ); | |
| 431 | + | |
| 432 | + wp_enqueue_script( | |
| 433 | + $handle, | |
| 434 | + JET_FORM_BUILDER_URL . 'assets/js/form-block.js', | |
| 435 | + array( | |
| 436 | + 'wp-editor', | |
| 437 | + 'wp-core-data', | |
| 438 | + 'wp-data', | |
| 439 | + 'wp-block-library', | |
| 440 | + 'wp-format-library', | |
| 441 | + 'wp-api-fetch', | |
| 442 | + ), | |
| 443 | + JET_FORM_BUILDER_VERSION, | |
| 444 | + true | |
| 445 | + ); | |
| 446 | + | |
| 447 | + wp_enqueue_style( | |
| 448 | + 'jet-form-builder-others', | |
| 449 | + Plugin::instance()->plugin_url( 'assets/css/frontend.css' ), | |
| 450 | + array(), | |
| 451 | + Plugin::instance()->get_version() | |
| 452 | + ); | |
| 453 | + | |
| 454 | + wp_localize_script( $handle, 'JetFormEditorData', array( | |
| 455 | + 'allowedBlocks' => $this->get_allowed_blocks(), | |
| 456 | + 'action' => $this->get_action(), | |
| 457 | + 'itemID' => $this->get_item_id(), | |
| 458 | + 'presetConfig' => $this->get_preset_config(), | |
| 459 | + ) ); | |
| 460 | + | |
| 461 | + do_action( 'jet-form-builder/other-editor-assets/after', $this, $handle ); | |
| 462 | + | |
| 463 | + } | |
| 464 | + | |
| 465 | + /** | |
| 466 | + * Render new editor instance | |
| 467 | + * | |
| 468 | + * @return [type] [description] | |
| 469 | + */ | |
| 470 | + public function render( $input_name = '', $content = '', $form_name = '' ) { | |
| 471 | + | |
| 472 | + if ( ! $input_name ) { | |
| 473 | + $input_name = 'jet_form_editor_' . self::$index; | |
| 474 | + self::$index ++; | |
| 475 | + } | |
| 476 | + | |
| 477 | + ?> | |
| 478 | + <input name="<?php echo esc_attr( $input_name ); ?>" id="<?php echo esc_attr( $input_name ); ?>" type="hidden"/> | |
| 479 | + <script> | |
| 480 | + document.addEventListener( 'jet-form-builder-initialized', function ( event ) { | |
| 481 | + window.JetFormEditor( | |
| 482 | + '<?php echo esc_js( $input_name ); ?>', | |
| 483 | + '<?php echo esc_js( $input_name ); ?>', | |
| 484 | + '<?php echo html_entity_decode( esc_js( $content ) ); ?>', | |
| 485 | + '<?php echo esc_js( $form_name ); ?>' | |
| 486 | + ); | |
| 487 | + } ); | |
| 488 | + </script> | |
| 489 | + <?php | |
| 490 | + } | |
| 491 | + | |
| 492 | +} | |