Update AdditionalPayments task to use default gateway suggestion (#50674)

* Add new default spec function

* Changelog

* Add a new method to get cached or default

* Add test

* Update comment

* Revert comment

* Update docblock

* Lint

* Typo

* Set specs to default when marketplace suggestion is disabled

* Update tests
This commit is contained in:
Ilyas Foo 2024-08-20 14:22:58 +08:00 committed by GitHub
parent 34d40f9a63
commit ad1b233a9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 117 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Update AdditionalPayments task to use default payment gateways

View File

@ -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 ) {

View File

@ -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.
*/

View File

@ -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.

View File

@ -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.
*

View File

@ -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.
*/