Automattic\WooCommerce\Internal\Features

FeaturesController::register_compatibility_internalprivateWC 10.1.0

Registers compatibility information internally for a given feature and plugin file.

This method normalizes the plugin file path to a plugin ID, handles validation and logging for invalid plugins, and registers the compatibility data if valid. It updates the internal compatibility arrays, checks for conflicts (e.g., a plugin declaring both compatible and incompatible with the same feature), and throws an exception if a conflict is detected. Duplicate declarations (same compatibility type) are ignored.

This is an internal helper method and should not be called directly.

Method of the class: FeaturesController{}

No Hooks.

Returns

true|false. True on successful registration, false if the feature does not exist.

Usage

// private - for code of main (parent) class only
$result = $this->register_compatibility_internal( $feature_id, $plugin_file, $positive_compatibility ): bool;
$feature_id(string) (required)
Unique feature ID.
$plugin_file(string) (required)
Raw plugin file path (full or 'directory/file.php').
$positive_compatibility(true|false) (required)
True if declaring compatibility, false if declaring incompatibility.

Changelog

Since 10.1.0 Introduced.

FeaturesController::register_compatibility_internal() code WC 10.3.3

private function register_compatibility_internal( string $feature_id, string $plugin_file, bool $positive_compatibility ): bool {
	if ( ! $this->feature_exists( $feature_id ) ) {
		return false;
	}

	// Normalize and validate plugin file.
	$plugin_id = $this->plugin_util->get_wp_plugin_id( $plugin_file );
	if ( ! $plugin_id ) {
		$logger = $this->proxy->call_function( 'wc_get_logger' );
		$logger->error( "FeaturesController: Invalid plugin file '{$plugin_file}' for feature '{$feature_id}'." );
		return false;
	}

	// Register compatibility by plugin.
	ArrayUtil::ensure_key_is_array( $this->compatibility_info_by_plugin, $plugin_id );

	$key          = $positive_compatibility ? FeaturePluginCompatibility::COMPATIBLE : FeaturePluginCompatibility::INCOMPATIBLE;
	$opposite_key = $positive_compatibility ? FeaturePluginCompatibility::INCOMPATIBLE : FeaturePluginCompatibility::COMPATIBLE;
	ArrayUtil::ensure_key_is_array( $this->compatibility_info_by_plugin[ $plugin_id ], $key );
	ArrayUtil::ensure_key_is_array( $this->compatibility_info_by_plugin[ $plugin_id ], $opposite_key );

	if ( in_array( $feature_id, $this->compatibility_info_by_plugin[ $plugin_id ][ $opposite_key ], true ) ) {
		throw new \Exception( esc_html( "Plugin $plugin_id is trying to declare itself as $key with the '$feature_id' feature, but it already declared itself as $opposite_key" ) );
	}

	if ( ! in_array( $feature_id, $this->compatibility_info_by_plugin[ $plugin_id ][ $key ], true ) ) {
		$this->compatibility_info_by_plugin[ $plugin_id ][ $key ][] = $feature_id;
	}

	// Register compatibility by feature.
	$key = $positive_compatibility ? FeaturePluginCompatibility::COMPATIBLE : FeaturePluginCompatibility::INCOMPATIBLE;

	if ( ! in_array( $plugin_id, $this->compatibility_info_by_feature[ $feature_id ][ $key ], true ) ) {
		$this->compatibility_info_by_feature[ $feature_id ][ $key ][] = $plugin_id;
	}

	return true;
}