Skip to content

Instantly share code, notes, and snippets.

@alfredo-wpmudev
Created January 10, 2024 16:37
Show Gist options
  • Save alfredo-wpmudev/634e54b60e8b7e9924c0fad1a3ed4d52 to your computer and use it in GitHub Desktop.
Save alfredo-wpmudev/634e54b60e8b7e9924c0fad1a3ed4d52 to your computer and use it in GitHub Desktop.
Allow Group Field Data on Notification without use {all_fields},for example to display {group-1} use {field-group-1}
<?php
/**
* Plugin Name: [Forminator Pro] Allow Group Field Data on Notification
* Description: Allow Group Field Data on Notification without use {all_fields},to display {group-1} use {field-group-1}
* Author: Alfredo Galano Loyola @ WPMUDEV
* Author URI: https://wpmudev.com
* License: GPLv2 or later
*/
add_filter( 'forminator_replace_form_data', 'wpmudev_send_post_data_email', 10, 3 );
function wpmudev_send_post_data_email( $content, $data, $original_content ){
$list_groups = [];
$group_limits = [];
//Add the forms ID to activate the feature:
$form_ids = array(1049195);
if( !in_array($data['form_id'], $form_ids)){
return $content;
}
$fields_wrappers = Forminator_API::get_form_wrappers($data['form_id'] );
foreach ($fields_wrappers as $field_wrappers) {
if ($field_wrappers["parent_group"]!=="") {
$field_id = $field_wrappers["fields"][0]["element_id"];
$field_label = $field_wrappers["fields"][0]["field_label"];
$list_groups[$field_wrappers["parent_group"]][] = [$field_id,$field_label];
}else{
$group_limits[$field_wrappers["fields"][0]["element_id"]] = intval($field_wrappers["fields"][0]["max_limit"]);
}
}
if (count($list_groups) > 0) {
foreach ($list_groups as $group => $fields) {
$needle = '{field-'.$group.'}';
$copies = 100;
if(isset($group_limits[$group]) && $group_limits[$group] > 0){
$copies = $group_limits[$group];
}
if( strpos( $content, $needle ) !== false ){
$group_html="";
for ($i=0; $i < $copies; $i++) {
foreach ($fields as $field) {
$element_id=$field[0];
if ($i!==0) {
$index_field = $i+1;
$element_id.="-".$index_field;
}
if(!isset($data[$element_id])){
break;
}
if(!empty($data[$element_id])){
$group_html .="<b>".$field[1].":</b>".$data[$element_id];
$group_html .="<br>";
}
}
$group_html .="<br>";
}
$content = str_replace( $needle,$group_html, $content );
}
}
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment