Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/09aea39f1cc2231b5969b13ec5bedcf4 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/09aea39f1cc2231b5969b13ec5bedcf4 to your computer and use it in GitHub Desktop.
[Hustle] - Insert Forminator submitted fields into Hustle.
<?php
/**
* Plugin Name: [Hustle] - Insert Forminator submission
* Plugin URI: https://premium.wpmudev.org/
* Description: A custom snippet to insert Forminator submitted fields into Hustle.
* Task: SLS -
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
function wpmudev_hustle_insert_formi_submitted_fields( $form_id ) {
if ( 115 !== (int) $form_id || ! class_exists( 'Hustle_Entry_Model' ) ) {
return;
}
// An array that maps the entry ids of Hustle with the submitted fields of Forminator
$entry_fields_map = array(
'email' => 'email-1',
'name' => 'name-1',
'phone' => 'phone-1',
);
// Import Forminator submitted fields into Hustle
$module_id = 2;
$module_type = 'popup'; // popup, slidein, embedded, social_sharing
$entry = new Hustle_Entry_Model();
$entry->entry_type = $module_type;
$entry->module_id = $module_id;
$entry_items = array();
$entry->save();
$client = isset( $_SERVER['HTTP_CLIENT_IP'] ) ? $_SERVER['HTTP_CLIENT_IP'] : null;
$forward = isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null;
$remote = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null;
$client_real = isset( $_SERVER['HTTP_X_REAL_IP'] ) ? $_SERVER['HTTP_X_REAL_IP'] : null;
$hustle_ip = $remote;
if ( filter_var( $client, FILTER_VALIDATE_IP ) ) {
$hustle_ip = $client;
} elseif ( filter_var( $client_real, FILTER_VALIDATE_IP ) ) {
$hustle_ip = $client_real;
} elseif ( ! empty( $forward ) ) {
$forward = explode( ',', $forward );
$ip = array_shift( $forward );
$ip = trim( $ip );
if ( filter_var( $ip, FILTER_VALIDATE_IP ) ) {
$hustle_ip = $ip;
}
}
foreach ( $entry_fields_map as $hustle_key => $forminator_key ) {
$entry_items[] = array(
'name' => $hustle_key,
'value' => isset( $_POST[ $forminator_key ] ) ? sanitize_text_field( $_POST[ $forminator_key ] ) : '' ,
);
}
$entry_items[] = array(
'name' => 'hustle_ip',
'value' => $hustle_ip,
);
$entry->set_fields( $entry_items );
}
// When Form's Submission Behaviour is set to Ajax
add_action( 'forminator_custom_form_after_save_entry', 'wpmudev_hustle_insert_formi_submitted_fields' );
// When Form's Submission Behaviour is set to Page Reload
add_action( 'forminator_custom_form_after_handle_submit', 'wpmudev_hustle_insert_formi_submitted_fields' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment