diff --git a/gp-easy-passthrough/gpep-duplicate-child-entries.php b/gp-easy-passthrough/gpep-duplicate-child-entries.php new file mode 100644 index 000000000..25717f5ab --- /dev/null +++ b/gp-easy-passthrough/gpep-duplicate-child-entries.php @@ -0,0 +1,82 @@ +type !== 'form' || $target_field_id !== $source_field->id ) { + return $field_value; + } + + // Ensure that the source field matches one of the Nested Form fields specified in $parent_form_field_ids. + if ( ! in_array( $source_field->id, $parent_form_field_ids ) ) { + return $field_value; + } + + /** + * Skip duplicating entries if the child entries are already temporary. + * + * @var GPNF_Session + */ + $session = new GPNF_Session( $form_id ); + $session->set_session_data(); + + $cookie = $session->get_cookie(); + + if ( rgars( $cookie, 'nested_entries/' . $target_field_id ) ) { + return ''; + } + + /** + * Loop through child entries for the current Nested Form field and duplicate them. + */ + $duplicated_child_entries = array(); + + $child_entries = gp_nested_forms()->get_entries( $field_value ); + + foreach ( $child_entries as $child_entry ) { + $duplicated_child_entry = GFAPI::add_entry( array_replace( $child_entry, array( + // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date + 'date_created' => date( 'Y-m-d H:i:s' ), + GPNF_Entry::ENTRY_PARENT_KEY => $session->get_runtime_hashcode(), + GPNF_Entry::ENTRY_PARENT_FORM_KEY => $form_id, + GPNF_Entry::ENTRY_NESTED_FORM_FIELD_KEY => $target_field_id, + ) ) ); + + // Attach session meta to child entry. + $session->add_child_entry( $duplicated_child_entry ); + + $duplicated_child_entries[] = $duplicated_child_entry; + } + + return implode( ',', $duplicated_child_entries ); +}, 10, 4 );