Skip to content

Commit c88aa94

Browse files
authored
gpep-duplicate-child-entries.php: Migrated from experimental folder.
1 parent e21b59b commit c88aa94

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Easy Passthrough + Nested Forms // Duplicate child entries on passthrough
4+
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
5+
* https://gravitywiz.com/documentation/gravity-forms-easy-passthrough/
6+
*
7+
* Experimental Snippet 🧪
8+
*
9+
* Installation Instructions: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
10+
*
11+
* Duplicate child entries when a parent form is passed through using Easy Passthrough.
12+
*
13+
* Configuring:
14+
* * Update $parent_form_ids to match the form IDs that need to have their Nested Form field child entries duplicated
15+
* * Update $parent_form_field_ids to include the Nested Form field IDs that should have their child entries duplicated
16+
*
17+
* As an example, this snippet is configured to duplicate child entries for Nested Form fields 4 and 5 in form 123.
18+
*/
19+
add_filter( 'gpep_target_field_value', function ( $field_value, $form_id, $target_field_id, $source_field ) {
20+
21+
/**
22+
* Important! Update these two variables.
23+
*/
24+
$parent_form_ids = array( 123 );
25+
$parent_form_field_ids = array( 4, 5 );
26+
27+
if ( ! class_exists( 'GPNF_Entry' ) || ! function_exists( 'gp_nested_forms' ) ) {
28+
return $field_value;
29+
}
30+
31+
if ( ! in_array( $form_id, $parent_form_ids ) ) {
32+
return $field_value;
33+
}
34+
35+
// Ensure both the source field and target field are Nested Form fields.
36+
if ( $source_field->type !== 'form' || $target_field_id !== $source_field->id ) {
37+
return $field_value;
38+
}
39+
40+
// Ensure that the source field matches one of the Nested Form fields specified in $parent_form_field_ids.
41+
if ( ! in_array( $source_field->id, $parent_form_field_ids ) ) {
42+
return $field_value;
43+
}
44+
45+
/**
46+
* Skip duplicating entries if the child entries are already temporary.
47+
*
48+
* @var GPNF_Session
49+
*/
50+
$session = new GPNF_Session( $form_id );
51+
$session->set_session_data();
52+
53+
$cookie = $session->get_cookie();
54+
55+
if ( rgars( $cookie, 'nested_entries/' . $target_field_id ) ) {
56+
return '';
57+
}
58+
59+
/**
60+
* Loop through child entries for the current Nested Form field and duplicate them.
61+
*/
62+
$duplicated_child_entries = array();
63+
64+
$child_entries = gp_nested_forms()->get_entries( $field_value );
65+
66+
foreach ( $child_entries as $child_entry ) {
67+
$duplicated_child_entry = GFAPI::add_entry( array_replace( $child_entry, array(
68+
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
69+
'date_created' => date( 'Y-m-d H:i:s' ),
70+
GPNF_Entry::ENTRY_PARENT_KEY => $session->get_runtime_hashcode(),
71+
GPNF_Entry::ENTRY_PARENT_FORM_KEY => $form_id,
72+
GPNF_Entry::ENTRY_NESTED_FORM_FIELD_KEY => $target_field_id,
73+
) ) );
74+
75+
// Attach session meta to child entry.
76+
$session->add_child_entry( $duplicated_child_entry );
77+
78+
$duplicated_child_entries[] = $duplicated_child_entry;
79+
}
80+
81+
return implode( ',', $duplicated_child_entries );
82+
}, 10, 4 );

0 commit comments

Comments
 (0)