PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.2.0
JetFormBuilder — Dynamic Blocks Form Builder v1.2.0
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / admin / editor.php
jetformbuilder / includes / admin Last commit date
pages 5 years ago tabs-handlers 5 years ago editor.php 5 years ago
editor.php
499 lines
1 <?php
2
3 namespace Jet_Form_Builder\Admin;
4
5 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
6 use Jet_Form_Builder\Classes\Tools;
7 use Jet_Form_Builder\Gateways\Gateway_Manager;
8 use Jet_Form_Builder\Plugin;
9
10 /**
11 * Form editor class
12 * Thanks Tom J Nowell for initial editor idea and inspiration!
13 */
14
15 // If this file is called directly, abort.
16 if ( ! defined( 'WPINC' ) ) {
17 die;
18 }
19
20 /**
21 * Define Editor class
22 */
23 class Editor {
24
25 const EDITOR_HANDLE = 'jet-form-builder-editor';
26 const EDITOR_PACKAGE_HANDLE = 'jet-form-builder-editor-package';
27
28 public static $index = 0;
29
30 public $allowed_blocks = null;
31 public $action = null;
32 public $item_id = null;
33
34 /**
35 * Set up editor instatnce props
36 *
37 * @param array $config [description]
38 */
39 public function __construct( $config = array() ) {
40
41 if ( ! empty( $config['allowed_blocks'] ) ) {
42 $this->allowed_blocks = $config['allowed_blocks'];
43 }
44
45 if ( ! empty( $config['action'] ) ) {
46 $this->action = $config['action'];
47 }
48
49 if ( ! empty( $config['item_id'] ) ) {
50 $this->item_id = $config['item_id'];
51 }
52
53 }
54
55 /**
56 * Returns action URL
57 *
58 * @return [type] [description]
59 */
60 public function get_action() {
61
62 if ( null === $this->action ) {
63 $this->action = apply_filters( 'jet-form-builder/editor/action', null );
64 }
65
66 return $this->action;
67
68 }
69
70 /**
71 * Returns current item ID
72 *
73 * @return [type] [description]
74 */
75 public function get_item_id() {
76 return $this->item_id;
77 }
78
79 /**
80 * Retuns allowed blocks list
81 *
82 * @return array
83 */
84 public function get_allowed_blocks() {
85
86 if ( empty( $this->allowed_blocks ) ) {
87
88 $this->allowed_blocks = apply_filters( 'jet-form-builder/editor/allowed-blocks', array(
89 'core/paragraph',
90 'core/image',
91 'core/heading',
92 'core/verse',
93 'core/freeform',
94 'core/spacer',
95 'core/subhead',
96 'core/pullquote',
97 'core/preformatted',
98 'core/shortcode',
99 'core/code',
100 'core/quote',
101 'core/list',
102 'core/heading',
103 'core/separator',
104 'core/text-columns',
105 'core/embed',
106 'core-embed/youtube',
107 'core-embed/twitter',
108 'core-embed/vimeo',
109 'core/columns',
110 'core/column',
111 ) );
112
113 }
114
115 return $this->allowed_blocks;
116 }
117
118 /**
119 * Returns taxonomies list for the config
120 * @return [type] [description]
121 */
122 public function get_taxonomies_list() {
123
124 $taxonomies = get_taxonomies( array(), 'objects' );
125
126 $result = array();
127
128 foreach ( $taxonomies as $tax ) {
129 $result[] = array(
130 'value' => $tax->name,
131 'label' => sprintf( '%1$s (%2$s)', $tax->label, $tax->name ),
132 );
133 }
134
135 return $result;
136
137 }
138
139 public function get_preset_config() {
140 return apply_filters( 'jet-form-builder/editor/preset-config', array(
141 'global_fields' => array(
142 array(
143 'name' => 'from',
144 'label' => __( 'Source:', 'jet-form-builder' ),
145 'type' => 'select',
146 'options' => Tools::with_placeholder( array(
147 array(
148 'value' => 'post',
149 'label' => __( 'Post', 'jet-form-builder' ),
150 ),
151 array(
152 'value' => 'user',
153 'label' => __( 'User', 'jet-form-builder' ),
154 ),
155 array(
156 'value' => 'query_var',
157 'label' => __( 'URL Query Variable', 'jet-form-builder' ),
158 ),
159 ) ),
160 ),
161 array(
162 'name' => 'post_from',
163 'label' => __( 'Get post ID from:', 'jet-form-builder' ),
164 'type' => 'select',
165 'options' => Tools::with_placeholder( array(
166 array(
167 'value' => 'current_post',
168 'label' => __( 'Current post', 'jet-form-builder' ),
169 ),
170 array(
171 'value' => 'query_var',
172 'label' => __( 'URL Query Variable', 'jet-form-builder' ),
173 ),
174 ) ),
175 'condition' => array(
176 'field' => 'from',
177 'value' => 'post',
178 ),
179 ),
180 array(
181 'name' => 'user_from',
182 'label' => __( 'Get user ID from:', 'jet-form-builder' ),
183 'type' => 'select',
184 'options' => Tools::with_placeholder( array(
185 array(
186 'value' => 'current_user',
187 'label' => __( 'Current user', 'jet-form-builder' ),
188 ),
189 array(
190 'value' => 'query_var',
191 'label' => __( 'URL Query Variable', 'jet-form-builder' ),
192 ),
193 ) ),
194 'condition' => array(
195 'field' => 'from',
196 'value' => 'user',
197 ),
198 ),
199 array(
200 'name' => 'query_var',
201 'label' => __( 'Query variable name:', 'jet-form-builder' ),
202 'type' => 'text',
203 'custom_condition' => 'query_var',
204 'position' => 'dynamic'
205 )
206 ),
207 'map_fields' => array(
208 array(
209 'name' => 'key',
210 'label' => __( 'Query variable key', 'jet-form-builder' ),
211 'type' => 'text',
212 'position' => 'general',
213 'parent_condition' => array(
214 'field' => 'from',
215 'value' => 'query_var'
216 ),
217 ),
218 array(
219 'name' => 'prop',
220 'label' => __( 'Post property', 'jet-form-builder' ),
221 'type' => 'select',
222 'options' => Tools::with_placeholder( array(
223 array(
224 'value' => 'ID',
225 'label' => __( 'Post ID', 'jet-form-builder' ),
226 ),
227 array(
228 'value' => 'post_title',
229 'label' => __( 'Post Title', 'jet-form-builder' ),
230 ),
231 array(
232 'value' => 'post_content',
233 'label' => __( 'Post Content', 'jet-form-builder' ),
234 ),
235 array(
236 'value' => 'post_excerpt',
237 'label' => __( 'Post Excerpt', 'jet-form-builder' ),
238 ),
239 array(
240 'value' => 'post_date',
241 'label' => __( 'Post Date', 'jet-form-builder' )
242 ),
243 array(
244 'value' => 'post_date_gmt',
245 'label' => __( 'Post Date GMT', 'jet-form-builder' )
246 ),
247 array(
248 'value' => 'post_thumb',
249 'label' => __( 'Post Thumbnail', 'jet-form-builder' ),
250 ),
251 array(
252 'value' => 'post_meta',
253 'label' => __( 'Post Meta', 'jet-form-builder' ),
254 ),
255 array(
256 'value' => 'post_terms',
257 'label' => __( 'Post Terms', 'jet-form-builder' ),
258 ),
259 ) ),
260 'parent_condition' => array(
261 'field' => 'from',
262 'value' => 'post'
263 ),
264 ),
265 array(
266 'name' => 'key',
267 'label' => __( 'Taxonomy', 'jet-form-builder' ),
268 'type' => 'select',
269 'options' => Tools::with_placeholder( $this->get_taxonomies_list() ),
270 'parent_condition' => array(
271 'field' => 'from',
272 'value' => 'post'
273 ),
274 'condition' => array(
275 'field' => 'prop',
276 'value' => 'post_terms'
277 ),
278 ),
279 array(
280 'name' => 'key',
281 'label' => __( 'Meta field key', 'jet-form-builder' ),
282 'type' => 'text',
283 'parent_condition' => array(
284 'field' => 'from',
285 'value' => 'post'
286 ),
287 'condition' => array(
288 'field' => 'prop',
289 'value' => 'post_meta'
290 ),
291 ),
292 array(
293 'name' => 'prop',
294 'label' => __( 'User field', 'jet-form-builder' ),
295 'type' => 'select',
296 'options' => Tools::with_placeholder( array(
297 array(
298 'value' => 'ID',
299 'label' => __( 'User ID', 'jet-form-builder' ),
300 ),
301 array(
302 'value' => 'user_login',
303 'label' => __( 'User Login', 'jet-form-builder' ),
304 ),
305 array(
306 'value' => 'user_email',
307 'label' => __( 'Email', 'jet-form-builder' ),
308 ),
309 array(
310 'value' => 'password',
311 'label' => __( 'Password', 'jet-form-builder' ),
312 ),
313 array(
314 'value' => 'first_name',
315 'label' => __( 'First Name', 'jet-form-builder' ),
316 ),
317 array(
318 'value' => 'last_name',
319 'label' => __( 'Last Name', 'jet-form-builder' ),
320 ),
321 array(
322 'value' => 'user_url',
323 'label' => __( 'User URL', 'jet-form-builder' ),
324 ),
325 array(
326 'value' => 'user_meta',
327 'label' => __( 'User Meta', 'jet-form-builder' ),
328 ),
329 ) ),
330 'parent_condition' => array(
331 'field' => 'from',
332 'value' => 'user'
333 ),
334 ),
335 array(
336 'name' => 'key',
337 'label' => __( 'Meta field key', 'jet-form-builder' ),
338 'type' => 'text',
339 'parent_condition' => array(
340 'field' => 'from',
341 'value' => 'user'
342 ),
343 'condition' => array(
344 'field' => 'prop',
345 'value' => 'user_meta'
346 ),
347 ),
348 ),
349 ) );
350 }
351
352 public function get_messages_default() {
353 return Plugin::instance()->post_type->get_messages_default();
354 }
355
356 /**
357 * Enqueue editor assets
358 *
359 * @return void
360 */
361 public function enqueue_assets() {
362 do_action( 'jet-form-builder/editor-package/before', $this, self::EDITOR_PACKAGE_HANDLE );
363
364 wp_enqueue_script(
365 self::EDITOR_PACKAGE_HANDLE,
366 JET_FORM_BUILDER_URL . 'assets/js/package.js',
367 array(
368 'wp-editor',
369 'wp-core-data',
370 'wp-data',
371 'wp-block-library',
372 'wp-format-library',
373 'wp-api-fetch',
374 ),
375 JET_FORM_BUILDER_VERSION,
376 true
377 );
378
379 do_action( 'jet-form-builder/editor-assets/before', $this, self::EDITOR_HANDLE );
380
381 wp_enqueue_script(
382 self::EDITOR_HANDLE,
383 JET_FORM_BUILDER_URL . 'assets/js/editor.js',
384 array(),
385 JET_FORM_BUILDER_VERSION,
386 true
387 );
388
389 wp_enqueue_style(
390 self::EDITOR_HANDLE,
391 JET_FORM_BUILDER_URL . 'assets/css/editor.css',
392 array(
393 'media',
394 'l10n',
395 'buttons',
396 'wp-edit-blocks',
397 'wp-editor',
398 ),
399 JET_FORM_BUILDER_VERSION,
400 'all'
401 );
402
403 wp_localize_script(
404 self::EDITOR_PACKAGE_HANDLE,
405 'JetFormEditorData',
406 array(
407 'allowedBlocks' => $this->get_allowed_blocks(),
408 'action' => $this->get_action(),
409 'itemID' => $this->get_item_id(),
410 'presetConfig' => $this->get_preset_config(),
411 'messagesDefault' => $this->get_messages_default(),
412 'gateways' => Gateway_Manager::instance()->editor_data(),
413 'helpForRepeaters' => $this->get_help_for_repeaters(),
414 'global_settings' => Tab_Handler_Manager::instance()->all()
415 )
416 );
417
418 do_action( 'jet-form-builder/editor-assets/after', $this, self::EDITOR_HANDLE );
419 }
420
421 private function get_help_for_repeaters() {
422 return array(
423 'conditional_block' => array(
424 'label' => __( 'With many conditions for the block, they are checked with the AND operator', 'jet-form-builder' )
425 ),
426 'conditional_action' => array(
427 'label' => __( 'With many conditions for the action, they are checked with the AND operator', 'jet-form-builder' )
428 ),
429 );
430 }
431
432 public function enqueue_form_assets() {
433
434 $handle = 'jet-form-builder/form';
435
436 do_action( 'jet-form-builder/other-editor-assets/before', $this, $handle );
437
438 wp_enqueue_script(
439 $handle,
440 JET_FORM_BUILDER_URL . 'assets/js/form-block.js',
441 array(
442 'wp-editor',
443 'wp-core-data',
444 'wp-data',
445 'wp-block-library',
446 'wp-format-library',
447 'wp-api-fetch',
448 ),
449 JET_FORM_BUILDER_VERSION,
450 true
451 );
452
453 wp_enqueue_style(
454 'jet-form-builder-others',
455 Plugin::instance()->plugin_url( 'assets/css/frontend.css' ),
456 array(),
457 Plugin::instance()->get_version()
458 );
459
460 wp_localize_script( $handle, 'JetFormEditorData', array(
461 'allowedBlocks' => $this->get_allowed_blocks(),
462 'action' => $this->get_action(),
463 'itemID' => $this->get_item_id(),
464 'presetConfig' => $this->get_preset_config(),
465 ) );
466
467 do_action( 'jet-form-builder/other-editor-assets/after', $this, $handle );
468
469 }
470
471 /**
472 * Render new editor instance
473 *
474 * @return [type] [description]
475 */
476 public function render( $input_name = '', $content = '', $form_name = '' ) {
477
478 if ( ! $input_name ) {
479 $input_name = 'jet_form_editor_' . self::$index;
480 self::$index ++;
481 }
482
483 ?>
484 <input name="<?php echo esc_attr( $input_name ); ?>" id="<?php echo esc_attr( $input_name ); ?>" type="hidden"/>
485 <script>
486 document.addEventListener( 'jet-form-builder-initialized', function ( event ) {
487 window.JetFormEditor(
488 '<?php echo esc_js( $input_name ); ?>',
489 '<?php echo esc_js( $input_name ); ?>',
490 '<?php echo html_entity_decode( esc_js( $content ) ); ?>',
491 '<?php echo esc_js( $form_name ); ?>'
492 );
493 } );
494 </script>
495 <?php
496 }
497
498 }
499