PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.4.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.4.2
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / post-types.php
post-types.php
855 lines 24.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Types Class file.
4 *
5 * @package sureforms.
6 * @since 0.0.1
7 */
8
9 namespace SRFM\Inc;
10
11 use SRFM\Inc\Database\Tables\Entries;
12 use SRFM\Inc\Traits\Get_Instance;
13 use WP_Admin_Bar;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Post Types Main Class.
21 *
22 * @since 0.0.1
23 */
24 class Post_Types {
25 use Get_Instance;
26
27 /**
28 * Constructor
29 *
30 * @since 0.0.1
31 */
32 public function __construct() {
33 $this->restrict_unwanted_insertions();
34 add_action( 'init', [ $this, 'register_post_types' ] );
35 add_action( 'init', [ $this, 'register_post_metas' ] );
36 add_filter( 'manage_sureforms_form_posts_columns', [ $this, 'custom_form_columns' ] );
37 add_action( 'manage_sureforms_form_posts_custom_column', [ $this, 'custom_form_column_data' ], 10, 2 );
38 add_shortcode( 'sureforms', [ $this, 'forms_shortcode' ] );
39 add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_form_state' ] );
40 add_action( 'in_admin_header', [ $this, 'embed_page_header' ] );
41 add_filter( 'post_row_actions', [ $this, 'modify_entries_list_row_actions' ], 10, 2 );
42 add_filter( 'bulk_actions-edit-sureforms_form', [ $this, 'register_modify_bulk_actions' ], 99 );
43 add_action( 'admin_notices', [ $this, 'import_form_popup' ] );
44 add_action( 'admin_bar_menu', [ $this, 'remove_admin_bar_menu_item' ], 80, 1 );
45 add_action( 'template_redirect', [ $this, 'srfm_instant_form_redirect' ] );
46 add_action( 'template_redirect', [ $this, 'disable_sureforms_archive_page' ], 9 );
47 }
48
49 /**
50 * Add SureForms menu.
51 *
52 * @param string $title Parent slug.
53 * @param string $subtitle Parent slug.
54 * @param string $image Parent slug.
55 * @param string $button_text Parent slug.
56 * @param string $button_url Parent slug.
57 * @param string $after_button After button content.
58 * @return void
59 * @since 0.0.1
60 */
61 public function get_blank_page_markup( $title, $subtitle, $image, $button_text = '', $button_url = '', $after_button = '' ) {
62 echo '<div class="sureform-add-new-form">';
63
64 echo '<p class="sureform-blank-page-title">' . esc_html( $title ) . '</p>';
65
66 echo '<p class="sureform-blank-page-subtitle">' . esc_html( $subtitle ) . '</p>';
67
68 echo '<img src="' . esc_url( SRFM_URL . '/images/' . $image . '.svg' ) . '">';
69
70 if ( ! empty( $button_text ) && ! empty( $button_url ) ) {
71 echo '<div class="sureforms-add-new-form-container"><a class="sf-add-new-form-button" href="' . esc_url( $button_url ) . '"><div class="button-secondary">' . esc_html( $button_text ) . '</div></a>' . wp_kses_post( $after_button ) . '</div>';
72 }
73 echo '</div>';
74 }
75
76 /**
77 * Render blank state for add new form screen.
78 *
79 * @param string $post_type Post type.
80 * @return void
81 * @since 0.0.1
82 */
83 public function sureforms_render_blank_state( $post_type ) {
84
85 if ( SRFM_FORMS_POST_TYPE === $post_type ) {
86 $page_name = 'add-new-form';
87 $new_form_url = admin_url( 'admin.php?page=' . $page_name );
88 $import_button = '<button class="button button-secondary srfm-import-btn">' . __( 'Import Form', 'sureforms' ) . '</button>';
89
90 $this->get_blank_page_markup(
91 esc_html__( 'Let’s build your first form', 'sureforms' ),
92 esc_html__(
93 'Craft beautiful and functional forms in minutes',
94 'sureforms'
95 ),
96 'add-new-form',
97 esc_html__( 'Add New Form', 'sureforms' ),
98 $new_form_url,
99 $import_button
100 );
101 }
102
103 if ( SRFM_ENTRIES === $post_type ) {
104
105 $this->get_blank_page_markup(
106 esc_html__( 'No records found', 'sureforms' ),
107 esc_html__(
108 'This is where your form entries will appear',
109 'sureforms'
110 ),
111 'blank-entries'
112 );
113 }
114 }
115
116 /**
117 * Registers the forms and submissions post types.
118 *
119 * @return void
120 * @since 0.0.1
121 */
122 public function register_post_types() {
123 $form_labels = [
124 'name' => _x( 'Forms', 'post type general name', 'sureforms' ),
125 'singular_name' => _x( 'Form', 'post type singular name', 'sureforms' ),
126 'menu_name' => _x( 'Forms', 'admin menu', 'sureforms' ),
127 'add_new' => _x( 'Add New', 'form', 'sureforms' ),
128 'add_new_item' => __( 'Add New Form', 'sureforms' ),
129 'new_item' => __( 'New Form', 'sureforms' ),
130 'edit_item' => __( 'Edit Form', 'sureforms' ),
131 'view_item' => __( 'View Form', 'sureforms' ),
132 'view_items' => __( 'View Forms', 'sureforms' ),
133 'all_items' => __( 'Forms', 'sureforms' ),
134 'search_items' => __( 'Search Forms', 'sureforms' ),
135 'parent_item_colon' => __( 'Parent Forms:', 'sureforms' ),
136 'not_found' => __( 'No forms found.', 'sureforms' ),
137 'not_found_in_trash' => __( 'No forms found in Trash.', 'sureforms' ),
138 'item_published' => __( 'Form published.', 'sureforms' ),
139 'item_updated' => __( 'Form updated.', 'sureforms' ),
140 ];
141 register_post_type(
142 SRFM_FORMS_POST_TYPE,
143 [
144 'labels' => $form_labels,
145 'rewrite' => [ 'slug' => 'form' ],
146 'public' => true,
147 'show_in_rest' => true,
148 'has_archive' => true,
149 'show_ui' => true,
150 'supports' => [ 'title', 'author', 'editor', 'custom-fields' ],
151 'show_in_menu' => 'sureforms_menu',
152 'show_in_nav_menus' => true,
153 ]
154 );
155 // will be used later.
156 // register_post_status(
157 // 'unread',
158 // array(
159 // 'label' => _x( 'Unread', 'sureforms', 'sureforms' ),
160 // 'public' => true,
161 // 'exclude_from_search' => false,
162 // 'show_in_admin_all_list' => true,
163 // 'show_in_admin_status_list' => true,
164 // Translators: %s is the number of unread items.
165 // 'label_count' => _n_noop( 'Unread (%s)', 'Unread (%s)', 'sureforms' ),
166 // )
167 // );.
168 }
169
170 /**
171 * Redirects requests for SureForms archieve page to homeurl
172 *
173 * @since 1.4.0
174 * @return void
175 */
176 public function disable_sureforms_archive_page() {
177 if ( is_post_type_archive( SRFM_FORMS_POST_TYPE ) ) {
178 wp_safe_redirect( home_url(), 301 );
179 exit;
180 }
181 }
182
183 /**
184 * Remove add new form menu item.
185 *
186 * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
187 *
188 * @return void
189 * @since 0.0.1
190 */
191 public function remove_admin_bar_menu_item( $wp_admin_bar ) {
192 $wp_admin_bar->remove_node( 'new-sureforms_form' );
193 }
194
195 /**
196 * Modify list row actions.
197 *
198 * @param array<mixed> $actions An array of row action links.
199 * @param \WP_Post $post The current WP_Post object.
200 *
201 * @return array<mixed> $actions Modified row action links.
202 * @since 0.0.1
203 */
204 public function modify_entries_list_row_actions( $actions, $post ) {
205 if ( 'sureforms_form' === $post->post_type ) {
206 $actions['export'] = '<a href="#" onclick="exportForm(' . $post->ID . ')">' . __( 'Export', 'sureforms' ) . '</a>';
207 }
208
209 return $actions;
210 }
211
212 /**
213 * Modify list bulk actions.
214 *
215 * @param array<mixed> $bulk_actions An array of bulk action links.
216 * @since 0.0.1
217 * @return array<mixed> $bulk_actions Modified action links.
218 */
219 public function register_modify_bulk_actions( $bulk_actions ) {
220
221 $white_listed_actions = [
222 'edit',
223 'trash',
224 'delete',
225 'untrash',
226 ];
227
228 // remove all actions except white listed actions.
229 $bulk_actions = array_intersect_key( $bulk_actions, array_flip( $white_listed_actions ) );
230
231 // Add export action only if edit and trash actions are present in bulk actions.
232 if ( isset( $bulk_actions['edit'] ) && isset( $bulk_actions['trash'] ) ) {
233 $bulk_actions['export'] = __( 'Export', 'sureforms' );
234 }
235
236 return $bulk_actions;
237 }
238
239 /**
240 * Show blank slate styles.
241 *
242 * @return void
243 * @since 0.0.1
244 */
245 public function get_blank_state_styles() {
246 echo '<style type="text/css">.sf-add-new-form-button:focus { box-shadow:none !important; outline:none !important; } #posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions, .wrap .subsubsub { display: none; } #posts-filter .tablenav.bottom { height: auto; } .sureform-add-new-form{ display: flex; flex-direction: column; gap: 8px; justify-content: center; align-items: center; padding: 24px 0 24px 0; } .sureform-blank-page-title { color: var(--dashboard-heading); font-family: Inter; font-size: 22px; font-style: normal; font-weight: 600; line-height: 28px; margin: 0; } .sureform-blank-page-subtitle { color: var(--dashboard-text); margin: 0; font-family: Inter; font-size: 14px; font-style: normal; font-weight: 400; line-height: 16px; }</style>';
247 }
248
249 /**
250 * Show blank slate.
251 *
252 * @param string $which String which tablenav is being shown.
253 * @return void
254 * @since 0.0.1
255 */
256 public function maybe_render_blank_form_state( $which ) {
257 $screen = get_current_screen();
258 $post_type = $screen ? $screen->post_type : '';
259
260 if ( SRFM_FORMS_POST_TYPE === $post_type && 'bottom' === $which ) {
261
262 $counts = (array) wp_count_posts( SRFM_FORMS_POST_TYPE );
263 unset( $counts['auto-draft'] );
264 $count = array_sum( $counts );
265
266 if ( 0 < $count ) {
267 return;
268 }
269
270 $this->sureforms_render_blank_state( $post_type );
271
272 $this->get_blank_state_styles();
273
274 }
275 }
276
277 /**
278 * Set up a div for the header to render into it.
279 *
280 * @return void
281 * @since 0.0.1
282 */
283 public static function embed_page_header() {
284 $screen = get_current_screen();
285 $screen_id = $screen ? $screen->id : '';
286
287 $is_screen_sureforms_entries = Helper::validate_request_context( SRFM_ENTRIES, 'page' );
288
289 if ( 'edit-' . SRFM_FORMS_POST_TYPE === $screen_id || $is_screen_sureforms_entries ) {
290 ?>
291 <style>
292 .srfm-page-header {
293 min-height: 65px;
294 @media screen and ( max-width: 600px ) {
295 padding-top: 46px;
296 }
297 }
298 </style>
299 <div id="srfm-page-header" class="srfm-page-header">
300 <div class="srfm-page-pre-nav-content"></div>
301 </div>
302 <?php
303 }
304 }
305
306 /**
307 * Registers the sureforms metas.
308 *
309 * @return void
310 * @since 0.0.1
311 */
312 public function register_post_metas() {
313 $check_icon = 'data:image/svg+xml;base64,' . base64_encode( strval( file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/check-icon.svg' ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
314
315 $metas = apply_filters(
316 'srfm_register_post_meta',
317 [
318 // General tab metas.
319 '_srfm_use_label_as_placeholder' => 'boolean',
320 '_srfm_submit_button_text' => 'string',
321 '_srfm_is_inline_button' => 'boolean',
322 // Submit Button.
323 '_srfm_submit_width_backend' => 'string',
324 '_srfm_button_border_radius' => 'integer',
325 '_srfm_submit_alignment' => 'string',
326 '_srfm_submit_alignment_backend' => 'string',
327 '_srfm_submit_width' => 'string',
328 '_srfm_inherit_theme_button' => 'boolean',
329 // Additional Classes.
330 '_srfm_additional_classes' => 'string',
331
332 // Advanced tab metas.
333 // Success Message.
334 '_srfm_submit_type' => 'string',
335 // Security.
336 '_srfm_captcha_security_type' => 'string',
337 '_srfm_form_recaptcha' => 'string',
338 // post meta to store if the form is AI generated.
339 '_srfm_is_ai_generated' => 'boolean',
340 ]
341 );
342
343 // Form Custom CSS meta.
344 register_post_meta(
345 'sureforms_form',
346 '_srfm_form_custom_css',
347 [
348 'show_in_rest' => true,
349 'type' => 'string',
350 'single' => true,
351 'auth_callback' => static function() {
352 return current_user_can( 'edit_posts' );
353 },
354 'sanitize_callback' => static function( $meta_value ) {
355 return wp_kses_post( $meta_value );
356 },
357 ]
358 );
359
360 foreach ( $metas as $meta => $type ) {
361 register_meta(
362 'post',
363 $meta,
364 [
365 'object_subtype' => SRFM_FORMS_POST_TYPE,
366 'show_in_rest' => true,
367 'single' => true,
368 'type' => $type,
369 'sanitize_callback' => 'sanitize_text_field',
370 'auth_callback' => static function() {
371 return current_user_can( 'edit_posts' );
372 },
373 ]
374 );
375 }
376
377 // Registers meta to handle values associated with form styling.
378 register_post_meta(
379 SRFM_FORMS_POST_TYPE,
380 '_srfm_instant_form_settings',
381 [
382 'single' => true,
383 'type' => 'object',
384 'auth_callback' => '__return_true',
385 'show_in_rest' => [
386 'schema' => [
387 'type' => 'object',
388 'properties' => [
389 'site_logo' => [
390 'type' => 'string',
391 ],
392 'site_logo_id' => [
393 'type' => 'integer',
394 ],
395 // Form page banner settings.
396 'cover_type' => [
397 'type' => 'string',
398 ],
399 'cover_color' => [
400 'type' => 'string',
401 ],
402 'cover_image' => [
403 'type' => 'string',
404 ],
405 'cover_image_id' => [
406 'type' => 'integer',
407 ],
408 // Form page background settings.
409 'bg_type' => [
410 'type' => 'string',
411 ],
412 'bg_color' => [
413 'type' => 'string',
414 ],
415 'bg_image' => [
416 'type' => 'string',
417 ],
418 'bg_image_id' => [
419 'type' => 'integer',
420 ],
421 'enable_instant_form' => [
422 'type' => 'boolean',
423 ],
424 'form_container_width' => [
425 'type' => 'integer',
426 ],
427 'single_page_form_title' => [
428 'type' => 'boolean',
429 ],
430 'use_banner_as_page_background' => [
431 'type' => 'boolean',
432 ],
433 ],
434 ],
435 ],
436 'default' => [
437 'bg_type' => 'color',
438 'bg_color' => '#ffffff',
439 'bg_image' => '',
440 'site_logo' => '',
441 'cover_type' => 'color',
442 'cover_color' => '#111C44',
443 'cover_image' => '',
444 'enable_instant_form' => false,
445 'form_container_width' => 620,
446 'single_page_form_title' => true,
447 'use_banner_as_page_background' => false,
448 ],
449 ]
450 );
451
452 register_post_meta(
453 SRFM_FORMS_POST_TYPE,
454 '_srfm_forms_styling',
455 [
456 'single' => true,
457 'type' => 'object',
458 'auth_callback' => '__return_true',
459 'show_in_rest' => [
460 'schema' => [
461 'type' => 'object',
462 'properties' => [
463 'primary_color' => [
464 'type' => 'string',
465 ],
466 'text_color' => [
467 'type' => 'string',
468 ],
469 'text_color_on_primary' => [
470 'type' => 'string',
471 ],
472 'field_spacing' => [
473 'type' => 'string',
474 ],
475 'submit_button_alignment' => [
476 'type' => 'string',
477 ],
478 ],
479 ],
480 ],
481 'default' => [
482 'primary_color' => '#111C44',
483 'text_color' => '#1E1E1E',
484 'text_color_on_primary' => '#FFFFFF',
485 'field_spacing' => 'medium',
486 'submit_button_alignment' => 'left',
487 ],
488 ]
489 );
490
491 // Email notification Metas.
492 register_post_meta(
493 'sureforms_form',
494 '_srfm_email_notification',
495 [
496 'single' => true,
497 'type' => 'array',
498 'auth_callback' => '__return_true',
499 'show_in_rest' => [
500 'schema' => [
501 'type' => 'array',
502 'items' => [
503 'type' => 'object',
504 'properties' => [
505 'id' => [
506 'type' => 'integer',
507 ],
508 'status' => [
509 'type' => 'boolean',
510 ],
511 'is_raw_format' => [
512 'type' => 'boolean',
513 ],
514 'name' => [
515 'type' => 'string',
516 ],
517 'email_to' => [
518 'type' => 'string',
519 ],
520 'email_reply_to' => [
521 'type' => 'string',
522 ],
523 'email_cc' => [
524 'type' => 'string',
525 ],
526 'email_bcc' => [
527 'type' => 'string',
528 ],
529 'subject' => [
530 'type' => 'string',
531 ],
532 'email_body' => [
533 'type' => 'string',
534 ],
535 ],
536 ],
537 ],
538 ],
539 'default' => [
540 [
541 'id' => 1,
542 'status' => true,
543 'is_raw_format' => false,
544 'name' => __( 'Admin Notification Email', 'sureforms' ),
545 'email_to' => '{admin_email}',
546 'email_reply_to' => '{admin_email}',
547 'email_cc' => '{admin_email}',
548 'email_bcc' => '{admin_email}',
549 'subject' => sprintf( /* translators: %s: Form title smart tag */ __( 'New Form Submission - %s', 'sureforms' ), '{form_title}' ),
550 'email_body' => '{all_data}',
551 ],
552 ],
553 ]
554 );
555
556 // Compliance Settings metas.
557 register_post_meta(
558 'sureforms_form',
559 '_srfm_compliance',
560 [
561 'single' => true,
562 'type' => 'array',
563 'auth_callback' => '__return_true',
564 'show_in_rest' => [
565 'schema' => [
566 'type' => 'array',
567 'items' => [
568 'type' => 'object',
569 'properties' => [
570 'id' => [
571 'type' => 'string',
572 ],
573 'gdpr' => [
574 'type' => 'boolean',
575 ],
576 'do_not_store_entries' => [
577 'type' => 'boolean',
578 ],
579 'auto_delete_entries' => [
580 'type' => 'boolean',
581 ],
582 'auto_delete_days' => [
583 'type' => 'string',
584 ],
585 ],
586 ],
587 ],
588 ],
589 'default' => [
590 [
591 'id' => 'gdpr',
592 'gdpr' => false,
593 'do_not_store_entries' => false,
594 'auto_delete_entries' => false,
595 'auto_delete_days' => '',
596 ],
597 ],
598 ]
599 );
600
601 // form confirmation.
602 register_post_meta(
603 'sureforms_form',
604 '_srfm_form_confirmation',
605 [
606 'single' => true,
607 'type' => 'array',
608 'auth_callback' => '__return_true',
609 'show_in_rest' => [
610 'schema' => [
611 'type' => 'array',
612 'items' => [
613 'type' => 'object',
614 'properties' => [
615 'id' => [
616 'type' => 'integer',
617 ],
618 'confirmation_type' => [
619 'type' => 'string',
620 ],
621 'page_url' => [
622 'type' => 'string',
623 ],
624 'custom_url' => [
625 'type' => 'string',
626 ],
627 'message' => [
628 'type' => 'string',
629 ],
630 'submission_action' => [
631 'type' => 'string',
632 ],
633 'enable_query_params' => [
634 'type' => 'boolean',
635 ],
636 'query_params' => [
637 'type' => 'array',
638 ],
639 ],
640 ],
641 ],
642 ],
643 'default' => [
644 [
645 'id' => 1,
646 'confirmation_type' => 'same page',
647 'page_url' => '',
648 'custom_url' => '',
649 'message' => '<p style="text-align: center;"><img src="' . esc_attr( $check_icon ) . '" alt="" aria-hidden="true"></img></p><h2 style="text-align: center;">' . esc_html__( 'Thank you', 'sureforms' ) . '</h2>',
650 'submission_action' => 'hide form',
651 ],
652 ],
653 ]
654 );
655
656 // conditional logic.
657 do_action( 'srfm_register_conditional_logic_post_meta' );
658 /**
659 * Hook for registering additional Post Meta
660 */
661 do_action( 'srfm_register_additional_post_meta' );
662 }
663
664 /**
665 * Custom Shortcode.
666 *
667 * @param array<mixed> $atts Attributes.
668 * @return string|false. $content Post Content.
669 * @since 0.0.1
670 */
671 public function forms_shortcode( array $atts ) {
672 $atts = shortcode_atts(
673 [
674 'id' => '',
675 'show_title' => true,
676 ],
677 $atts
678 );
679
680 $id = intval( $atts['id'] );
681 $post = get_post( $id );
682
683 if ( ! empty( $id ) && $post ) {
684 return Generate_Form_Markup::get_form_markup( $id, ! filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN ), '', 'post', true );
685 }
686
687 return '';
688 }
689
690 /**
691 * Add custom column header.
692 *
693 * @param array<mixed> $columns Attributes.
694 * @return array<mixed> $columns Post Content.
695 * @since 0.0.1
696 */
697 public function custom_form_columns( $columns ) {
698 return [
699 'cb' => $columns['cb'],
700 'title' => $columns['title'],
701 'sureforms' => __( 'Shortcode', 'sureforms' ),
702 'entries' => __( 'Entries', 'sureforms' ),
703 'author' => $columns['author'],
704 'date' => $columns['date'],
705 ];
706 }
707
708 /**
709 * Populate custom column with data.
710 *
711 * @param string $column Attributes.
712 * @param int $post_id Attributes.
713 * @return void
714 * @since 0.0.1
715 */
716 public function custom_form_column_data( $column, $post_id ) {
717 if ( 'sureforms' === $column ) {
718 ob_start();
719 ?>
720 <div class="srfm-shortcode-container">
721 <input id="srfm-shortcode-input-<?php echo esc_attr( Helper::get_string_value( $post_id ) ); ?>" class="srfm-shortcode-input" type="text" readonly value="[sureforms id='<?php echo esc_attr( Helper::get_string_value( $post_id ) ); ?>']" />
722 <button type="button" class="components-button components-clipboard-button has-icon srfm-shortcode" onclick="handleFormShortcode(this)">
723 <span id="srfm-copy-icon" class="dashicon dashicons dashicons-admin-page"></span>
724 </button>
725 </div>
726 <?php
727 ob_end_flush();
728 }
729 if ( 'entries' === $column ) {
730 // Entries URL to redirect user based on the form ID.
731 $entries_url = wp_nonce_url(
732 add_query_arg(
733 [
734 'form_filter' => $post_id,
735 ],
736 admin_url( 'admin.php?page=sureforms_entries' )
737 ),
738 'srfm_entries_action'
739 );
740
741 // Get the entry count for the form.
742 $entries_count = Entries::get_total_entries_by_status( 'all', $post_id );
743
744 ob_start();
745 ?>
746 <p class="srfm-entries-number"><a href="<?php echo esc_url( $entries_url ); ?>"><?php echo esc_html( Helper::get_string_value( $entries_count ) ); ?></a></p>
747 <?php
748 ob_end_flush();
749 }
750 }
751
752 /**
753 * Show the import form popup
754 *
755 * @since 0.0.1
756 * @return void
757 */
758 public function import_form_popup() {
759 $screen = get_current_screen();
760 $id = $screen ? $screen->id : '';
761 if ( 'edit-sureforms_form' === $id ) {
762 ?>
763 <div class="srfm-import-plugin-wrap">
764 <div class="srfm-import-wrap">
765 <p class="srfm-import-help"><?php echo esc_html__( 'Please choose the SureForms export file (.json) that you wish to import.', 'sureforms' ); ?></p>
766 <form method="post" enctype="multipart/form-data" class="srfm-import-form">
767 <input type="file" id="srfm-import-file" onchange="handleFileChange(event)" name="import form" accept=".json">
768 <input type="submit" name="import-form-submit" id="import-form-submit" class="srfm-import-button" value="<?php esc_attr_e( 'Import Now', 'sureforms' ); ?>" disabled>
769 </form>
770 <p id="srfm-import-error"><?php echo esc_html__( 'There is some error in json file, please export the SureForms Forms again.', 'sureforms' ); ?></p>
771 </div>
772 </div>
773 <?php
774 }
775 }
776
777 /**
778 * Redirect to home page if instant form is not enabled.
779 *
780 * @since 0.0.1
781 * @return void
782 */
783 public function srfm_instant_form_redirect() {
784
785 $form_id = Helper::get_integer_value( get_the_ID() );
786
787 $instant_form_settings = Helper::get_array_value( Helper::get_post_meta( $form_id, '_srfm_instant_form_settings' ) );
788 $enable_instant_form = ! empty( $instant_form_settings['enable_instant_form'] ) ? boolval( $instant_form_settings['enable_instant_form'] ) : false;
789
790 if ( $enable_instant_form ) {
791 return;
792 }
793
794 $form_preview = '';
795
796 $form_preview_attr = isset( $_GET['preview'] ) ? sanitize_text_field( wp_unslash( $_GET['preview'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not needed here.
797
798 if ( $form_preview_attr ) {
799 $form_preview = filter_var( $form_preview_attr, FILTER_VALIDATE_BOOLEAN );
800 }
801
802 if ( is_singular( 'sureforms_form' ) && ! $form_preview && ! current_user_can( 'manage_options' ) ) {
803 wp_safe_redirect( home_url() );
804 return;
805 }
806 }
807
808 /**
809 * Restrict RankMath meta boxes in edit page.
810 *
811 * @since 0.0.5
812 * @return void
813 */
814 public function restrict_data() {
815 add_filter( 'rank_math/excluded_post_types', [ $this, 'unset_sureforms_post_type' ] );
816 }
817
818 /**
819 * Remove SureForms post type from RankMath and Yoast.
820 *
821 * @param array<mixed> $post_types Post types.
822 * @since 0.0.5
823 * @return array<mixed> $post_types Modified post types.
824 */
825 public function unset_sureforms_post_type( $post_types ) {
826 return array_filter(
827 $post_types,
828 static function( $post_type ) {
829 if ( is_array( $post_type ) && isset( $post_type['name'] ) ) {
830 return SRFM_FORMS_POST_TYPE !== $post_type['name'];
831 }
832 return SRFM_FORMS_POST_TYPE !== $post_type;
833 }
834 );
835 }
836
837 /**
838 * Restrict interference of other plugins with SureForms.
839 *
840 * @since 0.0.5
841 * @return void
842 */
843 private function restrict_unwanted_insertions() {
844 // Restrict RankMath metaboxes in edit page.
845 add_action( 'cmb2_admin_init', [ $this, 'restrict_data' ] );
846
847 // Restrict Yoast columns.
848 add_filter( 'wpseo_accessible_post_types', [ $this, 'unset_sureforms_post_type' ] );
849 add_filter( 'wpseo_metabox_prio', '__return_false' );
850
851 // Restrict AIOSEO columns.
852 add_filter( 'aioseo_public_post_types', [ $this, 'unset_sureforms_post_type' ] );
853 }
854 }
855