* Add settings pages

* Replace the original settings page in the wp menu

* Add the settings feature toggle

* Get core items at later priority to allow registering all settings
This commit is contained in:
Joshua T Flowers 2021-02-12 13:46:29 -05:00 committed by GitHub
parent 683f8adf33
commit 4bbf3c3dc0
7 changed files with 145 additions and 1 deletions

View File

@ -13,6 +13,7 @@
"navigation": false,
"onboarding": true,
"remote-inbox-notifications": true,
"settings": false,
"shipping-label-banner": true,
"store-alerts": true,
"wcpay": true

View File

@ -13,6 +13,7 @@
"navigation": true,
"onboarding": true,
"remote-inbox-notifications": true,
"settings": true,
"shipping-label-banner": true,
"store-alerts": true,
"wcpay": true

View File

@ -13,6 +13,7 @@
"navigation": true,
"onboarding": true,
"remote-inbox-notifications": true,
"settings": false,
"shipping-label-banner": true,
"store-alerts": true,
"wcpay": true

View File

@ -60,8 +60,10 @@ class Features {
$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;
}
return $features;

View File

@ -7,6 +7,7 @@
namespace Automattic\WooCommerce\Admin\Features\Navigation;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\Navigation\Menu;
use Automattic\WooCommerce\Admin\Features\Navigation\Screen;
@ -44,6 +45,11 @@ class CoreMenu {
* Add registered admin settings as menu items.
*/
public static function get_setting_items() {
// Let the Settings feature add pages to the navigation if enabled.
if ( Features::is_enabled( 'settings' ) ) {
return array();
}
// Calling this method adds pages to the below tabs filter on non-settings pages.
\WC_Admin_Settings::get_settings_pages();
$tabs = apply_filters( 'woocommerce_settings_tabs_array', array() );

View File

@ -86,7 +86,7 @@ class Menu {
* Init.
*/
public function init() {
add_action( 'admin_menu', array( $this, 'add_core_items' ) );
add_action( 'admin_menu', array( $this, 'add_core_items' ), PHP_INT_MAX );
add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_data' ), 20 );
add_filter( 'admin_menu', array( $this, 'migrate_core_child_items' ), PHP_INT_MAX - 1 );

View File

@ -0,0 +1,133 @@
<?php
/**
* WooCommerce Settings.
* NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-admin.
*/
namespace Automattic\WooCommerce\Admin\Features;
use Automattic\WooCommerce\Admin\Marketing\InstalledExtensions;
use Automattic\WooCommerce\Admin\Loader;
use Automattic\WooCommerce\Admin\PageController;
/**
* Contains backend logic for the Settings feature.
*/
class Settings {
/**
* Option name used to toggle this feature.
*/
const TOGGLE_OPTION_NAME = 'woocommerce_settings_enabled';
/**
* Class instance.
*
* @var Settings instance
*/
protected static $instance = null;
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WooCommerce.
*/
public function __construct() {
if ( ! is_admin() ) {
return;
}
add_filter( 'woocommerce_settings_features', array( $this, 'add_feature_toggle' ) );
if ( 'yes' !== get_option( 'woocommerce_settings_enabled', 'no' ) ) {
return;
}
// Run this after the original WooCommerce settings have been added.
add_action( 'admin_menu', array( $this, 'register_pages' ), 60 );
}
/**
* Add the feature toggle to the features settings.
*
* @param array $features Feature sections.
* @return array
*/
public static function add_feature_toggle( $features ) {
$features[] = array(
'title' => __( 'Settings', 'woocommerce-admin' ),
'desc' => __(
'Adds the new WooCommerce settings UI.',
'woocommerce-admin'
),
'id' => 'woocommerce_settings_enabled',
'type' => 'checkbox',
);
return $features;
}
/**
* Registers settings pages.
*/
public function register_pages() {
$controller = PageController::get_instance();
$setting_pages = \WC_Admin_Settings::get_settings_pages();
$settings = array();
foreach ( $setting_pages as $setting_page ) {
$settings = $setting_page->add_settings_page( $settings );
}
$order = 0;
foreach ( $settings as $key => $setting ) {
$order += 10;
$settings_page = array(
'parent' => 'woocommerce-settings',
'title' => $setting,
'id' => 'settings-' . $key,
'path' => "/settings/$key",
'nav_args' => array(
'capability' => 'manage_woocommerce',
'order' => $order,
'parent' => 'woocommerce-settings',
),
);
// Replace the old menu with the first settings item.
if ( 10 === $order ) {
$this->replace_settings_page( $settings_page );
}
$controller->register_page( $settings_page );
}
}
/**
* Replace the Settings page in the original WooCommerce menu.
*
* @param array $page Page used to replace the original.
*/
protected function replace_settings_page( $page ) {
global $submenu;
// Check if WooCommerce parent menu has been registered.
if ( ! isset( $submenu['woocommerce'] ) ) {
return;
}
foreach ( $submenu['woocommerce'] as &$item ) {
// The "slug" (aka the path) is the third item in the array.
if ( 0 === strpos( $item[2], 'wc-settings' ) ) {
$item[2] = wc_admin_url( "&path={$page['path']}" );
}
}
}
}