1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The GravityView Duplicate Entry Extension |
4
|
|
|
* |
5
|
|
|
* Duplicate entries in GravityView. |
6
|
|
|
* |
7
|
|
|
* @since 2.5 |
8
|
|
|
* @package GravityView |
9
|
|
|
* @license GPL2+ |
10
|
|
|
* @author Katz Web Services, Inc. |
11
|
|
|
* @link http://gravityview.co |
12
|
|
|
* @copyright Copyright 2014, Katz Web Services, Inc. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
if ( ! defined( 'WPINC' ) ) { |
16
|
|
|
die; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @since 2.5 |
21
|
|
|
*/ |
22
|
|
|
final class GravityView_Duplicate_Entry { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string The location of this file. |
26
|
|
|
*/ |
27
|
|
|
static $file; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var GravityView_Duplicate_Entry This instance. |
31
|
|
|
*/ |
32
|
|
|
static $instance; |
33
|
|
|
|
34
|
|
|
var $view_id; |
35
|
|
|
|
36
|
1 |
|
function __construct() { |
|
|
|
|
37
|
|
|
|
38
|
1 |
|
self::$file = plugin_dir_path( __FILE__ ); |
39
|
1 |
|
$this->add_hooks(); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @since 2.5 |
44
|
|
|
*/ |
45
|
1 |
|
private function add_hooks() { |
46
|
|
|
|
47
|
1 |
|
add_action( 'wp', array( $this, 'process_duplicate' ), 10000 ); |
48
|
|
|
|
49
|
1 |
|
add_filter( 'gravityview_entry_default_fields', array( $this, 'add_default_field' ), 10, 3 ); |
50
|
|
|
|
51
|
1 |
|
add_action( 'gravityview_before', array( $this, 'maybe_display_message' ) ); |
52
|
|
|
|
53
|
|
|
// For the Duplicate Entry Link, you don't want visible to all users. |
54
|
1 |
|
add_filter( 'gravityview_field_visibility_caps', array( $this, 'modify_visibility_caps' ), 10, 5 ); |
55
|
|
|
|
56
|
|
|
// Modify the field options based on the name of the field type |
57
|
1 |
|
add_filter( 'gravityview_template_duplicate_link_options', array( $this, 'duplicate_link_field_options' ), 10, 5 ); |
58
|
|
|
|
59
|
|
|
// add template path to check for field |
60
|
1 |
|
add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) ); |
61
|
|
|
|
62
|
|
|
// Entry duplication in the backend |
63
|
1 |
|
add_action( 'gform_entries_first_column_actions', array( $this, 'make_duplicate_link_row' ), 10, 5 ); |
64
|
|
|
|
65
|
|
|
// Handle duplicate action in the backend |
66
|
1 |
|
add_action( 'gform_pre_entry_list', array( $this, 'maybe_duplicate_list' ) ); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the instantiated class object |
71
|
|
|
* |
72
|
|
|
* @since 2.5 |
73
|
|
|
* @return GravityView_Duplicate_Entry |
74
|
|
|
*/ |
75
|
2 |
|
static public function getInstance() { |
76
|
|
|
|
77
|
2 |
|
if ( empty( self::$instance ) ) { |
78
|
1 |
|
self::$instance = new self; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
return self::$instance; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Include this extension templates path |
86
|
|
|
* |
87
|
|
|
* @since 2.5 |
88
|
|
|
* |
89
|
|
|
* @param array $file_paths List of template paths ordered |
90
|
|
|
* |
91
|
|
|
* @return array File paths, with duplicate field path added at index 117 |
92
|
|
|
*/ |
93
|
2 |
|
public function add_template_path( $file_paths ) { |
94
|
|
|
|
95
|
|
|
// Index 100 is the default GravityView template path. |
96
|
|
|
// Index 110 is Edit Entry link |
97
|
2 |
|
$file_paths[ 117 ] = self::$file; |
98
|
|
|
|
99
|
2 |
|
return $file_paths; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add "Duplicate Link Text" setting to the edit_link field settings |
104
|
|
|
* |
105
|
|
|
* @since 2.5 |
106
|
|
|
* |
107
|
|
|
* @param array $field_options [description] |
108
|
|
|
* @param [type] $template_id [description] |
|
|
|
|
109
|
|
|
* @param [type] $field_id [description] |
|
|
|
|
110
|
|
|
* @param [type] $context [description] |
|
|
|
|
111
|
|
|
* @param [type] $input_type [description] |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return array [description] |
114
|
|
|
*/ |
115
|
|
|
public function duplicate_link_field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
116
|
|
|
|
117
|
|
|
// Always a link, never a filter, always same window |
118
|
|
|
unset( $field_options['show_as_link'], $field_options['search_filter'], $field_options['new_window'] ); |
119
|
|
|
|
120
|
|
|
// Duplicate Entry link should only appear to visitors capable of editing entries |
121
|
|
|
unset( $field_options['only_loggedin'], $field_options['only_loggedin_cap'] ); |
122
|
|
|
|
123
|
|
|
$add_option['duplicate_link'] = array( |
|
|
|
|
124
|
|
|
'type' => 'text', |
125
|
|
|
'label' => __( 'Duplicate Link Text', 'gravityview' ), |
126
|
|
|
'desc' => NULL, |
127
|
|
|
'value' => __( 'Duplicate Entry', 'gravityview' ), |
128
|
|
|
'merge_tags' => true, |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$field_options['allow_duplicate_cap'] = array( |
132
|
|
|
'type' => 'select', |
133
|
|
|
'label' => __( 'Allow the following users to duplicate the entry:', 'gravityview' ), |
134
|
|
|
'choices' => GravityView_Render_Settings::get_cap_choices( $template_id, $field_id, $context, $input_type ), |
135
|
|
|
'tooltip' => 'allow_duplicate_cap', |
136
|
|
|
'class' => 'widefat', |
137
|
|
|
'value' => 'read', // Default: entry creator |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return array_merge( $add_option, $field_options ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add Edit Link as a default field, outside those set in the Gravity Form form |
146
|
|
|
* |
147
|
|
|
* @since 2.5 |
148
|
|
|
* |
149
|
|
|
* @param array $entry_default_fields Existing fields |
150
|
|
|
* @param string|array $form form_ID or form object |
151
|
|
|
* @param string $zone Either 'single', 'directory', 'edit', 'header', 'footer' |
152
|
|
|
* |
153
|
|
|
* @return array $entry_default_fields, with `duplicate_link` added. Won't be added if in Edit Entry context. |
154
|
|
|
*/ |
155
|
|
|
public function add_default_field( $entry_default_fields, $form = array(), $zone = '' ) { |
156
|
|
|
|
157
|
|
|
if ( 'edit' !== $zone ) { |
158
|
|
|
$entry_default_fields['duplicate_link'] = array( |
159
|
|
|
'label' => __( 'Duplicate Entry', 'gravityview' ), |
160
|
|
|
'type' => 'duplicate_link', |
161
|
|
|
'desc' => __( 'A link to duplicate the entry. Respects the Duplicate Entry permissions.', 'gravityview' ), |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $entry_default_fields; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Add Duplicate Entry Link to the Add Field dialog |
170
|
|
|
* |
171
|
|
|
* @since 2.5 |
172
|
|
|
* |
173
|
|
|
* @param array $available_fields |
174
|
|
|
* |
175
|
|
|
* @return array Fields with `duplicate_link` added |
176
|
|
|
*/ |
177
|
|
|
public function add_available_field( $available_fields = array() ) { |
178
|
|
|
|
179
|
|
|
$available_fields['duplicate_link'] = array( |
180
|
|
|
'label_text' => __( 'Duplicate Entry', 'gravityview' ), |
181
|
|
|
'field_id' => 'duplicate_link', |
182
|
|
|
'label_type' => 'field', |
183
|
|
|
'input_type' => 'duplicate_link', |
184
|
|
|
'field_options' => NULL |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
return $available_fields; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Change wording for the Edit context to read Entry Creator |
192
|
|
|
* |
193
|
|
|
* @since 2.5 |
194
|
|
|
* |
195
|
|
|
* @param array $visibility_caps Array of capabilities to display in field dropdown. |
196
|
|
|
* @param string $field_type Type of field options to render (`field` or `widget`) |
|
|
|
|
197
|
|
|
* @param string $template_id Table slug |
198
|
|
|
* @param float|string $field_id GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by` |
199
|
|
|
* @param string $context What context are we in? Example: `single` or `directory` |
200
|
|
|
* @param string $input_type (textarea, list, select, etc.) |
201
|
|
|
* |
202
|
|
|
* @return array Array of field options with `label`, `value`, `type`, `default` keys |
203
|
|
|
*/ |
204
|
|
|
public function modify_visibility_caps( $visibility_caps = array(), $template_id = '', $field_id = '', $context = '', $input_type = '' ) { |
|
|
|
|
205
|
|
|
|
206
|
|
|
$caps = $visibility_caps; |
207
|
|
|
|
208
|
|
|
// If we're configuring fields in the edit context, we want a limited selection |
209
|
|
|
if ( 'duplicate_link' === $field_id ) { |
210
|
|
|
|
211
|
|
|
// Remove other built-in caps. |
212
|
|
|
unset( $caps['publish_posts'], $caps['gravityforms_view_entries'], $caps['duplicate_others_posts'] ); |
213
|
|
|
|
214
|
|
|
$caps['read'] = _x( 'Entry Creator', 'User capability', 'gravityview' ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $caps; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Generate a consistent nonce key based on the Entry ID |
222
|
|
|
* |
223
|
|
|
* @since 2.5 |
224
|
|
|
* |
225
|
|
|
* @param int $entry_id Entry ID |
226
|
|
|
* |
227
|
|
|
* @return string Key used to validate request |
228
|
|
|
*/ |
229
|
2 |
|
public static function get_nonce_key( $entry_id ) { |
230
|
2 |
|
return sprintf( 'duplicate_%s', $entry_id ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Generate a nonce link with the base URL of the current View embed |
236
|
|
|
* |
237
|
|
|
* We don't want to link to the single entry, because when duplicated, there would be nothing to return to. |
238
|
|
|
* |
239
|
|
|
* @since 2.5 |
240
|
|
|
* |
241
|
|
|
* @param array $entry Gravity Forms entry array |
242
|
|
|
* @param int $view_id The View id. Not optional since 2.0 |
243
|
|
|
* @param int $post_id ID of the current post/page being embedded on, if any |
244
|
|
|
* |
245
|
|
|
* @return string|null If directory link is valid, the URL to process the duplicate request. Otherwise, `NULL`. |
246
|
|
|
*/ |
247
|
2 |
|
public static function get_duplicate_link( $entry, $view_id, $post_id = null ) { |
248
|
|
|
|
249
|
2 |
|
$base = GravityView_API::directory_link( $post_id ? : $view_id, true ); |
250
|
|
|
|
251
|
2 |
|
if ( empty( $base ) ) { |
252
|
|
|
gravityview()->log->error( 'Post ID does not exist: {post_id}', array( 'post_id' => $post_id ) ); |
253
|
|
|
return NULL; |
254
|
|
|
} |
255
|
|
|
|
256
|
2 |
|
$actionurl = add_query_arg( array( |
257
|
2 |
|
'action' => 'duplicate', |
258
|
2 |
|
'entry_id' => $entry['id'], |
259
|
2 |
|
'gvid' => $view_id, |
260
|
2 |
|
'view_id' => $view_id, |
261
|
2 |
|
), $base ); |
262
|
|
|
|
263
|
2 |
|
return add_query_arg( 'duplicate', wp_create_nonce( self::get_nonce_key( $entry['id'] ) ), $actionurl ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Handle the duplication request, if $_GET['action'] is set to "duplicate" |
268
|
|
|
* |
269
|
|
|
* 1. Check referrer validity |
270
|
|
|
* 2. Make sure there's an entry with the slug of $_GET['entry_id'] |
271
|
|
|
* 3. If so, attempt to duplicate the entry. If not, set the error status |
272
|
|
|
* 4. Remove `action=duplicate` from the URL |
273
|
|
|
* 5. Redirect to the page using `wp_safe_redirect()` |
274
|
|
|
* |
275
|
|
|
* @since 2.5 |
276
|
|
|
* |
277
|
|
|
* @uses wp_safe_redirect() |
278
|
|
|
* |
279
|
|
|
* @return void|string $url URL during tests instead of redirect. |
280
|
|
|
*/ |
281
|
3 |
|
public function process_duplicate() { |
282
|
|
|
|
283
|
|
|
// If the form is submitted |
284
|
3 |
|
if ( ! isset( $_GET['action'] ) || 'duplicate' !== $_GET['action'] || ! isset( $_GET['entry_id'] ) ) { |
285
|
3 |
|
return; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// Make sure it's a GravityView request |
289
|
1 |
|
$valid_nonce_key = wp_verify_nonce( \GV\Utils::_GET( 'duplicate' ), self::get_nonce_key( $_GET['entry_id'] ) ); |
290
|
|
|
|
291
|
1 |
|
if ( ! $valid_nonce_key ) { |
292
|
1 |
|
gravityview()->log->debug( 'Duplicate entry not processed: nonce validation failed.' ); |
293
|
1 |
|
return; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// Get the entry slug |
297
|
1 |
|
$entry_slug = esc_attr( $_GET['entry_id'] ); |
298
|
|
|
|
299
|
|
|
// See if there's an entry there |
300
|
1 |
|
$entry = gravityview_get_entry( $entry_slug, true, false ); |
301
|
|
|
|
302
|
1 |
|
if ( $entry ) { |
303
|
|
|
|
304
|
1 |
|
$has_permission = $this->user_can_duplicate_entry( $entry ); |
305
|
|
|
|
306
|
1 |
|
if ( is_wp_error( $has_permission ) ) { |
307
|
|
|
|
308
|
|
|
$messages = array( |
309
|
1 |
|
'message' => urlencode( $has_permission->get_error_message() ), |
310
|
1 |
|
'status' => 'error', |
311
|
|
|
); |
312
|
|
|
|
313
|
|
|
} else { |
314
|
|
|
|
315
|
|
|
// Duplicate the entry |
316
|
1 |
|
$duplicate_response = $this->duplicate_entry( $entry ); |
317
|
|
|
|
318
|
1 |
|
if ( is_wp_error( $duplicate_response ) ) { |
319
|
|
|
|
320
|
|
|
$messages = array( |
321
|
1 |
|
'message' => urlencode( $duplicate_response->get_error_message() ), |
322
|
1 |
|
'status' => 'error', |
323
|
|
|
); |
324
|
|
|
|
325
|
1 |
|
gravityview()->log->error( 'Entry {entry_slug} cannot be duplicated: {error_code} {error_message}', array( |
326
|
1 |
|
'entry_slug' => $entry_slug, |
327
|
1 |
|
'error_code' => $duplicate_response->get_error_code(), |
328
|
1 |
|
'error_message' => $duplicate_response->get_error_message(), |
329
|
|
|
) ); |
330
|
|
|
|
331
|
|
|
} else { |
332
|
|
|
|
333
|
|
|
$messages = array( |
334
|
1 |
|
'status' => $duplicate_response, |
335
|
|
|
); |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
} else { |
342
|
|
|
|
343
|
1 |
|
gravityview()->log->error( 'Duplicate entry failed: there was no entry with the entry slug {entry_slug}', array( 'entry_slug' => $entry_slug ) ); |
344
|
|
|
|
345
|
|
|
$messages = array( |
346
|
1 |
|
'message' => urlencode( __( 'The entry does not exist.', 'gravityview' ) ), |
347
|
1 |
|
'status' => 'error', |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
1 |
|
$redirect_to_base = esc_url_raw( remove_query_arg( array( 'action', 'gvid', 'entry_id' ) ) ); |
352
|
1 |
|
$redirect_to = add_query_arg( $messages, $redirect_to_base ); |
353
|
|
|
|
354
|
1 |
|
if ( defined( 'DOING_GRAVITYVIEW_TESTS' ) ) { |
355
|
1 |
|
return $redirect_to; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
wp_safe_redirect( $redirect_to ); |
359
|
|
|
|
360
|
|
|
exit(); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Duplicate the entry. |
365
|
|
|
* |
366
|
|
|
* Done after all the checks in self::process_duplicate. |
367
|
|
|
* |
368
|
|
|
* @since 2.5 |
369
|
|
|
* |
370
|
|
|
* @param array $entry The entry to be duplicated |
371
|
|
|
* |
372
|
|
|
* @return WP_Error|boolean |
373
|
|
|
*/ |
374
|
1 |
|
private function duplicate_entry( $entry ) { |
375
|
|
|
|
376
|
1 |
|
if ( ! $entry_id = \GV\Utils::get( $entry, 'id' ) ) { |
377
|
|
|
return new WP_Error( 'gravityview-duplicate-entry-missing', __( 'The entry does not exist.', 'gravityview' ) ); |
378
|
|
|
} |
379
|
|
|
|
380
|
1 |
|
gravityview()->log->debug( 'Starting duplicate entry: {entry_id}', array( 'entry_id' => $entry_id ) ); |
381
|
|
|
|
382
|
1 |
|
global $wpdb; |
383
|
|
|
|
384
|
1 |
|
$entry_table = GFFormsModel::get_entry_table_name(); |
385
|
1 |
|
$entry_meta_table = GFFormsModel::get_entry_meta_table_name(); |
386
|
|
|
|
387
|
1 |
|
if ( ! $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $entry_table WHERE ID = %d", $entry_id ), ARRAY_A ) ) { |
388
|
|
|
return new WP_Error( 'gravityview-duplicate-entry-missing', __( 'The entry does not exist.', 'gravityview' ) ); |
389
|
|
|
} |
390
|
|
|
|
391
|
1 |
|
$row['id'] = null; |
392
|
1 |
|
$row['date_created'] = date( 'Y-m-d H:i:s', time() ); |
393
|
1 |
|
$row['date_updated'] = $row['date_created']; |
394
|
1 |
|
$row['is_starred'] = false; |
395
|
1 |
|
$row['is_read'] = false; |
396
|
1 |
|
$row['ip'] = GFFormsModel::get_ip(); |
397
|
1 |
|
$row['source_url'] = esc_url_raw( remove_query_arg( array( 'action', 'gvid' ) ) ); |
398
|
1 |
|
$row['user_agent'] = \GV\Utils::_SERVER( 'HTTP_USER_AGENT' ); |
399
|
1 |
|
$row['created_by'] = wp_get_current_user()->ID; |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @filter `gravityview/entry/duplicate/details` Modify the new entry details before it's created. |
403
|
|
|
* @since 2.5 |
404
|
|
|
* @param[in,out] array $row The entry details |
405
|
|
|
* @param array $entry The original entry |
406
|
|
|
*/ |
407
|
1 |
|
$row = apply_filters( 'gravityview/entry/duplicate/details', $row, $entry ); |
408
|
|
|
|
409
|
1 |
|
if ( ! $wpdb->insert( $entry_table, $row ) ) { |
410
|
1 |
|
return new WP_Error( 'gravityview-duplicate-entry-db-details', __( 'There was an error duplicating the entry.', 'gravityview' ) ); |
411
|
|
|
} |
412
|
|
|
|
413
|
1 |
|
$duplicated_id = $wpdb->insert_id; |
414
|
|
|
|
415
|
1 |
|
$meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $entry_meta_table WHERE entry_id = %d", $entry_id ), ARRAY_A ); |
416
|
|
|
|
417
|
1 |
|
$duplicate_meta = new WP_List_Util( $meta ); |
418
|
|
|
|
419
|
|
|
// Keys that should be reset by default |
420
|
1 |
|
$reset_meta = array( 'is_approved', 'gravityview_unique_id', 'workflow_current_status_timestamp' ); |
421
|
1 |
|
foreach ( $reset_meta as $meta_key ) { |
422
|
1 |
|
$duplicate_meta->filter( array( 'meta_key' => $meta_key ), 'NOT' ); |
423
|
|
|
} |
424
|
|
|
|
425
|
1 |
|
$save_this_meta = array(); |
426
|
1 |
|
foreach ( $duplicate_meta->get_output() as $m ) { |
427
|
1 |
|
$save_this_meta[] = array( |
428
|
1 |
|
'meta_key' => $m['meta_key'], |
429
|
1 |
|
'meta_value' => $m['meta_value'], |
430
|
1 |
|
'item_index' => $m['item_index'], |
431
|
|
|
); |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
// Update the row ID for later usage |
435
|
1 |
|
$row['id'] = $duplicated_id; |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @filter `gravityview/entry/duplicate/meta` Modify the new entry meta details. |
439
|
|
|
* @param[in,out] array $save_this_meta The duplicate meta. Use/add meta_key, meta_value, item_index. |
440
|
|
|
* @param array $row The duplicated entry |
441
|
|
|
* @param array $entry The original entry |
442
|
|
|
*/ |
443
|
1 |
|
$save_this_meta = apply_filters( 'gravityview/entry/duplicate/meta', $save_this_meta, $row, $entry ); |
444
|
|
|
|
445
|
1 |
|
foreach ( $save_this_meta as $data ) { |
446
|
1 |
|
$data['form_id'] = $entry['form_id']; |
447
|
1 |
|
$data['entry_id'] = $duplicated_id; |
448
|
|
|
|
449
|
1 |
|
if ( ! $wpdb->insert( $entry_meta_table, $data ) ) { |
450
|
|
|
return new WP_Error( 'gravityview-duplicate-entry-db-meta', __( 'There was an error duplicating the entry.', 'gravityview' ) ); |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
454
|
1 |
|
$duplicated_entry = \GFAPI::get_entry( $duplicated_id ); |
455
|
|
|
|
456
|
1 |
|
$duplicate_response = 'duplicated'; |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @action `gravityview/duplicate-entry/duplicated` Triggered when an entry is duplicated |
460
|
|
|
* @since 2.5 |
461
|
|
|
* @param array $duplicated_entry The duplicated entry |
462
|
|
|
* @param array $entry The original entry |
463
|
|
|
*/ |
464
|
1 |
|
do_action( 'gravityview/duplicate-entry/duplicated', $duplicated_entry, $entry ); |
465
|
|
|
|
466
|
1 |
|
gravityview()->log->debug( 'Duplicate response: {duplicate_response}', array( 'duplicate_response' => $duplicate_response ) ); |
467
|
|
|
|
468
|
1 |
|
return $duplicate_response; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Is the current nonce valid for editing the entry? |
473
|
|
|
* |
474
|
|
|
* @since 2.5 |
475
|
|
|
* |
476
|
|
|
* @return boolean |
477
|
|
|
*/ |
478
|
1 |
|
public function verify_nonce() { |
479
|
|
|
|
480
|
|
|
// No duplicate entry request was made |
481
|
1 |
|
if ( empty( $_GET['entry_id'] ) || empty( $_GET['duplicate'] ) ) { |
482
|
|
|
return false; |
483
|
|
|
} |
484
|
|
|
|
485
|
1 |
|
$nonce_key = self::get_nonce_key( $_GET['entry_id'] ); |
486
|
|
|
|
487
|
1 |
|
$valid = wp_verify_nonce( $_GET['duplicate'], $nonce_key ); |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* @filter `gravityview/duplicate-entry/verify_nonce` Override Duplicate Entry nonce validation. Return true to declare nonce valid. |
491
|
|
|
* @since 2.5 |
492
|
|
|
* @see wp_verify_nonce() |
493
|
|
|
* @param int|boolean $valid False if invalid; 1 or 2 when nonce was generated |
494
|
|
|
* @param string $nonce_key Name of nonce action used in wp_verify_nonce. $_GET['duplicate'] holds the nonce value itself. Default: `duplicate_{entry_id}` |
495
|
|
|
*/ |
496
|
1 |
|
$valid = apply_filters( 'gravityview/duplicate-entry/verify_nonce', $valid, $nonce_key ); |
497
|
|
|
|
498
|
1 |
|
return $valid; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Get the onclick attribute for the confirm dialogs that warns users before they duplicate an entry |
503
|
|
|
* |
504
|
|
|
* @since 2.5 |
505
|
|
|
* |
506
|
|
|
* @return string HTML `onclick` attribute |
507
|
|
|
*/ |
508
|
1 |
|
public static function get_confirm_dialog() { |
509
|
|
|
|
510
|
1 |
|
$confirm = __( 'Are you sure you want to duplicate this entry?', 'gravityview' ); |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @filter `gravityview/duplicate-entry/confirm-text` Modify the Duplicate Entry Javascript confirmation text (will be sanitized when output) |
514
|
|
|
* |
515
|
|
|
* @param string $confirm Default: "Are you sure you want to duplicate this entry?". If empty, disable confirmation dialog. |
516
|
|
|
*/ |
517
|
1 |
|
$confirm = apply_filters( 'gravityview/duplicate-entry/confirm-text', $confirm ); |
518
|
|
|
|
519
|
1 |
|
if ( empty( $confirm ) ) { |
520
|
1 |
|
return ''; |
521
|
|
|
} |
522
|
|
|
|
523
|
1 |
|
return 'return window.confirm(\''. esc_js( $confirm ) .'\');'; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Check if the user can edit the entry |
528
|
|
|
* |
529
|
|
|
* - Is the nonce valid? |
530
|
|
|
* - Does the user have the right caps for the entry |
531
|
|
|
* - Is the entry in the trash? |
532
|
|
|
* |
533
|
|
|
* @since 2.5 |
534
|
|
|
* |
535
|
|
|
* @param array $entry Gravity Forms entry array |
536
|
|
|
* @param int $view_id ID of the View being rendered |
537
|
|
|
* |
538
|
|
|
* @return boolean|WP_Error True: can edit form. WP_Error: nope. |
539
|
|
|
*/ |
540
|
1 |
|
private function user_can_duplicate_entry( $entry = array(), $view_id = null ) { |
541
|
|
|
|
542
|
1 |
|
$error = NULL; |
543
|
|
|
|
544
|
1 |
|
if ( ! $this->verify_nonce() ) { |
545
|
|
|
$error = __( 'The link to duplicate this entry is not valid; it may have expired.', 'gravityview' ); |
546
|
|
|
} |
547
|
|
|
|
548
|
1 |
|
if ( ! self::check_user_cap_duplicate_entry( $entry, array(), $view_id ) ) { |
549
|
1 |
|
$error = __( 'You do not have permission to duplicate this entry.', 'gravityview' ); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
// No errors; everything's fine here! |
553
|
1 |
|
if ( empty( $error ) ) { |
554
|
1 |
|
return true; |
555
|
|
|
} |
556
|
|
|
|
557
|
1 |
|
gravityview()->log->error( '{error}', array( 'erorr' => $error ) ); |
558
|
|
|
|
559
|
1 |
|
return new WP_Error( 'gravityview-duplicate-entry-permissions', $error ); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* checks if user has permissions to view the link or duplicate a specific entry |
565
|
|
|
* |
566
|
|
|
* @since 2.5 |
567
|
|
|
* |
568
|
|
|
* @param array $entry Gravity Forms entry array |
569
|
|
|
* @param array $field Field settings (optional) |
570
|
|
|
* @param int $view_id Pass a View ID to check caps against. If not set, check against current View |
571
|
|
|
* |
572
|
|
|
* @return bool |
573
|
|
|
*/ |
574
|
2 |
|
public static function check_user_cap_duplicate_entry( $entry, $field = array(), $view_id = 0 ) { |
575
|
2 |
|
$current_user = wp_get_current_user(); |
576
|
|
|
|
577
|
2 |
|
$entry_id = isset( $entry['id'] ) ? $entry['id'] : null; |
578
|
|
|
|
579
|
|
|
// Or if they can duplicate any entries (as defined in Gravity Forms), we're good. |
580
|
2 |
|
if ( GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gform_full_access', 'gravityview_full_access' ), $entry_id ) ) { |
581
|
|
|
|
582
|
2 |
|
gravityview()->log->debug( 'Current user has `gravityforms_edit_entries` capability.' ); |
583
|
|
|
|
584
|
2 |
|
return true; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
|
588
|
|
|
// If field options are passed, check if current user can view the link |
589
|
2 |
|
if ( ! empty( $field ) ) { |
590
|
|
|
|
591
|
|
|
// If capability is not defined, something is not right! |
592
|
|
|
if ( empty( $field['allow_duplicate_cap'] ) ) { |
593
|
|
|
|
594
|
|
|
gravityview()->log->error( 'Cannot read duplicate entry field caps', array( 'data' => $field ) ); |
595
|
|
|
|
596
|
|
|
return false; |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
if ( GVCommon::has_cap( $field['allow_duplicate_cap'] ) ) { |
600
|
|
|
|
601
|
|
|
// Do not return true if cap is read, as we need to check if the current user created the entry |
602
|
|
|
if ( 'read' !== $field['allow_duplicate_cap'] ) { |
603
|
|
|
return true; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
} else { |
607
|
|
|
|
608
|
|
|
gravityview()->log->debug( 'User {user_id} is not authorized to view duplicate entry link ', array( 'user_id' => $current_user->ID ) ); |
609
|
|
|
|
610
|
|
|
return false; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
} |
614
|
|
|
|
615
|
2 |
|
if ( ! isset( $entry['created_by'] ) ) { |
616
|
|
|
|
617
|
1 |
|
gravityview()->log->error( 'Cannot duplicate entry; entry `created_by` doesn\'t exist.' ); |
618
|
|
|
|
619
|
1 |
|
return false; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
// Only checks user_duplicate view option if view is already set |
623
|
2 |
|
if ( $view_id ) { |
624
|
|
|
|
625
|
1 |
|
if ( ! $view = \GV\View::by_id( $view_id ) ) { |
626
|
|
|
return false; |
627
|
|
|
} |
628
|
|
|
|
629
|
1 |
|
$user_duplicate = $view->settings->get( 'user_duplicate', false ); |
630
|
|
|
|
631
|
1 |
|
if ( empty( $user_duplicate ) ) { |
632
|
|
|
|
633
|
1 |
|
gravityview()->log->debug( 'User Duplicate is disabled. Returning false.' ); |
634
|
|
|
|
635
|
1 |
|
return false; |
636
|
|
|
} |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
// If the logged-in user is the same as the user who created the entry, we're good. |
640
|
2 |
|
if ( is_user_logged_in() && intval( $current_user->ID ) === intval( $entry['created_by'] ) ) { |
641
|
|
|
|
642
|
1 |
|
gravityview()->log->debug( 'User {user_id} created the entry.', array( 'user_id' => $current_user->ID ) ); |
643
|
|
|
|
644
|
1 |
|
return true; |
645
|
|
|
} |
646
|
|
|
|
647
|
2 |
|
return false; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* After processing duplicate entry, the user will be redirected to the referring View or embedded post/page. Display a message on redirection. |
653
|
|
|
* |
654
|
|
|
* If success, there will be `status` URL parameters `status=>success` |
655
|
|
|
* If an error, there will be `status` and `message` URL parameters `status=>error&message=example` |
656
|
|
|
* |
657
|
|
|
* @since 2.5 |
658
|
|
|
* |
659
|
|
|
* @param int $current_view_id The ID of the View being rendered |
660
|
|
|
* |
661
|
|
|
* @return void |
662
|
|
|
*/ |
663
|
37 |
|
public function maybe_display_message( $current_view_id = 0 ) { |
664
|
37 |
|
if ( empty( $_GET['status'] ) || ! self::verify_nonce() ) { |
665
|
37 |
|
return; |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
// Entry wasn't duplicated from current View |
669
|
|
|
if ( isset( $_GET['view_id'] ) && ( intval( $_GET['view_id'] ) !== intval( $current_view_id ) ) ) { |
670
|
|
|
return; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
$this->display_message(); |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
public function display_message() { |
677
|
|
|
if ( empty( $_GET['status'] ) || empty( $_GET['duplicate'] ) ) { |
678
|
|
|
return; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
$status = esc_attr( $_GET['status'] ); |
682
|
|
|
$message_from_url = \GV\Utils::_GET( 'message' ); |
683
|
|
|
$message_from_url = rawurldecode( stripslashes_deep( $message_from_url ) ); |
684
|
|
|
$class = ''; |
685
|
|
|
|
686
|
|
|
switch ( $status ) { |
687
|
|
|
case 'error': |
688
|
|
|
$class = ' gv-error error'; |
689
|
|
|
$error_message = __( 'There was an error duplicating the entry: %s', 'gravityview' ); |
690
|
|
|
$message = sprintf( $error_message, $message_from_url ); |
691
|
|
|
break; |
692
|
|
|
default: |
693
|
|
|
$message = __( 'The entry was successfully duplicated.', 'gravityview' ); |
694
|
|
|
break; |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* @filter `gravityview/duplicate-entry/message` Modify the Duplicate Entry messages. Allows HTML; will not be further sanitized. |
699
|
|
|
* @since 2.5 |
700
|
|
|
* @param string $message Message to be displayed, sanitized using esc_attr() |
701
|
|
|
* @param string $status Message status (`error` or `success`) |
702
|
|
|
* @param string $message_from_url The original error message, if any, without the "There was an error duplicating the entry:" prefix |
703
|
|
|
*/ |
704
|
|
|
$message = apply_filters( 'gravityview/duplicate-entry/message', esc_attr( $message ), $status, $message_from_url ); |
705
|
|
|
|
706
|
|
|
// DISPLAY ERROR/SUCCESS MESSAGE |
707
|
|
|
echo '<div class="gv-notice' . esc_attr( $class ) .'">'. $message .'</div>'; |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* Add a Duplicate link to the row of actions on the entry list in the backend. |
712
|
|
|
* |
713
|
|
|
* @since 2.5.1 |
714
|
|
|
* |
715
|
|
|
* @param int $form_id The form ID. |
716
|
|
|
* @param int $field_id The field ID. |
717
|
|
|
* @param string $value The value. |
718
|
|
|
* @param array $entry The entryvalue The value. |
719
|
|
|
* @param array $entry The entry. |
720
|
|
|
* @param string $query_string The query. |
721
|
|
|
* |
722
|
|
|
* @return void |
723
|
|
|
*/ |
724
|
|
|
public function make_duplicate_link_row( $form_id, $field_id, $value, $entry, $query_string ) { |
|
|
|
|
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* @filter `gravityview/duplicate/backend/enable` Allows developers to disable the duplicate link on the backend. |
728
|
|
|
* @param[in,out] boolean $enable True by default. Enabled. |
729
|
|
|
* @param int $form_id The form ID. |
730
|
|
|
*/ |
731
|
|
|
if ( ! apply_filters( 'gravityview/duplicate/backend/enable', true, $form_id ) ) { |
732
|
|
|
return; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
?> |
736
|
|
|
<span class="duplicate"> |
737
|
|
|
| |
738
|
|
|
<a href="<?php echo wp_nonce_url( add_query_arg( 'entry_id', $entry['id'] ), self::get_nonce_key( $entry['id'] ), 'duplicate' ); ?>"><?php esc_html_e( 'Duplicate', 'gravityview' ); ?></a> |
739
|
|
|
</span> |
740
|
|
|
<?php |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* Perhaps duplicate this entry if the action has been corrected. |
745
|
|
|
* |
746
|
|
|
* @since 2.5.1 |
747
|
|
|
* |
748
|
|
|
* @param int $form_id The form ID. |
749
|
|
|
* |
750
|
|
|
* @return void |
751
|
|
|
*/ |
752
|
|
|
public function maybe_duplicate_list( $form_id ) { |
|
|
|
|
753
|
|
|
|
754
|
|
|
if ( ! is_admin() ) { |
755
|
|
|
return; |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
if ( 'success' === \GV\Utils::_GET( 'result' ) ) { |
759
|
|
|
add_filter( 'gform_admin_messages', function( $messages ) { |
760
|
|
|
$messages = (array) $messages; |
761
|
|
|
|
762
|
|
|
$messages[] = esc_html__( 'Entry duplicated.', 'gravityview' ); |
763
|
|
|
return $messages; |
764
|
|
|
} ); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
if ( 'error' === \GV\Utils::_GET( 'result' ) ) { |
768
|
|
|
|
769
|
|
|
$is_logging_active = class_exists( 'GravityView_Logging' ) ? GravityView_Logging::is_logging_active() : true; |
770
|
|
|
|
771
|
|
|
$check_logs_message = ''; |
772
|
|
|
|
773
|
|
|
if( $is_logging_active ) { |
774
|
|
|
$check_logs_message = sprintf( ' <a href="%s">%s</a>', |
775
|
|
|
esc_url( admin_url( 'admin.php?page=gf_settings&subview=gravityformslogging' ) ), |
776
|
|
|
esc_html_x( 'Check the GravityView logs for more information.', 'Error message links to logging page', 'gravityview' ) |
777
|
|
|
); |
778
|
|
|
} |
779
|
|
|
|
780
|
|
|
add_filter( 'gform_admin_error_messages', function( $messages ) use ( $check_logs_message ) { |
781
|
|
|
$messages = (array) $messages; |
782
|
|
|
|
783
|
|
|
$messages[] = esc_html__( 'There was an error duplicating the entry.', 'gravityview' ) . $check_logs_message; |
784
|
|
|
|
785
|
|
|
return $messages; |
786
|
|
|
} ); |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
if ( ! wp_verify_nonce( \GV\Utils::_GET( 'duplicate' ), self::get_nonce_key( $entry_id = \GV\Utils::_GET( 'entry_id' ) ) ) ) { |
790
|
|
|
return; |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
if ( ! GVCommon::has_cap( array( 'gravityforms_edit_entries', 'gform_full_access', 'gravityview_full_access' ), $entry_id ) ) { |
794
|
|
|
return; |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
$entry = GFAPI::get_entry( $entry_id ); |
798
|
|
|
|
799
|
|
|
if ( is_wp_error( $entry ) ) { |
800
|
|
|
$is_duplicated = $entry; |
801
|
|
|
} else { |
802
|
|
|
$is_duplicated = $this->duplicate_entry( $entry ); |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
if ( is_wp_error( $is_duplicated ) ) { |
806
|
|
|
gravityview()->log->error( 'Error duplicating {id}: {error}', array( 'id' => $entry_id, 'error' => $is_duplicated->get_error_message() ) ); |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
$return_url = remove_query_arg( 'duplicate' ); |
810
|
|
|
$return_url = add_query_arg( 'result', is_wp_error( $is_duplicated ) ? 'error' : 'success', $return_url ); |
811
|
|
|
|
812
|
|
|
echo '<script>window.location.href = ' . json_encode( $return_url ) . ';</script>'; |
813
|
|
|
|
814
|
|
|
exit; |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
|
818
|
|
|
} // end class |
819
|
|
|
|
820
|
|
|
GravityView_Duplicate_Entry::getInstance(); |
821
|
|
|
|
822
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.