Skip to content

Commit

Permalink
Merge pull request #719 from godaddy-wordpress/mwc-17548
Browse files Browse the repository at this point in the history
Handle backwards compatibility for old HPOS compatibility format
  • Loading branch information
nmolham-godaddy authored Oct 10, 2024
2 parents 97a4dee + f3c9a7e commit fd86525
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
59 changes: 59 additions & 0 deletions tests/unit/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

use Generator;
use Mockery;
use ReflectionException;
use SkyVerge\WooCommerce\PluginFramework\v5_15_0\SV_WC_Plugin;
Expand Down Expand Up @@ -85,4 +86,62 @@ public function testCanCatchFailedAssertion() : void
$this->invokeInaccessibleMethod($this->testObject, 'assert', false)
);
}

/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_0\SV_WC_Plugin::maybeHandleBackwardsCompatibleArgs()
* @dataProvider providerCanMaybeHandleBackwardsCompatibleArgs
* @throws ReflectionException
*/
public function testCanMaybeHandleBackwardsCompatibleArgs(array $inputArgs, array $outputArgs): void
{
$this->assertSame(
$outputArgs,
$this->invokeInaccessibleMethod($this->testObject, 'maybeHandleBackwardsCompatibleArgs', $inputArgs)
);
}

/** @see testCanMaybeHandleBackwardsCompatibleArgs */
public function providerCanMaybeHandleBackwardsCompatibleArgs(): Generator
{
yield 'no HPOS args' => [
'inputArgs' => [],
'outputArgs' => [],
];

yield 'old HPOS args, no support' => [
'inputArgs' => [
'supports_hpos' => false,
],
'outputArgs' => [
'supported_features' => [
'hpos' => false,
],
],
];

yield 'old HPOS args, has support' => [
'inputArgs' => [
'supports_hpos' => true,
],
'outputArgs' => [
'supported_features' => [
'hpos' => true,
],
],
];

yield 'old HPOS args and new HPOS args' => [
'inputArgs' => [
'supports_hpos' => true,
'supported_features' => [
'hpos' => false,
],
],
'outputArgs' => [
'supported_features' => [
'hpos' => false,
],
],
];
}
}
26 changes: 25 additions & 1 deletion woocommerce/class-sv-wc-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function __construct( string $id, string $version, array $args = [] ) {
$this->id = $id;
$this->version = $version;

$args = wp_parse_args( $args, [
$args = wp_parse_args( $this->maybeHandleBackwardsCompatibleArgs($args), [
'text_domain' => '',
'dependencies' => [],
'supported_features' => [
Expand Down Expand Up @@ -183,6 +183,30 @@ public function __construct( string $id, string $version, array $args = [] ) {
$this->add_hooks();
}

/**
* Provides backward compatibility for arguments, where we can. This handles any format changes in the $args array.
*
* @param array $args
* @return array
*/
protected function maybeHandleBackwardsCompatibleArgs(array $args): array
{
// handle format change for HPOS declaration
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']);
}

return $args;
}


/** Init methods **********************************************************/

Expand Down

0 comments on commit fd86525

Please sign in to comment.