PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.5.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.5.1
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
1,034 lines 30.0 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 'bg_type' => [
479 'type' => 'string',
480 ],
481 'bg_color' => [
482 'type' => 'string',
483 ],
484 'bg_image' => [
485 'type' => 'string',
486 ],
487 'bg_image_id' => [
488 'type' => 'integer',
489 ],
490 // Image Properties.
491 'bg_image_position' => [
492 'type' => 'object',
493 'properties' => [
494 'x' => [
495 'type' => 'number',
496 'format' => 'float',
497 ],
498 'y' => [
499 'type' => 'number',
500 'format' => 'float',
501 ],
502 ],
503 ],
504 'bg_image_attachment' => [
505 'type' => 'string',
506 ],
507 'bg_image_repeat' => [
508 'type' => 'string',
509 ],
510 'bg_image_size' => [
511 'type' => 'string',
512 ],
513 'bg_image_size_custom' => [
514 'type' => 'integer',
515 ],
516 'bg_image_size_custom_unit' => [
517 'type' => 'string',
518 ],
519 // Gradient Properties.
520 'bg_gradient' => [
521 'type' => 'string',
522 ],
523 'gradient_type' => [
524 'type' => 'string',
525 ],
526 'bg_gradient_type' => [
527 'type' => 'string',
528 ],
529 'bg_gradient_color_1' => [
530 'type' => 'string',
531 ],
532 'bg_gradient_color_2' => [
533 'type' => 'string',
534 ],
535 'bg_gradient_angle' => [
536 'type' => 'integer',
537 ],
538 'bg_gradient_location_1' => [
539 'type' => 'integer',
540 ],
541 'bg_gradient_location_2' => [
542 'type' => 'integer',
543 ],
544 // Overlay Properties.
545 'bg_overlay_size' => [
546 'type' => 'string',
547 ],
548 'bg_gradient_overlay_type' => [
549 'type' => 'string',
550 ],
551 'bg_overlay_opacity' => [
552 'type' => 'number',
553 ],
554 'bg_overlay_image' => [
555 'type' => 'string',
556 ],
557 'bg_overlay_image_id' => [
558 'type' => 'integer',
559 ],
560 'bg_image_overlay_color' => [
561 'type' => 'string',
562 ],
563 'bg_overlay_custom_size_unit' => [
564 'type' => 'string',
565 ],
566 'bg_overlay_custom_size' => [
567 'type' => 'integer',
568 ],
569 'bg_overlay_blend_mode' => [
570 'type' => 'string',
571 ],
572 'bg_overlay_position' => [
573 'type' => 'object',
574 'properties' => [
575 'x' => [
576 'type' => 'number',
577 'format' => 'float',
578 ],
579 'y' => [
580 'type' => 'number',
581 'format' => 'float',
582 ],
583 ],
584 ],
585 'bg_overlay_attachment' => [
586 'type' => 'string',
587 ],
588 'bg_overlay_repeat' => [
589 'type' => 'string',
590 ],
591 // Gradient Overlay Properties.
592 'bg_overlay_gradient' => [
593 'type' => 'string',
594 ],
595 'overlay_gradient_type' => [
596 'type' => 'string',
597 ],
598 'bg_overlay_gradient_type' => [
599 'type' => 'string',
600 ],
601 'bg_overlay_gradient_color_1' => [
602 'type' => 'string',
603 ],
604 'bg_overlay_gradient_color_2' => [
605 'type' => 'string',
606 ],
607 'bg_overlay_gradient_angle' => [
608 'type' => 'integer',
609 ],
610 'bg_overlay_gradient_location_1' => [
611 'type' => 'integer',
612 ],
613 'bg_overlay_gradient_location_2' => [
614 'type' => 'integer',
615 ],
616 ],
617 ],
618 ],
619 'default' => [
620 'primary_color' => '#111C44',
621 'text_color' => '#1E1E1E',
622 'text_color_on_primary' => '#FFFFFF',
623 'field_spacing' => 'medium',
624 'submit_button_alignment' => 'left',
625 'bg_type' => 'color',
626 'bg_color' => '#ffffff',
627 'bg_image' => '',
628 'bg_image_position' => [
629 'x' => 0.5,
630 'y' => 0.5,
631 ],
632 'bg_image_attachment' => 'scroll',
633 'bg_image_repeat' => 'no-repeat',
634 'bg_image_size' => 'cover',
635 'bg_image_size_custom' => 100, // Image width when set to custom.
636 'bg_image_size_custom_unit' => '%',
637 'gradient_type' => 'basic',
638 'bg_gradient_type' => 'linear',
639 'bg_gradient_color_1' => '#FFC9B2',
640 'bg_gradient_color_2' => '#C7CBFF',
641 'bg_gradient_angle' => 90,
642 'bg_gradient_location_1' => 0,
643 'bg_gradient_location_2' => 100,
644 'bg_overlay_size' => 'cover',
645 'bg_gradient_overlay_type' => '',
646 'bg_overlay_opacity' => 1,
647 'bg_overlay_image' => '',
648 'bg_image_overlay_color' => '#FFFFFF75',
649 'bg_overlay_custom_size_unit' => '%',
650 'bg_overlay_custom_size' => 100,
651 'bg_overlay_position' => [
652 'x' => 0.5,
653 'y' => 0.5,
654 ],
655 'bg_overlay_attachment' => 'scroll',
656 'bg_overlay_repeat' => 'no-repeat',
657 'bg_overlay_blend_mode' => 'normal',
658 // Gradient Overlay Properties.
659 'overlay_gradient_type' => 'basic',
660 'bg_overlay_gradient_type' => 'linear',
661 'bg_overlay_gradient_color_1' => '#FFC9B2',
662 'bg_overlay_gradient_color_2' => '#C7CBFF',
663 'bg_overlay_gradient_angle' => 90,
664 'bg_overlay_gradient_location_1' => 0,
665 'bg_overlay_gradient_location_2' => 100,
666 ],
667 ]
668 );
669
670 // Email notification Metas.
671 register_post_meta(
672 'sureforms_form',
673 '_srfm_email_notification',
674 [
675 'single' => true,
676 'type' => 'array',
677 'auth_callback' => '__return_true',
678 'show_in_rest' => [
679 'schema' => [
680 'type' => 'array',
681 'items' => [
682 'type' => 'object',
683 'properties' => [
684 'id' => [
685 'type' => 'integer',
686 ],
687 'status' => [
688 'type' => 'boolean',
689 ],
690 'is_raw_format' => [
691 'type' => 'boolean',
692 ],
693 'name' => [
694 'type' => 'string',
695 ],
696 'email_to' => [
697 'type' => 'string',
698 ],
699 'email_reply_to' => [
700 'type' => 'string',
701 ],
702 'email_cc' => [
703 'type' => 'string',
704 ],
705 'email_bcc' => [
706 'type' => 'string',
707 ],
708 'subject' => [
709 'type' => 'string',
710 ],
711 'email_body' => [
712 'type' => 'string',
713 ],
714 ],
715 ],
716 ],
717 ],
718 'default' => [
719 [
720 'id' => 1,
721 'status' => true,
722 'is_raw_format' => false,
723 'name' => __( 'Admin Notification Email', 'sureforms' ),
724 'email_to' => '{admin_email}',
725 'email_reply_to' => '{admin_email}',
726 'email_cc' => '{admin_email}',
727 'email_bcc' => '{admin_email}',
728 'subject' => sprintf( /* translators: %s: Form title smart tag */ __( 'New Form Submission - %s', 'sureforms' ), '{form_title}' ),
729 'email_body' => '{all_data}',
730 ],
731 ],
732 ]
733 );
734
735 // Compliance Settings metas.
736 register_post_meta(
737 'sureforms_form',
738 '_srfm_compliance',
739 [
740 'single' => true,
741 'type' => 'array',
742 'auth_callback' => '__return_true',
743 'show_in_rest' => [
744 'schema' => [
745 'type' => 'array',
746 'items' => [
747 'type' => 'object',
748 'properties' => [
749 'id' => [
750 'type' => 'string',
751 ],
752 'gdpr' => [
753 'type' => 'boolean',
754 ],
755 'do_not_store_entries' => [
756 'type' => 'boolean',
757 ],
758 'auto_delete_entries' => [
759 'type' => 'boolean',
760 ],
761 'auto_delete_days' => [
762 'type' => 'string',
763 ],
764 ],
765 ],
766 ],
767 ],
768 'default' => [
769 [
770 'id' => 'gdpr',
771 'gdpr' => false,
772 'do_not_store_entries' => false,
773 'auto_delete_entries' => false,
774 'auto_delete_days' => '',
775 ],
776 ],
777 ]
778 );
779
780 // form confirmation.
781 register_post_meta(
782 'sureforms_form',
783 '_srfm_form_confirmation',
784 [
785 'single' => true,
786 'type' => 'array',
787 'auth_callback' => '__return_true',
788 'show_in_rest' => [
789 'schema' => [
790 'type' => 'array',
791 'items' => [
792 'type' => 'object',
793 'properties' => [
794 'id' => [
795 'type' => 'integer',
796 ],
797 'confirmation_type' => [
798 'type' => 'string',
799 ],
800 'page_url' => [
801 'type' => 'string',
802 ],
803 'custom_url' => [
804 'type' => 'string',
805 ],
806 'message' => [
807 'type' => 'string',
808 ],
809 'submission_action' => [
810 'type' => 'string',
811 ],
812 'enable_query_params' => [
813 'type' => 'boolean',
814 ],
815 'query_params' => [
816 'type' => 'array',
817 ],
818 ],
819 ],
820 ],
821 ],
822 'default' => [
823 [
824 'id' => 1,
825 'confirmation_type' => 'same page',
826 'page_url' => '',
827 'custom_url' => '',
828 '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>',
829 'submission_action' => 'hide form',
830 ],
831 ],
832 ]
833 );
834
835 // conditional logic.
836 do_action( 'srfm_register_conditional_logic_post_meta' );
837 /**
838 * Hook for registering additional Post Meta
839 */
840 do_action( 'srfm_register_additional_post_meta' );
841 }
842
843 /**
844 * Custom Shortcode.
845 *
846 * @param array<mixed> $atts Attributes.
847 * @return string|false. $content Post Content.
848 * @since 0.0.1
849 */
850 public function forms_shortcode( array $atts ) {
851 $atts = shortcode_atts(
852 [
853 'id' => '',
854 'show_title' => true,
855 ],
856 $atts
857 );
858
859 $id = intval( $atts['id'] );
860 $post = get_post( $id );
861
862 if ( ! empty( $id ) && $post ) {
863 return Generate_Form_Markup::get_form_markup( $id, ! filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN ), '', 'post', true );
864 }
865
866 return '';
867 }
868
869 /**
870 * Add custom column header.
871 *
872 * @param array<mixed> $columns Attributes.
873 * @return array<mixed> $columns Post Content.
874 * @since 0.0.1
875 */
876 public function custom_form_columns( $columns ) {
877 return [
878 'cb' => $columns['cb'],
879 'title' => $columns['title'],
880 'sureforms' => __( 'Shortcode', 'sureforms' ),
881 'entries' => __( 'Entries', 'sureforms' ),
882 'author' => $columns['author'],
883 'date' => $columns['date'],
884 ];
885 }
886
887 /**
888 * Populate custom column with data.
889 *
890 * @param string $column Attributes.
891 * @param int $post_id Attributes.
892 * @return void
893 * @since 0.0.1
894 */
895 public function custom_form_column_data( $column, $post_id ) {
896 if ( 'sureforms' === $column ) {
897 ob_start();
898 ?>
899 <div class="srfm-shortcode-container">
900 <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 ) ); ?>']" />
901 <button type="button" class="components-button components-clipboard-button has-icon srfm-shortcode" onclick="handleFormShortcode(this)">
902 <span id="srfm-copy-icon" class="dashicon dashicons dashicons-admin-page"></span>
903 </button>
904 </div>
905 <?php
906 ob_end_flush();
907 }
908 if ( 'entries' === $column ) {
909 // Entries URL to redirect user based on the form ID.
910 $entries_url = wp_nonce_url(
911 add_query_arg(
912 [
913 'form_filter' => $post_id,
914 ],
915 admin_url( 'admin.php?page=sureforms_entries' )
916 ),
917 'srfm_entries_action'
918 );
919
920 // Get the entry count for the form.
921 $entries_count = Entries::get_total_entries_by_status( 'all', $post_id );
922
923 ob_start();
924 ?>
925 <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>
926 <?php
927 ob_end_flush();
928 }
929 }
930
931 /**
932 * Show the import form popup
933 *
934 * @since 0.0.1
935 * @return void
936 */
937 public function import_form_popup() {
938 $screen = get_current_screen();
939 $id = $screen ? $screen->id : '';
940 if ( 'edit-sureforms_form' === $id ) {
941 ?>
942 <div class="srfm-import-plugin-wrap">
943 <div class="srfm-import-wrap">
944 <p class="srfm-import-help"><?php echo esc_html__( 'Please choose the SureForms export file (.json) that you wish to import.', 'sureforms' ); ?></p>
945 <form method="post" enctype="multipart/form-data" class="srfm-import-form">
946 <input type="file" id="srfm-import-file" onchange="handleFileChange(event)" name="import form" accept=".json">
947 <input type="submit" name="import-form-submit" id="import-form-submit" class="srfm-import-button" value="<?php esc_attr_e( 'Import Now', 'sureforms' ); ?>" disabled>
948 </form>
949 <p id="srfm-import-error"><?php echo esc_html__( 'There is some error in json file, please export the SureForms Forms again.', 'sureforms' ); ?></p>
950 </div>
951 </div>
952 <?php
953 }
954 }
955
956 /**
957 * Redirect to home page if instant form is not enabled.
958 *
959 * @since 0.0.1
960 * @return void
961 */
962 public function srfm_instant_form_redirect() {
963
964 $form_id = Helper::get_integer_value( get_the_ID() );
965
966 $instant_form_settings = Helper::get_array_value( Helper::get_post_meta( $form_id, '_srfm_instant_form_settings' ) );
967 $enable_instant_form = ! empty( $instant_form_settings['enable_instant_form'] ) ? boolval( $instant_form_settings['enable_instant_form'] ) : false;
968
969 if ( $enable_instant_form ) {
970 return;
971 }
972
973 $form_preview = '';
974
975 $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.
976
977 if ( $form_preview_attr ) {
978 $form_preview = filter_var( $form_preview_attr, FILTER_VALIDATE_BOOLEAN );
979 }
980
981 if ( is_singular( 'sureforms_form' ) && ! $form_preview && ! current_user_can( 'manage_options' ) ) {
982 wp_safe_redirect( home_url() );
983 return;
984 }
985 }
986
987 /**
988 * Restrict RankMath meta boxes in edit page.
989 *
990 * @since 0.0.5
991 * @return void
992 */
993 public function restrict_data() {
994 add_filter( 'rank_math/excluded_post_types', [ $this, 'unset_sureforms_post_type' ] );
995 }
996
997 /**
998 * Remove SureForms post type from RankMath and Yoast.
999 *
1000 * @param array<mixed> $post_types Post types.
1001 * @since 0.0.5
1002 * @return array<mixed> $post_types Modified post types.
1003 */
1004 public function unset_sureforms_post_type( $post_types ) {
1005 return array_filter(
1006 $post_types,
1007 static function( $post_type ) {
1008 if ( is_array( $post_type ) && isset( $post_type['name'] ) ) {
1009 return SRFM_FORMS_POST_TYPE !== $post_type['name'];
1010 }
1011 return SRFM_FORMS_POST_TYPE !== $post_type;
1012 }
1013 );
1014 }
1015
1016 /**
1017 * Restrict interference of other plugins with SureForms.
1018 *
1019 * @since 0.0.5
1020 * @return void
1021 */
1022 private function restrict_unwanted_insertions() {
1023 // Restrict RankMath metaboxes in edit page.
1024 add_action( 'cmb2_admin_init', [ $this, 'restrict_data' ] );
1025
1026 // Restrict Yoast columns.
1027 add_filter( 'wpseo_accessible_post_types', [ $this, 'unset_sureforms_post_type' ] );
1028 add_filter( 'wpseo_metabox_prio', '__return_false' );
1029
1030 // Restrict AIOSEO columns.
1031 add_filter( 'aioseo_public_post_types', [ $this, 'unset_sureforms_post_type' ] );
1032 }
1033 }
1034