gravityview /
GravityView
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 | 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 | if( ! class_exists( 'GFAPI' ) || ! class_exists( 'GF_User_Registration' ) || empty( $entry_id ) ) { |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** @var GF_User_Registration $gf_user_registration */ |
||
| 80 | $gf_user_registration = GF_User_Registration::get_instance(); |
||
| 81 | |||
| 82 | $entry = GFAPI::get_entry( $entry_id ); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @filter `gravityview/edit_entry/user_registration/entry` Modify entry details before updating the user via User Registration add-on |
||
| 86 | * @since 1.11 |
||
| 87 | * @param array $entry Gravity Forms entry |
||
| 88 | * @param array $form Gravity Forms form |
||
| 89 | */ |
||
| 90 | $entry = apply_filters( 'gravityview/edit_entry/user_registration/entry', $entry, $form ); |
||
| 91 | |||
| 92 | $config = $gf_user_registration->get_single_submission_feed( $entry, $form ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @filter `gravityview/edit_entry/user_registration/preserve_role` Keep the current user role or override with the role defined in the Create feed |
||
| 96 | * @since 1.15 |
||
| 97 | * @param[in,out] boolean $preserve_role Preserve current user role Default: true |
||
| 98 | * @param[in] array $config Gravity Forms User Registration feed configuration for the form |
||
| 99 | * @param[in] array $form Gravity Forms form array |
||
| 100 | * @param[in] array $entry Gravity Forms entry being edited |
||
| 101 | */ |
||
| 102 | $preserve_role = apply_filters( 'gravityview/edit_entry/user_registration/preserve_role', true, $config, $form, $entry ); |
||
| 103 | |||
| 104 | if( $preserve_role ) { |
||
| 105 | $config['meta']['role'] = 'gfur_preserve_role'; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Make sure the current display name is not changed with the update user method. |
||
| 110 | * @since 1.15 |
||
| 111 | */ |
||
| 112 | $config['meta']['displayname'] = $this->match_current_display_name( $entry['created_by'] ); |
||
| 113 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 114 | |||
| 115 | /** |
||
| 116 | * @filter `gravityview/edit_entry/user_registration/config` Modify the User Registration Addon feed configuration |
||
| 117 | * @since 1.14 |
||
| 118 | * @param[in,out] array $config Gravity Forms User Registration feed configuration for the form |
||
| 119 | * @param[in] array $form Gravity Forms form array |
||
| 120 | * @param[in] array $entry Gravity Forms entry being edited |
||
| 121 | */ |
||
| 122 | $config = apply_filters( 'gravityview/edit_entry/user_registration/config', $config, $form, $entry ); |
||
| 123 | |||
|
0 ignored issues
–
show
|
|||
| 124 | |||
| 125 | // Make sure the feed is active |
||
| 126 | if ( ! $config['is_active'] ) { |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | // If an Update feed, make sure the conditions are met. |
||
| 131 | if( rgars( $config, 'meta/feedType' ) === 'update' ) { |
||
|
0 ignored issues
–
show
|
|||
| 132 | if( ! $gf_user_registration->is_feed_condition_met( $config, $form, $entry ) ) { |
||
| 133 | return; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // The priority is set to 3 so that default priority (10) will still override it |
||
| 138 | add_filter( 'send_password_change_email', '__return_false', 3 ); |
||
| 139 | add_filter( 'send_email_change_email', '__return_false', 3 ); |
||
| 140 | |||
| 141 | // Trigger the User Registration update user method |
||
| 142 | $gf_user_registration->update_user( $entry, $form, $config ); |
||
| 143 | |||
| 144 | remove_filter( 'send_password_change_email', '__return_false', 3 ); |
||
| 145 | remove_filter( 'send_email_change_email', '__return_false', 3 ); |
||
| 146 | |||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Calculate the user display name format |
||
| 151 | * |
||
| 152 | * @since 1.15 |
||
| 153 | * |
||
| 154 | * @param int $user_id WP User ID |
||
| 155 | * @return string Display name format as used inside Gravity Forms User Registration |
||
| 156 | */ |
||
| 157 | public function match_current_display_name( $user_id ) { |
||
| 158 | |||
| 159 | $user = get_userdata( $user_id ); |
||
| 160 | |||
| 161 | $names = $this->generate_display_names( $user ); |
||
| 162 | |||
| 163 | $format = array_search( $user->display_name, $names, true ); |
||
| 164 | |||
| 165 | // In case we can't find the current display name format, or it is the 'nickname' format (which Gravity Forms doesn't support) |
||
| 166 | // trigger last resort method at the 'gform_user_updated' hook |
||
| 167 | if( false === $format || 'nickname' === $format ) { |
||
| 168 | $this->_user_before_update = $user; |
||
| 169 | $format = 'nickname'; |
||
| 170 | } |
||
| 171 | |||
| 172 | return $format; |
||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Generate an array of all the user display names possibilities |
||
| 178 | * |
||
| 179 | * @since 1.15 |
||
| 180 | * |
||
| 181 | * @param object $profileuser WP_User object |
||
| 182 | * @return array List all the possible display names for a certain User object |
||
| 183 | */ |
||
| 184 | public function generate_display_names( $profileuser ) { |
||
| 185 | |||
| 186 | $public_display = array(); |
||
| 187 | $public_display['nickname'] = $profileuser->nickname; |
||
| 188 | $public_display['username'] = $profileuser->user_login; |
||
| 189 | |||
| 190 | if ( !empty($profileuser->first_name) ) |
||
| 191 | $public_display['firstname'] = $profileuser->first_name; |
||
| 192 | |||
| 193 | if ( !empty($profileuser->last_name) ) |
||
| 194 | $public_display['lastname'] = $profileuser->last_name; |
||
| 195 | |||
| 196 | if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) { |
||
| 197 | $public_display['firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name; |
||
| 198 | $public_display['lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name; |
||
| 199 | } |
||
| 200 | |||
| 201 | $public_display = array_map( 'trim', $public_display ); |
||
| 202 | $public_display = array_unique( $public_display ); |
||
| 203 | |||
| 204 | return $public_display; |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Restore the Display Name and roles of a user after being updated by Gravity Forms User Registration Addon |
||
| 210 | * |
||
| 211 | * @see GFUser::update_user() |
||
| 212 | * @param int $user_id WP User ID that was updated by Gravity Forms User Registration Addon |
||
| 213 | * @param array $config Gravity Forms User Registration Addon form feed configuration |
||
| 214 | * @param array $entry The Gravity Forms entry that was just updated |
||
| 215 | * @param string $password User password |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function restore_display_name( $user_id = 0, $config = array(), $entry = array(), $password = '' ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @filter `gravityview/edit_entry/restore_display_name` Whether display names should be restored to before updating an entry. |
||
| 222 | * Otherwise, display names will be reset to the format specified in Gravity Forms User Registration "Update" feed |
||
| 223 | * @since 1.14.4 |
||
| 224 | * @param boolean $restore_display_name Restore Display Name? Default: true |
||
| 225 | */ |
||
| 226 | $restore_display_name = apply_filters( 'gravityview/edit_entry/restore_display_name', true ); |
||
| 227 | |||
| 228 | $is_update_feed = ( $config && rgars( $config, 'meta/feed_type') === 'update' ); |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Don't restore display name: |
||
| 232 | * - either disabled, |
||
| 233 | * - or it is an Update feed (we only care about Create feed) |
||
| 234 | * - or we don't need as we found the correct format before updating user. |
||
| 235 | * @since 1.14.4 |
||
| 236 | */ |
||
| 237 | if( ! $restore_display_name || $is_update_feed || is_null( $this->_user_before_update ) ) { |
||
| 238 | return; |
||
| 239 | } |
||
| 240 | |||
| 241 | $user_after_update = get_userdata( $user_id ); |
||
| 242 | |||
| 243 | $restored_user = $user_after_update; |
||
| 244 | |||
| 245 | // Restore previous display_name |
||
| 246 | $restored_user->display_name = $this->_user_before_update->display_name; |
||
| 247 | |||
| 248 | // Don't have WP update the password. |
||
| 249 | unset( $restored_user->data->user_pass, $restored_user->user_pass ); |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Modify the user data after updated by Gravity Forms User Registration but before restored by GravityView |
||
| 253 | * @since 1.14 |
||
| 254 | * @param WP_User $restored_user The user with restored details about to be updated by wp_update_user() |
||
| 255 | * @param WP_User $user_before_update The user before being updated by Gravity Forms User Registration |
||
| 256 | * @param WP_User $user_after_update The user after being updated by Gravity Forms User Registration |
||
| 257 | * @param array $entry The Gravity Forms entry that was just updated |
||
| 258 | */ |
||
| 259 | $restored_user = apply_filters( 'gravityview/edit_entry/user_registration/restored_user', $restored_user, $this->_user_before_update, $user_after_update, $entry ); |
||
| 260 | |||
| 261 | $updated = wp_update_user( $restored_user ); |
||
| 262 | |||
| 263 | if( is_wp_error( $updated ) ) { |
||
| 264 | do_action('gravityview_log_error', __METHOD__ . sprintf( ' - There was an error updating user #%d details', $user_id ), $updated ); |
||
| 265 | } else { |
||
| 266 | do_action('gravityview_log_debug', __METHOD__ . sprintf( ' - User #%d details restored', $user_id ) ); |
||
| 267 | } |
||
| 268 | |||
| 269 | $this->_user_before_update = null; |
||
| 270 | |||
| 271 | unset( $updated, $restored_user, $user_after_update ); |
||
| 272 | } |
||
| 273 | |||
| 274 | } //end class |
||
| 275 |