|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Wiz // Gravity Forms // Delay a feed until User Registration is Complete. |
| 4 | + * |
| 5 | + * Instruction Video: https://www.loom.com/share/176456848c614ab6a3792ca1f9ed9c14 |
| 6 | + * |
| 7 | + * Delay a specified feed until user registration is complete. |
| 8 | + * This is useful when you have a feed that needs to be processed after user registration is complete. |
| 9 | + */ |
| 10 | +class GW_Delayed_Feed_Processing { |
| 11 | + |
| 12 | + private $_args = array(); |
| 13 | + |
| 14 | + public function __construct( $args = array() ) { |
| 15 | + // Ensure feed_slug is always passed; form_id is optional |
| 16 | + if ( empty( $args['feed_slug'] ) ) { |
| 17 | + wp_die( 'Feed slug is required.' ); |
| 18 | + } |
| 19 | + |
| 20 | + $this->_args = wp_parse_args( $args, array( |
| 21 | + 'form_id' => false, |
| 22 | + 'feed_slug' => $args['feed_slug'], |
| 23 | + ) ); |
| 24 | + |
| 25 | + add_filter( 'gform_is_delayed_pre_process_feed', array( $this, 'maybe_delay_feed' ), 10, 4 ); |
| 26 | + add_action( 'gform_user_registered', array( $this, 'process_feed' ), 10, 4 ); |
| 27 | + add_filter( 'gform_is_feed_asynchronous', array( $this, 'make_feed_async' ), 10, 4 ); |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + // Delays the feed until user registration is complete. |
| 32 | + public function maybe_delay_feed( $is_delayed, $form, $entry, $slug ) { |
| 33 | + |
| 34 | + if ( $slug == $this->_args['feed_slug'] && function_exists( 'gf_user_registration' ) ) { |
| 35 | + $user_id = gf_user_registration()->get_user_by_entry_id( rgar( $entry, 'id' ), true ); |
| 36 | + |
| 37 | + // Delay feed if user is not registered (user_id is blank). |
| 38 | + return rgblank( $user_id ); |
| 39 | + } |
| 40 | + |
| 41 | + return $is_delayed; |
| 42 | + } |
| 43 | + |
| 44 | + // Processes the feed after the user is registered |
| 45 | + public function process_feed( $user_id, $feed, $entry, $user_pass ) { |
| 46 | + |
| 47 | + // Feed slug is converted to function name. |
| 48 | + $feed_slug = str_replace( '-', '_', $this->_args['feed_slug'] ); |
| 49 | + if ( function_exists( $feed_slug ) ) { |
| 50 | + $feed_class = call_user_func( $feed_slug ); |
| 51 | + |
| 52 | + // Process the feed for the specified form or for all forms if no form_id is passed. |
| 53 | + if ( $feed_class && ( empty( $this->_args['form_id'] ) || rgar( $feed, 'form_id' ) == $this->_args['form_id'] ) ) { |
| 54 | + $feed_class->maybe_process_feed( $entry, $form ); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Ensure the feed is async, so it can be trigerred when our conditions are met (User Registration). |
| 60 | + public function make_feed_async( $is_asynchronous, $feed, $entry, $form ) { |
| 61 | + if ( rgar( $feed, 'addon_slug' ) == $this->_args['feed_slug'] && $feed['form_id'] == $this->_args['form_id'] ) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + return $is_asynchronous; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +# Configuration: Pass feed_slug (mandatory) and form_id (optional) |
| 69 | +new GW_Delayed_Feed_Processing( array( |
| 70 | + 'feed_slug' => 'gc-notion', // Specify the feed_slug (mandatory) |
| 71 | + 'form_id' => 6, // Optional: Specify the form_id (or apply to all forms). |
| 72 | +) ); |
0 commit comments