Skip to content

Commit 1bf370a

Browse files
authored
Merge pull request #405 from Codeinwp/fix/meta-reorder
fix: add fields re-ordering for JSON payload
2 parents 8bb600f + 243d4b8 commit 1bf370a

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

inc/admin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ function ppom_admin_save_form_meta() {
276276
}
277277

278278
$_REQUEST['ppom'] = is_array( $_REQUEST['ppom'] ) ? $_REQUEST['ppom'] : json_decode( wp_unslash( $_REQUEST['ppom'] ), true );
279+
279280
global $wpdb;
280281

281282
extract( $_REQUEST );
@@ -453,6 +454,7 @@ function ppom_admin_update_form_meta() {
453454
wp_send_json( $resp );
454455
}
455456
$_REQUEST['ppom'] = is_array( $_REQUEST['ppom'] ) ? $_REQUEST['ppom'] : json_decode( wp_unslash( $_REQUEST['ppom'] ), true );
457+
456458
global $wpdb;
457459

458460
$ppom_meta = isset( $_REQUEST['ppom_meta'] ) ? $_REQUEST['ppom_meta'] : $_REQUEST['ppom'];

js/admin/ppom-admin.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,14 @@ jQuery(function($) {
128128
jQuery(".ppom-meta-save-notice").html('<img src="' + ppom_vars.loader + '">').show();
129129

130130
$('.ppom-unsave-data').remove();
131-
var data = $(this).serializeJSON();
131+
const data = $(this).serializeJSON();
132+
133+
const fieldsOrder = Array(...document.querySelectorAll('.ui-sortable-handle[id^="ppom_sort_id_"]'))
134+
.map( node => node.id.replace('ppom_sort_id_', '') ); // ['2', '3']
135+
data.ppom = fieldsOrder.map( fieldId => data.ppom[fieldId] );
136+
132137
data.ppom = JSON.stringify(data.ppom);
138+
133139
// Send the JSON data via POST request
134140
$.ajax({
135141
url: ajaxurl,
@@ -329,7 +335,7 @@ jQuery(function($) {
329335
placeholder = '-';
330336
}
331337

332-
var html = '<tr class="row_no_' + id + '" id="ppom_sort_id_' + id + '">';
338+
var html = '<tr class="row_no_' + id + ' ui-sortable-handle" id="ppom_sort_id_' + id + '">';
333339
html += '<td class="ppom-sortable-handle"><i class="fa fa-arrows" aria-hidden="true"></i></td>';
334340
html += '<td class="ppom-check-one-field ppom-checkboxe-style">';
335341
html += '<label>';

0 commit comments

Comments
 (0)