From bcd8c061773718d15e7b90f35fc907b9daf738ee Mon Sep 17 00:00:00 2001 From: Ravinder Kumar Date: Thu, 24 Aug 2023 11:40:42 +0530 Subject: [PATCH] refactor: display migration banner only on give pages --- assets/src/js/plugins/give-api/api.js | 7 ++-- .../V2/DonationFormsAdminPage.php | 5 ++- ...andardToDonationsMigrationGlobalBanner.php | 35 ++++++++++++------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/assets/src/js/plugins/give-api/api.js b/assets/src/js/plugins/give-api/api.js index 4976890ed2..8ef67bad5c 100644 --- a/assets/src/js/plugins/give-api/api.js +++ b/assets/src/js/plugins/give-api/api.js @@ -187,12 +187,15 @@ const Give = { /** * Get global param * + * @unreleased Return default value of global param if param not found. * @since 2.2.4 * * @return {object} WordPress localized global param. */ - getGlobal: function() { - return ( 'undefined' === typeof give_global_vars ) ? give_vars : give_global_vars; + getGlobal: function () { + return ('undefined' === typeof give_global_vars) + ? (window.give_vars || {}) + : (window.give_global_vars || {}); }, /** diff --git a/src/DonationForms/V2/DonationFormsAdminPage.php b/src/DonationForms/V2/DonationFormsAdminPage.php index 4bfec5927e..2f62e7aacf 100644 --- a/src/DonationForms/V2/DonationFormsAdminPage.php +++ b/src/DonationForms/V2/DonationFormsAdminPage.php @@ -73,6 +73,8 @@ public function highlightAllFormsMenuItem() /** * Load scripts + * + * @unreleased Set admin script and style dependencies to display PayPal Standard to Donations Migration banner. */ public function loadScripts() { @@ -87,6 +89,7 @@ public function loadScripts() ]; EnqueueScript::make('give-admin-donation-forms', 'assets/dist/js/give-admin-donation-forms.js') + ->dependencies(['give-admin-scripts']) ->loadInFooter() ->registerTranslations() ->registerLocalizeData('GiveDonationForms', $data)->enqueue(); @@ -94,7 +97,7 @@ public function loadScripts() wp_enqueue_style( 'give-admin-ui-font', 'https://fonts.googleapis.com/css2?family=Open+Sans:wght@400..700&display=swap', - [], + ['give-admin-styles'], null ); } diff --git a/src/PaymentGateways/PayPalCommerce/Banners/PayPalStandardToDonationsMigrationGlobalBanner.php b/src/PaymentGateways/PayPalCommerce/Banners/PayPalStandardToDonationsMigrationGlobalBanner.php index c1091fa671..5eaa4633f9 100644 --- a/src/PaymentGateways/PayPalCommerce/Banners/PayPalStandardToDonationsMigrationGlobalBanner.php +++ b/src/PaymentGateways/PayPalCommerce/Banners/PayPalStandardToDonationsMigrationGlobalBanner.php @@ -31,8 +31,13 @@ public function setHook() } add_action('admin_enqueue_scripts', function () { - if (give_is_gateway_active(PayPalStandard::id())) { - wp_add_inline_script('give-admin-scripts', $this->getModalScript()); + $isGivePage = ( isset($_GET['page']) && 'give-forms' === $_GET['page'] ) + || ( isset($_GET['post_type']) && 'give_forms' === $_GET['post_type'] ); + + if ($isGivePage && give_is_gateway_active(PayPalStandard::id())) { + add_action('admin_footer', function () { + wp_print_inline_script_tag($this->getModalScript()); + }); } }); } @@ -84,15 +89,19 @@ public function getModalScript(): string ); return << { - const dismissModalAjaxRequest = () => { - wp.ajax.post({ - 'give-action': 'dismiss_notices', - 'notice_id': '$this->bannerId', - 'dismissible_type': 'user', - 'dismiss_interval': 'permanent', - '_wpnonce': '$nonce' - }); + const givePayPalStandardToDonationsMigrationGlobalBanner = () => { + const dismissModalAjaxRequest = async () => { + const formData = new FormData(); + formData.append('give-action', 'dismiss_notices'); + formData.append('notice_id', '$this->bannerId'); + formData.append('dismissible_type', 'user'); + formData.append('dismiss_interval', 'permanent'); + formData.append('_wpnonce', '$nonce'); + + await fetch(ajaxurl, { + method: 'POST', + body: formData, + }) }; new Give.modal.GiveConfirmModal( { @@ -131,7 +140,9 @@ classes: { } } } ).render(); - }); + }; + + givePayPalStandardToDonationsMigrationGlobalBanner(); EOT; }