GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-edit-entry-user-registration.php
Go to the documentation of this file.
1 <?php
2 /**
3  * GravityView Edit Entry - Sync User Registration (when using the GF User Registration Add-on)
4  *
5  * @since 1.11
6  * @package GravityView
7  * @license GPL2+
8  * @author GravityView <[email protected]>
9  * @link http://gravityview.co
10  * @copyright Copyright 2015, Katz Web Services, Inc.
11  */
12 
13 if ( ! defined( 'WPINC' ) ) {
14  die;
15 }
16 
17 /**
18  * GravityView Edit Entry - Sync User Registration (when using the GF User Registration Add-on)
19  */
21 
22  /**
23  * @var GravityView_Edit_Entry $loader
24  */
25  protected $loader;
26 
27  /**
28  * @var WP_User|null Temporary storage used by restore_user_details()
29  */
30  private $_user_before_update = null;
31 
33  $this->loader = $loader;
34  }
35 
36  /**
37  * @since 1.11
38  */
39  public function load() {
40  add_action( 'wp', array( $this, 'add_hooks' ), 10 );
41  }
42 
43  /**
44  * Add hooks to trigger updating the user
45  *
46  * @since 1.18
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  // Only proceed if the registration class exists and is active.
76  if ( ! class_exists( 'GF_User_Registration' ) ) {
77  return;
78  }
79 
80  if( ! class_exists( 'GFAPI' ) ) {
81  gravityview()->log->error( 'GFAPI class not found; not updating the user' );
82  return;
83  }
84 
85  if( empty( $entry_id ) ) {
86  gravityview()->log->error( 'Entry ID is empty [{entry_id}]; not updating the user', array( 'entry_id' => $entry_id ) );
87  return;
88  }
89 
90  $gf_user_registration = GF_User_Registration::get_instance();
91 
92  $entry = GFAPI::get_entry( $entry_id );
93 
94  /**
95  * @filter `gravityview/edit_entry/user_registration/entry` Modify entry details before updating the user via User Registration add-on
96  * @since 1.11
97  * @param array $entry Gravity Forms entry
98  * @param array $form Gravity Forms form
99  */
100  $entry = apply_filters( 'gravityview/edit_entry/user_registration/entry', $entry, $form );
101 
102  $config = $this->get_feed_configuration( $entry, $form );
103 
104  // Make sure the feed is active
105  if ( ! \GV\Utils::get( $config, 'is_active', false ) ) {
106  return;
107  }
108 
109  // If an Update feed, make sure the conditions are met.
110  if ( \GV\Utils::get( $config, 'meta/feedType' ) === 'update' ) {
111  if( ! $gf_user_registration->is_feed_condition_met( $config, $form, $entry ) ) {
112  return;
113  }
114  }
115 
116  // Do not update user if the user hasn't been registered (happens when manual activation is enabled in User Registration feed)
117  $username = \GV\Utils::get( $config, 'meta/username', null );
118  if ( ! isset( $entry[ $username ] ) || ! get_user_by( 'login', $entry[ $username ] ) ) {
119  return;
120  }
121 
122  // The priority is set to 3 so that default priority (10) will still override it
123  add_filter( 'send_password_change_email', '__return_false', 3 );
124  add_filter( 'send_email_change_email', '__return_false', 3 );
125 
126  // Trigger the User Registration update user method
127  $gf_user_registration->update_user( $entry, $form, $config );
128 
129  remove_filter( 'send_password_change_email', '__return_false', 3 );
130  remove_filter( 'send_email_change_email', '__return_false', 3 );
131 
132  // Prevent double-triggering by removing the hook
133  remove_action( 'gravityview/edit_entry/after_update' , array( $this, 'update_user' ), 10 );
134  }
135 
136  /**
137  * Get the User Registration feed configuration for the entry & form
138  *
139  * @uses GF_User_Registration::get_single_submission_feed
140  * @uses GravityView_Edit_Entry_User_Registration::match_current_display_name
141  *
142  * @since 1.20
143  *
144  * @param $entry
145  * @param $form
146  *
147  * @return array
148  */
149  public function get_feed_configuration( $entry, $form ) {
150 
151  $gf_user_registration = GF_User_Registration::get_instance();
152 
153  $config = $gf_user_registration->get_single_submission_feed( $entry, $form );
154 
155  /**
156  * @filter `gravityview/edit_entry/user_registration/preserve_role` Keep the current user role or override with the role defined in the Create feed
157  * @since 1.15
158  * @param boolean $preserve_role Preserve current user role Default: true
159  * @param array $config Gravity Forms User Registration feed configuration for the form
160  * @param array $form Gravity Forms form array
161  * @param array $entry Gravity Forms entry being edited
162  */
163  $preserve_role = apply_filters( 'gravityview/edit_entry/user_registration/preserve_role', true, $config, $form, $entry );
164 
165  if( $preserve_role ) {
166  $config['meta']['role'] = 'gfur_preserve_role';
167  }
168 
169  $displayname = $this->match_current_display_name( $entry['created_by'] );
170 
171  /**
172  * Make sure the current display name is not changed with the update user method.
173  * @since 1.15
174  */
175  $config['meta']['displayname'] = $displayname ? $displayname : $config['meta']['displayname'];
176 
177  /**
178  * @filter `gravityview/edit_entry/user_registration/config` Modify the User Registration Addon feed configuration
179  * @since 1.14
180  * @param array $config Gravity Forms User Registration feed configuration for the form
181  * @param array $form Gravity Forms form array
182  * @param array $entry Gravity Forms entry being edited
183  */
184  $config = apply_filters( 'gravityview/edit_entry/user_registration/config', $config, $form, $entry );
185 
186  return $config;
187  }
188 
189  /**
190  * Calculate the user display name format
191  *
192  * @since 1.15
193  * @since 1.20 Returns false if user not found at $user_id
194  *
195  * @param int $user_id WP User ID
196  * @return false|string Display name format as used inside Gravity Forms User Registration. Returns false if user not found.
197  */
198  public function match_current_display_name( $user_id ) {
199 
200  $user = get_userdata( $user_id );
201 
202  if( ! $user ) {
203  return false;
204  }
205 
206  $names = $this->generate_display_names( $user );
207 
208  $format = array_search( $user->display_name, $names, true );
209 
210  /**
211  * In case we can't find the current display name format, trigger last resort method at the 'gform_user_updated' hook
212  * @see restore_display_name
213  */
214  if( false === $format ) {
215  $this->_user_before_update = $user;
216  }
217 
218  return $format;
219  }
220 
221  /**
222  * Generate an array of all the user display names possibilities
223  *
224  * @since 1.15
225  *
226  * @param object $profileuser WP_User object
227  * @return array List all the possible display names for a certain User object
228  */
229  public function generate_display_names( $profileuser ) {
230 
231  $public_display = array();
232  $public_display['nickname'] = $profileuser->nickname;
233  $public_display['username'] = $profileuser->user_login;
234 
235  if ( !empty($profileuser->first_name) ) {
236  $public_display['firstname'] = $profileuser->first_name;
237  }
238 
239  if ( !empty($profileuser->last_name) ) {
240  $public_display['lastname'] = $profileuser->last_name;
241  }
242 
243  if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) {
244  $public_display['firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name;
245  $public_display['lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name;
246  }
247 
248  $public_display = array_map( 'trim', $public_display );
249  $public_display = array_unique( $public_display );
250 
251  return $public_display;
252  }
253 
254 
255  /**
256  * Restore the Display Name and roles of a user after being updated by Gravity Forms User Registration Addon
257  *
258  * @see GFUser::update_user()
259  * @param int $user_id WP User ID that was updated by Gravity Forms User Registration Addon
260  * @param array $config Gravity Forms User Registration Addon form feed configuration
261  * @param array $entry The Gravity Forms entry that was just updated
262  * @param string $password User password
263  * @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
264  */
265  public function restore_display_name( $user_id = 0, $config = array(), $entry = array(), $password = '' ) {
266 
267  /**
268  * @filter `gravityview/edit_entry/restore_display_name` Whether display names should be restored to before updating an entry.
269  * Otherwise, display names will be reset to the format specified in Gravity Forms User Registration "Update" feed
270  * @since 1.14.4
271  * @param boolean $restore_display_name Restore Display Name? Default: true
272  */
273  $restore_display_name = apply_filters( 'gravityview/edit_entry/restore_display_name', true );
274 
275  $is_update_feed = ( $config && \GV\Utils::get( $config, 'meta/feed_type' ) === 'update' );
276 
277  /**
278  * Don't restore display name:
279  * - either disabled,
280  * - or it is an Update feed (we only care about Create feed)
281  * - or we don't need as we found the correct format before updating user.
282  * @since 1.14.4
283  */
284  if( ! $restore_display_name || $is_update_feed || is_null( $this->_user_before_update ) ) {
285  return null;
286  }
287 
288  $user_after_update = get_userdata( $user_id );
289 
290  // User not found
291  if ( ! $user_after_update ) {
292  gravityview()->log->error( 'User not found at $user_id #{user_id}', array( 'user_id' => $user_id ) );
293  return false;
294  }
295 
296  $restored_user = $user_after_update;
297 
298  // Restore previous display_name
299  $restored_user->display_name = $this->_user_before_update->display_name;
300 
301  // Don't have WP update the password.
302  unset( $restored_user->data->user_pass, $restored_user->user_pass );
303 
304  /**
305  * Modify the user data after updated by Gravity Forms User Registration but before restored by GravityView
306  * @since 1.14
307  * @param WP_User $restored_user The user with restored details about to be updated by wp_update_user()
308  * @param WP_User $user_before_update The user before being updated by Gravity Forms User Registration
309  * @param WP_User $user_after_update The user after being updated by Gravity Forms User Registration
310  * @param array $entry The Gravity Forms entry that was just updated
311  */
312  $restored_user = apply_filters( 'gravityview/edit_entry/user_registration/restored_user', $restored_user, $this->_user_before_update, $user_after_update, $entry );
313 
314  $updated = wp_update_user( $restored_user );
315 
316  if( is_wp_error( $updated ) ) {
317  gravityview()->log->error( 'There was an error updating user #{user_id} details', array( 'user_id' => $user_id, 'data' => $updated ) );
318  } else {
319  gravityview()->log->debug( 'User #{user_id} details restored', array( 'user_id' => $user_id ) );
320  }
321 
322  $this->_user_before_update = null;
323 
324  unset( $restored_user, $user_after_update );
325 
326  return $updated;
327  }
328 
329 } //end class
$_user_before_update
$loader
if(empty( $value)) $user
if(gv_empty( $field['value'], false, false)) $format
get_feed_configuration( $entry, $form)
Get the User Registration feed configuration for the entry & form.
if(gravityview() ->plugin->is_GF_25()) $form
__construct(GravityView_Edit_Entry $loader)
update_user( $form=array(), $entry_id=0)
Update the WordPress user profile based on the GF User Registration create feed.
restore_display_name( $user_id=0, $config=array(), $entry=array(), $password='')
Restore the Display Name and roles of a user after being updated by Gravity Forms User Registration A...
generate_display_names( $profileuser)
Generate an array of all the user display names possibilities.
add_hooks()
Add hooks to trigger updating the user.
match_current_display_name( $user_id)
Calculate the user display name format.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
$entry
Definition: notes.php:27
GravityView Edit Entry - Sync User Registration (when using the GF User Registration Add-on) ...
load()