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