Completed
Pull Request — master (#641)
by Zack
38:18 queued 33:54
created
includes/extensions/edit-entry/class-edit-entry-user-registration.php 1 patch
Indentation   +243 added lines, -243 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,255 +22,255 @@  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
 
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
-    }
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 275
 
276 276
 } //end class
Please login to merge, or discard this patch.
includes/extensions/edit-entry/class-edit-entry.php 1 patch
Indentation   +164 added lines, -164 removed lines patch added patch discarded remove patch
@@ -18,80 +18,80 @@  discard block
 block discarded – undo
18 18
 
19 19
 class GravityView_Edit_Entry {
20 20
 
21
-    /**
22
-     * @var string
23
-     */
21
+	/**
22
+	 * @var string
23
+	 */
24 24
 	static $file;
25 25
 
26 26
 	static $instance;
27 27
 
28
-    /**
29
-     * Component instances.
30
-     * @var array
31
-     */
32
-    public $instances = array();
28
+	/**
29
+	 * Component instances.
30
+	 * @var array
31
+	 */
32
+	public $instances = array();
33 33
 
34 34
 
35 35
 	function __construct() {
36 36
 
37
-        self::$file = plugin_dir_path( __FILE__ );
37
+		self::$file = plugin_dir_path( __FILE__ );
38 38
 
39
-        if( is_admin() ) {
40
-            $this->load_components( 'admin' );
41
-        }
39
+		if( is_admin() ) {
40
+			$this->load_components( 'admin' );
41
+		}
42 42
 
43 43
 
44
-        $this->load_components( 'render' );
44
+		$this->load_components( 'render' );
45 45
 
46
-        // If GF User Registration Add-on exists
47
-        $this->load_components( 'user-registration' );
46
+		// If GF User Registration Add-on exists
47
+		$this->load_components( 'user-registration' );
48 48
 
49
-        $this->add_hooks();
49
+		$this->add_hooks();
50 50
 
51 51
 		// Process hooks for addons that may or may not be present
52 52
 		$this->addon_specific_hooks();
53 53
 	}
54 54
 
55 55
 
56
-    static function getInstance() {
56
+	static function getInstance() {
57 57
 
58
-        if( empty( self::$instance ) ) {
59
-            self::$instance = new GravityView_Edit_Entry;
60
-        }
58
+		if( empty( self::$instance ) ) {
59
+			self::$instance = new GravityView_Edit_Entry;
60
+		}
61 61
 
62
-        return self::$instance;
63
-    }
62
+		return self::$instance;
63
+	}
64 64
 
65 65
 
66
-    private function load_components( $component ) {
66
+	private function load_components( $component ) {
67 67
 
68
-        $dir = trailingslashit( self::$file );
68
+		$dir = trailingslashit( self::$file );
69 69
 
70
-        $filename  = $dir . 'class-edit-entry-' . $component . '.php';
71
-        $classname = 'GravityView_Edit_Entry_' . str_replace( ' ', '_', ucwords( str_replace( '-', ' ', $component ) ) );
70
+		$filename  = $dir . 'class-edit-entry-' . $component . '.php';
71
+		$classname = 'GravityView_Edit_Entry_' . str_replace( ' ', '_', ucwords( str_replace( '-', ' ', $component ) ) );
72 72
 
73
-        // Loads component and pass extension's instance so that component can
74
-        // talk each other.
75
-        require_once $filename;
76
-        $this->instances[ $component ] = new $classname( $this );
77
-        $this->instances[ $component ]->load();
73
+		// Loads component and pass extension's instance so that component can
74
+		// talk each other.
75
+		require_once $filename;
76
+		$this->instances[ $component ] = new $classname( $this );
77
+		$this->instances[ $component ]->load();
78 78
 
79
-    }
79
+	}
80 80
 
81
-    private function add_hooks() {
81
+	private function add_hooks() {
82 82
 
83
-        // Add front-end access to Gravity Forms delete file action
84
-        add_action( 'wp_ajax_nopriv_rg_delete_file', array( 'RGForms', 'delete_file') );
83
+		// Add front-end access to Gravity Forms delete file action
84
+		add_action( 'wp_ajax_nopriv_rg_delete_file', array( 'RGForms', 'delete_file') );
85 85
 
86
-        // Make sure this hook is run for non-admins
87
-        add_action( 'wp_ajax_rg_delete_file', array( 'RGForms', 'delete_file') );
86
+		// Make sure this hook is run for non-admins
87
+		add_action( 'wp_ajax_rg_delete_file', array( 'RGForms', 'delete_file') );
88 88
 
89
-        add_filter( 'gravityview_blacklist_field_types', array( $this, 'modify_field_blacklist' ), 10, 2 );
89
+		add_filter( 'gravityview_blacklist_field_types', array( $this, 'modify_field_blacklist' ), 10, 2 );
90 90
 
91
-        // add template path to check for field
92
-        add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
91
+		// add template path to check for field
92
+		add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
93 93
 
94
-    }
94
+	}
95 95
 
96 96
 	/**
97 97
 	 * Trigger hooks that are normally run in the admin for Addons, but need to be triggered manually because we're not in the admin
@@ -105,75 +105,75 @@  discard block
 block discarded – undo
105 105
 
106 106
 	}
107 107
 
108
-    /**
109
-     * Include this extension templates path
110
-     * @param array $file_paths List of template paths ordered
111
-     */
112
-    public function add_template_path( $file_paths ) {
113
-
114
-        // Index 100 is the default GravityView template path.
115
-        $file_paths[ 110 ] = self::$file;
116
-
117
-        return $file_paths;
118
-    }
119
-
120
-    /**
121
-     *
122
-     * Return a well formatted nonce key according to GravityView Edit Entry protocol
123
-     *
124
-     * @param $view_id int GravityView view id
125
-     * @param $form_id int Gravity Forms form id
126
-     * @param $entry_id int Gravity Forms entry id
127
-     * @return string
128
-     */
129
-    public static function get_nonce_key( $view_id, $form_id, $entry_id ) {
130
-        return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id );
131
-    }
132
-
133
-
134
-    /**
135
-     * The edit entry link creates a secure link with a nonce
136
-     *
137
-     * It also mimics the URL structure Gravity Forms expects to have so that
138
-     * it formats the display of the edit form like it does in the backend, like
139
-     * "You can edit this post from the post page" fields, for example.
140
-     *
141
-     * @param $entry array Gravity Forms entry object
142
-     * @param $view_id int GravityView view id
143
-     * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2}
144
-     * @param string|array $field_values Parameters to pass in to the Edit Entry form to prefill data. Uses the same format as Gravity Forms "Allow field to be populated dynamically" {@since 1.9.2} {@see https://www.gravityhelp.com/documentation/article/allow-field-to-be-populated-dynamically/ }
145
-     * @return string
146
-     */
147
-    public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) {
148
-
149
-        $nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id']  );
150
-
151
-        $base = gv_entry_link( $entry, $post_id );
152
-
153
-        $url = add_query_arg( array(
154
-            'page' => 'gf_entries', // Needed for GFForms::get_page()
155
-            'view' => 'entry', // Needed for GFForms::get_page()
156
-            'edit' => wp_create_nonce( $nonce_key )
157
-        ), $base );
158
-
159
-	    /**
160
-	     * Allow passing params to dynamically populate entry with values
161
-	     * @since 1.9.2
162
-	     */
163
-	    if( !empty( $field_values ) ) {
164
-
165
-		    if( is_array( $field_values ) ) {
166
-			    // If already an array, no parse_str() needed
167
-			    $params = $field_values;
168
-		    } else {
169
-			    parse_str( $field_values, $params );
170
-		    }
171
-
172
-		    $url = add_query_arg( $params, $url );
173
-	    }
174
-
175
-        return $url;
176
-    }
108
+	/**
109
+	 * Include this extension templates path
110
+	 * @param array $file_paths List of template paths ordered
111
+	 */
112
+	public function add_template_path( $file_paths ) {
113
+
114
+		// Index 100 is the default GravityView template path.
115
+		$file_paths[ 110 ] = self::$file;
116
+
117
+		return $file_paths;
118
+	}
119
+
120
+	/**
121
+	 *
122
+	 * Return a well formatted nonce key according to GravityView Edit Entry protocol
123
+	 *
124
+	 * @param $view_id int GravityView view id
125
+	 * @param $form_id int Gravity Forms form id
126
+	 * @param $entry_id int Gravity Forms entry id
127
+	 * @return string
128
+	 */
129
+	public static function get_nonce_key( $view_id, $form_id, $entry_id ) {
130
+		return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id );
131
+	}
132
+
133
+
134
+	/**
135
+	 * The edit entry link creates a secure link with a nonce
136
+	 *
137
+	 * It also mimics the URL structure Gravity Forms expects to have so that
138
+	 * it formats the display of the edit form like it does in the backend, like
139
+	 * "You can edit this post from the post page" fields, for example.
140
+	 *
141
+	 * @param $entry array Gravity Forms entry object
142
+	 * @param $view_id int GravityView view id
143
+	 * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2}
144
+	 * @param string|array $field_values Parameters to pass in to the Edit Entry form to prefill data. Uses the same format as Gravity Forms "Allow field to be populated dynamically" {@since 1.9.2} {@see https://www.gravityhelp.com/documentation/article/allow-field-to-be-populated-dynamically/ }
145
+	 * @return string
146
+	 */
147
+	public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) {
148
+
149
+		$nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id']  );
150
+
151
+		$base = gv_entry_link( $entry, $post_id );
152
+
153
+		$url = add_query_arg( array(
154
+			'page' => 'gf_entries', // Needed for GFForms::get_page()
155
+			'view' => 'entry', // Needed for GFForms::get_page()
156
+			'edit' => wp_create_nonce( $nonce_key )
157
+		), $base );
158
+
159
+		/**
160
+		 * Allow passing params to dynamically populate entry with values
161
+		 * @since 1.9.2
162
+		 */
163
+		if( !empty( $field_values ) ) {
164
+
165
+			if( is_array( $field_values ) ) {
166
+				// If already an array, no parse_str() needed
167
+				$params = $field_values;
168
+			} else {
169
+				parse_str( $field_values, $params );
170
+			}
171
+
172
+			$url = add_query_arg( $params, $url );
173
+		}
174
+
175
+		return $url;
176
+	}
177 177
 
178 178
 
179 179
 	/**
@@ -209,81 +209,81 @@  discard block
 block discarded – undo
209 209
 	}
210 210
 
211 211
 
212
-    /**
213
-     * checks if user has permissions to edit a specific entry
214
-     *
215
-     * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!!
216
-     *
217
-     * @param  array $entry Gravity Forms entry array
218
-     * @param int $view_id ID of the view you want to check visibility against {@since 1.9.2}
219
-     * @return bool
220
-     */
221
-    public static function check_user_cap_edit_entry( $entry, $view_id = 0 ) {
212
+	/**
213
+	 * checks if user has permissions to edit a specific entry
214
+	 *
215
+	 * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!!
216
+	 *
217
+	 * @param  array $entry Gravity Forms entry array
218
+	 * @param int $view_id ID of the view you want to check visibility against {@since 1.9.2}
219
+	 * @return bool
220
+	 */
221
+	public static function check_user_cap_edit_entry( $entry, $view_id = 0 ) {
222 222
 
223
-        // No permission by default
224
-        $user_can_edit = false;
223
+		// No permission by default
224
+		$user_can_edit = false;
225 225
 
226
-        // If they can edit any entries (as defined in Gravity Forms)
227
-        // Or if they can edit other people's entries
228
-        // Then we're good.
229
-        if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) {
226
+		// If they can edit any entries (as defined in Gravity Forms)
227
+		// Or if they can edit other people's entries
228
+		// Then we're good.
229
+		if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) {
230 230
 
231
-            do_action('gravityview_log_debug', __METHOD__ . ' - User has ability to edit all entries.');
231
+			do_action('gravityview_log_debug', __METHOD__ . ' - User has ability to edit all entries.');
232 232
 
233
-            $user_can_edit = true;
233
+			$user_can_edit = true;
234 234
 
235
-        } else if( !isset( $entry['created_by'] ) ) {
235
+		} else if( !isset( $entry['created_by'] ) ) {
236 236
 
237
-            do_action('gravityview_log_error', 'GravityView_Edit_Entry[check_user_cap_edit_entry] Entry `created_by` doesn\'t exist.');
237
+			do_action('gravityview_log_error', 'GravityView_Edit_Entry[check_user_cap_edit_entry] Entry `created_by` doesn\'t exist.');
238 238
 
239
-            $user_can_edit = false;
239
+			$user_can_edit = false;
240 240
 
241
-        } else {
241
+		} else {
242 242
 
243
-            // get user_edit setting
244
-            if( empty( $view_id ) || $view_id == GravityView_View::getInstance()->getViewId() ) {
245
-                // if View ID not specified or is the current view
246
-                $user_edit = GravityView_View::getInstance()->getAtts('user_edit');
247
-            } else {
248
-                // in case is specified and not the current view
249
-                $user_edit = GVCommon::get_template_setting( $view_id, 'user_edit' );
250
-            }
243
+			// get user_edit setting
244
+			if( empty( $view_id ) || $view_id == GravityView_View::getInstance()->getViewId() ) {
245
+				// if View ID not specified or is the current view
246
+				$user_edit = GravityView_View::getInstance()->getAtts('user_edit');
247
+			} else {
248
+				// in case is specified and not the current view
249
+				$user_edit = GVCommon::get_template_setting( $view_id, 'user_edit' );
250
+			}
251 251
 
252
-            $current_user = wp_get_current_user();
252
+			$current_user = wp_get_current_user();
253 253
 
254
-            // User edit is disabled
255
-            if( empty( $user_edit ) ) {
254
+			// User edit is disabled
255
+			if( empty( $user_edit ) ) {
256 256
 
257
-                do_action('gravityview_log_debug', 'GravityView_Edit_Entry[check_user_cap_edit_entry] User Edit is disabled. Returning false.' );
257
+				do_action('gravityview_log_debug', 'GravityView_Edit_Entry[check_user_cap_edit_entry] User Edit is disabled. Returning false.' );
258 258
 
259
-                $user_can_edit = false;
260
-            }
259
+				$user_can_edit = false;
260
+			}
261 261
 
262
-            // User edit is enabled and the logged-in user is the same as the user who created the entry. We're good.
263
-            else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) {
262
+			// User edit is enabled and the logged-in user is the same as the user who created the entry. We're good.
263
+			else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) {
264 264
 
265
-                do_action('gravityview_log_debug', sprintf( 'GravityView_Edit_Entry[check_user_cap_edit_entry] User %s created the entry.', $current_user->ID ) );
265
+				do_action('gravityview_log_debug', sprintf( 'GravityView_Edit_Entry[check_user_cap_edit_entry] User %s created the entry.', $current_user->ID ) );
266 266
 
267
-                $user_can_edit = true;
267
+				$user_can_edit = true;
268 268
 
269
-            } else if( ! is_user_logged_in() ) {
269
+			} else if( ! is_user_logged_in() ) {
270 270
 
271
-                do_action( 'gravityview_log_debug', __METHOD__ . ' No user defined; edit entry requires logged in user' );
272
-            }
271
+				do_action( 'gravityview_log_debug', __METHOD__ . ' No user defined; edit entry requires logged in user' );
272
+			}
273 273
 
274
-        }
274
+		}
275 275
 
276
-        /**
277
-         * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry.
278
-         * @since 1.15 Added `$entry` and `$view_id` parameters
279
-         * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false)
280
-         * @param[in] array $entry Gravity Forms entry array {@since 1.15}
281
-         * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15}
282
-         */
283
-        $user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id );
276
+		/**
277
+		 * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry.
278
+		 * @since 1.15 Added `$entry` and `$view_id` parameters
279
+		 * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false)
280
+		 * @param[in] array $entry Gravity Forms entry array {@since 1.15}
281
+		 * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15}
282
+		 */
283
+		$user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id );
284 284
 
285
-        return (bool)$user_can_edit;
286
-    }
285
+		return (bool)$user_can_edit;
286
+	}
287 287
 
288 288
 
289 289
 
Please login to merge, or discard this patch.
includes/extensions/edit-entry/class-edit-entry-render.php 1 patch
Indentation   +1369 added lines, -1369 removed lines patch added patch discarded remove patch
@@ -10,21 +10,21 @@  discard block
 block discarded – undo
10 10
  */
11 11
 
12 12
 if ( ! defined( 'WPINC' ) ) {
13
-    die;
13
+	die;
14 14
 }
15 15
 
16 16
 
