Skip to content

Instantly share code, notes, and snippets.

@n8kowald
Last active July 4, 2022 06:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save n8kowald/1ca0e5985c6a95106e05788ade0c1724 to your computer and use it in GitHub Desktop.
Save n8kowald/1ca0e5985c6a95106e05788ade0c1724 to your computer and use it in GitHub Desktop.
Enable Create and Update Gravity Form User Registrations
<?php
/**
* 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:
* Include this file inside functions.php and call gravity_forms_user_registration::init() inside an hook such as
* plugins_loaded:
*
* include 'gravity-forms-user-registration.php';
* add_action( 'plugins_loaded', array( 'gravity_forms_user_registration', 'init' ) );
*/
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', [
self::class,
'enable_create_and_update_feeds'
], 10, 2 );
add_filter( 'gform_form_args', [ self::class, 'fix_error_on_forms_with_create_and_udpate_feeds' ], 10, 1 );
add_filter( 'gform_gravityformsuserregistration_pre_process_feeds', [
self::class,
'use_update_feed_if_logged_in'
], 10, 3 );
}
/**
* @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 = [];
$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 [ $feed ];
}
}
return $feeds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment