|
1 | 1 | <?php |
| 2 | + |
2 | 3 | /** |
3 | 4 | * Gravity Perks // Disable Entry Creation // Delay Deletion for Gravity PDF Background Processing |
4 | 5 | * http://gravitywiz.com/documentation/gravity-forms-disable-entry-creation/ |
|
9 | 10 | * |
10 | 11 | * Installation instructions: |
11 | 12 | * 1. https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
12 | | - * 2. Update FORMID and $form_id accordingly. |
| 13 | + * 2. See usage instructions at the bottom of the file |
13 | 14 | */ |
14 | | -add_filter( 'gpdec_should_delete_entry_FORMID', '__return_false' ); |
| 15 | +class GPDEC_GFPDF_Delayed_Deletion { |
| 16 | + public $deletion_queue = array(); |
| 17 | + |
| 18 | + function __construct( $args ) { |
| 19 | + $this->_args = wp_parse_args( $args, array( |
| 20 | + 'form_id' => false, |
| 21 | + ) ); |
| 22 | + |
| 23 | + add_action( 'plugins_loaded', array( $this, 'add_hooks' ), 16 ); |
| 24 | + } |
15 | 25 |
|
16 | | -add_action( 'gfpdf_post_generate_and_save_pdf_notification', function ( $form, $entry, $settings, $notifications ) { |
17 | | - $form_id = 8; |
| 26 | + public function add_hooks() { |
| 27 | + if ( ! function_exists( 'gp_disable_entry_creation' ) ) { |
| 28 | + return; |
| 29 | + } |
18 | 30 |
|
19 | | - if ( ! function_exists( 'gp_disable_entry_creation' ) ) { |
20 | | - return; |
| 31 | + add_filter( 'gpdec_should_delete_entry_' . $this->_args['form_id'], '__return_false' ); |
| 32 | + add_action( 'gfpdf_post_generate_and_save_pdf_notification', array( $this, 'post_generate_and_save' ), 50, 4 ); |
| 33 | + add_action( 'shutdown', array( $this, 'shutdown' ) ); |
21 | 34 | } |
22 | 35 |
|
23 | | - if ( $form['id'] !== $form_id ) { |
24 | | - return; |
| 36 | + public function post_generate_and_save( $form, $entry, $settings, $notifications ) { |
| 37 | + if ( $form['id'] != $this->_args['form_id'] ) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + $this->deletion_queue[] = $entry; |
25 | 42 | } |
26 | 43 |
|
27 | | - gp_disable_entry_creation()->delete_form_entry( $entry ); |
28 | | -}, 50, 4 ); |
| 44 | + public function shutdown() { |
| 45 | + if ( empty( $this->deletion_queue ) ) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + foreach ( $this->deletion_queue as $entry ) { |
| 50 | + gp_disable_entry_creation()->delete_form_entry( $entry ); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/* |
| 56 | + * Basic Usage |
| 57 | + * |
| 58 | + * Uncomment the lines below (remove the preceding // on each line) and adjust the form ID accordingly. |
| 59 | + * You may also duplicate the class instantiation if this is required for more than one form. |
| 60 | + */ |
| 61 | + |
| 62 | +//new GPDEC_GFPDF_Delayed_Deletion( array( |
| 63 | +// 'form_id' => 3, |
| 64 | +//) ); |
0 commit comments