@@ -18,84 +18,84 @@ discard block |
||
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 | add_filter( 'gravityview/api/reserved_query_args', array( $this, 'add_reserved_arg' ) ); |
98 | - } |
|
98 | + } |
|
99 | 99 | |
100 | 100 | /** |
101 | 101 | * Adds "edit" to the list of internal reserved query args |
@@ -166,74 +166,74 @@ discard block |
||
166 | 166 | return false; |
167 | 167 | } |
168 | 168 | |
169 | - /** |
|
170 | - * Include this extension templates path |
|
171 | - * @param array $file_paths List of template paths ordered |
|
172 | - */ |
|
173 | - public function add_template_path( $file_paths ) { |
|
174 | - |
|
175 | - // Index 100 is the default GravityView template path. |
|
176 | - $file_paths[ 110 ] = self::$file; |
|
177 | - |
|
178 | - return $file_paths; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * |
|
183 | - * Return a well formatted nonce key according to GravityView Edit Entry protocol |
|
184 | - * |
|
185 | - * @param $view_id int GravityView view id |
|
186 | - * @param $form_id int Gravity Forms form id |
|
187 | - * @param $entry_id int Gravity Forms entry id |
|
188 | - * @return string |
|
189 | - */ |
|
190 | - public static function get_nonce_key( $view_id, $form_id, $entry_id ) { |
|
191 | - return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id ); |
|
192 | - } |
|
193 | - |
|
194 | - |
|
195 | - /** |
|
196 | - * The edit entry link creates a secure link with a nonce |
|
197 | - * |
|
198 | - * It also mimics the URL structure Gravity Forms expects to have so that |
|
199 | - * it formats the display of the edit form like it does in the backend, like |
|
200 | - * "You can edit this post from the post page" fields, for example. |
|
201 | - * |
|
202 | - * @param $entry array Gravity Forms entry object |
|
203 | - * @param $view_id int GravityView view id |
|
204 | - * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2} |
|
205 | - * @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/ } |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) { |
|
209 | - |
|
210 | - $nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id'] ); |
|
211 | - |
|
212 | - $base = gv_entry_link( $entry, $post_id ? : $view_id ); |
|
213 | - |
|
214 | - $url = add_query_arg( array( |
|
215 | - 'edit' => wp_create_nonce( $nonce_key ) |
|
216 | - ), $base ); |
|
217 | - |
|
218 | - if( $post_id ) { |
|
219 | - $url = add_query_arg( array( 'gvid' => $view_id ), $url ); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Allow passing params to dynamically populate entry with values |
|
224 | - * @since 1.9.2 |
|
225 | - */ |
|
226 | - if( !empty( $field_values ) ) { |
|
227 | - |
|
228 | - if( is_array( $field_values ) ) { |
|
229 | - // If already an array, no parse_str() needed |
|
230 | - $params = $field_values; |
|
231 | - } else { |
|
232 | - parse_str( $field_values, $params ); |
|
233 | - } |
|
234 | - |
|
235 | - $url = add_query_arg( $params, $url ); |
|
236 | - } |
|
169 | + /** |
|
170 | + * Include this extension templates path |
|
171 | + * @param array $file_paths List of template paths ordered |
|
172 | + */ |
|
173 | + public function add_template_path( $file_paths ) { |
|
174 | + |
|
175 | + // Index 100 is the default GravityView template path. |
|
176 | + $file_paths[ 110 ] = self::$file; |
|
177 | + |
|
178 | + return $file_paths; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * |
|
183 | + * Return a well formatted nonce key according to GravityView Edit Entry protocol |
|
184 | + * |
|
185 | + * @param $view_id int GravityView view id |
|
186 | + * @param $form_id int Gravity Forms form id |
|
187 | + * @param $entry_id int Gravity Forms entry id |
|
188 | + * @return string |
|
189 | + */ |
|
190 | + public static function get_nonce_key( $view_id, $form_id, $entry_id ) { |
|
191 | + return sprintf( 'edit_%d_%d_%d', $view_id, $form_id, $entry_id ); |
|
192 | + } |
|
193 | + |
|
194 | + |
|
195 | + /** |
|
196 | + * The edit entry link creates a secure link with a nonce |
|
197 | + * |
|
198 | + * It also mimics the URL structure Gravity Forms expects to have so that |
|
199 | + * it formats the display of the edit form like it does in the backend, like |
|
200 | + * "You can edit this post from the post page" fields, for example. |
|
201 | + * |
|
202 | + * @param $entry array Gravity Forms entry object |
|
203 | + * @param $view_id int GravityView view id |
|
204 | + * @param $post_id int GravityView Post ID where View may be embedded {@since 1.9.2} |
|
205 | + * @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/ } |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + public static function get_edit_link( $entry, $view_id, $post_id = null, $field_values = '' ) { |
|
209 | + |
|
210 | + $nonce_key = self::get_nonce_key( $view_id, $entry['form_id'], $entry['id'] ); |
|
211 | + |
|
212 | + $base = gv_entry_link( $entry, $post_id ? : $view_id ); |
|
213 | + |
|
214 | + $url = add_query_arg( array( |
|
215 | + 'edit' => wp_create_nonce( $nonce_key ) |
|
216 | + ), $base ); |
|
217 | + |
|
218 | + if( $post_id ) { |
|
219 | + $url = add_query_arg( array( 'gvid' => $view_id ), $url ); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Allow passing params to dynamically populate entry with values |
|
224 | + * @since 1.9.2 |
|
225 | + */ |
|
226 | + if( !empty( $field_values ) ) { |
|
227 | + |
|
228 | + if( is_array( $field_values ) ) { |
|
229 | + // If already an array, no parse_str() needed |
|
230 | + $params = $field_values; |
|
231 | + } else { |
|
232 | + parse_str( $field_values, $params ); |
|
233 | + } |
|
234 | + |
|
235 | + $url = add_query_arg( $params, $url ); |
|
236 | + } |
|
237 | 237 | |
238 | 238 | /** |
239 | 239 | * @filter `gravityview/edit/link` Filter the edit URL link. |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | * @param \GV\View $view The View. |
243 | 243 | */ |
244 | 244 | return apply_filters( 'gravityview/edit/link', $url, $entry, \GV\View::by_id( $view_id ) ); |
245 | - } |
|
245 | + } |
|
246 | 246 | |
247 | 247 | /** |
248 | 248 | * Edit mode doesn't allow certain field types. |
@@ -297,19 +297,19 @@ discard block |
||
297 | 297 | } |
298 | 298 | |
299 | 299 | |
300 | - /** |
|
301 | - * checks if user has permissions to edit a specific entry |
|
302 | - * |
|
303 | - * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!! |
|
304 | - * |
|
305 | - * @param array $entry Gravity Forms entry array |
|
306 | - * @param \GV\View|int $view ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0 |
|
307 | - * @return bool |
|
308 | - */ |
|
309 | - public static function check_user_cap_edit_entry( $entry, $view = 0 ) { |
|
300 | + /** |
|
301 | + * checks if user has permissions to edit a specific entry |
|
302 | + * |
|
303 | + * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!! |
|
304 | + * |
|
305 | + * @param array $entry Gravity Forms entry array |
|
306 | + * @param \GV\View|int $view ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0 |
|
307 | + * @return bool |
|
308 | + */ |
|
309 | + public static function check_user_cap_edit_entry( $entry, $view = 0 ) { |
|
310 | 310 | |
311 | - // No permission by default |
|
312 | - $user_can_edit = false; |
|
311 | + // No permission by default |
|
312 | + $user_can_edit = false; |
|
313 | 313 | |
314 | 314 | // get user_edit setting |
315 | 315 | if ( empty( $view ) ) { |
@@ -327,60 +327,60 @@ discard block |
||
327 | 327 | $user_edit = GVCommon::get_template_setting( $view_id, 'user_edit' ); |
328 | 328 | } |
329 | 329 | |
330 | - // If they can edit any entries (as defined in Gravity Forms) |
|
331 | - // Or if they can edit other people's entries |
|
332 | - // Then we're good. |
|
333 | - if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) { |
|
330 | + // If they can edit any entries (as defined in Gravity Forms) |
|
331 | + // Or if they can edit other people's entries |
|
332 | + // Then we're good. |
|
333 | + if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) { |
|
334 | 334 | |
335 | - gravityview()->log->debug( 'User has ability to edit all entries.' ); |
|
335 | + gravityview()->log->debug( 'User has ability to edit all entries.' ); |
|
336 | 336 | |
337 | - $user_can_edit = true; |
|
337 | + $user_can_edit = true; |
|
338 | 338 | |
339 | - } else if( !isset( $entry['created_by'] ) ) { |
|
339 | + } else if( !isset( $entry['created_by'] ) ) { |
|
340 | 340 | |
341 | - gravityview()->log->error( 'Entry `created_by` doesn\'t exist.'); |
|
341 | + gravityview()->log->error( 'Entry `created_by` doesn\'t exist.'); |
|
342 | 342 | |
343 | - $user_can_edit = false; |
|
343 | + $user_can_edit = false; |
|
344 | 344 | |
345 | - } else { |
|
345 | + } else { |
|
346 | 346 | |
347 | - $current_user = wp_get_current_user(); |
|
347 | + $current_user = wp_get_current_user(); |
|
348 | 348 | |
349 | - // User edit is disabled |
|
350 | - if( empty( $user_edit ) ) { |
|
349 | + // User edit is disabled |
|
350 | + if( empty( $user_edit ) ) { |
|
351 | 351 | |
352 | - gravityview()->log->debug( 'User Edit is disabled. Returning false.' ); |
|
352 | + gravityview()->log->debug( 'User Edit is disabled. Returning false.' ); |
|
353 | 353 | |
354 | - $user_can_edit = false; |
|
355 | - } |
|
354 | + $user_can_edit = false; |
|
355 | + } |
|
356 | 356 | |
357 | - // User edit is enabled and the logged-in user is the same as the user who created the entry. We're good. |
|
358 | - else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) { |
|
357 | + // User edit is enabled and the logged-in user is the same as the user who created the entry. We're good. |
|
358 | + else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) { |
|
359 | 359 | |
360 | - gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) ); |
|
360 | + gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) ); |
|
361 | 361 | |
362 | - $user_can_edit = true; |
|
362 | + $user_can_edit = true; |
|
363 | 363 | |
364 | - } else if( ! is_user_logged_in() ) { |
|
364 | + } else if( ! is_user_logged_in() ) { |
|
365 | 365 | |
366 | - gravityview()->log->debug( 'No user defined; edit entry requires logged in user' ); |
|
366 | + gravityview()->log->debug( 'No user defined; edit entry requires logged in user' ); |
|
367 | 367 | |
368 | - $user_can_edit = false; // Here just for clarity |
|
369 | - } |
|
368 | + $user_can_edit = false; // Here just for clarity |
|
369 | + } |
|
370 | 370 | |
371 | - } |
|
371 | + } |
|
372 | 372 | |
373 | - /** |
|
374 | - * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry. |
|
375 | - * @since 1.15 Added `$entry` and `$view_id` parameters |
|
376 | - * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false) |
|
377 | - * @param[in] array $entry Gravity Forms entry array {@since 1.15} |
|
378 | - * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15} |
|
379 | - */ |
|
380 | - $user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id ); |
|
373 | + /** |
|
374 | + * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry. |
|
375 | + * @since 1.15 Added `$entry` and `$view_id` parameters |
|
376 | + * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false) |
|
377 | + * @param[in] array $entry Gravity Forms entry array {@since 1.15} |
|
378 | + * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15} |
|
379 | + */ |
|
380 | + $user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id ); |
|
381 | 381 | |
382 | - return (bool) $user_can_edit; |
|
383 | - } |
|
382 | + return (bool) $user_can_edit; |
|
383 | + } |
|
384 | 384 | |
385 | 385 | |
386 | 386 |