Edit Entry
Edit the entry that was passed through via GP Easy Passthrough rather than creating a new entry.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Read the Walkthrough
Code
Filename: gpep-edit-entry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* Gravity Perks // Easy Passthrough // Edit Entry
* https://gravitywiz.com/edit-gravity-forms-entries-on-the-front-end/
*
* Edit the entry that was passed through via GP Easy Passthrough rather than creating a new entry.
*
* Plugin Name: GP Easy Passthrough — Edit Entry
* Plugin URI: https://gravitywiz.com/edit-gravity-forms-entries-on-the-front-end/
* Description: Edit the entry that was passed through via GP Easy Passthrough rather than creating a new entry.
* Author: Gravity Wiz
* Version: 1.4.3
* Author URI: https://gravitywiz.com/
*/
class GPEP_Edit_Entry {
private $form_id;
private $delete_partial;
private $passed_through_entries;
private $refresh_token;
private $process_feeds;
public function __construct( $options ) {
if ( ! function_exists( 'rgar' ) ) {
return;
}
$this->form_id = rgar( $options, 'form_id' );
$this->delete_partial = rgar( $options, 'delete_partial', true );
$this->refresh_token = rgar( $options, 'refresh_token', false );
$this->process_feeds = rgar( $options, 'process_feeds', false );
add_filter( "gpep_form_{$this->form_id}", array( $this, 'capture_passed_through_entry_ids' ), 10, 3 );
add_filter( "gform_entry_id_pre_save_lead_{$this->form_id}", array( $this, 'update_entry_id' ), 10, 2 );
add_filter( "gform_entry_post_save_{$this->form_id}", array( $this, 'delete_values_for_conditionally_hidden_fields' ), 10, 2 );
// Enable edit view in GP Inventory.
add_filter( "gpi_is_edit_view_{$this->form_id}", '__return_true' );
// Bypass limit submissions on validation
add_filter( 'gform_validation', array( $this, 'bypass_limit_submission_validation' ) );
add_filter( "gpi_query_{$this->form_id}", array( $this, 'exclude_edit_entry_from_inventory' ), 10, 2 );
// If we need to reprocess any feeds on 'edit'.
add_filter( 'gform_entry_post_save', array( $this, 'process_feeds' ), 10, 2 );
}
public function capture_passed_through_entry_ids( $form, $values, $passed_through_entries ) {
// Save a runtime cache for use when releasing inventory reserved by the entry being edited.
$this->passed_through_entries = $passed_through_entries;
if ( empty( $passed_through_entries ) ) {
return $form;
}
// Add hidden input to capture entry IDs passed through via GPEP.
add_filter( "gform_form_tag_{$form['id']}", function( $form_tag, $form ) use ( $passed_through_entries ) {
$entry_ids = implode( ',', wp_list_pluck( $passed_through_entries, 'entry_id' ) );
$hash = wp_hash( $entry_ids );
$value = sprintf( '%s|%s', $entry_ids, $hash );
$input = sprintf( '<input type="hidden" name="%s" value="%s">', $this->get_passed_through_entries_input_name( $form['id'] ), $value );
$form_tag .= $input;
return $form_tag;
}, 10, 2 );
add_filter( "gpls_rule_groups_{$this->form_id}", function( $rule_groups, $form_id ) use ( $passed_through_entries ) {
// Bypass GPLS if we're updating an entry.
if ( ! empty( $passed_through_entries ) ) {
$rule_groups = array();
}
return $rule_groups;
}, 10, 2 );
return $form;
}
public function bypass_limit_submission_validation( $validation_result ) {
$edit_entry_id = $this->get_edit_entry_id( rgars( $validation_result, 'form/id' ) );
if ( ! $edit_entry_id ) {
return $validation_result;
}
add_filter( "gpls_rule_groups_{$this->form_id}", function( $rule_groups, $form_id ) use ( $edit_entry_id ) {
return array();
}, 10, 2 );
return $validation_result;
}
public function update_entry_id( $entry_id, $form ) {
$update_entry_id = $this->get_edit_entry_id( $form['id'] );
if ( $update_entry_id ) {
// Purge product info cache
$this->purge_product_cache( $form, GFAPI::get_entry( $update_entry_id ) );
if ( $this->delete_partial
&& is_callable( array( 'GF_Partial_Entries', 'get_instance' ) )
&& $entry_id !== null
&& ! empty( GF_Partial_Entries::get_instance()->get_active_feeds( $form['id'] ) )
) {
GFAPI::delete_entry( $entry_id );
}
if ( $this->refresh_token ) {
gform_delete_meta( $update_entry_id, 'fg_easypassthrough_token' );
gp_easy_passthrough()->get_entry_token( $update_entry_id );
// Remove entry from the session and prevent Easy Passthrough from resaving it.
$session = gp_easy_passthrough()->session_manager();
$session[ gp_easy_passthrough()->get_slug() . '_' . $form['id'] ] = null;
remove_action( 'gform_after_submission', array( gp_easy_passthrough(), 'store_entry_id' ) );
}
return $update_entry_id;
}
return $entry_id;
}
/**
* Delete values that exist for the entry in the database for fields that are now conditionally hidden.
*
* If we find any instance where a conditionally hidden field has a value, we'll update the DB with the passed entry,
* which was just submitted and will not contain conditionally hidden values.
*
* Note: There's a good case for us to simply call GFAPI::update_entry() with the passed entry without all the other
* fancy logic to that only makes the call if it identifies a conditionally hidden field with a DB value. A thought
* for future us.
*
* @param $entry
* @param $form
*
* @return mixed
*/
public function delete_values_for_conditionally_hidden_fields( $entry, $form ) {
// We'll only update the entry if we identify a field value that needs to be deleted.
$has_change = false;
// The passed entry does not reflect what is actually in the database.
$db_entry = null;
/**
* @var \GF_Field $field
*/
foreach ( $form['fields'] as $field ) {
if ( ! GFFormsModel::is_field_hidden( $form, $field, array(), $entry ) ) {
continue;
}
if ( ! $db_entry ) {
$db_entry = GFAPI::get_entry( $entry['id'] );
}
$inputs = $field->get_entry_inputs();
if ( ! $inputs ) {
$inputs = array(
array(
'id' => $field->id,
),
);
}
foreach ( $inputs as $input ) {
if ( ! empty( $db_entry[ $input['id'] ] ) ) {
$has_change = true;
break 2;
}
}
}
if ( $has_change ) {
GFAPI::update_entry( $entry );
}
return $entry;
}
public function get_passed_through_entries_input_name( $form_id ) {
return "gpepee_passed_through_entries_{$form_id}";
}
public function get_passed_through_entry_ids( $form_id ) {
$entry_ids = array();
if ( ! empty( $_POST ) ) {
$posted_value = rgpost( $this->get_passed_through_entries_input_name( $form_id ) );
if ( empty( $posted_value ) ) {
return $entry_ids;
}
list( $entry_ids, $hash ) = explode( '|', $posted_value );
if ( $hash !== wp_hash( $entry_ids ) ) {
return $entry_ids;
}
$entry_ids = explode( ',', $entry_ids );
} elseif ( ! empty( $this->passed_through_entries ) ) {
$entry_ids = wp_list_pluck( $this->passed_through_entries, 'entry_id' );
}
return $entry_ids;
}
public function get_edit_entry_id( $form_id ) {
$entry_ids = $this->get_passed_through_entry_ids( $form_id );
$entry_id = array_shift( $entry_ids );
/**
* Filter the ID that will be used to fetch assign the entry to be edited.
*
* @since 1.3
*
* @param int|bool $edit_entry_id The ID of the entry to be edited.
* @param int $form_id The ID of the form that was submitted.
*/
return gf_apply_filters( array( 'gpepee_edit_entry_id', $form_id ), $entry_id, $form_id );
}
/**
* Exclude the entry being edited in GravityView from inventory counts.
*
* Without this, you can't reselect choices that the current entry has consumed.
*/
public function exclude_edit_entry_from_inventory( $query, $field ) {
global $wpdb;
$entry_ids = $this->get_passed_through_entry_ids( $field->formId );
// @todo Update to work with multiple passed through entries.
$current_entry_id = array_pop( $entry_ids );
if ( ! $current_entry_id ) {
return $query;
}
$query['where'] .= $wpdb->prepare( "\nAND em.entry_id != %d", $current_entry_id );
return $query;
}
public static function purge_product_cache( $form, $entry ) {
$cache_options = array(
array( false, false ),
array( false, true ),
array( true, false ),
array( true, true ),
);
foreach ( $cache_options as $cache_option ) {
list( $use_choice_text, $use_admin_label ) = $cache_option;
if ( gform_get_meta( rgar( $entry, 'id' ), "gform_product_info_{$use_choice_text}_{$use_admin_label}" ) ) {
gform_delete_meta( rgar( $entry, 'id' ), "gform_product_info_{$use_choice_text}_{$use_admin_label}" );
}
}
}
public function process_feeds( $entry, $form ) {
if ( ! $this->process_feeds ) {
return $entry;
}
/**
* Disable asynchronous feed process on edit otherwise async feeds will not be re-ran due to a check in
* class-gf-feed-processor.php that checks `gform_get_meta( $entry_id, 'processed_feeds' )` and there isn't
* a way to bypass it.
*/
$filter_priority = rand( 100000, 999999 );
add_filter( 'gform_is_feed_asynchronous', '__return_false', $filter_priority );
foreach ( GFAddOn::get_registered_addons( true ) as $addon ) {
if ( method_exists( $addon, 'maybe_process_feed' ) && ( $this->process_feeds === true || strpos( $this->process_feeds, $addon->get_slug() ) !== false ) ) {
$addon->maybe_process_feed( $entry, $form );
}
}
remove_filter( 'gform_is_feed_asynchronous', '__return_false', $filter_priority );
return $entry;
}
}
// Configurations
new GPEP_Edit_Entry( array(
'form_id' => 123, // Set this to the form ID.
'delete_partial' => false, // Set this to false if you wish to preserve partial entries after an edit is submitted.
'refresh_token' => false, // Set this to true to generate a fresh Easy Passthrough token after updating an entry.
'process_feeds' => false, // Set this to true to process all feed addons on Edit Entry, or provide a comma separated list of addon slugs like 'gravityformsuserregistration', etc.
) );