Skip to content

Commit e56146b

Browse files
committed
Reduced complexity in update entry method
1 parent 43284e8 commit e56146b

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

includes/class-ajax.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function save_form() {
136136
$form_id = $form_data['wpuf_form_id'];
137137
$form = weforms()->form->get( $form_id );
138138

139-
$form->maybe_update_entries( $form_id, $form_fields );
139+
$form->maybe_update_entries( $form_fields );
140140

141141
do_action( 'weforms_update_form', $form_data['wpuf_form_id'], $form_fields, $settings );
142142

includes/class-form.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -371,61 +371,68 @@ public function entries() {
371371
}
372372

373373
/**
374-
* Update entries of form on Save
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+
*
375377
* @since 1.6.9
378+
*
376379
* @param int $form_id
377380
* @param array $form_fields
378381
*/
379-
380-
public function maybe_update_entries( $form_id, $form_fields ) {
382+
public function maybe_update_entries( $form_fields ) {
381383
$changed_fields = $this->get_changed_fields( $form_fields );
382384
// Loop through changed fields and update entries
383385
foreach ( $changed_fields as $old => $new) {
384-
$updated_fields = $this->rename_field($old, $new, $form_id );
386+
$updated_fields = $this->rename_field( $old, $new );
385387
}
386388
}
387389

388390
/**
389-
* Get changed fields of a form
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+
*
390394
* @since 1.6.9
395+
*
391396
* @param int $form_id
392397
* @param array $form_fields
393398
*
394399
* @return array
395400
*/
396401
public function get_changed_fields( $form_fields ) {
397402
$changed_fields = array();
398-
// Loop through form fields
399-
foreach ($form_fields as $field) {
400-
if ( array_key_exists( 'original_name', $field ) ) {
401-
$old_name = $field['original_name'];
402-
$new_name = $field['name'];
403-
} else {
404-
$old_name = $field['name'];
405-
$new_name = $field['name'];
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;
406409
}
407-
if ($new_name != $old_name) {
410+
if ( $field['name'] !== $field['original_name'] ) {
408411
$changed_fields[$field['original_name']] = $field['name'];
409412
} else {
410413
continue;
411414
}
412415
}
413-
// Return array of changed fields
414416
return $changed_fields;
415417

416418
}
419+
417420
/**
418-
* Rename fields of a form in entry
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+
*
419424
* @since 1.6.9
425+
*
420426
* @param int $form_id
421427
* @param array $form_fields
422428
*
423429
* @return array
424430
*/
425-
public function rename_field ( $old, $new, $form_id ) {
431+
public function rename_field ( $old, $new ) {
426432
global $wpdb;
427-
$entries = weforms_get_form_entries($form_id);
428-
// Update entries with changed fields
433+
434+
$entries = weforms_get_form_entries( $this->id );
435+
429436
foreach ( $entries as $entry ) {
430437
$entry_id = $entry->id;
431438
$values = weforms_get_entry_meta( $entry_id );

0 commit comments

Comments
 (0)