Make it possible (far easier) to add menu items under 'Marketing' (https://github.com/woocommerce/woocommerce-admin/pull/4561)

* Make it possible to add marketing submenu pages

* Always set the parent value for marketing pages

* phpcs

* Add ‘overview’ to breadcrumb and path

* Fix permission check conditional

* Add parent menu at priority of 9, before other items are usually added

* Revert to use `/marketing` as the page path
This commit is contained in:
Daniel Bitzer 2020-06-13 07:25:15 +09:30 committed by GitHub
parent 0fc1ec29fb
commit ca517af7c0
2 changed files with 60 additions and 7 deletions

View File

@ -169,9 +169,13 @@ export const getPages = () => {
path: '/marketing', path: '/marketing',
breadcrumbs: [ breadcrumbs: [
...initialBreadcrumbs, ...initialBreadcrumbs,
[
'/marketing',
__( 'Marketing', 'woocommerce-admin' ), __( 'Marketing', 'woocommerce-admin' ),
], ],
wpOpenMenu: 'toplevel_page_wc-admin-path--marketing', __( 'Overview', 'woocommerce-admin' ),
],
wpOpenMenu: 'toplevel_page_woocommerce-marketing',
} ); } );
} }

View File

@ -50,7 +50,9 @@ class Marketing {
* Hook into WooCommerce. * Hook into WooCommerce.
*/ */
public function __construct() { public function __construct() {
add_action( 'admin_menu', array( $this, 'add_parent_menu_item' ), 9 );
add_action( 'admin_menu', array( $this, 'register_pages' ) ); add_action( 'admin_menu', array( $this, 'register_pages' ) );
add_action( 'admin_head', array( $this, 'modify_menu_structure' ) );
if ( ! is_admin() ) { if ( ! is_admin() ) {
return; return;
@ -60,17 +62,32 @@ class Marketing {
add_filter( 'woocommerce_shared_settings', array( $this, 'component_settings' ), 30 ); add_filter( 'woocommerce_shared_settings', array( $this, 'component_settings' ), 30 );
} }
/**
* Add main marketing menu item.
*
* Uses priority of 9 so other items can easily be added at the default priority (10).
*/
public function add_parent_menu_item() {
add_menu_page(
__( 'Marketing', 'woocommerce-admin' ),
__( 'Marketing', 'woocommerce-admin' ),
'manage_woocommerce',
'woocommerce-marketing',
null,
'dashicons-megaphone',
58
);
}
/** /**
* Registers report pages. * Registers report pages.
*/ */
public function register_pages() { public function register_pages() {
$marketing_pages = array( $marketing_pages = array(
array( array(
'id' => 'woocommerce-marketing', 'id' => 'woocommerce-marketing-overview',
'title' => __( 'Marketing', 'woocommerce-admin' ), 'title' => __( 'Overview', 'woocommerce-admin' ),
'path' => '/marketing', 'path' => '/marketing',
'icon' => 'dashicons-megaphone',
'position' => 58, // After WooCommerce & Product menu items.
), ),
); );
@ -78,11 +95,43 @@ class Marketing {
foreach ( $marketing_pages as $marketing_page ) { foreach ( $marketing_pages as $marketing_page ) {
if ( ! is_null( $marketing_page ) ) { if ( ! is_null( $marketing_page ) ) {
$marketing_page['parent'] = 'woocommerce-marketing';
wc_admin_register_page( $marketing_page ); wc_admin_register_page( $marketing_page );
} }
} }
} }
/**
* Modify the Marketing menu structure
*/
public function modify_menu_structure() {
global $submenu;
$marketing_submenu_key = 'woocommerce-marketing';
$overview_key = null;
// User does not have capabilites to see the submenu.
if ( ! current_user_can( 'manage_woocommerce' ) || empty( $submenu[ $marketing_submenu_key ] ) ) {
return;
}
foreach ( $submenu[ $marketing_submenu_key ] as $submenu_key => $submenu_item ) {
if ( 'wc-admin&path=/marketing' === $submenu_item[2] ) {
$overview_key = $submenu_key;
}
}
// Remove PHP powered top level page.
unset( $submenu[ $marketing_submenu_key ][0] );
// Move overview menu item to top.
if ( null !== $overview_key ) {
$menu = $submenu[ $marketing_submenu_key ][ $overview_key ];
unset( $submenu[ $marketing_submenu_key ][ $overview_key ] );
array_unshift( $submenu[ $marketing_submenu_key ], $menu );
}
}
/** /**
* Preload options to prime state of the application. * Preload options to prime state of the application.
* *