Refactor resume sending js code out of PHP

[MAILPOET-1987]
This commit is contained in:
Rostislav Wolny
2019-05-15 19:37:45 +02:00
committed by M. Shull
parent ced9c549f6
commit 3aa382a060
5 changed files with 17 additions and 20 deletions

View File

@ -1,4 +1,5 @@
import jQuery from 'jquery';
import MailPoet from 'mailpoet';
jQuery(($) => {
$(document).on('click', '.mailpoet-dismissible-notice .notice-dismiss', function dismiss() {
@ -12,4 +13,18 @@ jQuery(($) => {
},
});
});
$(document).on('click', '.notice .mailpoet-js-button-resume-sending', function resumeSending() {
const noticeElement = $(this).closest('.notice');
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'mailer',
action: 'resumeSending',
}).done(() => {
noticeElement.slideUp();
MailPoet.Notice.success(MailPoet.I18n.t('mailerSendingResumedNotice'));
if (window.mailpoet_listing) { window.mailpoet_listing.forceUpdate(); }
}).fail((response) => {
MailPoet.Notice.showApiErrorNotice(response, { scroll: true });
});
});
});

View File

@ -103,9 +103,8 @@ class MailPoetMapper {
'rel' => 'noopener noreferrer',
]
);
$message .= ' &nbsp; <button class="button js-button-resume-sending">' . WPFunctions::get()->__('Resume sending', 'mailpoet') . '</button>';
$message .= ' &nbsp; <button class="button mailpoet-js-button-resume-sending">' . WPFunctions::get()->__('Resume sending', 'mailpoet') . '</button>';
$message .= '</p>';
$message .= "<script>jQuery('.js-button-resume-sending').on('click', function() { MailPoet.Ajax.post({ api_version: window.mailpoet_api_version, endpoint: 'mailer', action: 'resumeSending' }).done(function() { jQuery('.js-error-unauthorized-email').slideUp(); MailPoet.Notice.success('" . WPFunctions::get()->__('Sending has been resumed.') . "'); if (window.mailpoet_listing) { window.mailpoet_listing.forceUpdate(); }}).fail(function(response) { if (response.errors.length > 0) { MailPoet.Notice.error(response.errors.map(function(error) { return error.message }), { scroll: true }); }}); })</script>";
return $message;
}

View File

@ -90,23 +90,6 @@ class UnauthorizedEmailNotice {
private function getResumeSendingButton() {
$button = '<button class="button button-primary mailpoet-js-button-resume-sending">' . $this->wp->__('Resume sending', 'mailpoet') . '</button>';
$script = "
<script>
jQuery('.mailpoet-js-button-resume-sending').on('click', function() {
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'mailer', action: 'resumeSending'
}).done(function() {
jQuery('.mailpoet-js-error-unauthorized-emails-notice').slideUp();
MailPoet.Notice.success('" . WPFunctions::get()->__('Sending has been resumed.') . "');}
).fail(function(response) {
if (response.errors.length > 0) {
MailPoet.Notice.error(response.errors.map(function(error) { return error.message }), { scroll: true });
}
});
});
</script>";
$button .= str_replace("\n", "", $script);
return "<p>$button</p>";
}
}

View File

@ -71,6 +71,7 @@ jQuery('.toplevel_page_mailpoet-newsletters.menu-top-last')
'senderEmailAddressWarning1': _x('You might not reach the inbox of your subscribers if you use this email address.', 'In the last step, before sending a newsletter. URL: ?page=mailpoet-newsletters#/send/2'),
'senderEmailAddressWarning2': _x('Use an address like %suggested for the Sender and put %originalSender in the <em>Reply-to</em> field below.', 'In the last step, before sending a newsletter. URL: ?page=mailpoet-newsletters#/send/2'),
'senderEmailAddressWarning3': _x('Read more.'),
'mailerSendingResumedNotice': __('Sending has been resumed.'),
}) %>
<% block translations %><% endblock %>

View File

@ -293,7 +293,6 @@
'mailerErrorCode': __('Error code: %$1s'),
'mailerCheckSettingsNotice': __('Check your [link]sending method settings[/link].'),
'mailerResumeSendingButton': __('Resume sending'),
'mailerSendingResumedNotice': __('Sending has been resumed.'),
'confirmEdit': __('Sending is in progress. Do you want to pause sending and edit the newsletter?'),
'confirmTitle': __('Confirm to proceed'),
'confirmLabel': __('Confirm'),