editor.php
500 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Admin; |
| 4 | |
| 5 | use Jet_Form_Builder\Gateways\Gateway_Manager; |
| 6 | use Jet_Form_Builder\Plugin; |
| 7 | |
| 8 | /** |
| 9 | * Form editor class |
| 10 | * Thanks Tom J Nowell for initial editor idea and inspiration! |
| 11 | */ |
| 12 | |
| 13 | // If this file is called directly, abort. |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Define Editor class |
| 20 | */ |
| 21 | class Editor { |
| 22 | |
| 23 | public static $index = 0; |
| 24 | |
| 25 | public $allowed_blocks = null; |
| 26 | public $action = null; |
| 27 | public $item_id = null; |
| 28 | |
| 29 | /** |
| 30 | * Set up editor instatnce props |
| 31 | * |
| 32 | * @param array $config [description] |
| 33 | */ |
| 34 | public function __construct( $config = array() ) { |
| 35 | |
| 36 | if ( ! empty( $config['allowed_blocks'] ) ) { |
| 37 | $this->allowed_blocks = $config['allowed_blocks']; |
| 38 | } |
| 39 | |
| 40 | if ( ! empty( $config['action'] ) ) { |
| 41 | $this->action = $config['action']; |
| 42 | } |
| 43 | |
| 44 | if ( ! empty( $config['item_id'] ) ) { |
| 45 | $this->item_id = $config['item_id']; |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns action URL |
| 52 | * |
| 53 | * @return [type] [description] |
| 54 | */ |
| 55 | public function get_action() { |
| 56 | |
| 57 | if ( null === $this->action ) { |
| 58 | $this->action = apply_filters( 'jet-form-builder/editor/action', null ); |
| 59 | } |
| 60 | |
| 61 | return $this->action; |
| 62 | |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns current item ID |
| 67 | * |
| 68 | * @return [type] [description] |
| 69 | */ |
| 70 | public function get_item_id() { |
| 71 | return $this->item_id; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Retuns allowed blocks list |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | public function get_allowed_blocks() { |
| 80 | |
| 81 | if ( empty( $this->allowed_blocks ) ) { |
| 82 | |
| 83 | $this->allowed_blocks = apply_filters( 'jet-form-builder/editor/allowed-blocks', array( |
| 84 | 'core/paragraph', |
| 85 | 'core/image', |
| 86 | 'core/heading', |
| 87 | 'core/verse', |
| 88 | 'core/freeform', |
| 89 | 'core/spacer', |
| 90 | 'core/subhead', |
| 91 | 'core/pullquote', |
| 92 | 'core/preformatted', |
| 93 | 'core/shortcode', |
| 94 | 'core/code', |
| 95 | 'core/quote', |
| 96 | 'core/list', |
| 97 | 'core/heading', |
| 98 | 'core/separator', |
| 99 | 'core/text-columns', |
| 100 | 'core/embed', |
| 101 | 'core-embed/youtube', |
| 102 | 'core-embed/twitter', |
| 103 | 'core-embed/vimeo', |
| 104 | 'core/columns', |
| 105 | 'core/column', |
| 106 | ) ); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | return $this->allowed_blocks; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns taxonomies list for the config |
| 115 | * @return [type] [description] |
| 116 | */ |
| 117 | public function get_taxonomies_list() { |
| 118 | |
| 119 | $taxonomies = get_taxonomies( array(), 'objects' ); |
| 120 | |
| 121 | $result = array(); |
| 122 | |
| 123 | foreach ( $taxonomies as $tax ) { |
| 124 | $result[] = array( |
| 125 | 'value' => $tax->name, |
| 126 | 'label' => sprintf( '%1$s (%2$s)', $tax->label, $tax->name ), |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | return $result; |
| 131 | |
| 132 | } |
| 133 | |
| 134 | public function get_preset_config() { |
| 135 | return apply_filters( 'jet-form-builder/editor/preset-config', array( |
| 136 | 'global_fields' => array( |
| 137 | array( |
| 138 | 'name' => 'from', |
| 139 | 'label' => __( 'Source:', 'jet-form-builder' ), |
| 140 | 'type' => 'select', |
| 141 | 'options' => array( |
| 142 | array( |
| 143 | 'value' => '', |
| 144 | 'label' => __( 'Select...', 'jet-form-builder' ), |
| 145 | ), |
| 146 | array( |
| 147 | 'value' => 'post', |
| 148 | 'label' => __( 'Post', 'jet-form-builder' ), |
| 149 | ), |
| 150 | array( |
| 151 | 'value' => 'user', |
| 152 | 'label' => __( 'User', 'jet-form-builder' ), |
| 153 | ), |
| 154 | ) |
| 155 | ), |
| 156 | array( |
| 157 | 'name' => 'post_from', |
| 158 | 'label' => __( 'Get post ID from:', 'jet-form-builder' ), |
| 159 | 'type' => 'select', |
| 160 | 'options' => array( |
| 161 | array( |
| 162 | 'value' => 'current_post', |
| 163 | 'label' => __( 'Current post', 'jet-form-builder' ), |
| 164 | ), |
| 165 | array( |
| 166 | 'value' => 'query_var', |
| 167 | 'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 168 | ), |
| 169 | ), |
| 170 | 'condition' => array( |
| 171 | 'field' => 'from', |
| 172 | 'value' => 'post', |
| 173 | ), |
| 174 | ), |
| 175 | array( |
| 176 | 'name' => 'user_from', |
| 177 | 'label' => __( 'Get user ID from:', 'jet-form-builder' ), |
| 178 | 'type' => 'select', |
| 179 | 'options' => array( |
| 180 | array( |
| 181 | 'value' => 'current_user', |
| 182 | 'label' => __( 'Current user', 'jet-form-builder' ), |
| 183 | ), |
| 184 | array( |
| 185 | 'value' => 'query_var', |
| 186 | 'label' => __( 'URL Query Variable', 'jet-form-builder' ), |
| 187 | ), |
| 188 | ), |
| 189 | 'condition' => array( |
| 190 | 'field' => 'from', |
| 191 | 'value' => 'user', |
| 192 | ), |
| 193 | ), |
| 194 | array( |
| 195 | 'name' => 'query_var', |
| 196 | 'label' => __( 'Query variable name:', 'jet-form-builder' ), |
| 197 | 'type' => 'text', |
| 198 | 'custom_condition' => 'query_var', |
| 199 | ) |
| 200 | ), |
| 201 | 'map_fields' => array( |
| 202 | array( |
| 203 | 'name' => 'key', |
| 204 | 'label' => __( 'Query variable key', 'jet-form-builder' ), |
| 205 | 'type' => 'text', |
| 206 | 'parent_condition' => array( |
| 207 | 'field' => 'from', |
| 208 | 'value' => 'query_vars' |
| 209 | ), |
| 210 | ), |
| 211 | array( |
| 212 | 'name' => 'prop', |
| 213 | 'label' => __( 'Post property', 'jet-form-builder' ), |
| 214 | 'type' => 'select', |
| 215 | 'options' => array( |
| 216 | array( |
| 217 | 'value' => '', |
| 218 | 'label' => __( '--', 'jet-form-builder' ), |
| 219 | ), |
| 220 | array( |
| 221 | 'value' => 'ID', |
| 222 | 'label' => __( 'Post ID', 'jet-form-builder' ), |
| 223 | ), |
| 224 | array( |
| 225 | 'value' => 'post_title', |
| 226 | 'label' => __( 'Post Title', 'jet-form-builder' ), |
| 227 | ), |
| 228 | array( |
| 229 | 'value' => 'post_content', |
| 230 | 'label' => __( 'Post Content', 'jet-form-builder' ), |
| 231 | ), |
| 232 | array( |
| 233 | 'value' => 'post_excerpt', |
| 234 | 'label' => __( 'Post Excerpt', 'jet-form-builder' ), |
| 235 | ), |
| 236 | array( |
| 237 | 'value' => 'post_thumb', |
| 238 | 'label' => __( 'Post Thumbnail', 'jet-form-builder' ), |
| 239 | ), |
| 240 | array( |
| 241 | 'value' => 'post_meta', |
| 242 | 'label' => __( 'Post Meta', 'jet-form-builder' ), |
| 243 | ), |
| 244 | array( |
| 245 | 'value' => 'post_terms', |
| 246 | 'label' => __( 'Post Terms', 'jet-form-builder' ), |
| 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' => array_merge( array( |
| 259 | array( |
| 260 | 'value' => '', |
| 261 | 'label' => __( 'Select taxonomy...', 'jet-form-builder' ), |
| 262 | ) |
| 263 | ), $this->get_taxonomies_list() ), |
| 264 | 'parent_condition' => array( |
| 265 | 'field' => 'from', |
| 266 | 'value' => 'post' |
| 267 | ), |
| 268 | 'condition' => array( |
| 269 | 'field' => 'prop', |
| 270 | 'value' => 'post_terms' |
| 271 | ), |
| 272 | ), |
| 273 | array( |
| 274 | 'name' => 'key', |
| 275 | 'label' => __( 'Meta field key', 'jet-form-builder' ), |
| 276 | 'type' => 'text', |
| 277 | 'parent_condition' => array( |
| 278 | 'field' => 'from', |
| 279 | 'value' => 'post' |
| 280 | ), |
| 281 | 'condition' => array( |
| 282 | 'field' => 'prop', |
| 283 | 'value' => 'post_meta' |
| 284 | ), |
| 285 | ), |
| 286 | array( |
| 287 | 'name' => 'prop', |
| 288 | 'label' => __( 'User field', 'jet-form-builder' ), |
| 289 | 'type' => 'select', |
| 290 | 'options' => array( |
| 291 | array( |
| 292 | 'value' => '', |
| 293 | 'label' => __( '--', 'jet-form-builder' ), |
| 294 | ), |
| 295 | array( |
| 296 | 'value' => 'ID', |
| 297 | 'label' => __( 'User ID', 'jet-form-builder' ), |
| 298 | ), |
| 299 | array( |
| 300 | 'value' => 'login', |
| 301 | 'label' => __( 'User Login', 'jet-form-builder' ), |
| 302 | ), |
| 303 | array( |
| 304 | 'value' => 'email', |
| 305 | 'label' => __( 'Email', 'jet-form-builder' ), |
| 306 | ), |
| 307 | array( |
| 308 | 'value' => 'password', |
| 309 | 'label' => __( 'Password', 'jet-form-builder' ), |
| 310 | ), |
| 311 | array( |
| 312 | 'value' => 'first_name', |
| 313 | 'label' => __( 'First Name', 'jet-form-builder' ), |
| 314 | ), |
| 315 | array( |
| 316 | 'value' => 'last_name', |
| 317 | 'label' => __( 'Last Name', 'jet-form-builder' ), |
| 318 | ), |
| 319 | array( |
| 320 | 'value' => 'user_url', |
| 321 | 'label' => __( 'User URL', 'jet-form-builder' ), |
| 322 | ), |
| 323 | array( |
| 324 | 'value' => 'user_meta', |
| 325 | 'label' => __( 'User Meta', 'jet-form-builder' ), |
| 326 | ), |
| 327 | ), |
| 328 | 'parent_condition' => array( |
| 329 | 'field' => 'from', |
| 330 | 'value' => 'user' |
| 331 | ), |
| 332 | ), |
| 333 | array( |
| 334 | 'name' => 'key', |
| 335 | 'label' => __( 'Meta field key', 'jet-form-builder' ), |
| 336 | 'type' => 'text', |
| 337 | 'parent_condition' => array( |
| 338 | 'field' => 'from', |
| 339 | 'value' => 'user' |
| 340 | ), |
| 341 | 'condition' => array( |
| 342 | 'field' => 'prop', |
| 343 | 'value' => 'user_meta' |
| 344 | ), |
| 345 | ), |
| 346 | ), |
| 347 | ) ); |
| 348 | } |
| 349 | |
| 350 | public function get_messages_default() { |
| 351 | return Plugin::instance()->post_type->get_messages_default(); |
| 352 | } |
| 353 | |
| 354 | public function get_recaptcha_labels() { |
| 355 | return array( |
| 356 | 'enabled' => __( 'Enable reCAPTCHA v3 form verification', 'jet-form-builder' ), |
| 357 | 'key' => __( 'Site Key:', 'jet-form-builder' ), |
| 358 | 'secret' => __( 'Secret Key:', 'jet-form-builder' ), |
| 359 | ); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Enqueue editor assets |
| 364 | * |
| 365 | * @return void |
| 366 | */ |
| 367 | public function enqueue_assets() { |
| 368 | |
| 369 | $handle = 'jet-form-builder'; |
| 370 | |
| 371 | do_action( 'jet-form-builder/editor-assets/before', $this, $handle ); |
| 372 | |
| 373 | wp_enqueue_script( |
| 374 | $handle, |
| 375 | JET_FORM_BUILDER_URL . 'assets/js/editor.js', |
| 376 | array( |
| 377 | 'wp-editor', |
| 378 | 'wp-core-data', |
| 379 | 'wp-data', |
| 380 | 'wp-block-library', |
| 381 | 'wp-format-library', |
| 382 | 'wp-api-fetch', |
| 383 | ), |
| 384 | JET_FORM_BUILDER_VERSION, |
| 385 | true |
| 386 | ); |
| 387 | |
| 388 | wp_enqueue_style( |
| 389 | $handle, |
| 390 | JET_FORM_BUILDER_URL . 'assets/css/editor.css', |
| 391 | array( |
| 392 | 'media', |
| 393 | 'l10n', |
| 394 | 'buttons', |
| 395 | 'wp-edit-blocks', |
| 396 | 'wp-editor', |
| 397 | ), |
| 398 | JET_FORM_BUILDER_VERSION, |
| 399 | 'all' |
| 400 | ); |
| 401 | |
| 402 | |
| 403 | wp_localize_script( $handle, 'JetFormEditorData', array( |
| 404 | 'allowedBlocks' => $this->get_allowed_blocks(), |
| 405 | 'action' => $this->get_action(), |
| 406 | 'itemID' => $this->get_item_id(), |
| 407 | 'presetConfig' => $this->get_preset_config(), |
| 408 | 'messagesDefault' => $this->get_messages_default(), |
| 409 | 'recaptchaLabels' => $this->get_recaptcha_labels(), |
| 410 | 'gateways' => $this->get_gateways_data(), |
| 411 | ) ); |
| 412 | |
| 413 | do_action( 'jet-form-builder/editor-assets/after', $this, $handle ); |
| 414 | } |
| 415 | |
| 416 | private function get_gateways_data() { |
| 417 | $result = array( |
| 418 | 'allowed' => Plugin::instance()->post_type->allow_gateways |
| 419 | ); |
| 420 | |
| 421 | if ( $result['allowed'] ) { |
| 422 | |
| 423 | $result['list'] = Gateway_Manager::instance()->get_gateways_for_js(); |
| 424 | $result['messages'] = array( |
| 425 | 'success' => 'Payment success message', |
| 426 | 'failed' => 'Payment failed message', |
| 427 | ); |
| 428 | } |
| 429 | |
| 430 | return $result; |
| 431 | } |
| 432 | |
| 433 | public function enqueue_form_assets() { |
| 434 | |
| 435 | $handle = 'jet-form-builder/form'; |
| 436 | |
| 437 | do_action( 'jet-form-builder/other-editor-assets/before', $this, $handle ); |
| 438 | |
| 439 | wp_enqueue_script( |
| 440 | $handle, |
| 441 | JET_FORM_BUILDER_URL . 'assets/js/form-block.js', |
| 442 | array( |
| 443 | 'wp-editor', |
| 444 | 'wp-core-data', |
| 445 | 'wp-data', |
| 446 | 'wp-block-library', |
| 447 | 'wp-format-library', |
| 448 | 'wp-api-fetch', |
| 449 | ), |
| 450 | JET_FORM_BUILDER_VERSION, |
| 451 | true |
| 452 | ); |
| 453 | |
| 454 | wp_enqueue_style( |
| 455 | 'jet-form-builder-others', |
| 456 | Plugin::instance()->plugin_url( 'assets/css/frontend.css' ), |
| 457 | array(), |
| 458 | Plugin::instance()->get_version() |
| 459 | ); |
| 460 | |
| 461 | wp_localize_script( $handle, 'JetFormEditorData', array( |
| 462 | 'allowedBlocks' => $this->get_allowed_blocks(), |
| 463 | 'action' => $this->get_action(), |
| 464 | 'itemID' => $this->get_item_id(), |
| 465 | 'presetConfig' => $this->get_preset_config(), |
| 466 | ) ); |
| 467 | |
| 468 | do_action( 'jet-form-builder/other-editor-assets/after', $this, $handle ); |
| 469 | |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Render new editor instance |
| 474 | * |
| 475 | * @return [type] [description] |
| 476 | */ |
| 477 | public function render( $input_name = '', $content = '', $form_name = '' ) { |
| 478 | |
| 479 | if ( ! $input_name ) { |
| 480 | $input_name = 'jet_form_editor_' . self::$index; |
| 481 | self::$index ++; |
| 482 | } |
| 483 | |
| 484 | ?> |
| 485 | <input name="<?php echo esc_attr( $input_name ); ?>" id="<?php echo esc_attr( $input_name ); ?>" type="hidden"/> |
| 486 | <script> |
| 487 | document.addEventListener( 'jet-form-builder-initialized', function ( event ) { |
| 488 | window.JetFormEditor( |
| 489 | '<?php echo esc_js( $input_name ); ?>', |
| 490 | '<?php echo esc_js( $input_name ); ?>', |
| 491 | '<?php echo html_entity_decode( esc_js( $content ) ); ?>', |
| 492 | '<?php echo esc_js( $form_name ); ?>' |
| 493 | ); |
| 494 | } ); |
| 495 | </script> |
| 496 | <?php |
| 497 | } |
| 498 | |
| 499 | } |
| 500 |