17 17
 class GravityView_Edit_Entry_Render {
18 18
 
19
-    /**
20
-     * @var GravityView_Edit_Entry
21
-     */
22
-    protected $loader;
19
+	/**
20
+	 * @var GravityView_Edit_Entry
21
+	 */
22
+	protected $loader;
23 23
 
24 24
 	/**
25 25
 	 * @var string String used to generate unique nonce for the entry/form/view combination. Allows access to edit page.
26 26
 	 */
27
-    static $nonce_key;
27
+	static $nonce_key;
28 28
 
29 29
 	/**
30 30
 	 * @since 1.9
@@ -44,130 +44,130 @@  discard block
 block discarded – undo
44 44
 	 */
45 45
 	private static $supports_product_fields = false;
46 46
 
47
-    /**
48
-     * Gravity Forms entry array
49
-     *
50
-     * @var array
51
-     */
52
-    var $entry;
53
-
54
-    /**
55
-     * Gravity Forms form array (GravityView modifies the content through this class lifecycle)
56
-     *
57
-     * @var array
58
-     */
59
-    var $form;
60
-
61
-    /**
62
-     * Gravity Forms form array (it won't get changed during this class lifecycle)
63
-     * @since 1.16.3
64
-     * @var array
65
-     */
66
-    var $original_form;
67
-
68
-    /**
69
-     * Gravity Forms form array after the form validation process
70
-     * @since 1.13
71
-     * @var array
72
-     */
73
-    var $form_after_validation = null;
74
-
75
-    /**
76
-     * Gravity Forms form id
77
-     *
78
-     * @var int
79
-     */
80
-    var $form_id;
81
-
82
-    /**
83
-     * ID of the current view
84
-     *
85
-     * @var int
86
-     */
87
-    var $view_id;
88
-
89
-    /**
90
-     * Updated entry is valid (GF Validation object)
91
-     *
92
-     * @var array
93
-     */
94
-    var $is_valid = NULL;
95
-
96
-    function __construct( GravityView_Edit_Entry $loader ) {
97
-        $this->loader = $loader;
98
-    }
99
-
100
-    function load() {
101
-
102
-        /** @define "GRAVITYVIEW_DIR" "../../../" */
103
-        include_once( GRAVITYVIEW_DIR .'includes/class-admin-approve-entries.php' );
104
-
105
-        // Don't display an embedded form when editing an entry
106
-        add_action( 'wp_head', array( $this, 'prevent_render_form' ) );
107
-        add_action( 'wp_footer', array( $this, 'prevent_render_form' ) );
108
-
109
-        // Stop Gravity Forms processing what is ours!
110
-        add_filter( 'wp', array( $this, 'prevent_maybe_process_form'), 8 );
111
-
112
-        add_filter( 'gravityview_is_edit_entry', array( $this, 'is_edit_entry') );
113
-
114
-        add_action( 'gravityview_edit_entry', array( $this, 'init' ) );
115
-
116
-        // Disable conditional logic if needed (since 1.9)
117
-        add_filter( 'gform_has_conditional_logic', array( $this, 'manage_conditional_logic' ), 10, 2 );
118
-
119
-        // Make sure GF doesn't validate max files (since 1.9)
120
-        add_filter( 'gform_plupload_settings', array( $this, 'modify_fileupload_settings' ), 10, 3 );
121
-
122
-        // Add fields expected by GFFormDisplay::validate()
123
-        add_filter( 'gform_pre_validation', array( $this, 'gform_pre_validation') );
124
-
125
-    }
126
-
127
-    /**
128
-     * Don't show any forms embedded on a page when GravityView is in Edit Entry mode
129
-     *
130
-     * Adds a `__return_empty_string` filter on the Gravity Forms shortcode on the `wp_head` action
131
-     * And then removes it on the `wp_footer` action
132
-     *
133
-     * @since 1.16.1
134
-     *
135
-     * @return void
136
-     */
137
-    function prevent_render_form() {
138
-        if( $this->is_edit_entry() ) {
139
-            if( 'wp_head' === current_filter() ) {
140
-                add_filter( 'gform_shortcode_form', '__return_empty_string' );
141
-            } else {
142
-                remove_filter( 'gform_shortcode_form', '__return_empty_string' );
143
-            }
144
-        }
145
-    }
146
-
147
-    /**
148
-     * Because we're mimicking being a front-end Gravity Forms form while using a Gravity Forms
149
-     * backend form, we need to prevent them from saving twice.
150
-     * @return void
151
-     */
152
-    function prevent_maybe_process_form() {
153
-
154
-        do_action('gravityview_log_debug', 'GravityView_Edit_Entry[prevent_maybe_process_form] $_POSTed data (sanitized): ', esc_html( print_r( $_POST, true ) ) );
155
-
156
-        if( $this->is_edit_entry_submission() && $this->verify_nonce() ) {
157
-            remove_action( 'wp',  array( 'RGForms', 'maybe_process_form'), 9 );
158
-        }
159
-    }
160
-
161
-    /**
162
-     * Is the current page an Edit Entry page?
163
-     * @return boolean
164
-     */
165
-    public function is_edit_entry() {
166
-
167
-        $gf_page = ( 'entry' === RGForms::get( 'view' ) );
168
-
169
-        return ( $gf_page && isset( $_GET['edit'] ) || RGForms::post( 'action' ) === 'update' );
170
-    }
47
+	/**
48
+	 * Gravity Forms entry array
49
+	 *
50
+	 * @var array
51
+	 */
52
+	var $entry;
53
+
54
+	/**
55
+	 * Gravity Forms form array (GravityView modifies the content through this class lifecycle)
56
+	 *
57
+	 * @var array
58
+	 */
59
+	var $form;
60
+
61
+	/**
62
+	 * Gravity Forms form array (it won't get changed during this class lifecycle)
63
+	 * @since 1.16.3
64
+	 * @var array
65
+	 */
66
+	var $original_form;
67
+
68
+	/**
69
+	 * Gravity Forms form array after the form validation process
70
+	 * @since 1.13
71
+	 * @var array
72
+	 */
73
+	var $form_after_validation = null;
74
+
75
+	/**
76
+	 * Gravity Forms form id
77
+	 *
78
+	 * @var int
79
+	 */
80
+	var $form_id;
81
+
82
+	/**
83
+	 * ID of the current view
84
+	 *
85
+	 * @var int
86
+	 */
87
+	var $view_id;
88
+
89
+	/**
90
+	 * Updated entry is valid (GF Validation object)
91
+	 *
92
+	 * @var array
93
+	 */
94
+	var $is_valid = NULL;
95
+
96
+	function __construct( GravityView_Edit_Entry $loader ) {
97
+		$this->loader = $loader;
98
+	}
99
+
100
+	function load() {
101
+
102
+		/** @define "GRAVITYVIEW_DIR" "../../../" */
103
+		include_once( GRAVITYVIEW_DIR .'includes/class-admin-approve-entries.php' );
104
+
105
+		// Don't display an embedded form when editing an entry
106
+		add_action( 'wp_head', array( $this, 'prevent_render_form' ) );
107
+		add_action( 'wp_footer', array( $this, 'prevent_render_form' ) );
108
+
109
+		// Stop Gravity Forms processing what is ours!
110
+		add_filter( 'wp', array( $this, 'prevent_maybe_process_form'), 8 );
111
+
112
+		add_filter( 'gravityview_is_edit_entry', array( $this, 'is_edit_entry') );
113
+
114
+		add_action( 'gravityview_edit_entry', array( $this, 'init' ) );
115
+
116
+		// Disable conditional logic if needed (since 1.9)
117
+		add_filter( 'gform_has_conditional_logic', array( $this, 'manage_conditional_logic' ), 10, 2 );
118
+
119
+		// Make sure GF doesn't validate max files (since 1.9)
120
+		add_filter( 'gform_plupload_settings', array( $this, 'modify_fileupload_settings' ), 10, 3 );
121
+
122
+		// Add fields expected by GFFormDisplay::validate()
123
+		add_filter( 'gform_pre_validation', array( $this, 'gform_pre_validation') );
124
+
125
+	}
126
+
127
+	/**
128
+	 * Don't show any forms embedded on a page when GravityView is in Edit Entry mode
129
+	 *
130
+	 * Adds a `__return_empty_string` filter on the Gravity Forms shortcode on the `wp_head` action
131
+	 * And then removes it on the `wp_footer` action
132
+	 *
133
+	 * @since 1.16.1
134
+	 *
135
+	 * @return void
136
+	 */
137
+	function prevent_render_form() {
138
+		if( $this->is_edit_entry() ) {
139
+			if( 'wp_head' === current_filter() ) {
140
+				add_filter( 'gform_shortcode_form', '__return_empty_string' );
141
+			} else {
142
+				remove_filter( 'gform_shortcode_form', '__return_empty_string' );
143
+			}
144
+		}
145
+	}
146
+
147
+	/**
148
+	 * Because we're mimicking being a front-end Gravity Forms form while using a Gravity Forms
149
+	 * backend form, we need to prevent them from saving twice.
150
+	 * @return void
151
+	 */
152
+	function prevent_maybe_process_form() {
153
+
154
+		do_action('gravityview_log_debug', 'GravityView_Edit_Entry[prevent_maybe_process_form] $_POSTed data (sanitized): ', esc_html( print_r( $_POST, true ) ) );
155
+
156
+		if( $this->is_edit_entry_submission() && $this->verify_nonce() ) {
157
+			remove_action( 'wp',  array( 'RGForms', 'maybe_process_form'), 9 );
158
+		}
159
+	}
160
+
161
+	/**
162
+	 * Is the current page an Edit Entry page?
163
+	 * @return boolean
164
+	 */
165
+	public function is_edit_entry() {
166
+
167
+		$gf_page = ( 'entry' === RGForms::get( 'view' ) );
168
+
169
+		return ( $gf_page && isset( $_GET['edit'] ) || RGForms::post( 'action' ) === 'update' );
170
+	}
171 171
 
172 172
 	/**
173 173
 	 * Is the current page an Edit Entry page?
@@ -178,469 +178,469 @@  discard block
 block discarded – undo
178 178
 		return !empty( $_POST[ self::$nonce_field ] );
179 179
 	}
180 180
 
181
-    /**
182
-     * When Edit entry view is requested setup the vars
183
-     */
184
-    function setup_vars() {
185
-        $gravityview_view = GravityView_View::getInstance();
181
+	/**
182
+	 * When Edit entry view is requested setup the vars
183
+	 */
184
+	function setup_vars() {
185
+		$gravityview_view = GravityView_View::getInstance();
186
+
186 187
 
188
+		$entries = $gravityview_view->getEntries();
189
+		$this->entry = $entries[0];
190
+
191
+		$this->original_form = $this->form = $gravityview_view->getForm();
192
+		$this->form_id = $gravityview_view->getFormId();
193
+		$this->view_id = $gravityview_view->getViewId();
194
+
195
+		self::$nonce_key = GravityView_Edit_Entry::get_nonce_key( $this->view_id, $this->form_id, $this->entry['id'] );
196
+	}
187 197
 
188
-        $entries = $gravityview_view->getEntries();
189
-        $this->entry = $entries[0];
190 198
 
191
-        $this->original_form = $this->form = $gravityview_view->getForm();
192
-        $this->form_id = $gravityview_view->getFormId();
193
-        $this->view_id = $gravityview_view->getViewId();
199
+	/**
200
+	 * Load required files and trigger edit flow
201
+	 *
202
+	 * Run when the is_edit_entry returns true.
203
+	 *
204
+	 * @param GravityView_View_Data $gv_data GravityView Data object
205
+	 * @return void
206
+	 */
207
+	function init( $gv_data ) {
194 208
 
195
-        self::$nonce_key = GravityView_Edit_Entry::get_nonce_key( $this->view_id, $this->form_id, $this->entry['id'] );
196
-    }
209
+		require_once( GFCommon::get_base_path() . '/form_display.php' );
210
+		require_once( GFCommon::get_base_path() . '/entry_detail.php' );
197 211
 
212
+		$this->setup_vars();
198 213
 
199
-    /**
200
-     * Load required files and trigger edit flow
201
-     *
202
-     * Run when the is_edit_entry returns true.
203
-     *
204
-     * @param GravityView_View_Data $gv_data GravityView Data object
205
-     * @return void
206
-     */
207
-    function init( $gv_data ) {
214
+		// Multiple Views embedded, don't proceed if nonce fails
215
+		if( $gv_data->has_multiple_views() && ! wp_verify_nonce( $_GET['edit'], self::$nonce_key ) ) {
216
+			return;
217
+		}
208 218
 
209
-        require_once( GFCommon::get_base_path() . '/form_display.php' );
210
-        require_once( GFCommon::get_base_path() . '/entry_detail.php' );
219
+		// Sorry, you're not allowed here.
220
+		if( false === $this->user_can_edit_entry( true ) ) {
221
+			return;
222
+		}
211 223
 
212
-        $this->setup_vars();
224
+		$this->print_scripts();
213 225
 
214
-        // Multiple Views embedded, don't proceed if nonce fails
215
-        if( $gv_data->has_multiple_views() && ! wp_verify_nonce( $_GET['edit'], self::$nonce_key ) ) {
216
-            return;
217
-        }
226
+		$this->process_save();
218 227
 
219
-        // Sorry, you're not allowed here.
220
-        if( false === $this->user_can_edit_entry( true ) ) {
221
-            return;
222
-        }
228
+		$this->edit_entry_form();
223 229
 
224
-        $this->print_scripts();
230
+	}
225 231
 
226
-        $this->process_save();
227 232
 
228
-        $this->edit_entry_form();
233
+	/**
234
+	 * Force Gravity Forms to output scripts as if it were in the admin
235
+	 * @return void
236
+	 */
237
+	function print_scripts() {
238
+		$gravityview_view = GravityView_View::getInstance();
229 239
 
230
-    }
240
+		wp_register_script( 'gform_gravityforms', GFCommon::get_base_url().'/js/gravityforms.js', array( 'jquery', 'gform_json', 'gform_placeholder', 'sack', 'plupload-all', 'gravityview-fe-view' ) );
231 241
 
242
+		GFFormDisplay::enqueue_form_scripts($gravityview_view->getForm(), false);
232 243
 
233
-    /**
234
-     * Force Gravity Forms to output scripts as if it were in the admin
235
-     * @return void
236
-     */
237
-    function print_scripts() {
238
-        $gravityview_view = GravityView_View::getInstance();
244
+		// Sack is required for images
245
+		wp_print_scripts( array( 'sack', 'gform_gravityforms' ) );
246
+	}
239 247
 
240
-        wp_register_script( 'gform_gravityforms', GFCommon::get_base_url().'/js/gravityforms.js', array( 'jquery', 'gform_json', 'gform_placeholder', 'sack', 'plupload-all', 'gravityview-fe-view' ) );
241 248
 
242
-        GFFormDisplay::enqueue_form_scripts($gravityview_view->getForm(), false);
249
+	/**
250
+	 * Process edit entry form save
251
+	 */
252
+	function process_save() {
243 253
 
244
-        // Sack is required for images
245
-        wp_print_scripts( array( 'sack', 'gform_gravityforms' ) );
246
-    }
254
+		if( empty( $_POST ) ) {
255
+			return;
256
+		}
247 257
 
258
+		// Make sure the entry, view, and form IDs are all correct
259
+		$valid = $this->verify_nonce();
248 260
 
249
-    /**
250
-     * Process edit entry form save
251
-     */
252
-    function process_save() {
261
+		if( !$valid ) {
262
+			do_action('gravityview_log_error', __METHOD__ . ' Nonce validation failed.' );
263
+			return;
264
+		}
253 265
 
254
-        if( empty( $_POST ) ) {
255
-            return;
256
-        }
266
+		if( $this->entry['id'] !== $_POST['lid'] ) {
267
+			do_action('gravityview_log_error', __METHOD__ . ' Entry ID did not match posted entry ID.' );
268
+			return;
269
+		}
257 270
 
258
-        // Make sure the entry, view, and form IDs are all correct
259
-        $valid = $this->verify_nonce();
271
+		do_action('gravityview_log_debug', 'GravityView_Edit_Entry[process_save] $_POSTed data (sanitized): ', esc_html( print_r( $_POST, true ) ) );
260 272
 
261
-        if( !$valid ) {
262
-            do_action('gravityview_log_error', __METHOD__ . ' Nonce validation failed.' );
263
-            return;
264
-        }
273
+		$this->process_save_process_files( $this->form_id );
265 274
 
266
-        if( $this->entry['id'] !== $_POST['lid'] ) {
267
-            do_action('gravityview_log_error', __METHOD__ . ' Entry ID did not match posted entry ID.' );
268
-            return;
269
-        }
275
+		$this->validate();
270 276
 
271
-        do_action('gravityview_log_debug', 'GravityView_Edit_Entry[process_save] $_POSTed data (sanitized): ', esc_html( print_r( $_POST, true ) ) );
277
+		if( $this->is_valid ) {
272 278
 
273
-        $this->process_save_process_files( $this->form_id );
279
+			do_action('gravityview_log_debug', 'GravityView_Edit_Entry[process_save] Submission is valid.' );
274 280
 
275
-        $this->validate();
281
+			/**
282
+			 * @hack This step is needed to unset the adminOnly from form fields
283
+			 */
284
+			$form = $this->form_prepare_for_save();
276 285
 
277
-        if( $this->is_valid ) {
286
+			/**
287
+			 * @hack to avoid the capability validation of the method save_lead for GF 1.9+
288
+			 */
289
+			unset( $_GET['page'] );
278 290
 
279
-            do_action('gravityview_log_debug', 'GravityView_Edit_Entry[process_save] Submission is valid.' );
291
+			GFFormsModel::save_lead( $form, $this->entry );
280 292
 
281
-            /**
282
-             * @hack This step is needed to unset the adminOnly from form fields
283
-             */
284
-            $form = $this->form_prepare_for_save();
293
+			// If there's a post associated with the entry, process post fields
294
+			if( !empty( $this->entry['post_id'] ) ) {
295
+				$this->maybe_update_post_fields( $form );
296
+			}
285 297
 
286
-            /**
287
-             * @hack to avoid the capability validation of the method save_lead for GF 1.9+
288
-             */
289
-            unset( $_GET['page'] );
298
+			// Perform actions normally performed after updating a lead
299
+			$this->after_update();
290 300
 
291
-            GFFormsModel::save_lead( $form, $this->entry );
301
+			/**
302
+			 * @action `gravityview/edit_entry/after_update` Perform an action after the entry has been updated using Edit Entry
303
+			 * @param array $form Gravity Forms form array
304
+			 * @param string $entry_id Numeric ID of the entry that was updated
305
+			 */
306
+			do_action( 'gravityview/edit_entry/after_update', $this->form, $this->entry['id'] );
307
+		}
292 308
 
293
-            // If there's a post associated with the entry, process post fields
294
-            if( !empty( $this->entry['post_id'] ) ) {
295
-                $this->maybe_update_post_fields( $form );
296
-            }
309
+	} // process_save
297 310
 
298
-            // Perform actions normally performed after updating a lead
299
-            $this->after_update();
300 311
 
301
-            /**
302
-             * @action `gravityview/edit_entry/after_update` Perform an action after the entry has been updated using Edit Entry
303
-             * @param array $form Gravity Forms form array
304
-             * @param string $entry_id Numeric ID of the entry that was updated
305
-             */
306
-            do_action( 'gravityview/edit_entry/after_update', $this->form, $this->entry['id'] );
307
-        }
312
+	/**
313
+	 * Have GF handle file uploads
314
+	 *
315
+	 * Copy of code from GFFormDisplay::process_form()
316
+	 *
317
+	 * @param int $form_id
318
+	 */
319
+	function process_save_process_files( $form_id ) {
308 320
 
309
-    } // process_save
321
+		//Loading files that have been uploaded to temp folder
322
+		$files = GFCommon::json_decode( stripslashes( RGForms::post( 'gform_uploaded_files' ) ) );
323
+		if ( ! is_array( $files ) ) {
324
+			$files = array();
325
+		}
310 326
 
327
+		RGFormsModel::$uploaded_files[ $form_id ] = $files;
328
+	}
311 329
 
312
-    /**
313
-     * Have GF handle file uploads
314
-     *
315
-     * Copy of code from GFFormDisplay::process_form()
316
-     *
317
-     * @param int $form_id
318
-     */
319
-    function process_save_process_files( $form_id ) {
330
+	/**
331
+	 * Remove max_files validation (done on gravityforms.js) to avoid conflicts with GravityView
332
+	 * Late validation done on self::custom_validation
333
+	 *
334
+	 * @param $plupload_init array Plupload settings
335
+	 * @param $form_id
336
+	 * @param $instance
337
+	 * @return mixed
338
+	 */
339
+	public function modify_fileupload_settings( $plupload_init, $form_id, $instance ) {
340
+		if( ! $this->is_edit_entry() ) {
341
+			return $plupload_init;
342
+		}
320 343
 
321
-        //Loading files that have been uploaded to temp folder
322
-        $files = GFCommon::json_decode( stripslashes( RGForms::post( 'gform_uploaded_files' ) ) );
323
-        if ( ! is_array( $files ) ) {
324
-            $files = array();
325
-        }
344
+		$plupload_init['gf_vars']['max_files'] = 0;
326 345
 
327
-        RGFormsModel::$uploaded_files[ $form_id ] = $files;
328
-    }
346
+		return $plupload_init;
347
+	}
329 348
 
330
-    /**
331
-     * Remove max_files validation (done on gravityforms.js) to avoid conflicts with GravityView
332
-     * Late validation done on self::custom_validation
333
-     *
334
-     * @param $plupload_init array Plupload settings
335
-     * @param $form_id
336
-     * @param $instance
337
-     * @return mixed
338
-     */
339
-    public function modify_fileupload_settings( $plupload_init, $form_id, $instance ) {
340
-        if( ! $this->is_edit_entry() ) {
341
-            return $plupload_init;
342
-        }
343 349
 
344
-        $plupload_init['gf_vars']['max_files'] = 0;
350
+	/**
351
+	 * Unset adminOnly and convert field input key to string
352
+	 * @return array $form
353
+	 */
354
+	private function form_prepare_for_save() {
355
+		$form = $this->original_form;
345 356
 
346
-        return $plupload_init;
347
-    }
357
+		foreach( $form['fields'] as &$field ) {
348 358
 
359
+			$field->adminOnly = false;
349 360
 
350
-    /**
351
-     * Unset adminOnly and convert field input key to string
352
-     * @return array $form
353
-     */
354
-    private function form_prepare_for_save() {
355
-        $form = $this->original_form;
356
-
357
-        foreach( $form['fields'] as &$field ) {
361
+			if( isset( $field->inputs ) && is_array( $field->inputs ) ) {
362
+				foreach( $field->inputs as $key => $input ) {
363
+					$field->inputs[ $key ][ 'id' ] = (string)$input['id'];
364
+				}
365
+			}
366
+		}
358 367
 
359
-            $field->adminOnly = false;
368
+		return $form;
369
+	}
360 370
 
361
-            if( isset( $field->inputs ) && is_array( $field->inputs ) ) {
362
-                foreach( $field->inputs as $key => $input ) {
363
-                    $field->inputs[ $key ][ 'id' ] = (string)$input['id'];
364
-                }
365
-            }
366
-        }
367 371
 
368
-        return $form;
369
-    }
372
+	/**
373
+	 * Loop through the fields being edited and if they include Post fields, update the Entry's post object
374
+	 *
375
+	 * @param array $form Gravity Forms form
376
+	 *
377
+	 * @return void
378
+	 */
379
+	function maybe_update_post_fields( $form ) {
370 380
 
381
+		$post_id = $this->entry['post_id'];
371 382
 
372
-    /**
373
-     * Loop through the fields being edited and if they include Post fields, update the Entry's post object
374
-     *
375
-     * @param array $form Gravity Forms form
376
-     *
377
-     * @return void
378
-     */
379
-    function maybe_update_post_fields( $form ) {
383
+		// Security check
384
+		if( false === GVCommon::has_cap( 'edit_post', $post_id ) ) {
385
+			do_action( 'gravityview_log_error', 'The current user does not have the ability to edit Post #'.$post_id );
386
+			return;
387
+		}
380 388
 
381
-        $post_id = $this->entry['post_id'];
382
-
383
-        // Security check
384
-        if( false === GVCommon::has_cap( 'edit_post', $post_id ) ) {
385
-            do_action( 'gravityview_log_error', 'The current user does not have the ability to edit Post #'.$post_id );
386
-            return;
387
-        }
389
+		$update_entry = false;
388 390
 
389
-        $update_entry = false;
391
+		$updated_post = $original_post = get_post( $post_id );
390 392
 
391
-        $updated_post = $original_post = get_post( $post_id );
393
+		foreach ( $this->entry as $field_id => $value ) {
392 394
 
393
-        foreach ( $this->entry as $field_id => $value ) {
394
-
395
-            //todo: only run through the edit entry configured fields
395
+			//todo: only run through the edit entry configured fields
396 396
 
397
-            $field = RGFormsModel::get_field( $form, $field_id );
397
+			$field = RGFormsModel::get_field( $form, $field_id );
398 398
 
399
-            if( class_exists('GF_Fields') ) {
400
-                $field = GF_Fields::create( $field );
401
-            }
399
+			if( class_exists('GF_Fields') ) {
400
+				$field = GF_Fields::create( $field );
401
+			}
402 402
 
403
-            if( GFCommon::is_post_field( $field ) ) {
403
+			if( GFCommon::is_post_field( $field ) ) {
404 404
 
405
-                // Get the value of the field, including $_POSTed value
406
-                $value = RGFormsModel::get_field_value( $field );
405
+				// Get the value of the field, including $_POSTed value
406
+				$value = RGFormsModel::get_field_value( $field );
407 407
 
408
-                switch( $field->type ) {
408
+				switch( $field->type ) {
409 409
 
410
-                    case 'post_title':
411
-                    case 'post_content':
412
-                    case 'post_excerpt':
413
-                        $updated_post->{$field->type} = $value;
414
-                        break;
415
-                    case 'post_tags':
416
-                        wp_set_post_tags( $post_id, $value, false );
417
-                        break;
418
-                    case 'post_category':
410
+					case 'post_title':
411
+					case 'post_content':
412
+					case 'post_excerpt':
413
+						$updated_post->{$field->type} = $value;
414
+						break;
415
+					case 'post_tags':
416
+						wp_set_post_tags( $post_id, $value, false );
417
+						break;
418
+					case 'post_category':
419 419
 
420
-                        $categories = is_array( $value ) ? array_values( $value ) : (array)$value;
421
-                        $categories = array_filter( $categories );
420
+						$categories = is_array( $value ) ? array_values( $value ) : (array)$value;
421
+						$categories = array_filter( $categories );
422 422
 
423
-                        wp_set_post_categories( $post_id, $categories, false );
423
+						wp_set_post_categories( $post_id, $categories, false );
424 424
 
425
-                        // if post_category is type checkbox, then value is an array of inputs
426
-                        if( isset( $value[ strval( $field_id ) ] ) ) {
427
-                            foreach( $value as $input_id => $val ) {
428
-                                $input_name = 'input_' . str_replace( '.', '_', $input_id );
429
-                                $this->entry[ strval( $input_id ) ] = RGFormsModel::prepare_value( $form, $field, $val, $input_name, $this->entry['id'] );
430
-                            }
431
-                        } else {
432
-                            $input_name = 'input_' . str_replace( '.', '_', $field_id );
433
-                            $this->entry[ strval( $field_id ) ] = RGFormsModel::prepare_value( $form, $field, $value, $input_name, $this->entry['id'] );
434
-                        }
425
+						// if post_category is type checkbox, then value is an array of inputs
426
+						if( isset( $value[ strval( $field_id ) ] ) ) {
427
+							foreach( $value as $input_id => $val ) {
428
+								$input_name = 'input_' . str_replace( '.', '_', $input_id );
429
+								$this->entry[ strval( $input_id ) ] = RGFormsModel::prepare_value( $form, $field, $val, $input_name, $this->entry['id'] );
430
+							}
431
+						} else {
432
+							$input_name = 'input_' . str_replace( '.', '_', $field_id );
433
+							$this->entry[ strval( $field_id ) ] = RGFormsModel::prepare_value( $form, $field, $value, $input_name, $this->entry['id'] );
434
+						}
435 435
 
436
-                        break;
437
-                    case 'post_custom_field':
436
+						break;
437
+					case 'post_custom_field':
438 438
 
439
-                        $input_type = RGFormsModel::get_input_type( $field );
440
-                        $custom_field_name = $field->postCustomFieldName;
439
+						$input_type = RGFormsModel::get_input_type( $field );
440
+						$custom_field_name = $field->postCustomFieldName;
441 441
 
442
-                        // Only certain custom field types are supported
443
-                        switch( $input_type ) {
444
-                            case 'fileupload':
445
-                            /** @noinspection PhpMissingBreakStatementInspection */
446
-                            case 'list':
447
-                                if( ! is_string( $value ) ) {
448
-                                    $value = function_exists('wp_json_encode') ? wp_json_encode( $value ) : json_encode( $value );
449
-                                }
450
-                            // break; left intentionally out
451
-                            default:
452
-                                update_post_meta( $post_id, $custom_field_name, $value );
453
-                        }
442
+						// Only certain custom field types are supported
443
+						switch( $input_type ) {
444
+							case 'fileupload':
445
+							/** @noinspection PhpMissingBreakStatementInspection */
446
+							case 'list':
447
+								if( ! is_string( $value ) ) {
448
+									$value = function_exists('wp_json_encode') ? wp_json_encode( $value ) : json_encode( $value );
449
+								}
450
+							// break; left intentionally out
451
+							default:
452
+								update_post_meta( $post_id, $custom_field_name, $value );
453
+						}
454 454
 
455
-                        break;
455
+						break;
456 456
 
457
-                    case 'post_image':
457
+					case 'post_image':
458 458
 
459
-                        $input_name = 'input_' . $field_id;
459
+						$input_name = 'input_' . $field_id;
460 460
 
461
-                        if ( !empty( $_FILES[ $input_name ]['name'] ) ) {
461
+						if ( !empty( $_FILES[ $input_name ]['name'] ) ) {
462 462
 
463
-                            // We have a new image
463
+							// We have a new image
464 464
 
465
-                            $value = RGFormsModel::prepare_value( $form, $field, $value, $input_name, $this->entry['id'] );
465
+							$value = RGFormsModel::prepare_value( $form, $field, $value, $input_name, $this->entry['id'] );
466 466
 
467
-                            // is this field set as featured image, if not, leave
468
-                            if ( ! $field->postFeaturedImage ) {
469
-                                break;
470
-                            }
467
+							// is this field set as featured image, if not, leave
468
+							if ( ! $field->postFeaturedImage ) {
469
+								break;
470
+							}
471 471
 
472
-                            $ary = ! empty( $value ) ? explode( '|:|', $value ) : array();
473
-                            $img_url = rgar( $ary, 0 );
472
+							$ary = ! empty( $value ) ? explode( '|:|', $value ) : array();
473
+							$img_url = rgar( $ary, 0 );
474 474
 
475
-                            $img_title       = count( $ary ) > 1 ? $ary[1] : '';
476
-                            $img_caption     = count( $ary ) > 2 ? $ary[2] : '';
477
-                            $img_description = count( $ary ) > 3 ? $ary[3] : '';
475
+							$img_title       = count( $ary ) > 1 ? $ary[1] : '';
476
+							$img_caption     = count( $ary ) > 2 ? $ary[2] : '';
477
+							$img_description = count( $ary ) > 3 ? $ary[3] : '';
478 478
 
479
-                            $image_meta = array(
480
-                                'post_excerpt' => $img_caption,
481
-                                'post_content' => $img_description,
482
-                            );
479
+							$image_meta = array(
480
+								'post_excerpt' => $img_caption,
481
+								'post_content' => $img_description,
482
+							);
483 483
 
484
-                            //adding title only if it is not empty. It will default to the file name if it is not in the array
485
-                            if ( ! empty( $img_title ) ) {
486
-                                $image_meta['post_title'] = $img_title;
487
-                            }
484
+							//adding title only if it is not empty. It will default to the file name if it is not in the array
485
+							if ( ! empty( $img_title ) ) {
486
+								$image_meta['post_title'] = $img_title;
487
+							}
488 488
 
489
-                            //todo: As soon as \GFFormsModel::media_handle_upload becomes a public method, move this call to \GFFormsModel::media_handle_upload and remove the hack from this class.
490
-                            require_once GRAVITYVIEW_DIR . 'includes/class-gravityview-gfformsmodel.php';
491
-                            $media_id = GravityView_GFFormsModel::media_handle_upload( $img_url, $post_id, $image_meta );
489
+							//todo: As soon as \GFFormsModel::media_handle_upload becomes a public method, move this call to \GFFormsModel::media_handle_upload and remove the hack from this class.
490
+							require_once GRAVITYVIEW_DIR . 'includes/class-gravityview-gfformsmodel.php';
491
+							$media_id = GravityView_GFFormsModel::media_handle_upload( $img_url, $post_id, $image_meta );
492 492
 
493
-                            if ( $media_id ) {
494
-                                set_post_thumbnail( $post_id, $media_id );
495
-                            }
493
+							if ( $media_id ) {
494
+								set_post_thumbnail( $post_id, $media_id );
495
+							}
496 496
 
497
-                            break;
497
+							break;
498 498
 
499
-                        } elseif ( !empty( $_POST[ $input_name ] ) && is_array( $value ) ) {
499
+						} elseif ( !empty( $_POST[ $input_name ] ) && is_array( $value ) ) {
500 500
 
501
-                            // Same image although the image title, caption or description might have changed
501
+							// Same image although the image title, caption or description might have changed
502 502
 
503
-                            $ary = ! empty( $this->entry[ $field_id ] ) ? explode( '|:|', $this->entry[ $field_id ] ) : array();
504
-                            $img_url = rgar( $ary, 0 );
503
+							$ary = ! empty( $this->entry[ $field_id ] ) ? explode( '|:|', $this->entry[ $field_id ] ) : array();
504
+							$img_url = rgar( $ary, 0 );
505 505
 
506
-                            // is this really the same image or something went wrong ?
507
-                            if( $img_url === $_POST[ $input_name ] ) {
506
+							// is this really the same image or something went wrong ?
507
+							if( $img_url === $_POST[ $input_name ] ) {
508 508
 
509
-                                $img_title       = isset( $value[ $field_id .'.1' ] ) ? $value[ $field_id .'.1' ] : '';
510
-                                $img_caption     = isset( $value[ $field_id .'.4' ] ) ? $value[ $field_id .'.4' ] : '';
511
-                                $img_description = isset( $value[ $field_id .'.7' ] ) ? $value[ $field_id .'.7' ] : '';
509
+								$img_title       = isset( $value[ $field_id .'.1' ] ) ? $value[ $field_id .'.1' ] : '';
510
+								$img_caption     = isset( $value[ $field_id .'.4' ] ) ? $value[ $field_id .'.4' ] : '';
511
+								$img_description = isset( $value[ $field_id .'.7' ] ) ? $value[ $field_id .'.7' ] : '';
512 512
 
513
-                                $value = ! empty( $img_url ) ? $img_url . "|:|" . $img_title . "|:|" . $img_caption . "|:|" . $img_description : '';
513
+								$value = ! empty( $img_url ) ? $img_url . "|:|" . $img_title . "|:|" . $img_caption . "|:|" . $img_description : '';
514 514
 
515
-                                if ( $field->postFeaturedImage ) {
515
+								if ( $field->postFeaturedImage ) {
516 516
 
517
-                                    $image_meta = array(
518
-                                        'ID' => get_post_thumbnail_id( $post_id ),
519
-                                        'post_title' => $img_title,
520
-                                        'post_excerpt' => $img_caption,
521
-                                        'post_content' => $img_description,
522
-                                    );
517
+									$image_meta = array(
518
+										'ID' => get_post_thumbnail_id( $post_id ),
519
+										'post_title' => $img_title,
520
+										'post_excerpt' => $img_caption,
521
+										'post_content' => $img_description,
522
+									);
523 523
 
524
-                                    // update image title, caption or description
525
-                                    wp_update_post( $image_meta );
526
-                                }
524
+									// update image title, caption or description
525
+									wp_update_post( $image_meta );
526
+								}
527 527
 
528
-                                break;
529
-                            }
528
+								break;
529
+							}
530 530
 
531
-                        }
531
+						}
532 532
 
533
-                        // if we get here, image was removed or not set.
533
+						// if we get here, image was removed or not set.
534 534
 
535
-                        $value = '';
536
-                        if ( $field->postFeaturedImage ) {
537
-                            delete_post_thumbnail( $post_id );
538
-                        }
535
+						$value = '';
536
+						if ( $field->postFeaturedImage ) {
537
+							delete_post_thumbnail( $post_id );
538
+						}
539 539
 
540
-                        break;
540
+						break;
541 541
 
542
-                }
542
+				}
543 543
 
544
-                //ignore fields that have not changed
545
-                if ( $value === rgget( (string) $field_id, $this->entry ) ) {
546
-                    continue;
547
-                }
544
+				//ignore fields that have not changed
545
+				if ( $value === rgget( (string) $field_id, $this->entry ) ) {
546
+					continue;
547
+				}
548 548
 
549
-                // update entry
550
-                if( 'post_category' !== $field->type ) {
551
-                    $this->entry[ strval( $field_id ) ] = $value;
552
-                }
549
+				// update entry
550
+				if( 'post_category' !== $field->type ) {
551
+					$this->entry[ strval( $field_id ) ] = $value;
552
+				}
553 553
 
554
-                $update_entry = true;
554
+				$update_entry = true;
555 555
 
556
-            }
556
+			}
557 557
 
558
-        }
558
+		}
559 559
 
560
-        if( $update_entry ) {
560
+		if( $update_entry ) {
561 561
 
562
-            $return_entry = GFAPI::update_entry( $this->entry );
562
+			$return_entry = GFAPI::update_entry( $this->entry );
563 563
 
564
-            if( is_wp_error( $return_entry ) ) {
565
-                do_action( 'gravityview_log_error', 'Updating the entry post fields failed', $return_entry );
566
-            } else {
567
-                do_action( 'gravityview_log_debug', 'Updating the entry post fields for post #'.$post_id.' succeeded' );
568
-            }
564
+			if( is_wp_error( $return_entry ) ) {
565
+				do_action( 'gravityview_log_error', 'Updating the entry post fields failed', $return_entry );
566
+			} else {
567
+				do_action( 'gravityview_log_debug', 'Updating the entry post fields for post #'.$post_id.' succeeded' );
568
+			}
569 569
 
570
-        }
570
+		}
571 571
 
572
-        $return_post = wp_update_post( $updated_post, true );
572
+		$return_post = wp_update_post( $updated_post, true );
573 573
 
574
-        if( is_wp_error( $return_post ) ) {
575
-            $return_post->add_data( $updated_post, '$updated_post' );
576
-            do_action( 'gravityview_log_error', 'Updating the post content failed', $return_post );
577
-        } else {
578
-            do_action( 'gravityview_log_debug', 'Updating the post content for post #'.$post_id.' succeeded', $updated_post );
579
-        }
574
+		if( is_wp_error( $return_post ) ) {
575
+			$return_post->add_data( $updated_post, '$updated_post' );
576
+			do_action( 'gravityview_log_error', 'Updating the post content failed', $return_post );
577
+		} else {
578
+			do_action( 'gravityview_log_debug', 'Updating the post content for post #'.$post_id.' succeeded', $updated_post );
579
+		}
580 580
 
581
-    }
581
+	}
582 582
 
583 583
 
584
-    /**
585
-     * Perform actions normally performed after updating a lead
586
-     *
587
-     * @since 1.8
588
-     *
589
-     * @see GFEntryDetail::lead_detail_page()
590
-     *
591
-     * @return void
592
-     */
593
-    function after_update() {
584
+	/**
585
+	 * Perform actions normally performed after updating a lead
586
+	 *
587
+	 * @since 1.8
588
+	 *
589
+	 * @see GFEntryDetail::lead_detail_page()
590
+	 *
591
+	 * @return void
592
+	 */
593
+	function after_update() {
594 594
 
595
-        do_action( 'gform_after_update_entry', $this->form, $this->entry['id'] );
596
-        do_action( "gform_after_update_entry_{$this->form['id']}", $this->form, $this->entry['id'] );
595
+		do_action( 'gform_after_update_entry', $this->form, $this->entry['id'] );
596
+		do_action( "gform_after_update_entry_{$this->form['id']}", $this->form, $this->entry['id'] );
597 597
 
598
-        // Re-define the entry now that we've updated it.
599
-        $entry = RGFormsModel::get_lead( $this->entry['id'] );
598
+		// Re-define the entry now that we've updated it.
599
+		$entry = RGFormsModel::get_lead( $this->entry['id'] );
600 600
 
601
-        $entry = GFFormsModel::set_entry_meta( $entry, $this->form );
601
+		$entry = GFFormsModel::set_entry_meta( $entry, $this->form );
602 602
 
603
-        // We need to clear the cache because Gravity Forms caches the field values, which
604
-        // we have just updated.
605
-        foreach ($this->form['fields'] as $key => $field) {
606
-            GFFormsModel::refresh_lead_field_value( $entry['id'], $field->id );
607
-        }
603
+		// We need to clear the cache because Gravity Forms caches the field values, which
604
+		// we have just updated.
605
+		foreach ($this->form['fields'] as $key => $field) {
606
+			GFFormsModel::refresh_lead_field_value( $entry['id'], $field->id );
607
+		}
608 608
 
609
-        $this->entry = $entry;
610
-    }
609
+		$this->entry = $entry;
610
+	}
611 611
 
612 612
 
613
-    /**
614
-     * Display the Edit Entry form
615
-     *
616
-     * @return [type] [description]
617
-     */
618
-    public function edit_entry_form() {
613
+	/**
614
+	 * Display the Edit Entry form
615
+	 *
616
+	 * @return [type] [description]
617
+	 */
618
+	public function edit_entry_form() {
619 619
 
620
-        ?>
620
+		?>
621 621
 
622 622
         <div class="gv-edit-entry-wrapper"><?php
623 623
 
624
-            $javascript = gravityview_ob_include( GravityView_Edit_Entry::$file .'/partials/inline-javascript.php', $this );
624
+			$javascript = gravityview_ob_include( GravityView_Edit_Entry::$file .'/partials/inline-javascript.php', $this );
625 625
 
626
-            /**
627
-             * Fixes weird wpautop() issue
628
-             * @see https://github.com/katzwebservices/GravityView/issues/451
629
-             */
630
-            echo gravityview_strip_whitespace( $javascript );
626
+			/**
627
+			 * Fixes weird wpautop() issue
628
+			 * @see https://github.com/katzwebservices/GravityView/issues/451
629
+			 */
630
+			echo gravityview_strip_whitespace( $javascript );
631 631
 
632
-            ?><h2 class="gv-edit-entry-title">
632
+			?><h2 class="gv-edit-entry-title">
633 633
                 <span><?php
634 634
 
635
-                    /**
636
-                     * @filter `gravityview_edit_entry_title` Modify the edit entry title
637
-                     * @param string $edit_entry_title Modify the "Edit Entry" title
638
-                     * @param GravityView_Edit_Entry_Render $this This object
639
-                     */
640
-                    $edit_entry_title = apply_filters('gravityview_edit_entry_title', __('Edit Entry', 'gravityview'), $this );
635
+					/**
636
+					 * @filter `gravityview_edit_entry_title` Modify the edit entry title
637
+					 * @param string $edit_entry_title Modify the "Edit Entry" title
638
+					 * @param GravityView_Edit_Entry_Render $this This object
639
+					 */
640
+					$edit_entry_title = apply_filters('gravityview_edit_entry_title', __('Edit Entry', 'gravityview'), $this );
641 641
 
642
-                    echo esc_attr( $edit_entry_title );
643
-            ?></span>
642
+					echo esc_attr( $edit_entry_title );
643
+			?></span>
644 644
             </h2>
645 645
 
646 646
             <?php $this->maybe_print_message(); ?>
@@ -651,672 +651,672 @@  discard block
 block discarded – undo
651 651
 
652 652
                 <?php
653 653
 
654
-                wp_nonce_field( self::$nonce_key, self::$nonce_key );
654
+				wp_nonce_field( self::$nonce_key, self::$nonce_key );
655 655
 
656
-                wp_nonce_field( self::$nonce_field, self::$nonce_field, false );
656
+				wp_nonce_field( self::$nonce_field, self::$nonce_field, false );
657 657
 
658
-                // Print the actual form HTML
659
-                $this->render_edit_form();
658
+				// Print the actual form HTML
659
+				$this->render_edit_form();
660 660
 
661
-                ?>
661
+				?>
662 662
             </form>
663 663
 
664 664
         </div>
665 665
 
666 666
     <?php
667
-    }
668
-
669
-    /**
670
-     * Display success or error message if the form has been submitted
671
-     *
672
-     * @uses GVCommon::generate_notice
673
-     *
674
-     * @since TODO
675
-     *
676
-     * @return void
677
-     */
678
-    private function maybe_print_message() {
679
-
680
-        if( rgpost('action') === 'update' ) {
681
-
682
-            $back_link = esc_url( remove_query_arg( array( 'page', 'view', 'edit' ) ) );
683
-
684
-            if( ! $this->is_valid ){
685
-
686
-                // Keeping this compatible with Gravity Forms.
687
-                $validation_message = "<div class='validation_error'>" . __('There was a problem with your submission.', 'gravityview') . " " . __('Errors have been highlighted below.', 'gravityview') . "</div>";
688
-                $message = apply_filters("gform_validation_message_{$this->form['id']}", apply_filters("gform_validation_message", $validation_message, $this->form), $this->form);
689
-
690
-                echo GVCommon::generate_notice( $message , 'gv-error' );
691
-
692
-            } else {
693
-                $entry_updated_message = sprintf( esc_attr__('Entry Updated. %sReturn to Entry%s', 'gravityview'), '<a href="'. $back_link .'">', '</a>' );
694
-
695
-                /**
696
-                 * @filter `gravityview/edit_entry/success` Modify the edit entry success message (including the anchor link)
697
-                 * @since 1.5.4
698
-                 * @param string $entry_updated_message Existing message
699
-                 * @param int $view_id View ID
700
-                 * @param array $entry Gravity Forms entry array
701
-                 * @param string $back_link URL to return to the original entry. @since 1.6
702
-                 */
703
-                $message = apply_filters( 'gravityview/edit_entry/success', $entry_updated_message , $this->view_id, $this->entry, $back_link );
704
-
705
-                echo GVCommon::generate_notice( $message );
706
-            }
707
-
708
-        }
709
-    }
710
-
711
-    /**
712
-     * Display the Edit Entry form in the original Gravity Forms format
713
-     *
714
-     * @since 1.9
715
-     *
716
-     * @param $form
717
-     * @param $lead
718
-     * @param $view_id
719
-     *
720
-     * @return void
721
-     */
722
-    private function render_edit_form() {
723
-
724
-        add_filter( 'gform_pre_render', array( $this, 'filter_modify_form_fields'), 5000, 3 );
725
-        add_filter( 'gform_submit_button', array( $this, 'render_form_buttons') );
726
-        add_filter( 'gform_disable_view_counter', '__return_true' );
727
-
728
-        add_filter( 'gform_field_input', array( $this, 'verify_user_can_edit_post' ), 5, 5 );
729
-        add_filter( 'gform_field_input', array( $this, 'modify_edit_field_input' ), 10, 5 );
730
-
731
-        // We need to remove the fake $_GET['page'] arg to avoid rendering form as if in admin.
732
-        unset( $_GET['page'] );
733
-
734
-        // TODO: Make sure validation isn't handled by GF
735
-        // TODO: Include CSS for file upload fields
736
-        // TODO: Verify multiple-page forms
737
-        // TODO: Product fields are not editable
738
-        // TODO: Check Updated and Error messages
739
-
740
-        $html = GFFormDisplay::get_form( $this->form['id'], false, false, true, $this->entry );
741
-
742
-	    remove_filter( 'gform_pre_render', array( $this, 'filter_modify_form_fields' ), 5000 );
743
-        remove_filter( 'gform_submit_button', array( $this, 'render_form_buttons' ) );
744
-        remove_filter( 'gform_disable_view_counter', '__return_true' );
745
-        remove_filter( 'gform_field_input', array( $this, 'verify_user_can_edit_post' ), 5 );
746
-        remove_filter( 'gform_field_input', array( $this, 'modify_edit_field_input' ), 10 );
747
-
748
-        echo $html;
749
-    }
750
-
751
-    /**
752
-     * Display the Update/Cancel/Delete buttons for the Edit Entry form
753
-     * @since 1.8
754
-     * @return string
755
-     */
756
-    public function render_form_buttons() {
757
-        return gravityview_ob_include( GravityView_Edit_Entry::$file .'/partials/form-buttons.php', $this );
758
-    }
759
-
760
-
761
-    /**
762
-     * Modify the form fields that are shown when using GFFormDisplay::get_form()
763
-     *
764
-     * By default, all fields will be shown. We only want the Edit Tab configured fields to be shown.
765
-     *
766
-     * @param array $form
767
-     * @param boolean $ajax Whether in AJAX mode
768
-     * @param array|string $field_values Passed parameters to the form
769
-     *
770
-     * @since 1.9
771
-     *
772
-     * @return array Modified form array
773
-     */
774
-    public function filter_modify_form_fields( $form, $ajax = false, $field_values = '' ) {
775
-
776
-        // In case we have validated the form, use it to inject the validation results into the form render
777
-        if( isset( $this->form_after_validation ) ) {
778
-            $form = $this->form_after_validation;
779
-        } else {
780
-            $form['fields'] = $this->get_configured_edit_fields( $form, $this->view_id );
781
-        }
782
-
783
-        $form = $this->filter_conditional_logic( $form );
784
-
785
-        // for now we don't support Save and Continue feature.
786
-        if( ! self::$supports_save_and_continue ) {
787
-	        unset( $form['save'] );
788
-        }
789
-
790
-        return $form;
791
-    }
792
-
793
-    /**
794
-     * When displaying a field, check if it's a Post Field, and if so, make sure the post exists and current user has edit rights.
795
-     *
796
-     * @since TODO
797
-     *
798
-     * @param string $field_content Always empty. Returning not-empty overrides the input.
799
-     * @param GF_Field $field
800
-     * @param string|array $value If array, it's a field with multiple inputs. If string, single input.
801
-     * @param int $lead_id Lead ID. Always 0 for the `gform_field_input` filter.
802
-     * @param int $form_id Form ID
803
-     *
804
-     * @return string If error, the error message. If no error, blank string (modify_edit_field_input() runs next)
805
-     */
806
-    function verify_user_can_edit_post( $field_content = '', $field, $value, $lead_id = 0, $form_id ) {
807
-
808
-        if( GFCommon::is_post_field( $field ) ) {
809
-
810
-            $message = null;
811
-
812
-            // First, make sure they have the capability to edit the post.
813
-            if( false === current_user_can( 'edit_post', $this->entry['post_id'] ) ) {
814
-
815
-                /**
816
-                 * @filter `gravityview/edit_entry/unsupported_post_field_text` Modify the message when someone isn't able to edit a post
817
-                 * @param string $message The existing "You don't have permission..." text
818
-                 */
819
-                $message = apply_filters('gravityview/edit_entry/unsupported_post_field_text', __('You don&rsquo;t have permission to edit this post.', 'gravityview') );
820
-
821
-            } elseif( null === get_post( $this->entry['post_id'] ) ) {
822
-                /**
823
-                 * @filter `gravityview/edit_entry/no_post_text` Modify the message when someone is editing an entry attached to a post that no longer exists
824
-                 * @param string $message The existing "This field is not editable; the post no longer exists." text
825
-                 */
826
-                $message = apply_filters('gravityview/edit_entry/no_post_text', __('This field is not editable; the post no longer exists.', 'gravityview' ) );
827
-            }
828
-
829
-            if( $message ) {
830
-                $field_content = sprintf('<div class="ginput_container ginput_container_' . $field->type . '">%s</div>', wpautop( $message ) );
831
-            }
832
-        }
833
-
834
-        return $field_content;
835
-    }
836
-
837
-    /**
838
-     *
839
-     * Fill-in the saved values into the form inputs
840
-     *
841
-     * @param string $field_content Always empty. Returning not-empty overrides the input.
842
-     * @param GF_Field $field
843
-     * @param string|array $value If array, it's a field with multiple inputs. If string, single input.
844
-     * @param int $lead_id Lead ID. Always 0 for the `gform_field_input` filter.
845
-     * @param int $form_id Form ID
846
-     *
847
-     * @return mixed
848
-     */
849
-    function modify_edit_field_input( $field_content = '', $field, $value, $lead_id = 0, $form_id ) {
850
-
851
-        $gv_field = GravityView_Fields::get_associated_field( $field );
852
-
853
-        // If the form has been submitted, then we don't need to pre-fill the values,
854
-        // Except for fileupload type and when a field input is overridden- run always!!
855
-        if(
856
-            ( $this->is_edit_entry_submission() && !in_array( $field->type, array( 'fileupload', 'post_image' ) ) )
857
-            && false === ( $gv_field && is_callable( array( $gv_field, 'get_field_input' ) ) )
858
-            || ! empty( $field_content )
859
-            || GFCommon::is_product_field( $field->type ) // Prevent product fields from appearing editable
860
-        ) {
861
-	        return $field_content;
862
-        }
863
-
864
-        // Turn on Admin-style display for file upload fields only
865
-        if( 'fileupload' === $field->type ) {
866
-            $_GET['page'] = 'gf_entries';
867
-        }
868
-
869
-        // SET SOME FIELD DEFAULTS TO PREVENT ISSUES
870
-        $field->adminOnly = false; /** @see GFFormDisplay::get_counter_init_script() need to prevent adminOnly */
871
-
872
-        // add categories as choices for Post Category field
873
-        if ( 'post_category' === $field->type ) {
874
-            $field = GFCommon::add_categories_as_choices( $field, $value );
875
-        }
876
-
877
-        $field_value = $this->get_field_value( $field );
878
-
879
-        /**
880
-         * @filter `gravityview/edit_entry/field_value` Change the value of an Edit Entry field, if needed
881
-         * @since 1.11
882
-         * @param mixed $field_value field value used to populate the input
883
-         * @param object $field Gravity Forms field object ( Class GF_Field )
884
-         */
885
-        $field_value = apply_filters( 'gravityview/edit_entry/field_value', $field_value, $field );
886
-
887
-	    // Prevent any PHP warnings, like undefined index
888
-	    ob_start();
889
-
890
-        if( $gv_field && is_callable( array( $gv_field, 'get_field_input' ) ) ) {
891
-            $return = $gv_field->get_field_input( $this->form, $field_value, $this->entry, $field );
892
-        } else {
893
-	        $return = $field->get_field_input( $this->form, $field_value, $this->entry );
894
-        }
895
-
896
-
897
-	    // If there was output, it's an error
898
-	    $warnings = ob_get_clean();
899
-
900
-	    if( !empty( $warnings ) ) {
901
-		    do_action( 'gravityview_log_error', __METHOD__ . $warnings, $field_value );
902
-	    }
903
-
904
-        /**
905
-         * Unset hack $_GET['page'] = 'gf_entries'
906
-         * We need the fileupload html field to render with the proper id
907
-         *  ( <li id="field_80_16" ... > )
908
-         */
909
-        unset( $_GET['page'] );
910
-
911
-        return $return;
912
-    }
913
-
914
-    /**
915
-     * Modify the value for the current field input
916
-     *
917
-     * @param GF_Field $field
918
-     *
919
-     * @return array|mixed|string|void
920
-     */
921
-    private function get_field_value( $field ) {
922
-
923
-        /**
924
-         * @filter `gravityview/edit_entry/pre_populate/override` Allow the pre-populated value to override saved value in Edit Entry form. By default, pre-populate mechanism only kicks on empty fields.
925
-         * @param boolean True: override saved values; False: don't override (default)
926
-         * @param $field GF_Field object Gravity Forms field object
927
-         * @since 1.13
928
-         */
929
-        $override_saved_value = apply_filters( 'gravityview/edit_entry/pre_populate/override', false, $field );
930
-
931
-        // We're dealing with multiple inputs (e.g. checkbox) but not time or date (as it doesn't store data in input IDs)
932
-        if( isset( $field->inputs ) && is_array( $field->inputs ) && !in_array( $field->type, array( 'time', 'date' ) ) ) {
933
-
934
-            $field_value = array();
935
-
936
-            // only accept pre-populated values if the field doesn't have any choice selected.
937
-            $allow_pre_populated = $field->allowsPrepopulate;
938
-
939
-            foreach ( (array)$field->inputs as $input ) {
940
-
941
-                $input_id = strval( $input['id'] );
667
+	}
668
+
669
+	/**
670
+	 * Display success or error message if the form has been submitted
671
+	 *
672
+	 * @uses GVCommon::generate_notice
673
+	 *
674
+	 * @since TODO
675
+	 *
676
+	 * @return void
677
+	 */
678
+	private function maybe_print_message() {
679
+
680
+		if( rgpost('action') === 'update' ) {
681
+
682
+			$back_link = esc_url( remove_query_arg( array( 'page', 'view', 'edit' ) ) );
683
+
684
+			if( ! $this->is_valid ){
685
+
686
+				// Keeping this compatible with Gravity Forms.
687
+				$validation_message = "<div class='validation_error'>" . __('There was a problem with your submission.', 'gravityview') . " " . __('Errors have been highlighted below.', 'gravityview') . "</div>";
688
+				$message = apply_filters("gform_validation_message_{$this->form['id']}", apply_filters("gform_validation_message", $validation_message, $this->form), $this->form);
689
+
690
+				echo GVCommon::generate_notice( $message , 'gv-error' );
691
+
692
+			} else {
693
+				$entry_updated_message = sprintf( esc_attr__('Entry Updated. %sReturn to Entry%s', 'gravityview'), '<a href="'. $back_link .'">', '</a>' );
694
+
695
+				/**
696
+				 * @filter `gravityview/edit_entry/success` Modify the edit entry success message (including the anchor link)
697
+				 * @since 1.5.4
698
+				 * @param string $entry_updated_message Existing message
699
+				 * @param int $view_id View ID
700
+				 * @param array $entry Gravity Forms entry array
701
+				 * @param string $back_link URL to return to the original entry. @since 1.6
702
+				 */
703
+				$message = apply_filters( 'gravityview/edit_entry/success', $entry_updated_message , $this->view_id, $this->entry, $back_link );
704
+
705
+				echo GVCommon::generate_notice( $message );
706
+			}
707
+
708
+		}
709
+	}
710
+
711
+	/**
712
+	 * Display the Edit Entry form in the original Gravity Forms format
713
+	 *
714
+	 * @since 1.9
715
+	 *
716
+	 * @param $form
717
+	 * @param $lead
718
+	 * @param $view_id
719
+	 *
720
+	 * @return void
721
+	 */
722
+	private function render_edit_form() {
723
+
724
+		add_filter( 'gform_pre_render', array( $this, 'filter_modify_form_fields'), 5000, 3 );
725
+		add_filter( 'gform_submit_button', array( $this, 'render_form_buttons') );
726
+		add_filter( 'gform_disable_view_counter', '__return_true' );
727
+
728
+		add_filter( 'gform_field_input', array( $this, 'verify_user_can_edit_post' ), 5, 5 );
729
+		add_filter( 'gform_field_input', array( $this, 'modify_edit_field_input' ), 10, 5 );
730
+
731
+		// We need to remove the fake $_GET['page'] arg to avoid rendering form as if in admin.
732
+		unset( $_GET['page'] );
733
+
734
+		// TODO: Make sure validation isn't handled by GF
735
+		// TODO: Include CSS for file upload fields
736
+		// TODO: Verify multiple-page forms
737
+		// TODO: Product fields are not editable
738
+		// TODO: Check Updated and Error messages
739
+
740
+		$html = GFFormDisplay::get_form( $this->form['id'], false, false, true, $this->entry );
741
+
742
+		remove_filter( 'gform_pre_render', array( $this, 'filter_modify_form_fields' ), 5000 );
743
+		remove_filter( 'gform_submit_button', array( $this, 'render_form_buttons' ) );
744
+		remove_filter( 'gform_disable_view_counter', '__return_true' );
745
+		remove_filter( 'gform_field_input', array( $this, 'verify_user_can_edit_post' ), 5 );
746
+		remove_filter( 'gform_field_input', array( $this, 'modify_edit_field_input' ), 10 );
747
+
748
+		echo $html;
749
+	}
750
+
751
+	/**
752
+	 * Display the Update/Cancel/Delete buttons for the Edit Entry form
753
+	 * @since 1.8
754
+	 * @return string
755
+	 */
756
+	public function render_form_buttons() {
757
+		return gravityview_ob_include( GravityView_Edit_Entry::$file .'/partials/form-buttons.php', $this );
758
+	}
759
+
760
+
761
+	/**
762
+	 * Modify the form fields that are shown when using GFFormDisplay::get_form()
763
+	 *
764
+	 * By default, all fields will be shown. We only want the Edit Tab configured fields to be shown.
765
+	 *
766
+	 * @param array $form
767
+	 * @param boolean $ajax Whether in AJAX mode
768
+	 * @param array|string $field_values Passed parameters to the form
769
+	 *
770
+	 * @since 1.9
771
+	 *
772
+	 * @return array Modified form array
773
+	 */
774
+	public function filter_modify_form_fields( $form, $ajax = false, $field_values = '' ) {
775
+
776
+		// In case we have validated the form, use it to inject the validation results into the form render
777
+		if( isset( $this->form_after_validation ) ) {
778
+			$form = $this->form_after_validation;
779
+		} else {
780
+			$form['fields'] = $this->get_configured_edit_fields( $form, $this->view_id );
781
+		}
782
+
783
+		$form = $this->filter_conditional_logic( $form );
784
+
785
+		// for now we don't support Save and Continue feature.
786
+		if( ! self::$supports_save_and_continue ) {
787
+			unset( $form['save'] );
788
+		}
789
+
790
+		return $form;
791
+	}
792
+
793
+	/**
794
+	 * When displaying a field, check if it's a Post Field, and if so, make sure the post exists and current user has edit rights.
795
+	 *
796
+	 * @since TODO
797
+	 *
798
+	 * @param string $field_content Always empty. Returning not-empty overrides the input.
799
+	 * @param GF_Field $field
800
+	 * @param string|array $value If array, it's a field with multiple inputs. If string, single input.
801
+	 * @param int $lead_id Lead ID. Always 0 for the `gform_field_input` filter.
802
+	 * @param int $form_id Form ID
803
+	 *
804
+	 * @return string If error, the error message. If no error, blank string (modify_edit_field_input() runs next)
805
+	 */
806
+	function verify_user_can_edit_post( $field_content = '', $field, $value, $lead_id = 0, $form_id ) {
807
+
808
+		if( GFCommon::is_post_field( $field ) ) {
809
+
810
+			$message = null;
811
+
812
+			// First, make sure they have the capability to edit the post.
813
+			if( false === current_user_can( 'edit_post', $this->entry['post_id'] ) ) {
814
+
815
+				/**
816
+				 * @filter `gravityview/edit_entry/unsupported_post_field_text` Modify the message when someone isn't able to edit a post
817
+				 * @param string $message The existing "You don't have permission..." text
818
+				 */
819
+				$message = apply_filters('gravityview/edit_entry/unsupported_post_field_text', __('You don&rsquo;t have permission to edit this post.', 'gravityview') );
820
+
821
+			} elseif( null === get_post( $this->entry['post_id'] ) ) {
822
+				/**
823
+				 * @filter `gravityview/edit_entry/no_post_text` Modify the message when someone is editing an entry attached to a post that no longer exists
824
+				 * @param string $message The existing "This field is not editable; the post no longer exists." text
825
+				 */
826
+				$message = apply_filters('gravityview/edit_entry/no_post_text', __('This field is not editable; the post no longer exists.', 'gravityview' ) );
827
+			}
828
+
829
+			if( $message ) {
830
+				$field_content = sprintf('<div class="ginput_container ginput_container_' . $field->type . '">%s</div>', wpautop( $message ) );
831
+			}
832
+		}
833
+
834
+		return $field_content;
835
+	}
836
+
837
+	/**
838
+	 *
839
+	 * Fill-in the saved values into the form inputs
840
+	 *
841
+	 * @param string $field_content Always empty. Returning not-empty overrides the input.
842
+	 * @param GF_Field $field
843
+	 * @param string|array $value If array, it's a field with multiple inputs. If string, single input.
844
+	 * @param int $lead_id Lead ID. Always 0 for the `gform_field_input` filter.
845
+	 * @param int $form_id Form ID
846
+	 *
847
+	 * @return mixed
848
+	 */
849
+	function modify_edit_field_input( $field_content = '', $field, $value, $lead_id = 0, $form_id ) {
850
+
851
+		$gv_field = GravityView_Fields::get_associated_field( $field );
852
+
853
+		// If the form has been submitted, then we don't need to pre-fill the values,
854
+		// Except for fileupload type and when a field input is overridden- run always!!
855
+		if(
856
+			( $this->is_edit_entry_submission() && !in_array( $field->type, array( 'fileupload', 'post_image' ) ) )
857
+			&& false === ( $gv_field && is_callable( array( $gv_field, 'get_field_input' ) ) )
858
+			|| ! empty( $field_content )
859
+			|| GFCommon::is_product_field( $field->type ) // Prevent product fields from appearing editable
860
+		) {
861
+			return $field_content;
862
+		}
863
+
864
+		// Turn on Admin-style display for file upload fields only
865
+		if( 'fileupload' === $field->type ) {
866
+			$_GET['page'] = 'gf_entries';
867
+		}
868
+
869
+		// SET SOME FIELD DEFAULTS TO PREVENT ISSUES
870
+		$field->adminOnly = false; /** @see GFFormDisplay::get_counter_init_script() need to prevent adminOnly */
871
+
872
+		// add categories as choices for Post Category field
873
+		if ( 'post_category' === $field->type ) {
874
+			$field = GFCommon::add_categories_as_choices( $field, $value );
875
+		}
876
+
877
+		$field_value = $this->get_field_value( $field );
878
+
879
+		/**
880
+		 * @filter `gravityview/edit_entry/field_value` Change the value of an Edit Entry field, if needed
881
+		 * @since 1.11
882
+		 * @param mixed $field_value field value used to populate the input
883
+		 * @param object $field Gravity Forms field object ( Class GF_Field )
884
+		 */
885
+		$field_value = apply_filters( 'gravityview/edit_entry/field_value', $field_value, $field );
886
+
887
+		// Prevent any PHP warnings, like undefined index
888
+		ob_start();
889
+
890
+		if( $gv_field && is_callable( array( $gv_field, 'get_field_input' ) ) ) {
891
+			$return = $gv_field->get_field_input( $this->form, $field_value, $this->entry, $field );
892
+		} else {
893
+			$return = $field->get_field_input( $this->form, $field_value, $this->entry );
894
+		}
895
+
896
+
897
+		// If there was output, it's an error
898
+		$warnings = ob_get_clean();
899
+
900
+		if( !empty( $warnings ) ) {
901
+			do_action( 'gravityview_log_error', __METHOD__ . $warnings, $field_value );
902
+		}
903
+
904
+		/**
905
+		 * Unset hack $_GET['page'] = 'gf_entries'
906
+		 * We need the fileupload html field to render with the proper id
907
+		 *  ( <li id="field_80_16" ... > )
908
+		 */
909
+		unset( $_GET['page'] );
910
+
911
+		return $return;
912
+	}
913
+
914
+	/**
915
+	 * Modify the value for the current field input
916
+	 *
917
+	 * @param GF_Field $field
918
+	 *
919
+	 * @return array|mixed|string|void
920
+	 */
921
+	private function get_field_value( $field ) {
922
+
923
+		/**
924
+		 * @filter `gravityview/edit_entry/pre_populate/override` Allow the pre-populated value to override saved value in Edit Entry form. By default, pre-populate mechanism only kicks on empty fields.
925
+		 * @param boolean True: override saved values; False: don't override (default)
926
+		 * @param $field GF_Field object Gravity Forms field object
927
+		 * @since 1.13
928
+		 */
929
+		$override_saved_value = apply_filters( 'gravityview/edit_entry/pre_populate/override', false, $field );
930
+
931
+		// We're dealing with multiple inputs (e.g. checkbox) but not time or date (as it doesn't store data in input IDs)
932
+		if( isset( $field->inputs ) && is_array( $field->inputs ) && !in_array( $field->type, array( 'time', 'date' ) ) ) {
933
+
934
+			$field_value = array();
935
+
936
+			// only accept pre-populated values if the field doesn't have any choice selected.
937
+			$allow_pre_populated = $field->allowsPrepopulate;
938
+
939
+			foreach ( (array)$field->inputs as $input ) {
940
+
941
+				$input_id = strval( $input['id'] );
942 942
                 
943
-                if ( isset( $this->entry[ $input_id ] ) && ! gv_empty( $this->entry[ $input_id ], false, false ) ) {
944
-                    $field_value[ $input_id ] =  'post_category' === $field->type ? GFCommon::format_post_category( $this->entry[ $input_id ], true ) : $this->entry[ $input_id ];
945
-                    $allow_pre_populated = false;
946
-                }
943
+				if ( isset( $this->entry[ $input_id ] ) && ! gv_empty( $this->entry[ $input_id ], false, false ) ) {
944
+					$field_value[ $input_id ] =  'post_category' === $field->type ? GFCommon::format_post_category( $this->entry[ $input_id ], true ) : $this->entry[ $input_id ];
945
+					$allow_pre_populated = false;
946
+				}
947 947
 
948
-            }
948
+			}
949 949
 
950
-            $pre_value = $field->get_value_submission( array(), false );
950
+			$pre_value = $field->get_value_submission( array(), false );
951 951
 
952
-            $field_value = ! $allow_pre_populated && ! ( $override_saved_value && !gv_empty( $pre_value, false, false ) ) ? $field_value : $pre_value;
952
+			$field_value = ! $allow_pre_populated && ! ( $override_saved_value && !gv_empty( $pre_value, false, false ) ) ? $field_value : $pre_value;
953 953
 
954
-        } else {
954
+		} else {
955 955
 
956
-            $id = intval( $field->id );
956
+			$id = intval( $field->id );
957 957
 
958
-            // get pre-populated value if exists
959
-            $pre_value = $field->allowsPrepopulate ? GFFormsModel::get_parameter_value( $field->inputName, array(), $field ) : '';
958
+			// get pre-populated value if exists
959
+			$pre_value = $field->allowsPrepopulate ? GFFormsModel::get_parameter_value( $field->inputName, array(), $field ) : '';
960 960
 
961
-            // saved field entry value (if empty, fallback to the pre-populated value, if exists)
962
-            // or pre-populated value if not empty and set to override saved value
963
-            $field_value = !gv_empty( $this->entry[ $id ], false, false ) && ! ( $override_saved_value && !gv_empty( $pre_value, false, false ) ) ? $this->entry[ $id ] : $pre_value;
961
+			// saved field entry value (if empty, fallback to the pre-populated value, if exists)
962
+			// or pre-populated value if not empty and set to override saved value
963
+			$field_value = !gv_empty( $this->entry[ $id ], false, false ) && ! ( $override_saved_value && !gv_empty( $pre_value, false, false ) ) ? $this->entry[ $id ] : $pre_value;
964 964
 
965
-            // in case field is post_category but inputType is select, multi-select or radio, convert value into array of category IDs.
966
-            if ( 'post_category' === $field->type && !gv_empty( $field_value, false, false ) ) {
967
-                $categories = array();
968
-                foreach ( explode( ',', $field_value ) as $cat_string ) {
969
-                    $categories[] = GFCommon::format_post_category( $cat_string, true );
970
-                }
971
-                $field_value = 'multiselect' === $field->get_input_type() ? $categories : implode( '', $categories );
972
-            }
965
+			// in case field is post_category but inputType is select, multi-select or radio, convert value into array of category IDs.
966
+			if ( 'post_category' === $field->type && !gv_empty( $field_value, false, false ) ) {
967
+				$categories = array();
968
+				foreach ( explode( ',', $field_value ) as $cat_string ) {
969
+					$categories[] = GFCommon::format_post_category( $cat_string, true );
970
+				}
971
+				$field_value = 'multiselect' === $field->get_input_type() ? $categories : implode( '', $categories );
972
+			}
973 973
 
974
-        }
974
+		}
975 975
 
976
-        // if value is empty get the default value if defined
977
-        $field_value = $field->get_value_default_if_empty( $field_value );
976
+		// if value is empty get the default value if defined
977
+		$field_value = $field->get_value_default_if_empty( $field_value );
978 978
 
979
-        return $field_value;
980
-    }
979
+		return $field_value;
980
+	}
981 981
 
982 982
 
983
-    // ---- Entry validation
983
+	// ---- Entry validation
984 984
 
985
-    /**
986
-     * Add field keys that Gravity Forms expects.
987
-     *
988
-     * @see GFFormDisplay::validate()
989
-     * @param  array $form GF Form
990
-     * @return array       Modified GF Form
991
-     */
992
-    function gform_pre_validation( $form ) {
985
+	/**
986
+	 * Add field keys that Gravity Forms expects.
987
+	 *
988
+	 * @see GFFormDisplay::validate()
989
+	 * @param  array $form GF Form
990
+	 * @return array       Modified GF Form
991
+	 */
992
+	function gform_pre_validation( $form ) {
993 993
 
994
-        if( ! $this->verify_nonce() ) {
995
-            return $form;
996
-        }
994
+		if( ! $this->verify_nonce() ) {
995
+			return $form;
996
+		}
997 997
 
998
-        // Fix PHP warning regarding undefined index.
999
-        foreach ( $form['fields'] as &$field) {
998
+		// Fix PHP warning regarding undefined index.
999
+		foreach ( $form['fields'] as &$field) {
1000 1000
 
1001
-            // This is because we're doing admin form pretending to be front-end, so Gravity Forms
1002
-            // expects certain field array items to be set.
1003
-            foreach ( array( 'noDuplicates', 'adminOnly', 'inputType', 'isRequired', 'enablePrice', 'inputs', 'allowedExtensions' ) as $key ) {
1004
-	            $field->{$key} = isset( $field->{$key} ) ? $field->{$key} : NULL;
1005
-            }
1001
+			// This is because we're doing admin form pretending to be front-end, so Gravity Forms
1002
+			// expects certain field array items to be set.
1003
+			foreach ( array( 'noDuplicates', 'adminOnly', 'inputType', 'isRequired', 'enablePrice', 'inputs', 'allowedExtensions' ) as $key ) {
1004
+				$field->{$key} = isset( $field->{$key} ) ? $field->{$key} : NULL;
1005
+			}
1006 1006
 
1007
-            // unset emailConfirmEnabled for email type fields
1008
-           /* if( 'email' === $field['type'] && !empty( $field['emailConfirmEnabled'] ) ) {
1007
+			// unset emailConfirmEnabled for email type fields
1008
+		   /* if( 'email' === $field['type'] && !empty( $field['emailConfirmEnabled'] ) ) {
1009 1009
                 $field['emailConfirmEnabled'] = '';
1010 1010
             }*/
1011 1011
 
1012
-            switch( RGFormsModel::get_input_type( $field ) ) {
1012
+			switch( RGFormsModel::get_input_type( $field ) ) {
1013 1013
 
1014
-                /**
1015
-                 * this whole fileupload hack is because in the admin, Gravity Forms simply doesn't update any fileupload field if it's empty, but it DOES in the frontend.
1016
-                 *
1017
-                 * What we have to do is set the value so that it doesn't get overwritten as empty on save and appears immediately in the Edit Entry screen again.
1018
-                 *
1019
-                 * @hack
1020
-                 */
1021
-                case 'fileupload':
1022
-
1023
-                    // Set the previous value
1024
-                    $entry = $this->get_entry();
1025
-
1026
-                    $input_name = 'input_'.$field->id;
1027
-                    $form_id = $form['id'];
1028
-
1029
-                    $value = NULL;
1030
-
1031
-                    // Use the previous entry value as the default.
1032
-                    if( isset( $entry[ $field->id ] ) ) {
1033
-                        $value = $entry[ $field->id ];
1034
-                    }
1014
+				/**
1015
+				 * this whole fileupload hack is because in the admin, Gravity Forms simply doesn't update any fileupload field if it's empty, but it DOES in the frontend.
1016
+				 *
1017
+				 * What we have to do is set the value so that it doesn't get overwritten as empty on save and appears immediately in the Edit Entry screen again.
1018
+				 *
1019
+				 * @hack
1020
+				 */
1021
+				case 'fileupload':
1035 1022
 
1036
-                    // If this is a single upload file
1037
-                    if( !empty( $_FILES[ $input_name ] ) && !empty( $_FILES[ $input_name ]['name'] ) ) {
1038
-                        $file_path = GFFormsModel::get_file_upload_path( $form['id'], $_FILES[ $input_name ]['name'] );
1039
-                        $value = $file_path['url'];
1023
+					// Set the previous value
1024
+					$entry = $this->get_entry();
1040 1025
 
1041
-                    } else {
1042
-
1043
-                        // Fix PHP warning on line 1498 of form_display.php for post_image fields
1044
-                        // Fix PHP Notice:  Undefined index:  size in form_display.php on line 1511
1045
-                        $_FILES[ $input_name ] = array('name' => '', 'size' => '' );
1046
-
1047
-                    }
1048
-
1049
-                    if( rgar($field, "multipleFiles") ) {
1050
-
1051
-                        // If there are fresh uploads, process and merge them.
1052
-                        // Otherwise, use the passed values, which should be json-encoded array of URLs
1053
-                        if( isset( GFFormsModel::$uploaded_files[$form_id][$input_name] ) ) {
1054
-                            $value = empty( $value ) ? '[]' : $value;
1055
-                            $value = stripslashes_deep( $value );
1056
-                            $value = GFFormsModel::prepare_value( $form, $field, $value, $input_name, $entry['id'], array());
1057
-                        }
1026
+					$input_name = 'input_'.$field->id;
1027
+					$form_id = $form['id'];
1058 1028
 
1059
-                    } else {
1029
+					$value = NULL;
1060 1030
 
1061
-                        // A file already exists when editing an entry
1062
-                        // We set this to solve issue when file upload fields are required.
1063
-                        GFFormsModel::$uploaded_files[ $form_id ][ $input_name ] = $value;
1031
+					// Use the previous entry value as the default.
1032
+					if( isset( $entry[ $field->id ] ) ) {
1033
+						$value = $entry[ $field->id ];
1034
+					}
1064 1035
 
1065
-                    }
1036
+					// If this is a single upload file
1037
+					if( !empty( $_FILES[ $input_name ] ) && !empty( $_FILES[ $input_name ]['name'] ) ) {
1038
+						$file_path = GFFormsModel::get_file_upload_path( $form['id'], $_FILES[ $input_name ]['name'] );
1039
+						$value = $file_path['url'];
1066 1040
 
1067
-                    $this->entry[ $input_name ] = $value;
1068
-                    $_POST[ $input_name ] = $value;
1041
+					} else {
1069 1042
 
1070
-                    break;
1043
+						// Fix PHP warning on line 1498 of form_display.php for post_image fields
1044
+						// Fix PHP Notice:  Undefined index:  size in form_display.php on line 1511
1045
+						$_FILES[ $input_name ] = array('name' => '', 'size' => '' );
1071 1046
 
1072
-                case 'number':
1073
-                    // Fix "undefined index" issue at line 1286 in form_display.php
1074
-                    if( !isset( $_POST['input_'.$field->id ] ) ) {
1075
-                        $_POST['input_'.$field->id ] = NULL;
1076
-                    }
1077
-                    break;
1078
-                case 'captcha':
1079
-                    // Fix issue with recaptcha_check_answer() on line 1458 in form_display.php
1080
-                    $_POST['recaptcha_challenge_field'] = NULL;
1081
-                    $_POST['recaptcha_response_field'] = NULL;
1082
-                    break;
1083
-            }
1047
+					}
1084 1048
 
1085
-        }
1049
+					if( rgar($field, "multipleFiles") ) {
1086 1050
 
1087
-        return $form;
1088
-    }
1051
+						// If there are fresh uploads, process and merge them.
1052
+						// Otherwise, use the passed values, which should be json-encoded array of URLs
1053
+						if( isset( GFFormsModel::$uploaded_files[$form_id][$input_name] ) ) {
1054
+							$value = empty( $value ) ? '[]' : $value;
1055
+							$value = stripslashes_deep( $value );
1056
+							$value = GFFormsModel::prepare_value( $form, $field, $value, $input_name, $entry['id'], array());
1057
+						}
1089 1058
 
1059
+					} else {
1090 1060
 
1091
-    /**
1092
-     * Process validation for a edit entry submission
1093
-     *
1094
-     * Sets the `is_valid` object var
1095
-     *
1096
-     * @return void
1097
-     */
1098
-    function validate() {
1061
+						// A file already exists when editing an entry
1062
+						// We set this to solve issue when file upload fields are required.
1063
+						GFFormsModel::$uploaded_files[ $form_id ][ $input_name ] = $value;
1099 1064
 
1100
-        /**
1101
-         * If using GF User Registration Add-on, remove the validation step, otherwise generates error when updating the entry
1102
-         * GF User Registration Add-on version > 3.x has a different class name
1103
-         * @since 1.16.2
1104
-         */
1105
-        if ( class_exists( 'GF_User_Registration' ) ) {
1106
-            remove_filter( 'gform_validation', array( GF_User_Registration::get_instance(), 'validate' ) );
1107
-        } else  if ( class_exists( 'GFUser' ) ) {
1108
-            remove_filter( 'gform_validation', array( 'GFUser', 'user_registration_validation' ) );
1109
-        }
1065
+					}
1110 1066
 
1067
+					$this->entry[ $input_name ] = $value;
1068
+					$_POST[ $input_name ] = $value;
1111 1069
 
1112
-        /**
1113
-         * For some crazy reason, Gravity Forms doesn't validate Edit Entry form submissions.
1114
-         * You can enter whatever you want!
1115
-         * We try validating, and customize the results using `self::custom_validation()`
1116
-         */
1117
-        add_filter( 'gform_validation_'. $this->form_id, array( $this, 'custom_validation' ), 10, 4);
1070
+					break;
1118 1071
 
1119
-        // Needed by the validate funtion
1120
-        $failed_validation_page = NULL;
1121
-        $field_values = RGForms::post( 'gform_field_values' );
1072
+				case 'number':
1073
+					// Fix "undefined index" issue at line 1286 in form_display.php
1074
+					if( !isset( $_POST['input_'.$field->id ] ) ) {
1075
+						$_POST['input_'.$field->id ] = NULL;
1076
+					}
1077
+					break;
1078
+				case 'captcha':
1079
+					// Fix issue with recaptcha_check_answer() on line 1458 in form_display.php
1080
+					$_POST['recaptcha_challenge_field'] = NULL;
1081
+					$_POST['recaptcha_response_field'] = NULL;
1082
+					break;
1083
+			}
1122 1084
 
1123
-        // Prevent entry limit from running when editing an entry, also
1124
-        // prevent form scheduling from preventing editing
1125
-        unset( $this->form['limitEntries'], $this->form['scheduleForm'] );
1085
+		}
1126 1086
 
1127
-        // Hide fields depending on Edit Entry settings
1128
-        $this->form['fields'] = $this->get_configured_edit_fields( $this->form, $this->view_id );
1087
+		return $form;
1088
+	}
1129 1089
 
1130
-        $this->is_valid = GFFormDisplay::validate( $this->form, $field_values, 1, $failed_validation_page );
1131 1090
 
1132
-        remove_filter( 'gform_validation_'. $this->form_id, array( $this, 'custom_validation' ), 10 );
1133
-    }
1091
+	/**
1092
+	 * Process validation for a edit entry submission
1093
+	 *
1094
+	 * Sets the `is_valid` object var
1095
+	 *
1096
+	 * @return void
1097
+	 */
1098
+	function validate() {
1099
+
1100
+		/**
1101
+		 * If using GF User Registration Add-on, remove the validation step, otherwise generates error when updating the entry
1102
+		 * GF User Registration Add-on version > 3.x has a different class name
1103
+		 * @since 1.16.2
1104
+		 */
1105
+		if ( class_exists( 'GF_User_Registration' ) ) {
1106
+			remove_filter( 'gform_validation', array( GF_User_Registration::get_instance(), 'validate' ) );
1107
+		} else  if ( class_exists( 'GFUser' ) ) {
1108
+			remove_filter( 'gform_validation', array( 'GFUser', 'user_registration_validation' ) );
1109
+		}
1134 1110
 
1135 1111
 
1136
-    /**
1137
-     * Make validation work for Edit Entry
1138
-     *
1139
-     * Because we're calling the GFFormDisplay::validate() in an unusual way (as a front-end
1140
-     * form pretending to be a back-end form), validate() doesn't know we _can't_ edit post
1141
-     * fields. This goes through all the fields and if they're an invalid post field, we
1142
-     * set them as valid. If there are still issues, we'll return false.
1143
-     *
1144
-     * @param  [type] $validation_results [description]
1145
-     * @return [type]                     [description]
1146
-     */
1147
-    function custom_validation( $validation_results ) {
1112
+		/**
1113
+		 * For some crazy reason, Gravity Forms doesn't validate Edit Entry form submissions.
1114
+		 * You can enter whatever you want!
1115
+		 * We try validating, and customize the results using `self::custom_validation()`
1116
+		 */
1117
+		add_filter( 'gform_validation_'. $this->form_id, array( $this, 'custom_validation' ), 10, 4);
1148 1118
 
1149
-        do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Validation results: ', $validation_results );
1119
+		// Needed by the validate funtion
1120
+		$failed_validation_page = NULL;
1121
+		$field_values = RGForms::post( 'gform_field_values' );
1150 1122
 
1151
-        do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] $_POSTed data (sanitized): ', esc_html( print_r( $_POST, true ) ) );
1123
+		// Prevent entry limit from running when editing an entry, also
1124
+		// prevent form scheduling from preventing editing
1125
+		unset( $this->form['limitEntries'], $this->form['scheduleForm'] );
1152 1126
 
1153
-        $gv_valid = true;
1127
+		// Hide fields depending on Edit Entry settings
1128
+		$this->form['fields'] = $this->get_configured_edit_fields( $this->form, $this->view_id );
1154 1129
 
1155
-        foreach ( $validation_results['form']['fields'] as $key => &$field ) {
1130
+		$this->is_valid = GFFormDisplay::validate( $this->form, $field_values, 1, $failed_validation_page );
1156 1131
 
1157
-            $value = RGFormsModel::get_field_value( $field );
1158
-            $field_type = RGFormsModel::get_input_type( $field );
1132
+		remove_filter( 'gform_validation_'. $this->form_id, array( $this, 'custom_validation' ), 10 );
1133
+	}
1159 1134
 
1160
-            // Validate always
1161
-            switch ( $field_type ) {
1162 1135
 
1136
+	/**
1137
+	 * Make validation work for Edit Entry
1138
+	 *
1139
+	 * Because we're calling the GFFormDisplay::validate() in an unusual way (as a front-end
1140
+	 * form pretending to be a back-end form), validate() doesn't know we _can't_ edit post
1141
+	 * fields. This goes through all the fields and if they're an invalid post field, we
1142
+	 * set them as valid. If there are still issues, we'll return false.
1143
+	 *
1144
+	 * @param  [type] $validation_results [description]
1145
+	 * @return [type]                     [description]
1146
+	 */
1147
+	function custom_validation( $validation_results ) {
1163 1148
 
1164
-                case 'fileupload' :
1165
-                case 'post_image':
1149
+		do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Validation results: ', $validation_results );
1166 1150
 
1167
-                    // in case nothing is uploaded but there are already files saved
1168
-                    if( !empty( $field->failed_validation ) && !empty( $field->isRequired ) && !empty( $value ) ) {
1169
-                        $field->failed_validation = false;
1170
-                        unset( $field->validation_message );
1171
-                    }
1151
+		do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] $_POSTed data (sanitized): ', esc_html( print_r( $_POST, true ) ) );
1172 1152
 
1173
-                    // validate if multi file upload reached max number of files [maxFiles] => 2
1174
-                    if( rgobj( $field, 'maxFiles') && rgobj( $field, 'multipleFiles') ) {
1153
+		$gv_valid = true;
1175 1154
 
1176
-                        $input_name = 'input_' . $field->id;
1177
-                        //uploaded
1178
-                        $file_names = isset( GFFormsModel::$uploaded_files[ $validation_results['form']['id'] ][ $input_name ] ) ? GFFormsModel::$uploaded_files[ $validation_results['form']['id'] ][ $input_name ] : array();
1155
+		foreach ( $validation_results['form']['fields'] as $key => &$field ) {
1179 1156
 
1180
-                        //existent
1181
-                        $entry = $this->get_entry();
1182
-                        $value = NULL;
1183
-                        if( isset( $entry[ $field->id ] ) ) {
1184
-                            $value = json_decode( $entry[ $field->id ], true );
1185
-                        }
1157
+			$value = RGFormsModel::get_field_value( $field );
1158
+			$field_type = RGFormsModel::get_input_type( $field );
1186 1159
 
1187
-                        // count uploaded files and existent entry files
1188
-                        $count_files = count( $file_names ) + count( $value );
1160
+			// Validate always
1161
+			switch ( $field_type ) {
1189 1162
 
1190
-                        if( $count_files > $field->maxFiles ) {
1191
-                            $field->validation_message = __( 'Maximum number of files reached', 'gravityview' );
1192
-                            $field->failed_validation = 1;
1193
-                            $gv_valid = false;
1194 1163
 
1195
-                            // in case of error make sure the newest upload files are removed from the upload input
1196
-                            GFFormsModel::$uploaded_files[ $validation_results['form']['id'] ] = null;
1197
-                        }
1164
+				case 'fileupload' :
1165
+				case 'post_image':
1198 1166
 
1199
-                    }
1167
+					// in case nothing is uploaded but there are already files saved
1168
+					if( !empty( $field->failed_validation ) && !empty( $field->isRequired ) && !empty( $value ) ) {
1169
+						$field->failed_validation = false;
1170
+						unset( $field->validation_message );
1171
+					}
1200 1172
 
1173
+					// validate if multi file upload reached max number of files [maxFiles] => 2
1174
+					if( rgobj( $field, 'maxFiles') && rgobj( $field, 'multipleFiles') ) {
1201 1175
 
1202
-                    break;
1176
+						$input_name = 'input_' . $field->id;
1177
+						//uploaded
1178
+						$file_names = isset( GFFormsModel::$uploaded_files[ $validation_results['form']['id'] ][ $input_name ] ) ? GFFormsModel::$uploaded_files[ $validation_results['form']['id'] ][ $input_name ] : array();
1203 1179
 
1204
-            }
1180
+						//existent
1181
+						$entry = $this->get_entry();
1182
+						$value = NULL;
1183
+						if( isset( $entry[ $field->id ] ) ) {
1184
+							$value = json_decode( $entry[ $field->id ], true );
1185
+						}
1205 1186
 
1206
-            // This field has failed validation.
1207
-            if( !empty( $field->failed_validation ) ) {
1187
+						// count uploaded files and existent entry files
1188
+						$count_files = count( $file_names ) + count( $value );
1208 1189
 
1209
-                do_action( 'gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Field is invalid.', array( 'field' => $field, 'value' => $value ) );
1190
+						if( $count_files > $field->maxFiles ) {
1191
+							$field->validation_message = __( 'Maximum number of files reached', 'gravityview' );
1192
+							$field->failed_validation = 1;
1193
+							$gv_valid = false;
1210 1194
 
1211
-                switch ( $field_type ) {
1195
+							// in case of error make sure the newest upload files are removed from the upload input
1196
+							GFFormsModel::$uploaded_files[ $validation_results['form']['id'] ] = null;
1197
+						}
1212 1198
 
1213
-                    // Captchas don't need to be re-entered.
1214
-                    case 'captcha':
1199
+					}
1215 1200
 
1216
-                        // Post Image fields aren't editable, so we un-fail them.
1217
-                    case 'post_image':
1218
-                        $field->failed_validation = false;
1219
-                        unset( $field->validation_message );
1220
-                        break;
1221 1201
 
1222
-                }
1202
+					break;
1223 1203
 
1224
-                // You can't continue inside a switch, so we do it after.
1225
-                if( empty( $field->failed_validation ) ) {
1226
-                    continue;
1227
-                }
1204
+			}
1228 1205
 
1229
-                // checks if the No Duplicates option is not validating entry against itself, since
1230
-                // we're editing a stored entry, it would also assume it's a duplicate.
1231
-                if( !empty( $field->noDuplicates ) ) {
1206
+			// This field has failed validation.
1207
+			if( !empty( $field->failed_validation ) ) {
1232 1208
 
1233
-                    $entry = $this->get_entry();
1209
+				do_action( 'gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Field is invalid.', array( 'field' => $field, 'value' => $value ) );
1234 1210
 
1235
-                    // If the value of the entry is the same as the stored value
1236
-                    // Then we can assume it's not a duplicate, it's the same.
1237
-                    if( !empty( $entry ) && $value == $entry[ $field->id ] ) {
1238
-                        //if value submitted was not changed, then don't validate
1239
-                        $field->failed_validation = false;
1211
+				switch ( $field_type ) {
1240 1212
 
1241
-                        unset( $field->validation_message );
1213
+					// Captchas don't need to be re-entered.
1214
+					case 'captcha':
1242 1215
 
1243
-                        do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Field not a duplicate; it is the same entry.', $entry );
1216
+						// Post Image fields aren't editable, so we un-fail them.
1217
+					case 'post_image':
1218
+						$field->failed_validation = false;
1219
+						unset( $field->validation_message );
1220
+						break;
1244 1221
 
1245
-                        continue;
1246
-                    }
1247
-                }
1222
+				}
1248 1223
 
1249
-                // if here then probably we are facing the validation 'At least one field must be filled out'
1250
-                if( GFFormDisplay::is_empty( $field, $this->form_id  ) && empty( $field->isRequired ) ) {
1251
-                    unset( $field->validation_message );
1252
-	                $field->validation_message = false;
1253
-                    continue;
1254
-                }
1224
+				// You can't continue inside a switch, so we do it after.
1225
+				if( empty( $field->failed_validation ) ) {
1226
+					continue;
1227
+				}
1255 1228
 
1256
-                $gv_valid = false;
1229
+				// checks if the No Duplicates option is not validating entry against itself, since
1230
+				// we're editing a stored entry, it would also assume it's a duplicate.
1231
+				if( !empty( $field->noDuplicates ) ) {
1257 1232
 
1258
-            }
1233
+					$entry = $this->get_entry();
1259 1234
 
1260
-        }
1235
+					// If the value of the entry is the same as the stored value
1236
+					// Then we can assume it's not a duplicate, it's the same.
1237
+					if( !empty( $entry ) && $value == $entry[ $field->id ] ) {
1238
+						//if value submitted was not changed, then don't validate
1239
+						$field->failed_validation = false;
1261 1240
 
1262
-        $validation_results['is_valid'] = $gv_valid;
1241
+						unset( $field->validation_message );
1263 1242
 
1264
-        do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Validation results.', $validation_results );
1243
+						do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Field not a duplicate; it is the same entry.', $entry );
1265 1244
 
1266
-        // We'll need this result when rendering the form ( on GFFormDisplay::get_form )
1267
-        $this->form_after_validation = $validation_results['form'];
1245
+						continue;
1246
+					}
1247
+				}
1268 1248
 
1269
-        return $validation_results;
1270
-    }
1249
+				// if here then probably we are facing the validation 'At least one field must be filled out'
1250
+				if( GFFormDisplay::is_empty( $field, $this->form_id  ) && empty( $field->isRequired ) ) {
1251
+					unset( $field->validation_message );
1252
+					$field->validation_message = false;
1253
+					continue;
1254
+				}
1271 1255
 
1256
+				$gv_valid = false;
1272 1257
 
1273
-    /**
1274
-     * TODO: This seems to be hacky... we should remove it. Entry is set when updating the form using setup_vars()!
1275
-     * Get the current entry and set it if it's not yet set.
1276
-     * @return array Gravity Forms entry array
1277
-     */
1278
-    private function get_entry() {
1258
+			}
1279 1259
 
1280
-        if( empty( $this->entry ) ) {
1281
-            // Get the database value of the entry that's being edited
1282
-            $this->entry = gravityview_get_entry( GravityView_frontend::is_single_entry() );
1283
-        }
1260
+		}
1261
+
1262
+		$validation_results['is_valid'] = $gv_valid;
1263
+
1264
+		do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Validation results.', $validation_results );
1284 1265
 
1285
-        return $this->entry;
1286
-    }
1266
+		// We'll need this result when rendering the form ( on GFFormDisplay::get_form )
1267
+		$this->form_after_validation = $validation_results['form'];
1287 1268
 
1269
+		return $validation_results;
1270
+	}
1288 1271
 
1289 1272
 
1290
-    // --- Filters
1273
+	/**
1274
+	 * TODO: This seems to be hacky... we should remove it. Entry is set when updating the form using setup_vars()!
1275
+	 * Get the current entry and set it if it's not yet set.
1276
+	 * @return array Gravity Forms entry array
1277
+	 */
1278
+	private function get_entry() {
1291 1279
 
1292
-    /**
1293
-     * Get the Edit Entry fields as configured in the View
1294
-     *
1295
-     * @since 1.8
1296
-     *
1297
-     * @param int $view_id
1298
-     *
1299
-     * @return array Array of fields that are configured in the Edit tab in the Admin
1300
-     */
1301
-    private function get_configured_edit_fields( $form, $view_id ) {
1280
+		if( empty( $this->entry ) ) {
1281
+			// Get the database value of the entry that's being edited
1282
+			$this->entry = gravityview_get_entry( GravityView_frontend::is_single_entry() );
1283
+		}
1284
+
1285
+		return $this->entry;
1286
+	}
1287
+
1288
+
1289
+
1290
+	// --- Filters
1291
+
1292
+	/**
1293
+	 * Get the Edit Entry fields as configured in the View
1294
+	 *
1295
+	 * @since 1.8
1296
+	 *
1297
+	 * @param int $view_id
1298
+	 *
1299
+	 * @return array Array of fields that are configured in the Edit tab in the Admin
1300
+	 */
1301
+	private function get_configured_edit_fields( $form, $view_id ) {
1302 1302
 
1303
-        // Get all fields for form
1304
-        $properties = GravityView_View_Data::getInstance()->get_fields( $view_id );
1303
+		// Get all fields for form
1304
+		$properties = GravityView_View_Data::getInstance()->get_fields( $view_id );
1305 1305
 
1306
-        // If edit tab not yet configured, show all fields
1307
-        $edit_fields = !empty( $properties['edit_edit-fields'] ) ? $properties['edit_edit-fields'] : NULL;
1306
+		// If edit tab not yet configured, show all fields
1307
+		$edit_fields = !empty( $properties['edit_edit-fields'] ) ? $properties['edit_edit-fields'] : NULL;
1308 1308
 
1309
-	    // Show hidden fields as text fields
1310
-	    $form = $this->fix_hidden_fields( $form );
1309
+		// Show hidden fields as text fields
1310
+		$form = $this->fix_hidden_fields( $form );
1311 1311
 
1312
-        // Hide fields depending on admin settings
1313
-        $fields = $this->filter_fields( $form['fields'], $edit_fields );
1312
+		// Hide fields depending on admin settings
1313
+		$fields = $this->filter_fields( $form['fields'], $edit_fields );
1314 1314
 
1315
-	    // If Edit Entry fields are configured, remove adminOnly field settings. Otherwise, don't.
1316
-	    $fields = $this->filter_admin_only_fields( $fields, $edit_fields, $form, $view_id );
1315
+		// If Edit Entry fields are configured, remove adminOnly field settings. Otherwise, don't.
1316
+		$fields = $this->filter_admin_only_fields( $fields, $edit_fields, $form, $view_id );
1317 1317
 
1318
-        return $fields;
1319
-    }
1318
+		return $fields;
1319
+	}
1320 1320
 
1321 1321
 	/**
1322 1322
 	 * @since 1.9.2
@@ -1340,376 +1340,376 @@  discard block
 block discarded – undo
1340 1340
 	}
1341 1341
 
1342 1342
 
1343
-    /**
1344
-     * Filter area fields based on specified conditions
1345
-     *  - This filter removes the fields that have calculation configured
1346
-     *
1347
-     * @uses GravityView_Edit_Entry::user_can_edit_field() Check caps
1348
-     * @access private
1349
-     * @param GF_Field[] $fields
1350
-     * @param array $configured_fields
1351
-     * @since  1.5
1352
-     * @return array $fields
1353
-     */
1354
-    private function filter_fields( $fields, $configured_fields ) {
1355
-
1356
-        if( empty( $fields ) || !is_array( $fields ) ) {
1357
-            return $fields;
1358
-        }
1359
-
1360
-        $edit_fields = array();
1361
-
1362
-        $field_type_blacklist = array(
1363
-            'page',
1364
-        );
1365
-
1366
-	    /**
1367
-	     * @filter `gravityview/edit_entry/hide-product-fields` Hide product fields from being editable.
1368
-	     * @since 1.9.1
1369
-         * @param boolean $hide_product_fields Whether to hide product fields in the editor.  Default: false
1370
-	     */
1371
-	    $hide_product_fields = apply_filters( 'gravityview/edit_entry/hide-product-fields', empty( self::$supports_product_fields ) );
1372
-
1373
-	    if( $hide_product_fields ) {
1374
-		    $field_type_blacklist[] = 'option';
1375
-		    $field_type_blacklist[] = 'quantity';
1376
-            $field_type_blacklist[] = 'product';
1377
-            $field_type_blacklist[] = 'total';
1378
-            $field_type_blacklist[] = 'shipping';
1379
-            $field_type_blacklist[] = 'calculation';
1380
-	    }
1381
-
1382
-        // First, remove blacklist or calculation fields
1383
-        foreach ( $fields as $key => $field ) {
1384
-
1385
-            // Remove the fields that have calculation properties
1386
-            // @since 1.16.2
1387
-            if( $field->has_calculation() ) {
1388
-                unset( $fields[ $key ] );
1389
-            }
1390
-
1391
-            if( in_array( $field->type, $field_type_blacklist ) ) {
1392
-                unset( $fields[ $key ] );
1393
-            }
1394
-        }
1395
-
1396
-        // The Edit tab has not been configured, so we return all fields by default.
1397
-        if( empty( $configured_fields ) ) {
1398
-            return $fields;
1399
-        }
1400
-
1401
-        // The edit tab has been configured, so we loop through to configured settings
1402
-        foreach ( $configured_fields as $configured_field ) {
1403
-
1404
-	        /** @var GF_Field $field */
1405
-	        foreach ( $fields as $field ) {
1406
-
1407
-                if( intval( $configured_field['id'] ) === intval( $field->id ) && $this->user_can_edit_field( $configured_field, false ) ) {
1408
-                    $edit_fields[] = $this->merge_field_properties( $field, $configured_field );
1409
-                    break;
1410
-                }
1411
-
1412
-            }
1413
-
1414
-        }
1415
-
1416
-        return $edit_fields;
1417
-
1418
-    }
1419
-
1420
-    /**
1421
-     * Override GF Form field properties with the ones defined on the View
1422
-     * @param  GF_Field $field GF Form field object
1423
-     * @param  array $setting  GV field options
1424
-     * @since  1.5
1425
-     * @return array
1426
-     */
1427
-    private function merge_field_properties( $field, $field_setting ) {
1428
-
1429
-        $return_field = $field;
1430
-
1431
-        if( empty( $field_setting['show_label'] ) ) {
1432
-            $return_field->label = '';
1433
-        } elseif ( !empty( $field_setting['custom_label'] ) ) {
1434
-            $return_field->label = $field_setting['custom_label'];
1435
-        }
1436
-
1437
-        if( !empty( $field_setting['custom_class'] ) ) {
1438
-            $return_field->cssClass .= ' '. gravityview_sanitize_html_class( $field_setting['custom_class'] );
1439
-        }
1440
-
1441
-        /**
1442
-         * Normalize page numbers - avoid conflicts with page validation
1443
-         * @since 1.6
1444
-         */
1445
-        $return_field->pageNumber = 1;
1446
-
1447
-        return $return_field;
1448
-
1449
-    }
1450
-
1451
-    /**
1452
-     * Remove fields that shouldn't be visible based on the Gravity Forms adminOnly field property
1453
-     *
1454
-     * @since 1.9.1
1455
-     *
1456
-     * @param array|GF_Field[] $fields Gravity Forms form fields
1457
-     * @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration
1458
-     * @param array $form GF Form array
1459
-     * @param int $view_id View ID
1460
-     *
1461
-     * @return array Possibly modified form array
1462
-     */
1463
-    function filter_admin_only_fields( $fields = array(), $edit_fields = null, $form = array(), $view_id = 0 ) {
1464
-
1465
-	    /**
1466
-         * @filter `gravityview/edit_entry/use_gf_admin_only_setting` When Edit tab isn't configured, should the Gravity Forms "Admin Only" field settings be used to control field display to non-admins? Default: true
1467
-	     * If the Edit Entry tab is not configured, adminOnly fields will not be shown to non-administrators.
1468
-	     * If the Edit Entry tab *is* configured, adminOnly fields will be shown to non-administrators, using the configured GV permissions
1469
-	     * @since 1.9.1
1470
-	     * @param boolean $use_gf_adminonly_setting True: Hide field if set to Admin Only in GF and the user is not an admin. False: show field based on GV permissions, ignoring GF permissions.
1471
-	     * @param array $form GF Form array
1472
-	     * @param int $view_id View ID
1473
-	     */
1474
-	    $use_gf_adminonly_setting = apply_filters( 'gravityview/edit_entry/use_gf_admin_only_setting', empty( $edit_fields ), $form, $view_id );
1475
-
1476
-	    if( $use_gf_adminonly_setting && false === GVCommon::has_cap( 'gravityforms_edit_entries', $this->entry['id'] ) ) {
1477
-            foreach( $fields as $k => $field ) {
1478
-                if( $field->adminOnly ) {
1479
-                    unset( $fields[ $k ] );
1480
-                }
1481
-            }
1482
-            return $fields;
1483
-        }
1484
-
1485
-	    foreach( $fields as &$field ) {
1486
-		    $field->adminOnly = false;
1487
-        }
1488
-
1489
-        return $fields;
1490
-    }
1491
-
1492
-    // --- Conditional Logic
1493
-
1494
-    /**
1495
-     * Remove the conditional logic rules from the form button and the form fields, if needed.
1496
-     *
1497
-     * @since 1.9
1498
-     *
1499
-     * @param array $form Gravity Forms form
1500
-     * @return array Modified form, if not using Conditional Logic
1501
-     */
1502
-    function filter_conditional_logic( $form ) {
1503
-
1504
-        /**
1505
-         * @filter `gravityview/edit_entry/conditional_logic` Should the Edit Entry form use Gravity Forms conditional logic showing/hiding of fields?
1506
-         * @since 1.9
1507
-         * @param bool $use_conditional_logic True: Gravity Forms will show/hide fields just like in the original form; False: conditional logic will be disabled and fields will be shown based on configuration. Default: true
1508
-         * @param array $form Gravity Forms form
1509
-         */
1510
-        $use_conditional_logic = apply_filters( 'gravityview/edit_entry/conditional_logic', true, $form );
1511
-
1512
-        if( $use_conditional_logic ) {
1513
-            return $form;
1514
-        }
1515
-
1516
-        foreach( $form['fields'] as &$field ) {
1517
-            /* @var GF_Field $field */
1518
-            $field->conditionalLogic = null;
1519
-        }
1520
-
1521
-        unset( $form['button']['conditionalLogic'] );
1522
-
1523
-        return $form;
1524
-
1525
-    }
1526
-
1527
-    /**
1528
-     * Disable the Gravity Forms conditional logic script and features on the Edit Entry screen
1529
-     *
1530
-     * @since 1.9
1531
-     *
1532
-     * @param $has_conditional_logic
1533
-     * @param $form
1534
-     * @return mixed|void
1535
-     */
1536
-    function manage_conditional_logic( $has_conditional_logic, $form ) {
1537
-
1538
-        if( ! $this->is_edit_entry() ) {
1539
-            return $has_conditional_logic;
1540
-        }
1541
-
1542
-        return apply_filters( 'gravityview/edit_entry/conditional_logic', $has_conditional_logic, $form );
1543
-    }
1544
-
1545
-
1546
-    // --- User checks and nonces
1547
-
1548
-    /**
1549
-     * Check if the user can edit the entry
1550
-     *
1551
-     * - Is the nonce valid?
1552
-     * - Does the user have the right caps for the entry
1553
-     * - Is the entry in the trash?
1554
-     *
1555
-     * @todo Move to GVCommon
1556
-     *
1557
-     * @param  boolean $echo Show error messages in the form?
1558
-     * @return boolean        True: can edit form. False: nope.
1559
-     */
1560
-    function user_can_edit_entry( $echo = false ) {
1561
-
1562
-        $error = NULL;
1563
-
1564
-        /**
1565
-         *  1. Permalinks are turned off
1566
-         *  2. There are two entries embedded using oEmbed
1567
-         *  3. One of the entries has just been saved
1568
-         */
1569
-        if( !empty( $_POST['lid'] ) && !empty( $_GET['entry'] ) && ( $_POST['lid'] !== $_GET['entry'] ) ) {
1570
-
1571
-            $error = true;
1572
-
1573
-        }
1574
-
1575
-        if( !empty( $_GET['entry'] ) && (string)$this->entry['id'] !== $_GET['entry'] ) {
1576
-
1577
-            $error = true;
1578
-
1579
-        } elseif( ! $this->verify_nonce() ) {
1580
-
1581
-            /**
1582
-             * If the Entry is embedded, there may be two entries on the same page.
1583
-             * If that's the case, and one is being edited, the other should fail gracefully and not display an error.
1584
-             */
1585
-            if( GravityView_oEmbed::getInstance()->get_entry_id() ) {
1586
-                $error = true;
1587
-            } else {
1588
-                $error = __( 'The link to edit this entry is not valid; it may have expired.', 'gravityview');
1589
-            }
1343
+	/**
1344
+	 * Filter area fields based on specified conditions
1345
+	 *  - This filter removes the fields that have calculation configured
1346
+	 *
1347
+	 * @uses GravityView_Edit_Entry::user_can_edit_field() Check caps
1348
+	 * @access private
1349
+	 * @param GF_Field[] $fields
1350
+	 * @param array $configured_fields
1351
+	 * @since  1.5
1352
+	 * @return array $fields
1353
+	 */
1354
+	private function filter_fields( $fields, $configured_fields ) {
1590 1355
 
1591
-        }
1592
-
1593
-        if( ! GravityView_Edit_Entry::check_user_cap_edit_entry( $this->entry ) ) {
1594
-            $error = __( 'You do not have permission to edit this entry.', 'gravityview');
1595
-        }
1596
-
1597
-        if( $this->entry['status'] === 'trash' ) {
1598
-            $error = __('You cannot edit the entry; it is in the trash.', 'gravityview' );
1599
-        }
1356
+		if( empty( $fields ) || !is_array( $fields ) ) {
1357
+			return $fields;
1358
+		}
1600 1359
 
1601
-        // No errors; everything's fine here!
1602
-        if( empty( $error ) ) {
1603
-            return true;
1604
-        }
1360
+		$edit_fields = array();
1361
+
1362
+		$field_type_blacklist = array(
1363
+			'page',
1364
+		);
1365
+
1366
+		/**
1367
+		 * @filter `gravityview/edit_entry/hide-product-fields` Hide product fields from being editable.
1368
+		 * @since 1.9.1
1369
+		 * @param boolean $hide_product_fields Whether to hide product fields in the editor.  Default: false
1370
+		 */
1371
+		$hide_product_fields = apply_filters( 'gravityview/edit_entry/hide-product-fields', empty( self::$supports_product_fields ) );
1372
+
1373
+		if( $hide_product_fields ) {
1374
+			$field_type_blacklist[] = 'option';
1375
+			$field_type_blacklist[] = 'quantity';
1376
+			$field_type_blacklist[] = 'product';
1377
+			$field_type_blacklist[] = 'total';
1378
+			$field_type_blacklist[] = 'shipping';
1379
+			$field_type_blacklist[] = 'calculation';
1380
+		}
1605 1381
 
1606
-        if( $echo && $error !== true ) {
1382
+		// First, remove blacklist or calculation fields
1383
+		foreach ( $fields as $key => $field ) {
1607 1384
 
1608
-	        $error = esc_html( $error );
1385
+			// Remove the fields that have calculation properties
1386
+			// @since 1.16.2
1387
+			if( $field->has_calculation() ) {
1388
+				unset( $fields[ $key ] );
1389
+			}
1609 1390
 
1610
-	        /**
1611
-	         * @since 1.9
1612
-	         */
1613
-	        if ( ! empty( $this->entry ) ) {
1614
-		        $error .= ' ' . gravityview_get_link( '#', _x('Go back.', 'Link shown when invalid Edit Entry link is clicked', 'gravityview' ), array( 'onclick' => "window.history.go(-1); return false;" ) );
1615
-	        }
1391
+			if( in_array( $field->type, $field_type_blacklist ) ) {
1392
+				unset( $fields[ $key ] );
1393
+			}
1394
+		}
1616 1395
 
1617
-            echo GVCommon::generate_notice( wpautop( $error ), 'gv-error error');
1618
-        }
1396
+		// The Edit tab has not been configured, so we return all fields by default.
1397
+		if( empty( $configured_fields ) ) {
1398
+			return $fields;
1399
+		}
1619 1400
 
1620
-        do_action('gravityview_log_error', 'GravityView_Edit_Entry[user_can_edit_entry]' . $error );
1401
+		// The edit tab has been configured, so we loop through to configured settings
1402
+		foreach ( $configured_fields as $configured_field ) {
1621 1403
 
1622
-        return false;
1623
-    }
1404
+			/** @var GF_Field $field */
1405
+			foreach ( $fields as $field ) {
1624 1406
 
1407
+				if( intval( $configured_field['id'] ) === intval( $field->id ) && $this->user_can_edit_field( $configured_field, false ) ) {
1408
+					$edit_fields[] = $this->merge_field_properties( $field, $configured_field );
1409
+					break;
1410
+				}
1625 1411
 
1626
-    /**
1627
-     * Check whether a field is editable by the current user, and optionally display an error message
1628
-     * @uses  GravityView_Edit_Entry->check_user_cap_edit_field() Check user capabilities
1629
-     * @param  array  $field Field or field settings array
1630
-     * @param  boolean $echo  Whether to show error message telling user they aren't allowed
1631
-     * @return boolean         True: user can edit the current field; False: nope, they can't.
1632
-     */
1633
-    private function user_can_edit_field( $field, $echo = false ) {
1634
-
1635
-        $error = NULL;
1412
+			}
1636 1413
 
1637
-        if( ! $this->check_user_cap_edit_field( $field ) ) {
1638
-            $error = __( 'You do not have permission to edit this field.', 'gravityview');
1639
-        }
1414
+		}
1640 1415
 
1641
-        // No errors; everything's fine here!
1642
-        if( empty( $error ) ) {
1643
-            return true;
1644
-        }
1416
+		return $edit_fields;
1645 1417
 
1646
-        if( $echo ) {
1647
-            echo GVCommon::generate_notice( wpautop( esc_html( $error ) ), 'gv-error error');
1648
-        }
1649
-
1650
-        do_action('gravityview_log_error', 'GravityView_Edit_Entry[user_can_edit_field]' . $error );
1418
+	}
1651 1419
 
1652
-        return false;
1420
+	/**
1421
+	 * Override GF Form field properties with the ones defined on the View
1422
+	 * @param  GF_Field $field GF Form field object
1423
+	 * @param  array $setting  GV field options
1424
+	 * @since  1.5
1425
+	 * @return array
1426
+	 */
1427
+	private function merge_field_properties( $field, $field_setting ) {
1653 1428
 
1654
-    }
1429
+		$return_field = $field;
1655 1430
 
1431
+		if( empty( $field_setting['show_label'] ) ) {
1432
+			$return_field->label = '';
1433
+		} elseif ( !empty( $field_setting['custom_label'] ) ) {
1434
+			$return_field->label = $field_setting['custom_label'];
1435
+		}
1656 1436
 
1657
-    /**
1658
-     * checks if user has permissions to edit a specific field
1659
-     *
1660
-     * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_field for maximum security!!
1661
-     *
1662
-     * @param  [type] $field [description]
1663
-     * @return bool
1664
-     */
1665
-    private function check_user_cap_edit_field( $field ) {
1437
+		if( !empty( $field_setting['custom_class'] ) ) {
1438
+			$return_field->cssClass .= ' '. gravityview_sanitize_html_class( $field_setting['custom_class'] );
1439
+		}
1666 1440
 
1667
-        // If they can edit any entries (as defined in Gravity Forms), we're good.
1668
-        if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ) ) ) {
1669
-            return true;
1670
-        }
1441
+		/**
1442
+		 * Normalize page numbers - avoid conflicts with page validation
1443
+		 * @since 1.6
1444
+		 */
1445
+		$return_field->pageNumber = 1;
1671 1446
 
1672
-        $field_cap = isset( $field['allow_edit_cap'] ) ? $field['allow_edit_cap'] : false;
1447
+		return $return_field;
1673 1448
 
1674
-        // If the field has custom editing capaibilities set, check those
1675
-        if( $field_cap ) {
1676
-            return GVCommon::has_cap( $field['allow_edit_cap'] );
1677
-        }
1678
-
1679
-        return false;
1680
-    }
1449
+	}
1681 1450
 
1451
+	/**
1452
+	 * Remove fields that shouldn't be visible based on the Gravity Forms adminOnly field property
1453
+	 *
1454
+	 * @since 1.9.1
1455
+	 *
1456
+	 * @param array|GF_Field[] $fields Gravity Forms form fields
1457
+	 * @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration
1458
+	 * @param array $form GF Form array
1459
+	 * @param int $view_id View ID
1460
+	 *
1461
+	 * @return array Possibly modified form array
1462
+	 */
1463
+	function filter_admin_only_fields( $fields = array(), $edit_fields = null, $form = array(), $view_id = 0 ) {
1464
+
1465
+		/**
1466
+		 * @filter `gravityview/edit_entry/use_gf_admin_only_setting` When Edit tab isn't configured, should the Gravity Forms "Admin Only" field settings be used to control field display to non-admins? Default: true
1467
+		 * If the Edit Entry tab is not configured, adminOnly fields will not be shown to non-administrators.
1468
+		 * If the Edit Entry tab *is* configured, adminOnly fields will be shown to non-administrators, using the configured GV permissions
1469
+		 * @since 1.9.1
1470
+		 * @param boolean $use_gf_adminonly_setting True: Hide field if set to Admin Only in GF and the user is not an admin. False: show field based on GV permissions, ignoring GF permissions.
1471
+		 * @param array $form GF Form array
1472
+		 * @param int $view_id View ID
1473
+		 */
1474
+		$use_gf_adminonly_setting = apply_filters( 'gravityview/edit_entry/use_gf_admin_only_setting', empty( $edit_fields ), $form, $view_id );
1475
+
1476
+		if( $use_gf_adminonly_setting && false === GVCommon::has_cap( 'gravityforms_edit_entries', $this->entry['id'] ) ) {
1477
+			foreach( $fields as $k => $field ) {
1478
+				if( $field->adminOnly ) {
1479
+					unset( $fields[ $k ] );
1480
+				}
1481
+			}
1482
+			return $fields;
1483
+		}
1682 1484
 
1683
-    /**
1684
-     * Is the current nonce valid for editing the entry?
1685
-     * @return boolean
1686
-     */
1687
-    public function verify_nonce() {
1485
+		foreach( $fields as &$field ) {
1486
+			$field->adminOnly = false;
1487
+		}
1488
+
1489
+		return $fields;
1490
+	}
1491
+
1492
+	// --- Conditional Logic
1493
+
1494
+	/**
1495
+	 * Remove the conditional logic rules from the form button and the form fields, if needed.
1496
+	 *
1497
+	 * @since 1.9
1498
+	 *
1499
+	 * @param array $form Gravity Forms form
1500
+	 * @return array Modified form, if not using Conditional Logic
1501
+	 */
1502
+	function filter_conditional_logic( $form ) {
1503
+
1504
+		/**
1505
+		 * @filter `gravityview/edit_entry/conditional_logic` Should the Edit Entry form use Gravity Forms conditional logic showing/hiding of fields?
1506
+		 * @since 1.9
1507
+		 * @param bool $use_conditional_logic True: Gravity Forms will show/hide fields just like in the original form; False: conditional logic will be disabled and fields will be shown based on configuration. Default: true
1508
+		 * @param array $form Gravity Forms form
1509
+		 */
1510
+		$use_conditional_logic = apply_filters( 'gravityview/edit_entry/conditional_logic', true, $form );
1511
+
1512
+		if( $use_conditional_logic ) {
1513
+			return $form;
1514
+		}
1515
+
1516
+		foreach( $form['fields'] as &$field ) {
1517
+			/* @var GF_Field $field */
1518
+			$field->conditionalLogic = null;
1519
+		}
1520
+
1521
+		unset( $form['button']['conditionalLogic'] );
1522
+
1523
+		return $form;
1524
+
1525
+	}
1526
+
1527
+	/**
1528
+	 * Disable the Gravity Forms conditional logic script and features on the Edit Entry screen
1529
+	 *
1530
+	 * @since 1.9
1531
+	 *
1532
+	 * @param $has_conditional_logic
1533
+	 * @param $form
1534
+	 * @return mixed|void
1535
+	 */
1536
+	function manage_conditional_logic( $has_conditional_logic, $form ) {
1537
+
1538
+		if( ! $this->is_edit_entry() ) {
1539
+			return $has_conditional_logic;
1540
+		}
1541
+
1542
+		return apply_filters( 'gravityview/edit_entry/conditional_logic', $has_conditional_logic, $form );
1543
+	}
1544
+
1545
+
1546
+	// --- User checks and nonces
1547
+
1548
+	/**
1549
+	 * Check if the user can edit the entry
1550
+	 *
1551
+	 * - Is the nonce valid?
1552
+	 * - Does the user have the right caps for the entry
1553
+	 * - Is the entry in the trash?
1554
+	 *
1555
+	 * @todo Move to GVCommon
1556
+	 *
1557
+	 * @param  boolean $echo Show error messages in the form?
1558
+	 * @return boolean        True: can edit form. False: nope.
1559
+	 */
1560
+	function user_can_edit_entry( $echo = false ) {
1561
+
1562
+		$error = NULL;
1563
+
1564
+		/**
1565
+		 *  1. Permalinks are turned off
1566
+		 *  2. There are two entries embedded using oEmbed
1567
+		 *  3. One of the entries has just been saved
1568
+		 */
1569
+		if( !empty( $_POST['lid'] ) && !empty( $_GET['entry'] ) && ( $_POST['lid'] !== $_GET['entry'] ) ) {
1570
+
1571
+			$error = true;
1572
+
1573
+		}
1574
+
1575
+		if( !empty( $_GET['entry'] ) && (string)$this->entry['id'] !== $_GET['entry'] ) {
1576
+
1577
+			$error = true;
1578
+
1579
+		} elseif( ! $this->verify_nonce() ) {
1580
+
1581
+			/**
1582
+			 * If the Entry is embedded, there may be two entries on the same page.
1583
+			 * If that's the case, and one is being edited, the other should fail gracefully and not display an error.
1584
+			 */
1585
+			if( GravityView_oEmbed::getInstance()->get_entry_id() ) {
1586
+				$error = true;
1587
+			} else {
1588
+				$error = __( 'The link to edit this entry is not valid; it may have expired.', 'gravityview');
1589
+			}
1590
+
1591
+		}
1592
+
1593
+		if( ! GravityView_Edit_Entry::check_user_cap_edit_entry( $this->entry ) ) {
1594
+			$error = __( 'You do not have permission to edit this entry.', 'gravityview');
1595
+		}
1688 1596
 
1689
-        // Verify form submitted for editing single
1690
-        if( $this->is_edit_entry_submission() ) {
1691
-            $valid = wp_verify_nonce( $_POST[ self::$nonce_field ], self::$nonce_field );
1692
-        }
1597
+		if( $this->entry['status'] === 'trash' ) {
1598
+			$error = __('You cannot edit the entry; it is in the trash.', 'gravityview' );
1599
+		}
1600
+
1601
+		// No errors; everything's fine here!
1602
+		if( empty( $error ) ) {
1603
+			return true;
1604
+		}
1605
+
1606
+		if( $echo && $error !== true ) {
1607
+
1608
+			$error = esc_html( $error );
1609
+
1610
+			/**
1611
+			 * @since 1.9
1612
+			 */
1613
+			if ( ! empty( $this->entry ) ) {
1614
+				$error .= ' ' . gravityview_get_link( '#', _x('Go back.', 'Link shown when invalid Edit Entry link is clicked', 'gravityview' ), array( 'onclick' => "window.history.go(-1); return false;" ) );
1615
+			}
1616
+
1617
+			echo GVCommon::generate_notice( wpautop( $error ), 'gv-error error');
1618
+		}
1619
+
1620
+		do_action('gravityview_log_error', 'GravityView_Edit_Entry[user_can_edit_entry]' . $error );
1621
+
1622
+		return false;
1623
+	}
1624
+
1625
+
1626
+	/**
1627
+	 * Check whether a field is editable by the current user, and optionally display an error message
1628
+	 * @uses  GravityView_Edit_Entry->check_user_cap_edit_field() Check user capabilities
1629
+	 * @param  array  $field Field or field settings array
1630
+	 * @param  boolean $echo  Whether to show error message telling user they aren't allowed
1631
+	 * @return boolean         True: user can edit the current field; False: nope, they can't.
1632
+	 */
1633
+	private function user_can_edit_field( $field, $echo = false ) {
1634
+
1635
+		$error = NULL;
1636
+
1637
+		if( ! $this->check_user_cap_edit_field( $field ) ) {
1638
+			$error = __( 'You do not have permission to edit this field.', 'gravityview');
1639
+		}
1640
+
1641
+		// No errors; everything's fine here!
1642
+		if( empty( $error ) ) {
1643
+			return true;
1644
+		}
1645
+
1646
+		if( $echo ) {
1647
+			echo GVCommon::generate_notice( wpautop( esc_html( $error ) ), 'gv-error error');
1648
+		}
1649
+
1650
+		do_action('gravityview_log_error', 'GravityView_Edit_Entry[user_can_edit_field]' . $error );
1651
+
1652
+		return false;
1693 1653
 
1694
-        // Verify
1695
-        else if( ! $this->is_edit_entry() ) {
1696
-            $valid = false;
1697
-        }
1654
+	}
1655
+
1656
+
1657
+	/**
1658
+	 * checks if user has permissions to edit a specific field
1659
+	 *
1660
+	 * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_field for maximum security!!
1661
+	 *
1662
+	 * @param  [type] $field [description]
1663
+	 * @return bool
1664
+	 */
1665
+	private function check_user_cap_edit_field( $field ) {
1698 1666
 
1699
-        else {
1700
-            $valid = wp_verify_nonce( $_GET['edit'], self::$nonce_key );
1701
-        }
1667
+		// If they can edit any entries (as defined in Gravity Forms), we're good.
1668
+		if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ) ) ) {
1669
+			return true;
1670
+		}
1671
+
1672
+		$field_cap = isset( $field['allow_edit_cap'] ) ? $field['allow_edit_cap'] : false;
1673
+
1674
+		// If the field has custom editing capaibilities set, check those
1675
+		if( $field_cap ) {
1676
+			return GVCommon::has_cap( $field['allow_edit_cap'] );
1677
+		}
1678
+
1679
+		return false;
1680
+	}
1702 1681
 
1703
-        /**
1704
-         * @filter `gravityview/edit_entry/verify_nonce` Override Edit Entry nonce validation. Return true to declare nonce valid.
1705
-         * @since 1.13
1706
-         * @param int|boolean $valid False if invalid; 1 or 2 when nonce was generated
1707
-         * @param string $nonce_field Key used when validating submissions. Default: is_gv_edit_entry
1708
-         */
1709
-        $valid = apply_filters( 'gravityview/edit_entry/verify_nonce', $valid, self::$nonce_field );
1710 1682
 
1711
-        return $valid;
1712
-    }
1683
+	/**
1684
+	 * Is the current nonce valid for editing the entry?
1685
+	 * @return boolean
1686
+	 */
1687
+	public function verify_nonce() {
1688
+
1689
+		// Verify form submitted for editing single
1690
+		if( $this->is_edit_entry_submission() ) {
1691
+			$valid = wp_verify_nonce( $_POST[ self::$nonce_field ], self::$nonce_field );
1692
+		}
1693
+
1694
+		// Verify
1695
+		else if( ! $this->is_edit_entry() ) {
1696
+			$valid = false;
1697
+		}
1698
+
1699
+		else {
1700
+			$valid = wp_verify_nonce( $_GET['edit'], self::$nonce_key );
1701
+		}
1702
+
1703
+		/**
1704
+		 * @filter `gravityview/edit_entry/verify_nonce` Override Edit Entry nonce validation. Return true to declare nonce valid.
1705
+		 * @since 1.13
1706
+		 * @param int|boolean $valid False if invalid; 1 or 2 when nonce was generated
1707
+		 * @param string $nonce_field Key used when validating submissions. Default: is_gv_edit_entry
1708
+		 */
1709
+		$valid = apply_filters( 'gravityview/edit_entry/verify_nonce', $valid, self::$nonce_field );
1710
+
1711
+		return $valid;
1712
+	}
1713 1713
 
1714 1714
 
1715 1715
 
Please login to merge, or discard this patch.