From f3c9a7e1a0c20e825b604c7a5b8ba6cd233046b6 Mon Sep 17 00:00:00 2001 From: Nabeel Molham Date: Wed, 9 Oct 2024 11:47:20 +1100 Subject: [PATCH] Assign `supported_features.hpos` value if not defined --- tests/unit/PluginTest.php | 24 +++++++++++++++++++----- woocommerce/class-sv-wc-plugin.php | 14 +++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/tests/unit/PluginTest.php b/tests/unit/PluginTest.php index fd8c693e9..2e0a18f5a 100644 --- a/tests/unit/PluginTest.php +++ b/tests/unit/PluginTest.php @@ -104,28 +104,42 @@ public function testCanMaybeHandleBackwardsCompatibleArgs(array $inputArgs, arra public function providerCanMaybeHandleBackwardsCompatibleArgs(): Generator { yield 'no HPOS args' => [ - 'inputArgs' => [], + 'inputArgs' => [], 'outputArgs' => [], ]; yield 'old HPOS args, no support' => [ - 'inputArgs' => [ + 'inputArgs' => [ 'supports_hpos' => false, ], 'outputArgs' => [ 'supported_features' => [ - 'hpos' => false, + 'hpos' => false, ], ], ]; yield 'old HPOS args, has support' => [ - 'inputArgs' => [ + 'inputArgs' => [ 'supports_hpos' => true, ], 'outputArgs' => [ 'supported_features' => [ - 'hpos' => true, + 'hpos' => true, + ], + ], + ]; + + yield 'old HPOS args and new HPOS args' => [ + 'inputArgs' => [ + 'supports_hpos' => true, + 'supported_features' => [ + 'hpos' => false, + ], + ], + 'outputArgs' => [ + 'supported_features' => [ + 'hpos' => false, ], ], ]; diff --git a/woocommerce/class-sv-wc-plugin.php b/woocommerce/class-sv-wc-plugin.php index 92ab71f05..a380240c6 100644 --- a/woocommerce/class-sv-wc-plugin.php +++ b/woocommerce/class-sv-wc-plugin.php @@ -192,11 +192,15 @@ public function __construct( string $id, string $version, array $args = [] ) { protected function maybeHandleBackwardsCompatibleArgs(array $args): array { // handle format change for HPOS declaration - if ( - array_key_exists('supports_hpos', $args) && - ! isset($args['supported_features']['hpos']) - ) { - $args['supported_features']['hpos'] = $args['supports_hpos']; + if (array_key_exists('supports_hpos', $args)) { + // make sure `supported_features` initialized + if (! array_key_exists('supported_features', $args)) { + $args['supported_features'] = []; + } + + // Assign `supported_features.hpos` value if not already assigned + $args['supported_features']['hpos'] = $args['supported_features']['hpos'] ?? $args['supports_hpos']; + unset($args['supports_hpos']); }