Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle backwards compatibility for old HPOS compatibility format #719

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 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,48 @@ 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,
],
],
];
}
}
22 changes: 21 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,26 @@ 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) &&
! isset($args['supported_features']['hpos'])
) {
$args['supported_features']['hpos'] = $args['supports_hpos'];
unset($args['supports_hpos']);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we unset the supports_hpos anyways without checking if supported_features.hpos is set? As well as making sure supported_features key is initialized

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aka f3c9a7e

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @nmolham-godaddy ! Thank you!


return $args;
}


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

Expand Down
Loading