Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save glaubersilva/4116e4243044a8f89f37cec2afe67481 to your computer and use it in GitHub Desktop.
Save glaubersilva/4116e4243044a8f89f37cec2afe67481 to your computer and use it in GitHub Desktop.
[Hustle] - Insert Forminator submissions by AJAX
<?php
/**
* Plugin Name: [Hustle] - Insert Forminator submissions by AJAX
* Plugin URI: https://wpmudev.com/
* Description: This snippet preserves all Hustle integrations.
* Task: SLS-3648
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://wpmudev.com/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
function wpmudev_hustle_insert_forminator_submission_by_ajax( $form_id ) {
if ( 7 !== (int) $form_id || ! class_exists( 'Hustle_Module_Front_Ajax' ) ) {
return;
}
$entry_fields_map = array( // An array that maps the entry ids of Hustle with the submitted fields of Forminator.
'email' => 'email-1',
'name' => 'name-1',
'phone' => 'phone-1',
);
$module_id = 1; // Import Forminator submitted fields into Hustle.
$entry_items = '';
foreach ( $entry_fields_map as $hustle_key => $forminator_key ) {
if ( 'email' === $hustle_key ) {
$hustle_value = isset( $_POST[ $forminator_key ] ) ? sanitize_email( $_POST[ $forminator_key ] ) : '';
} else {
$hustle_value = isset( $_POST[ $forminator_key ] ) ? sanitize_text_field( $_POST[ $forminator_key ] ) : '';
}
$entry_items = add_query_arg( $hustle_key, urlencode( $hustle_value ), $entry_items );
}
$entry_items = add_query_arg( 'hustle_module_id', $module_id, $entry_items );
$entry_items = add_query_arg( 'post_id', get_the_ID(), $entry_items );
$entry_items = explode( '?', $entry_items )[1];
$body = array(
'data' => array(
'form' => $entry_items,
'module_id' => $module_id,
'gdpr' => 1,
'uri' => esc_url_raw( $_REQUEST['referer_url'] ),
),
);
$url = admin_url( 'admin-ajax.php?action=hustle_module_form_submit' );
/**
* ### IMPORTANT ###
*
* Uncomment the line below if you are testing it on a
* localhost environment with a self-signed SSL certificate.
*/
//$url = str_replace( 'https', 'http', $url );
wp_remote_post( $url, array( 'body' => $body ) );
}
add_action( 'forminator_form_after_save_entry', 'wpmudev_hustle_insert_forminator_submission_by_ajax' ); // When Form's Submission Behaviour is set to Ajax.
add_action( 'forminator_form_after_handle_submit', 'wpmudev_hustle_insert_forminator_submission_by_ajax' ); // When Form's Submission Behaviour is set to Page Reload.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment