* add toggle and check before registering routes

* remove preloaded option

* early return

* changelog

* page refresh on option change

* pass value to client

* use Features::is_enabled

* handle option default

* go back to using feature flags on client

* remove unused use

* hide customers behind analytics flag for now

* remove unneeded class check
This commit is contained in:
Paul Sealock 2021-06-23 13:50:15 +12:00 committed by GitHub
parent c683376761
commit 011b098645
4 changed files with 101 additions and 18 deletions

View File

@ -78,6 +78,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
- Add: SlotFill to Abbreviated Notification panel #7091
- Add: Add unit tests around extended payment gateway controller #7133
- Add: Add payment gateway suggestion unit tests #7142
- Add: Feature toggle to disable Analytics UI #7168
- Fix: WCPay not working in local payments task #7151
- Fix: Include onboarding settings on the analytic pages #7109
- Fix: RemoteFreeExtension hide bundle when all of its plugins are not visible #7182

View File

@ -8,11 +8,16 @@ namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\Loader;
use Automattic\WooCommerce\Admin\API\Reports\Cache;
use Automattic\WooCommerce\Admin\Features\Features;
/**
* Contains backend logic for the Analytics feature.
*/
class Analytics {
/**
* Option name used to toggle this feature.
*/
const TOGGLE_OPTION_NAME = 'woocommerce_analytics_enabled';
/**
* Clear cache tool identifier.
*/
@ -39,12 +44,73 @@ class Analytics {
* Hook into WooCommerce.
*/
public function __construct() {
add_filter( 'woocommerce_settings_features', array( $this, 'add_feature_toggle' ) );
add_action( 'update_option_' . self::TOGGLE_OPTION_NAME, array( $this, 'reload_page_on_toggle' ), 10, 2 );
add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) );
if ( ! Features::is_enabled( 'analytics' ) ) {
return;
}
add_filter( 'woocommerce_component_settings_preload_endpoints', array( $this, 'add_preload_endpoints' ) );
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'admin_menu', array( $this, 'register_pages' ) );
add_filter( 'woocommerce_debug_tools', array( $this, 'register_cache_clear_tool' ) );
}
/**
* Add the feature toggle to the features settings.
*
* @param array $features Feature sections.
* @return array
*/
public static function add_feature_toggle( $features ) {
$description = __(
'Enables WooCommerce Analytics',
'woocommerce-admin'
);
$features[] = array(
'title' => __( 'Analytics', 'woocommerce-admin' ),
'desc' => $description,
'id' => self::TOGGLE_OPTION_NAME,
'type' => 'checkbox',
'default' => 'yes',
'class' => '',
);
return $features;
}
/**
* Preload options to prime state of the application.
*
* @param array $options Array of options to preload.
* @return array
*/
public function preload_options( $options ) {
$options[] = self::TOGGLE_OPTION_NAME;
return $options;
}
/**
* Reloads the page when the option is toggled to make sure all Analytics features are loaded.
*
* @param string $old_value Old value.
* @param string $value New value.
*/
public static function reload_page_on_toggle( $old_value, $value ) {
if ( $old_value === $value ) {
return;
}
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
wp_safe_redirect( wp_unslash( $_SERVER['REQUEST_URI'] ) );
exit();
}
}
/**
* Preload data from the countries endpoint.
*

View File

@ -18,6 +18,17 @@ class Features {
*/
protected static $instance = null;
/**
* Optional features
*
* @var array
*/
protected static $optional_features = array(
'navigation' => array( 'default' => 'no' ),
'settings' => array( 'default' => 'no' ),
'analytics' => array( 'default' => 'yes' ),
);
/**
* Get class instance.
*/
@ -52,20 +63,20 @@ class Features {
}
/**
* Gets the beta feature options as an associative array that can be toggled on or off.
* Gets the optional feature options as an associative array that can be toggled on or off.
*
* @return array
*/
public static function get_beta_feature_options() {
public static function get_optional_feature_options() {
$features = [];
$navigation_class = self::get_feature_class( 'navigation' );
$settings_class = self::get_feature_class( 'settings' );
if ( $navigation_class ) {
$features['navigation'] = $navigation_class::TOGGLE_OPTION_NAME;
$features['settings'] = $settings_class::TOGGLE_OPTION_NAME;
}
foreach ( array_keys( self::$optional_features ) as $optional_feature_key ) {
$feature_class = self::get_feature_class( $optional_feature_key );
if ( $feature_class ) {
$features[ $optional_feature_key ] = $feature_class::TOGGLE_OPTION_NAME;
}
}
return $features;
}
@ -117,7 +128,7 @@ class Features {
}
/**
* Check if a feature is enabled. Defaults to true for all features unless they are in beta.
* Check if a feature is enabled. Defaults to true for all features unless they are optional.
*
* @param string $feature Feature slug.
* @return bool
@ -127,7 +138,7 @@ class Features {
return false;
}
$features = self::get_beta_feature_options();
$features = self::get_optional_feature_options();
if ( isset( $features[ $feature ] ) ) {
$feature_option = $features[ $feature ];
@ -137,20 +148,22 @@ class Features {
return true;
}
return 'yes' === get_option( $feature_option, 'no' );
$default = isset( self::$optional_features[ $feature ]['default'] ) ? self::$optional_features[ $feature ]['default'] : 'no';
return 'yes' === get_option( $feature_option, $default );
}
return true;
}
/**
* Enable a toggleable beta feature.
* Enable a toggleable optional feature.
*
* @param string $feature Feature name.
* @return bool
*/
public static function enable( $feature ) {
$features = self::get_beta_feature_options();
$features = self::get_optional_feature_options();
if ( isset( $features[ $feature ] ) ) {
update_option( $features[ $feature ], 'yes' );
@ -161,13 +174,13 @@ class Features {
}
/**
* Disable a toggleable beta feature.
* Disable a toggleable optional feature.
*
* @param string $feature Feature name.
* @return bool
*/
public static function disable( $feature ) {
$features = self::get_beta_feature_options();
$features = self::get_optional_feature_options();
if ( isset( $features[ $feature ] ) ) {
update_option( $features[ $feature ], 'no' );

View File

@ -79,6 +79,7 @@ class CoreMenu {
* @return array
*/
public static function get_categories() {
$analytics_enabled = Features::is_enabled( 'analytics' );
return array(
array(
'title' => __( 'Orders', 'woocommerce-admin' ),
@ -90,17 +91,19 @@ class CoreMenu {
'id' => 'woocommerce-products',
'order' => 20,
),
$analytics_enabled ?
array(
'title' => __( 'Analytics', 'woocommerce-admin' ),
'id' => 'woocommerce-analytics',
'order' => 30,
),
) : null,
$analytics_enabled ?
array(
'title' => __( 'Reports', 'woocommerce-admin' ),
'id' => 'woocommerce-reports',
'parent' => 'woocommerce-analytics',
'order' => 200,
),
) : null,
array(
'title' => __( 'Marketing', 'woocommerce-admin' ),
'id' => 'woocommerce-marketing',
@ -186,7 +189,7 @@ class CoreMenu {
}
$customers_item = array();
if ( class_exists( '\Automattic\WooCommerce\Admin\Features\Analytics' ) ) {
if ( Features::is_enabled( 'analytics' ) ) {
$customers_item = array(
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce-admin' ),