Skip to content

Commit b4c74fe

Browse files
committed
Fixes Entry updates when form in saved. Fixes #62
1 parent dbd52b8 commit b4c74fe

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

includes/class-ajax.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ 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+
$update_entries = $form->update_entries( $form_id, $form_fields );
139+
140+
141+
//error_log(print_r($form_fields,1));
142+
135143
do_action( 'weforms_update_form', $form_data['wpuf_form_id'], $form_fields, $settings );
136144

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

includes/class-form.php

Lines changed: 42 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 for updating old 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,45 @@ public function entries() {
367370
return new WeForms_Form_Entry_Manager( $this->id, $this );
368371
}
369372

373+
/**
374+
* Update entries of this form on Save
375+
*
376+
*
377+
*/
378+
379+
public function update_entries( $form_id, $form_fields ) {
380+
global $wpdb;
381+
foreach ($form_fields as $field) {
382+
$old_name = $field['original_name'];
383+
$new_name = $field['name'];
384+
if ($new_name != $old_name) {
385+
$field_to_change = $field['original_name'];
386+
} else {
387+
$field_to_change = " ";
388+
}
389+
}
390+
$entries = weforms_get_form_entries($form_id);
391+
error_log("Entries" . " = " . print_r($entries,1));
392+
error_log("Fields = " . print_r($field_to_change,1));
393+
if ($field_to_change == " ") {
394+
return;
395+
}
396+
foreach ( $entries as $entry ) {
397+
398+
$entry_id = $entry->id;
399+
$values = weforms_get_entry_meta( $entry_id );
400+
$keys = array_keys($values);
401+
error_log("keys" . " = " . print_r($keys,1));
402+
foreach ( $keys as $key ) {
403+
if ( $key == $field_to_change) {
404+
$update_keys = $wpdb->update( $wpdb->weforms_entrymeta, array( 'meta_key' => $new_name ), array( 'meta_key' => $key, 'weforms_entry_id' => $entry_id ) );
405+
error_log("Key" . " = " . print_r($key,1));
406+
error_log("keys will be changed");
407+
}
408+
}
409+
}
410+
}
411+
370412
/**
371413
* Get number of form entries
372414
*

0 commit comments

Comments
 (0)