Refactor unsupported currency notice to show only on payment pages and under the PayPal plugin.

This commit is contained in:
Pedro Silva 2023-08-11 10:35:50 +01:00
parent 402b87face
commit 1b87257fdb
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
10 changed files with 131 additions and 22 deletions

View file

@ -35,17 +35,26 @@ class Message {
*/ */
private $dismissable; private $dismissable;
/**
* The wrapper selector that will contain the notice.
*
* @var string
*/
private $wrapper;
/** /**
* Message constructor. * Message constructor.
* *
* @param string $message The message text. * @param string $message The message text.
* @param string $type The message type. * @param string $type The message type.
* @param bool $dismissable Whether the message is dismissable. * @param bool $dismissable Whether the message is dismissable.
* @param string $wrapper The wrapper selector that will contain the notice.
*/ */
public function __construct( string $message, string $type, bool $dismissable = true ) { public function __construct( string $message, string $type, bool $dismissable = true, string $wrapper = '' ) {
$this->type = $type; $this->type = $type;
$this->message = $message; $this->message = $message;
$this->dismissable = $dismissable; $this->dismissable = $dismissable;
$this->wrapper = $wrapper;
} }
/** /**
@ -74,4 +83,13 @@ class Message {
public function is_dismissable(): bool { public function is_dismissable(): bool {
return $this->dismissable; return $this->dismissable;
} }
/**
* Returns the wrapper selector that will contain the notice.
*
* @return string
*/
public function wrapper(): string {
return $this->wrapper;
}
} }

View file

@ -41,9 +41,10 @@ class Renderer implements RendererInterface {
$messages = $this->repository->current_message(); $messages = $this->repository->current_message();
foreach ( $messages as $message ) { foreach ( $messages as $message ) {
printf( printf(
'<div class="notice notice-%s %s"><p>%s</p></div>', '<div class="notice notice-%s %s" %s><p>%s</p></div>',
$message->type(), $message->type(),
( $message->is_dismissable() ) ? 'is-dismissible' : '', ( $message->is_dismissable() ) ? 'is-dismissible' : '',
$message->wrapper() ? sprintf('data-ppcp-wrapper="%s"', $message->wrapper()) : '',
wp_kses_post( $message->message() ) wp_kses_post( $message->message() )
); );
} }

View file

@ -0,0 +1,10 @@
import moveWrappedElements from "./common/wrapped-elements";
document.addEventListener(
'DOMContentLoaded',
() => {
// Wait for current execution context to end.
setTimeout(function () {
moveWrappedElements();
}, 0);
}
);

View file

@ -0,0 +1,14 @@
// This function is needed because WordPress moves our custom notices to the global placeholder.
function moveWrappedElements() {
(($) => {
$('*[data-ppcp-wrapper]').each(function() {
let $wrapper = $('.' + $(this).data('ppcpWrapper'));
if ($wrapper.length) {
$wrapper.append(this);
}
});
})(jQuery)
}
export default moveWrappedElements;

View file

@ -214,7 +214,15 @@ return array(
$state = $container->get( 'onboarding.state' ); $state = $container->get( 'onboarding.state' );
$shop_currency = $container->get( 'api.shop.currency' ); $shop_currency = $container->get( 'api.shop.currency' );
$supported_currencies = $container->get( 'api.supported-currencies' ); $supported_currencies = $container->get( 'api.supported-currencies' );
return new UnsupportedCurrencyAdminNotice( $state, $shop_currency, $supported_currencies ); $is_wc_gateways_list_page = $container->get( 'wcgateway.is-wc-gateways-list-page' );
$is_ppcp_settings_page = $container->get( 'wcgateway.is-ppcp-settings-page' );
return new UnsupportedCurrencyAdminNotice(
$state,
$shop_currency,
$supported_currencies,
$is_wc_gateways_list_page,
$is_ppcp_settings_page
);
}, },
'wcgateway.notice.dcc-without-paypal' => static function ( ContainerInterface $container ): GatewayWithoutPayPalAdminNotice { 'wcgateway.notice.dcc-without-paypal' => static function ( ContainerInterface $container ): GatewayWithoutPayPalAdminNotice {
return new GatewayWithoutPayPalAdminNotice( return new GatewayWithoutPayPalAdminNotice(

View file

@ -87,6 +87,13 @@ class SettingsPageAssets {
*/ */
protected $all_funding_sources; protected $all_funding_sources;
/**
* Whether it's a settings page of this plugin.
*
* @var bool
*/
private $is_settings_page;
/** /**
* Assets constructor. * Assets constructor.
* *
@ -100,6 +107,7 @@ class SettingsPageAssets {
* @param bool $is_pay_later_button_enabled Whether Pay Later button is enabled either for checkout, cart or product page. * @param bool $is_pay_later_button_enabled Whether Pay Later button is enabled either for checkout, cart or product page.
* @param array $disabled_sources The list of disabled funding sources. * @param array $disabled_sources The list of disabled funding sources.
* @param array $all_funding_sources The list of all existing funding sources. * @param array $all_funding_sources The list of all existing funding sources.
* @param bool $is_settings_page Whether it's a settings page of this plugin.
*/ */
public function __construct( public function __construct(
string $module_url, string $module_url,
@ -111,7 +119,8 @@ class SettingsPageAssets {
Environment $environment, Environment $environment,
bool $is_pay_later_button_enabled, bool $is_pay_later_button_enabled,
array $disabled_sources, array $disabled_sources,
array $all_funding_sources array $all_funding_sources,
bool $is_settings_page
) { ) {
$this->module_url = $module_url; $this->module_url = $module_url;
$this->version = $version; $this->version = $version;
@ -123,6 +132,7 @@ class SettingsPageAssets {
$this->is_pay_later_button_enabled = $is_pay_later_button_enabled; $this->is_pay_later_button_enabled = $is_pay_later_button_enabled;
$this->disabled_sources = $disabled_sources; $this->disabled_sources = $disabled_sources;
$this->all_funding_sources = $all_funding_sources; $this->all_funding_sources = $all_funding_sources;
$this->is_settings_page = $is_settings_page;
} }
/** /**
@ -136,11 +146,13 @@ class SettingsPageAssets {
return; return;
} }
if ( ! $this->is_paypal_payment_method_page() ) { if ( $this->is_settings_page ) {
return; $this->register_admin_assets();
} }
$this->register_admin_assets(); if ( $this->is_paypal_payment_method_page() ) {
$this->register_paypal_admin_assets();
}
} }
); );
@ -171,9 +183,9 @@ class SettingsPageAssets {
} }
/** /**
* Register assets for admin pages. * Register assets for PayPal admin pages.
*/ */
private function register_admin_assets(): void { private function register_paypal_admin_assets(): void {
wp_enqueue_style( wp_enqueue_style(
'ppcp-gateway-settings', 'ppcp-gateway-settings',
trailingslashit( $this->module_url ) . 'assets/css/gateway-settings.css', trailingslashit( $this->module_url ) . 'assets/css/gateway-settings.css',
@ -210,4 +222,18 @@ class SettingsPageAssets {
) )
); );
} }
/**
* Register assets for PayPal admin pages.
*/
private function register_admin_assets(): void {
wp_enqueue_script(
'ppcp-admin-common',
trailingslashit( $this->module_url ) . 'assets/js/common.js',
array(),
$this->version,
true
);
}
} }

View file

@ -40,6 +40,20 @@ class UnsupportedCurrencyAdminNotice {
*/ */
private $shop_currency; private $shop_currency;
/**
* Indicates if we're on the WooCommerce gateways list page.
*
* @var bool
*/
private $is_wc_gateways_list_page;
/**
* Indicates if we're on a PPCP Settings page.
*
* @var bool
*/
private $is_ppcp_settings_page;
/** /**
* UnsupportedCurrencyAdminNotice constructor. * UnsupportedCurrencyAdminNotice constructor.
* *
@ -47,10 +61,19 @@ class UnsupportedCurrencyAdminNotice {
* @param string $shop_currency The shop currency. * @param string $shop_currency The shop currency.
* @param array $supported_currencies The supported currencies. * @param array $supported_currencies The supported currencies.
*/ */
public function __construct( State $state, string $shop_currency, array $supported_currencies ) { public function __construct(
State $state,
string $shop_currency,
array $supported_currencies,
bool $is_wc_gateways_list_page,
bool $is_ppcp_settings_page
) {
$this->state = $state; $this->state = $state;
$this->shop_currency = $shop_currency; $this->shop_currency = $shop_currency;
$this->supported_currencies = $supported_currencies; $this->supported_currencies = $supported_currencies;
$this->is_wc_gateways_list_page = $is_wc_gateways_list_page;
$this->is_ppcp_settings_page = $is_ppcp_settings_page;
} }
/** /**
@ -63,16 +86,19 @@ class UnsupportedCurrencyAdminNotice {
return null; return null;
} }
$paypal_currency_support_url = 'https://developer.paypal.com/api/rest/reference/currency-codes/';
$message = sprintf( $message = sprintf(
/* translators: %1$s the shop currency, 2$s the gateway name. */ /* translators: %1$s the shop currency, %2$s the PayPal currency support page link opening HTML tag, %3$s the link ending HTML tag. */
__( __(
'Attention: Your current WooCommerce store currency (%1$s) is not supported by PayPal. Please update your store currency to one that is supported by PayPal to ensure smooth transactions. Visit the <a href="%2$s">PayPal currency support page</a> for more information on supported currencies.', 'Attention: Your current WooCommerce store currency (%1$s) is not supported by PayPal. Please update your store currency to one that is supported by PayPal to ensure smooth transactions. Visit the %2$sPayPal currency support page%3$s for more information on supported currencies.',
'woocommerce-paypal-payments' 'woocommerce-paypal-payments'
), ),
$this->shop_currency, $this->shop_currency,
'https://developer.paypal.com/api/rest/reference/currency-codes/' '<a href="' . esc_url( $paypal_currency_support_url ) . '">',
'</a>'
); );
return new Message( $message, 'warning' ); return new Message( $message, 'warning', true, 'ppcp-notice-wrapper' );
} }
/** /**
@ -81,7 +107,9 @@ class UnsupportedCurrencyAdminNotice {
* @return bool * @return bool
*/ */
protected function should_display(): bool { protected function should_display(): bool {
return $this->state->current_state() === State::STATE_ONBOARDED && ! $this->currency_supported(); return $this->state->current_state() === State::STATE_ONBOARDED
&& ! $this->currency_supported()
&& ($this->is_wc_gateways_list_page || $this->is_ppcp_settings_page);
} }
/** /**

View file

@ -77,6 +77,8 @@ class HeaderRenderer {
'</a> '</a>
</span> </span>
</div> </div>
<div class="ppcp-notice-wrapper"></div>
'; ';
} }
} }

View file

@ -183,7 +183,8 @@ class WCGatewayModule implements ModuleInterface {
$c->get( 'onboarding.environment' ), $c->get( 'onboarding.environment' ),
$settings_status->is_pay_later_button_enabled(), $settings_status->is_pay_later_button_enabled(),
$settings->has( 'disable_funding' ) ? $settings->get( 'disable_funding' ) : array(), $settings->has( 'disable_funding' ) ? $settings->get( 'disable_funding' ) : array(),
$c->get( 'wcgateway.settings.funding-sources' ) $c->get( 'wcgateway.settings.funding-sources' ),
$c->get( 'wcgateway.is-ppcp-settings-page' )
); );
$assets->register_assets(); $assets->register_assets();
} }

View file

@ -6,6 +6,7 @@ module.exports = {
mode: isProduction ? 'production' : 'development', mode: isProduction ? 'production' : 'development',
target: 'web', target: 'web',
entry: { entry: {
'common': path.resolve('./resources/js/common.js'),
'gateway-settings': path.resolve('./resources/js/gateway-settings.js'), 'gateway-settings': path.resolve('./resources/js/gateway-settings.js'),
'fraudnet': path.resolve('./resources/js/fraudnet.js'), 'fraudnet': path.resolve('./resources/js/fraudnet.js'),
'oxxo': path.resolve('./resources/js/oxxo.js'), 'oxxo': path.resolve('./resources/js/oxxo.js'),