Skip to content

Commit

Permalink
Sync MCCY settings interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo committed Oct 25, 2024
1 parent 35865b0 commit 6792d84
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 56 deletions.
42 changes: 42 additions & 0 deletions includes/class-wc-payments-settings-service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* WooCommerce Payments WC_Payments_Settings_Service Class
*
* @package WooCommerce\Payments
*/

use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;

defined( 'ABSPATH' ) || exit;

/**
* WC_Payments_Settings_Service.
*/
class WC_Payments_Settings_Service implements MultiCurrencySettingsInterface {
/**
* Checks if dev mode is enabled.
*
* @return bool
*/
public function is_dev_mode(): bool {
return WC_Payments::mode()->is_dev();
}

/**
* Gets the plugin file path.
*
* @return string
*/
public function get_plugin_file_path(): string {
return WCPAY_PLUGIN_FILE;
}

/**
* Gets the plugin version.
*
* @return string
*/
public function get_plugin_version(): string {
return WCPAY_VERSION_NUMBER;
}
}
34 changes: 18 additions & 16 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class WC_Payments {
*/
private static $localization_service;

/**
* Instance of WC_Payments_Settings_Service, created in init function
*
* @var WC_Payments_Settings_Service
*/
private static $settings_service;

/**
* Instance of WC_Payments_Dependency_Service, created in init function
*
Expand Down Expand Up @@ -476,6 +483,7 @@ public static function init() {
include_once __DIR__ . '/class-wc-payments-onboarding-service.php';
include_once __DIR__ . '/class-experimental-abtest.php';
include_once __DIR__ . '/class-wc-payments-localization-service.php';
include_once __DIR__ . '/class-wc-payments-settings-service.php';
include_once __DIR__ . '/in-person-payments/class-wc-payments-in-person-payments-receipts-service.php';
include_once __DIR__ . '/class-wc-payments-order-service.php';
include_once __DIR__ . '/class-wc-payments-order-success-page.php';
Expand Down Expand Up @@ -529,6 +537,7 @@ public static function init() {
self::$fraud_service = new WC_Payments_Fraud_Service( self::$api_client, self::$customer_service, self::$account, self::$session_service, self::$database_cache );
self::$in_person_payments_receipts_service = new WC_Payments_In_Person_Payments_Receipts_Service();
self::$localization_service = new WC_Payments_Localization_Service();
self::$settings_service = new WC_Payments_Settings_Service();
self::$failed_transaction_rate_limiter = new Session_Rate_Limiter( Session_Rate_Limiter::SESSION_KEY_DECLINED_CARD_REGISTRY, 5, 10 * MINUTE_IN_SECONDS );
self::$order_success_page = new WC_Payments_Order_Success_Page();
self::$woopay_util = new WooPay_Utilities();
Expand Down Expand Up @@ -1335,6 +1344,15 @@ public static function get_localization_service() {
return self::$localization_service;
}

/**
* Returns the WC_Payments_Settings_Service
*
* @return WC_Payments_Settings_Service Localization Service instance
*/
public static function get_settings_service() {
return self::$settings_service;
}

/**
* Returns the WC_Payments_Action_Scheduler_Service
*
Expand Down Expand Up @@ -1420,22 +1438,6 @@ public static function get_session_service() {
return self::$session_service;
}

/**
* Returns gateway context variables needed for multi-currency support.
*
* @return array
*/
public static function get_context_for_multi_currency() {
// While multi-currency is being decoupled from WooPayments into a separate module, it is still rendered within the plugin.
// We don't want to reference WCPAY constants from within the module, therefore, we need a few variables from the gateway,
// as reflected in the array below.
return [
'plugin_version' => WCPAY_VERSION_NUMBER,
'plugin_file_path' => WCPAY_PLUGIN_FILE,
'is_dev_mode' => self::mode()->is_dev(),
];
}

/**
* Registers the payment method with the blocks registry.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function WC_Payments_Multi_Currency() { // phpcs:ignore WordPress.NamingConventi

if ( is_null( $instance ) ) {
$instance = new WCPay\MultiCurrency\MultiCurrency(
WC_Payments::get_context_for_multi_currency(),
WC_Payments::get_settings_service(),
WC_Payments::get_payments_api_client(),
WC_Payments::get_account_service(),
WC_Payments::get_localization_service(),
Expand Down
18 changes: 14 additions & 4 deletions multi-currency/src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Automattic\WooCommerce\Utilities\OrderUtil;
use WC_Order;
use WC_Order_Refund;
use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -41,13 +42,22 @@ class Analytics {
*/
private $multi_currency;

/**
* Instance of MultiCurrencySettingsInterface.
*
* @var MultiCurrencySettingsInterface $settings_service
*/
private $settings_service;

/**
* Constructor
*
* @param MultiCurrency $multi_currency Instance of MultiCurrency.
* @param MultiCurrency $multi_currency Instance of MultiCurrency.
* @param MultiCurrencySettingsInterface $settings_service Instance of MultiCurrencySettingsInterface.
*/
public function __construct( MultiCurrency $multi_currency ) {
$this->multi_currency = $multi_currency;
public function __construct( MultiCurrency $multi_currency, MultiCurrencySettingsInterface $settings_service ) {
$this->multi_currency = $multi_currency;
$this->settings_service = $settings_service;
$this->init();
}

Expand All @@ -62,7 +72,7 @@ public function init() {
$this->register_customer_currencies();
}

if ( $this->multi_currency->gateway_context['is_dev_mode'] ) {
if ( $this->settings_service->is_dev_mode() ) {
add_filter( 'woocommerce_analytics_report_should_use_cache', [ $this, 'disable_report_caching' ] );
}

Expand Down
34 changes: 34 additions & 0 deletions multi-currency/src/Interfaces/MultiCurrencySettingsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Interface MultiCurrencySettingsInterface
*
* @package WooCommerce\Payments\MultiCurrency\Interfaces
*/

namespace WCPay\MultiCurrency\Interfaces;

defined( 'ABSPATH' ) || exit;

interface MultiCurrencySettingsInterface {

/**
* Checks if dev mode is enabled.
*
* @return bool
*/
public function is_dev_mode(): bool;

/**
* Gets the plugin file path.
*
* @return string
*/
public function get_plugin_file_path(): string;

/**
* Gets the plugin version.
*
* @return string
*/
public function get_plugin_version(): string;
}
32 changes: 17 additions & 15 deletions multi-currency/src/MultiCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use WCPay\MultiCurrency\Interfaces\MultiCurrencyApiClientInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyCacheInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;
use WCPay\MultiCurrency\Logger;
use WCPay\MultiCurrency\Notes\NoteMultiCurrencyAvailable;
use WCPay\MultiCurrency\Utils;
Expand Down Expand Up @@ -127,6 +128,13 @@ class MultiCurrency {
*/
protected $enabled_currencies;

/**
* Instance of MultiCurrencySettingsInterface.
*
* @var MultiCurrencySettingsInterface
*/
private $settings_service;

/**
* Client for making requests to the API
*
Expand Down Expand Up @@ -169,25 +177,19 @@ class MultiCurrency {
*/
protected $simulation_params = [];

/**
* Gateway context.
*
* @var array
*/
public $gateway_context;

/**
* Class constructor.
*
* @param array $gateway_context Gateway context.
* @param MultiCurrencySettingsInterface $settings_service Settings service.
* @param MultiCurrencyApiClientInterface $payments_api_client Payments API client.
* @param MultiCurrencyAccountInterface $payments_account Payments Account instance.
* @param MultiCurrencyLocalizationInterface $localization_service Localization Service instance.
* @param MultiCurrencyCacheInterface $cache Cache instance.
* @param Utils|null $utils Optional Utils instance.
*/
public function __construct( array $gateway_context, MultiCurrencyApiClientInterface $payments_api_client, MultiCurrencyAccountInterface $payments_account, MultiCurrencyLocalizationInterface $localization_service, MultiCurrencyCacheInterface $cache, Utils $utils = null ) {
$this->gateway_context = $gateway_context;
public function __construct( MultiCurrencySettingsInterface $settings_service, MultiCurrencyApiClientInterface $payments_api_client, MultiCurrencyAccountInterface $payments_account, MultiCurrencyLocalizationInterface $localization_service, MultiCurrencyCacheInterface $cache, Utils $utils = null ) {
$this->settings_service = $settings_service;
$this->payments_api_client = $payments_api_client;
$this->payments_account = $payments_account;
$this->localization_service = $localization_service;
Expand Down Expand Up @@ -287,7 +289,7 @@ public function init() {

$admin_notices = new AdminNotices();
$user_settings = new UserSettings( $this );
new Analytics( $this );
new Analytics( $this, $this->settings_service );

$this->frontend_prices = new FrontendPrices( $this, $this->compatibility );
$this->frontend_currencies = new FrontendCurrencies( $this, $this->localization_service, $this->utils, $this->compatibility );
Expand Down Expand Up @@ -1108,8 +1110,8 @@ public function set_client_format_and_rounding_precision() {
*/
public function register_script_with_dependencies( string $handler, string $script, array $additional_dependencies = [] ) {
$script_file = $script . '.js';
$script_src_url = plugins_url( $script_file, $this->gateway_context['plugin_file_path'] );
$script_asset_path = plugin_dir_path( $this->gateway_context['plugin_file_path'] ) . $script . '.asset.php';
$script_src_url = plugins_url( $script_file, $this->settings_service->get_plugin_file_path() );
$script_asset_path = plugin_dir_path( $this->settings_service->get_plugin_file_path() ) . $script . '.asset.php';
$script_asset = file_exists( $script_asset_path ) ? require $script_asset_path : [ 'dependencies' => [] ]; // nosemgrep: audit.php.lang.security.file.inclusion-arg -- server generated path is used.
$all_dependencies = array_merge( $script_asset['dependencies'], $additional_dependencies );

Expand All @@ -1130,13 +1132,13 @@ public function register_script_with_dependencies( string $handler, string $scri
* @return string
*/
public function get_file_version( $file ) {
$plugin_path = plugin_dir_path( $this->gateway_context['plugin_file_path'] );
$plugin_path = plugin_dir_path( $this->settings_service->get_plugin_file_path() );

if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $plugin_path . $file ) ) {
return (string) filemtime( $plugin_path . trim( $file, '/' ) );
}

return $this->gateway_context['plugin_version'];
return $this->settings_service->get_plugin_version();
}

/**
Expand Down Expand Up @@ -1590,7 +1592,7 @@ private function register_admin_scripts() {

wp_register_style(
'WCPAY_MULTI_CURRENCY_SETTINGS',
plugins_url( 'dist/multi-currency.css', $this->gateway_context['plugin_file_path'] ),
plugins_url( 'dist/multi-currency.css', $this->settings_service->get_plugin_file_path() ),
[ 'wc-components', 'WCPAY_ADMIN_SETTINGS' ],
$this->get_file_version( 'dist/multi-currency.css' ),
'all'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use WCPay\MultiCurrency\Interfaces\MultiCurrencyApiClientInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyCacheInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Utils;

Expand Down Expand Up @@ -70,10 +71,10 @@ public function set_up() {
$mock_account = $this->createMock( MultiCurrencyAccountInterface::class );
$mock_localization = $this->createMock( MultiCurrencyLocalizationInterface::class );
$mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
$gateway_context = [];
$mock_settings = $this->createMock( MultiCurrencySettingsInterface::class );

$this->mock_multi_currency = $this->getMockBuilder( MultiCurrency::class )
->setConstructorArgs( [ $gateway_context, $mock_api_client, $mock_account, $mock_localization, $mock_cache ] )
->setConstructorArgs( [ $mock_settings, $mock_api_client, $mock_account, $mock_localization, $mock_cache ] )
->getMock();

$this->mock_utils = $this->createMock( Utils::class );
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/multi-currency/test-class-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use WCPay\MultiCurrency\Interfaces\MultiCurrencyApiClientInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyCacheInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyLocalizationInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;
use WCPay\MultiCurrency\MultiCurrency;

/**
Expand Down Expand Up @@ -80,12 +81,10 @@ public function set_up() {
$mock_account = $this->createMock( MultiCurrencyAccountInterface::class );
$mock_localization = $this->createMock( MultiCurrencyLocalizationInterface::class );
$mock_cache = $this->createMock( MultiCurrencyCacheInterface::class );
$gateway_context = [
'is_dev_mode' => true,
];
$mock_settings = $this->createMock( MultiCurrencySettingsInterface::class );

$this->mock_multi_currency = $this->getMockBuilder( MultiCurrency::class )
->setConstructorArgs( [ $gateway_context, $mock_api_client, $mock_account, $mock_localization, $mock_cache ] )
->setConstructorArgs( [ $mock_settings, $mock_api_client, $mock_account, $mock_localization, $mock_cache ] )
->getMock();

$this->mock_multi_currency->expects( $this->any() )
Expand All @@ -96,7 +95,7 @@ public function set_up() {
->method( 'get_available_currencies' )
->willReturn( $this->get_mock_available_currencies() );

$this->analytics = new Analytics( $this->mock_multi_currency );
$this->analytics = new Analytics( $this->mock_multi_currency, $mock_settings );

$this->mock_localization_service = $this->createMock( MultiCurrencyLocalizationInterface::class );
$this->mock_localization_service->expects( $this->any() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public function test_rest_api_ensure_should_return_store_currency_and_should_con
$_SERVER['REQUEST_URI'] = $request_uri;

$mccy = new WCPay\MultiCurrency\MultiCurrency(
WC_Payments::get_context_for_multi_currency(),
WC_Payments::get_settings_service(),
WC_Payments::get_payments_api_client(),
WC_Payments::get_account_service(),
WC_Payments::get_localization_service(),
Expand Down
15 changes: 11 additions & 4 deletions tests/unit/multi-currency/test-class-multi-currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WCPay\MultiCurrency\Interfaces\MultiCurrencyAccountInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyApiClientInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencyCacheInterface;
use WCPay\MultiCurrency\Interfaces\MultiCurrencySettingsInterface;
use WCPay\MultiCurrency\MultiCurrency;
use WCPay\MultiCurrency\Settings;
use WCPay\MultiCurrency\SettingsOnboardCta;
Expand Down Expand Up @@ -91,6 +92,13 @@ class WCPay_Multi_Currency_Tests extends WCPAY_UnitTestCase {
*/
private $mock_cache;

/**
* Mock of MultiCurrencySettingsInterface.
*
* @var MultiCurrencySettingsInterface;
*/
private $mock_settings;

/**
* Mock of Utils.
*
Expand Down Expand Up @@ -1428,11 +1436,10 @@ private function init_multi_currency( $mock_api_client = null, $wcpay_account_co

$this->mock_utils = $this->createMock( Utils::class );

$gateway_context = [
'is_dev_mode' => true,
];
$this->mock_settings = $this->createMock( MultiCurrencySettingsInterface::class );

$this->multi_currency = new MultiCurrency(
$gateway_context,
$this->mock_settings,
$mock_api_client ?? $this->mock_api_client,
$this->mock_account,
$this->localization_service,
Expand Down
Loading

0 comments on commit 6792d84

Please sign in to comment.