From c38b91f9411ceb834f59c92156f30b8f241a0249 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Tue, 3 Oct 2023 13:28:21 +0200 Subject: [PATCH] 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] --- mailpoet/assets/js/src/global.d.ts | 1 + mailpoet/assets/js/src/public.tsx | 7 +++++++ mailpoet/lib/Form/AssetsController.php | 20 ++------------------ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/mailpoet/assets/js/src/global.d.ts b/mailpoet/assets/js/src/global.d.ts index 27c2925e76..4999822808 100644 --- a/mailpoet/assets/js/src/global.d.ts +++ b/mailpoet/assets/js/src/global.d.ts @@ -154,6 +154,7 @@ interface Window { grecaptcha?: any; MailPoetForm?: { ajax_url: string; + ajax_common_error_message: string; }; mailpoet_authorized_emails?: string[]; mailpoet_verified_sender_domains?: string[]; diff --git a/mailpoet/assets/js/src/public.tsx b/mailpoet/assets/js/src/public.tsx index 8e4b3505a7..c5ecfc0174 100644 --- a/mailpoet/assets/js/src/public.tsx +++ b/mailpoet/assets/js/src/public.tsx @@ -9,6 +9,13 @@ const exitIntentEvent = 'mouseleave.mailpoet.form-exit-intent'; const startingClassName = 'starting-to-show'; jQuery(($) => { + // Initialize Ajax Error message + MailPoet.I18n.add( + 'ajaxFailedErrorMessage', + window.MailPoetForm.ajax_common_error_message, + ); + + // Initialize Parsley validation Parsley.addValidator('names', { requirementType: ['string', 'string'], validateString: (value, errorBrackets, errorURL) => { diff --git a/mailpoet/lib/Form/AssetsController.php b/mailpoet/lib/Form/AssetsController.php index 2833b5fe58..3f7da0ec53 100644 --- a/mailpoet/lib/Form/AssetsController.php +++ b/mailpoet/lib/Form/AssetsController.php @@ -86,28 +86,12 @@ class AssetsController { true ); + $ajaxFailedErrorMessage = __('An error has happened while performing a request, please try again later.', 'mailpoet'); $this->wp->wpLocalizeScript('mailpoet_public', 'MailPoetForm', [ 'ajax_url' => $this->wp->adminUrl('admin-ajax.php'), '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 = <<wp->wpAddInlineScript( - 'mailpoet_public', - sprintf($inlineScript, esc_js($ajaxFailedErrorMessage)), - 'after' - ); } public function setupAdminWidgetPageDependencies() {