Skip to content

Commit 93cb728

Browse files
authored
Merge pull request #410 from Codeinwp/fix/options-reordering
fix: use fetch for saving meta group fields
2 parents 02ccb99 + dc3925b commit 93cb728

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

inc/admin.php

Lines changed: 17 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 );
@@ -285,6 +283,12 @@ function ppom_admin_save_form_meta() {
285283
$aviary_api_key = 'NA';
286284
$show_cart_thumb = 'NA';
287285

286+
if ( is_string( $_REQUEST['ppom'] ) ) {
287+
$ppom_encoded = $_REQUEST['ppom'];
288+
parse_str( $ppom_encoded, $ppom_decoded);
289+
$_REQUEST['ppom'] = $ppom_decoded['ppom'];
290+
}
291+
288292
$ppom_meta = ( isset($_REQUEST['ppom_meta']) ? $_REQUEST['ppom_meta'] : isset($_REQUEST['ppom']) ) ? $_REQUEST['ppom'] : '';
289293

290294
if ( empty( $ppom_meta ) ) {
@@ -358,6 +362,11 @@ function( $pm ) {
358362

359363

360364
$ppom_id = $wpdb->insert_id;
365+
if ( is_string( $ppom ) ) {
366+
$ppom_encoded = $ppom;
367+
parse_str( $ppom_encoded, $ppom_decoded);
368+
$ppom = $ppom_decoded['ppom'];
369+
}
361370

362371
$product_meta = apply_filters( 'ppom_meta_data_saving', (array) $ppom, $ppom_id );
363372
$product_meta = ppom_sanitize_array_data( $product_meta );
@@ -450,10 +459,14 @@ function ppom_admin_update_form_meta() {
450459

451460
wp_send_json( $resp );
452461
}
453-
$_REQUEST['ppom'] = is_array( $_REQUEST['ppom'] ) ? $_REQUEST['ppom'] : json_decode( wp_unslash( $_REQUEST['ppom'] ), true );
454-
455462
global $wpdb;
456463

464+
if ( is_string( $_REQUEST['ppom'] ) ) {
465+
$ppom_encoded = $_REQUEST['ppom'];
466+
parse_str( $ppom_encoded, $ppom_decoded);
467+
$_REQUEST['ppom'] = $ppom_decoded['ppom'];
468+
}
469+
457470
$ppom_meta = isset( $_REQUEST['ppom_meta'] ) ? $_REQUEST['ppom_meta'] : $_REQUEST['ppom'];
458471
$product_meta = apply_filters( 'ppom_meta_data_saving', (array) $ppom_meta, $productmeta_id );
459472
$product_meta = ppom_sanitize_array_data( $product_meta );

js/admin/ppom-admin.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -128,38 +128,40 @@ 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] );
131+
132+
const formData = new FormData();
133+
const ppomFields = new URLSearchParams();
136134

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-
}
155-
}
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' });
135+
// NOTE: since the request is to big for small values of `max_input_vars`, we will send the PPOM fields as a single string.
136+
(new FormData(this)).forEach(( value, key) => {
137+
if ( key.startsWith('ppom[') && typeof value === 'string' ) {
138+
ppomFields.append( key, value );
139+
} else {
140+
formData.append(key, value);
160141
}
161142
});
162143

144+
formData.append('ppom', ppomFields.toString());
145+
146+
fetch(ajaxurl, {
147+
method: 'POST',
148+
body: formData
149+
})
150+
.then(response => response.json())
151+
.then(resp => {
152+
const bg_color = resp.status == 'success' ? '#4e694859' : '#ee8b94';
153+
jQuery(".ppom-meta-save-notice").html(resp.message).css({ 'background-color': bg_color, 'padding': '8px', 'border-left': '5px solid #008c00' });
154+
if (resp.status == 'success') {
155+
if (resp.redirect_to != '') {
156+
window.location = resp.redirect_to;
157+
} else {
158+
window.location.reload();
159+
}
160+
}
161+
})
162+
.catch(() => {
163+
jQuery(".ppom-meta-save-notice").html("An error occurred. Please try again.").css({ 'background-color': '#ee8b94', 'padding': '8px', 'border-left': '5px solid #c00' });
164+
});
163165
});
164166

165167

0 commit comments

Comments
 (0)