Skip to content

Commit 775835d

Browse files
fix: use fetch for saving meta group fields
1 parent 2af426c commit 775835d

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

inc/admin.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ function ppom_admin_save_form_meta() {
275275
wp_send_json( $resp );
276276
}
277277

278-
$_REQUEST['ppom'] = is_array( $_REQUEST['ppom'] ) ? $_REQUEST['ppom'] : json_decode( wp_unslash( $_REQUEST['ppom'] ), true );
279-
280278
global $wpdb;
281279

282280
extract( $_REQUEST );
@@ -450,8 +448,6 @@ function ppom_admin_update_form_meta() {
450448

451449
wp_send_json( $resp );
452450
}
453-
$_REQUEST['ppom'] = is_array( $_REQUEST['ppom'] ) ? $_REQUEST['ppom'] : json_decode( wp_unslash( $_REQUEST['ppom'] ), true );
454-
455451
global $wpdb;
456452

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

js/admin/ppom-admin.js

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

130130
$('.ppom-unsave-data').remove();
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-
137-
data.ppom = JSON.stringify(data.ppom);
138-
139-
// Send the JSON data via POST request
140-
$.ajax({
141-
url: ajaxurl,
142-
data: data, // Send as regular object (no need to stringify)
143-
type: 'POST',
144-
success: function(resp) {
145-
146-
const bg_color = resp.status == 'success' ? '#4e694859' : '#ee8b94';
147-
jQuery(".ppom-meta-save-notice").html(resp.message).css({ 'background-color': bg_color, 'padding': '8px', 'border-left': '5px solid #008c00' });
148-
if (resp.status == 'success') {
149-
if (resp.redirect_to != '') {
150-
window.location = resp.redirect_to;
151-
}
152-
else {
153-
window.location.reload(true);
154-
}
131+
132+
fetch(ajaxurl, {
133+
method: 'POST',
134+
body: new FormData(this)
135+
})
136+
.then(response => response.json())
137+
.then(resp => {
138+
const bg_color = resp.status == 'success' ? '#4e694859' : '#ee8b94';
139+
jQuery(".ppom-meta-save-notice").html(resp.message).css({ 'background-color': bg_color, 'padding': '8px', 'border-left': '5px solid #008c00' });
140+
if (resp.status == 'success') {
141+
if (resp.redirect_to != '') {
142+
window.location = resp.redirect_to;
143+
} else {
144+
window.location.reload();
155145
}
156-
},
157-
error: function() {
158-
// Handle error
159-
jQuery(".ppom-meta-save-notice").html("An error occurred. Please try again.").css({ 'background-color': '#ee8b94', 'padding': '8px', 'border-left': '5px solid #c00' });
160146
}
147+
})
148+
.catch(() => {
149+
jQuery(".ppom-meta-save-notice").html("An error occurred. Please try again.").css({ 'background-color': '#ee8b94', 'padding': '8px', 'border-left': '5px solid #c00' });
161150
});
162-
163151
});
164152

165153

0 commit comments

Comments
 (0)