Skip to content

Commit 9dd9918

Browse files
committed
PR Feedbacks
1 parent 4e11798 commit 9dd9918

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

assets/js/admin.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@
565565
const $userSyncForm = $('.mailchimp-sf-user-sync-form');
566566
const $submitButtons = $('input[type="submit"].mailchimp-sf-button-submit');
567567
const params = window.mailchimp_sf_admin_params || {};
568-
const ajaxUrl = params.ajax_url;
569-
const ajaxNonce = params.preview_form_nonce;
568+
const ajaxUrl = params.ajax_url || '';
569+
const ajaxNonce = params.preview_form_nonce || '';
570570

571571
// Initially hide all submit buttons
572572
$submitButtons.hide();
@@ -649,14 +649,15 @@
649649
display_unsub_link: $('#mc_use_unsub_link').is(':checked'),
650650
};
651651

652-
$.post(
653-
ajaxUrl,
654-
{
652+
$.ajax({
653+
url: ajaxUrl,
654+
type: 'POST',
655+
data: {
655656
action: 'mailchimp_sf_preview_form',
656657
nonce: ajaxNonce,
657658
preview_data: previewData,
658659
},
659-
function (response) {
660+
success(response) {
660661
if (response.success && response.data) {
661662
unblockElement('.mailchimp-sf-form-preview-content');
662663
$previewer.html(response.data);
@@ -669,7 +670,17 @@
669670
);
670671
}
671672
},
672-
);
673+
error(jqXHR, textStatus, errorThrown) {
674+
// eslint-disable-next-line no-console
675+
console.error('Error: ', textStatus, ', Details: ', errorThrown);
676+
unblockElement('.mailchimp-sf-form-preview-content');
677+
$previewer.html(
678+
'<div class="mailchimp-sf-form-preview-error">' +
679+
params.generic_error +
680+
'</div>',
681+
);
682+
},
683+
});
673684
}
674685

675686
const debouncedPreviewForm = debounce(previewForm, 300);

includes/class-mailchimp-admin.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,11 @@ public function check_login_session() {
293293
* This function previews the subscribe form on the settings page based on the form settings.
294294
*/
295295
public function preview_subscribe_form() {
296-
// Validate the nonce and permissions.
297-
if (
298-
! current_user_can( 'manage_options' ) ||
299-
! isset( $_POST['nonce'] ) ||
300-
! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'mailchimp_sf_preview_form_nonce' )
301-
) {
296+
// Check the nonce for security
297+
check_ajax_referer( 'mailchimp_sf_preview_form_nonce', 'nonce' );
298+
299+
// Validate the permissions.
300+
if ( ! current_user_can( 'manage_options' ) ) {
302301
wp_send_json_error( array( 'message' => esc_html__( 'You do not have permission to perform this action.', 'mailchimp' ) ) );
303302
}
304303

0 commit comments

Comments
 (0)