diff --git a/CHANGELOG.md b/CHANGELOG.md index 41c44a35..296749e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +### [v5.3.1 _(Sep 05, 2023)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.3.1) +- Fixed capabilities api calling on every pages. (PR [#398](https://github.com/omise/omise-woocommerce/pull/398)) + ### [v5.3.0 _(Aug 23, 2023)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.3.0) - Add Alipay+ on Thailand psp. (PR [#394](https://github.com/omise/omise-woocommerce/pull/394)) - Adding admin_notices action once all dependencies are loaded. (PR [#395](https://github.com/omise/omise-woocommerce/pull/395)) diff --git a/includes/class-omise-capabilities.php b/includes/class-omise-capabilities.php index 66d65fc9..c7de111e 100644 --- a/includes/class-omise-capabilities.php +++ b/includes/class-omise-capabilities.php @@ -35,6 +35,10 @@ class Omise_Capabilities { */ public static function retrieve($pKey = null, $sKey = null) { + if ( !self::shouldCallApi() ) { + return null; + } + $keys = self::getKeys($pKey, $sKey); // Do not call capabilities API if keys are not present @@ -68,6 +72,25 @@ public static function retrieve($pKey = null, $sKey = null) return self::$instance; } + /** + * @return boolean + */ + public static function shouldCallApi() { + $omiseSettingPages = [ 'omise' ]; + $currentAdminPage = isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : ''; + // If page is omise setting page from admin panel. + $isOmiseSettingPage = is_admin() && in_array( $currentAdminPage, $omiseSettingPages ); + + // If page is checkout page but not thank you page. + // By default thank you page is also part of checkout pages + // and we do not need to call capabilities on thank you page. + // If endpoint url is `order-received`, it mean thank you page. + $isPaymentPage = is_checkout() && !is_wc_endpoint_url( 'order-received' ); + + return $isPaymentPage || $isOmiseSettingPage; + } + + /** * @param string|null $pKey * @param string|null $sKey diff --git a/omise-woocommerce.php b/omise-woocommerce.php index bb0d874c..919c77b9 100644 --- a/omise-woocommerce.php +++ b/omise-woocommerce.php @@ -4,7 +4,7 @@ * Plugin Name: Opn Payments * Plugin URI: https://www.omise.co/woocommerce * Description: Opn Payments is a WordPress plugin designed specifically for WooCommerce. The plugin adds support for Opn Payments Payment Gateway's payment methods to WooCommerce. - * Version: 5.3.0 + * Version: 5.3.1 * Author: Opn Payments and contributors * Author URI: https://github.com/omise/omise-woocommerce/graphs/contributors * Text Domain: omise @@ -22,7 +22,7 @@ class Omise * * @var string */ - public $version = '5.3.0'; + public $version = '5.3.1'; /** * The Omise Instance. diff --git a/readme.txt b/readme.txt index fd631f17..625c9574 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: Opn Payments Tags: opn payments, payment, payment gateway, woocommerce plugin, omise, opn, installment, internet banking, alipay, paynow, truemoney wallet, woocommerce payment Requires at least: 4.3.1 Tested up to: 6.0.2 -Stable tag: 5.3.0 +Stable tag: 5.3.1 License: MIT License URI: https://opensource.org/licenses/MIT @@ -34,6 +34,10 @@ From there: == Changelog == += 5.3.1 = + +- Fixed capabilities api calling on every pages. (PR [#398](https://github.com/omise/omise-woocommerce/pull/398)) + = 5.3.0 = - Add Alipay+ on Thailand psp. (PR [#394](https://github.com/omise/omise-woocommerce/pull/394)) diff --git a/tests/unit/includes/backends/class-omise-backend-installment-test.php b/tests/unit/includes/backends/class-omise-backend-installment-test.php index 3809077a..977def24 100644 --- a/tests/unit/includes/backends/class-omise-backend-installment-test.php +++ b/tests/unit/includes/backends/class-omise-backend-installment-test.php @@ -247,29 +247,3 @@ public function correctly_calculating_monthly_payment_amount_as_merchant_absorbs $this->assertEquals(833.33, $result); } } - -/** - * Mock Omise_Capabilities class. - * NOTE: This might not be an ideal way to mock a class, - * feel free to enhance the test or the core code. - * - * @see includes/class-omise-capabilities - */ -class Omise_Capabilities -{ - /** - * @var self - */ - protected static $the_instance = null; - - public static function retrieve() - { - self::$the_instance = self::$the_instance ?: new self(); - return self::$the_instance; - } - - public function is_zero_interest() - { - return false; - } -} diff --git a/tests/unit/includes/class-omise-capabilities-test.php b/tests/unit/includes/class-omise-capabilities-test.php new file mode 100644 index 00000000..3c66a39a --- /dev/null +++ b/tests/unit/includes/class-omise-capabilities-test.php @@ -0,0 +1,81 @@ +omiseSettingMock = Mockery::mock('alias:Omise_Setting'); + $this->omiseCapabilitiesMock = Mockery::mock('alias:OmiseCapabilities'); + } + + /** + * close mockery after test cases are done + */ + public function tearDown(): void + { + Mockery::close(); + } + + /** + * @dataProvider retrieve_data_provider + * @runInSeparateProcess + * @covers Omise_Capabilities + */ + public function test_retrieve_should_return_value_when_it_should_call_api($isCheckout, $isThankYouPage, $isAdmin, $adminPageName, $expected) + { + // assigning to global variable, so that we can use in child functions + $GLOBALS['isCheckout'] = $isCheckout; + $GLOBALS['isThankYouPage'] = $isThankYouPage; + $GLOBALS['isAdmin'] = $isAdmin; + + // mocking page name + $_GET['page'] = $adminPageName; + + function is_checkout() { return $GLOBALS['isCheckout']; } + function is_wc_endpoint_url($page) { return $GLOBALS['isThankYouPage']; } + function is_admin() { return $GLOBALS['isAdmin']; } + + if ($expected) { + $this->omiseSettingMock->shouldReceive('instance')->andReturn($this->omiseSettingMock); + $this->omiseSettingMock->shouldReceive('public_key')->andReturn('pkey_xxx'); + $this->omiseSettingMock->shouldReceive('secret_key')->andReturn('skey_xxx'); + $this->omiseCapabilitiesMock->shouldReceive('retrieve')->once(); + $result = Omise_Capabilities::retrieve(); + $this->assertEquals('Omise_Capabilities', get_class($result)); + } else { + $result = Omise_Capabilities::retrieve(); + $this->assertEquals(null, $result); + } + } + + /** + * Data provider for toSubunitReturnCorrectFormat + */ + public function retrieve_data_provider() + { + return [ + // checkout page and not thank you page + [true, false, false, '', true], + // // checkout page and also thank you page + [true, true, false, '', false], + // // omise setting page + [true, true, true, 'omise', true], + // // other admin page + [true, true, true, 'other-page', false], + // // non checkout page and also no-admin page + [false, false, false, 'other-page', false], + // // non checkout page, non admin page + [false, false, false, '', false], + ]; + } +} diff --git a/tests/unit/includes/gateway/class-omise-offsite-test.php b/tests/unit/includes/gateway/class-omise-offsite-test.php index 6e163ed4..4712b196 100644 --- a/tests/unit/includes/gateway/class-omise-offsite-test.php +++ b/tests/unit/includes/gateway/class-omise-offsite-test.php @@ -1,7 +1,6 @@