-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathFrmFormsController.php
More file actions
3933 lines (3325 loc) · 109 KB
/
Copy pathFrmFormsController.php
File metadata and controls
3933 lines (3325 loc) · 109 KB
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}
class FrmFormsController {
/**
* Track the form that opened the redirect URL in a new tab. This is used to check if we should show the default
* message in the current tab.
*
* @since 6.2
*
* @var array Keys are form IDs and values are 1.
*/
private static $redirected_in_new_tab = array();
/**
* The HTML for the Formdiable TinyMCE button (That triggers a popup to insert shortcodes)
* is stored here and re-used as an optimization.
*
* @since 6.11
*
* @var string|null
*/
private static $formidable_tinymce_button;
public static function menu() {
$menu_label = __( 'Forms', 'formidable' );
if ( ! FrmAppHelper::pro_is_installed() ) {
$menu_label .= ' (Lite)';
}
add_submenu_page( 'formidable', 'Formidable | ' . $menu_label, $menu_label, 'frm_view_forms', 'formidable', 'FrmFormsController::route' );
self::maybe_load_listing_hooks();
}
public static function maybe_load_listing_hooks() {
if ( ! FrmAppHelper::on_form_listing_page() ) {
return;
}
add_filter( 'get_user_option_managetoplevel_page_formidablecolumnshidden', 'FrmFormsController::hidden_columns' );
add_filter( 'manage_toplevel_page_formidable_columns', 'FrmFormsController::get_columns', 0 );
add_filter( 'manage_toplevel_page_formidable_sortable_columns', 'FrmFormsController::get_sortable_columns' );
}
/**
* Runs on admin head of the formidable forms page.
*
* @since 6.32 This adds screen options.
*
* @return void
*/
public static function head() {
if ( wp_is_mobile() ) {
wp_enqueue_script( 'jquery-touch-punch' );
}
}
public static function register_widgets() {
require_once FrmAppHelper::plugin_path() . '/classes/widgets/FrmShowForm.php';
register_widget( 'FrmShowForm' );
}
/**
* Show a message about conditional logic
*
* @since 4.06.03
*/
public static function logic_tip() {
$images_url = FrmAppHelper::plugin_url() . '/images/';
$data_message = __( 'Only show the fields you need and create branching forms. Upgrade to get conditional logic and question branching.', 'formidable' );
$data_message .= ' <img src="' . esc_url( $images_url ) . '/survey-logic.png" srcset="' . esc_url( $images_url ) . 'survey-logic@2x.png 2x" alt="' . esc_attr__( 'Conditional Logic options', 'formidable' ) . '"/>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
echo '<a href="javascript:void(0)" class="frm_noallow frm_show_upgrade frm_add_logic_link frm-collapsed frm-flex-justify" data-upgrade="' . esc_attr__( 'Conditional Logic options', 'formidable' ) . '" data-message="' . esc_attr( $data_message ) . '" data-medium="builder" data-content="logic">'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
esc_html_e( 'Conditional Logic', 'formidable' );
FrmAppHelper::icon_by_class( 'frmfont frm_arrowdown8_icon', array( 'aria-hidden' => 'true' ) );
echo '</a>';
}
/**
* By default, Divi processes form shortcodes on the edit post page.
* Now that won't do.
*
* @since 3.01
*
* @param array $shortcodes List of shortcodes to process.
*
* @return array
*/
public static function prevent_divi_conflict( $shortcodes ) {
$shortcodes[] = 'formidable';
return $shortcodes;
}
/**
* @return void
*/
public static function list_form() {
FrmAppHelper::permission_check( 'frm_view_forms' );
$message = '';
$params = FrmForm::list_page_params();
$errors = self::process_bulk_form_actions( array() );
if ( isset( $errors['message'] ) ) {
$message = $errors['message'];
unset( $errors['message'] );
}
$errors = apply_filters( 'frm_admin_list_form_action', $errors );
self::display_forms_list( $params, $message, $errors );
}
/**
* Create the default email action
*
* @since 2.02.11
*
* @param int|object $form
*
* @return void
*/
private static function create_default_email_action( $form ) {
FrmForm::maybe_get_form( $form );
if ( ! is_object( $form ) ) {
return;
}
$create_email = apply_filters( 'frm_create_default_email_action', true, $form );
if ( ! $create_email ) {
return;
}
/**
* @var FrmFormAction
*/
$action_control = FrmFormActionsController::get_form_actions( 'email' );
$action_control->create( $form->id );
}
/**
* Create the default On submit action
*
* @since 6.0.0
*
* @param int|object $form Form object or ID.
*
* @return void
*/
private static function create_default_on_submit_action( $form ) {
FrmForm::maybe_get_form( $form );
if ( ! is_object( $form ) ) {
return;
}
/**
* Enable or disable the default On Submit action.
*
* @since 6.0.0
*
* @param bool $create Set to `false` if you want to disable.
* @param object $form Form object.
*/
$create = apply_filters( 'frm_create_default_on_submit_action', true, $form );
if ( ! $create ) {
return;
}
/**
* @var FrmFormAction
*/
$action_control = FrmFormActionsController::get_form_actions( FrmOnSubmitAction::$slug );
$action_control->create( $form->id );
}
/**
* Creates submit button field.
*
* @since 6.9
*
* @param int|object $form Form ID or object.
*
* @return void
*/
private static function create_submit_button_field( $form ) {
FrmForm::maybe_get_form( $form );
if ( ! is_object( $form ) ) {
return;
}
if ( FrmSubmitHelper::get_submit_field( $form->id ) ) {
// Do not create submit button field if it exists.
return;
}
FrmField::create(
array(
'type' => FrmSubmitHelper::FIELD_TYPE,
'name' => __( 'Submit', 'formidable' ),
'field_order' => FrmSubmitHelper::DEFAULT_ORDER,
'form_id' => $form->id,
'field_options' => FrmFieldsHelper::get_default_field_options( FrmSubmitHelper::FIELD_TYPE ),
'description' => '',
'default_value' => '',
'options' => array(),
)
);
}
/**
* @param array|false $values
*
* @return void
*/
public static function edit( $values = false ) {
FrmAppHelper::permission_check( 'frm_edit_forms' );
$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
self::get_edit_vars( $id );
}
/**
* @param mixed $id
* @param string $message
*
* @return void
*/
public static function settings( $id = false, $message = '' ) {
FrmAppHelper::permission_check( 'frm_edit_forms' );
if ( ! $id || ! is_numeric( $id ) ) {
$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
}
FrmOnSubmitHelper::maybe_migrate_submit_settings_to_action( $id );
self::get_settings_vars( $id, array(), $message );
}
public static function update_settings() {
FrmAppHelper::permission_check( 'frm_edit_forms' );
$process_form = FrmAppHelper::get_post_param( 'process_form', '', 'sanitize_text_field' );
if ( ! wp_verify_nonce( $process_form, 'process_form_nonce' ) ) {
$frm_settings = FrmAppHelper::get_settings();
$error_args = array(
'title' => __( 'Verification failed', 'formidable' ),
'body' => $frm_settings->admin_permission,
'cancel_text' => __( 'Cancel', 'formidable' ),
);
FrmAppController::show_error_modal( $error_args );
return;
}
$id = FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
$errors = FrmForm::validate( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$warnings = FrmFormsHelper::check_for_warnings( $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( count( $errors ) > 0 ) {
self::get_settings_vars( $id, $errors, compact( 'warnings' ) );
return;
}
do_action( 'frm_before_update_form_settings', $id );
$antispam_was_on = self::antispam_was_on( $id ); // phpcs:ignore Formidable.CodeAnalysis.InlineSingleUseVariable
FrmForm::update( $id, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$antispam_is_on = ! empty( $_POST['options']['antispam'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( $antispam_is_on !== $antispam_was_on ) {
FrmAntiSpam::clear_caches();
}
// Handle captcha field inclusion
$include_captcha = isset( $_POST['frm_include_captcha'] ) && '1' === $_POST['frm_include_captcha']; // phpcs:ignore WordPress.Security.NonceVerification.Missing
self::handle_captcha_field( $id, $include_captcha );
$message = __( 'Settings Successfully Updated', 'formidable' );
self::get_settings_vars( $id, array(), compact( 'message', 'warnings' ) );
}
/**
* @param int $form_id
*
* @return bool
*/
private static function antispam_was_on( $form_id ) {
$form = FrmForm::getOne( $form_id );
return ! empty( $form->options['antispam'] );
}
/**
* Handle captcha field inclusion in form
*
* @since 6.33
*
* @param int $form_id Form ID.
* @param bool $include_captcha Whether to include captcha field.
*
* @return void
*/
private static function handle_captcha_field( $form_id, $include_captcha ) {
$form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude' );
$captcha_field_id = 0;
$submit_field_order = 0;
foreach ( $form_fields as $field ) {
if ( 'captcha' === $field->type ) {
$captcha_field_id = $field->id;
break;
}
if ( 'submit' === $field->type ) {
$submit_field_order = $field->field_order;
}
}
if ( $include_captcha && ! $captcha_field_id ) {
// Create captcha field just before submit button
$field_values = FrmFieldsHelper::setup_new_vars( 'captcha', $form_id );
$field_values['name'] = __( 'Captcha', 'formidable' );
$field_values['field_order'] = $submit_field_order > 0 ? $submit_field_order - 1 : 0;
FrmField::create( $field_values );
} elseif ( ! $include_captcha && $captcha_field_id ) {
// Delete captcha field
FrmField::destroy( $captcha_field_id );
}
}
/**
* @param array $values
*
* @return void
*/
public static function update( $values = array() ) {
if ( ! $values ) {
$values = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
// Set radio button and checkbox meta equal to "other" value.
if ( FrmAppHelper::pro_is_installed() ) {
$values = FrmProEntry::mod_other_vals( $values, 'back' );
}
$errors = FrmForm::validate( $values );
$permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'frm_save_form', 'frm_save_form_nonce' );
if ( $permission_error !== false ) {
$errors['form'] = $permission_error;
}
$id = isset( $values['id'] ) ? absint( $values['id'] ) : FrmAppHelper::get_param( 'id', '', 'get', 'absint' );
if ( count( $errors ) > 0 ) {
self::get_edit_vars( $id, $errors );
return;
}
self::maybe_remove_draft_option_from_fields( $id );
FrmForm::update( $id, $values );
$message = __( 'Form was successfully updated.', 'formidable' );
if ( self::is_too_long( $values ) ) {
$message .= '<br/> ' . sprintf(
/* translators: %1$s: Start link HTML, %2$s: end link HTML */
__( 'However, your form is very long and may be %1$sreaching server limits%2$s.', 'formidable' ),
'<a href="' . esc_url( self::get_form_too_long_docs_url() ) . '" target="_blank" rel="noopener">',
'</a>'
);
}
if ( defined( 'DOING_AJAX' ) ) {
wp_die( FrmAppHelper::kses( $message, array( 'a' ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
self::get_edit_vars( $id, array(), $message );
}
/**
* @since 6.25.1
*
* @return string
*/
private static function get_form_too_long_docs_url() {
$utm = array(
'campaign' => 'builder',
'content' => 'form-too-long',
);
return FrmAppHelper::admin_upgrade_link( $utm, 'knowledgebase/i-have-a-long-form-why-did-the-options-at-the-end-of-the-form-stop-saving/' );
}
/**
* Remove the draft flag from any new fields from this current session.
*
* @since 6.8
*
* @param int $form_id
*
* @return void
*/
private static function maybe_remove_draft_option_from_fields( $form_id ) {
$draft_field_ids_csv = FrmAppHelper::get_post_param( 'draft_fields', '', 'sanitize_text_field' );
if ( ! $draft_field_ids_csv ) {
// If the draft_fields input is empty there are no new fields in the session.
return;
}
$draft_field_ids = array_filter( explode( ',', $draft_field_ids_csv ), 'is_numeric' );
if ( ! $draft_field_ids ) {
// Exit early if the draft fields input is invalid. It should be a CSV of integer values.
return;
}
$draft_field_rows = FrmFieldsHelper::get_draft_field_results( $form_id, $draft_field_ids );
foreach ( $draft_field_rows as $row ) {
$row->field_options['draft'] = 0;
FrmField::update( $row->id, array( 'field_options' => $row->field_options ) );
}
}
/**
* Check if the value at the end of the form was included.
* If it's missing, it means other values at the end of the form
* were likely not saved either.
*
* @since 3.06.01
*
* @param array $values
*
* @return bool
*/
private static function is_too_long( $values ) {
return empty( $values['frm_end'] );
}
public static function duplicate() {
FrmAppHelper::permission_check( 'frm_edit_forms' );
$nonce = FrmAppHelper::simple_get( '_wpnonce' );
if ( ! wp_verify_nonce( $nonce ) ) {
$frm_settings = FrmAppHelper::get_settings();
wp_die( esc_html( $frm_settings->admin_permission ) );
}
$params = FrmForm::list_page_params();
$form = FrmForm::duplicate( $params['id'], $params['template'], true );
$url = admin_url( 'admin.php?page=formidable' );
$message = 'form_duplicate_error';
if ( $form ) {
$new_template = FrmAppHelper::simple_get( 'new_template' ) ? '&new_template=true' : '';
$url = admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . absint( $form ) . $new_template );
$message = 'form_duplicated';
}
$url .= '&message=' . $message;
wp_safe_redirect( $url );
exit();
}
/**
* @return string|null
*/
public static function page_preview() {
$params = FrmForm::list_page_params();
if ( ! $params['form'] || is_numeric( $params['form'] ) ) {
return null;
}
self::maybe_block_preview( $params['form'] );
$form = FrmForm::getOne( $params['form'] );
if ( ! $form ) {
return null;
}
return self::show_form( $form->id, '', 'auto', 'auto' );
}
/**
* @since 3.0
*
* @return void
*/
public static function show_page_preview() {
$preview = self::page_preview();
if ( is_null( $preview ) ) {
wp_die(
'<h1>' . esc_html__( 'Form key is invalid', 'formidable' ) . '</h1>',
'<p>' . esc_html__( 'Form key is invalid', 'formidable' ) . '</p>',
404
);
}
echo $preview; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* @return void
*/
public static function preview() {
do_action( 'frm_wp' );
FrmAppHelper::set_current_screen_and_hook_suffix();
global $frm_vars;
$frm_vars['preview'] = true;
// print_emoji_styles is deprecated.
remove_action( 'wp_print_styles', 'print_emoji_styles' );
if ( FrmTestModeController::should_add_test_mode_container() ) {
do_action( 'frm_test_mode_init' );
add_action( 'wp_enqueue_scripts', 'FrmTestModeController::register_and_enqueue_required_scripts' );
add_filter( 'frm_filter_final_form', 'FrmTestModeController::maybe_add_test_mode_container', 99 );
}
$include_theme = FrmAppHelper::get_param( 'theme', '', 'get', 'absint' );
if ( $include_theme ) {
self::set_preview_query();
self::load_theme_preview();
} else {
self::load_direct_preview();
}
wp_die();
}
/**
* Set the page global to any available page (except for the Blog Page).
*
* @return void
*/
private static function set_preview_query() {
$page_query = array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'ASC',
'post_type' => 'page',
);
$page_for_posts = get_option( 'page_for_posts' );
if ( is_numeric( $page_for_posts ) ) {
// Avoid querying for the "Posts Page" or "Blog Page" so we don't display 10 forms.
$page_query['post__not_in'] = array( $page_for_posts );
}
$random_page = get_posts( $page_query );
if ( ! $random_page ) {
return;
}
$random_page = reset( $random_page );
if ( ! is_a( $random_page, 'WP_Post' ) ) {
// The return type can also be int.
return;
}
query_posts(
array(
'post_type' => 'page',
'page_id' => $random_page->ID,
)
);
// Fixes Pro issue #3004. Prevent an undefined $post object.
// Otherwise WordPress themes will trigger a warning "Attempt to read property "comment_count" on null".
self::set_post_global( $random_page );
}
/**
* Set the WP $post global object. Used for in-theme preview when defining a page.
*
* @since 5.5.2
*
* @param WP_Post $page The page object.
*
* @return void
*/
private static function set_post_global( $page ) {
global $post;
$post = $page; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
}
/**
* @since 3.0
*
* @return void
*/
private static function load_theme_preview() {
add_filter( 'wp_title', 'FrmFormsController::preview_title', 9999 );
add_filter( 'the_title', 'FrmFormsController::preview_page_title', 9999 );
add_filter( 'the_content', 'FrmFormsController::preview_content', 9999 );
add_action( 'loop_no_results', 'FrmFormsController::show_page_preview' );
add_filter( 'is_active_sidebar', '__return_false' );
FrmStylesController::enqueue_css( 'enqueue', true );
if ( false !== get_template_part( 'page' ) ) {
return;
}
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
add_filter( 'body_class', 'FrmFormsController::preview_block_theme_body_classnames' );
}
self::fallback_when_page_template_part_is_not_supported_by_theme();
}
/**
* Add padding to the body for block themes.
*
* @since 6.5.2
*
* @param array $classes The body classes list.
*
* @return array
*/
public static function preview_block_theme_body_classnames( $classes ) {
$classes[] = 'has-global-padding';
return $classes;
}
/**
* Not every theme supports get_template_part( 'page' ).
* When this is not supported, false is returned, and we can handle a fallback.
*
* @return void
*/
private static function fallback_when_page_template_part_is_not_supported_by_theme() {
if ( ! have_posts() ) {
return;
}
the_post();
self::get_template( 'header' );
// Add some generic class names to the container to add some natural padding to the content.
// .entry-content catches the WordPress TwentyTwenty theme.
// .container catches Customizr content.
echo '<div class="container entry-content">';
the_content();
echo '</div>';
// Prevent extra unexpected forms in the footer.
// For some reason this happens in the Twenty Twenty Five theme.
add_filter( 'frm_filter_final_form', '__return_empty_string' );
self::get_template( 'footer' );
}
/**
* Calls core function to get a template part if it doesn't cause deprecation warnings. Otherwise skips the deprecation function call
* and renders required html fragments calling required functions.
*
* @since 6.7.1
*
* @param string $template
*
* @return void
*/
private static function get_template( $template ) {
if ( self::should_try_getting_template( $template ) ) {
call_user_func( 'get_' . $template );
return;
}
if ( 'header' === $template ) {
include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/preview/header.php';
return;
}
if ( 'footer' === $template ) {
include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/preview/footer.php';
}
}
/**
* Returns true if calling a template function doesn't trigger deprecation warnings.
*
* @since 6.7.1
*
* @param string $template
*
* @return bool
*/
private static function should_try_getting_template( $template ) {
$stylesheet_path = get_stylesheet_directory();
$template_path = get_template_directory();
$is_child_theme = $stylesheet_path !== $template_path;
$template_name = $template . '.php';
return file_exists( $stylesheet_path . '/' . $template_name ) || $is_child_theme && file_exists( $template_path . '/' . $template_name );
}
/**
* Set the page title for the theme preview page
*
* @since 3.0
*
* @param string $title
*
* @return string
*/
public static function preview_page_title( $title ) {
return in_the_loop() ? self::preview_title( $title ) : $title;
}
/**
* Set the page title for the theme preview page.
*
* @since 3.0
*
* @param string $title
*
* @return string
*/
public static function preview_title( $title ) {
return __( 'Form Preview', 'formidable' );
}
/**
* Set the page content for the theme preview page.
*
* @since 3.0
*
* @param string $content
*
* @return string
*/
public static function preview_content( $content ) {
if ( in_the_loop() ) {
self::show_page_preview();
// Clear the content for the page we're using.
$content = '';
}
return $content;
}
/**
* @since 3.0
*/
private static function load_direct_preview() {
$key = FrmAppHelper::simple_get( 'form', 'sanitize_title' );
// phpcs:ignore Universal.Operators.StrictComparisons
if ( $key == '' ) {
$key = FrmAppHelper::get_post_param( 'form', '', 'sanitize_title' );
}
if ( ! $key ) {
$error = __( 'Form key is missing', 'formidable' );
wp_die(
'<h1>' . esc_html( $error ) . '</h1>',
'<p>' . esc_html( $error ) . '</p>',
404
);
}
self::maybe_block_preview( $key );
$form = FrmForm::getAll( array( 'form_key' => $key ), '', 1 );
if ( ! $form ) {
$error = __( 'Form does not exist', 'formidable' );
wp_die(
'<h1>' . esc_html( $error ) . '</h1>',
'<p>' . esc_html( $error ) . '</p>',
404
);
}
header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
self::fix_deprecated_null_param_warning();
require FrmAppHelper::plugin_path() . '/classes/views/frm-entries/direct.php';
}
/**
* Block the form preview for the contact form by default if the user cannot view forms.
* This is to prevent the contact form being exploited.
* Since this form is added by default and every site uses the same key, it is too easy
* to guess this form.
*
* @since 6.20
*
* @param string $form_key Form key.
*
* @return void
*/
public static function maybe_block_preview( $form_key ) {
if ( ! FrmFormsHelper::should_block_preview( $form_key ) ) {
return;
}
$error = __( 'You do not have permission to view this form', 'formidable' );
wp_die(
'<h1>' . esc_html( $error ) . '</h1>',
'<p>' . esc_html( $error ) . '</p>',
403
);
}
/**
* Some themes have a null $src value.
* This function adds a filter to ensure that $src is not null.
* WP will call str_starts_with with the null value triggering a deprecated message otherwise.
*
* @since 6.10
*
* @return void
*/
private static function fix_deprecated_null_param_warning() {
add_filter(
'script_loader_src',
/**
* @param string|null $src
*
* @return string
*/
function ( $src ) {
return is_null( $src ) ? '' : $src;
}
);
}
public static function untrash() {
self::change_form_status( 'untrash' );
}
/**
* @param array $ids
*
* @return string
*/
public static function bulk_untrash( $ids ) {
FrmAppHelper::permission_check( 'frm_edit_forms' );
$count = FrmForm::set_status( $ids, 'published' );
if ( ! $count ) {
// Don't show "0 forms restored" on refresh.
return '';
}
/* translators: %1$s: Number of forms */
return sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
}
/**
* @since 3.06
*/
public static function ajax_trash() {
FrmAppHelper::permission_check( 'frm_delete_forms' );
check_ajax_referer( 'frm_ajax', 'nonce' );
$form_id = FrmAppHelper::get_param( 'id', '', 'post', 'absint' );
FrmForm::set_status( $form_id, 'trash' );
wp_die();
}
public static function trash() {
self::change_form_status( 'trash' );
}
/**
* @param string $status
*
* @return void
*/
public static function change_form_status( $status ) {
$available_status = array(
'untrash' => array(
'permission' => 'frm_edit_forms',
'new_status' => 'published',
),
'trash' => array(
'permission' => 'frm_delete_forms',
'new_status' => 'trash',
),
);
if ( ! isset( $available_status[ $status ] ) ) {
return;
}
FrmAppHelper::permission_check( $available_status[ $status ]['permission'] );
$params = FrmForm::list_page_params();
// Check nonce url.
check_admin_referer( $status . '_form_' . $params['id'] );
$count = 0;
if ( FrmForm::set_status( $params['id'], $available_status[ $status ]['new_status'] ) ) {
++$count;
}
$form_type = FrmAppHelper::get_simple_request(
array(
'param' => 'form_type',
'type' => 'request',
)
);
/* translators: %1$s: Number of forms */
$available_status['untrash']['message'] = sprintf( _n( '%1$s form restored from the Trash.', '%1$s forms restored from the Trash.', $count, 'formidable' ), $count );
/* translators: %1$s: Number of forms, %2$s: Start link HTML, %3$s: End link HTML */
$available_status['trash']['message'] = sprintf( _n( '%1$s form moved to the Trash. %2$sUndo%3$s', '%1$s forms moved to the Trash. %2$sUndo%3$s', $count, 'formidable' ), $count, '<a href="' . esc_url( wp_nonce_url( '?page=formidable&frm_action=untrash&form_type=' . $form_type . '&id=' . $params['id'], 'untrash_form_' . $params['id'] ) ) . '">', '</a>' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
$message = $available_status[ $status ]['message'];
self::display_forms_list( $params, $message );
}
/**
* @param array $ids
*
* @return string
*/
public static function bulk_trash( $ids ) {
FrmAppHelper::permission_check( 'frm_delete_forms' );
$count = 0;
foreach ( $ids as $id ) {
if ( FrmForm::trash( $id ) ) {
++$count;
}
}
if ( ! $count ) {
return '';
}
$current_page = FrmAppHelper::get_simple_request(
array(
'param' => 'form_type',
'type' => 'request',
)
);
return sprintf(
/* translators: %1$s: Number of forms, %2$s: Start link HTML, %3$s: End link HTML */
_n( '%1$s form moved to the Trash. %2$sUndo%3$s', '%1$s forms moved to the Trash. %2$sUndo%3$s', $count, 'formidable' ),
$count,
'<a href="' . esc_url( wp_nonce_url( '?page=formidable&frm_action=list&action=bulk_untrash&form_type=' . $current_page . '&item-action=' . implode( ',', $ids ), 'bulk-toplevel_page_formidable' ) ) . '">', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
'</a>'
);
}
public static function destroy() {
FrmAppHelper::permission_check( 'frm_delete_forms' );
$params = FrmForm::list_page_params();
// Check nonce url.
check_admin_referer( 'destroy_form_' . $params['id'] );
$count = 0;
if ( FrmForm::destroy( $params['id'] ) ) {
++$count;
}
/* translators: %1$s: Number of forms */
$message = sprintf( _n( '%1$s Form Permanently Deleted', '%1$s Forms Permanently Deleted', $count, 'formidable' ), $count );
self::display_forms_list( $params, $message );
}
/**
* @param array $ids
*