Commit dcda7d8f54d for woocommerce
commit dcda7d8f54d5e7f4bd7f80765d9ef5f6485385c2
Author: Tom Cafferkey <tjcafferkey@gmail.com>
Date: Mon Nov 30 14:32:44 2026 +0000
Remove deprecated feature definition registration hook (#68926)
* Remove deprecated feature definition hook
* Add changelog entry for removed feature definition hook
* Bring back register_additional_features as a no-op
* Remove get_mcp_integration_description|
* use wc_deprecated_function in register_additional_features
* Add tearDown
* Fix lazy initialization of built-in feature definitions
* join lines
* Fix built-in initialization overwriting registered features
* Restore deterministic feature definition initialization
diff --git a/plugins/woocommerce/changelog/remove-deprecated-feature-definition-hook b/plugins/woocommerce/changelog/remove-deprecated-feature-definition-hook
new file mode 100644
index 00000000000..cf03ba7ca4a
--- /dev/null
+++ b/plugins/woocommerce/changelog/remove-deprecated-feature-definition-hook
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Remove the deprecated woocommerce_register_feature_definitions hook.
diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php
index cc3d96a4186..ddf5612ce8b 100644
--- a/plugins/woocommerce/includes/class-woocommerce.php
+++ b/plugins/woocommerce/includes/class-woocommerce.php
@@ -1051,9 +1051,6 @@ final class WooCommerce {
* @return void
*/
public function init() {
- // See the comment inside FeaturesController::__construct.
- wc_get_container()->get( FeaturesController::class )->register_additional_features();
-
/**
* Action triggered before WooCommerce initialization begins.
*/
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index b1cdc2c92b7..c63e40f5008 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -58858,12 +58858,6 @@ parameters:
count: 1
path: src/Internal/Features/FeaturesController.php
- -
- message: '#^Method Automattic\\WooCommerce\\Internal\\Features\\FeaturesController\:\:register_additional_features\(\) has no return type specified\.$#'
- identifier: missingType.return
- count: 1
- path: src/Internal/Features/FeaturesController.php
-
-
message: '#^Parameter \#3 \$autoload of function update_option expects bool\|null, string given\.$#'
identifier: argument.type
diff --git a/plugins/woocommerce/src/Internal/Features/FeaturesController.php b/plugins/woocommerce/src/Internal/Features/FeaturesController.php
index c80529bbb65..7f8bd93b04c 100644
--- a/plugins/woocommerce/src/Internal/Features/FeaturesController.php
+++ b/plugins/woocommerce/src/Internal/Features/FeaturesController.php
@@ -31,12 +31,6 @@ defined( 'ABSPATH' ) || exit;
* Class to define the WooCommerce features that can be enabled and disabled by admin users,
* provides also a mechanism for WooCommerce plugins to declare that they are compatible
* (or incompatible) with a given feature.
- *
- * Note: the 'woocommerce_register_feature_definitions' hook allows registering new features
- * externally. This hook is deprecated, features should be registered from within get_feature_definitions.
- * However, in case you use it for testing purposes, keep in mind that the hook is fired from inside 'init';
- * therefore, features that need to be queried, enabled, or disabled before 'init' (e.g. during WP CLI initialization)
- * can't be registered using the hook.
*/
class FeaturesController {
@@ -111,19 +105,18 @@ class FeaturesController {
/**
* Flag indicating if additional features have been registered already
- * via woocommerce_register_feature_definitions action.
+ * via calls to other classes.
*
* @var bool
*/
- private bool $registered_additional_features_via_action = false;
+ private bool $registered_additional_features_via_class_calls = false;
/**
- * Flag indicating if additional features have been registered already
- * via calls to other classes.
+ * Flag indicating if hardcoded feature definitions have been initialized.
*
* @var bool
*/
- private bool $registered_additional_features_via_class_calls = false;
+ private bool $initialized_feature_definitions = false;
/**
* Flag indicating if hardcoded feature definitions are currently being initialized.
@@ -150,19 +143,11 @@ class FeaturesController {
* Creates a new instance of the class.
*/
public function __construct() {
- // In principle, register_additional_features is triggered manually from within class-woocommerce
- // right before before_woocommerce_init is fired (this is needed for the features to be visible
- // to plugins executing declare_compatibility).
- // However we add additional checks/hookings here to support unit tests and possible overlooked/future
- // DI container/class instantiation nuances.
- if ( ! $this->registered_additional_features_via_action ) {
- if ( did_action( 'before_woocommerce_init' ) ) {
- // Needed for unit tests, where 'before_woocommerce_init' will have been fired already at this point.
- $this->register_additional_features();
- } else {
- // This needs to have a higher $priority than the 'before_woocommerce_init' hooked by plugins that declare compatibility.
- add_filter( 'before_woocommerce_init', array( $this, 'register_additional_features' ), -9999, 0 );
- }
+ if ( did_action( 'before_woocommerce_init' ) ) {
+ $this->maybe_init_feature_definitions();
+ } else {
+ // Initialize before plugins declare compatibility, unless an earlier query already did so.
+ add_action( 'before_woocommerce_init', array( $this, 'maybe_init_feature_definitions' ), -9999, 0 );
}
if ( did_action( 'init' ) ) {
@@ -190,9 +175,6 @@ class FeaturesController {
/**
* Register a feature.
*
- * This used to be called during the `woocommerce_register_feature_definitions` action hook,
- * now it's called directly from get_feature_definitions as needed.
- *
* @param string $slug The ID slug of the feature.
* @param string $name The name of the feature that will appear on the Features screen and elsewhere.
* @param array $args {
@@ -283,9 +265,7 @@ class FeaturesController {
// to prevent infinite loops in case one of these calls ends up calling here again.
$this->registered_additional_features_via_class_calls = true;
- // Additional feature definitions.
- // These used to be tied to the now deprecated woocommerce_register_feature_definitions action,
- // and aren't processed in init_feature_definitions to avoid circular calls in the dependency injection container.
+ // These aren't processed in init_feature_definitions to avoid circular calls in the dependency injection container.
$container = wc_get_container();
$container->get( CustomOrdersTableController::class )->add_feature_definition( $this );
$container->get( CostOfGoodsSoldController::class )->add_feature_definition( $this );
@@ -298,8 +278,11 @@ class FeaturesController {
/**
* Initialize hardcoded feature definitions if they have not been initialized yet.
+ *
+ * @internal
+ * @since 11.4.0
*/
- private function maybe_init_feature_definitions(): void {
+ public function maybe_init_feature_definitions(): void {
if ( $this->initializing_feature_definitions ) {
if ( ! $this->issued_feature_definitions_notice ) {
// Set this before issuing the notice because wc_doing_it_wrong() invokes gettext callbacks.
@@ -314,7 +297,7 @@ class FeaturesController {
return;
}
- if ( ! empty( $this->features ) ) {
+ if ( $this->initialized_feature_definitions ) {
return;
}
@@ -322,6 +305,7 @@ class FeaturesController {
try {
$this->init_feature_definitions();
+ $this->initialized_feature_definitions = true;
} finally {
$this->initializing_feature_definitions = false;
}
@@ -329,10 +313,7 @@ class FeaturesController {
/**
* Initialize the hardcoded feature definitions array.
- * This doesn't include:
- * - Features that get initialized via the (deprecated) woocommerce_register_feature_definitions.
- * - Features whose definition comes from another class. These are initialized directly in get_feature_definitions
- * to avoid circular calls in the dependency injection container.
+ * This doesn't include features whose definition comes from another class. These are initialized directly in get_feature_definitions to avoid circular calls in the dependency injection container.
*/
private function init_feature_definitions(): void {
$alpha_feature_testing_is_enabled = Constants::is_true( 'WOOCOMMERCE_ENABLE_ALPHA_FEATURE_TESTING' );
@@ -774,6 +755,10 @@ class FeaturesController {
}
foreach ( $legacy_features as $slug => $definition ) {
+ if ( isset( $this->features[ $slug ] ) ) {
+ continue;
+ }
+
$this->add_feature_definition( $slug, $definition['name'], $definition );
}
@@ -807,35 +792,14 @@ class FeaturesController {
}
/**
- * Function to trigger the (now deprecated) 'woocommerce_register_feature_definitions' hook.
- *
- * This function must execute immediately before the 'before_woocommerce_init'
- * action is fired, so that feature compatibility declarations happening
- * in that action find all the features properly declared already.
+ * Retained for callers that invoke this method directly. It no longer registers features or fires the removed hook.
*
+ * @deprecated 11.4.0
* @internal
+ * @return void
*/
public function register_additional_features() {
- if ( $this->registered_additional_features_via_action ) {
- return;
- }
-
- $this->maybe_init_feature_definitions();
-
- /**
- * The action for registering features.
- *
- * @since 8.3.0
- *
- * @param FeaturesController $features_controller The instance of FeaturesController.
- *
- * @deprecated 9.9.0 Features should be defined directly in get_feature_definitions.
- */
- do_action( 'woocommerce_register_feature_definitions', $this );
-
- $this->init_compatibility_info_by_feature();
-
- $this->registered_additional_features_via_action = true;
+ wc_deprecated_function( __METHOD__, '11.4.0' );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php
index 8e268e93f08..4d4848b4c14 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php
@@ -3,12 +3,15 @@
* FeaturesControllerTest class file.
*/
+declare( strict_types=1 );
+
namespace Automattic\WooCommerce\Tests\Internal\Features;
use Automattic\WooCommerce\Internal\CostOfGoodsSold\CostOfGoodsSoldController;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use Automattic\WooCommerce\Internal\Features\FeaturesController;
+use Automattic\WooCommerce\Enums\FeaturePluginCompatibility;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\Utilities\PluginUtil;
@@ -38,13 +41,6 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
$this->set_up_plugins();
- add_action(
- 'woocommerce_register_feature_definitions',
- array( $this, 'register_dummy_features' ),
- 11,
- 1
- );
-
// phpcs:disable Squiz.Commenting.FunctionComment.Missing
$dummy_feature_registerer = new class() {
public function add_feature_definition( $features_controller ) {
@@ -56,6 +52,7 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
$container->replace( CostOfGoodsSoldController::class, $dummy_feature_registerer );
$this->sut = new FeaturesController();
+ $this->register_dummy_features( $this->sut );
$this->sut->init( wc_get_container()->get( LegacyProxy::class ), $this->fake_plugin_util );
delete_option( 'woocommerce_feature_mature1_enabled' );
@@ -167,6 +164,10 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
$features_property->setAccessible( true );
$features_property->setValue( $sut, array() );
+ $initialized_property = $reflection_class->getProperty( 'initialized_feature_definitions' );
+ $initialized_property->setAccessible( true );
+ $initialized_property->setValue( $sut, true );
+
$compat_property = $reflection_class->getProperty( 'compatibility_info_by_feature' );
$compat_property->setAccessible( true );
$compat_property->setValue( $sut, array() );
@@ -190,6 +191,15 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
parent::tearDown();
}
+ /**
+ * The removed feature registration method emits a deprecation notice.
+ */
+ public function test_register_additional_features_is_deprecated(): void {
+ $this->setExpectedDeprecated( FeaturesController::class . '::register_additional_features' );
+
+ $this->sut->register_additional_features();
+ }
+
/**
* @testdox 'get_features' returns existing non-experimental features without enabling information if requested to do so.
*/
@@ -220,6 +230,54 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
$this->assertEquals( $expected, $actual );
}
+ /**
+ * @testdox Built-in features load on the first list query even if a custom feature was registered earlier.
+ */
+ public function test_early_feature_registration_does_not_skip_builtin_definitions(): void {
+ $controller = new FeaturesController();
+ $controller->init( wc_get_container()->get( LegacyProxy::class ), $this->fake_plugin_util );
+ $controller->add_feature_definition( 'early_feature', 'Early feature', array( 'default_plugin_compatibility' => 'compatible' ) );
+
+ $features = $controller->get_features( true );
+
+ $this->assertArrayHasKey( 'early_feature', $features, 'The early registration should remain available.' );
+ $this->assertSame( 'Early feature', $features['early_feature']['name'] );
+ $this->assertArrayHasKey( 'analytics', $features, 'Built-in features should still load on the first query.' );
+ }
+
+ /**
+ * @testdox Built-in initialization preserves an early registration with the same feature ID.
+ */
+ public function test_early_registration_of_builtin_feature_is_preserved(): void {
+ $option_key = 'extension_rate_limit_checkout_enabled';
+ delete_option( $option_key );
+
+ $controller = new FeaturesController();
+ $controller->init( wc_get_container()->get( LegacyProxy::class ), $this->fake_plugin_util );
+ $controller->add_feature_definition(
+ 'rate_limit_checkout',
+ 'Extension checkout rate limiting',
+ array(
+ 'option_key' => $option_key,
+ 'enabled_by_default' => true,
+ 'default_plugin_compatibility' => FeaturePluginCompatibility::COMPATIBLE,
+ )
+ );
+
+ $features = $controller->get_features( true, true );
+
+ $this->assertArrayHasKey( 'rate_limit_checkout', $features );
+ $this->assertSame( 'Extension checkout rate limiting', $features['rate_limit_checkout']['name'] );
+ $this->assertSame( $option_key, $features['rate_limit_checkout']['option_key'] );
+ $this->assertTrue( $features['rate_limit_checkout']['enabled_by_default'] );
+ $this->assertTrue( $features['rate_limit_checkout']['is_enabled'], 'The original default should apply when the custom option is absent.' );
+ $this->assertSame( $option_key, $controller->feature_enable_option_name( 'rate_limit_checkout' ) );
+
+ update_option( $option_key, 'no' );
+
+ $this->assertFalse( $controller->feature_is_enabled( 'rate_limit_checkout' ), 'The original option key should still control the feature after listing.' );
+ }
+
/**
* @testdox 'get_features' returns all existing features with enabling information if requested to do so.
*/
@@ -288,6 +346,10 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
$features_property->setAccessible( true );
$features_property->setValue( $this->sut, array() );
+ $initialized_property = $reflection_class->getProperty( 'initialized_feature_definitions' );
+ $initialized_property->setAccessible( true );
+ $initialized_property->setValue( $this->sut, false );
+
$compat_property = $reflection_class->getProperty( 'compatibility_info_by_feature' );
$compat_property->setAccessible( true );
$compat_property->setValue( $this->sut, array() );
@@ -377,11 +439,6 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
public function test_block_email_editor_setting_is_visible_and_persists_enablement(): void {
$feature_option_name = 'woocommerce_feature_block_email_editor_enabled';
- // setUp() registers this class's own dummy features on this hook. Detach them so the
- // real controller below sees only the built-in definitions. _restore_hooks() puts the
- // callback back after the test, the same way the rollback puts the option row back.
- remove_action( 'woocommerce_register_feature_definitions', array( $this, 'register_dummy_features' ), 11 );
-
// `change_feature_enable` reports whether `update_option` wrote anything, so it
// returns false when the option already reads `yes`. Start from no option at
// all, so the assertion below measures the transition rather than whatever an
@@ -658,54 +715,47 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
* @testdox 'get_compatible_features_for_plugin' returns proper information for a plugin that has declared compatibility with the passed feature, when only enabled features are requested.
*/
public function test_get_compatible_enabled_features_for_registered_plugin() {
- add_action(
- 'woocommerce_register_feature_definitions',
- function ( $features_controller ) {
- $features = array(
- 'mature1' => array(
- 'name' => 'Mature feature 1',
- 'description' => 'The mature feature number 1',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'mature2' => array(
- 'name' => 'Mature feature 2',
- 'description' => 'The mature feature number 2',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'mature3' => array(
- 'name' => 'Mature feature 3',
- 'description' => 'The mature feature number 3',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'experimental1' => array(
- 'name' => 'Experimental feature 1',
- 'description' => 'The experimental feature number 1',
- 'is_experimental' => true,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'experimental2' => array(
- 'name' => 'Experimental feature 2',
- 'description' => 'The experimental feature number 2',
- 'is_experimental' => true,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'experimental3' => array(
- 'name' => 'Experimental feature 3',
- 'description' => 'The experimental feature number 3',
- 'is_experimental' => true,
- 'default_plugin_compatibility' => 'compatible',
- ),
- );
-
- $this->reset_features_list( $features_controller, $features );
- },
- 20
+ $features = array(
+ 'mature1' => array(
+ 'name' => 'Mature feature 1',
+ 'description' => 'The mature feature number 1',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'mature2' => array(
+ 'name' => 'Mature feature 2',
+ 'description' => 'The mature feature number 2',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'mature3' => array(
+ 'name' => 'Mature feature 3',
+ 'description' => 'The mature feature number 3',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'experimental1' => array(
+ 'name' => 'Experimental feature 1',
+ 'description' => 'The experimental feature number 1',
+ 'is_experimental' => true,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'experimental2' => array(
+ 'name' => 'Experimental feature 2',
+ 'description' => 'The experimental feature number 2',
+ 'is_experimental' => true,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'experimental3' => array(
+ 'name' => 'Experimental feature 3',
+ 'description' => 'The experimental feature number 3',
+ 'is_experimental' => true,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
);
$this->sut = new FeaturesController();
+ $this->reset_features_list( $this->sut, $features );
$this->sut->init( wc_get_container()->get( LegacyProxy::class ), $this->fake_plugin_util );
$this->simulate_inside_before_woocommerce_init_hook();
@@ -736,32 +786,25 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
* @testdox Deprecated features are included in 'get_compatible_features_for_plugin' results.
*/
public function test_deprecated_features_included_in_get_compatible_features_for_plugin() {
- add_action(
- 'woocommerce_register_feature_definitions',
- function ( $features_controller ) {
- $features = array(
- 'active_feature' => array(
- 'name' => 'Active feature',
- 'description' => 'An active feature',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'deprecated_feature' => array(
- 'name' => 'Deprecated feature',
- 'description' => 'A deprecated feature',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- 'deprecated_since' => '10.5.0',
- 'deprecated_value' => true,
- ),
- );
-
- $this->reset_features_list( $features_controller, $features );
- },
- 20
+ $features = array(
+ 'active_feature' => array(
+ 'name' => 'Active feature',
+ 'description' => 'An active feature',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'deprecated_feature' => array(
+ 'name' => 'Deprecated feature',
+ 'description' => 'A deprecated feature',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ 'deprecated_since' => '10.5.0',
+ 'deprecated_value' => true,
+ ),
);
$this->sut = new FeaturesController();
+ $this->reset_features_list( $this->sut, $features );
$this->sut->init( wc_get_container()->get( LegacyProxy::class ), $this->fake_plugin_util );
$this->simulate_inside_before_woocommerce_init_hook();
@@ -782,40 +825,33 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
* @testdox Deprecated features with deprecated_value=true are included when filtering by enabled features.
*/
public function test_deprecated_features_with_true_value_included_when_filtering_enabled_features() {
- add_action(
- 'woocommerce_register_feature_definitions',
- function ( $features_controller ) {
- $features = array(
- 'active_feature' => array(
- 'name' => 'Active feature',
- 'description' => 'An active feature',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'deprecated_enabled_feature' => array(
- 'name' => 'Deprecated enabled feature',
- 'description' => 'A deprecated feature with true value',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- 'deprecated_since' => '10.5.0',
- 'deprecated_value' => true,
- ),
- 'deprecated_disabled_feature' => array(
- 'name' => 'Deprecated disabled feature',
- 'description' => 'A deprecated feature with false value',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'compatible',
- 'deprecated_since' => '10.5.0',
- 'deprecated_value' => false,
- ),
- );
-
- $this->reset_features_list( $features_controller, $features );
- },
- 20
+ $features = array(
+ 'active_feature' => array(
+ 'name' => 'Active feature',
+ 'description' => 'An active feature',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'deprecated_enabled_feature' => array(
+ 'name' => 'Deprecated enabled feature',
+ 'description' => 'A deprecated feature with true value',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ 'deprecated_since' => '10.5.0',
+ 'deprecated_value' => true,
+ ),
+ 'deprecated_disabled_feature' => array(
+ 'name' => 'Deprecated disabled feature',
+ 'description' => 'A deprecated feature with false value',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ 'deprecated_since' => '10.5.0',
+ 'deprecated_value' => false,
+ ),
);
$this->sut = new FeaturesController();
+ $this->reset_features_list( $this->sut, $features );
$this->sut->init( wc_get_container()->get( LegacyProxy::class ), $this->fake_plugin_util );
$this->simulate_inside_before_woocommerce_init_hook();
@@ -843,37 +879,29 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
* @testdox Deprecated features can be checked in 'get_incompatible_plugins' without triggering deprecation notices.
*/
public function test_deprecated_features_in_get_incompatible_plugins_without_notices() {
- add_action(
- 'woocommerce_register_feature_definitions',
- function ( $features_controller ) {
- $features = array(
- 'active_feature' => array(
- 'name' => 'Active feature',
- 'description' => 'An active feature',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'incompatible',
- ),
- 'deprecated_enabled_feature' => array(
- 'name' => 'Deprecated enabled feature',
- 'description' => 'A deprecated feature that is considered enabled',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'incompatible',
- 'deprecated_since' => '10.5.0',
- 'deprecated_value' => true,
- ),
- 'deprecated_disabled_feature' => array(
- 'name' => 'Deprecated disabled feature',
- 'description' => 'A deprecated feature that is considered disabled',
- 'is_experimental' => false,
- 'default_plugin_compatibility' => 'incompatible',
- 'deprecated_since' => '10.5.0',
- 'deprecated_value' => false,
- ),
- );
-
- $this->reset_features_list( $features_controller, $features );
- },
- 20
+ $features = array(
+ 'active_feature' => array(
+ 'name' => 'Active feature',
+ 'description' => 'An active feature',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'incompatible',
+ ),
+ 'deprecated_enabled_feature' => array(
+ 'name' => 'Deprecated enabled feature',
+ 'description' => 'A deprecated feature that is considered enabled',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'incompatible',
+ 'deprecated_since' => '10.5.0',
+ 'deprecated_value' => true,
+ ),
+ 'deprecated_disabled_feature' => array(
+ 'name' => 'Deprecated disabled feature',
+ 'description' => 'A deprecated feature that is considered disabled',
+ 'is_experimental' => false,
+ 'default_plugin_compatibility' => 'incompatible',
+ 'deprecated_since' => '10.5.0',
+ 'deprecated_value' => false,
+ ),
);
// phpcs:disable Squiz.Commenting
@@ -912,7 +940,9 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
$proxy_prop->setValue( $fake_plugin_util, wc_get_container()->get( LegacyProxy::class ) );
$this->sut = new FeaturesController();
+ $this->reset_features_list( $this->sut, $features );
$this->sut->init( wc_get_container()->get( LegacyProxy::class ), $fake_plugin_util );
+ wc_get_container()->replace( FeaturesController::class, $this->sut );
$this->simulate_inside_before_woocommerce_init_hook();
$fake_plugin_util->set_active_plugins( array( 'test_plugin' ) );
@@ -1224,32 +1254,26 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
);
// phpcs:enable Squiz.Commenting, Generic.CodeAnalysis.UnusedFunctionParameter.Found
- add_action(
- 'woocommerce_register_feature_definitions',
- function ( $features_controller ) {
- $features = array(
- 'custom_order_tables' => array(
- 'name' => __( 'High-Performance order storage', 'woocommerce' ),
- 'is_experimental' => true,
- 'enabled_by_default' => false,
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'cart_checkout_blocks' => array(
- 'name' => __( 'Cart & Checkout Blocks', 'woocommerce' ),
- 'description' => __( 'Optimize for faster checkout', 'woocommerce' ),
- 'is_experimental' => false,
- 'disable_ui' => true,
- 'default_plugin_compatibility' => 'compatible',
- ),
- );
-
- $this->reset_features_list( $features_controller, $features );
- },
- 20
+ $features = array(
+ 'custom_order_tables' => array(
+ 'name' => __( 'High-Performance order storage', 'woocommerce' ),
+ 'is_experimental' => true,
+ 'enabled_by_default' => false,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'cart_checkout_blocks' => array(
+ 'name' => __( 'Cart & Checkout Blocks', 'woocommerce' ),
+ 'description' => __( 'Optimize for faster checkout', 'woocommerce' ),
+ 'is_experimental' => false,
+ 'disable_ui' => true,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
);
$local_sut = new FeaturesController();
+ $this->reset_features_list( $local_sut, $features );
$local_sut->init( wc_get_container()->get( LegacyProxy::class ), $fake_plugin_util );
+ wc_get_container()->replace( FeaturesController::class, $local_sut );
$plugins = array( 'compatible_plugin1', 'compatible_plugin2' );
$fake_plugin_util->set_active_plugins( $plugins );
foreach ( $plugins as $plugin ) {
@@ -1324,33 +1348,27 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
);
// phpcs:enable Squiz.Commenting, Generic.CodeAnalysis.UnusedFunctionParameter.Found
- add_action(
- 'woocommerce_register_feature_definitions',
- function ( $features_controller ) {
- $features = array(
- 'custom_order_tables' => array(
- 'name' => __( 'High-Performance order storage', 'woocommerce' ),
- 'is_experimental' => false,
- 'enabled_by_default' => false,
- 'option_key' => CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION,
- 'default_plugin_compatibility' => 'incompatible',
- ),
- 'cart_checkout_blocks' => array(
- 'name' => __( 'Cart & Checkout Blocks', 'woocommerce' ),
- 'description' => __( 'Optimize for faster checkout', 'woocommerce' ),
- 'is_experimental' => false,
- 'disable_ui' => true,
- 'default_plugin_compatibility' => 'compatible',
- ),
- );
-
- $this->reset_features_list( $features_controller, $features );
- },
- 20
+ $features = array(
+ 'custom_order_tables' => array(
+ 'name' => __( 'High-Performance order storage', 'woocommerce' ),
+ 'is_experimental' => false,
+ 'enabled_by_default' => false,
+ 'option_key' => CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION,
+ 'default_plugin_compatibility' => 'incompatible',
+ ),
+ 'cart_checkout_blocks' => array(
+ 'name' => __( 'Cart & Checkout Blocks', 'woocommerce' ),
+ 'description' => __( 'Optimize for faster checkout', 'woocommerce' ),
+ 'is_experimental' => false,
+ 'disable_ui' => true,
+ 'default_plugin_compatibility' => 'compatible',
+ ),
);
$local_sut = new FeaturesController();
+ $this->reset_features_list( $local_sut, $features );
$local_sut->init( wc_get_container()->get( LegacyProxy::class ), $fake_plugin_util );
+ wc_get_container()->replace( FeaturesController::class, $local_sut );
$local_sut->change_feature_enable( 'custom_order_tables', $hpos_is_enabled );
$plugins = array( 'compatible_plugin', 'incompatible_plugin' );
$fake_plugin_util->set_active_plugins( $plugins );
@@ -1577,8 +1595,6 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
* @testdox The point_of_sale feature is always enabled regardless of the stored option value.
*/
public function test_point_of_sale_feature_is_always_enabled(): void {
- remove_action( 'woocommerce_register_feature_definitions', array( $this, 'register_dummy_features' ), 11 );
-
$sut = new FeaturesController();
$sut->init( wc_get_container()->get( LegacyProxy::class ), $this->fake_plugin_util );
diff --git a/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php b/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php
index 3b6ac101e30..5fcc998719b 100644
--- a/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php
+++ b/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php
@@ -33,6 +33,15 @@ class PluginUtilTests extends \WC_Unit_Test_Case {
$this->sut = $this->get_instance_of( PluginUtil::class );
}
+ /**
+ * Clears the feature definitions registered on the container's controller during tests.
+ */
+ public function tearDown(): void {
+ $this->reset_container_resolutions();
+
+ parent::tearDown();
+ }
+
/**
* @testdox `get_all_active_valid_plugins` gets the active plugins *that actually exist* and returns them
* as a list of absolute file paths.
@@ -247,30 +256,24 @@ class PluginUtilTests extends \WC_Unit_Test_Case {
public function test_get_items_considered_incompatible() {
$this->reset_container_resolutions();
- add_action(
- 'woocommerce_register_feature_definitions',
- function ( $features_controller ) {
- $features = array(
- 'test_feature_1' => array(
- 'name' => 'Test feature 1',
- 'default_plugin_compatibility' => 'incompatible',
- ),
- 'test_feature_2' => array(
- 'name' => 'Test feature 2',
- 'default_plugin_compatibility' => 'compatible',
- ),
- 'test_feature_3' => array(
- 'name' => 'Test feature 3',
- 'default_plugin_compatibility' => 'compatible',
- ),
- );
-
- foreach ( $features as $slug => $definition ) {
- $features_controller->add_feature_definition( $slug, $definition['name'], $definition );
- }
- },
- 20
+ $features = array(
+ 'test_feature_1' => array(
+ 'name' => 'Test feature 1',
+ 'default_plugin_compatibility' => 'incompatible',
+ ),
+ 'test_feature_2' => array(
+ 'name' => 'Test feature 2',
+ 'default_plugin_compatibility' => 'compatible',
+ ),
+ 'test_feature_3' => array(
+ 'name' => 'Test feature 3',
+ 'default_plugin_compatibility' => 'compatible',
+ ),
);
+ $features_controller = wc_get_container()->get( FeaturesController::class );
+ foreach ( $features as $slug => $definition ) {
+ $features_controller->add_feature_definition( $slug, $definition['name'], $definition );
+ }
$plugin_compatibility_info = array(
'compatible' => array(