From 0817dd2b00c7acfbee241574313a3a3fa239f5c7 Mon Sep 17 00:00:00 2001 From: Alda Vigdis Skarphedinsdottir Date: Sat, 23 Mar 2024 18:38:10 +0100 Subject: [PATCH] Adding a Config class (#52) * Adding a new Config class * Adding a default DK API key to the test environment --- phpcs.xml | 16 +++--- phpunit.xml | 1 + src/Config.php | 103 +++++++++++++++++++++++++++++++++++++ src/Rest/Settings.php | 16 ++---- tests/ConfigTest.php | 33 ++++++++++++ tests/RestSettingsTest.php | 2 +- views/admin.php | 99 ++++++++++++++++++----------------- 7 files changed, 202 insertions(+), 68 deletions(-) create mode 100644 src/Config.php create mode 100644 tests/ConfigTest.php diff --git a/phpcs.xml b/phpcs.xml index c2c6f51..7f6cfff 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -39,6 +39,14 @@ and heresy. --> + + + + + ./tests/** - - ./tests/** - - @@ -82,6 +83,7 @@ + diff --git a/phpunit.xml b/phpunit.xml index 49d0a23..9963412 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,6 +7,7 @@ > + diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..66ca818 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,103 @@ + $woo_id, + 'dk_id' => $dk_id, + 'dk_name' => $dk_name, + ) + ); + } + + /** + * Get a payment mapping from a WooCommerce payment gateway ID + * + * @param string $woo_id The WooCommerce payment gateway ID. + * @param bool $empty_object Populates a default value as an object with + * empty properties. If false, it will return + * false if no mapping is found. + * + * @return stdClass An object containing woo_id, dk_id and dk_name properties. + */ + public static function get_payment_mapping( string $woo_id, bool $empty_object = true ): stdClass { + if ( true === $empty_object ) { + $default = (object) array( + 'woo_id' => '', + 'dk_id' => '', + 'dk_name' => '', + ); + } else { + $default = false; + } + + return get_option( + '1984_woo_dk_payment_method_' . $woo_id, + $default + ); + } +} diff --git a/src/Rest/Settings.php b/src/Rest/Settings.php index facbf99..1388e6b 100644 --- a/src/Rest/Settings.php +++ b/src/Rest/Settings.php @@ -4,6 +4,8 @@ namespace NineteenEightyFour\NineteenEightyWoo\Rest; +use NineteenEightyFour\NineteenEightyWoo\Config; + use WP_Error; use WP_REST_Request; use WP_REST_Response; @@ -91,20 +93,10 @@ public static function rest_api_callback( ); } - update_option( '1984_woo_dk_api_key', $rest_json->api_key ); - - foreach ( $rest_json->payment_methods as $p ) { - update_option( - '1984_woo_dk_payment_method_' . $p->woo_id, - $p - ); - } + Config::set_dk_api_key( $rest_json->api_key ); foreach ( $rest_json->payment_methods as $p ) { - update_option( - '1984_woo_dk_payment_method_' . $p->woo_id, - $p - ); + Config::set_payment_mapping( $p->woo_id, $p->dk_id, $p->dk_name ); } return new WP_REST_Response( array( 'status' => 200 ) ); diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100644 index 0000000..37c6336 --- /dev/null +++ b/tests/ConfigTest.php @@ -0,0 +1,33 @@ + 'MwmTtCTfhVQvlEOKCqFxfRAuPYHgy17E', + 'api_key' => '3541031f-baf2-4737-a7e8-c66396e5a5e3', 'payment_methods' => [ [ 'woo_id' => 'bacs', diff --git a/views/admin.php b/views/admin.php index 3175628..a03545d 100644 --- a/views/admin.php +++ b/views/admin.php @@ -2,6 +2,8 @@ declare(strict_types = 1); +use NineteenEightyFour\NineteenEightyWoo\Config; + $wc_payment_gateways = new WC_Payment_Gateways(); ?> @@ -29,8 +31,8 @@ class="wrap nineteen-eighty-woo-wrap" class="regular-text api-key-input" name="api_key" type="text" - value="" - pattern="(a|b|c|d|e|f|[0-9]|-)+" + value="" + pattern="" required /> @@ -57,52 +59,53 @@ class="regular-text api-key-input" payment_gateways as $p ) : ?> - - - - - + id ); ?> + + + + +
- title ); ?> - enabled ) : ?> - - - - - - - - - - - -

-

-
- - -

-

-
+ title ); ?> + enabled ) : ?> + + + + + + + + + + + +

+

+
+ + +

+

+