Skip to content

Commit b6fcd39

Browse files
authored
Use an autoloader for Multi Currency (Automattic#2061)
* Use an autoloader for Multi Currency * Use camel case * Add autoload regen to git hooks * Fix lint * Force git to rename notes to Notes
1 parent b1c7069 commit b6fcd39

29 files changed

+225
-236
lines changed

.husky/post-checkout

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
# using `--no-install` just in case it's the first time a person is checking out the repo and doesn't have yarnhook installed
55
npx --no-install yarnhook
6+
7+
# make sure the autoload files are regenerated
8+
composer dumpautoload

.husky/post-merge

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
# using `--no-install` just in case it's the first time a person is checking out the repo and doesn't have yarnhook installed
55
npx --no-install yarnhook
6+
7+
# make sure the autoload files are regenerated
8+
composer dumpautoload

.husky/post-rewrite

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
# using `--no-install` just in case it's the first time a person is checking out the repo and doesn't have yarnhook installed
55
npx --no-install yarnhook
6+
7+
# make sure the autoload files are regenerated
8+
composer dumpautoload

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@
4747
"check-lock-file",
4848
"check-abandoned"
4949
]
50+
},
51+
"autoload": {
52+
"psr-4": {
53+
"WCPay\\MultiCurrency\\": "includes/multi-currency"
54+
}
5055
}
5156
}

includes/multi-currency/class-compatibility.php includes/multi-currency/Compatibility.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @package WooCommerce\Payments\Compatibility
66
*/
77

8-
namespace WCPay\Multi_Currency;
8+
namespace WCPay\MultiCurrency;
99

1010
defined( 'ABSPATH' ) || exit;
1111

includes/multi-currency/class-country-flags.php includes/multi-currency/CountryFlags.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22
/**
3-
* Class Country_Flags
3+
* Class CountryFlags
44
*
55
* @package WooCommerce\Payments
66
*/
77

8-
namespace WCPay\Multi_Currency;
8+
namespace WCPay\MultiCurrency;
99

1010
defined( 'ABSPATH' ) || exit;
1111

