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 Katz Web Services, Inc. |
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
|
|
|
*/ |
20
|
|
|
class GravityView_Edit_Entry_User_Registration { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var GravityView_Edit_Entry |
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
|
|
|
|
32
|
|
|
function __construct( GravityView_Edit_Entry $loader ) { |
|
|
|
|
33
|
|
|
$this->loader = $loader; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @since 1.11 |
38
|
|
|
*/ |
39
|
|
|
public function load() { |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @filter `gravityview/edit_entry/user_registration/trigger_update` Choose whether to update user information via User Registration add-on when an entry is updated? |
43
|
|
|
* @since 1.11 |
44
|
|
|
* @param boolean $boolean Whether to trigger update on user registration (default: true) |
45
|
|
|
*/ |
46
|
|
|
if( apply_filters( 'gravityview/edit_entry/user_registration/trigger_update', true ) ) { |
|
|
|
|
47
|
|
|
add_action( 'gravityview/edit_entry/after_update' , array( $this, 'update_user' ), 10, 2 ); |
48
|
|
|
|
49
|
|
|
// last resort in case the current user display name don't match any of the defaults |
50
|
|
|
add_action( 'gform_user_updated', array( $this, 'restore_display_name' ), 10, 4 ); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Update the WordPress user profile based on the GF User Registration create feed |
56
|
|
|
* |
57
|
|
|
* @since 1.11 |
58
|
|
|
* |
59
|
|
|
* @param array $form Gravity Forms form array |
60
|
|
|
* @param string $entry_id Gravity Forms entry ID |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function update_user( $form = array(), $entry_id = 0 ) { |
64
|
|
|
|
65
|
|
|
if( !class_exists( 'GFAPI' ) || !class_exists( 'GFUser' ) || empty( $entry_id ) ) { |
|
|
|
|
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// support for GF User Registration 3.x |
70
|
|
|
$gf_user_3 = class_exists('GF_User_Registration') ? true : false; |
|
|
|
|
71
|
|
|
|
72
|
|
|
if( $gf_user_3 ) { |
|
|
|
|
73
|
|
|
$gf_user_registration = GF_User_Registration::get_instance(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$entry = GFAPI::get_entry( $entry_id ); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @filter `gravityview/edit_entry/user_registration/entry` Modify entry details before updating the user via User Registration add-on |
80
|
|
|
* @since 1.11 |
81
|
|
|
* @param array $entry Gravity Forms entry |
82
|
|
|
* @param array $form Gravity Forms form |
83
|
|
|
*/ |
84
|
|
|
$entry = apply_filters( 'gravityview/edit_entry/user_registration/entry', $entry, $form ); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @since 1.14 |
88
|
|
|
*/ |
89
|
|
|
if( $gf_user_3 ) { |
|
|
|
|
90
|
|
|
$config = $gf_user_registration->get_single_submission_feed( $entry, $form ); |
|
|
|
|
91
|
|
|
} else { |
92
|
|
|
$config = GFUser::get_active_config( $form, $entry ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @filter `gravityview/edit_entry/user_registration/preserve_role` Keep the current user role or override with the role defined in the Create feed |
97
|
|
|
* @since 1.15 |
98
|
|
|
* @param[in,out] boolean $preserve_role Preserve current user role Default: true |
99
|
|
|
* @param[in] array $config Gravity Forms User Registration feed configuration for the form |
100
|
|
|
* @param[in] array $form Gravity Forms form array |
101
|
|
|
* @param[in] array $entry Gravity Forms entry being edited |
102
|
|
|
*/ |
103
|
|
|
$preserve_role = apply_filters( 'gravityview/edit_entry/user_registration/preserve_role', true, $config, $form, $entry ); |
104
|
|
|
|
105
|
|
|
if( $preserve_role ) { |
|
|
|
|
106
|
|
|
$config['meta']['role'] = 'gfur_preserve_role'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Make sure the current display name is not changed with the update user method. |
111
|
|
|
* @since 1.15 |
112
|
|
|
*/ |
113
|
|
|
$config['meta']['displayname'] = $this->match_current_display_name( $entry['created_by'] ); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @filter `gravityview/edit_entry/user_registration/config` Modify the User Registration Addon feed configuration |
118
|
|
|
* @since 1.14 |
119
|
|
|
* @param[in,out] array $config Gravity Forms User Registration feed configuration for the form |
120
|
|
|
* @param[in] array $form Gravity Forms form array |
121
|
|
|
* @param[in] array $entry Gravity Forms entry being edited |
122
|
|
|
*/ |
123
|
|
|
$config = apply_filters( 'gravityview/edit_entry/user_registration/config', $config, $form, $entry ); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
126
|
|
|
$feed_pos = $gf_user_3 ? 'meta/feedType' : 'meta/feed_type'; |
127
|
|
|
$is_create_feed = ( $config && rgars( $config, $feed_pos ) === 'create' ); |
128
|
|
|
|
129
|
|
|
// Only update if it's a create feed |
130
|
|
|
if( ! $is_create_feed ) { |
|
|
|
|
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// The priority is set to 3 so that default priority (10) will still override it |
135
|
|
|
add_filter( 'send_password_change_email', '__return_false', 3 ); |
136
|
|
|
add_filter( 'send_email_change_email', '__return_false', 3 ); |
137
|
|
|
|
138
|
|
|
// Trigger the User Registration update user method |
139
|
|
|
if( $gf_user_3 ) { |
|
|
|
|
140
|
|
|
$gf_user_registration->update_user( $entry, $form, $config ); |
141
|
|
|
} else { |
142
|
|
|
GFUser::update_user( $entry, $form, $config ); |
143
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
146
|
|
|
remove_filter( 'send_password_change_email', '__return_false', 3 ); |
147
|
|
|
remove_filter( 'send_email_change_email', '__return_false', 3 ); |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Calculate the user display name format |
153
|
|
|
* |
154
|
|
|
* @since 1.15 |
155
|
|
|
* |
156
|
|
|
* @param int $user_id WP User ID |
157
|
|
|
* @return string Display name format as used inside Gravity Forms User Registration |
158
|
|
|
*/ |
159
|
|
|
public function match_current_display_name( $user_id ) { |
160
|
|
|
|
161
|
|
|
$user = get_userdata( $user_id ); |
162
|
|
|
|
163
|
|
|
$names = $this->generate_display_names( $user ); |
164
|
|
|
|
165
|
|
|
$format = array_search( $user->display_name, $names, true ); |
166
|
|
|
|
167
|
|
|
// In case we can't find the current display name format, or it is the 'nickname' format (which Gravity Forms doesn't support) |
168
|
|
|
// trigger last resort method at the 'gform_user_updated' hook |
169
|
|
|
if( false === $format || 'nickname' === $format ) { |
|
|
|
|
170
|
|
|
$this->_user_before_update = $user; |
171
|
|
|
$format = 'nickname'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $format; |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Generate an array of all the user display names possibilities |
180
|
|
|
* |
181
|
|
|
* @since 1.15 |
182
|
|
|
* |
183
|
|
|
* @param object $profileuser WP_User object |
184
|
|
|
* @return array List all the possible display names for a certain User object |
185
|
|
|
*/ |
186
|
|
|
public function generate_display_names( $profileuser ) { |
187
|
|
|
|
188
|
|
|
$public_display = array(); |
189
|
|
|
$public_display['nickname'] = $profileuser->nickname; |
190
|
|
|
$public_display['username'] = $profileuser->user_login; |
191
|
|
|
|
192
|
|
|
if ( !empty($profileuser->first_name) ) |
|
|
|
|
193
|
|
|
$public_display['firstname'] = $profileuser->first_name; |
194
|
|
|
|
195
|
|
|
if ( !empty($profileuser->last_name) ) |
|
|
|
|
196
|
|
|
$public_display['lastname'] = $profileuser->last_name; |
197
|
|
|
|
198
|
|
|
if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) { |
|
|
|
|
199
|
|
|
$public_display['firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name; |
200
|
|
|
$public_display['lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$public_display = array_map( 'trim', $public_display ); |
204
|
|
|
$public_display = array_unique( $public_display ); |
205
|
|
|
|
206
|
|
|
return $public_display; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Restore the Display Name and roles of a user after being updated by Gravity Forms User Registration Addon |
212
|
|
|
* |
213
|
|
|
* @see GFUser::update_user() |
214
|
|
|
* @param int $user_id WP User ID that was updated by Gravity Forms User Registration Addon |
215
|
|
|
* @param array $config Gravity Forms User Registration Addon form feed configuration |
216
|
|
|
* @param array $entry The Gravity Forms entry that was just updated |
217
|
|
|
* @param string $password User password |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function restore_display_name( $user_id = 0, $config = array(), $entry = array(), $password = '' ) { |
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @filter `gravityview/edit_entry/restore_display_name` Whether display names should be restored to before updating an entry. |
224
|
|
|
* Otherwise, display names will be reset to the format specified in Gravity Forms User Registration "Update" feed |
225
|
|
|
* @since 1.14.4 |
226
|
|
|
* @param boolean $restore_display_name Restore Display Name? Default: true |
227
|
|
|
*/ |
228
|
|
|
$restore_display_name = apply_filters( 'gravityview/edit_entry/restore_display_name', true ); |
229
|
|
|
|
230
|
|
|
$is_update_feed = ( $config && rgars( $config, 'meta/feed_type') === 'update' ); |
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Don't restore display name: |
234
|
|
|
* - either disabled, |
235
|
|
|
* - or it is an Update feed (we only care about Create feed) |
236
|
|
|
* - or we don't need as we found the correct format before updating user. |
237
|
|
|
* @since 1.14.4 |
238
|
|
|
*/ |
239
|
|
|
if( ! $restore_display_name || $is_update_feed || is_null( $this->_user_before_update ) ) { |
|
|
|
|
240
|
|
|
return; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$user_after_update = get_userdata( $user_id ); |
244
|
|
|
|
245
|
|
|
$restored_user = $user_after_update; |
246
|
|
|
|
247
|
|
|
// Restore previous display_name |
248
|
|
|
$restored_user->display_name = $this->_user_before_update->display_name; |
249
|
|
|
|
250
|
|
|
// Don't have WP update the password. |
251
|
|
|
unset( $restored_user->data->user_pass, $restored_user->user_pass ); |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Modify the user data after updated by Gravity Forms User Registration but before restored by GravityView |
255
|
|
|
* @since 1.14 |
256
|
|
|
* @param WP_User $restored_user The user with restored details about to be updated by wp_update_user() |
257
|
|
|
* @param WP_User $user_before_update The user before being updated by Gravity Forms User Registration |
258
|
|
|
* @param WP_User $user_after_update The user after being updated by Gravity Forms User Registration |
259
|
|
|
* @param array $entry The Gravity Forms entry that was just updated |
260
|
|
|
*/ |
261
|
|
|
$restored_user = apply_filters( 'gravityview/edit_entry/user_registration/restored_user', $restored_user, $this->_user_before_update, $user_after_update, $entry ); |
262
|
|
|
|
263
|
|
|
$updated = wp_update_user( $restored_user ); |
264
|
|
|
|
265
|
|
|
if( is_wp_error( $updated ) ) { |
|
|
|
|
266
|
|
|
do_action('gravityview_log_error', __METHOD__ . sprintf( ' - There was an error updating user #%d details', $user_id ), $updated ); |
|
|
|
|
267
|
|
|
} else { |
268
|
|
|
do_action('gravityview_log_debug', __METHOD__ . sprintf( ' - User #%d details restored', $user_id ) ); |
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$this->_user_before_update = null; |
272
|
|
|
|
273
|
|
|
unset( $updated, $restored_user, $user_after_update ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
} //end class |
277
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.