Automattic\WooCommerce\Internal\Features

FeaturesController::declare_compatibility()publicWC 1.0

Declare (in)compatibility with a given feature for a given plugin.

This method MUST be executed from inside a handler for the before_woocommerce_init

The plugin name is expected to be in the form 'directory/file.php' and be one of the keys of the array returned by 'get_plugins', but this won't be checked. Plugins are expected to use FeaturesUtil::declare_compatibility instead, passing the full plugin file path instead of the plugin name.

Метод класса: FeaturesController{}

No Hooks.

Return

true|false. True on success, false on error (feature doesn't exist or not inside the required hook).

Usage

$FeaturesController = new FeaturesController();
$FeaturesController->declare_compatibility( $feature_id, $plugin_name, $positive_compatibility ): bool;
$feature_id(string) (required)
Unique feature id.
$plugin_name(string) (required)
Plugin name, in the form 'directory/file.php'.
$positive_compatibility(true|false)
True if the plugin declares being compatible with the feature, false if it declares being incompatible.
Default: true

Code of FeaturesController::declare_compatibility() WC 7.1.0

public function declare_compatibility( string $feature_id, string $plugin_name, bool $positive_compatibility = true ): bool {
	if ( ! $this->proxy->call_function( 'doing_action', 'before_woocommerce_init' ) ) {
		$class_and_method = ( new \ReflectionClass( $this ) )->getShortName() . '::' . __FUNCTION__;
		/* translators: 1: class::method 2: before_woocommerce_init */
		$this->proxy->call_function( 'wc_doing_it_wrong', $class_and_method, sprintf( __( '%1$s should be called inside the %2$s action.', 'woocommerce' ), $class_and_method, 'before_woocommerce_init' ), '7.0' );
		return false;
	}

	if ( ! $this->feature_exists( $feature_id ) ) {
		return false;
	}

	$plugin_name = str_replace( '\\', '/', $plugin_name );

	// Register compatibility by plugin.

	ArrayUtil::ensure_key_is_array( $this->compatibility_info_by_plugin, $plugin_name );

	$key          = $positive_compatibility ? 'compatible' : 'incompatible';
	$opposite_key = $positive_compatibility ? 'incompatible' : 'compatible';
	ArrayUtil::ensure_key_is_array( $this->compatibility_info_by_plugin[ $plugin_name ], $key );
	ArrayUtil::ensure_key_is_array( $this->compatibility_info_by_plugin[ $plugin_name ], $opposite_key );

	if ( in_array( $feature_id, $this->compatibility_info_by_plugin[ $plugin_name ][ $opposite_key ], true ) ) {
		throw new \Exception( "Plugin $plugin_name 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_name ][ $key ], true ) ) {
		$this->compatibility_info_by_plugin[ $plugin_name ][ $key ][] = $feature_id;
	}

	// Register compatibility by feature.

	$key = $positive_compatibility ? 'compatible' : 'incompatible';

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

	return true;
}