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