mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add locations for block, do not load non-block scripts on block pages
This commit is contained in:
parent
dc3fde5abf
commit
bfaf05a96e
7 changed files with 89 additions and 9 deletions
|
@ -9,4 +9,26 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Blocks;
|
||||
|
||||
return array();
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
|
||||
return array(
|
||||
'wcgateway.button.locations' => function ( ContainerInterface $container, array $locations ): array {
|
||||
return array_merge(
|
||||
$locations,
|
||||
array(
|
||||
'checkout-block-express' => _x( 'Block Express Checkout', 'Name of Buttons Location', 'woocommerce-paypal-payments' ),
|
||||
'cart-block' => _x( 'Block Cart', 'Name of Buttons Location', 'woocommerce-paypal-payments' ),
|
||||
)
|
||||
);
|
||||
},
|
||||
'wcgateway.settings.pay-later.messaging-locations' => function ( ContainerInterface $container, array $locations ): array {
|
||||
unset( $locations['checkout-block-express'] );
|
||||
unset( $locations['cart-block'] );
|
||||
return $locations;
|
||||
},
|
||||
'wcgateway.settings.pay-later.button-locations' => function ( ContainerInterface $container, array $locations ): array {
|
||||
unset( $locations['checkout-block-express'] );
|
||||
unset( $locations['cart-block'] );
|
||||
return $locations;
|
||||
},
|
||||
);
|
||||
|
|
|
@ -163,7 +163,7 @@ registerExpressPaymentMethod({
|
|||
content: <PayPalComponent/>,
|
||||
edit: <b>TODO: editing</b>,
|
||||
ariaLabel: config.title,
|
||||
canMakePayment: () => true,
|
||||
canMakePayment: () => config.enabled,
|
||||
supports: {
|
||||
features: ['products'],
|
||||
},
|
||||
|
|
|
@ -28,6 +28,8 @@ return array(
|
|||
$container->get( 'blocks.url' ),
|
||||
$container->get( 'ppcp.asset-version' ),
|
||||
$container->get( 'button.smart-button' ),
|
||||
$container->get( 'wcgateway.settings' ),
|
||||
$container->get( 'wcgateway.settings.status' ),
|
||||
$container->get( 'wcgateway.paypal-gateway' )
|
||||
);
|
||||
},
|
||||
|
|
|
@ -12,6 +12,8 @@ namespace WooCommerce\PayPalCommerce\Blocks;
|
|||
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
|
||||
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
/**
|
||||
* Class PayPalPaymentMethod
|
||||
|
@ -38,6 +40,20 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
|
|||
*/
|
||||
private $smart_button;
|
||||
|
||||
/**
|
||||
* The settings.
|
||||
*
|
||||
* @var Settings
|
||||
*/
|
||||
private $plugin_settings;
|
||||
|
||||
/**
|
||||
* The Settings status helper.
|
||||
*
|
||||
* @var SettingsStatus
|
||||
*/
|
||||
protected $settings_status;
|
||||
|
||||
/**
|
||||
* The WC gateway.
|
||||
*
|
||||
|
@ -51,19 +67,25 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
|
|||
* @param string $module_url The url of this module.
|
||||
* @param string $version The assets version.
|
||||
* @param SmartButtonInterface $smart_button The smart button script loading handler.
|
||||
* @param Settings $plugin_settings The settings.
|
||||
* @param SettingsStatus $settings_status The Settings status helper.
|
||||
* @param PayPalGateway $gateway The WC gateway.
|
||||
*/
|
||||
public function __construct(
|
||||
string $module_url,
|
||||
string $version,
|
||||
SmartButtonInterface $smart_button,
|
||||
Settings $plugin_settings,
|
||||
SettingsStatus $settings_status,
|
||||
PayPalGateway $gateway
|
||||
) {
|
||||
$this->name = PayPalGateway::ID;
|
||||
$this->module_url = $module_url;
|
||||
$this->version = $version;
|
||||
$this->smart_button = $smart_button;
|
||||
$this->gateway = $gateway;
|
||||
$this->name = PayPalGateway::ID;
|
||||
$this->module_url = $module_url;
|
||||
$this->version = $version;
|
||||
$this->smart_button = $smart_button;
|
||||
$this->plugin_settings = $plugin_settings;
|
||||
$this->settings_status = $settings_status;
|
||||
$this->gateway = $gateway;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,6 +93,18 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
|
|||
*/
|
||||
public function initialize() { }
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function is_active() {
|
||||
// Do not load when definitely not needed,
|
||||
// but we still need to check the locations later and handle in JS
|
||||
// because has_block cannot be called here (too early).
|
||||
return $this->plugin_settings->has( 'enabled' ) && $this->plugin_settings->get( 'enabled' )
|
||||
&& ( $this->settings_status->is_smart_button_enabled_for_location( 'checkout-block-express' ) ||
|
||||
$this->settings_status->is_smart_button_enabled_for_location( 'cart-block' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -90,11 +124,14 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
public function get_payment_method_data() {
|
||||
$script_data = $this->smart_button->script_data();
|
||||
|
||||
return array(
|
||||
'id' => $this->gateway->id,
|
||||
'title' => $this->gateway->title,
|
||||
'description' => $this->gateway->description,
|
||||
'scriptData' => $this->smart_button->script_data(),
|
||||
'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ),
|
||||
'scriptData' => $script_data,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -516,6 +516,10 @@ class SmartButton implements SmartButtonInterface {
|
|||
return false;
|
||||
}
|
||||
|
||||
if ( in_array( $this->context(), array( 'checkout-block', 'cart-block' ), true ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->should_load_buttons() || $this->can_render_dcc();
|
||||
}
|
||||
|
||||
|
@ -537,6 +541,9 @@ class SmartButton implements SmartButtonInterface {
|
|||
case 'cart':
|
||||
case 'pay-now':
|
||||
return $smart_button_enabled_for_current_location || $messaging_enabled_for_current_location;
|
||||
case 'checkout-block':
|
||||
case 'cart-block':
|
||||
return $smart_button_enabled_for_current_location;
|
||||
case 'product':
|
||||
return $smart_button_enabled_for_current_location || $messaging_enabled_for_current_location || $smart_button_enabled_for_mini_cart;
|
||||
default:
|
||||
|
|
|
@ -22,11 +22,20 @@ trait ContextTrait {
|
|||
$context = 'product';
|
||||
}
|
||||
|
||||
// has_block may not work if called too early, such as during the block registration.
|
||||
if ( has_block( 'woocommerce/cart' ) ) {
|
||||
$context = 'cart-block';
|
||||
}
|
||||
|
||||
if ( is_cart() ) {
|
||||
$context = 'cart';
|
||||
}
|
||||
|
||||
if ( ( is_checkout() || has_block( 'woocommerce/checkout' ) ) && ! $this->is_paypal_continuation() ) {
|
||||
if ( has_block( 'woocommerce/checkout' ) ) {
|
||||
$context = 'checkout-block';
|
||||
}
|
||||
|
||||
if ( ( is_checkout() ) && ! $this->is_paypal_continuation() ) {
|
||||
$context = 'checkout';
|
||||
}
|
||||
|
||||
|
|
|
@ -98,6 +98,9 @@ class SettingsStatus {
|
|||
if ( 'pay-now' === $location ) {
|
||||
$location = 'checkout';
|
||||
}
|
||||
if ( 'checkout-block' === $location ) {
|
||||
$location = 'checkout-block-express';
|
||||
}
|
||||
return $location;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue