Completed
Push — develop ( 069d2f...416d90 )
by Zack
05:25 queued 05:19
created

get_feed_configuration()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 40
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 2
dl 0
loc 40
ccs 0
cts 11
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 14.

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.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 1
    public function update_user( $form = array(), $entry_id = 0 ) {
74
75 1
        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 1
        } 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 1
        $gf_user_registration = GF_User_Registration::get_instance();
85
86 1
        $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 1
        $entry = apply_filters( 'gravityview/edit_entry/user_registration/entry', $entry, $form );
95
96 1
	    $config = $this->get_feed_configuration( $entry, $form );
97
98
        // Make sure the feed is active
99 1
	    if ( ! rgar( $config, 'is_active', false ) ) {
100 1
			return;
101
	    }
102
103
	    // If an Update feed, make sure the conditions are met.
104
	    if( rgars( $config, 'meta/feedType' ) === 'update' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
105
	    	if( ! $gf_user_registration->is_feed_condition_met( $config, $form, $entry ) ) {
106
			    return;
107
		    }
108
	    }
109
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 );
113
114
        // Trigger the User Registration update user method
115
        $gf_user_registration->update_user( $entry, $form, $config );
116
117
        remove_filter( 'send_password_change_email', '__return_false', 3 );
118
        remove_filter( 'send_email_change_email', '__return_false', 3 );
119
120
    }
121
122
	/**
123
	 * Get the User Registration feed configuration for the entry & form
124
	 *
125
	 * @uses GF_User_Registration::get_single_submission_feed
126
	 * @uses GravityView_Edit_Entry_User_Registration::match_current_display_name
127
	 *
128
	 * @since 1.20
129
	 *
130
	 * @param $entry
131
	 * @param $form
132
	 *
133
	 * @return array
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 1
    public function match_current_display_name( $user_id ) {
186
187 1
        $user = get_userdata( $user_id );
188
189 1
        if( ! $user ) {
190 1
        	return false;
191
        }
192
193 1
        $names = $this->generate_display_names( $user );
194
195 1
        $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 1
        if( false === $format ) {
202
            $this->_user_before_update = $user;
203
        }
204
205 1
        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 1
    public function generate_display_names( $profileuser ) {
217
218 1
        $public_display = array();
219 1
        $public_display['nickname']  = $profileuser->nickname;
220 1
        $public_display['username']  = $profileuser->user_login;
221
222 1
        if ( !empty($profileuser->first_name) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
223 1
	        $public_display['firstname'] = $profileuser->first_name;
224 1
        }
225
226 1
        if ( !empty($profileuser->last_name) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
227 1
	        $public_display['lastname'] = $profileuser->last_name;
228 1
        }
229
230 1
        if ( !empty($profileuser->first_name) && !empty($profileuser->last_name) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
231 1
            $public_display['firstlast'] = $profileuser->first_name . ' ' . $profileuser->last_name;
232 1
            $public_display['lastfirst'] = $profileuser->last_name . ' ' . $profileuser->first_name;
233 1
        }
234
235 1
        $public_display = array_map( 'trim', $public_display );
236 1
        $public_display = array_unique( $public_display );
237
238 1
        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 1
    public function restore_display_name( $user_id = 0, $config = array(), $entry = array(), $password = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $password is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 1
        $restore_display_name = apply_filters( 'gravityview/edit_entry/restore_display_name', true );
261
262 1
        $is_update_feed = ( $config && rgars( $config, 'meta/feed_type') === 'update' );
0 ignored issues
show
Bug Best Practice introduced by
The expression $config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 1
        if( ! $restore_display_name || $is_update_feed || is_null( $this->_user_before_update ) ) {
272 1
            return null;
273
        }
274
275 1
        $user_after_update = get_userdata( $user_id );
276
277
        // User not found
278 1
	    if ( ! $user_after_update ) {
279 1
	    	do_action('gravityview_log_error', __METHOD__ . sprintf( ' - User not found at $user_id #%d', $user_id ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
280 1
		    return false;
281
	    }
282
283 1
        $restored_user = $user_after_update;
284
285
	    // Restore previous display_name
286 1
        $restored_user->display_name = $this->_user_before_update->display_name;
287
288
	    // Don't have WP update the password.
289 1
	    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 1
        $restored_user = apply_filters( 'gravityview/edit_entry/user_registration/restored_user', $restored_user, $this->_user_before_update, $user_after_update, $entry );
300
301 1
        $updated = wp_update_user( $restored_user );
302
303 1
        if( is_wp_error( $updated ) ) {
304 1
            do_action('gravityview_log_error', __METHOD__ . sprintf( ' - There was an error updating user #%d details', $user_id ), $updated );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
305 1
        } else {
306 1
            do_action('gravityview_log_debug', __METHOD__ . sprintf( ' - User #%d details restored', $user_id ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
307
        }
308
309 1
        $this->_user_before_update = null;
310
311 1
        unset( $restored_user, $user_after_update );
312
313 1
        return $updated;
314
    }
315
316
} //end class
317