Completed
Pull Request — develop (#843)
by Zack
05:39
created
includes/extensions/edit-entry/class-edit-entry-user-registration.php 1 patch
Indentation   +257 added lines, -257 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
11 11
  */
12 12
 
13 13
 if ( ! defined( 'WPINC' ) ) {
14
-    die;
14
+	die;
15 15
 }
16 16
 
17 17
 /**
@@ -22,102 +22,102 @@  discard block
 block discarded – undo
22 22
 	/**
23 23
 	 * @var GravityView_Edit_Entry
24 24
 	 */
25
-    protected $loader;
25
+	protected $loader;
26 26
 
27
-    /**
28
-     * @var WP_User|null Temporary storage used by restore_user_details()
29
-     */
30
-    private $_user_before_update = null;
27
+	/**
28
+	 * @var WP_User|null Temporary storage used by restore_user_details()
29
+	 */
30
+	private $_user_before_update = null;
31 31
 
32
-    function __construct( GravityView_Edit_Entry $loader ) {
33
-        $this->loader = $loader;
34
-    }
32
+	function __construct( GravityView_Edit_Entry $loader ) {
33
+		$this->loader = $loader;
34
+	}
35 35
 
36 36
 	/**
37 37
 	 * @since 1.11
38 38
 	 */
39 39
 	public function load() {
40 40
 		add_action( 'wp', array( $this, 'add_hooks' ), 10 );
41
-    }
41
+	}
42 42
 
43 43
 	/**
44 44
 	 * Add hooks to trigger updating the user
45 45
 	 *
46 46
 	 * @since 1.18
47 47
 	 */