1212
/**
1313
* Class that bring flags per country/currency.
1414
*/
15-
class Country_Flags {
15+
class CountryFlags {
1616

1717
const EMOJI_COUNTRIES_FLAGS = [
1818
'AD' => '🇦🇩',

includes/multi-currency/class-currency.php includes/multi-currency/Currency.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @package WooCommerce\Payments
66
*/
77

8-
namespace WCPay\Multi_Currency;
8+
namespace WCPay\MultiCurrency;
99

1010
use WC_Payments_Utils;
1111

@@ -113,7 +113,7 @@ public function get_charm(): float {
113113
*/
114114
public function get_flag(): string {
115115
// Maybe add param img/emoji to return which you want?
116-
return Country_Flags::get_by_currency( $this->code );
116+
return CountryFlags::get_by_currency( $this->code );
117117
}
118118

119119
/**

includes/multi-currency/class-currency-switcher-widget.php includes/multi-currency/CurrencySwitcherWidget.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @package WooCommerce\Payments
66
*/
77

8-
namespace WCPay\Multi_Currency;
8+
namespace WCPay\MultiCurrency;
99

1010
use WP_Widget;
1111

@@ -14,7 +14,7 @@
1414
/**
1515
* Currency Switcher Widget Class
1616
*/
17-
class Currency_Switcher_Widget extends WP_Widget {
17+
class CurrencySwitcherWidget extends WP_Widget {
1818

1919
const DEFAULT_SETTINGS = [
2020
'title' => '',
@@ -32,17 +32,17 @@ class Currency_Switcher_Widget extends WP_Widget {
3232
/**
3333
* Multi-Currency instance.
3434
*
35-
* @var Multi_Currency
35+
* @var MultiCurrency
3636
*/
3737
protected $multi_currency;
3838

3939
/**
4040
* Register widget with WordPress.
4141
*
42-
* @param Multi_Currency $multi_currency The Multi_Currency instance.
43-
* @param Compatibility $compatibility The Compatibility instance.
42+
* @param MultiCurrency $multi_currency The MultiCurrency instance.
43+
* @param Compatibility $compatibility The Compatibility instance.
4444
*/
45-
public function __construct( Multi_Currency $multi_currency, Compatibility $compatibility ) {
45+
public function __construct( MultiCurrency $multi_currency, Compatibility $compatibility ) {
4646
$this->multi_currency = $multi_currency;
4747
$this->compatibility = $compatibility;
4848

includes/multi-currency/class-frontend-currencies.php includes/multi-currency/FrontendCurrencies.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
* @package WooCommerce\Payments
66
*/
77

8-
namespace WCPay\Multi_Currency;
8+
namespace WCPay\MultiCurrency;
99

1010
defined( 'ABSPATH' ) || exit;
1111

1212
/**
1313
* Class that formats Multi Currency currencies on the frontend.
1414
*/
15-
class Frontend_Currencies {
15+
class FrontendCurrencies {
1616
/**
1717
* Multi-Currency instance.
1818
*
19-
* @var Multi_Currency
19+
* @var MultiCurrency
2020
*/
2121
protected $multi_currency;
2222

@@ -37,10 +37,10 @@ class Frontend_Currencies {
3737
/**
3838
* Constructor.
3939
*
40-
* @param Multi_Currency $multi_currency The Multi_Currency instance.
41-
* @param Utils $utils The Utils instance.
40+
* @param MultiCurrency $multi_currency The MultiCurrency instance.
41+
* @param Utils $utils The Utils instance.
4242
*/
43-
public function __construct( Multi_Currency $multi_currency, Utils $utils ) {
43+
public function __construct( MultiCurrency $multi_currency, Utils $utils ) {
4444
$this->multi_currency = $multi_currency;
4545
$this->utils = $utils;
4646

includes/multi-currency/class-frontend-prices.php includes/multi-currency/FrontendPrices.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
* @package WooCommerce\Payments
66
*/
77

8-
namespace WCPay\Multi_Currency;
8+
namespace WCPay\MultiCurrency;
99

1010
defined( 'ABSPATH' ) || exit;
1111

1212
/**
1313
* Class that applies Multi Currency prices on the frontend.
1414
*/
15-
class Frontend_Prices {
15+
class FrontendPrices {
1616
/**
1717
* Compatibility instance.
1818
*
@@ -23,17 +23,17 @@ class Frontend_Prices {
2323
/**
2424
* Multi-Currency instance.
2525
*
26-
* @var Multi_Currency
26+
* @var MultiCurrency
2727
*/
2828
protected $multi_currency;
2929

3030
/**
3131
* Constructor.
3232
*
33-
* @param Multi_Currency $multi_currency The Multi_Currency instance.
34-
* @param Compatibility $compatibility The Compatibility instance.
33+
* @param MultiCurrency $multi_currency The MultiCurrency instance.
34+
* @param Compatibility $compatibility The Compatibility instance.
3535
*/
36-
public function __construct( Multi_Currency $multi_currency, Compatibility $compatibility ) {
36+
public function __construct( MultiCurrency $multi_currency, Compatibility $compatibility ) {
3737
$this->multi_currency = $multi_currency;
3838
$this->compatibility = $compatibility;
3939

includes/multi-currency/class-multi-currency.php includes/multi-currency/MultiCurrency.php

+26-48
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<?php
22
/**
3-
* Class Multi_Currency
3+
* Class MultiCurrency
44
*
5-
* @package WooCommerce\Payments\Multi_Currency
5+
* @package WooCommerce\Payments\MultiCurrency
66
*/
77

8-
namespace WCPay\Multi_Currency;
8+
namespace WCPay\MultiCurrency;
99

1010
use WC_Payments;
1111
use WC_Payments_API_Client;
1212
use WCPay\Exceptions\API_Exception;
13+
use WCPay\MultiCurrency\Notes\NoteMultiCurrencyAvailable;
1314

1415
defined( 'ABSPATH' ) || exit;
1516

1617
/**
1718
* Class that controls Multi Currency functionality.
1819
*/
19-
class Multi_Currency {
20+
class MultiCurrency {
2021

2122
const CURRENCY_SESSION_KEY = 'wcpay_currency';
2223
const CURRENCY_META_KEY = 'wcpay_currency';
@@ -33,7 +34,7 @@ class Multi_Currency {
3334
/**
3435
* The single instance of the class.
3536
*
36-
* @var Multi_Currency
37+
* @var MultiCurrency
3738
*/
3839
protected static $instance = null;
3940

@@ -45,16 +46,16 @@ class Multi_Currency {
4546
protected $compatibility;
4647

4748
/**
48-
* Frontend_Prices instance.
49+
* FrontendPrices instance.
4950
*
50-
* @var Frontend_Prices
51+
* @var FrontendPrices
5152
*/
5253
protected $frontend_prices;
5354

5455
/**
55-
* Frontend_Currencies instance.
56+
* FrontendCurrencies instance.
5657
*
57-
* @var Frontend_Currencies
58+
* @var FrontendCurrencies
5859
*/
5960
protected $frontend_currencies;
6061

@@ -87,12 +88,12 @@ class Multi_Currency {
8788
private $payments_api_client;
8889

8990
/**
90-
* Main Multi_Currency Instance.
91+
* Main MultiCurrency Instance.
9192
*
92-
* Ensures only one instance of Multi_Currency is loaded or can be loaded.
93+
* Ensures only one instance of MultiCurrency is loaded or can be loaded.
9394
*
9495
* @static
95-
* @return Multi_Currency - Main instance.
96+
* @return MultiCurrency - Main instance.
9697
*/
9798
public static function instance() {
9899
if ( is_null( self::$instance ) ) {
@@ -107,9 +108,6 @@ public static function instance() {
107108
* @param WC_Payments_API_Client $payments_api_client Payments API client.
108109
*/
109110
public function __construct( WC_Payments_API_Client $payments_api_client ) {
110-
// Load the include files.
111-
$this->includes();
112-
113111
$this->payments_api_client = $payments_api_client;
114112
$this->utils = new Utils();
115113
$this->compatibility = new Compatibility( $this->utils );
@@ -135,10 +133,10 @@ public function init() {
135133
$this->set_default_currency();
136134
$this->initialize_enabled_currencies();
137135

138-
new User_Settings( $this );
136+
new UserSettings( $this );
139137

140-
$this->frontend_prices = new Frontend_Prices( $this, $this->compatibility );
141-
$this->frontend_currencies = new Frontend_Currencies( $this, $this->utils );
138+
$this->frontend_prices = new FrontendPrices( $this, $this->compatibility );
139+
$this->frontend_currencies = new FrontendCurrencies( $this, $this->utils );
142140

143141
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
144142

@@ -152,17 +150,15 @@ public function init() {
152150
* Initialize the REST API controller.
153151
*/
154152
public function init_rest_api() {
155-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-wc-rest-controller.php';
156-
157-
$api_controller = new WC_REST_Controller( \WC_Payments::create_api_client() );
153+
$api_controller = new RestController( \WC_Payments::create_api_client() );
158154
$api_controller->register_routes();
159155
}
160156

161157
/**
162158
* Initialize the Widgets.
163159
*/
164160
public function init_widgets() {
165-
register_widget( new Currency_Switcher_Widget( $this, $this->compatibility ) );
161+
register_widget( new CurrencySwitcherWidget( $this, $this->compatibility ) );
166162
}
167163

168164
/**
@@ -173,8 +169,6 @@ public function init_widgets() {
173169
* @return array The new settings pages.
174170
*/
175171
public function init_settings_pages( $settings_pages ): array {
176-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-settings.php';
177-
178172
$settings_pages[] = new Settings( $this );
179173
return $settings_pages;
180174
}
@@ -283,20 +277,20 @@ public function get_compatibility() {
283277
}
284278

285279
/**
286-
* Returns the Frontend_Prices instance.
280+
* Returns the FrontendPrices instance.
287281
*
288-
* @return Frontend_Prices
282+
* @return FrontendPrices
289283
*/
290-
public function get_frontend_prices(): Frontend_Prices {
284+
public function get_frontend_prices(): FrontendPrices {
291285
return $this->frontend_prices;
292286
}
293287

294288
/**
295-
* Returns the Frontend_Currencies instance.
289+
* Returns the FrontendCurrencies instance.
296290
*
297-
* @return Frontend_Currencies
291+
* @return FrontendCurrencies
298292
*/
299-
public function get_frontend_currencies(): Frontend_Currencies {
293+
public function get_frontend_currencies(): FrontendCurrencies {
300294
return $this->frontend_currencies;
301295
}
302296

@@ -535,8 +529,7 @@ public function recalculate_cart() {
535529
*/
536530
public static function add_woo_admin_notes() {
537531
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '4.4.0', '>=' ) ) {
538-
require_once WCPAY_ABSPATH . 'includes/multi-currency/notes/class-note-multi-currency-available.php';
539-
Note_Multi_Currency_Available::possibly_add_note();
532+
NoteMultiCurrencyAvailable::possibly_add_note();
540533
}
541534
}
542535

@@ -545,8 +538,7 @@ public static function add_woo_admin_notes() {
545538
*/
546539
public static function remove_woo_admin_notes() {
547540
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '4.4.0', '>=' ) ) {
548-
require_once WCPAY_ABSPATH . 'includes/multi-currency/notes/class-note-multi-currency-available.php';
549-
Note_Multi_Currency_Available::possibly_delete_note();
541+
NoteMultiCurrencyAvailable::possibly_delete_note();
550542
}
551543
}
552544

@@ -587,20 +579,6 @@ protected function ceil_price( float $price, float $rounding ): float {
587579
return ceil( $price / $rounding ) * $rounding;
588580
}
589581

590-
/**
591-
* Include required core files used in admin and on the frontend.
592-
*/
593-
protected function includes() {
594-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-compatibility.php';
595-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-currency.php';
596-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-currency-switcher-widget.php';
597-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-country-flags.php';
598-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-frontend-prices.php';
599-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-frontend-currencies.php';
600-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-user-settings.php';
601-
include_once WCPAY_ABSPATH . 'includes/multi-currency/class-utils.php';
602-
}
603-
604582
/**
605583
* Caches currency data for a period of time.
606584
*

0 commit comments

Comments
 (0)