Refactor app entry page and dashboard page registration.

This commit is contained in:
Jeff Stieler 2019-05-07 13:48:34 -06:00
parent 7506201571
commit 9fdf2cfb47
3 changed files with 76 additions and 28 deletions

View File

@ -10,6 +10,10 @@
* WC_Admin_Loader Class.
*/
class WC_Admin_Loader {
/**
* App entry point.
*/
const APP_ENTRY_POINT = 'wc-admin';
/**
* Class instance.
@ -56,7 +60,7 @@ class WC_Admin_Loader {
add_action( 'admin_notices', array( 'WC_Admin_Loader', 'inject_after_notices' ), PHP_INT_MAX );
// priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165.
add_action( 'admin_head', array( 'WC_Admin_Loader', 'update_link_structure' ), 20 );
add_action( 'admin_head', array( 'WC_Admin_Loader', 'remove_app_entry_page_menu_item' ), 20 );
}
/**
@ -138,28 +142,20 @@ class WC_Admin_Loader {
* @todo The entry point for the embed needs moved to this class as well.
*/
public static function register_page_handler() {
$page_title = null;
$menu_title = null;
if ( self::is_feature_enabled( 'analytics-dashboard' ) ) {
$page_title = __( 'WooCommerce Dashboard', 'woocommerce-admin' );
$menu_title = __( 'Dashboard', 'woocommerce-admin' );
}
add_submenu_page(
'woocommerce',
$page_title,
$menu_title,
'manage_options',
'wc-admin',
array( 'WC_Admin_Loader', 'page_wrapper' )
wc_admin_register_page(
array(
'id' => 'woocommerce-dashboard', // Expected to be overridden if dashboard is enabled.
'parent' => 'woocommerce',
'title' => null,
'path' => self::APP_ENTRY_POINT,
)
);
}
/**
* Update the WooCommerce menu structure to make our main dashboard/handler the top level link for 'WooCommerce'.
* Remove the menu item for the app entry point page.
*/
public static function update_link_structure() {
public static function remove_app_entry_page_menu_item() {
global $submenu;
// User does not have capabilites to see the submenu.
if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) {
@ -168,7 +164,8 @@ class WC_Admin_Loader {
$wc_admin_key = null;
foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) {
if ( 'wc-admin' === $submenu_item[2] ) {
// Our app entry page menu item has no title.
if ( is_null( $submenu_item[0] ) && self::APP_ENTRY_POINT === $submenu_item[2] ) {
$wc_admin_key = $submenu_key;
break;
}
@ -178,11 +175,7 @@ class WC_Admin_Loader {
return;
}
$menu = $submenu['woocommerce'][ $wc_admin_key ];
// Move menu item to top of array.
unset( $submenu['woocommerce'][ $wc_admin_key ] );
array_unshift( $submenu['woocommerce'], $menu );
}
/**

View File

@ -10,6 +10,11 @@
* Contains backend logic for the dashboard feature.
*/
class WC_Admin_Analytics_Dashboard {
/**
* Menu slug.
*/
const MENU_SLUG = 'wc-admin';
/**
* Class instance.
*
@ -33,6 +38,9 @@ class WC_Admin_Analytics_Dashboard {
public function __construct() {
add_filter( 'woocommerce_component_settings_preload_endpoints', array( $this, 'add_preload_endpoints' ) );
add_filter( 'wc_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'admin_menu', array( $this, 'register_page' ) );
// priority is 20 to run after https://github.com/woocommerce/woocommerce/blob/a55ae325306fc2179149ba9b97e66f32f84fdd9c/includes/admin/class-wc-admin-menus.php#L165.
add_action( 'admin_head', array( $this, 'update_link_structure' ), 20 );
}
/**
@ -64,6 +72,50 @@ class WC_Admin_Analytics_Dashboard {
)
);
}
/**
* Registers dashboard page.
*/
public function register_page() {
wc_admin_register_page(
array(
'id' => 'woocommerce-dashboard',
'title' => 'Dashboard',
'parent' => 'woocommerce',
'path' => self::MENU_SLUG,
)
);
}
/**
* Update the WooCommerce menu structure to make our main dashboard/handler
* the top level link for 'WooCommerce'.
*/
public function update_link_structure() {
global $submenu;
// User does not have capabilites to see the submenu.
if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu['woocommerce'] ) ) {
return;
}
$wc_admin_key = null;
foreach ( $submenu['woocommerce'] as $submenu_key => $submenu_item ) {
if ( self::MENU_SLUG === $submenu_item[2] ) {
$wc_admin_key = $submenu_key;
break;
}
}
if ( ! $wc_admin_key ) {
return;
}
$menu = $submenu['woocommerce'][ $wc_admin_key ];
// Move menu item to top of array.
unset( $submenu['woocommerce'][ $wc_admin_key ] );
array_unshift( $submenu['woocommerce'], $menu );
}
}
new WC_Admin_Analytics_Dashboard();

View File

@ -10,7 +10,7 @@
*/
class WC_Admin_Page_Controller {
// JS-powered page root.
const PAGE_ROOT = 'wc-admin#';
const PAGE_ROOT = 'wc-admin';
/**
* Singleton instance of self.
@ -42,13 +42,13 @@ class WC_Admin_Page_Controller {
* Returns the path from an ID.
*
* @param string $id ID to get path for.
* @return string|null Path for the given ID.
* @return string Path for the given ID, or the ID on lookup miss.
*/
public function get_path_from_id( $id ) {
if ( isset( $this->pages[ $id ] ) && isset( $this->pages[ $id ]['path'] ) ) {
return $this->pages[ $id ]['path'];
}
return null;
return $id;
}
/**
@ -77,8 +77,11 @@ class WC_Admin_Page_Controller {
'position' => null,
);
$options = wp_parse_args( $options, $defaults );
$options['path'] = self::PAGE_ROOT . $options['path'];
$options = wp_parse_args( $options, $defaults );
if ( 0 !== strpos( $options['path'], self::PAGE_ROOT ) ) {
$options['path'] = self::PAGE_ROOT . '#' . $options['path'];
}
// TODO: check for null ID, or collision.
$this->pages[ $options['id'] ] = $options;