48
-    public function add_hooks() {
49
-
50
-	    /**
51
-	     * @filter `gravityview/edit_entry/user_registration/trigger_update` Choose whether to update user information via User Registration add-on when an entry is updated?
52
-	     * @since 1.11
53
-	     * @param boolean $boolean Whether to trigger update on user registration (default: true)
54
-	     */
55
-	    if( apply_filters( 'gravityview/edit_entry/user_registration/trigger_update', true ) ) {
56
-
57
-	    	add_action( 'gravityview/edit_entry/after_update' , array( $this, 'update_user' ), 10, 2 );
58
-
59
-		    // last resort in case the current user display name don't match any of the defaults
60
-		    add_action( 'gform_user_updated', array( $this, 'restore_display_name' ), 10, 4 );
61
-	    }
62
-    }
63
-
64
-    /**
65
-     * Update the WordPress user profile based on the GF User Registration create feed
66
-     *
67
-     * @since 1.11
68
-     *
69
-     * @param array $form Gravity Forms form array
70
-     * @param string $entry_id Gravity Forms entry ID
71
-     * @return void
72
-     */
73
-    public function update_user( $form = array(), $entry_id = 0 ) {
74
-
75
-        if( ! class_exists( 'GFAPI' ) || ! class_exists( 'GF_User_Registration' ) ) {
76
-	        do_action( 'gravityview_log_error', __METHOD__ . ': GFAPI or User Registration class not found; not updating the user' );
77
-	        return;
78
-        } elseif( empty( $entry_id ) ) {
79
-        	do_action( 'gravityview_log_error', __METHOD__ . ': Entry ID is empty; not updating the user', $entry_id );
80
-	        return;
81
-        }
82
-
83
-        /** @var GF_User_Registration $gf_user_registration */
84
-        $gf_user_registration = GF_User_Registration::get_instance();
85
-
86
-        $entry = GFAPI::get_entry( $entry_id );
87
-
88
-	    /**
89
-	     * @filter `gravityview/edit_entry/user_registration/entry` Modify entry details before updating the user via User Registration add-on
90
-	     * @since 1.11
91
-	     * @param array $entry Gravity Forms entry
92
-	     * @param array $form Gravity Forms form
93
-	     */
94
-        $entry = apply_filters( 'gravityview/edit_entry/user_registration/entry', $entry, $form );
95
-
96
-	    $config = $this->get_feed_configuration( $entry, $form );
97
-
98
-        // Make sure the feed is active
99
-	    if ( ! rgar( $config, 'is_active', false ) ) {
48
+	public function add_hooks() {
49
+
50
+		/**
51
+		 * @filter `gravityview/edit_entry/user_registration/trigger_update` Choose whether to update user information via User Registration add-on when an entry is updated?
52
+		 * @since 1.11
53
+		 * @param boolean $boolean Whether to trigger update on user registration (default: true)
54
+		 */
55
+		if( apply_filters( 'gravityview/edit_entry/user_registration/trigger_update', true ) ) {
56
+
57
+			add_action( 'gravityview/edit_entry/after_update' , array( $this, 'update_user' ), 10, 2 );
58
+
59
+			// last resort in case the current user display name don't match any of the defaults
60
+			add_action( 'gform_user_updated', array( $this, 'restore_display_name' ), 10, 4 );
61
+		}
62
+	}
63
+
64
+	/**
65
+	 * Update the WordPress user profile based on the GF User Registration create feed
66
+	 *
67
+	 * @since 1.11
68
+	 *
69
+	 * @param array $form Gravity Forms form array
70
+	 * @param string $entry_id Gravity Forms entry ID
71
+	 * @return void
72
+	 */
73
+	public function update_user( $form = array(), $entry_id = 0 ) {
74
+
75
+		if( ! class_exists( 'GFAPI' ) || ! class_exists( 'GF_User_Registration' ) ) {
76
+			do_action( 'gravityview_log_error', __METHOD__ . ': GFAPI or User Registration class not found; not updating the user' );
100 77
 			return;
101
-	    }
78
+		} elseif( empty( $entry_id ) ) {
79
+			do_action( 'gravityview_log_error', __METHOD__ . ': Entry ID is empty; not updating the user', $entry_id );
80
+			return;
81
+		}
82
+
83
+		/** @var GF_User_Registration $gf_user_registration */
84
+		$gf_user_registration = GF_User_Registration::get_instance();
85
+
86
+		$entry = GFAPI::get_entry( $entry_id );
102 87
 
103
-	    // If an Update feed, make sure the conditions are met.
104
-	    if( rgars( $config, 'meta/feedType' ) === 'update' ) {
105
-	    	if( ! $gf_user_registration->is_feed_condition_met( $config, $form, $entry ) ) {
106
-			    return;
107
-		    }
108
-	    }
88
+		/**
89
+		 * @filter `gravityview/edit_entry/user_registration/entry` Modify entry details before updating the user via User Registration add-on
90
+		 * @since 1.11
91
+		 * @param array $entry Gravity Forms entry
92
+		 * @param array $form Gravity Forms form
93
+		 */
94
+		$entry = apply_filters( 'gravityview/edit_entry/user_registration/entry', $entry, $form );
95
+
96
+		$config = $this->get_feed_configuration( $entry, $form );
97
+
98
+		// Make sure the feed is active
99
+		if ( ! rgar( $config, 'is_active', false ) ) {
100
+			return;
101
+		}
109 102
 
110
-        // The priority is set to 3 so that default priority (10) will still override it
111
-        add_filter( 'send_password_change_email', '__return_false', 3 );
112
-        add_filter( 'send_email_change_email', '__return_false', 3 );
103
+		// If an Update feed, make sure the conditions are met.
104
+		if( rgars( $config, 'meta/feedType' ) === 'update' ) {
105
+			if( ! $gf_user_registration->is_feed_condition_met( $config, $form, $entry ) ) {
106
+				return;
107
+			}
108
+		}
113 109
 
114
-        // Trigger the User Registration update user method
115
-        $gf_user_registration->update_user( $entry, $form, $config );
110
+		// The priority is set to 3 so that default priority (10) will still override it
111
+		add_filter( 'send_password_change_email', '__return_false', 3 );
112
+		add_filter( 'send_email_change_email', '__return_false', 3 );
116 113
 
117
-        remove_filter( 'send_password_change_email', '__return_false', 3 );
118
-        remove_filter( 'send_email_change_email', '__return_false', 3 );
114
+		// Trigger the User Registration update user method
115
+		$gf_user_registration->update_user( $entry, $form, $config );
119 116
 
120
-    }
117
+		remove_filter( 'send_password_change_email', '__return_false', 3 );
118
+		remove_filter( 'send_email_change_email', '__return_false', 3 );
119
+
120
+	}
121 121
 
