Completed
Push — develop ( 334ce8...0382bd )
by Zack
16:59
created
includes/extensions/edit-entry/class-edit-entry.php 1 patch
Indentation   +158 added lines, -158 removed lines patch added patch discarded remove patch
@@ -18,82 +18,82 @@  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( 'GFForms', 'delete_file') );
83
+		// Add front-end access to Gravity Forms delete file action
84
+		add_action( 'wp_ajax_nopriv_rg_delete_file', array( 'GFForms', 'delete_file') );
85 85
 
86
-        // Make sure this hook is run for non-admins
87
-        add_action( 'wp_ajax_rg_delete_file', array( 'GFForms', 'delete_file') );
86
+		// Make sure this hook is run for non-admins
87
+		add_action( 'wp_ajax_rg_delete_file', array( 'GFForms', '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
 		add_filter( 'gravityview/field/is_visible', array( $this, 'maybe_not_visible' ), 10, 3 );
95 95
 
96
-    }
96
+	}
97 97
 
98 98
 	/**
99 99
 	 * Trigger hooks that are normally run in the admin for Addons, but need to be triggered manually because we're not in the admin
@@ -151,77 +151,77 @@  discard block
 block discarded – undo
151 151
 		return false;
152 152
 	}
153 153
 
154
-    /**
155
-     * Include this extension templates path
156
-     * @param array $file_paths List of template paths ordered
157
-     */
158
-    public function add_template_path( $file_paths ) {
159
-
160
-        // Index 100 is the default GravityView template path.
161
-        $file_paths[ 110 ] = self::$file;
162
-
163
-        return $file_paths;
164
-    }
165
-
166
-    /**
167
-     *
168
-     * Return a well formatted nonce key according to GravityView Edit Entry protocol
169
-     *
170
-     * @param $view_id int GravityView view id
171
-     * @param $form_id int Gravity Forms form id
172
-     * @param $entry_id int Gravity Forms entry id
173
-     * @return string
174
-     */
175
-    public static function get_nonce_key( $view_id, $form_id, $entry_id ) {
176
-        return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id );
177
-    }
178
-
179
-
180
-    /**
181
-     * The edit entry link creates a secure link with a nonce
182
-     *
183
-     * It also mimics the URL structure Gravity Forms expects to have so that
184
-     * it formats the display of the edit form like it does in the backend, like
185
-     * "You can edit this post from the post page" fields, for example.
186
-     *
187
-     * @param $entry array Gravity Forms entry object
188
-     * @param $view_id int GravityView view id
189
-     * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2}
190
-     * @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/ }
191
-     * @return string
192
-     */
193
-    public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) {
194
-
195
-        $nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id']  );
196
-
197
-        $base = gv_entry_link( $entry, $post_id ? : $view_id  );
198
-
199
-        $url = add_query_arg( array(
200
-            'edit' => wp_create_nonce( $nonce_key )
201
-        ), $base );
202
-
203
-        if( $post_id ) {
204
-	        $url = add_query_arg( array( 'gvid' => $view_id ), $url );
205
-        }
206
-
207
-	    /**
208
-	     * Allow passing params to dynamically populate entry with values
209
-	     * @since 1.9.2
210
-	     */
211
-	    if( !empty( $field_values ) ) {
212
-
213
-		    if( is_array( $field_values ) ) {
214
-			    // If already an array, no parse_str() needed
215
-			    $params = $field_values;
216
-		    } else {
217
-			    parse_str( $field_values, $params );
218
-		    }
219
-
220
-		    $url = add_query_arg( $params, $url );
221
-	    }
222
-
223
-        return $url;
224
-    }
154
+	/**
155
+	 * Include this extension templates path
156
+	 * @param array $file_paths List of template paths ordered
157
+	 */
158
+	public function add_template_path( $file_paths ) {
159
+
160
+		// Index 100 is the default GravityView template path.
161
+		$file_paths[ 110 ] = self::$file;
162
+
163
+		return $file_paths;
164
+	}
165
+
166
+	/**
167
+	 *
168
+	 * Return a well formatted nonce key according to GravityView Edit Entry protocol
169
+	 *
170
+	 * @param $view_id int GravityView view id
171
+	 * @param $form_id int Gravity Forms form id
172
+	 * @param $entry_id int Gravity Forms entry id
173
+	 * @return string
174
+	 */
175
+	public static function get_nonce_key( $view_id, $form_id, $entry_id ) {
176
+		return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id );
177
+	}
178
+
179
+
180
+	/**
181
+	 * The edit entry link creates a secure link with a nonce
182
+	 *
183
+	 * It also mimics the URL structure Gravity Forms expects to have so that
184
+	 * it formats the display of the edit form like it does in the backend, like
185
+	 * "You can edit this post from the post page" fields, for example.
186
+	 *
187
+	 * @param $entry array Gravity Forms entry object
188
+	 * @param $view_id int GravityView view id
189
+	 * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2}
190
+	 * @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/ }
191
+	 * @return string
192
+	 */
193
+	public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) {
194
+
195
+		$nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id']  );
196
+
197
+		$base = gv_entry_link( $entry, $post_id ? : $view_id  );
198
+
199
+		$url = add_query_arg( array(
200
+			'edit' => wp_create_nonce( $nonce_key )
201
+		), $base );
202
+
203
+		if( $post_id ) {
204
+			$url = add_query_arg( array( 'gvid' => $view_id ), $url );
205
+		}
206
+
207
+		/**
208
+		 * Allow passing params to dynamically populate entry with values
209
+		 * @since 1.9.2
210
+		 */
211
+		if( !empty( $field_values ) ) {
212
+
213
+			if( is_array( $field_values ) ) {
214
+				// If already an array, no parse_str() needed
215
+				$params = $field_values;
216
+			} else {
217
+				parse_str( $field_values, $params );
218
+			}
219
+
220
+			$url = add_query_arg( $params, $url );
221
+		}
222
+
223
+		return $url;
224
+	}
225 225
 
226 226
 	/**
227 227
 	 * Edit mode doesn't allow certain field types.
@@ -276,19 +276,19 @@  discard block
 block discarded – undo
276 276
 	}
277 277
 
278 278
 
279
-    /**
280
-     * checks if user has permissions to edit a specific entry
281
-     *
282
-     * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!!
283
-     *
284
-     * @param  array $entry Gravity Forms entry array
285
-     * @param \GV\View int $view_id ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0
286
-     * @return bool
287
-     */
288
-    public static function check_user_cap_edit_entry( $entry, $view = 0 ) {
279
+	/**
280
+	 * checks if user has permissions to edit a specific entry
281
+	 *
282
+	 * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!!
283
+	 *
284
+	 * @param  array $entry Gravity Forms entry array
285
+	 * @param \GV\View int $view_id ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0
286
+	 * @return bool
287
+	 */
288
+	public static function check_user_cap_edit_entry( $entry, $view = 0 ) {
289 289
 
290
-        // No permission by default
291
-        $user_can_edit = false;
290
+		// No permission by default
291
+		$user_can_edit = false;
292 292
 
293 293
 		// get user_edit setting
294 294
 		if ( empty( $view ) ) {
@@ -306,59 +306,59 @@  discard block
 block discarded – undo
306 306
 			$user_edit = GVCommon::get_template_setting( $view_id, 'user_edit' );
307 307
 		}
308 308
 
309
-        // If they can edit any entries (as defined in Gravity Forms)
310
-        // Or if they can edit other people's entries
311
-        // Then we're good.
312
-        if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) {
309
+		// If they can edit any entries (as defined in Gravity Forms)
310
+		// Or if they can edit other people's entries
311
+		// Then we're good.
312
+		if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) {
313 313
 
314
-            gravityview()->log->debug( 'User has ability to edit all entries.' );
314
+			gravityview()->log->debug( 'User has ability to edit all entries.' );
315 315
 
316
-            $user_can_edit = true;
316
+			$user_can_edit = true;
317 317
 
318
-        } else if( !isset( $entry['created_by'] ) ) {
318
+		} else if( !isset( $entry['created_by'] ) ) {
319 319
 
320
-            gravityview()->log->error( 'Entry `created_by` doesn\'t exist.');
320
+			gravityview()->log->error( 'Entry `created_by` doesn\'t exist.');
321 321
 
322
-            $user_can_edit = false;
322
+			$user_can_edit = false;
323 323
 
324
-        } else {
324
+		} else {
325 325
 
326 326
 
327
-            $current_user = wp_get_current_user();
327
+			$current_user = wp_get_current_user();
328 328
 
329
-            // User edit is disabled
330
-            if( empty( $user_edit ) ) {
329
+			// User edit is disabled
330
+			if( empty( $user_edit ) ) {
331 331
 
332
-                gravityview()->log->debug( 'User Edit is disabled. Returning false.' );
332
+				gravityview()->log->debug( 'User Edit is disabled. Returning false.' );
333 333
 
334
-                $user_can_edit = false;
335
-            }
334
+				$user_can_edit = false;
335
+			}
336 336
 
337
-            // User edit is enabled and the logged-in user is the same as the user who created the entry. We're good.
338
-            else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) {
337
+			// User edit is enabled and the logged-in user is the same as the user who created the entry. We're good.
338
+			else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) {
339 339
 
340
-                gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) );
340
+				gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) );
341 341
 
342
-                $user_can_edit = true;
342
+				$user_can_edit = true;
343 343
 
344
-            } else if( ! is_user_logged_in() ) {
344
+			} else if( ! is_user_logged_in() ) {
345 345
 
346
-                gravityview()->log->debug( 'No user defined; edit entry requires logged in user' );
347
-            }
346
+				gravityview()->log->debug( 'No user defined; edit entry requires logged in user' );
347
+			}
348 348
 
349
-        }
349
+		}
350 350
 
351
-        /**
352
-         * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry.
353
-         * @since 1.15 Added `$entry` and `$view_id` parameters
354
-         * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false)
355
-         * @param[in] array $entry Gravity Forms entry array {@since 1.15}
356
-         * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15}
357
-         */
358
-        $user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id );
351
+		/**
352
+		 * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry.
353
+		 * @since 1.15 Added `$entry` and `$view_id` parameters
354
+		 * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false)
355
+		 * @param[in] array $entry Gravity Forms entry array {@since 1.15}
356
+		 * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15}
357
+		 */
358
+		$user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id );
359 359
 
360
-        return (bool)$user_can_edit;
361
-    }
360
+		return (bool)$user_can_edit;
361
+	}
362 362
 
363 363
 
364 364
 
Please login to merge, or discard this patch.