diff --git a/plugins/woocommerce/changelog/fix-50543-additional-payments-use-default-gateways b/plugins/woocommerce/changelog/fix-50543-additional-payments-use-default-gateways new file mode 100644 index 00000000000..ef2ef6f4773 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-50543-additional-payments-use-default-gateways @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Update AdditionalPayments task to use default payment gateways diff --git a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/AdditionalPayments.php b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/AdditionalPayments.php index 672b9a4a932..f5cdd68fedd 100644 --- a/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/AdditionalPayments.php +++ b/plugins/woocommerce/src/Admin/Features/OnboardingTasks/Tasks/AdditionalPayments.php @@ -188,7 +188,7 @@ class AdditionalPayments extends Payments { */ private static function get_suggestion_gateways( $filter_by = 'category_additional' ) { $country = wc_get_base_location()['country']; - $plugin_suggestions = Init::get_suggestions(); + $plugin_suggestions = Init::get_cached_or_default_suggestions(); $plugin_suggestions = array_filter( $plugin_suggestions, function( $plugin ) use ( $country, $filter_by ) { diff --git a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php index 581f86b80b8..b6a7712aa18 100644 --- a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php +++ b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/Init.php @@ -61,6 +61,31 @@ class Init extends RemoteSpecsEngine { return $specs_to_return; } + /** + * Gets either cached or default suggestions. + * + * @return array + */ + public static function get_cached_or_default_suggestions() { + $specs = 'no' === get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) + ? DefaultPaymentGateways::get_all() + : PaymentGatewaySuggestionsDataSourcePoller::get_instance()->get_cached_specs(); + + if ( ! is_array( $specs ) || 0 === count( $specs ) ) { + $specs = DefaultPaymentGateways::get_all(); + } + /** + * Allows filtering of payment gateway suggestion specs + * + * @since 6.4.0 + * + * @param array Gateway specs. + */ + $specs = apply_filters( 'woocommerce_admin_payment_gateway_suggestion_specs', $specs ); + $results = EvaluateSuggestion::evaluate_specs( $specs ); + return $results['suggestions']; + } + /** * Delete the specs transient. */ diff --git a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/PaymentGatewaySuggestionsDataSourcePoller.php b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/PaymentGatewaySuggestionsDataSourcePoller.php index 5e3608b635d..d53ac724afd 100644 --- a/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/PaymentGatewaySuggestionsDataSourcePoller.php +++ b/plugins/woocommerce/src/Admin/Features/PaymentGatewaySuggestions/PaymentGatewaySuggestionsDataSourcePoller.php @@ -2,7 +2,7 @@ namespace Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions; -use Automattic\WooCommerce\Admin\DataSourcePoller; +use Automattic\WooCommerce\Admin\RemoteSpecs\DataSourcePoller; /** * Specs data source poller class for payment gateway suggestions. diff --git a/plugins/woocommerce/src/Admin/RemoteSpecs/DataSourcePoller.php b/plugins/woocommerce/src/Admin/RemoteSpecs/DataSourcePoller.php index 0769d65ffa6..d0812fd5ffa 100644 --- a/plugins/woocommerce/src/Admin/RemoteSpecs/DataSourcePoller.php +++ b/plugins/woocommerce/src/Admin/RemoteSpecs/DataSourcePoller.php @@ -126,6 +126,29 @@ abstract class DataSourcePoller { return false !== $specs ? $specs : array(); } + /** + * Gets specs from cache if it exists. + * + * @return array list of specs. + */ + public function get_cached_specs() { + $locale = get_user_locale(); + $specs_group = get_transient( $this->args['transient_name'] ) ?? array(); + $specs = isset( $specs_group[ $locale ] ) ? $specs_group[ $locale ] : null; + + /** + * Filter specs. + * + * @param array $specs List of specs. + * @param string $this->id Spec identifier. + * + * @since 8.8.0 + */ + $specs = apply_filters( self::FILTER_NAME_SPECS, $specs, $this->id ); + + return false !== $specs ? $specs : array(); + } + /** * Reads the data sources for specs and persists those specs. * diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php index 74f18eb6f30..728c78ce286 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/features/payment-gateway-suggestions/payment-gateway-suggestions.php @@ -117,6 +117,69 @@ class WC_Admin_Tests_PaymentGatewaySuggestions_Init extends WC_Unit_Test_Case { $this->assertEquals( $expected_suggestions[1]['id'], $suggestions[1]->id ); } + /** + * Test that specs are read from cache when they exist. + */ + public function test_cached_or_default_suggestions_when_cache_exist() { + // Arrange. + $expected_suggestions = array( + array( + 'id' => 'mock-gateway1', + ), + array( + 'id' => 'mock-gateway2', + ), + ); + set_transient( + 'woocommerce_admin_' . PaymentGatewaySuggestionsDataSourcePoller::ID . '_specs', + array( + 'en_US' => $expected_suggestions, + ) + ); + + // Act. + $suggestions = PaymentGatewaySuggestions::get_cached_or_default_suggestions(); + + // Assert. + $this->assertCount( count( $expected_suggestions ), $suggestions ); + $this->assertEquals( $expected_suggestions[0]['id'], $suggestions[0]->id ); + $this->assertEquals( $expected_suggestions[1]['id'], $suggestions[1]->id ); + } + + /** + * Test that specs are read from default when cache is empty. + */ + public function test_cached_or_default_suggestions_when_cache_empty() { + // Arrange. + PaymentGatewaySuggestionsDataSourcePoller::get_instance()->delete_specs_transient(); + + // Act. + $suggestions = PaymentGatewaySuggestions::get_cached_or_default_suggestions(); + + // Assert. + $default_suggestions = EvaluateSuggestion::evaluate_specs( DefaultPaymentGateways::get_all() )['suggestions']; + + $this->assertEquals( $default_suggestions, $suggestions ); + } + + + /** + * Test that default gateways are provided when remote sources don't exist. + */ + public function test_cached_or_default_suggestions_when_marketplace_suggestions_off() { + // Arrange. + update_option( 'woocommerce_show_marketplace_suggestions', 'no' ); + PaymentGatewaySuggestionsDataSourcePoller::get_instance()->delete_specs_transient(); + + // Act. + $suggestions = PaymentGatewaySuggestions::get_cached_or_default_suggestions(); + $default_suggestions = EvaluateSuggestion::evaluate_specs( DefaultPaymentGateways::get_all() )['suggestions']; + + // Assert. + $this->assertEquals( $suggestions, $default_suggestions ); + } + + /** * Test that non-matched suggestions are not shown. */