@@ -18,82 +18,82 @@ 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 | |
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,74 +151,74 @@ discard block |
||
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 | - } |
|
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 | 222 | |
223 | 223 | /** |
224 | 224 | * @filter `gravityview/edit/link` Filter the edit URL link. |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | * @param \GV\View $view The View. |
228 | 228 | */ |
229 | 229 | return apply_filters( 'gravityview/edit/link', $url, $entry, \GV\View::by_id( $view_id ) ); |
230 | - } |
|
230 | + } |
|
231 | 231 | |
232 | 232 | /** |
233 | 233 | * Edit mode doesn't allow certain field types. |
@@ -282,19 +282,19 @@ discard block |
||
282 | 282 | } |
283 | 283 | |
284 | 284 | |
285 | - /** |
|
286 | - * checks if user has permissions to edit a specific entry |
|
287 | - * |
|
288 | - * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!! |
|
289 | - * |
|
290 | - * @param array $entry Gravity Forms entry array |
|
291 | - * @param \GV\View int $view_id ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0 |
|
292 | - * @return bool |
|
293 | - */ |
|
294 | - public static function check_user_cap_edit_entry( $entry, $view = 0 ) { |
|
285 | + /** |
|
286 | + * checks if user has permissions to edit a specific entry |
|
287 | + * |
|
288 | + * Needs to be used combined with GravityView_Edit_Entry::user_can_edit_entry for maximum security!! |
|
289 | + * |
|
290 | + * @param array $entry Gravity Forms entry array |
|
291 | + * @param \GV\View int $view_id ID of the view you want to check visibility against {@since 1.9.2}. Required since 2.0 |
|
292 | + * @return bool |
|
293 | + */ |
|
294 | + public static function check_user_cap_edit_entry( $entry, $view = 0 ) { |
|
295 | 295 | |
296 | - // No permission by default |
|
297 | - $user_can_edit = false; |
|
296 | + // No permission by default |
|
297 | + $user_can_edit = false; |
|
298 | 298 | |
299 | 299 | // get user_edit setting |
300 | 300 | if ( empty( $view ) ) { |
@@ -312,59 +312,59 @@ discard block |
||
312 | 312 | $user_edit = GVCommon::get_template_setting( $view_id, 'user_edit' ); |
313 | 313 | } |
314 | 314 | |
315 | - // If they can edit any entries (as defined in Gravity Forms) |
|
316 | - // Or if they can edit other people's entries |
|
317 | - // Then we're good. |
|
318 | - if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) { |
|
315 | + // If they can edit any entries (as defined in Gravity Forms) |
|
316 | + // Or if they can edit other people's entries |
|
317 | + // Then we're good. |
|
318 | + if( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gravityview_edit_others_entries' ), $entry['id'] ) ) { |
|
319 | 319 | |
320 | - gravityview()->log->debug( 'User has ability to edit all entries.' ); |
|
320 | + gravityview()->log->debug( 'User has ability to edit all entries.' ); |
|
321 | 321 | |
322 | - $user_can_edit = true; |
|
322 | + $user_can_edit = true; |
|
323 | 323 | |
324 | - } else if( !isset( $entry['created_by'] ) ) { |
|
324 | + } else if( !isset( $entry['created_by'] ) ) { |
|
325 | 325 | |
326 | - gravityview()->log->error( 'Entry `created_by` doesn\'t exist.'); |
|
326 | + gravityview()->log->error( 'Entry `created_by` doesn\'t exist.'); |
|
327 | 327 | |
328 | - $user_can_edit = false; |
|
328 | + $user_can_edit = false; |
|
329 | 329 | |
330 | - } else { |
|
330 | + } else { |
|
331 | 331 | |
332 | 332 | |
333 | - $current_user = wp_get_current_user(); |
|
333 | + $current_user = wp_get_current_user(); |
|
334 | 334 | |
335 | - // User edit is disabled |
|
336 | - if( empty( $user_edit ) ) { |
|
335 | + // User edit is disabled |
|
336 | + if( empty( $user_edit ) ) { |
|
337 | 337 | |
338 | - gravityview()->log->debug( 'User Edit is disabled. Returning false.' ); |
|
338 | + gravityview()->log->debug( 'User Edit is disabled. Returning false.' ); |
|
339 | 339 | |
340 | - $user_can_edit = false; |
|
341 | - } |
|
340 | + $user_can_edit = false; |
|
341 | + } |
|
342 | 342 | |
343 | - // User edit is enabled and the logged-in user is the same as the user who created the entry. We're good. |
|
344 | - else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) { |
|
343 | + // User edit is enabled and the logged-in user is the same as the user who created the entry. We're good. |
|
344 | + else if( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) { |
|
345 | 345 | |
346 | - gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) ); |
|
346 | + gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id', $current_user->ID ) ); |
|
347 | 347 | |
348 | - $user_can_edit = true; |
|
348 | + $user_can_edit = true; |
|
349 | 349 | |
350 | - } else if( ! is_user_logged_in() ) { |
|
350 | + } else if( ! is_user_logged_in() ) { |
|
351 | 351 | |
352 | - gravityview()->log->debug( 'No user defined; edit entry requires logged in user' ); |
|
353 | - } |
|
352 | + gravityview()->log->debug( 'No user defined; edit entry requires logged in user' ); |
|
353 | + } |
|
354 | 354 | |
355 | - } |
|
355 | + } |
|
356 | 356 | |
357 | - /** |
|
358 | - * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry. |
|
359 | - * @since 1.15 Added `$entry` and `$view_id` parameters |
|
360 | - * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false) |
|
361 | - * @param[in] array $entry Gravity Forms entry array {@since 1.15} |
|
362 | - * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15} |
|
363 | - */ |
|
364 | - $user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id ); |
|
357 | + /** |
|
358 | + * @filter `gravityview/edit_entry/user_can_edit_entry` Modify whether user can edit an entry. |
|
359 | + * @since 1.15 Added `$entry` and `$view_id` parameters |
|
360 | + * @param[in,out] boolean $user_can_edit Can the current user edit the current entry? (Default: false) |
|
361 | + * @param[in] array $entry Gravity Forms entry array {@since 1.15} |
|
362 | + * @param[in] int $view_id ID of the view you want to check visibility against {@since 1.15} |
|
363 | + */ |
|
364 | + $user_can_edit = apply_filters( 'gravityview/edit_entry/user_can_edit_entry', $user_can_edit, $entry, $view_id ); |
|
365 | 365 | |
366 | - return (bool)$user_can_edit; |
|
367 | - } |
|
366 | + return (bool)$user_can_edit; |
|
367 | + } |
|
368 | 368 | |
369 | 369 | |
370 | 370 |