Skip to content

Commit fd86525

Browse files
Merge pull request #719 from godaddy-wordpress/mwc-17548
Handle backwards compatibility for old HPOS compatibility format
2 parents 97a4dee + f3c9a7e commit fd86525

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

tests/unit/PluginTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_0\Tests\Unit;
44

5+
use Generator;
56
use Mockery;
67
use ReflectionException;
78
use SkyVerge\WooCommerce\PluginFramework\v5_15_0\SV_WC_Plugin;
@@ -85,4 +86,62 @@ public function testCanCatchFailedAssertion() : void
8586
$this->invokeInaccessibleMethod($this->testObject, 'assert', false)
8687
);
8788
}
89+
90+
/**
91+
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_0\SV_WC_Plugin::maybeHandleBackwardsCompatibleArgs()
92+
* @dataProvider providerCanMaybeHandleBackwardsCompatibleArgs
93+
* @throws ReflectionException
94+
*/
95+
public function testCanMaybeHandleBackwardsCompatibleArgs(array $inputArgs, array $outputArgs): void
96+
{
97+
$this->assertSame(
98+
$outputArgs,
99+
$this->invokeInaccessibleMethod($this->testObject, 'maybeHandleBackwardsCompatibleArgs', $inputArgs)
100+
);
101+
}
102+
103+
/** @see testCanMaybeHandleBackwardsCompatibleArgs */
104+
public function providerCanMaybeHandleBackwardsCompatibleArgs(): Generator
105+
{
106+
yield 'no HPOS args' => [
107+
'inputArgs' => [],
108+
'outputArgs' => [],
109+
];
110+
111+
yield 'old HPOS args, no support' => [
112+
'inputArgs' => [
113+
'supports_hpos' => false,
114+
],
115+
'outputArgs' => [
116+
'supported_features' => [
117+
'hpos' => false,
118+
],
119+
],
120+
];
121+
122+
yield 'old HPOS args, has support' => [
123+
'inputArgs' => [
124+
'supports_hpos' => true,
125+
],
126+
'outputArgs' => [
127+
'supported_features' => [
128+
'hpos' => true,
129+
],
130+
],
131+
];
132+
133+
yield 'old HPOS args and new HPOS args' => [
134+
'inputArgs' => [
135+
'supports_hpos' => true,
136+
'supported_features' => [
137+
'hpos' => false,
138+
],
139+
],
140+
'outputArgs' => [
141+
'supported_features' => [
142+
'hpos' => false,
143+
],
144+
],
145+
];
146+
}
88147
}

woocommerce/class-sv-wc-plugin.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function __construct( string $id, string $version, array $args = [] ) {
140140
$this->id = $id;
141141
$this->version = $version;
142142

143-
$args = wp_parse_args( $args, [
143+
$args = wp_parse_args( $this->maybeHandleBackwardsCompatibleArgs($args), [
144144
'text_domain' => '',
145145
'dependencies' => [],
146146
'supported_features' => [
@@ -183,6 +183,30 @@ public function __construct( string $id, string $version, array $args = [] ) {
183183
$this->add_hooks();
184184
}
185185

186+
/**
187+
* Provides backward compatibility for arguments, where we can. This handles any format changes in the $args array.
188+
*
189+
* @param array $args
190+
* @return array
191+
*/
192+
protected function maybeHandleBackwardsCompatibleArgs(array $args): array
193+
{
194+
// handle format change for HPOS declaration
195+
if (array_key_exists('supports_hpos', $args)) {
196+
// make sure `supported_features` initialized
197+
if (! array_key_exists('supported_features', $args)) {
198+
$args['supported_features'] = [];
199+
}
200+
201+
// Assign `supported_features.hpos` value if not already assigned
202+
$args['supported_features']['hpos'] = $args['supported_features']['hpos'] ?? $args['supports_hpos'];
203+
204+
unset($args['supports_hpos']);
205+
}
206+
207+
return $args;
208+
}
209+
186210

187211
/** Init methods **********************************************************/
188212

0 commit comments

Comments
 (0)