Skip to content

Commit 0e3e61f

Browse files
authored
gpnf-gravitypdf-auto-attach-child-pdfs.php: Added a snippet for attaching child entry PDFs generated by Gravity PDF to parent entry notifications.
1 parent 8eba912 commit 0e3e61f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Nested Forms + Gravity PDF // Auto-attach Child PDFs to Parent Form Notifications
4+
* http://gravitywiz.com/documentation/gravity-forms-nested-forms/
5+
*/
6+
add_filter( 'gform_notification', function ( $notification, $form, $entry ) {
7+
8+
if ( ! class_exists( 'GPNF_Entry' ) || ! class_exists( 'GFPDF\Model\Model_PDF' ) ) {
9+
return $notification;
10+
}
11+
12+
// Initialize attachments array.
13+
if ( ! isset( $notification['attachments'] ) ) {
14+
$notification['attachments'] = array();
15+
}
16+
17+
$attachments =& $notification['attachments'];
18+
$parent_entry = new GPNF_Entry( $entry );
19+
20+
foreach ( $form['fields'] as $field ) {
21+
22+
if ( $field->get_input_type() !== 'form' ) {
23+
continue;
24+
}
25+
26+
$child_form = GFAPI::get_form( $field->gpnfForm );
27+
$child_entries = $parent_entry->get_child_entries( $field->id );
28+
29+
foreach ( $child_entries as $child_entry ) {
30+
31+
$pdf_model = GPDFAPI::get_mvc_class( 'Model_PDF' );
32+
$pdfs = isset( $child_form['gfpdf_form_settings'] ) ? $pdf_model->get_active_pdfs( $child_form['gfpdf_form_settings'], $child_entry ) : array();
33+
34+
foreach ( $pdfs as $pdf ) {
35+
$settings = $child_form['gfpdf_form_settings'][ $pdf['id'] ];
36+
37+
// Generate our PDF.
38+
$filename = $pdf_model->generate_and_save_pdf( $child_entry, $settings );
39+
40+
if ( ! is_wp_error( $filename ) ) {
41+
// Add PDF to notification PDFs.
42+
$attachments[] = $filename;
43+
}
44+
}
45+
}
46+
}
47+
48+
return $notification;
49+
}, 10, 3 );

0 commit comments

Comments
 (0)