122 122
 	/**
123 123
 	 * Get the User Registration feed configuration for the entry & form
@@ -132,185 +132,185 @@  discard block
 block discarded – undo
132 132
 	 *
133 133
 	 * @return array
134 134
 	 */
135
-    public function get_feed_configuration( $entry, $form ) {
136
-
137
-	    /** @var GF_User_Registration $gf_user_registration */
138
-	    $gf_user_registration = GF_User_Registration::get_instance();
139
-
140
-	    $config = $gf_user_registration->get_single_submission_feed( $entry, $form );
141
-
142
-	    /**
143
-	     * @filter `gravityview/edit_entry/user_registration/preserve_role` Keep the current user role or override with the role defined in the Create feed
144
-	     * @since 1.15
145
-	     * @param[in,out] boolean $preserve_role Preserve current user role Default: true
146
-	     * @param[in] array $config Gravity Forms User Registration feed configuration for the form
147
-	     * @param[in] array $form Gravity Forms form array
148
-	     * @param[in] array $entry Gravity Forms entry being edited
149
-	     */
150
-	    $preserve_role = apply_filters( 'gravityview/edit_entry/user_registration/preserve_role', true, $config, $form, $entry );
151
-
152
-	    if( $preserve_role ) {
153
-		    $config['meta']['role'] = 'gfur_preserve_role';
154
-	    }
155
-
156
-	    $displayname = $this->match_current_display_name( $entry['created_by'] );
157
-
158
-	    /**
159
-	     * Make sure the current display name is not changed with the update user method.
160
-	     * @since 1.15
161
-	     */
162
-	    $config['meta']['displayname'] = $displayname ? $displayname : $config['meta']['displayname'];
163
-
164
-	    /**
165
-	     * @filter `gravityview/edit_entry/user_registration/config` Modify the User Registration Addon feed configuration
166
-	     * @since 1.14
167
-	     * @param[in,out] array $config Gravity Forms User Registration feed configuration for the form
168
-	     * @param[in] array $form Gravity Forms form array
169
-	     * @param[in] array $entry Gravity Forms entry being edited
170
-	     */
171
-	    $config = apply_filters( 'gravityview/edit_entry/user_registration/config', $config, $form, $entry );
172
-
173
-	    return $config;
174
-    }
175
-
176
-    /**
177
-     * Calculate the user display name format
178
-     *
179
-     * @since 1.15
180
-     * @since 1.20 Returns false if user not found at $user_id
181
-     *
182
-     * @param int $user_id WP User ID
183
-     * @return false|string Display name format as used inside Gravity Forms User Registration. Returns false if user not found.
184
-     */
185
-    public function match_current_display_name( $user_id ) {
186
-
187
-        $user = get_userdata( $user_id );
188
-
189
-        if( ! $user ) {
190
-        	return false;
191
-        }
192
-
193
-        $names = $this->generate_display_names( $user );
194
-
195
-        $format = array_search( $user->display_name, $names, true );
196
-
197
-        /**
198
-         * In case we can't find the current display name format, trigger last resort method at the 'gform_user_updated' hook
199
-         * @see restore_display_name
200
-         */
201
-        if( false === $format ) {
202
-            $this->_user_before_update = $user;
203
-        }
204
-
205
-        return $format;
206
-    }
207
-
208
-    /**
209
-     * Generate an array of all the user display names possibilities
210
-     *
211
-     * @since 1.15
212
-     *
213
-     * @param object $profileuser WP_User object
214
-     * @return array List all the possible display names for a certain User object
215
-     */
216
-    public function generate_display_names( $profileuser ) {
217
-
218
-        $public_display = array();
219
-        $public_display['nickname']  = $profileuser->nickname;
220
-        $public_display['username']  = $profileuser->user_login;
221
-
222
-        if ( !empty($profileuser->first_name) ) {
223
-	        $public_display['firstname'] = $profileuser->first_name;
224
-        }
225
-
226
-        if ( !empty($profileuser->last_name) ) {
227
-	        $public_display['lastname'] = $profileuser->last_name;
228
-        }
229
-
230
-        if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) {
231
-            $public_display['firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name;
232
-            $public_display['lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name;
233
-        }
234
-
235
-        $public_display = array_map( 'trim', $public_display );
236
-        $public_display = array_unique( $public_display );
237
-
238
-        return $public_display;
239
-    }
240
-
241
-
242
-    /**
243
-     * Restore the Display Name and roles of a user after being updated by Gravity Forms User Registration Addon
244
-     *
245
-     * @see GFUser::update_user()
246
-     * @param int $user_id WP User ID that was updated by Gravity Forms User Registration Addon
247
-     * @param array $config Gravity Forms User Registration Addon form feed configuration
248
-     * @param array $entry The Gravity Forms entry that was just updated
249
-     * @param string $password User password
250
-     * @return int|false|WP_Error|null True: User updated; False: $user_id not a valid User ID; WP_Error: User update error; Null: Method didn't process
251
-     */
252
-    public function restore_display_name( $user_id = 0, $config = array(), $entry = array(), $password = '' ) {
253
-
254
-        /**
255
-         * @filter `gravityview/edit_entry/restore_display_name` Whether display names should be restored to before updating an entry.
256
-         * Otherwise, display names will be reset to the format specified in Gravity Forms User Registration "Update" feed
257
-         * @since 1.14.4
258
-         * @param boolean $restore_display_name Restore Display Name? Default: true
259
-         */
260
-        $restore_display_name = apply_filters( 'gravityview/edit_entry/restore_display_name', true );
261
-
262
-        $is_update_feed = ( $config && rgars( $config, 'meta/feed_type') === 'update' );
263
-
264
-        /**
265
-         * Don't restore display name:
266
-         *   - either disabled,
267
-         *   - or it is an Update feed (we only care about Create feed)
268
-         *   - or we don't need as we found the correct format before updating user.
269
-         * @since 1.14.4
270
-         */
271
-        if( ! $restore_display_name || $is_update_feed || is_null( $this->_user_before_update ) ) {
272
-            return null;
273
-        }
274
-
275
-        $user_after_update = get_userdata( $user_id );
276
-
277
-        // User not found
278
-	    if ( ! $user_after_update ) {
279
-	    	do_action('gravityview_log_error', __METHOD__ . sprintf( ' - User not found at $user_id #%d', $user_id ) );
280
-		    return false;
281
-	    }
282
-
283
-        $restored_user = $user_after_update;
284
-
285
-	    // Restore previous display_name
286
-        $restored_user->display_name = $this->_user_before_update->display_name;
287
-
288
-	    // Don't have WP update the password.
289
-	    unset( $restored_user->data->user_pass, $restored_user->user_pass );
290
-
291
-        /**
292
-         * Modify the user data after updated by Gravity Forms User Registration but before restored by GravityView
293
-         * @since 1.14
294
-         * @param WP_User $restored_user The user with restored details about to be updated by wp_update_user()
295
-         * @param WP_User $user_before_update The user before being updated by Gravity Forms User Registration
296
-         * @param WP_User $user_after_update The user after being updated by Gravity Forms User Registration
297
-         * @param array   $entry The Gravity Forms entry that was just updated
298
-         */
299
-        $restored_user = apply_filters( 'gravityview/edit_entry/user_registration/restored_user', $restored_user, $this->_user_before_update, $user_after_update, $entry );
300
-
301
-        $updated = wp_update_user( $restored_user );
302
-
303
-        if( is_wp_error( $updated ) ) {
304
-            do_action('gravityview_log_error', __METHOD__ . sprintf( ' - There was an error updating user #%d details', $user_id ), $updated );
305
-        } else {
306
-            do_action('gravityview_log_debug', __METHOD__ . sprintf( ' - User #%d details restored', $user_id ) );
307
-        }
308
-
309
-        $this->_user_before_update = null;
310
-
311
-        unset( $restored_user, $user_after_update );
312
-
313
-        return $updated;
314
-    }
135
+	public function get_feed_configuration( $entry, $form ) {
136
+
137
+		/** @var GF_User_Registration $gf_user_registration */
138
+		$gf_user_registration = GF_User_Registration::get_instance();
139
+
140
+		$config = $gf_user_registration->get_single_submission_feed( $entry, $form );
141
+
142
+		/**
143
+		 * @filter `gravityview/edit_entry/user_registration/preserve_role` Keep the current user role or override with the role defined in the Create feed
144
+		 * @since 1.15
145
+		 * @param[in,out] boolean $preserve_role Preserve current user role Default: true
146
+		 * @param[in] array $config Gravity Forms User Registration feed configuration for the form
147
+		 * @param[in] array $form Gravity Forms form array
148
+		 * @param[in] array $entry Gravity Forms entry being edited
149
+		 */
150
+		$preserve_role = apply_filters( 'gravityview/edit_entry/user_registration/preserve_role', true, $config, $form, $entry );
151
+
152
+		if( $preserve_role ) {
153
+			$config['meta']['role'] = 'gfur_preserve_role';
154
+		}
155
+
156
+		$displayname = $this->match_current_display_name( $entry['created_by'] );
157
+
158
+		/**
159
+		 * Make sure the current display name is not changed with the update user method.
160
+		 * @since 1.15
161
+		 */
162
+		$config['meta']['displayname'] = $displayname ? $displayname : $config['meta']['displayname'];
163
+
164
+		/**
165
+		 * @filter `gravityview/edit_entry/user_registration/config` Modify the User Registration Addon feed configuration
166
+		 * @since 1.14
167
+		 * @param[in,out] array $config Gravity Forms User Registration feed configuration for the form
168
+		 * @param[in] array $form Gravity Forms form array
169
+		 * @param[in] array $entry Gravity Forms entry being edited
170
+		 */
171
+		$config = apply_filters( 'gravityview/edit_entry/user_registration/config', $config, $form, $entry );
172
+
173
+		return $config;
174
+	}
175
+
176
+	/**
177
+	 * Calculate the user display name format
178
+	 *
179
+	 * @since 1.15
180
+	 * @since 1.20 Returns false if user not found at $user_id
181
+	 *
182
+	 * @param int $user_id WP User ID
183
+	 * @return false|string Display name format as used inside Gravity Forms User Registration. Returns false if user not found.
184
+	 */
185
+	public function match_current_display_name( $user_id ) {
186
+
187
+		$user = get_userdata( $user_id );
188
+
189
+		if( ! $user ) {
190
+			return false;
191
+		}
192
+
193
+		$names = $this->generate_display_names( $user );
194
+
195
+		$format = array_search( $user->display_name, $names, true );
196
+
197
+		/**
198
+		 * In case we can't find the current display name format, trigger last resort method at the 'gform_user_updated' hook
199
+		 * @see restore_display_name
200
+		 */
201
+		if( false === $format ) {
202
+			$this->_user_before_update = $user;
203
+		}
204
+
205
+		return $format;
206
+	}
207
+
208
+	/**
209
+	 * Generate an array of all the user display names possibilities
210
+	 *
211
+	 * @since 1.15
212
+	 *
213
+	 * @param object $profileuser WP_User object
214
+	 * @return array List all the possible display names for a certain User object
215
+	 */
216
+	public function generate_display_names( $profileuser ) {
217
+
218
+		$public_display = array();
219
+		$public_display['nickname']  = $profileuser->nickname;
220
+		$public_display['username']  = $profileuser->user_login;
221
+
222
+		if ( !empty($profileuser->first_name) ) {
223
+			$public_display['firstname'] = $profileuser->first_name;
224
+		}
225
+
226
+		if ( !empty($profileuser->last_name) ) {
227
+			$public_display['lastname'] = $profileuser->last_name;
228
+		}
229
+
230
+		if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) {
231
+			$public_display['firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name;
232
+			$public_display['lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name;
233
+		}
234
+
235
+		$public_display = array_map( 'trim', $public_display );
236
+		$public_display = array_unique( $public_display );
237
+
238
+		return $public_display;
239
+	}
240
+
241
+
242
+	/**
243
+	 * Restore the Display Name and roles of a user after being updated by Gravity Forms User Registration Addon
244
+	 *
245
+	 * @see GFUser::update_user()
246
+	 * @param int $user_id WP User ID that was updated by Gravity Forms User Registration Addon
247
+	 * @param array $config Gravity Forms User Registration Addon form feed configuration
248
+	 * @param array $entry The Gravity Forms entry that was just updated
249
+	 * @param string $password User password
250
+	 * @return int|false|WP_Error|null True: User updated; False: $user_id not a valid User ID; WP_Error: User update error; Null: Method didn't process
251
+	 */
252
+	public function restore_display_name( $user_id = 0, $config = array(), $entry = array(), $password = '' ) {
253
+
254
+		/**
255
+		 * @filter `gravityview/edit_entry/restore_display_name` Whether display names should be restored to before updating an entry.
256
+		 * Otherwise, display names will be reset to the format specified in Gravity Forms User Registration "Update" feed
257
+		 * @since 1.14.4
258
+		 * @param boolean $restore_display_name Restore Display Name? Default: true
259
+		 */
260
+		$restore_display_name = apply_filters( 'gravityview/edit_entry/restore_display_name', true );
261
+
262
+		$is_update_feed = ( $config && rgars( $config, 'meta/feed_type') === 'update' );
263
+
264
+		/**
265
+		 * Don't restore display name:
266
+		 *   - either disabled,
267
+		 *   - or it is an Update feed (we only care about Create feed)
268
+		 *   - or we don't need as we found the correct format before updating user.
269
+		 * @since 1.14.4
270
+		 */
271
+		if( ! $restore_display_name || $is_update_feed || is_null( $this->_user_before_update ) ) {
272
+			return null;
273
+		}
274
+
275
+		$user_after_update = get_userdata( $user_id );
276
+
277
+		// User not found
278
+		if ( ! $user_after_update ) {
279
+			do_action('gravityview_log_error', __METHOD__ . sprintf( ' - User not found at $user_id #%d', $user_id ) );
280
+			return false;
281
+		}
282
+
283
+		$restored_user = $user_after_update;
284
+
285
+		// Restore previous display_name
286
+		$restored_user->display_name = $this->_user_before_update->display_name;
287
+
288
+		// Don't have WP update the password.
289
+		unset( $restored_user->data->user_pass, $restored_user->user_pass );
290
+
291
+		/**
292
+		 * Modify the user data after updated by Gravity Forms User Registration but before restored by GravityView
293
+		 * @since 1.14
294
+		 * @param WP_User $restored_user The user with restored details about to be updated by wp_update_user()
295
+		 * @param WP_User $user_before_update The user before being updated by Gravity Forms User Registration
296
+		 * @param WP_User $user_after_update The user after being updated by Gravity Forms User Registration
297
+		 * @param array   $entry The Gravity Forms entry that was just updated
298
+		 */
299
+		$restored_user = apply_filters( 'gravityview/edit_entry/user_registration/restored_user', $restored_user, $this->_user_before_update, $user_after_update, $entry );
300
+
301
+		$updated = wp_update_user( $restored_user );
302
+
303
+		if( is_wp_error( $updated ) ) {
304
+			do_action('gravityview_log_error', __METHOD__ . sprintf( ' - There was an error updating user #%d details', $user_id ), $updated );
305
+		} else {
306
+			do_action('gravityview_log_debug', __METHOD__ . sprintf( ' - User #%d details restored', $user_id ) );
307
+		}
308
+
309
+		$this->_user_before_update = null;
310
+
311
+		unset( $restored_user, $user_after_update );
312
+
313
+		return $updated;
314
+	}
315 315
 
316 316
 } //end class
Please login to merge, or discard this patch.