restrict_unwanted_insertions();
add_action( 'init', [ $this, 'register_post_types' ] );
add_action( 'init', [ $this, 'register_post_metas' ] );
add_filter( 'manage_sureforms_form_posts_columns', [ $this, 'custom_form_columns' ] );
add_action( 'manage_sureforms_form_posts_custom_column', [ $this, 'custom_form_column_data' ], 10, 2 );
add_filter( 'manage_sureforms_entry_posts_columns', [ $this, 'custom_entry_columns' ] );
add_action( 'manage_sureforms_entry_posts_custom_column', [ $this, 'custom_entry_column_data' ], 10, 2 );
add_shortcode( 'sureforms', [ $this, 'forms_shortcode' ] );
add_action( 'add_meta_boxes', [ $this, 'entries_meta_box' ] );
add_action( 'restrict_manage_posts', [ $this, 'add_tax_filter' ] );
add_action( 'manage_posts_extra_tablenav', [ $this, 'maybe_render_blank_form_state' ] );
add_action( 'in_admin_header', [ $this, 'embed_page_header' ] );
add_action( 'admin_head', [ $this, 'remove_entries_publishing_actions' ] );
add_filter( 'post_row_actions', [ $this, 'modify_entries_list_row_actions' ], 10, 2 );
add_filter( 'post_updated_messages', [ $this, 'entries_updated_message' ] );
add_filter( 'bulk_actions-edit-sureforms_form', [ $this, 'register_modify_bulk_actions' ], 99 );
add_action( 'admin_notices', [ $this, 'import_form_popup' ] );
add_action( 'admin_bar_menu', [ $this, 'remove_admin_bar_menu_item' ], 80, 1 );
add_action( 'template_redirect', [ $this, 'srfm_instant_form_redirect' ] );}
/**
* Add SureForms menu.
*
* @param string $title Parent slug.
* @param string $subtitle Parent slug.
* @param string $image Parent slug.
* @param string $button_text Parent slug.
* @param string $button_url Parent slug.
* @param string $after_button After button content.
* @return void
* @since 0.0.1
*/
public function get_blank_page_markup( $title, $subtitle, $image, $button_text = '', $button_url = '', $after_button = '' ) {
echo '
';
}
/**
* Render blank state for add new form screen.
*
* @param string $post_type Post type.
* @return void
* @since 0.0.1
*/
public function sureforms_render_blank_state( $post_type ) {
if ( SRFM_FORMS_POST_TYPE === $post_type ) {
$page_name = 'add-new-form';
$new_form_url = admin_url( 'admin.php?page=' . $page_name );
$import_button = '';
$this->get_blank_page_markup(
esc_html__( 'Let’s build your first form', 'sureforms' ),
esc_html__(
'Craft beautiful and functional forms in minutes',
'sureforms'
),
'add-new-form',
esc_html__( 'Add New Form', 'sureforms' ),
$new_form_url,
$import_button
);
}
if ( SRFM_ENTRIES_POST_TYPE === $post_type ) {
$this->get_blank_page_markup(
esc_html__( 'No records found', 'sureforms' ),
esc_html__(
'This is where your form entries will appear',
'sureforms'
),
'blank-entries'
);
}
}
/**
* Registers the forms and submissions post types.
*
* @return void
* @since 0.0.1
*/
public function register_post_types() {
$form_labels = [
'name' => _x( 'Forms', 'post type general name', 'sureforms' ),
'singular_name' => _x( 'Form', 'post type singular name', 'sureforms' ),
'menu_name' => _x( 'Forms', 'admin menu', 'sureforms' ),
'add_new' => _x( 'Add New', 'form', 'sureforms' ),
'add_new_item' => __( 'Add New Form', 'sureforms' ),
'new_item' => __( 'New Form', 'sureforms' ),
'edit_item' => __( 'Edit Form', 'sureforms' ),
'view_item' => __( 'View Form', 'sureforms' ),
'view_items' => __( 'View Forms', 'sureforms' ),
'all_items' => __( 'Forms', 'sureforms' ),
'search_items' => __( 'Search Forms', 'sureforms' ),
'parent_item_colon' => __( 'Parent Forms:', 'sureforms' ),
'not_found' => __( 'No forms found.', 'sureforms' ),
'not_found_in_trash' => __( 'No forms found in Trash.', 'sureforms' ),
'item_published' => __( 'Form published.', 'sureforms' ),
'item_updated' => __( 'Form updated.', 'sureforms' ),
];
register_post_type(
SRFM_FORMS_POST_TYPE,
[
'labels' => $form_labels,
'rewrite' => [ 'slug' => 'form' ],
'public' => true,
'show_in_rest' => true,
'has_archive' => false,
'show_ui' => true,
'supports' => [ 'title', 'author', 'editor', 'custom-fields' ],
'show_in_menu' => 'sureforms_menu',
'show_in_nav_menus' => true,
]
);
$result_labels = [
'name' => _x( 'Entries', 'post type general name', 'sureforms' ),
'singular_name' => _x( 'Entry', 'post type singular name', 'sureforms' ),
'menu_name' => _x( 'Entries', 'admin menu', 'sureforms' ),
'name_admin_bar' => _x( 'Entry', 'add new on admin bar', 'sureforms' ),
'add_new' => _x( 'Add New', 'Entry', 'sureforms' ),
'add_new_item' => __( 'Add New Entry', 'sureforms' ),
'new_item' => __( 'New Entry', 'sureforms' ),
'edit_item' => __( 'View Entry', 'sureforms' ),
'view_item' => __( 'View Entry', 'sureforms' ),
'all_items' => __( 'Entries', 'sureforms' ),
'search_items' => __( 'Search Entries', 'sureforms' ),
'parent_item_colon' => __( 'Parent Entries:', 'sureforms' ),
'not_found' => __( 'No results found.', 'sureforms' ),
'not_found_in_trash' => __( 'No results found in Trash.', 'sureforms' ),
];
register_post_type(
SRFM_ENTRIES_POST_TYPE,
[
'labels' => $result_labels,
'supports' => [ 'title' ],
'public' => false,
'show_in_rest' => true,
'exclude_from_search' => true,
'publicly_queryable' => false,
'has_archive' => true,
'capability_type' => 'post',
'capabilities' => [
'create_posts' => 'do_not_allow',
],
'map_meta_cap' => true,
'show_ui' => false, // Hide the entries post type from the admin menu.
'show_in_menu' => 'sureforms_menu',
]
);
register_taxonomy(
'sureforms_tax',
'sureforms_entry',
[
'label' => __( 'Form ID', 'sureforms' ),
'hierarchical' => true,
'capabilities' => [
'assign_terms' => 'god',
'edit_terms' => 'god',
'manage_terms' => 'god',
],
'public' => false,
'show_in_rest' => true,
'show_admin_column' => false,
'show_in_nav_menus' => false,
'show_ui' => false,
]
);
// will be used later.
// register_post_status(
// 'unread',
// array(
// 'label' => _x( 'Unread', 'sureforms', 'sureforms' ),
// 'public' => true,
// 'exclude_from_search' => false,
// 'show_in_admin_all_list' => true,
// 'show_in_admin_status_list' => true,
// Translators: %s is the number of unread items.
// 'label_count' => _n_noop( 'Unread (%s)', 'Unread (%s)', 'sureforms' ),
// )
// );.
}
/**
* Remove add new form menu item.
*
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
*
* @return void
* @since 0.0.1
*/
public function remove_admin_bar_menu_item( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-sureforms_form' );
}
/**
* Modify post update message for Entry post type.
*
* @param string $messages Post type.
* @return string
* @since 0.0.1
*/
public function entries_updated_message( $messages ) {
global $post_ID;
$post_type = get_post_type( $post_ID );
if ( SRFM_ENTRIES_POST_TYPE === $post_type ) {
// @phpstan-ignore-next-line -- False positive
$messages['post'][1] = __( 'Entry updated.', 'sureforms' );
}
return $messages;
}
/**
* Remove publishing actions from single entries page.
*
* @return void
* @since 0.0.1
*/
public function remove_entries_publishing_actions() {
global $typenow;
if ( 'sureforms_entry' === $typenow ) { ?>
$actions An array of row action links.
* @param \WP_Post $post The current WP_Post object.
*
* @return array $actions Modified row action links.
* @since 0.0.1
*/
public function modify_entries_list_row_actions( $actions, $post ) {
if ( 'sureforms_entry' === $post->post_type ) {
$actions['edit'] = 'View';
}
if ( 'sureforms_form' === $post->post_type ) {
$actions['export'] = 'Export';
}
return $actions;
}
/**
* Modify list bulk actions.
*
* @param array $bulk_actions An array of bulk action links.
* @since 0.0.1
* @return array $bulk_actions Modified action links.
*/
public function register_modify_bulk_actions( $bulk_actions ) {
$white_listed_actions = [
'edit',
'trash',
'delete',
'untrash',
];
// remove all actions except white listed actions.
$bulk_actions = array_intersect_key( $bulk_actions, array_flip( $white_listed_actions ) );
// Add export action only if edit and trash actions are present in bulk actions.
if ( isset( $bulk_actions['edit'] ) && isset( $bulk_actions['trash'] ) ) {
$bulk_actions['export'] = __( 'Export', 'sureforms' );
}
return $bulk_actions;
}
/**
* Show blank slate styles.
*
* @return void
* @since 0.0.1
*/
public function get_blank_state_styles() {
echo '';
}
/**
* Show blank slate.
*
* @param string $which String which tablenav is being shown.
* @return void
* @since 0.0.1
*/
public function maybe_render_blank_form_state( $which ) {
$screen = get_current_screen();
$post_type = $screen ? $screen->post_type : '';
if ( SRFM_FORMS_POST_TYPE === $post_type && 'bottom' === $which ) {
$counts = (array) wp_count_posts( SRFM_FORMS_POST_TYPE );
unset( $counts['auto-draft'] );
$count = array_sum( $counts );
if ( 0 < $count ) {
return;
}
$this->sureforms_render_blank_state( $post_type );
$this->get_blank_state_styles();
}
if ( SRFM_ENTRIES_POST_TYPE === $post_type && 'bottom' === $which ) {
$counts = (array) wp_count_posts( SRFM_ENTRIES_POST_TYPE );
unset( $counts['auto-draft'] );
$count = array_sum( $counts );
if ( 0 < $count ) {
return;
}
$this->sureforms_render_blank_state( $post_type );
$this->get_blank_state_styles();
}
}
/**
* Set up a div for the header to render into it.
*
* @return void
* @since 0.0.1
*/
public static function embed_page_header() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( 'edit-' . SRFM_FORMS_POST_TYPE === $screen_id || 'edit-' . SRFM_ENTRIES_POST_TYPE === $screen_id || 'sureforms_page_sureforms_entries' === $screen_id ) {
?>
'boolean',
'_srfm_submit_button_text' => 'string',
'_srfm_is_inline_button' => 'boolean',
// Submit Button.
'_srfm_submit_width_backend' => 'string',
'_srfm_button_border_radius' => 'integer',
'_srfm_submit_alignment' => 'string',
'_srfm_submit_alignment_backend' => 'string',
'_srfm_submit_width' => 'string',
'_srfm_inherit_theme_button' => 'boolean',
// Additional Classes.
'_srfm_additional_classes' => 'string',
// Advanced tab metas.
// Success Message.
'_srfm_submit_type' => 'string',
// Security.
'_srfm_captcha_security_type' => 'string',
'_srfm_form_recaptcha' => 'string',
]
);
// Form Custom CSS meta.
register_post_meta(
'sureforms_form',
'_srfm_form_custom_css',
[
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
'sanitize_callback' => function( $meta_value ) {
return wp_kses_post( $meta_value );
},
]
);
foreach ( $metas as $meta => $type ) {
register_meta(
'post',
$meta,
[
'object_subtype' => SRFM_FORMS_POST_TYPE,
'show_in_rest' => true,
'single' => true,
'type' => $type,
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
]
);
}
// Registers meta to handle values associated with form styling.
register_post_meta(
SRFM_FORMS_POST_TYPE,
'_srfm_instant_form_settings',
[
'single' => true,
'type' => 'object',
'auth_callback' => '__return_true',
'show_in_rest' => [
'schema' => [
'type' => 'object',
'properties' => [
'site_logo' => [
'type' => 'string',
],
'site_logo_id' => [
'type' => 'integer',
],
// Form page banner settings.
'cover_type' => [
'type' => 'string',
],
'cover_color' => [
'type' => 'string',
],
'cover_image' => [
'type' => 'string',
],
'cover_image_id' => [
'type' => 'integer',
],
// Form page background settings.
'bg_type' => [
'type' => 'string',
],
'bg_color' => [
'type' => 'string',
],
'bg_image' => [
'type' => 'string',
],
'bg_image_id' => [
'type' => 'integer',
],
'enable_instant_form' => [
'type' => 'boolean',
],
'form_container_width' => [
'type' => 'integer',
],
'single_page_form_title' => [
'type' => 'boolean',
],
'use_banner_as_page_background' => [
'type' => 'boolean',
],
],
],
],
'default' => [
'bg_type' => 'color',
'bg_color' => '#ffffff',
'bg_image' => '',
'site_logo' => '',
'cover_type' => 'color',
'cover_color' => '#0C78FB',
'cover_image' => '',
'enable_instant_form' => false,
'form_container_width' => 620,
'single_page_form_title' => true,
'use_banner_as_page_background' => false,
],
]
);
register_post_meta(
SRFM_FORMS_POST_TYPE,
'_srfm_forms_styling',
[
'single' => true,
'type' => 'object',
'auth_callback' => '__return_true',
'show_in_rest' => [
'schema' => [
'type' => 'object',
'properties' => [
'primary_color' => [
'type' => 'string',
],
'text_color' => [
'type' => 'string',
],
'text_color_on_primary' => [
'type' => 'string',
],
'field_spacing' => [
'type' => 'string',
],
'submit_button_alignment' => [
'type' => 'string',
],
],
],
],
'default' => [
'primary_color' => '#0C78FB',
'text_color' => '#1E1E1E',
'text_color_on_primary' => '#FFFFFF',
'field_spacing' => 'medium',
'submit_button_alignment' => 'left',
],
]
);
// Email notification Metas.
register_post_meta(
'sureforms_form',
'_srfm_email_notification',
[
'single' => true,
'type' => 'array',
'auth_callback' => '__return_true',
'show_in_rest' => [
'schema' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'id' => [
'type' => 'integer',
],
'status' => [
'type' => 'boolean',
],
'is_raw_format' => [
'type' => 'boolean',
],
'name' => [
'type' => 'string',
],
'email_to' => [
'type' => 'string',
],
'email_reply_to' => [
'type' => 'string',
],
'email_cc' => [
'type' => 'string',
],
'email_bcc' => [
'type' => 'string',
],
'subject' => [
'type' => 'string',
],
'email_body' => [
'type' => 'string',
],
],
],
],
],
'default' => [
[
'id' => 1,
'status' => true,
'is_raw_format' => false,
'name' => 'Admin Notification Email',
'email_to' => '{admin_email}',
'email_reply_to' => '{admin_email}',
'email_cc' => '{admin_email}',
'email_bcc' => '{admin_email}',
'subject' => 'New Form Submission',
'email_body' => '{all_data}',
],
],
]
);
// Compliance Settings metas.
register_post_meta(
'sureforms_form',
'_srfm_compliance',
[
'single' => true,
'type' => 'array',
'auth_callback' => '__return_true',
'show_in_rest' => [
'schema' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'id' => [
'type' => 'string',
],
'gdpr' => [
'type' => 'boolean',
],
'do_not_store_entries' => [
'type' => 'boolean',
],
'auto_delete_entries' => [
'type' => 'boolean',
],
'auto_delete_days' => [
'type' => 'string',
],
],
],
],
],
'default' => [
[
'id' => 'gdpr',
'gdpr' => false,
'do_not_store_entries' => false,
'auto_delete_entries' => false,
'auto_delete_days' => '',
],
],
]
);
// form confirmation.
register_post_meta(
'sureforms_form',
'_srfm_form_confirmation',
[
'single' => true,
'type' => 'array',
'auth_callback' => '__return_true',
'show_in_rest' => [
'schema' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'id' => [
'type' => 'integer',
],
'confirmation_type' => [
'type' => 'string',
],
'page_url' => [
'type' => 'string',
],
'custom_url' => [
'type' => 'string',
],
'message' => [
'type' => 'string',
],
'submission_action' => [
'type' => 'string',
],
],
],
],
],
'default' => [
[
'id' => 1,
'confirmation_type' => 'same page',
'page_url' => '',
'custom_url' => '',
'message' => ' . ')
Thank you
We have received your email. You\'ll hear from us as soon as possible.
Please be sure to whitelist our {admin_email} email address to ensure our replies reach your inbox safely.
',
'submission_action' => 'hide form',
],
],
]
);
// Sureforms entry metas.
register_post_meta(
'sureforms_entry',
'_srfm_submission_info',
[
'single' => true,
'type' => 'array',
'auth_callback' => '__return_true',
'show_in_rest' => [
'schema' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'user_ip' => [
'type' => 'string',
],
'browser_name' => [
'type' => 'string',
],
'device_name' => [
'type' => 'string',
],
],
],
],
],
]
);
// store form id in entry.
register_post_meta(
'sureforms_entry',
'_srfm_entry_form_id',
[
'single' => true,
'type' => 'integer',
'auth_callback' => '__return_true',
'show_in_rest' => true,
]
);
// conditional logic.
do_action( 'srfm_register_conditional_logic_post_meta' );
/**
* Hook for registering additional Post Meta
*/
do_action( 'srfm_register_additional_post_meta' );
}
/**
* Sureforms entries meta box callback.
*
* @param \WP_Post $post Template.
* @return void
* @since 0.0.1
*/
public function sureforms_meta_box_callback( \WP_Post $post ) {
$meta_data = get_post_meta( $post->ID, 'srfm_entry_meta', true );
if ( ! is_array( $meta_data ) ) {
return;
}
$excluded_fields = [ 'srfm-honeypot-field', 'g-recaptcha-response', 'srfm-sender-email-field' ];
?>
| |
$value ) :
if ( in_array( $field_name, $excluded_fields, true ) ) {
continue;
}
if ( false === str_contains( $field_name, '-lbl-' ) ) {
continue;
}
$label = explode( '-lbl-', $field_name )[1];
// Getting the encrypted label. we are removing the block slug here.
$label = explode( '-', $label )[0];
?>
|
|
|
|
|
ID;
$taxonomy = 'sureforms_tax';
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( is_array( $terms ) && count( $terms ) > 0 ) {
$form_id = intval( $terms[0]->slug );
$form_name = ! empty( get_the_title( $form_id ) ) ? get_the_title( $form_id ) : 'SureForms Form';
$submission_info = get_post_meta( $post_id, '_srfm_submission_info', true );
if ( is_array( $submission_info ) && count( $submission_info ) > 0 ) {
$user_ip = $submission_info[0]['user_ip'] ? $submission_info[0]['user_ip'] : '';
$browser_name = $submission_info[0]['browser_name'] ? $submission_info[0]['browser_name'] : '';
$device_name = $submission_info[0]['device_name'] ? $submission_info[0]['device_name'] : '';
$entry_form_id = Helper::get_string_value( get_post_meta( $post_id, '_srfm_entry_form_id', true ) );
} else {
$user_ip = '';
$browser_name = '';
$device_name = '';
$entry_form_id = '';
}
?>
$atts Attributes.
* @return string|false. $content Post Content.
* @since 0.0.1
*/
public function forms_shortcode( array $atts ) {
$atts = shortcode_atts(
[
'id' => '',
'show_title' => true,
],
$atts
);
$id = intval( $atts['id'] );
$post = get_post( $id );
if ( ! empty( $id ) && $post ) {
$content = Generate_Form_Markup::get_form_markup( $id, ! filter_var( $atts['show_title'], FILTER_VALIDATE_BOOLEAN ), '', 'post', true );
return $content;
}
return '';
}
/**
* Add custom column header.
*
* @param array $columns Attributes.
* @return array $columns Post Content.
* @since 0.0.1
*/
public function custom_form_columns( $columns ) {
$columns = [
'cb' => $columns['cb'],
'title' => $columns['title'],
'sureforms' => __( 'Shortcode', 'sureforms' ),
'entries' => __( 'Entries', 'sureforms' ),
'author' => $columns['author'],
'date' => $columns['date'],
];
return $columns;
}
/**
* Populate custom column with data.
*
* @param string $column Attributes.
* @param integer $post_id Attributes.
* @return void
* @since 0.0.1
*/
public function custom_form_column_data( $column, $post_id ) {
if ( 'sureforms' === $column ) {
ob_start();
?>
$post_id,
],
admin_url( 'admin.php?page=sureforms_entries' )
),
'srfm_entries_action'
);
// Get the entry count for the form.
$entries_count = Entries::get_total_entries_by_status( 'all', $post_id );
ob_start();
?>
$columns Attributes.
* @return array $columns Post Content.
* @since 0.0.1
*/
public function custom_entry_columns( $columns ) {
$columns = [
'cb' => $columns['cb'],
'title' => __( 'First Field', 'sureforms' ),
'form_name' => __( 'Form Name', 'sureforms' ),
'entry_id' => __( 'ID', 'sureforms' ),
'date' => __( 'Submitted On', 'sureforms' ),
];
return $columns;
}
/**
* Populate custom column with data.
*
* @param string $column Attributes.
* @param integer $post_id Attributes.
* @return void
* @since 0.0.1
*/
public function custom_entry_column_data( $column, $post_id ) {
if ( 'entry_id' === $column ) {
$entry_id = strval( $post_id );
echo '#' . esc_html( $entry_id ) . '
';
}
if ( 'form_name' === $column ) {
$taxonomy = 'sureforms_tax';
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( is_array( $terms ) && count( $terms ) > 0 ) {
$form_id = intval( $terms[0]->slug );
$form_name = ! empty( get_the_title( $form_id ) ) ? get_the_title( $form_id ) : 'SureForms Form';
echo '' . esc_html( $form_name . ' #' . $form_id ) . '
';
} else {
?>
id ) {
$forms = get_posts(
[
'post_type' => SRFM_FORMS_POST_TYPE,
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
]
);
if ( ! empty( $forms ) ) {
$selected = isset( $_GET['sureforms_tax'] ) ? sanitize_key( wp_unslash( $_GET['sureforms_tax'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce Verification is not needed in this case. We are not getting the nonce value.
echo '';
}
}
}
/**
* Show the import form popup
*
* @since 0.0.1
* @return void
*/
public function import_form_popup() {
$screen = get_current_screen();
$id = $screen ? $screen->id : '';
if ( 'edit-sureforms_form' === $id ) {
?>
$post_types Post types.
* @since 0.0.5
* @return array $post_types Modified post types.
*/
public function unset_sureforms_post_type( $post_types ) {
$filtered_post_types = array_filter(
$post_types,
function( $post_type ) {
if ( is_array( $post_type ) && isset( $post_type['name'] ) ) {
return SRFM_FORMS_POST_TYPE !== $post_type['name'];
} else {
return SRFM_FORMS_POST_TYPE !== $post_type;
}
}
);
return $filtered_post_types;
}
}