Skip to content

Commit 43284e8

Browse files
committed
Updated methods to be reusable
1 parent 0f989c5 commit 43284e8

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

includes/class-ajax.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public function save_form() {
133133
$form_fields = weforms()->form->save( $data );
134134

135135
// Update Old Entry meta_key if changed
136-
$form_id = $form_data['wpuf_form_id'];
137-
$form = weforms()->form->get( $form_id );
136+
$form_id = $form_data['wpuf_form_id'];
137+
$form = weforms()->form->get( $form_id );
138138

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

includes/class-form.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -378,30 +378,15 @@ public function entries() {
378378
*/
379379

380380
public function maybe_update_entries( $form_id, $form_fields ) {
381-
global $wpdb;
382381
$changed_fields = $this->get_changed_fields( $form_fields );
383-
384-
error_log("Fields to change" . " = " . print_r($changed_fields,1));
385-
386-
return;
387-
$entries = weforms_get_form_entries($form_id);
388-
// if ($field_to_change == " ") {
389-
// return;
390-
// }
391-
foreach ( $entries as $entry ) {
392-
$entry_id = $entry->id;
393-
$values = weforms_get_entry_meta( $entry_id );
394-
$keys = array_keys($values);
395-
foreach ( $keys as $key ) {
396-
if ( $key == $field_to_change) {
397-
$update_keys = $wpdb->update( $wpdb->weforms_entrymeta, array( 'meta_key' => $new_name ), array( 'meta_key' => $key, 'weforms_entry_id' => $entry_id ) );
398-
}
399-
}
382+
// Loop through changed fields and update entries
383+
foreach ( $changed_fields as $old => $new) {
384+
$updated_fields = $this->rename_field($old, $new, $form_id );
400385
}
401386
}
402387

403388
/**
404-
* Get changed fields of form on Save
389+
* Get changed fields of a form
405390
* @since 1.6.9
406391
* @param int $form_id
407392
* @param array $form_fields
@@ -410,6 +395,7 @@ public function maybe_update_entries( $form_id, $form_fields ) {
410395
*/
411396
public function get_changed_fields( $form_fields ) {
412397
$changed_fields = array();
398+
// Loop through form fields
413399
foreach ($form_fields as $field) {
414400
if ( array_key_exists( 'original_name', $field ) ) {
415401
$old_name = $field['original_name'];
@@ -419,12 +405,32 @@ public function get_changed_fields( $form_fields ) {
419405
$new_name = $field['name'];
420406
}
421407
if ($new_name != $old_name) {
422-
$changed_fields[] = $field['original_name'];
408+
$changed_fields[$field['original_name']] = $field['name'];
423409
} else {
424410
continue;
425411
}
426412
}
413+
// Return array of changed fields
427414
return $changed_fields;
415+
416+
}
417+
/**
418+
* Rename fields of a form in entry
419+
* @since 1.6.9
420+
* @param int $form_id
421+
* @param array $form_fields
422+
*
423+
* @return array
424+
*/
425+
public function rename_field ( $old, $new, $form_id ) {
426+
global $wpdb;
427+
$entries = weforms_get_form_entries($form_id);
428+
// Update entries with changed fields
429+
foreach ( $entries as $entry ) {
430+
$entry_id = $entry->id;
431+
$values = weforms_get_entry_meta( $entry_id );
432+
$update_keys = $wpdb->update( $wpdb->weforms_entrymeta, array( 'meta_key' => $new ), array( 'meta_key' => $old, 'weforms_entry_id' => $entry_id ) );
433+
}
428434
}
429435

430436
/**

0 commit comments

Comments
 (0)