Merge pull request #2816 from woocommerce/PCP-3924-translation-loading

Translation Loading Issue in WooCommerce PayPal Payments Plugin on WordPress 6.7 (Deprecation Warning)
This commit is contained in:
Niklas Gutberlet 2024-11-29 12:23:18 +01:00 committed by GitHub
commit 48e89d82ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 440 additions and 391 deletions

View file

@ -19,7 +19,7 @@ hooks:
pre-start:
- exec-host: "mkdir -p .ddev/wordpress/wp-content/plugins/${DDEV_PROJECT}"
web_environment:
- WP_VERSION=6.3.3
- WP_VERSION=6.7.1
- WP_LOCALE=en_US
- WP_TITLE=WooCommerce PayPal Payments
- WP_MULTISITE=true

View file

@ -46,12 +46,6 @@ class CardFieldsModule implements ServiceModule, ExtendingModule, ExecutableModu
return true;
}
$dcc_configuration = $c->get( 'wcgateway.configuration.dcc' );
assert( $dcc_configuration instanceof DCCGatewayConfiguration );
if ( ! $dcc_configuration->is_enabled() ) {
return true;
}
/**
* Param types removed to avoid third-party issues.
*
@ -59,7 +53,10 @@ class CardFieldsModule implements ServiceModule, ExtendingModule, ExecutableModu
*/
add_filter(
'woocommerce_paypal_payments_sdk_components_hook',
function( $components ) {
function( $components ) use ( $c ) {
if ( ! $c->get( 'wcgateway.configuration.dcc' )->is_enabled() ) {
return $components;
}
if ( in_array( 'hosted-fields', $components, true ) ) {
$key = array_search( 'hosted-fields', $components, true );
if ( $key !== false ) {
@ -80,7 +77,10 @@ class CardFieldsModule implements ServiceModule, ExtendingModule, ExecutableModu
* @psalm-suppress MissingClosureReturnType
* @psalm-suppress MissingClosureParamType
*/
function( $default_fields, $id ) {
function( $default_fields, $id ) use ( $c ) {
if ( ! $c->get( 'wcgateway.configuration.dcc' )->is_enabled() ) {
return $default_fields;
}
if ( CreditCardGateway::ID === $id && apply_filters( 'woocommerce_paypal_payments_enable_cardholder_name_field', false ) ) {
$default_fields['card-name-field'] = '<p class="form-row form-row-wide">
<label for="ppcp-credit-card-gateway-card-name">' . esc_attr__( 'Cardholder Name', 'woocommerce-paypal-payments' ) . '</label>
@ -113,6 +113,9 @@ class CardFieldsModule implements ServiceModule, ExtendingModule, ExecutableModu
add_filter(
'ppcp_create_order_request_body_data',
function( array $data, string $payment_method ) use ( $c ): array {
if ( ! $c->get( 'wcgateway.configuration.dcc' )->is_enabled() ) {
return $data;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( $payment_method !== CreditCardGateway::ID ) {
return $data;

View file

@ -51,14 +51,24 @@ class CompatModule implements ServiceModule, ExtendingModule, ExecutableModule {
*/
public function run( ContainerInterface $c ): bool {
$this->initialize_ppec_compat_layer( $c );
$this->initialize_tracking_compat_layer( $c );
add_action(
'woocommerce_init',
function() use ( $c ) {
$this->initialize_ppec_compat_layer( $c );
$this->initialize_tracking_compat_layer( $c );
}
);
$asset_loader = $c->get( 'compat.assets' );
assert( $asset_loader instanceof CompatAssets );
add_action(
'init',
function() use ( $c ) {
$asset_loader = $c->get( 'compat.assets' );
assert( $asset_loader instanceof CompatAssets );
add_action( 'init', array( $asset_loader, 'register' ) );
add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) );
$asset_loader->register();
add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) );
}
);
$this->migrate_pay_later_settings( $c );
$this->migrate_smart_button_settings( $c );
@ -72,11 +82,9 @@ class CompatModule implements ServiceModule, ExtendingModule, ExecutableModule {
$this->initialize_nyp_compat_layer();
}
$logger = $c->get( 'woocommerce.logger.woocommerce' );
$is_wc_bookings_active = $c->get( 'compat.wc_bookings.is_supported_plugin_version_active' );
if ( $is_wc_bookings_active ) {
$this->initialize_wc_bookings_compat_layer( $logger );
$this->initialize_wc_bookings_compat_layer( $c );
}
return true;
@ -427,13 +435,13 @@ class CompatModule implements ServiceModule, ExtendingModule, ExecutableModule {
/**
* Sets up the compatibility layer for WooCommerce Bookings plugin.
*
* @param LoggerInterface $logger The logger.
* @param ContainerInterface $container The logger.
* @return void
*/
protected function initialize_wc_bookings_compat_layer( LoggerInterface $logger ): void {
protected function initialize_wc_bookings_compat_layer( ContainerInterface $container ): void {
add_action(
'woocommerce_paypal_payments_shipping_callback_woocommerce_order_created',
static function ( WC_Order $wc_order, WC_Cart $wc_cart ) use ( $logger ): void {
static function ( WC_Order $wc_order, WC_Cart $wc_cart ) use ( $container ): void {
try {
$cart_contents = $wc_cart->get_cart();
foreach ( $cart_contents as $cart_item ) {
@ -474,7 +482,7 @@ class CompatModule implements ServiceModule, ExtendingModule, ExecutableModule {
}
}
} catch ( Exception $exception ) {
$logger->warning( 'Failed to create booking for WooCommerce Bookings plugin: ' . $exception->getMessage() );
$container->get( 'woocommerce.logger.woocommerce' )->warning( 'Failed to create booking for WooCommerce Bookings plugin: ' . $exception->getMessage() );
}
},
10,

View file

@ -44,12 +44,6 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): bool {
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
if ( ! self::should_add_local_apm_gateways( $settings ) ) {
return true;
}
add_filter(
'woocommerce_payment_gateways',
@ -59,6 +53,9 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
* @psalm-suppress MissingClosureParamType
*/
function ( $methods ) use ( $c ) {
if ( ! self::should_add_local_apm_gateways( $c ) ) {
return $methods;
}
$onboarding_state = $c->get( 'onboarding.state' );
if ( $onboarding_state->current_state() === State::STATE_START ) {
return $methods;
@ -85,6 +82,9 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
* @psalm-suppress MissingClosureParamType
*/
function ( $methods ) use ( $c ) {
if ( ! self::should_add_local_apm_gateways( $c ) ) {
return $methods;
}
if ( ! is_array( $methods ) ) {
return $methods;
}
@ -115,6 +115,9 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
add_action(
'woocommerce_blocks_payment_method_type_registration',
function( PaymentMethodRegistry $payment_method_registry ) use ( $c ): void {
if ( ! self::should_add_local_apm_gateways( $c ) ) {
return;
}
$payment_methods = $c->get( 'ppcp-local-apms.payment-methods' );
foreach ( $payment_methods as $key => $value ) {
$payment_method_registry->register( $c->get( 'ppcp-local-apms.' . $key . '.payment-method' ) );
@ -125,6 +128,9 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
add_filter(
'woocommerce_paypal_payments_localized_script_data',
function ( array $data ) use ( $c ) {
if ( ! self::should_add_local_apm_gateways( $c ) ) {
return $data;
}
$payment_methods = $c->get( 'ppcp-local-apms.payment-methods' );
$default_disable_funding = $data['url_params']['disable-funding'] ?? '';
@ -143,6 +149,9 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
* @psalm-suppress MissingClosureParamType
*/
function( $order_id ) use ( $c ) {
if ( ! self::should_add_local_apm_gateways( $c ) ) {
return;
}
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return;
@ -175,6 +184,9 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
add_action(
'woocommerce_paypal_payments_payment_capture_completed_webhook_handler',
function( WC_Order $wc_order, string $order_id ) use ( $c ) {
if ( ! self::should_add_local_apm_gateways( $c ) ) {
return;
}
$payment_methods = $c->get( 'ppcp-local-apms.payment-methods' );
if (
! $this->is_local_apm( $wc_order->get_payment_method(), $payment_methods )
@ -214,10 +226,12 @@ class LocalAlternativePaymentMethodsModule implements ServiceModule, ExtendingMo
/**
* Check if the local APMs should be added to the available payment gateways.
*
* @param Settings $settings PayPal gateway settings.
* @param ContainerInterface $container Container.
* @return bool
*/
private function should_add_local_apm_gateways( Settings $settings ): bool {
private function should_add_local_apm_gateways( ContainerInterface $container ): bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
return $settings->has( 'enabled' )
&& $settings->get( 'enabled' ) === true
&& $settings->has( 'allow_local_apm_gateways' )

View file

@ -45,34 +45,32 @@ class OnboardingModule implements ServiceModule, ExtendingModule, ExecutableModu
*/
public function run( ContainerInterface $c ): bool {
if ( ! apply_filters(
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
'woocommerce.feature-flags.woocommerce_paypal_payments.settings_enabled',
getenv( 'PCP_SETTINGS_ENABLED' ) === '1'
) || SettingsModule::should_use_the_old_ui()
) {
add_action(
'admin_enqueue_scripts',
function() use ( $c ) {
if (
apply_filters(
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
'woocommerce.feature-flags.woocommerce_paypal_payments.settings_enabled',
getenv( 'PCP_SETTINGS_ENABLED' ) === '1'
) && ! SettingsModule::should_use_the_old_ui()
) {
return;
}
$asset_loader = $c->get( 'onboarding.assets' );
/**
* The OnboardingAssets.
*
* @var OnboardingAssets $asset_loader
*/
add_action(
'admin_enqueue_scripts',
array(
$asset_loader,
'register',
)
);
add_action(
'woocommerce_settings_checkout',
array(
$asset_loader,
'enqueue',
)
);
}
$asset_loader = $c->get( 'onboarding.assets' );
assert( $asset_loader instanceof OnboardingAssets );
$asset_loader->register();
add_action(
'woocommerce_settings_checkout',
array(
$asset_loader,
'enqueue',
)
);
}
);
add_filter(
'woocommerce_form_field',

View file

@ -55,23 +55,21 @@ class OrderTrackingModule implements ServiceModule, ExtendingModule, ExecutableM
* @throws NotFoundException
*/
public function run( ContainerInterface $c ): bool {
$endpoint = $c->get( 'order-tracking.endpoint.controller' );
assert( $endpoint instanceof OrderTrackingEndpoint );
add_action( 'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT, array( $endpoint, 'handle_request' ) );
add_action(
'wc_ajax_' . OrderTrackingEndpoint::ENDPOINT,
function() use ( $c ) {
$c->get( 'order-tracking.endpoint.controller' )->handle_request();
}
);
$asset_loader = $c->get( 'order-tracking.assets' );
assert( $asset_loader instanceof OrderEditPageAssets );
$logger = $c->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
$bearer = $c->get( 'api.bearer' );
add_action(
'init',
function() use ( $asset_loader, $bearer ) {
if ( ! $this->is_tracking_enabled( $bearer ) ) {
function() use ( $asset_loader, $c ) {
if ( ! $this->is_tracking_enabled( $c->get( 'api.bearer' ) ) ) {
return;
}
@ -80,8 +78,8 @@ class OrderTrackingModule implements ServiceModule, ExtendingModule, ExecutableM
);
add_action(
'init',
function() use ( $asset_loader, $bearer ) {
if ( ! $this->is_tracking_enabled( $bearer ) ) {
function() use ( $asset_loader, $c ) {
if ( ! $this->is_tracking_enabled( $c->get( 'api.bearer' ) ) ) {
return;
}
@ -89,9 +87,6 @@ class OrderTrackingModule implements ServiceModule, ExtendingModule, ExecutableM
}
);
$meta_box_renderer = $c->get( 'order-tracking.meta-box.renderer' );
assert( $meta_box_renderer instanceof MetaBoxRenderer );
add_action(
'add_meta_boxes',
/**
@ -103,8 +98,8 @@ class OrderTrackingModule implements ServiceModule, ExtendingModule, ExecutableM
*
* @psalm-suppress MissingClosureParamType
*/
function( string $post_type, $post_or_order_object ) use ( $meta_box_renderer, $bearer ) {
if ( ! $this->is_tracking_enabled( $bearer ) ) {
function( string $post_type, $post_or_order_object ) use ( $c ) {
if ( ! $this->is_tracking_enabled( $c->get( 'api.bearer' ) ) ) {
return;
}
@ -135,6 +130,9 @@ class OrderTrackingModule implements ServiceModule, ExtendingModule, ExecutableM
? wc_get_page_screen_id( 'shop-order' )
: 'shop_order';
$meta_box_renderer = $c->get( 'order-tracking.meta-box.renderer' );
assert( $meta_box_renderer instanceof MetaBoxRenderer );
add_meta_box(
'ppcp_order-tracking',
__( 'PayPal Package Tracking', 'woocommerce-paypal-payments' ),

View file

@ -71,12 +71,12 @@ class PayLaterBlockModule implements ServiceModule, ExtendingModule, ExecutableM
return true;
}
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
add_action(
'init',
function () use ( $c, $settings ): void {
function () use ( $c ): void {
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
$script_handle = 'ppcp-paylater-block';
wp_register_script(
$script_handle,

View file

@ -56,46 +56,46 @@ class PayLaterConfiguratorModule implements ServiceModule, ExtendingModule, Exec
* {@inheritDoc}
*/
public function run( ContainerInterface $c ) : bool {
$is_available = $c->get( 'paylater-configurator.is-available' );
if ( ! $is_available ) {
return true;
}
$current_page_id = $c->get( 'wcgateway.current-ppcp-settings-page-id' );
$is_wc_settings_page = $c->get( 'wcgateway.is-wc-settings-page' );
$messaging_locations = $c->get( 'paylater-configurator.messaging-locations' );
$this->add_paylater_update_notice( $messaging_locations, $is_wc_settings_page, $current_page_id );
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
add_action(
'wc_ajax_' . SaveConfig::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'paylater-configurator.endpoint.save-config' );
assert( $endpoint instanceof SaveConfig );
$endpoint->handle_request();
}
);
add_action(
'wc_ajax_' . GetConfig::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'paylater-configurator.endpoint.get-config' );
assert( $endpoint instanceof GetConfig );
$endpoint->handle_request();
}
);
if ( $current_page_id !== Settings::PAY_LATER_TAB_ID ) {
return true;
}
add_action(
'init',
static function () use ( $c, $settings ) {
static function () use ( $c ) {
$is_available = $c->get( 'paylater-configurator.is-available' );
if ( ! $is_available ) {
return;
}
$current_page_id = $c->get( 'wcgateway.current-ppcp-settings-page-id' );
$is_wc_settings_page = $c->get( 'wcgateway.is-wc-settings-page' );
$messaging_locations = $c->get( 'paylater-configurator.messaging-locations' );
self::add_paylater_update_notice( $messaging_locations, $is_wc_settings_page, $current_page_id );
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
add_action(
'wc_ajax_' . SaveConfig::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'paylater-configurator.endpoint.save-config' );
assert( $endpoint instanceof SaveConfig );
$endpoint->handle_request();
}
);
add_action(
'wc_ajax_' . GetConfig::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'paylater-configurator.endpoint.get-config' );
assert( $endpoint instanceof GetConfig );
$endpoint->handle_request();
}
);
if ( $current_page_id !== Settings::PAY_LATER_TAB_ID ) {
return;
}
wp_enqueue_script(
'ppcp-paylater-configurator-lib',
'https://www.paypalobjects.com/merchant-library/merchant-configurator.js',
@ -165,7 +165,7 @@ class PayLaterConfiguratorModule implements ServiceModule, ExtendingModule, Exec
*
* @return void
*/
private function add_paylater_update_notice( array $message_locations, bool $is_settings_page, string $current_page_id ) : void {
private static function add_paylater_update_notice( array $message_locations, bool $is_settings_page, string $current_page_id ) : void {
// The message must be registered on any WC-Settings page, except for the Pay Later page.
if ( ! $is_settings_page || Settings::PAY_LATER_TAB_ID === $current_page_id ) {
return;

View file

@ -99,12 +99,11 @@ class PayLaterWCBlocksModule implements ServiceModule, ExtendingModule, Executab
return true;
}
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
add_action(
'init',
function () use ( $c, $settings ): void {
function () use ( $c ): void {
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
$config_factory = $c->get( 'paylater-configurator.factory.config' );
assert( $config_factory instanceof ConfigFactory );
@ -186,47 +185,52 @@ class PayLaterWCBlocksModule implements ServiceModule, ExtendingModule, Executab
2
);
/**
* Cannot return false for this path.
*
* @psalm-suppress PossiblyFalseArgument
*/
if ( function_exists( 'register_block_type' ) ) {
register_block_type(
dirname( realpath( __FILE__ ), 2 ) . '/resources/js/CartPayLaterMessagesBlock',
array(
'render_callback' => function ( array $attributes ) use ( $c ) {
return PayLaterWCBlocksUtils::render_paylater_block(
$attributes['blockId'] ?? 'woocommerce-paypal-payments/cart-paylater-messages',
$attributes['ppcpId'] ?? 'ppcp-cart-paylater-messages',
'cart',
$c
);
},
)
);
}
add_action(
'init',
function () use ( $c ): void {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
/**
* Cannot return false for this path.
*
* @psalm-suppress PossiblyFalseArgument
*/
if ( function_exists( 'register_block_type' ) ) {
register_block_type(
dirname( realpath( __FILE__ ), 2 ) . '/resources/js/CheckoutPayLaterMessagesBlock',
array(
'render_callback' => function ( array $attributes ) use ( $c ) {
return PayLaterWCBlocksUtils::render_paylater_block(
$attributes['blockId'] ?? 'woocommerce-paypal-payments/checkout-paylater-messages',
$attributes['ppcpId'] ?? 'ppcp-checkout-paylater-messages',
'checkout',
$c
);
},
)
);
}
/**
* Cannot return false for this path.
*
* @psalm-suppress PossiblyFalseArgument
*/
register_block_type(
dirname( realpath( __FILE__ ), 2 ) . '/resources/js/CartPayLaterMessagesBlock',
array(
'render_callback' => function ( array $attributes ) use ( $c ) {
return PayLaterWCBlocksUtils::render_paylater_block(
$attributes['blockId'] ?? 'woocommerce-paypal-payments/cart-paylater-messages',
$attributes['ppcpId'] ?? 'ppcp-cart-paylater-messages',
'cart',
$c
);
},
)
);
/**
* Cannot return false for this path.
*
* @psalm-suppress PossiblyFalseArgument
*/
register_block_type(
dirname( realpath( __FILE__ ), 2 ) . '/resources/js/CheckoutPayLaterMessagesBlock',
array(
'render_callback' => function ( array $attributes ) use ( $c ) {
return PayLaterWCBlocksUtils::render_paylater_block(
$attributes['blockId'] ?? 'woocommerce-paypal-payments/checkout-paylater-messages',
$attributes['ppcpId'] ?? 'ppcp-checkout-paylater-messages',
'checkout',
$c
);
},
)
);
}
);
// This is a fallback for the default Cart block that haven't been saved with the inserted Pay Later messaging block.
add_filter(
@ -271,7 +275,7 @@ class PayLaterWCBlocksModule implements ServiceModule, ExtendingModule, Executab
if ( self::is_under_cart_totals_placement_enabled() ) {
add_action(
'enqueue_block_editor_assets',
function () use ( $c, $settings ): void {
function () use ( $c ): void {
$handle = 'ppcp-checkout-paylater-block-editor-inserter';
$path = $c->get( 'paylater-wc-blocks.url' ) . 'assets/js/cart-paylater-block-inserter.js';

View file

@ -599,11 +599,11 @@ class PayPalSubscriptionsModule implements ServiceModule, ExtendingModule, Execu
}
);
$endpoint = $c->get( 'paypal-subscriptions.deactivate-plan-endpoint' );
assert( $endpoint instanceof DeactivatePlanEndpoint );
add_action(
'wc_ajax_' . DeactivatePlanEndpoint::ENDPOINT,
array( $endpoint, 'handle_request' )
function() use ( $c ) {
$c->get( 'paypal-subscriptions.deactivate-plan-endpoint' )->handle_request();
}
);
add_action(

View file

@ -63,33 +63,26 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
return true;
}
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
$billing_agreements_endpoint = $c->get( 'api.endpoint.billing-agreements' );
assert( $billing_agreements_endpoint instanceof BillingAgreementsEndpoint );
add_action(
'woocommerce_paypal_payments_gateway_migrate_on_update',
function() use ( $settings, $billing_agreements_endpoint ) {
function() use ( $c ) {
$billing_agreements_endpoint = $c->get( 'api.endpoint.billing-agreements' );
assert( $billing_agreements_endpoint instanceof BillingAgreementsEndpoint );
$reference_transaction_enabled = $billing_agreements_endpoint->reference_transaction_enabled();
if ( $reference_transaction_enabled !== true ) {
$settings->set( 'vault_enabled', false );
$settings->persist();
$c->get( 'wcgateway.settings' )->set( 'vault_enabled', false );
$c->get( 'wcgateway.settings' )->persist();
}
}
);
if (
( ! $settings->has( 'vault_enabled' ) || ! $settings->get( 'vault_enabled' ) )
&& ( ! $settings->has( 'vault_enabled_dcc' ) || ! $settings->get( 'vault_enabled_dcc' ) )
) {
return true;
}
add_filter(
'woocommerce_paypal_payments_localized_script_data',
function( array $localized_script_data ) use ( $c ) {
if ( ! self::vault_enabled( $c ) ) {
return $localized_script_data;
}
$subscriptions_helper = $c->get( 'wc-subscriptions.helper' );
assert( $subscriptions_helper instanceof SubscriptionHelper );
if ( ! is_user_logged_in() && ! $subscriptions_helper->cart_contains_subscription() ) {
@ -109,12 +102,11 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
// Adds attributes needed to save payment method.
add_filter(
'ppcp_create_order_request_body_data',
function( array $data, string $payment_method, array $request_data ) use ( $settings ): array {
function( array $data, string $payment_method, array $request_data ) use ( $c ): array {
if ( ! self::vault_enabled( $c ) ) {
return $data;
}
if ( $payment_method === CreditCardGateway::ID ) {
if ( ! $settings->has( 'vault_enabled_dcc' ) || ! $settings->get( 'vault_enabled_dcc' ) ) {
return $data;
}
$save_payment_method = $request_data['save_payment_method'] ?? false;
if ( $save_payment_method ) {
$data['payment_source'] = array(
@ -141,10 +133,6 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
}
if ( $payment_method === PayPalGateway::ID ) {
if ( ! $settings->has( 'vault_enabled' ) || ! $settings->get( 'vault_enabled' ) ) {
return $data;
}
$funding_source = $request_data['funding_source'] ?? null;
if ( $funding_source && $funding_source === 'venmo' ) {
@ -197,6 +185,9 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_action(
'woocommerce_paypal_payments_after_order_processor',
function( WC_Order $wc_order, Order $order ) use ( $c ) {
if ( ! self::vault_enabled( $c ) ) {
return;
}
$payment_source = $order->payment_source();
assert( $payment_source instanceof PaymentSource );
@ -259,13 +250,30 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
2
);
add_filter( 'woocommerce_paypal_payments_disable_add_payment_method', '__return_false' );
add_filter( 'woocommerce_paypal_payments_should_render_card_custom_fields', '__return_false' );
add_filter(
'woocommerce_paypal_payments_disable_add_payment_method',
function ( bool $value ) use ( $c ): bool {
if ( ! self::vault_enabled( $c ) ) {
return $value;
}
return false;
}
);
add_filter(
'woocommerce_paypal_payments_should_render_card_custom_fields',
function ( bool $value ) use ( $c ): bool {
if ( ! self::vault_enabled( $c ) ) {
return $value;
}
return false;
}
);
add_action(
'wp_enqueue_scripts',
function() use ( $c ) {
if ( ! is_user_logged_in() || ! ( $this->is_add_payment_method_page() || $this->is_subscription_change_payment_method_page() ) ) {
if ( ! is_user_logged_in() || ! ( $this->is_add_payment_method_page() || $this->is_subscription_change_payment_method_page() ) || ! self::vault_enabled( $c ) ) {
return;
}
@ -355,8 +363,8 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_action(
'woocommerce_add_payment_method_form_bottom',
function () {
if ( ! is_user_logged_in() || ! is_add_payment_method_page() ) {
function () use ( $c ) {
if ( ! is_user_logged_in() || ! is_add_payment_method_page() || ! self::vault_enabled( $c ) ) {
return;
}
@ -367,6 +375,9 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_action(
'wc_ajax_' . CreateSetupToken::ENDPOINT,
static function () use ( $c ) {
if ( ! self::vault_enabled( $c ) ) {
return;
}
$endpoint = $c->get( 'save-payment-methods.endpoint.create-setup-token' );
assert( $endpoint instanceof CreateSetupToken );
@ -377,6 +388,9 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_action(
'wc_ajax_' . CreatePaymentToken::ENDPOINT,
static function () use ( $c ) {
if ( ! self::vault_enabled( $c ) ) {
return;
}
$endpoint = $c->get( 'save-payment-methods.endpoint.create-payment-token' );
assert( $endpoint instanceof CreatePaymentToken );
@ -387,6 +401,9 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_action(
'wc_ajax_' . CreatePaymentTokenForGuest::ENDPOINT,
static function () use ( $c ) {
if ( ! self::vault_enabled( $c ) ) {
return;
}
$endpoint = $c->get( 'save-payment-methods.endpoint.create-payment-token-for-guest' );
assert( $endpoint instanceof CreatePaymentTokenForGuest );
@ -397,6 +414,9 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_action(
'woocommerce_paypal_payments_before_delete_payment_token',
function( string $token_id ) use ( $c ) {
if ( ! self::vault_enabled( $c ) ) {
return;
}
try {
$endpoint = $c->get( 'api.endpoint.payment-tokens' );
assert( $endpoint instanceof PaymentTokensEndpoint );
@ -419,13 +439,11 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_filter(
'woocommerce_paypal_payments_credit_card_gateway_supports',
function( array $supports ) use ( $c ): array {
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );
if ( $settings->has( 'vault_enabled_dcc' ) && $settings->get( 'vault_enabled_dcc' ) ) {
$supports[] = 'tokenization';
$supports[] = 'add_payment_method';
if ( ! self::vault_enabled( $c ) ) {
return $supports;
}
$supports[] = 'tokenization';
$supports[] = 'add_payment_method';
return $supports;
}
@ -433,7 +451,10 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_filter(
'woocommerce_paypal_payments_save_payment_methods_eligible',
function() {
function( bool $value ) use ( $c ): bool {
if ( ! self::vault_enabled( $c ) ) {
return $value;
}
return true;
}
);
@ -481,4 +502,24 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
return $localized_script_data;
}
/**
* Checks whether the vault functionality is enabled based on configuration settings.
*
* @param ContainerInterface $container The dependency injection container from which settings can be retrieved.
*
* @return bool Returns true if either 'vault_enabled' or 'vault_enabled_dcc' settings are enabled; otherwise, false.
*/
private static function vault_enabled( ContainerInterface $container ): bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
if (
( ! $settings->has( 'vault_enabled' ) || ! $settings->get( 'vault_enabled' ) )
&& ( ! $settings->has( 'vault_enabled_dcc' ) || ! $settings->get( 'vault_enabled_dcc' ) )
) {
return false;
}
return true;
}
}

View file

@ -43,33 +43,29 @@ class UninstallModule implements ServiceModule, ExtendingModule, ExecutableModul
* {@inheritDoc}
*/
public function run( ContainerInterface $container ): bool {
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( Settings::CONNECTION_TAB_ID === $page_id ) {
$this->registerClearDatabaseAssets( $container->get( 'uninstall.clear-db-assets' ) );
}
add_action(
'init',
static function () use ( $container ) {
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( Settings::CONNECTION_TAB_ID === $page_id ) {
$container->get( 'uninstall.clear-db-assets' )->register();
add_action( 'admin_enqueue_scripts', array( $container->get( 'uninstall.clear-db-assets' ), 'enqueue' ) );
}
$request_data = $container->get( 'button.request-data' );
$clear_db = $container->get( 'uninstall.clear-db' );
$clear_db_endpoint = $container->get( 'uninstall.clear-db-endpoint' );
$option_names = $container->get( 'uninstall.ppcp-all-option-names' );
$scheduled_action_names = $container->get( 'uninstall.ppcp-all-scheduled-action-names' );
$action_names = $container->get( 'uninstall.ppcp-all-action-names' );
$request_data = $container->get( 'button.request-data' );
$clear_db = $container->get( 'uninstall.clear-db' );
$clear_db_endpoint = $container->get( 'uninstall.clear-db-endpoint' );
$option_names = $container->get( 'uninstall.ppcp-all-option-names' );
$scheduled_action_names = $container->get( 'uninstall.ppcp-all-scheduled-action-names' );
$action_names = $container->get( 'uninstall.ppcp-all-action-names' );
$this->handleClearDbAjaxRequest( $request_data, $clear_db, $clear_db_endpoint, $option_names, $scheduled_action_names, $action_names );
self::handleClearDbAjaxRequest( $request_data, $clear_db, $clear_db_endpoint, $option_names, $scheduled_action_names, $action_names );
}
);
return true;
}
/**
* Registers the assets for clear database functionality.
*
* @param ClearDatabaseAssets $asset_loader The clear database functionality asset loader.
*/
protected function registerClearDatabaseAssets( ClearDatabaseAssets $asset_loader ): void {
add_action( 'init', array( $asset_loader, 'register' ) );
add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) );
}
/**
* Handles the AJAX request to clear the database.
*
@ -80,7 +76,7 @@ class UninstallModule implements ServiceModule, ExtendingModule, ExecutableModul
* @param string[] $scheduled_action_names The list of scheduled action names.
* @param string[] $action_names The list of action names.
*/
protected function handleClearDbAjaxRequest(
protected static function handleClearDbAjaxRequest(
RequestData $request_data,
ClearDatabaseInterface $clear_db,
string $nonce,

View file

@ -51,10 +51,16 @@ class VaultingModule implements ServiceModule, ExtendingModule, ExecutableModule
* @throws NotFoundException When service could not be found.
*/
public function run( ContainerInterface $container ): bool {
$listener = $container->get( 'vaulting.customer-approval-listener' );
assert( $listener instanceof CustomerApprovalListener );
$listener->listen();
add_action(
'woocommerce_init',
function() use ( $container ) {
$listener = $container->get( 'vaulting.customer-approval-listener' );
assert( $listener instanceof CustomerApprovalListener );
$listener->listen();
}
);
$subscription_helper = $container->get( 'wc-subscriptions.helper' );
add_action(

View file

@ -174,23 +174,17 @@ class SettingsPageAssets {
* @return void
*/
public function register_assets(): void {
add_action(
'admin_enqueue_scripts',
function() {
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}
if ( $this->is_settings_page ) {
$this->register_admin_assets();
}
if ( $this->is_paypal_payment_method_page ) {
$this->register_paypal_admin_assets();
}
}
);
if ( $this->is_settings_page ) {
$this->register_admin_assets();
}
if ( $this->is_paypal_payment_method_page ) {
$this->register_paypal_admin_assets();
}
}
/**

View file

@ -181,34 +181,43 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul
}
);
if ( $c->has( 'wcgateway.url' ) ) {
$settings_status = $c->get( 'wcgateway.settings.status' );
assert( $settings_status instanceof SettingsStatus );
add_action(
'admin_enqueue_scripts',
function () use ( $c ) {
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}
if ( ! $c->has( 'wcgateway.url' ) ) {
return;
}
$settings_status = $c->get( 'wcgateway.settings.status' );
assert( $settings_status instanceof SettingsStatus );
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
$dcc_configuration = $c->get( 'wcgateway.configuration.dcc' );
assert( $dcc_configuration instanceof DCCGatewayConfiguration );
$dcc_configuration = $c->get( 'wcgateway.configuration.dcc' );
assert( $dcc_configuration instanceof DCCGatewayConfiguration );
$assets = new SettingsPageAssets(
$c->get( 'wcgateway.url' ),
$c->get( 'ppcp.asset-version' ),
$c->get( 'wc-subscriptions.helper' ),
$c->get( 'button.client_id_for_admin' ),
$c->get( 'api.shop.currency.getter' ),
$c->get( 'api.shop.country' ),
$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.is-ppcp-settings-page' ),
$dcc_configuration->is_enabled(),
$c->get( 'api.endpoint.billing-agreements' ),
$c->get( 'wcgateway.is-ppcp-settings-payment-methods-page' )
);
$assets->register_assets();
}
$assets = new SettingsPageAssets(
$c->get( 'wcgateway.url' ),
$c->get( 'ppcp.asset-version' ),
$c->get( 'wc-subscriptions.helper' ),
$c->get( 'button.client_id_for_admin' ),
$c->get( 'api.shop.currency.getter' ),
$c->get( 'api.shop.country' ),
$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.is-ppcp-settings-page' ),
$dcc_configuration->is_enabled(),
$c->get( 'api.endpoint.billing-agreements' ),
$c->get( 'wcgateway.is-ppcp-settings-payment-methods-page' )
);
$assets->register_assets();
}
);
add_filter(
Repository::NOTICES_FILTER,

View file

@ -56,8 +56,6 @@ class WebhookModule implements ServiceModule, FactoryModule, ExtendingModule, Ex
* {@inheritDoc}
*/
public function run( ContainerInterface $container ): bool {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
add_action(
'rest_api_init',
@ -127,38 +125,35 @@ class WebhookModule implements ServiceModule, FactoryModule, ExtendingModule, Ex
}
);
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( Settings::CONNECTION_TAB_ID === $page_id ) {
$asset_loader = $container->get( 'webhook.status.assets' );
assert( $asset_loader instanceof WebhooksStatusPageAssets );
add_action(
'init',
array( $asset_loader, 'register' )
);
add_action(
'admin_enqueue_scripts',
array( $asset_loader, 'enqueue' )
);
try {
$webhooks = $container->get( 'webhook.status.registered-webhooks' );
$state = $container->get( 'onboarding.state' );
if ( empty( $webhooks ) && $state->current_state() >= State::STATE_ONBOARDED ) {
$registrar = $container->get( 'webhook.registrar' );
assert( $registrar instanceof WebhookRegistrar );
// Looks like we cannot call rest_url too early.
add_action(
'init',
function () use ( $registrar ) {
$registrar->register();
}
);
add_action(
'init',
function () use ( $container ) {
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( Settings::CONNECTION_TAB_ID !== $page_id ) {
return;
}
$asset_loader = $container->get( 'webhook.status.assets' );
assert( $asset_loader instanceof WebhooksStatusPageAssets );
$asset_loader->register();
add_action(
'admin_enqueue_scripts',
array( $asset_loader, 'enqueue' )
);
try {
$webhooks = $container->get( 'webhook.status.registered-webhooks' );
$state = $container->get( 'onboarding.state' );
if ( empty( $webhooks ) && $state->current_state() >= State::STATE_ONBOARDED ) {
$registrar = $container->get( 'webhook.registrar' );
assert( $registrar instanceof WebhookRegistrar );
$registrar->register();
}
} catch ( Exception $exception ) {
$container->get( 'woocommerce.logger.woocommerce' )->error( 'Failed to load webhooks list: ' . $exception->getMessage() );
}
} catch ( Exception $exception ) {
$logger->error( 'Failed to load webhooks list: ' . $exception->getMessage() );
}
}
);
add_action(
'woocommerce_paypal_payments_gateway_migrate',

View file

@ -56,16 +56,19 @@ class FilePathPluginFactory implements FilePathPluginFactoryInterface {
);
}
if ( ! function_exists( 'get_plugin_data' ) ) {
/**
* Skip check for WP files.
*
* @psalm-suppress MissingFile
*/
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$default_headers = array(
'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI',
'Version' => 'Version',
'Description' => 'Description',
'TextDomain' => 'Text Domain',
'RequiresWP' => 'Requires at least',
'RequiresPHP' => 'Requires PHP',
'RequiresPlugins' => 'Requires Plugins',
);
$plugin_data = \get_file_data( $filePath, $default_headers, 'plugin' );
$plugin_data = get_plugin_data( $filePath, false );
if ( empty( $plugin_data ) ) {
throw new UnexpectedValueException(
sprintf(
@ -98,7 +101,7 @@ class FilePathPluginFactory implements FilePathPluginFactoryInterface {
$this->create_version( $plugin_data['Version'] ),
$base_dir,
$base_name,
$plugin_data['Title'],
$plugin_data['PluginURI'],
$plugin_data['Description'],
$text_domain,
$this->create_version( $plugin_data['RequiresPHP'] ),

View file

@ -46,11 +46,11 @@ class Plugin implements PluginInterface {
protected $base_name;
/**
* The plugin title.
* The plugin URI.
*
* @var string
*/
protected $title;
protected $plugin_uri;
/**
* The plugin description.
@ -87,7 +87,7 @@ class Plugin implements PluginInterface {
* @param VersionInterface $version The plugin version.
* @param string $base_dir The path to the plugin base directory.
* @param string $base_name The plugin base name.
* @param string $title The plugin title.
* @param string $plugin_uri The plugin URI.
* @param string $description The plugin description.
* @param string $text_domain The text domain of this plugin.
* @param VersionInterface $min_php_version The minimal version of PHP required by this plugin.
@ -98,7 +98,7 @@ class Plugin implements PluginInterface {
VersionInterface $version,
string $base_dir,
string $base_name,
string $title,
string $plugin_uri,
string $description,
string $text_domain,
VersionInterface $min_php_version,
@ -109,7 +109,7 @@ class Plugin implements PluginInterface {
$this->version = $version;
$this->base_dir = $base_dir;
$this->base_name = $base_name;
$this->title = $title;
$this->plugin_uri = $plugin_uri;
$this->text_domain = $text_domain;
$this->min_php_version = $min_php_version;
$this->min_wp_version = $min_wp_version;
@ -126,7 +126,27 @@ class Plugin implements PluginInterface {
* The plugin description.
*/
public function getDescription(): string {
return $this->description;
$allowed_tags = array(
'abbr' => array( 'title' => true ),
'acronym' => array( 'title' => true ),
'code' => true,
'em' => true,
'strong' => true,
'a' => array(
'href' => true,
'title' => true,
),
);
// phpcs:disable
$text = \__( $this->description, $this->text_domain );
/**
* @psalm-suppress InvalidArgument
*/
return wp_kses( $text, $allowed_tags );
// phpcs:enable
}
/**
@ -157,11 +177,18 @@ class Plugin implements PluginInterface {
return $this->text_domain;
}
/**
* The plugin URI.
*/
public function getUri(): string {
return esc_url( $this->plugin_uri );
}
/**
* The plugin title.
*/
public function getTitle(): string {
return $this->title;
return '<a href="' . $this->getUri() . '">' . $this->getName() . '</a>';
}
/**

View file

@ -141,7 +141,10 @@ class PaymentTokenEndpointTest extends TestCase
$this->sut->for_user($id);
}
public function testDeleteToken()
/**
* @doesNotPerformAssertions
*/
public function testDeleteToken()
{
$paymentToken = Mockery::mock(PaymentToken::class);
$paymentToken->shouldReceive('id')

View file

@ -31,6 +31,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
when('wc_string_to_bool')->alias(function ($string) {
return is_bool( $string ) ? $string : ( 'yes' === strtolower( $string ) || 1 === $string || 'true' === strtolower( $string ) || '1' === $string );
});
when('get_file_data')->justReturn(['Version' => '1.0','PluginURI' => 'https://github.com/woocommerce/woocommerce-paypal-payments']);
when('get_plugin_data')->justReturn(['Version' => '1.0']);
when('plugin_basename')->justReturn('woocommerce-paypal-payments/woocommerce-paypal-payments.php');
when('get_transient')->returnArg();

View file

@ -1,50 +0,0 @@
<?php
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingAgreementsEndpoint;
use WooCommerce\PayPalCommerce\Helper\CurrencyGetterStub;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\when;
use Mockery;
class SettingsPagesAssetsTest extends TestCase
{
public function testRegisterAssets()
{
$moduleUrl = 'http://example.com/wp-content/plugins/woocommerce-paypal-payments/modules/ppcp-wc-gateway';
$modulePath = '/var/www/html/wp-content/plugins/woocommerce-paypal-payments/modules/ppcp-wc-gateway';
$subscriptionsHelper = Mockery::mock(SubscriptionHelper::class);
$billingAgreementEndpoint = Mockery::mock(BillingAgreementsEndpoint::class);
$testee = new SettingsPageAssets(
$moduleUrl,
$modulePath,
$subscriptionsHelper,
'123',
new CurrencyGetterStub(),
'DE',
Mockery::mock(Environment::class),
true,
array(),
array(),
true,
false,
$billingAgreementEndpoint,
true
);
when('is_admin')
->justReturn(true);
when('wp_doing_ajax')
->justReturn(false);
$testee->register_assets();
self::assertSame(has_action('admin_enqueue_scripts', "function()"), 10);
}
}

View file

@ -22,6 +22,9 @@ class SettingsListenerTest extends ModularTestCase
parent::setUp();
}
/**
* @doesNotPerformAssertions
*/
public function testListen()
{
$settings = Mockery::mock(Settings::class);

View file

@ -90,32 +90,28 @@ define( 'PPCP_PAYPAL_BN_CODE', 'Woo_PPCP' );
'plugins_loaded',
function () {
init();
add_action(
'init',
function () {
$current_plugin_version = (string) PPCP::container()->get( 'ppcp.plugin' )->getVersion();
$installed_plugin_version = get_option( 'woocommerce-ppcp-version' );
if ( $installed_plugin_version !== $current_plugin_version ) {
/**
* The hook fired when the plugin is installed or updated.
*/
do_action( 'woocommerce_paypal_payments_gateway_migrate', $installed_plugin_version );
if ( ! function_exists( 'get_plugin_data' ) ) {
/**
* Skip check for WP files.
*
* @psalm-suppress MissingFile
*/
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data( __DIR__ . '/woocommerce-paypal-payments.php', false );
$plugin_version = $plugin_data['Version'] ?? null;
$installed_plugin_version = get_option( 'woocommerce-ppcp-version' );
if ( $installed_plugin_version !== $plugin_version ) {
/**
* The hook fired when the plugin is installed or updated.
*/
do_action( 'woocommerce_paypal_payments_gateway_migrate', $installed_plugin_version );
if ( $installed_plugin_version ) {
/**
* The hook fired when the plugin is updated.
*/
do_action( 'woocommerce_paypal_payments_gateway_migrate_on_update' );
}
update_option( 'woocommerce-ppcp-version', $plugin_version );
}
if ( $installed_plugin_version ) {
/**
* The hook fired when the plugin is updated.
*/
do_action( 'woocommerce_paypal_payments_gateway_migrate_on_update' );
}
update_option( 'woocommerce-ppcp-version', $current_plugin_version );
}
},
-1
);
}
);
register_activation_hook(