Get rid of inline JS code for public.js

This code was preventing WordPress from rendering the public.js
script as deferred.
The defer attribute can be applied only to scripts
that are loaded via src. WP detected that there was an associated
inline script and removed the defer attribute from the public.js script.

In this commit, I moved the initialization of the error message to public.js and
I pass the value via static object.
[MAILPOET-5623]
This commit is contained in:
Rostislav Wolny
2023-10-03 13:28:21 +02:00
committed by Aschepikov
parent 71cb252e8c
commit c38b91f941
3 changed files with 10 additions and 18 deletions

View File

@@ -154,6 +154,7 @@ interface Window {
grecaptcha?: any; grecaptcha?: any;
MailPoetForm?: { MailPoetForm?: {
ajax_url: string; ajax_url: string;
ajax_common_error_message: string;
}; };
mailpoet_authorized_emails?: string[]; mailpoet_authorized_emails?: string[];
mailpoet_verified_sender_domains?: string[]; mailpoet_verified_sender_domains?: string[];

View File

@@ -9,6 +9,13 @@ const exitIntentEvent = 'mouseleave.mailpoet.form-exit-intent';
const startingClassName = 'starting-to-show'; const startingClassName = 'starting-to-show';
jQuery(($) => { jQuery(($) => {
// Initialize Ajax Error message
MailPoet.I18n.add(
'ajaxFailedErrorMessage',
window.MailPoetForm.ajax_common_error_message,
);
// Initialize Parsley validation
Parsley.addValidator('names', { Parsley.addValidator('names', {
requirementType: ['string', 'string'], requirementType: ['string', 'string'],
validateString: (value, errorBrackets, errorURL) => { validateString: (value, errorBrackets, errorURL) => {

View File

@@ -86,28 +86,12 @@ class AssetsController {
true true
); );
$ajaxFailedErrorMessage = __('An error has happened while performing a request, please try again later.', 'mailpoet');
$this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [ $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [
'ajax_url' => $this->wp->adminUrl('admin-ajax.php'), 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'),
'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false), 'is_rtl' => (function_exists('is_rtl') ? (bool)is_rtl() : false),
'ajax_common_error_message' => esc_js($ajaxFailedErrorMessage),
]); ]);
$ajaxFailedErrorMessage = __('An error has happened while performing a request, please try again later.', 'mailpoet');
$inlineScript = <<<EOL
function initMailpoetTranslation() {
if (typeof MailPoet !== 'undefined') {
MailPoet.I18n.add('ajaxFailedErrorMessage', '%s')
} else {
setTimeout(initMailpoetTranslation, 250);
}
}
setTimeout(initMailpoetTranslation, 250);
EOL;
$this->wp->wpAddInlineScript(
'mailpoet_public',
sprintf($inlineScript, esc_js($ajaxFailedErrorMessage)),
'after'
);
} }
public function setupAdminWidgetPageDependencies() { public function setupAdminWidgetPageDependencies() {