ArrayUtil.php
1 year ago
CallbackUtil.php
5 months ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
5 months ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
MetaDataUtil.php
2 months ago
NumberUtil.php
11 months ago
OrderUtil.php
7 months ago
PluginUtil.php
4 weeks ago
RestApiUtil.php
7 months ago
ShippingUtil.php
1 year ago
StringUtil.php
2 years ago
TimeUtil.php
2 years ago
FeaturesUtil.php
133 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 | $features_controller = wc_get_container()->get( FeaturesController::class ); |
| 43 | $feature = $features_controller->get_feature_definition( $feature_id ); |
| 44 | |
| 45 | // Log deprecation notice for deprecated features (only from the public API). |
| 46 | if ( ! empty( $feature['deprecated_since'] ) ) { |
| 47 | self::log_deprecated_feature_usage( $feature_id, $feature['deprecated_since'] ); |
| 48 | } |
| 49 | |
| 50 | return $features_controller->feature_is_enabled( $feature_id ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Log usage of a deprecated feature. |
| 55 | * |
| 56 | * This method ensures logging only happens once per request to avoid spam. |
| 57 | * |
| 58 | * @param string $feature_id The feature id being checked. |
| 59 | * @param string $deprecated_since The version since which the feature is deprecated. |
| 60 | */ |
| 61 | private static function log_deprecated_feature_usage( string $feature_id, string $deprecated_since ): void { |
| 62 | static $logged = array(); |
| 63 | |
| 64 | if ( isset( $logged[ $feature_id ] ) ) { |
| 65 | return; |
| 66 | } |
| 67 | $logged[ $feature_id ] = true; |
| 68 | |
| 69 | wc_deprecated_function( |
| 70 | "FeaturesUtil::feature_is_enabled('{$feature_id}')", |
| 71 | $deprecated_since |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Declare (in)compatibility with a given feature for a given plugin. |
| 77 | * |
| 78 | * This method MUST be executed from inside a handler for the 'before_woocommerce_init' hook and |
| 79 | * SHOULD be executed from the main plugin file passing __FILE__ or 'my-plugin/my-plugin.php' for the |
| 80 | * $plugin_file argument. |
| 81 | * |
| 82 | * @param string $feature_id Unique feature id. |
| 83 | * @param string $plugin_file The full plugin file path. |
| 84 | * @param bool $positive_compatibility True if the plugin declares being compatible with the feature, false if it declares being incompatible. |
| 85 | * @return bool True on success, false on error (feature doesn't exist or not inside the required hook). |
| 86 | */ |
| 87 | public static function declare_compatibility( string $feature_id, string $plugin_file, bool $positive_compatibility = true ): bool { |
| 88 | return wc_get_container()->get( FeaturesController::class )->declare_compatibility( |
| 89 | $feature_id, |
| 90 | $plugin_file, |
| 91 | $positive_compatibility |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the ids of the features that a certain plugin has declared compatibility for. |
| 97 | * |
| 98 | * This method can't be called before the 'woocommerce_init' hook is fired. |
| 99 | * |
| 100 | * @param string $plugin_name Plugin name, in the form 'directory/file.php'. |
| 101 | * @return array An array having a 'compatible' and an 'incompatible' key, each holding an array of plugin ids. |
| 102 | */ |
| 103 | public static function get_compatible_features_for_plugin( string $plugin_name ): array { |
| 104 | return wc_get_container()->get( FeaturesController::class )->get_compatible_features_for_plugin( $plugin_name ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the names of the plugins that have been declared compatible or incompatible with a given feature. |
| 109 | * |
| 110 | * @param string $feature_id Feature id. |
| 111 | * @return array An array having a 'compatible' and an 'incompatible' key, each holding an array of plugin names. |
| 112 | */ |
| 113 | public static function get_compatible_plugins_for_feature( string $feature_id ): array { |
| 114 | return wc_get_container()->get( FeaturesController::class )->get_compatible_plugins_for_feature( $feature_id ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Sets a flag indicating that it's allowed to enable features for which incompatible plugins are active |
| 119 | * from the WooCommerce feature settings page. |
| 120 | */ |
| 121 | public static function allow_enabling_features_with_incompatible_plugins(): void { |
| 122 | wc_get_container()->get( FeaturesController::class )->allow_enabling_features_with_incompatible_plugins(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Sets a flag indicating that it's allowed to activate plugins for which incompatible features are enabled |
| 127 | * from the WordPress plugins page. |
| 128 | */ |
| 129 | public static function allow_activating_plugins_with_incompatible_features(): void { |
| 130 | wc_get_container()->get( FeaturesController::class )->allow_activating_plugins_with_incompatible_features(); |
| 131 | } |
| 132 | } |
| 133 |