Skip to content

Commit 8f351b6

Browse files
authored
Merge pull request #118 from BoldGrid/issue-62
Fixes Entry updates when form in saved. Fixes #62
2 parents 6640671 + e56146b commit 8f351b6

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

includes/class-ajax.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ public function save_form() {
132132

133133
$form_fields = weforms()->form->save( $data );
134134

135+
// Update Old Entry meta_key if changed
136+
$form_id = $form_data['wpuf_form_id'];
137+
$form = weforms()->form->get( $form_id );
138+
139+
$form->maybe_update_entries( $form_fields );
140+
135141
do_action( 'weforms_update_form', $form_data['wpuf_form_id'], $form_fields, $settings );
136142

137143
wp_send_json_success( [ 'form_fields' => $form_fields ] );

includes/class-form.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public function get_fields() {
139139
$field['recaptcha_theme'] = isset( $field['recaptcha_theme'] ) ? $field['recaptcha_theme'] : 'light';
140140
}
141141

142+
// Check if meta_key has changed when saving form compared current entries
143+
$field['original_name'] = $field['name'];
144+
142145
$form_fields[] = apply_filters( 'weforms-get-form-field', $field, $this->id );
143146
}
144147

@@ -367,6 +370,76 @@ public function entries() {
367370
return new WeForms_Form_Entry_Manager( $this->id, $this );
368371
}
369372

373+
/**
374+
* When a user is editing their form they may change a fields name.
375+
* This method will loop through existing entries to match the new field names.
376+
*
377+
* @since 1.6.9
378+
*
379+
* @param int $form_id
380+
* @param array $form_fields
381+
*/
382+
public function maybe_update_entries( $form_fields ) {
383+
$changed_fields = $this->get_changed_fields( $form_fields );
384+
// Loop through changed fields and update entries
385+
foreach ( $changed_fields as $old => $new) {
386+
$updated_fields = $this->rename_field( $old, $new );
387+
}
388+
}
389+
390+
/**
391+
* When a user is editing their form they may change a fields name.
392+
* This method will loop through all fields that have changed.
393+
*
394+
* @since 1.6.9
395+
*
396+
* @param int $form_id
397+
* @param array $form_fields
398+
*
399+
* @return array
400+
*/
401+
public function get_changed_fields( $form_fields ) {
402+
$changed_fields = array();
403+
foreach ( $form_fields as $field ) {
404+
$org_field = $field['original_name'];
405+
error_log("Org Field" . " = " . print_r($org_field,1));
406+
// All form fields should have an original name.
407+
if ( empty( $field['original_name'] ) ) {
408+
continue;
409+
}
410+
if ( $field['name'] !== $field['original_name'] ) {
411+
$changed_fields[$field['original_name']] = $field['name'];
412+
} else {
413+
continue;
414+
}
415+
}
416+
return $changed_fields;
417+
418+
}
419+
420+
/**
421+
* When a user changes the field names of a form, the existing entries will need updated.
422+
* This method will loop through the existing entries and update them will the new names.
423+
*
424+
* @since 1.6.9
425+
*
426+
* @param int $form_id
427+
* @param array $form_fields
428+
*
429+
* @return array
430+
*/
431+
public function rename_field ( $old, $new ) {
432+
global $wpdb;
433+
434+
$entries = weforms_get_form_entries( $this->id );
435+
436+
foreach ( $entries as $entry ) {
437+
$entry_id = $entry->id;
438+
$values = weforms_get_entry_meta( $entry_id );
439+
$update_keys = $wpdb->update( $wpdb->weforms_entrymeta, array( 'meta_key' => $new ), array( 'meta_key' => $old, 'weforms_entry_id' => $entry_id ) );
440+
}
441+
}
442+
370443
/**
371444
* Get number of form entries
372445
*

0 commit comments

Comments
 (0)