Commit e81cd617731 for woocommerce

commit e81cd6177313ab805d4aebc61d28b8963154f68a
Author: Jorge A. Torres <jorge.torres@automattic.com>
Date:   Tue Dec 1 20:23:09 2026 +0000

    Follow-up fixes to feature definition initialization in `FeaturesController` (#71641)

diff --git a/plugins/woocommerce/changelog/fix-features-controller-misc b/plugins/woocommerce/changelog/fix-features-controller-misc
new file mode 100644
index 00000000000..e9b6e9a077e
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-features-controller-misc
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Tidy feature definition initialization in FeaturesController and its tests. Nothing merchant-facing.
diff --git a/plugins/woocommerce/includes/class-woocommerce.php b/plugins/woocommerce/includes/class-woocommerce.php
index ddf5612ce8b..c732d0c17ea 100644
--- a/plugins/woocommerce/includes/class-woocommerce.php
+++ b/plugins/woocommerce/includes/class-woocommerce.php
@@ -1051,6 +1051,9 @@ final class WooCommerce {
 	 * @return void
 	 */
 	public function init() {
+		// Features must be defined before compatibility declarations happen.
+		wc_get_container()->get( FeaturesController::class )->maybe_init_feature_definitions();
+
 		/**
 		 * Action triggered before WooCommerce initialization begins.
 		 */
diff --git a/plugins/woocommerce/src/Internal/Features/FeaturesController.php b/plugins/woocommerce/src/Internal/Features/FeaturesController.php
index 7f8bd93b04c..7112164792f 100644
--- a/plugins/woocommerce/src/Internal/Features/FeaturesController.php
+++ b/plugins/woocommerce/src/Internal/Features/FeaturesController.php
@@ -143,13 +143,6 @@ class FeaturesController {
 	 * Creates a new instance of the class.
 	 */
 	public function __construct() {
-		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' ) ) {
 			// Needed for unit tests, where 'init' will have been fired already at this point.
 			$this->start_listening_for_option_changes();
@@ -280,7 +273,8 @@ class FeaturesController {
 	 * Initialize hardcoded feature definitions if they have not been initialized yet.
 	 *
 	 * @internal
-	 * @since 11.4.0
+	 * @since 11.2.0
+	 * @since 11.5.0 Made public.
 	 */
 	public function maybe_init_feature_definitions(): void {
 		if ( $this->initializing_feature_definitions ) {
@@ -755,10 +749,6 @@ class FeaturesController {
 		}

 		foreach ( $legacy_features as $slug => $definition ) {
-			if ( isset( $this->features[ $slug ] ) ) {
-				continue;
-			}
-
 			$this->add_feature_definition( $slug, $definition['name'], $definition );
 		}

@@ -794,12 +784,12 @@ class FeaturesController {
 	/**
 	 * Retained for callers that invoke this method directly. It no longer registers features or fires the removed hook.
 	 *
-	 * @deprecated 11.4.0
+	 * @deprecated 11.5.0
 	 * @internal
 	 * @return void
 	 */
 	public function register_additional_features() {
-		wc_deprecated_function( __METHOD__, '11.4.0' );
+		wc_deprecated_function( __METHOD__, '11.5.0' );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php
index 4d4848b4c14..2543ab9f90c 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Features/FeaturesControllerTest.php
@@ -7,11 +7,11 @@ declare( strict_types=1 );

 namespace Automattic\WooCommerce\Tests\Internal\Features;

+use Automattic\WooCommerce\Internal\Admin\Analytics;
 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;

@@ -154,10 +154,11 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
 	 *
 	 * @param FeaturesController $sut The instance of the FeaturesController class.
 	 * @param array              $features The list of features to repopulate the controller with.
+	 * @param bool               $mark_initialized Whether to mark the built-in definitions as already initialized, so they aren't loaded on the next query.
 	 *
 	 * @return void
 	 */
-	private function reset_features_list( $sut, $features ) {
+	private function reset_features_list( $sut, $features, bool $mark_initialized = true ) {
 		$reflection_class = new \ReflectionClass( $sut );

 		$features_property = $reflection_class->getProperty( 'features' );
@@ -166,7 +167,7 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {

 		$initialized_property = $reflection_class->getProperty( 'initialized_feature_definitions' );
 		$initialized_property->setAccessible( true );
-		$initialized_property->setValue( $sut, true );
+		$initialized_property->setValue( $sut, $mark_initialized );

 		$compat_property = $reflection_class->getProperty( 'compatibility_info_by_feature' );
 		$compat_property->setAccessible( true );
@@ -192,7 +193,7 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * The removed feature registration method emits a deprecation notice.
+	 * @testdox 'register_additional_features' emits a deprecation notice.
 	 */
 	public function test_register_additional_features_is_deprecated(): void {
 		$this->setExpectedDeprecated( FeaturesController::class . '::register_additional_features' );
@@ -246,36 +247,17 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
 	}

 	/**
-	 * @testdox Built-in initialization preserves an early registration with the same feature ID.
+	 * @testdox The built-in definition wins when a feature with the same slug was registered before initialization.
 	 */
-	public function test_early_registration_of_builtin_feature_is_preserved(): void {
-		$option_key = 'extension_rate_limit_checkout_enabled';
-		delete_option( $option_key );
-
+	public function test_builtin_definition_replaces_early_registration_with_same_slug(): void {
 		$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,
-			)
-		);
+		$controller->add_feature_definition( 'analytics', 'Not the core analytics', array( 'default_plugin_compatibility' => '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' );
+		$features = $controller->get_features( true );

-		$this->assertFalse( $controller->feature_is_enabled( 'rate_limit_checkout' ), 'The original option key should still control the feature after listing.' );
+		$this->assertSame( 'WooCommerce Analytics', $features['analytics']['name'] );
+		$this->assertSame( Analytics::TOGGLE_OPTION_NAME, $controller->feature_enable_option_name( 'analytics' ) );
 	}

 	/**
@@ -338,21 +320,10 @@ class FeaturesControllerTest extends \WC_Unit_Test_Case {
 	 * @testdox Feature definition initialization is safe when a translation callback checks a feature, and the notice stays in English.
 	 */
 	public function test_feature_definition_initialization_is_reentrant_safe() {
-		$reflection_class = new \ReflectionClass( $this->sut );
-		$notice_function  = FeaturesController::class . '::maybe_init_feature_definitions';
+		$notice_function = FeaturesController::class . '::maybe_init_feature_definitions';
 		$this->setExpectedIncorrectUsage( $notice_function );

-		$features_property = $reflection_class->getProperty( 'features' );
-		$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() );
+		$this->reset_features_list( $this->sut, array(), false );

 		$reentrant_feature_enabled = null;
 		$translation_count         = 0;
diff --git a/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php b/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php
index 5fcc998719b..e719ef62fad 100644
--- a/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php
+++ b/plugins/woocommerce/tests/php/src/Utilities/PluginUtilTests.php
@@ -254,8 +254,6 @@ class PluginUtilTests extends \WC_Unit_Test_Case {
 	 * @testdox Test the `get_items_considered_incompatible` method.
 	 */
 	public function test_get_items_considered_incompatible() {
-		$this->reset_container_resolutions();
-
 		$features            = array(
 			'test_feature_1' => array(
 				'name'                         => 'Test feature 1',
@@ -322,7 +320,6 @@ class PluginUtilTests extends \WC_Unit_Test_Case {
 	 * @testdox Test 'get_wp_plugin_id' is not called during declaration but is called during query in lazy mode.
 	 */
 	public function test_lazy_normalization_during_query() {
-		$this->reset_container_resolutions();
 		$this->reset_legacy_proxy_mocks();

 		$get_plugins_call_count = 0;