From 0809b07254237bea363b8b28af1fedbbc372cba7 Mon Sep 17 00:00:00 2001 From: Joshua T Flowers Date: Wed, 21 Oct 2020 13:15:36 -0400 Subject: [PATCH] Add features section and settings (https://github.com/woocommerce/woocommerce-admin/pull/5381) * Add features section and settings * Force page refresh on option update --- .../src/Features/Navigation/Navigation.php | 17 +++++++ plugins/woocommerce-admin/src/Loader.php | 48 +++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/plugins/woocommerce-admin/src/Features/Navigation/Navigation.php b/plugins/woocommerce-admin/src/Features/Navigation/Navigation.php index 3938dae0246..423a5ad7de4 100644 --- a/plugins/woocommerce-admin/src/Features/Navigation/Navigation.php +++ b/plugins/woocommerce-admin/src/Features/Navigation/Navigation.php @@ -22,6 +22,7 @@ class Navigation { public function __construct() { add_filter( 'woocommerce_admin_preload_options', array( $this, 'preload_options' ) ); add_filter( 'woocommerce_admin_features', array( $this, 'maybe_remove_nav_feature' ), 0 ); + add_action( 'update_option_woocommerce_navigation_enabled', array( $this, 'reload_page_on_toggle' ), 10, 2 ); if ( Loader::is_feature_enabled( 'navigation' ) ) { add_action( 'in_admin_header', array( __CLASS__, 'embed_navigation' ) ); @@ -88,4 +89,20 @@ class Navigation { Loader::get_file_version( 'css' ) ); } + + /** + * Reloads the page when the option is toggled to make sure all nav 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'] ) ); + } + } } diff --git a/plugins/woocommerce-admin/src/Loader.php b/plugins/woocommerce-admin/src/Loader.php index 2ba42e3a315..352d7f8e9ad 100644 --- a/plugins/woocommerce-admin/src/Loader.php +++ b/plugins/woocommerce-admin/src/Loader.php @@ -60,6 +60,8 @@ class Loader { add_action( 'init', array( __CLASS__, 'define_tables' ) ); // Load feature before WooCommerce update hooks. add_action( 'init', array( __CLASS__, 'load_features' ), 4 ); + add_filter( 'woocommerce_get_sections_advanced', array( __CLASS__, 'add_features_section' ) ); + add_filter( 'woocommerce_get_settings_advanced', array( __CLASS__, 'add_features_settings' ), 10, 2 ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'inject_wc_settings_dependencies' ), 14 ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts' ), 15 ); @@ -232,6 +234,52 @@ class Loader { } } + /** + * Adds the Features section to the advanced tab of WooCommerce Settings + * + * @param array $sections Sections. + * @return array + */ + public static function add_features_section( $sections ) { + $sections['features'] = __( 'Features', 'woocommerce-admin' ); + return $sections; + } + + /** + * Adds the Features settings. + * + * @param array $settings Settings. + * @param string $current_section Current section slug. + * @return array + */ + public static function add_features_settings( $settings, $current_section ) { + if ( 'features' !== $current_section ) { + return $settings; + } + + return apply_filters( + 'woocommerce_settings_features', + array( + array( + 'title' => __( 'Features', 'woocommerce-admin' ), + 'type' => 'title', + 'desc' => __( 'Start using new features that are being progressively rolled out to improve the store management experience.', 'woocommerce-admin' ), + 'id' => 'features_options', + ), + array( + 'title' => __( 'Navigation', 'woocommerce-admin' ), + 'desc' => __( 'Adds the new WooCommerce navigation experience to the dashboard', 'woocommerce-admin' ), + 'id' => 'woocommerce_navigation_enabled', + 'type' => 'checkbox', + ), + array( + 'type' => 'sectionend', + 'id' => 'features_options', + ), + ) + ); + } + /** * Connects existing WooCommerce pages. *