Created
July 25, 2023 09:51
-
-
Save adczk/f0e23c8eab2a42ff0f14f624528a2074 to your computer and use it in GitHub Desktop.
Forminator - Hustle: add data submitted in Forminator form to Hustle's local email list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: [Forminator] - Forminator - Hustle: add data submitted in Forminator form to Hustle's local email list | |
| * Description: [Forminator] - Forminator - Hustle: add data submitted in Forminator form to Hustle's local email list | |
| * Author: Adam @ WPMUDEV | |
| * Author URI: https://wpmudev.com | |
| * License: GPLv2 or later | |
| * | |
| * Tested with Forminator 1.24.6 and Hustle 4.8.0 (backwards/future compatibility unknown) | |
| * | |
| * use as MU plugin | |
| * | |
| * make sure to have a form (with e-mail field) already existing | |
| * and hustle module with "Local List" added to integrations | |
| * | |
| * configure as per description in ### CONFIG ### section below | |
| * | |
| **/ | |
| add_filter( 'forminator_custom_form_submit_field_data', 'wpmudev_formi_to_hustle_grab', 10, 2 ); | |
| function wpmudev_formi_to_hustle_grab( $field_data_array, $form_id ) { | |
| ### CONFIG ### | |
| // map Forminator FORM ID to Hustle Module ID | |
| // e.g. Map form 16699 to popup 57 - so form data will be added to email list of that popup | |
| $form_hustle = array( | |
| 16699 => '57', | |
| 123 => '99', | |
| ); | |
| // map form fields to hustle list field | |
| // each mapping is in form of | |
| // 57 => array( | |
| // 'email-1' => 'email', | |
| // ), | |
| // where number is Hustle ID module | |
| // array key (left) is Forminator form field ID | |
| // array value (right) is Hustle list field name | |
| // follow example below | |
| $fields_mapps = array( | |
| 57 => array( | |
| 'email-1' => 'email', | |
| 'name-1' => 'name', | |
| 'phone-1' => 'phone', | |
| ), | |
| 99 => array( | |
| 'email-1' => 'email', | |
| 'text-1' => 'website', | |
| ), | |
| ); | |
| // if to allow already subscribed or not (true/false) | |
| $allow_subs = true; | |
| ### DON'T EDIT BELOW ### | |
| $hustle_submit = array(); | |
| $is_subscribed = false; | |
| // Check form and bail out if not allowed or if Hustle not active | |
| if ( ( !in_array( $form_id, array_keys( $form_hustle ) ) ) || !class_exists( 'Hustle_Entry_Model' ) ) { | |
| return $field_data_array; | |
| } | |
| //error_log( print_r( $field_data_array , true ) ); | |
| // check if form and field mappins are defined | |
| if ( array_key_exists( $form_id, $form_hustle ) && array_key_exists( $form_hustle[$form_id], $fields_mapps ) && is_array( $fields_mapps[$form_hustle[$form_id]] ) ) { | |
| // if yes, grab Hustle module ID and specific mapping (for clarity of use) from CONFIG | |
| $hustle_id = $form_hustle[$form_id]; | |
| $mapping = $fields_mapps[$hustle_id]; | |
| // build up data for Hustle | |
| foreach( $field_data_array as $key => $field ) { | |
| // grab field name and value (for clarity of use) | |
| $field_name = $field['name']; | |
| $field_value = $field['value']; | |
| if ( array_key_exists( $field_name, $mapping ) ) { | |
| $hustle_submit[] = array( | |
| 'name' => $mapping[$field_name], | |
| 'value' => $field_value, | |
| ); | |
| } | |
| if ( $field_name == '_forminator_user_ip' ) { | |
| $hustle_submit[] = array( | |
| 'name' => 'hustle_ip', | |
| 'value' => $field_value, | |
| ); | |
| } | |
| } | |
| // if existing e-mail subscription not allowed, check if subscribed | |
| if ( $allow_subs === false ) { | |
| $is_subscribed = Hustle_Entry_Model::is_email_subscribed_to_module_id( $hustle_id, $hustle_submit['email'] ); | |
| if ( $is_subscribed === false ) { | |
| $allow_subs = true; | |
| } | |
| } | |
| // if allowed, do subscribe | |
| if ( $allow_subs !== false ) { | |
| // save data to Hustle Local List here | |
| $module = new Hustle_Module_Model( $hustle_id ); | |
| $entry = new Hustle_Entry_Model(); | |
| $entry->entry_type = $module->module_type; | |
| $entry->module_id = $hustle_id; | |
| $entry->save(); | |
| $entry->set_fields( $hustle_submit ); | |
| } | |
| } | |
| return $field_data_array; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment