Completed
Push — develop ( ad3c06...5160a1 )
by Zack
06:19
created
includes/extensions/edit-entry/class-edit-entry.php 1 patch
Indentation   +156 added lines, -156 removed lines patch added patch discarded remove patch
@@ -18,83 +18,83 @@  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
 		$this->load_components( 'locking' );
44 44
 
45
-        $this->load_components( 'render' );
45
+		$this->load_components( 'render' );
46 46
 
47
-        // If GF User Registration Add-on exists
48
-        $this->load_components( 'user-registration' );
47
+		// If GF User Registration Add-on exists
48
+		$this->load_components( 'user-registration' );
49 49
 
50
-        $this->add_hooks();
50
+		$this->add_hooks();
51 51
 
52 52
 		// Process hooks for addons that may or may not be present
53 53
 		$this->addon_specific_hooks();
54 54
 	}
55 55
 
56 56
 
57
-    static function getInstance() {
57
+	static function getInstance() {
58 58
 
59
-        if( empty( self::$instance ) ) {
60
-            self::$instance = new GravityView_Edit_Entry;
61
-        }
59
+		if( empty( self::$instance ) ) {
60
+			self::$instance = new GravityView_Edit_Entry;
61
+		}
62 62
 
63
-        return self::$instance;
64
-    }
63
+		return self::$instance;
64
+	}
65 65
 
66 66
 
67
-    private function load_components( $component ) {
67
+	private function load_components( $component ) {
68 68
 
69
-        $dir = trailingslashit( self::$file );
69
+		$dir = trailingslashit( self::$file );
70 70
 
71
-        $filename  = $dir . 'class-edit-entry-' . $component . '.php';
72
-        $classname = 'GravityView_Edit_Entry_' . str_replace( ' ', '_', ucwords( str_replace( '-', ' ', $component ) ) );
71
+		$filename  = $dir . 'class-edit-entry-' . $component . '.php';
72
+		$classname = 'GravityView_Edit_Entry_' . str_replace( ' ', '_', ucwords( str_replace( '-', ' ', $component ) ) );
73 73
 
74
-        // Loads component and pass extension's instance so that component can
75
-        // talk each other.
76
-        require_once $filename;
77
-        $this->instances[ $component ] = new $classname( $this );
78
-        $this->instances[ $component ]->load();
74
+		// Loads component and pass extension's instance so that component can
75
+		// talk each other.
76
+		require_once $filename;
77
+		$this->instances[ $component ] = new $classname( $this );
78
+		$this->instances[ $component ]->load();
79 79
 
80
-    }
80
+	}
81 81
 
82
-    private function add_hooks() {
82
+	private function add_hooks() {
83 83
 
84
-        // Add front-end access to Gravity Forms delete file action
85
-        add_action( 'wp_ajax_nopriv_rg_delete_file', array( 'GFForms', 'delete_file') );
84
+		// Add front-end access to Gravity Forms delete file action
85
+		add_action( 'wp_ajax_nopriv_rg_delete_file', array( 'GFForms', 'delete_file') );
86 86
 
87
-        // Make sure this hook is run for non-admins
88
-        add_action( 'wp_ajax_rg_delete_file', array( 'GFForms', 'delete_file') );
87
+		// Make sure this hook is run for non-admins
88
+		add_action( 'wp_ajax_rg_delete_file', array( 'GFForms', 'delete_file') );
89 89
 
90
-        add_filter( 'gravityview_blacklist_field_types', array( $this, 'modify_field_blacklist' ), 10, 2 );
90
+		add_filter( 'gravityview_blacklist_field_types', array( $this, 'modify_field_blacklist' ), 10, 2 );
91 91
 
92
-        // add template path to check for field
93
-        add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
92
+		// add template path to check for field
93
+		add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
94 94
 
95 95
 		add_filter( 'gravityview/field/is_visible', array( $this, 'maybe_not_visible' ), 10, 3 );
96 96
 
97
-    }
97
+	}
98 98
 
99 99
 	/**
100 100
 	 * Trigger hooks that are normally run in the admin for Addons, but need to be triggered manually because we're not in the admin
@@ -152,74 +152,74 @@  discard block
 block discarded – undo
152 152
 		return false;
153 153
 	}
154 154
 
155
-    /**
156
-     * Include this extension templates path
157
-     * @param array $file_paths List of template paths ordered
158
-     */
159
-    public function add_template_path( $file_paths ) {
160
-
161
-        // Index 100 is the default GravityView template path.
162
-        $file_paths[ 110 ] = self::$file;
163
-
164
-        return $file_paths;
165
-    }
166
-
167
-    /**
168
-     *
169
-     * Return a well formatted nonce key according to GravityView Edit Entry protocol
170
-     *
171
-     * @param $view_id int GravityView view id
172
-     * @param $form_id int Gravity Forms form id
173
-     * @param $entry_id int Gravity Forms entry id
174
-     * @return string
175
-     */
176
-    public static function get_nonce_key( $view_id, $form_id, $entry_id ) {
177
-        return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id );
178
-    }
179
-
180
-
181
-    /**
182
-     * The edit entry link creates a secure link with a nonce
183
-     *
184
-     * It also mimics the URL structure Gravity Forms expects to have so that
185
-     * it formats the display of the edit form like it does in the backend, like
186
-     * "You can edit this post from the post page" fields, for example.
187
-     *
188
-     * @param $entry array Gravity Forms entry object
189
-     * @param $view_id int GravityView view id
190
-     * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2}
191
-     * @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/ }
192
-     * @return string
193
-     */
194
-    public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) {
195
-
196
-        $nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id']  );
197
-
198
-        $base = gv_entry_link( $entry, $post_id ? : $view_id  );
199
-
200
-        $url = add_query_arg( array(
201
-            'edit' => wp_create_nonce( $nonce_key )
202
-        ), $base );
203
-
204
-        if( $post_id ) {
205
-	        $url = add_query_arg( array( 'gvid' => $view_id ), $url );
206
-        }
207
-
208
-	    /**
209
-	     * Allow passing params to dynamically populate entry with values
210
-	     * @since 1.9.2
211
-	     */
212
-	    if( !empty( $field_values ) ) {
213
-
214
-		    if( is_array( $field_values ) ) {
215
-			    // If already an array, no parse_str() needed
216
-			    $params = $field_values;
217
-		    } else {
218
-			    parse_str( $field_values, $params );
219
-		    }
220
-
221
-		    $url = add_query_arg( $params, $url );
222
-	    }
155
+	/**
156
+	 * Include this extension templates path
157
+	 * @param array $file_paths List of template paths ordered
158
+	 */
159
+	public function add_template_path( $file_paths ) {
160
+
161
+		// Index 100 is the default GravityView template path.
162
+		$file_paths[ 110 ] = self::$file;
163
+
164
+		return $file_paths;
165
+	}
166
+
167
+	/**
168
+	 *
169
+	 * Return a well formatted nonce key according to GravityView Edit Entry protocol
170
+	 *
171
+	 * @param $view_id int GravityView view id
172
+	 * @param $form_id int Gravity Forms form id
173
+	 * @param $entry_id int Gravity Forms entry id
174
+	 * @return string
175
+	 */
176
+	public static function get_nonce_key( $view_id, $form_id, $entry_id ) {
177
+		return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id );
178
+	}
179
+
180
+
181
+	/**
182
+	 * The edit entry link creates a secure link with a nonce
183
+	 *
184
+	 * It also mimics the URL structure Gravity Forms expects to have so that
185
+	 * it formats the display of the edit form like it does in the backend, like
186
+	 * "You can edit this post from the post page" fields, for example.
187
+	 *
188
+	 * @param $entry array Gravity Forms entry object
189
+	 * @param $view_id int GravityView view id
190
+	 * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2}
191
+	 * @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/ }
192
+	 * @return string
193
+	 */
194
+	public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) {
195
+
196
+		$nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id']  );
197
+
198
+		$base = gv_entry_link( $entry, $post_id ? : $view_id  );
199
+
200
+		$url = add_query_arg( array(
201
+			'edit' => wp_create_nonce( $nonce_key )
202
+		), $base );
203
+
204
+		if( $post_id ) {
205
+			$url = add_query_arg( array( 'gvid' => $view_id ), $url );
206
+		}
207
+
208
+		/**
209
+		 * Allow passing params to dynamically populate entry with values
210
+		 * @since 1.9.2
211
+		 */
212
+		if( !empty( $field_values ) ) {
213
+
214
+			if( is_array( $field_values ) ) {
215
+				// If already an array, no parse_str() needed
216
+				$params = $field_values;
217
+			} else {
218
+				parse_str( $field_values, $params );
219
+			}
220
+
221
+			$url = add_query_arg( $params, $url );
222
+		}
223 223
 
224 224
 		/**
225 225
 		 * @filter `gravityview/edit/link` Filter the edit URL link.
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
 		 * @param \GV\View $view The View.
229 229
 		 */
230 230
 		return apply_filters( 'gravityview/edit/link', $url, $entry, \GV\View::by_id( $view_id  ) );
231
-    }
231
+	}
232 232
 
233 233
 	/**
234 234
 	 * Edit mode doesn't allow certain field types.
@@ -283,19 +283,19 @@  discard block
 block discarded – undo
283 283
 	}
284 284
 
285 285
 
286
-    /**
287
-     * checks if user has permissions to edit a specific entry
288
-     *
289
-     * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!!
290
-     *
291
-     * @param  array $entry Gravity Forms entry array
292
-     * @param \GV\View int $view_id ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0
293
-     * @return bool
294
-     */
295
-    public static function check_user_cap_edit_entry( $entry, $view = 0 ) {
286
+	/**
287
+	 * checks if user has permissions to edit a specific entry
288
+	 *
289
+	 * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!!
290
+	 *
291
+	 * @param  array $entry Gravity Forms entry array
292
+	 * @param \GV\View int $view_id ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0
293
+	 * @return bool
294
+	 */
295
+	public static function check_user_cap_edit_entry( $entry, $view = 0 ) {
296 296
 
297
-        // No permission by default
298
-        $user_can_edit = false;
297
+		// No permission by default
298
+		$user_can_edit = false;
299 299
 
300 300
 		// get user_edit setting
301 301
 		if ( empty( $view ) ) {
@@ -313,59 +313,59 @@  discard block
 block discarded – undo
313 313
 			$user_edit = GVCommon::get_template_setting( $view_id, 'user_edit' );
314 314
 		}
315 315
 
316
-        // If they can edit any entries (as defined in Gravity Forms)
317
-        // Or if they can edit other people's entries
318
-        // Then we're good.
319
-        if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) {
316
+		// If they can edit any entries (as defined in Gravity Forms)
317
+		// Or if they can edit other people's entries
318
+		// Then we're good.
319
+		if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) {
320 320
 
321
-            gravityview()->log->debug( 'User has ability to edit all entries.' );
321
+			gravityview()->log->debug( 'User has ability to edit all entries.' );
322 322
 
323
-            $user_can_edit = true;
323
+			$user_can_edit = true;
324 324
 
325
-        } else if( !isset( $entry['created_by'] ) ) {
325
+		} else if( !isset( $entry['created_by'] ) ) {
326 326
 
327
-            gravityview()->log->error( 'Entry `created_by` doesn\'t exist.');
327
+			gravityview()->log->error( 'Entry `created_by` doesn\'t exist.');
328 328
 
329
-            $user_can_edit = false;
329
+			$user_can_edit = false;
330 330
 
331
-        } else {
331
+		} else {
332 332
 
333 333
 
334
-            $current_user = wp_get_current_user();
334
+			$current_user = wp_get_current_user();
335 335
 
336
-            // User edit is disabled
337
-            if( empty( $user_edit ) ) {
336
+			// User edit is disabled
337
+			if( empty( $user_edit ) ) {
338 338
 
339
-                gravityview()->log->debug( 'User Edit is disabled. Returning false.' );
339
+				gravityview()->log->debug( 'User Edit is disabled. Returning false.' );
340 340
 
341
-                $user_can_edit = false;
342
-            }
341
+				$user_can_edit = false;
342
+			}
343 343
 
344
-            // User edit is enabled and the logged-in user is the same as the user who created the entry. We're good.
345
-            else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) {
344
+			// User edit is enabled and the logged-in user is the same as the user who created the entry. We're good.
345
+			else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) {
346 346
 
347
-                gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) );
347
+				gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) );
348 348
 
349
-                $user_can_edit = true;
349
+				$user_can_edit = true;
350 350
 
351
-            } else if( ! is_user_logged_in() ) {
351
+			} else if( ! is_user_logged_in() ) {
352 352
 
353
-                gravityview()->log->debug( 'No user defined; edit entry requires logged in user' );
354
-            }
353
+				gravityview()->log->debug( 'No user defined; edit entry requires logged in user' );
354
+			}
355 355
 
356
-        }
356
+		}
357 357
 
358
-        /**
359
-         * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry.
360
-         * @since 1.15 Added `$entry` and `$view_id` parameters
361
-         * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false)
362
-         * @param[in] array $entry Gravity Forms entry array {@since 1.15}
363
-         * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15}
364
-         */
365
-        $user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id );
358
+		/**
359
+		 * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry.
360
+		 * @since 1.15 Added `$entry` and `$view_id` parameters
361
+		 * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false)
362
+		 * @param[in] array $entry Gravity Forms entry array {@since 1.15}
363
+		 * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15}
364
+		 */
365
+		$user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id );
366 366
 
367
-        return (bool)$user_can_edit;
368
-    }
367
+		return (bool)$user_can_edit;
368
+	}
369 369
 
370 370
 
371 371
 
Please login to merge, or discard this patch.