From a1f5b71e272213006b81c51135e8d5cb47bae919 Mon Sep 17 00:00:00 2001 From: "Jorge A. Torres" Date: Wed, 28 Sep 2022 17:17:08 -0300 Subject: [PATCH] Allow features to declare a default 'enabled' state (#34867) Allow features to have a default state via `enabled_by_default`. * Remove unnecessary import * Allow features to have a default state via `enabled_by_default` * Add changelog --- plugins/woocommerce/changelog/fix-34866 | 4 +++ .../src/Admin/Features/Features.php | 1 - .../Internal/Features/FeaturesController.php | 27 +++++++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-34866 diff --git a/plugins/woocommerce/changelog/fix-34866 b/plugins/woocommerce/changelog/fix-34866 new file mode 100644 index 00000000000..25019e6042f --- /dev/null +++ b/plugins/woocommerce/changelog/fix-34866 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Allow features to declare their initial enabled state. diff --git a/plugins/woocommerce/src/Admin/Features/Features.php b/plugins/woocommerce/src/Admin/Features/Features.php index bf797ef1eb9..ab3ae7e7f46 100644 --- a/plugins/woocommerce/src/Admin/Features/Features.php +++ b/plugins/woocommerce/src/Admin/Features/Features.php @@ -8,7 +8,6 @@ namespace Automattic\WooCommerce\Admin\Features; use Automattic\WooCommerce\Admin\PageController; use Automattic\WooCommerce\Internal\Admin\Loader; use Automattic\WooCommerce\Internal\Admin\WCAdminAssets; -use Automattic\WooCommerce\Internal\Features\FeaturesController; /** * Features Class. diff --git a/plugins/woocommerce/src/Internal/Features/FeaturesController.php b/plugins/woocommerce/src/Internal/Features/FeaturesController.php index 63cc9ee7ca2..630c6ff2181 100644 --- a/plugins/woocommerce/src/Internal/Features/FeaturesController.php +++ b/plugins/woocommerce/src/Internal/Features/FeaturesController.php @@ -57,10 +57,11 @@ class FeaturesController { */ public function __construct() { $features = array( - 'analytics' => array( - 'name' => __( 'Analytics', 'woocommerce' ), - 'description' => __( 'Enables WooCommerce Analytics', 'woocommerce' ), - 'is_experimental' => false, + 'analytics' => array( + 'name' => __( 'Analytics', 'woocommerce' ), + 'description' => __( 'Enables WooCommerce Analytics', 'woocommerce' ), + 'is_experimental' => false, + 'enabled_by_default' => true, ), 'new_navigation' => array( 'name' => __( 'Navigation', 'woocommerce' ), @@ -163,7 +164,22 @@ class FeaturesController { * @return bool True if the feature is enabled, false if not or if the feature doesn't exist. */ public function feature_is_enabled( string $feature_id ): bool { - return $this->feature_exists( $feature_id ) && 'yes' === get_option( $this->feature_enable_option_name( $feature_id ) ); + if ( ! $this->feature_exists( $feature_id ) ) { + return false; + } + + $default_value = $this->feature_is_enabled_by_default( $feature_id ) ? 'yes' : 'no'; + return 'yes' === get_option( $this->feature_enable_option_name( $feature_id ), $default_value ); + } + + /** + * Check if a given feature is enabled by default. + * + * @param string $feature_id Unique feature id. + * @return boolean TRUE if the feature is enabled by default, FALSE otherwise. + */ + private function feature_is_enabled_by_default( string $feature_id ): bool { + return ! empty( $this->features[ $feature_id ]['enabled_by_default'] ); } /** @@ -502,6 +518,7 @@ class FeaturesController { 'id' => $this->feature_enable_option_name( $feature_id ), 'class' => $disabled ? 'disabled' : '', 'desc_tip' => $desc_tip, + 'default' => $this->feature_is_enabled_by_default( $feature_id ) ? 'yes' : 'no', ); } }