forked from weDevsOfficial/wp-user-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.php
564 lines (484 loc) · 18.5 KB
/
form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
<?php
/**
* Post Forms or wpuf_forms form builder class
*
* @package WP User Frontend
*/
class WPUF_Admin_Form {
/**
* Form type of which we're working on
*
* @var string
*/
private $form_type = 'post';
/**
* Form settings key
*
* @var string
*/
private $form_settings_key = 'wpuf_form_settings';
/**
* WP post types
*
* @var string
*/
private $wp_post_types = array();
/**
* Add neccessary actions and filters
*
* @return void
*/
public function __construct() {
add_action( 'init', array($this, 'register_post_type') );
add_action( "load-user-frontend_page_wpuf-post-forms", array( $this, 'post_forms_builder_init' ) );
}
/**
* Register form post types
*
* @return void
*/
public function register_post_type() {
$capability = wpuf_admin_role();
register_post_type( 'wpuf_forms', array(
'label' => __( 'Forms', 'wp-user-frontend' ),
'public' => false,
'show_ui' => false,
'show_in_menu' => false, //false,
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => false,
'supports' => array('title'),
'capabilities' => array(
'publish_posts' => $capability,
'edit_posts' => $capability,
'edit_others_posts' => $capability,
'delete_posts' => $capability,
'delete_others_posts' => $capability,
'read_private_posts' => $capability,
'edit_post' => $capability,
'delete_post' => $capability,
'read_post' => $capability,
),
'labels' => array(
'name' => __( 'Forms', 'wp-user-frontend' ),
'singular_name' => __( 'Form', 'wp-user-frontend' ),
'menu_name' => __( 'Forms', 'wp-user-frontend' ),
'add_new' => __( 'Add Form', 'wp-user-frontend' ),
'add_new_item' => __( 'Add New Form', 'wp-user-frontend' ),
'edit' => __( 'Edit', 'wp-user-frontend' ),
'edit_item' => __( 'Edit Form', 'wp-user-frontend' ),
'new_item' => __( 'New Form', 'wp-user-frontend' ),
'view' => __( 'View Form', 'wp-user-frontend' ),
'view_item' => __( 'View Form', 'wp-user-frontend' ),
'search_items' => __( 'Search Form', 'wp-user-frontend' ),
'not_found' => __( 'No Form Found', 'wp-user-frontend' ),
'not_found_in_trash' => __( 'No Form Found in Trash', 'wp-user-frontend' ),
'parent' => __( 'Parent Form', 'wp-user-frontend' ),
),
) );
register_post_type( 'wpuf_profile', array(
'label' => __( 'Registraton Forms', 'wp-user-frontend' ),
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => false,
'supports' => array('title'),
'capabilities' => array(
'publish_posts' => $capability,
'edit_posts' => $capability,
'edit_others_posts' => $capability,
'delete_posts' => $capability,
'delete_others_posts' => $capability,
'read_private_posts' => $capability,
'edit_post' => $capability,
'delete_post' => $capability,
'read_post' => $capability,
),
'labels' => array(
'name' => __( 'Forms', 'wp-user-frontend' ),
'singular_name' => __( 'Form', 'wp-user-frontend' ),
'menu_name' => __( 'Registration Forms', 'wp-user-frontend' ),
'add_new' => __( 'Add Form', 'wp-user-frontend' ),
'add_new_item' => __( 'Add New Form', 'wp-user-frontend' ),
'edit' => __( 'Edit', 'wp-user-frontend' ),
'edit_item' => __( 'Edit Form', 'wp-user-frontend' ),
'new_item' => __( 'New Form', 'wp-user-frontend' ),
'view' => __( 'View Form', 'wp-user-frontend' ),
'view_item' => __( 'View Form', 'wp-user-frontend' ),
'search_items' => __( 'Search Form', 'wp-user-frontend' ),
'not_found' => __( 'No Form Found', 'wp-user-frontend' ),
'not_found_in_trash' => __( 'No Form Found in Trash', 'wp-user-frontend' ),
'parent' => __( 'Parent Form', 'wp-user-frontend' ),
),
) );
register_post_type( 'wpuf_input', array(
'public' => false,
'show_ui' => false,
'show_in_menu' => false,
) );
}
/**
* Initiate form builder for wpuf_forms post type
*
* @since 2.5
*
* @return void
*/
public function post_forms_builder_init() {
if ( ! isset( $_GET['action'] ) ) {
return;
}
if ( 'add-new' === $_GET['action'] && empty( $_GET['id'] ) ) {
$form_id = wpuf_create_sample_form( 'Sample Form', 'wpuf_forms', true );
$add_new_page_url = add_query_arg( array( 'id' => $form_id ), admin_url( 'admin.php?page=wpuf-post-forms&action=edit' ) );
wp_redirect( $add_new_page_url );
}
if ( ( 'edit' === $_GET['action'] ) && ! empty( $_GET['id'] ) ) {
add_action( 'wpuf-form-builder-tabs-post', array( $this, 'add_primary_tabs' ) );
add_action( 'wpuf-form-builder-tab-contents-post', array( $this, 'add_primary_tab_contents' ) );
add_action( 'wpuf-form-builder-settings-tabs-post', array( $this, 'add_settings_tabs' ) );
add_action( 'wpuf-form-builder-settings-tab-contents-post', array( $this, 'add_settings_tab_contents' ) );
add_filter( 'wpuf-form-fields-section-before', array( $this, 'add_post_field_section' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wpuf-form-builder-js-deps', array( $this, 'js_dependencies' ) );
add_filter( 'wpuf-form-builder-js-root-mixins', array( $this, 'js_root_mixins' ) );
add_filter( 'wpuf-form-builder-js-builder-stage-mixins', array( $this, 'js_builder_stage_mixins' ) );
add_filter( 'wpuf-form-builder-js-field-options-mixins', array( $this, 'js_field_options_mixins' ) );
add_action( 'wpuf-form-builder-template-builder-stage-submit-area', array( $this, 'add_form_submit_area' ) );
add_action( 'wpuf-form-builder-localize-script', array( $this, 'add_to_localize_script' ) );
add_filter( 'wpuf-form-fields', array( $this, 'add_field_settings' ) );
add_filter( 'wpuf-form-builder-i18n', array( $this, 'i18n' ) );
do_action( 'wpuf-form-builder-init-type-wpuf_forms' );
$this->set_wp_post_types();
$settings = array(
'form_type' => 'post',
'post_type' => 'wpuf_forms',
'post_id' => $_GET['id'],
'form_settings_key' => $this->form_settings_key,
'shortcodes' => array( array( 'name' => 'wpuf_form' ) )
);
new WPUF_Admin_Form_Builder( $settings );
}
}
/**
* Additional primary tabs
*
* @since 2.5
*
* @return void
*/
public function add_primary_tabs() {
?>
<a href="#wpuf-form-builder-notification" class="nav-tab">
<?php _e( 'Notification', 'wp-user-frontend' ); ?>
</a>
<?php
}
/**
* Add primary tab contents
*
* @since 2.5
*
* @return void
*/
public function add_primary_tab_contents() {
?>
<div id="wpuf-form-builder-notification" class="group">
<?php do_action('wpuf_form_settings_post_notification'); ?>
</div><!-- #wpuf-form-builder-notification -->
<?php
}
/**
* Add settings tabs
*
* @since 2.5
*
* @return void
*/
public function add_settings_tabs() {
?>
<a href="#wpuf-metabox-settings" class="nav-tab"><?php _e( 'Post Settings', 'wp-user-frontend' ); ?></a>
<a href="#wpuf-metabox-settings-update" class="nav-tab"><?php _e( 'Edit Settings', 'wp-user-frontend' ); ?></a>
<a href="#wpuf-metabox-submission-restriction" class="nav-tab"><?php _e( 'Submission Restriction', 'wp-user-frontend' ); ?></a>
<a href="#wpuf-metabox-settings-payment" class="nav-tab"><?php _e( 'Payment Settings', 'wp-user-frontend' ); ?></a>
<a href="#wpuf-metabox-settings-display" class="nav-tab"><?php _e( 'Display Settings', 'wp-user-frontend' ); ?></a>
<a href="#wpuf-metabox-post_expiration" class="nav-tab"><?php _e( 'Post Expiration', 'wp-user-frontend' ); ?></a>
<?php do_action( 'wpuf_post_form_tab' ); ?>
<?php
}
/**
* Add settings tabs
*
* @since 2.5
*
* @return void
*/
public function add_settings_tab_contents() {
global $post;
$form_settings = wpuf_get_form_settings( $post->ID );
?>
<div id="wpuf-metabox-settings" class="group">
<?php include_once dirname( __FILE__ ) . '/html/form-settings-post.php'; ?>
</div>
<div id="wpuf-metabox-settings-update" class="group">
<?php include_once dirname( __FILE__ ) . '/html/form-settings-post-edit.php'; ?>
</div>
<div id="wpuf-metabox-submission-restriction" class="group">
<?php include_once dirname( __FILE__ ) . '/html/form-submission-restriction.php'; ?>
</div>
<div id="wpuf-metabox-settings-payment" class="group">
<?php include_once dirname( __FILE__ ) . '/html/form-settings-payment.php'; ?>
</div>
<div id="wpuf-metabox-settings-display" class="group">
<?php include_once dirname( __FILE__ ) . '/html/form-settings-display.php'; ?>
</div>
<div id="wpuf-metabox-post_expiration" class="group wpuf-metabox-post_expiration">
<?php $this->form_post_expiration(); ?>
</div>
<?php do_action( 'wpuf_post_form_tab_content' ); ?>
<?php
}
/**
* Subscription dropdown
*
* @since 2.5
*
* @param string $selected
*
* @return void
*/
public function subscription_dropdown( $selected = null ) {
$subscriptions = WPUF_Subscription::init()->get_subscriptions();
if ( ! $subscriptions ) {
printf( '<option>%s</option>', __( '- Select -', 'wp-user-frontend' ) );
return;
}
printf( '<option>%s</option>', __( '- Select -', 'wp-user-frontend' ) );
foreach ( $subscriptions as $key => $subscription ) {
?>
<option value="<?php echo esc_attr( $subscription->ID ); ?>" <?php selected( $selected, $subscription->ID ); ?> ><?php echo $subscription->post_title; ?></option>
<?php
}
}
/**
* Settings for post expiration
*
* @since 2.2.7
*
* @global $post
*/
public function form_post_expiration(){
do_action('wpuf_form_post_expiration');
}
/**
* Add post fields in form builder
*
* @since 2.5
*
* @return array
*/
public function add_post_field_section() {
$post_fields = apply_filters( 'wpuf-form-builder-wp_forms-fields-section-post-fields', array(
'post_title', 'post_content', 'post_excerpt', 'featured_image'
) );
return array(
array(
'title' => __( 'Post Fields', 'wp-user-frontend' ),
'id' => 'post-fields',
'fields' => $post_fields
),
array(
'title' => __( 'Taxonomies', 'wp-user-frontend' ),
'id' => 'taxonomies',
'fields' => array()
)
);
}
/**
* Admin script form wpuf_forms form builder
*
* @since 2.5
*
* @return void
*/
public function admin_enqueue_scripts() {
wp_register_script(
'wpuf-form-builder-wpuf-forms',
WPUF_ASSET_URI . '/js/wpuf-form-builder-wpuf-forms.js',
array( 'jquery', 'underscore', 'wpuf-vue', 'wpuf-vuex' ),
WPUF_VERSION,
true
);
}
/**
* Add dependencies to form builder script
*
* @since 2.5
*
* @param array $deps
*
* @return array
*/
public function js_dependencies( $deps ) {
$deps[] = 'wpuf-form-builder-wpuf-forms';
return apply_filters( 'wpuf-form-builder-wpuf-forms-js-deps', $deps );
}
/**
* Add mixins to root instance
*
* @since 2.5
*
* @param array $mixins
*
* @return array
*/
public function js_root_mixins( $mixins ) {
array_push( $mixins , 'wpuf_forms_mixin_root' );
return $mixins;
}
/**
* Add mixins to form builder builder stage component
*
* @since 2.5
*
* @param array $mixins
*
* @return array
*/
public function js_builder_stage_mixins( $mixins ) {
array_push( $mixins , 'wpuf_forms_mixin_builder_stage' );
return $mixins;
}
/**
* Add mixins to form builder field options component
*
* @since 2.5
*
* @param array $mixins
*
* @return array
*/
public function js_field_options_mixins( $mixins ) {
array_push( $mixins , 'wpuf_forms_mixin_field_options' );
return $mixins;
}
/**
* Add buttons in form submit area
*
* @since 2.5
*
* @return void
*/
public function add_form_submit_area() {
?>
<input @click.prevent="" type="submit" name="submit" :value="post_form_settings.submit_text">
<a
v-if="post_form_settings.draft_post"
@click.prevent=""
href="#"
class="btn"
id="wpuf-post-draft"
>
<?php _e( 'Save Draft', 'wp-user-frontend' ); ?>
</a>
<?php
}
/**
* Populate available wp post types
*
* @since 2.5
*
* @return void
*/
public function set_wp_post_types() {
$args = array( '_builtin' => true );
$wpuf_post_types = wpuf_get_post_types( $args );
$ignore_taxonomies = apply_filters( 'wpuf-ignore-taxonomies', array(
'post_format'
) );
foreach ( $wpuf_post_types as $post_type ) {
$this->wp_post_types[ $post_type ] = array();
$taxonomies = get_object_taxonomies( $post_type, 'object' );
foreach ( $taxonomies as $tax_name => $taxonomy ) {
if ( ! in_array( $tax_name, $ignore_taxonomies ) ) {
$this->wp_post_types[ $post_type ][ $tax_name ] = array(
'title' => $taxonomy->label,
'hierarchical' => $taxonomy->hierarchical
);
$this->wp_post_types[ $post_type ][ $tax_name ]['terms'] = get_terms( array(
'taxonomy' => $tax_name,
'hide_empty' => false
) );
}
}
}
}
/**
* Add data to localize_script
*
* @since 2.5
*
* @param array $data
*
* @return array
*/
public function add_to_localize_script( $data ) {
return array_merge( $data, array(
'wp_post_types' => $this->wp_post_types
) );
}
/**
* Add field settings
*
* @since 2.5
*
* @param array $field_settings
*
* @return array
*/
public function add_field_settings( $field_settings ) {
if ( class_exists( 'WPUF_Field_Contract' ) ) {
require_once WPUF_ROOT . '/includes/fields/class-field-post-title.php';
require_once WPUF_ROOT . '/includes/fields/class-field-post-content.php';
require_once WPUF_ROOT . '/includes/fields/class-field-post-tags.php';
require_once WPUF_ROOT . '/includes/fields/class-field-post-excerpt.php';
require_once WPUF_ROOT . '/includes/fields/class-field-post-taxonomy.php';
require_once WPUF_ROOT . '/includes/fields/class-field-featured-image.php';
$field_settings['post_title'] = new WPUF_Form_Field_Post_Title();
$field_settings['post_content'] = new WPUF_Form_Field_Post_Content();
$field_settings['post_excerpt'] = new WPUF_Form_Field_Post_Excerpt();
$field_settings['featured_image'] = new WPUF_Form_Field_Featured_Image();
$taxonomy_templates = array();
foreach ( $this->wp_post_types as $post_type => $taxonomies ) {
if ( ! empty( $taxonomies ) ) {
foreach ( $taxonomies as $tax_name => $taxonomy ) {
if ( 'post_tag' === $tax_name ) {
$taxonomy_templates['post_tag'] = new WPUF_Form_Field_Post_Tags();
} else {
$taxonomy_templates[ $tax_name ] = new WPUF_Form_Field_Post_Taxonomy($tax_name, $taxonomy);
// $taxonomy_templates[ 'taxonomy' ] = new WPUF_Form_Field_Post_Taxonomy($tax_name, $taxonomy);
}
}
}
}
$field_settings = array_merge( $field_settings, $taxonomy_templates );
}
return $field_settings;
}
/**
* i18n strings specially for Post Forms
*
* @since 2.5
*
* @param array $i18n
*
* @return array
*/
public function i18n( $i18n ) {
return array_merge( $i18n, array(
'any_of_three_needed' => __( 'Post Forms must have either Post Title, Post Body or Excerpt field', 'wp-user-frontend' )
) );
}
}