woocommerce/plugins/woocommerce-admin/src/Features/Settings.php

134 lines
3.1 KiB
PHP
Raw Normal View History

<?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']}" );
}
}
}
}