ArrayUtil.php
1 year ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
10 months ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
NumberUtil.php
10 months ago
OrderUtil.php
8 months ago
PluginUtil.php
7 months ago
RestApiUtil.php
2 years ago
ShippingUtil.php
1 year ago
StringUtil.php
1 year ago
TimeUtil.php
2 years ago
FeaturesUtil.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * FeaturesUtil class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Utilities; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\Features\FeaturesController; |
| 9 | |
| 10 | /** |
| 11 | * Class with methods that allow to retrieve information about the existing WooCommerce features, |
| 12 | * also has methods for WooCommerce plugins to declare (in)compatibility with the features. |
| 13 | */ |
| 14 | class FeaturesUtil { |
| 15 | |
| 16 | /** |
| 17 | * Get all the existing WooCommerce features. |
| 18 | * |
| 19 | * Returns an associative array where keys are unique feature ids |
| 20 | * and values are arrays with these keys: |
| 21 | * |
| 22 | * - name |
| 23 | * - description |
| 24 | * - is_experimental |
| 25 | * - is_enabled (if $include_enabled_info is passed as true) |
| 26 | * |
| 27 | * @param bool $include_experimental Include also experimental/work in progress features in the list. |
| 28 | * @param bool $include_enabled_info True to include the 'is_enabled' field in the returned features info. |
| 29 | * @returns array An array of information about existing features. |
| 30 | */ |
| 31 | public static function get_features( bool $include_experimental = false, bool $include_enabled_info = false ): array { |
| 32 | return wc_get_container()->get( FeaturesController::class )->get_features( $include_experimental, $include_enabled_info ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Check if a given feature is currently enabled. |
| 37 | * |
| 38 | * @param string $feature_id Unique feature id. |
| 39 | * @return bool True if the feature is enabled, false if not or if the feature doesn't exist. |
| 40 | */ |
| 41 | public static function feature_is_enabled( string $feature_id ): bool { |
| 42 | return wc_get_container()->get( FeaturesController::class )->feature_is_enabled( $feature_id ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Declare (in)compatibility with a given feature for a given plugin. |
| 47 | * |
| 48 | * This method MUST be executed from inside a handler for the 'before_woocommerce_init' hook and |
| 49 | * SHOULD be executed from the main plugin file passing __FILE__ or 'my-plugin/my-plugin.php' for the |
| 50 | * $plugin_file argument. |
| 51 | * |
| 52 | * @param string $feature_id Unique feature id. |
| 53 | * @param string $plugin_file The full plugin file path. |
| 54 | * @param bool $positive_compatibility True if the plugin declares being compatible with the feature, false if it declares being incompatible. |
| 55 | * @return bool True on success, false on error (feature doesn't exist or not inside the required hook). |
| 56 | */ |
| 57 | public static function declare_compatibility( string $feature_id, string $plugin_file, bool $positive_compatibility = true ): bool { |
| 58 | return wc_get_container()->get( FeaturesController::class )->declare_compatibility( |
| 59 | $feature_id, |
| 60 | $plugin_file, |
| 61 | $positive_compatibility |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the ids of the features that a certain plugin has declared compatibility for. |
| 67 | * |
| 68 | * This method can't be called before the 'woocommerce_init' hook is fired. |
| 69 | * |
| 70 | * @param string $plugin_name Plugin name, in the form 'directory/file.php'. |
| 71 | * @return array An array having a 'compatible' and an 'incompatible' key, each holding an array of plugin ids. |
| 72 | */ |
| 73 | public static function get_compatible_features_for_plugin( string $plugin_name ): array { |
| 74 | return wc_get_container()->get( FeaturesController::class )->get_compatible_features_for_plugin( $plugin_name ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the names of the plugins that have been declared compatible or incompatible with a given feature. |
| 79 | * |
| 80 | * @param string $feature_id Feature id. |
| 81 | * @return array An array having a 'compatible' and an 'incompatible' key, each holding an array of plugin names. |
| 82 | */ |
| 83 | public static function get_compatible_plugins_for_feature( string $feature_id ): array { |
| 84 | return wc_get_container()->get( FeaturesController::class )->get_compatible_plugins_for_feature( $feature_id ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sets a flag indicating that it's allowed to enable features for which incompatible plugins are active |
| 89 | * from the WooCommerce feature settings page. |
| 90 | */ |
| 91 | public static function allow_enabling_features_with_incompatible_plugins(): void { |
| 92 | wc_get_container()->get( FeaturesController::class )->allow_enabling_features_with_incompatible_plugins(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets a flag indicating that it's allowed to activate plugins for which incompatible features are enabled |
| 97 | * from the WordPress plugins page. |
| 98 | */ |
| 99 | public static function allow_activating_plugins_with_incompatible_features(): void { |
| 100 | wc_get_container()->get( FeaturesController::class )->allow_activating_plugins_with_incompatible_features(); |
| 101 | } |
| 102 | } |
| 103 |