Merge pull request #1588 from woocommerce/PCP-1926-improve-invalid-currency-backend-notice

improve invalid currency backend notice (1926)
This commit is contained in:
Emili Castells 2023-09-04 17:17:08 +02:00 committed by GitHub
commit 104aa3b22a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 136 additions and 24 deletions

View file

@ -35,17 +35,26 @@ class Message {
*/
private $dismissable;
/**
* The wrapper selector that will contain the notice.
*
* @var string
*/
private $wrapper;
/**
* Message constructor.
*
* @param string $message The message text.
* @param string $type The message type.
* @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->message = $message;
$this->dismissable = $dismissable;
$this->wrapper = $wrapper;
}
/**
@ -74,4 +83,13 @@ class Message {
public function is_dismissable(): bool {
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();
foreach ( $messages as $message ) {
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->is_dismissable() ) ? 'is-dismissible' : '',
( $message->wrapper() ? sprintf( 'data-ppcp-wrapper="%s"', esc_attr( $message->wrapper() ) ) : '' ),
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

@ -212,10 +212,18 @@ return array(
return new ConnectAdminNotice( $state, $settings );
},
'wcgateway.notice.currency-unsupported' => static function ( ContainerInterface $container ): UnsupportedCurrencyAdminNotice {
$state = $container->get( 'onboarding.state' );
$shop_currency = $container->get( 'api.shop.currency' );
$supported_currencies = $container->get( 'api.supported-currencies' );
return new UnsupportedCurrencyAdminNotice( $state, $shop_currency, $supported_currencies );
$state = $container->get( 'onboarding.state' );
$shop_currency = $container->get( 'api.shop.currency' );
$supported_currencies = $container->get( 'api.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 {
return new GatewayWithoutPayPalAdminNotice(

View file

@ -87,6 +87,13 @@ class SettingsPageAssets {
*/
protected $all_funding_sources;
/**
* Whether it's a settings page of this plugin.
*
* @var bool
*/
private $is_settings_page;
/**
* 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 array $disabled_sources The list of disabled 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(
string $module_url,
@ -111,7 +119,8 @@ class SettingsPageAssets {
Environment $environment,
bool $is_pay_later_button_enabled,
array $disabled_sources,
array $all_funding_sources
array $all_funding_sources,
bool $is_settings_page
) {
$this->module_url = $module_url;
$this->version = $version;
@ -123,6 +132,7 @@ class SettingsPageAssets {
$this->is_pay_later_button_enabled = $is_pay_later_button_enabled;
$this->disabled_sources = $disabled_sources;
$this->all_funding_sources = $all_funding_sources;
$this->is_settings_page = $is_settings_page;
}
/**
@ -138,11 +148,13 @@ class SettingsPageAssets {
return;
}
if ( ! $this->is_paypal_payment_method_page() ) {
return;
if ( $this->is_settings_page ) {
$this->register_admin_assets();
}
$this->register_admin_assets();
if ( $this->is_paypal_payment_method_page() ) {
$this->register_paypal_admin_assets();
}
}
);
@ -173,9 +185,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(
'ppcp-gateway-settings',
trailingslashit( $this->module_url ) . 'assets/css/gateway-settings.css',
@ -212,4 +224,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,17 +40,42 @@ class UnsupportedCurrencyAdminNotice {
*/
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.
*
* @param State $state The state.
* @param string $shop_currency The shop currency.
* @param array $supported_currencies The supported currencies.
* @param bool $is_wc_gateways_list_page Indicates if we're on the WooCommerce gateways list page.
* @param bool $is_ppcp_settings_page Indicates if we're on a PPCP Settings page.
*/
public function __construct( State $state, string $shop_currency, array $supported_currencies ) {
$this->state = $state;
$this->shop_currency = $shop_currency;
$this->supported_currencies = $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->shop_currency = $shop_currency;
$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 +88,19 @@ class UnsupportedCurrencyAdminNotice {
return null;
}
$paypal_currency_support_url = 'https://developer.paypal.com/api/rest/reference/currency-codes/';
$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'
),
$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 +109,9 @@ class UnsupportedCurrencyAdminNotice {
* @return 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>
</span>
</div>
<div class="ppcp-notice-wrapper"></div>
';
}
}

View file

@ -183,7 +183,8 @@ class WCGatewayModule implements ModuleInterface {
$c->get( 'onboarding.environment' ),
$settings_status->is_pay_later_button_enabled(),
$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();
}

View file

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

View file

@ -27,8 +27,9 @@ class SettingsPagesAssetsTest extends TestCase
Mockery::mock(Environment::class),
true,
array(),
array()
);
array(),
true
);
when('is_admin')
->justReturn(true);