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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
<?php /** * Ajax actions used in by admin. * * @since 1.0.0 */
/** * Save a form. * * @since 1.0.0 */ function wpforms_save_form() {
// Run a security check. if ( ! check_ajax_referer( 'wpforms-builder', 'nonce', false ) ) { wp_send_json_error( esc_html__( 'Your session expired. Please reload the builder.', 'wpforms-lite' ) ); }
// Check for permissions. if ( ! wpforms_current_user_can( 'edit_forms' ) ) { wp_send_json_error( esc_html__( 'You are not allowed to perform this action.', 'wpforms-lite' ) ); }
// Check for form data. if ( empty( $_POST['data'] ) ) { wp_send_json_error( esc_html__( 'Something went wrong while performing this action.', 'wpforms-lite' ) ); }
$form_post = json_decode( stripslashes( $_POST['data'] ) ); // phpcs:ignore $data = array();
if ( ! is_null( $form_post ) && $form_post ) { foreach ( $form_post as $post_input_data ) { // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`), // derive the array path keys via regex and set the value in $_POST. preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches );
$array_bits = array( $matches[1] );
if ( isset( $matches[3] ) ) { $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) ); }
$new_post_data = array();
// Build the new array value from leaf to trunk. for ( $i = count( $array_bits ) - 1; $i >= 0; $i -- ) { if ( $i === count( $array_bits ) - 1 ) { $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value ); } else { $new_post_data = array( $array_bits[ $i ] => $new_post_data, ); } }
$data = array_replace_recursive( $data, $new_post_data ); } }
$form_id = wpforms()->form->update( $data['id'], $data );
do_action( 'wpforms_builder_save_form', $form_id, $data );
if ( ! $form_id ) { wp_send_json_error( esc_html__( 'Something went wrong while saving the form.', 'wpforms-lite' ) ); }
wp_send_json_success( apply_filters( 'wpforms_builder_save_form_response_data', array( 'form_name' => esc_html( $data['settings']['form_title'] ), 'form_desc' => $data['settings']['form_desc'], 'redirect' => admin_url( 'admin.php?page=wpforms-overview' ), ), $form_id, $data ) ); }
add_action( 'wp_ajax_wpforms_save_form', 'wpforms_save_form' );
/** * Create a new form * * @since 1.0.0 */ function wpforms_new_form() {
// Run a security check. check_ajax_referer( 'wpforms-builder', 'nonce' );
// Check for form name. if ( empty( $_POST['title'] ) ) { die( esc_html__( 'No form name provided', 'wpforms-lite' ) ); }
// Create form. $form_title = sanitize_text_field( $_POST['title'] ); $form_template = sanitize_text_field( $_POST['template'] ); $title_exists = get_page_by_title( $form_title, 'OBJECT', 'wpforms' ); $form_id = wpforms()->form->add( $form_title, array(), array( 'template' => $form_template, ) ); if ( null !== $title_exists ) { wp_update_post( array( 'ID' => $form_id, 'post_title' => $form_title . ' (ID #' . $form_id . ')', ) ); }
if ( ! $form_id ) { die( esc_html__( 'Error creating form', 'wpforms-lite' ) ); }
if ( wpforms_current_user_can( 'edit_form_single', $form_id ) ) { wp_send_json_success( array( 'id' => $form_id, 'redirect' => add_query_arg( array( 'view' => 'fields', 'form_id' => $form_id, 'newform' => '1', ), admin_url( 'admin.php?page=wpforms-builder' ) ), ) ); }
if ( wpforms_current_user_can( 'view_forms' ) ) { wp_send_json_success( array( 'redirect' => admin_url( 'admin.php?page=wpforms-overview' ) ) ); }
wp_send_json_success( array( 'redirect' => admin_url() ) ); }
add_action( 'wp_ajax_wpforms_new_form', 'wpforms_new_form' );
/** * Update form template. * * @since 1.0.0 */ function wpforms_update_form_template() {
// Run a security check. check_ajax_referer( 'wpforms-builder', 'nonce' );
// Check for form name. if ( empty( $_POST['form_id'] ) ) { die( esc_html__( 'No form ID provided', 'wpforms-lite' ) ); }
$data = wpforms()->form->get( (int) $_POST['form_id'], array( 'content_only' => true, ) ); $form_id = wpforms()->form->update( (int) $_POST['form_id'], $data, array( 'template' => $_POST['template'], ) );
if ( $form_id ) { $data = array( 'id' => $form_id, 'redirect' => add_query_arg( array( 'view' => 'fields', 'form_id' => $form_id, ), admin_url( 'admin.php?page=wpforms-builder' ) ), ); wp_send_json_success( $data ); } else { die( esc_html__( 'Error updating form template', 'wpforms-lite' ) ); } }
add_action( 'wp_ajax_wpforms_update_form_template', 'wpforms_update_form_template' );
/** * Form Builder update next field ID. * * @since 1.2.9 */ function wpforms_builder_increase_next_field_id() {
// Run a security check. check_ajax_referer( 'wpforms-builder', 'nonce' );
// Check for permissions. if ( ! wpforms_current_user_can( 'edit_forms' ) ) { wp_send_json_error(); }
// Check for required items. if ( empty( $_POST['form_id'] ) ) { wp_send_json_error(); }
wpforms()->form->next_field_id( absint( $_POST['form_id'] ) );
wp_send_json_success(); }
add_action( 'wp_ajax_wpforms_builder_increase_next_field_id', 'wpforms_builder_increase_next_field_id' );
/** * Form Builder Dynamic Choices option toggle. * * This can be triggered with select/radio/checkbox fields. * * @since 1.2.8 */ function wpforms_builder_dynamic_choices() {
// Run a security check. check_ajax_referer( 'wpforms-builder', 'nonce' );
// Check for permissions. if ( ! wpforms_current_user_can( 'edit_forms' ) ) { wp_send_json_error(); }
// Check for valid/required items. if ( ! isset( $_POST['field_id'] ) || empty( $_POST['type'] ) || ! in_array( $_POST['type'], array( 'post_type', 'taxonomy' ), true ) ) { wp_send_json_error(); }
$type = esc_attr( $_POST['type'] ); $id = absint( $_POST['field_id'] );
// Fetch the option row HTML to be returned to the builder. $field = new WPForms_Field_Select( false ); $field_args = array( 'id' => $id, 'dynamic_choices' => $type, ); $option_row = $field->field_option( 'dynamic_choices_source', $field_args, array(), false );
wp_send_json_success( array( 'markup' => $option_row, ) ); }
add_action( 'wp_ajax_wpforms_builder_dynamic_choices', 'wpforms_builder_dynamic_choices' );
/** * Form Builder Dynamic Choices Source option toggle. * * This can be triggered with select/radio/checkbox fields. * * @since 1.2.8 */ function wpforms_builder_dynamic_source() {
// Run a security check. check_ajax_referer( 'wpforms-builder', 'nonce' );
// Check for permissions. if ( ! wpforms_current_user_can( 'edit_forms' ) ) { wp_send_json_error(); }
// Check for required items. if ( ! isset( $_POST['field_id'] ) || empty( $_POST['form_id'] ) || empty( $_POST['type'] ) || empty( $_POST['source'] ) ) { wp_send_json_error(); }
$type = esc_attr( $_POST['type'] ); $source = esc_attr( $_POST['source'] ); $id = absint( $_POST['field_id'] ); $form_id = absint( $_POST['form_id'] ); $items = array(); $total = 0; $source_name = ''; $type_name = '';
if ( 'post_type' === $type ) {
$type_name = esc_html__( 'post type', 'wpforms-lite' ); $args = array( 'post_type' => $source, 'posts_per_page' => - 1, 'orderby' => 'title', 'order' => 'ASC', ); $posts = wpforms_get_hierarchical_object( apply_filters( 'wpforms_dynamic_choice_post_type_args', $args, array( 'id' => $id, ), $form_id ), true ); $total = wp_count_posts( $source ); $total = $total->publish; $pt = get_post_type_object( $source ); $source_name = ''; if ( null !== $pt ) { $source_name = $pt->labels->name; }
foreach ( $posts as $post ) { $items[] = $post->post_title; } } elseif ( 'taxonomy' === $type ) {
$type_name = esc_html__( 'taxonomy', 'wpforms-lite' ); $args = array( 'taxonomy' => $source, 'hide_empty' => false, ); $terms = wpforms_get_hierarchical_object( apply_filters( 'wpforms_dynamic_choice_taxonomy_args', $args, array( 'id' => $id, ), $form_id ), true ); $total = wp_count_terms( $source ); $tax = get_taxonomy( $source ); $source_name = $tax->labels->name;
foreach ( $terms as $term ) { $items[] = $term->name; } }
if ( empty( $items ) ) { $items = array( esc_html__( '(empty)', 'wpforms-lite' ), ); }
wp_send_json_success( array( 'items' => $items, 'source' => $source, 'source_name' => $source_name, 'total' => $total, 'type' => $type, 'type_name' => $type_name, ) ); }
add_action( 'wp_ajax_wpforms_builder_dynamic_source', 'wpforms_builder_dynamic_source' );
/** * Perform test connection to verify that the current web host can successfully * make outbound SSL connections. * * @since 1.4.5 */ function wpforms_verify_ssl() {
// Run a security check. check_ajax_referer( 'wpforms-admin', 'nonce' );
// Check for permissions. if ( ! wpforms_current_user_can() ) { wp_send_json_error( array( 'msg' => esc_html__( 'You do not have permission to perform this operation.', 'wpforms-lite' ), ) ); }
$response = wp_remote_post( 'https://wpforms.com/connection-test.php' );
if ( 200 === wp_remote_retrieve_response_code( $response ) ) { wp_send_json_success( array( 'msg' => esc_html__( 'Success! Your server can make SSL connections.', 'wpforms-lite' ), ) ); }
wp_send_json_error( array( 'msg' => esc_html__( 'There was an error and the connection failed. Please contact your web host with the technical details below.', 'wpforms-lite' ), 'debug' => '<pre>' . print_r( map_deep( $response, 'wp_strip_all_tags' ), true ) . '</pre>', ) ); } add_action( 'wp_ajax_wpforms_verify_ssl', 'wpforms_verify_ssl' );
/** * Deactivate addon. * * @since 1.0.0 * @since 1.6.2.3 Updated the permissions checking. */ function wpforms_deactivate_addon() {
// Run a security check. check_ajax_referer( 'wpforms-admin', 'nonce' );
// Check for permissions. if ( ! current_user_can( 'deactivate_plugins' ) ) { wp_send_json_error( esc_html__( 'Plugin deactivation is disabled for you on this site.', 'wpforms-lite' ) ); }
$type = 'addon'; if ( ! empty( $_POST['type'] ) ) { $type = sanitize_key( $_POST['type'] ); }
if ( isset( $_POST['plugin'] ) ) { $plugin = sanitize_text_field( wp_unslash( $_POST['plugin'] ) );
deactivate_plugins( $plugin );
do_action( 'wpforms_plugin_deactivated', $plugin );
if ( 'plugin' === $type ) { wp_send_json_success( esc_html__( 'Plugin deactivated.', 'wpforms-lite' ) ); } else { wp_send_json_success( esc_html__( 'Addon deactivated.', 'wpforms-lite' ) ); } }
wp_send_json_error( esc_html__( 'Could not deactivate the addon. Please deactivate from the Plugins page.', 'wpforms-lite' ) ); } add_action( 'wp_ajax_wpforms_deactivate_addon', 'wpforms_deactivate_addon' );
/** * Activate addon. * * @since 1.0.0 * @since 1.6.2.3 Updated the permissions checking. */ function wpforms_activate_addon() {
// Run a security check. check_ajax_referer( 'wpforms-admin', 'nonce' );
// Check for permissions. if ( ! current_user_can( 'activate_plugins' ) ) { wp_send_json_error( esc_html__( 'Plugin activation is disabled for you on this site.', 'wpforms-lite' ) ); }
if ( isset( $_POST['plugin'] ) ) {
$type = 'addon'; if ( ! empty( $_POST['type'] ) ) { $type = sanitize_key( $_POST['type'] ); }
$plugin = sanitize_text_field( wp_unslash( $_POST['plugin'] ) ); $activate = activate_plugins( $plugin );
do_action( 'wpforms_plugin_activated', $plugin );
if ( ! is_wp_error( $activate ) ) { if ( 'plugin' === $type ) { wp_send_json_success( esc_html__( 'Plugin activated.', 'wpforms-lite' ) ); } else { wp_send_json_success( esc_html__( 'Addon activated.', 'wpforms-lite' ) ); } } }
wp_send_json_error( esc_html__( 'Could not activate addon. Please activate from the Plugins page.', 'wpforms-lite' ) ); } add_action( 'wp_ajax_wpforms_activate_addon', 'wpforms_activate_addon' );
/** * Install addon. * * @since 1.0.0 * @since 1.6.2.3 Updated the permissions checking. */ function wpforms_install_addon() {
// Run a security check. check_ajax_referer( 'wpforms-admin', 'nonce' );
$generic_error = esc_html__( 'There was an error while performing your request.', 'wpforms-lite' );
$type = 'addon'; if ( ! empty( $_POST['type'] ) ) { $type = sanitize_key( $_POST['type'] ); }
// Check if new installations are allowed. if ( ! wpforms_can_install( $type ) ) { wp_send_json_error( $generic_error ); }
$error = esc_html__( 'Could not install addon. Please download from wpforms.com and install manually.', 'wpforms-lite' );
if ( empty( $_POST['plugin'] ) ) { wp_send_json_error( $error ); }
// Set the current screen to avoid undefined notices. set_current_screen( 'wpforms_page_wpforms-settings' );
// Prepare variables. $url = esc_url_raw( add_query_arg( array( 'page' => 'wpforms-addons', ), admin_url( 'admin.php' ) ) );
$creds = request_filesystem_credentials( $url, '', false, false, null );
// Check for file system permissions. if ( false === $creds ) { wp_send_json_error( $error ); }
if ( ! WP_Filesystem( $creds ) ) { wp_send_json_error( $error ); }
/* * We do not need any extra credentials if we have gotten this far, so let's install the plugin. */
require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-install-skin.php';
// Do not allow WordPress to search/download translations, as this will break JS output. remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
// Create the plugin upgrader with our custom skin. $installer = new WPForms\Helpers\PluginSilentUpgrader( new WPForms_Install_Skin() );
// Error check. if ( ! method_exists( $installer, 'install' ) || empty( $_POST['plugin'] ) ) { wp_send_json_error( $error ); }
$installer->install( $_POST['plugin'] ); // phpcs:ignore
// Flush the cache and return the newly installed plugin basename. wp_cache_flush();
$plugin_basename = $installer->plugin_info();
if ( empty( $plugin_basename ) ) { wp_send_json_error( $error ); }
$result = array( 'msg' => $generic_error, 'is_activated' => false, 'basename' => $plugin_basename, );
// Check for permissions. if ( ! current_user_can( 'activate_plugins' ) ) { $result['msg'] = 'plugin' === $type ? esc_html__( 'Plugin installed.', 'wpforms-lite' ) : esc_html__( 'Addon installed.', 'wpforms-lite' );
wp_send_json_success( $result ); }
// Activate the plugin silently. $activated = activate_plugin( $plugin_basename );
if ( ! is_wp_error( $activated ) ) { $result['is_activated'] = true; $result['msg'] = 'plugin' === $type ? esc_html__( 'Plugin installed & activated.', 'wpforms-lite' ) : esc_html__( 'Addon installed & activated.', 'wpforms-lite' );
wp_send_json_success( $result ); }
// Fallback error just in case. wp_send_json_error( $result ); } add_action( 'wp_ajax_wpforms_install_addon', 'wpforms_install_addon' );
|