Last active
September 20, 2024 02:59
-
-
Save n8kowald/7bd0deefb9bd2817268b48f3b4cc05b5 to your computer and use it in GitHub Desktop.
Updated enable Create and Update Gravity Form User Registrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Note: DO NOT add the "<?php" tag if you're adding this code into functions.php or via a snippet. | |
| * It has only been added here so this gist gets PHP syntax highlighting. | |
| */ | |
| /** | |
| * This class gets around a limitation of the Gravity Forms User Registration Add-On that limits you to | |
| * one type of User Registration feed per form: Create OR Update. | |
| * https://www.gravityforms.com/add-ons/user-registration/. | |
| * | |
| * We use this plugin in membership application forms. | |
| * Membership application forms are used by both logged-in and logged-out users. | |
| * Being able to use a Create AND Update feed allows us share one membership form. | |
| * | |
| * Usage: copy this gist code into your functions.php or snippets area, and make sure to include the plugins_loaded action. | |
| */ | |
| class gravity_forms_user_registration { | |
| /** | |
| * Enable/disable both create and update feeds. | |
| * We use an Advanced Custom Field setting for this. | |
| * | |
| * @return bool | |
| */ | |
| public static function is_create_and_update_feeds_enabled() { | |
| return true; | |
| } | |
| /** | |
| * Entry into this class. | |
| * Include this class then use gravity_forms_user_registration::init() in your functions.php file. | |
| */ | |
| public static function init() { | |
| if ( self::is_create_and_update_feeds_enabled() ) { | |
| self::setup_hooks(); | |
| } | |
| } | |
| /** | |
| * @return bool | |
| */ | |
| public static function is_user_registration_plugin_installed() { | |
| return class_exists( '\GF_User_Registration' ); | |
| } | |
| /** | |
| * Setup the hooks that enable both Create and Update feeds. | |
| */ | |
| public static function setup_hooks() { | |
| add_filter( 'gform_userregistration_feed_settings_fields', array( self::class, 'enable_create_and_update_feeds'), 10, 2 ); | |
| add_filter( 'gform_form_args', array( self::class, 'fix_error_on_forms_with_create_and_udpate_feeds' ), 10, 1 ); | |
| add_filter( 'gform_gravityformsuserregistration_pre_process_feeds', array( self::class, 'use_update_feed_if_logged_in' ), 10, 3 ); | |
| add_filter( 'gform_is_feed_asynchronous', array( self::class, 'disable_async_processing_for_user_registration_feeds' ), 10, 4 ); | |
| } | |
| /** | |
| * @param $fields | |
| * @param $form | |
| * | |
| * @return mixed | |
| */ | |
| public static function enable_create_and_update_feeds( $fields, $form ) { | |
| unset( $fields['feed_settings']['fields'][1]['choices']['update']['disabled'] ); | |
| return $fields; | |
| } | |
| /** | |
| * Using a filter that fires early to disable an action which causes an error when both | |
| * a create and update feed exist and a user not logged in. | |
| * | |
| * @param $form_args | |
| * | |
| * @return mixed | |
| */ | |
| public static function fix_error_on_forms_with_create_and_udpate_feeds( $form_args ) { | |
| // User registration only returns an error when an update feed exists and a user is NOT logged in. | |
| if ( is_user_logged_in() || ! class_exists( '\GF_User_Registration' ) ) { | |
| return $form_args; | |
| } | |
| $gf_user_reg_feed_types = array(); | |
| $gf_user_reg = \GF_User_Registration::get_instance(); | |
| $feeds = $gf_user_reg->get_feeds( $form_args['form_id'] ); | |
| foreach ( $feeds as $feed ) { | |
| if ( $feed['is_active'] && | |
| $feed['addon_slug'] === 'gravityformsuserregistration' | |
| ) { | |
| $gf_user_reg_feed_types[] = rgars( $feed, 'meta/feedType' ); | |
| } | |
| } | |
| // A create AND update feed exists: | |
| // Disable the maybe_prepopulate_form method which prepopulates a form from a logged in | |
| // user (not relevant if not logged in), and returns an error if an update feed exists but a user is NOT logged in. | |
| if ( $gf_user_reg_feed_types === [ 'create', 'update' ] ) { | |
| remove_action( 'gform_pre_render', [ 'GF_User_Registration', 'maybe_prepopulate_form' ] ); | |
| } | |
| return $form_args; | |
| } | |
| /** | |
| * Detects a logged in user and returns the Update feed for these users if created. | |
| * | |
| * @param $feeds | |
| * @param $entry | |
| * @param $form | |
| * | |
| * @return array | |
| */ | |
| public static function use_update_feed_if_logged_in( $feeds, $entry, $form ) { | |
| if ( ! is_user_logged_in() || ! class_exists( '\GF_User_Registration' ) ) { | |
| return $feeds; | |
| } | |
| $gf_user_reg = \GF_User_Registration::get_instance(); | |
| $feeds = $gf_user_reg->get_feeds( $form['id'] ); | |
| foreach ( $feeds as $feed ) { | |
| if ( $feed['is_active'] && | |
| 'gravityformsuserregistration' === $feed['addon_slug'] && | |
| 'update' === $feed['meta']['feedType'] && | |
| $gf_user_reg->is_feed_condition_met( $feed, $form, $entry ) | |
| ) { | |
| return array( $feed ); | |
| } | |
| } | |
| return $feeds; | |
| } | |
| /** | |
| * gravityformsuserregistration can create two feeds: create and update. | |
| * Create is async disabled. | |
| * Update is async enabled. | |
| * | |
| * We want Update feeds to be async disabled also. | |
| * | |
| * @param bool $is_async_feed_enabled | |
| * @param array $feed | |
| * @param array $entry | |
| * @param array $form | |
| * | |
| * @return boolean | |
| */ | |
| public static function disable_async_processing_for_user_registration_feeds( bool $is_async_feed_enabled, $feed, $entry, $form ) { | |
| if ( ! empty( $feed['addon_slug'] ) && 'gravityformsuserregistration' === $feed['addon_slug'] ) { | |
| return false; | |
| } | |
| return $is_async_feed_enabled; | |
| } | |
| } | |
| // Finally, run the init method. | |
| // This is required to make everything work. | |
| add_action( 'plugins_loaded', array( 'gravity_forms_user_registration', 'init' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment