Skip to content

Commit 7ab5b0f

Browse files
authored
gw-user-registration-update-by-email.php: Added new_email_field_id option to allow updating a user's email via a separate field.
1 parent a041cf8 commit 7ab5b0f

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

gravity-forms/gw-user-registration-update-by-email.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
* Create User Registration feeds that will update the user by the submitted email address, allowing non-logged-in users
77
* to be targeted by GFUR update feeds.
88
*
9+
* Optionally supports changing the user's email address by specifying a separate "New Email" field via
10+
* the `new_email_field_id` argument.
11+
*
912
* Plugin Name: Gravity Forms User Registration - Update by Email
1013
* Plugin URI: https://gravitywiz.com/
1114
* Description: Create User Registration feeds that will update the user by the submitted email address, allowing non-logged-in users to be targeted by GFUR update feeds.
1215
* Author: Gravity Wiz
13-
* Version: 0.7
16+
* Version: 0.8
1417
* Author URI: https://gravitywiz.com/
1518
*/
1619
class GW_UR_Update_By_Email {
@@ -23,6 +26,16 @@ public function __construct( $args = array() ) {
2326
$this->_args = wp_parse_args( $args, array(
2427
'form_id' => false,
2528
'field_id' => false,
29+
/*
30+
* Specify a separate Email field that contains the user's new/desired email address.
31+
*
32+
* When set, the feed's Email mapping is used to look up the existing user (current email),
33+
* and the new email field value is used to update the user's email address. If the new email
34+
* field is left blank, the existing email is preserved.
35+
*
36+
* When not set, the feed's Email field is used for both lookup and update (default behavior).
37+
*/
38+
'new_email_field_id' => false,
2639
/*
2740
* Require that the logged-in user have a specific capability to be able to edit users by email.
2841
*
@@ -62,6 +75,7 @@ public function init() {
6275
add_action( 'gform_pre_process', array( $this, 'handle_validation' ) );
6376
add_filter( 'gform_entry_post_save', array( $this, 'add_created_by_by_email' ), 9, 2 );
6477
add_filter( 'gform_gravityformsuserregistration_pre_process_feeds', array( $this, 'filter_feeds' ), 10, 3 );
78+
add_filter( 'gform_gravityformsuserregistration_pre_process_feeds', array( $this, 'maybe_update_email' ), 11, 3 );
6579
}
6680

6781
}
@@ -136,6 +150,33 @@ public function get_user_by_feed_email( $feed, $entry ) {
136150
return get_user_by( 'email', $email );
137151
}
138152

153+
/**
154+
* If a new email field is configured and has a value, swap the feed's email mapping so GFUR
155+
* writes the new email. Runs after filter_feeds (priority 11) so user lookup has already
156+
* completed using the original email field.
157+
*/
158+
public function maybe_update_email( $feeds, $entry, $form ) {
159+
160+
if ( ! $this->_args['new_email_field_id'] ) {
161+
return $feeds;
162+
}
163+
164+
$new_email = rgar( $entry, $this->_args['new_email_field_id'] );
165+
if ( empty( $new_email ) ) {
166+
return $feeds;
167+
}
168+
169+
foreach ( $feeds as &$feed ) {
170+
if ( ! $this->is_update_by_email( $feed ) ) {
171+
continue;
172+
}
173+
174+
$feed['meta']['email'] = $this->_args['new_email_field_id'];
175+
}
176+
177+
return $feeds;
178+
}
179+
139180
public function filter_feeds( $feeds, $entry, $form ) {
140181

141182
$feed = $this->get_single_submission_feed( $entry, $form );

0 commit comments

Comments
 (0)