Allow WCA registered pages to register plugin items and categories (https://github.com/woocommerce/woocommerce-admin/pull/5717)

* Allow WCA registered pages to register plugin items and categories

* Don't allow top-level item registration through WCA

* Remove is_top_level from menu API

* Prevent directly setting woocommerce as parent ID
This commit is contained in:
Joshua T Flowers 2020-12-01 10:22:00 -05:00 committed by GitHub
parent 60c3dcbd55
commit 4e29944b6c
4 changed files with 92 additions and 145 deletions

View File

@ -235,14 +235,10 @@ class Analytics {
),
) : null,
array(
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce-admin' ),
'parent' => 'woocommerce',
'path' => '/customers',
'nav_args' => array(
'is_top_level' => true,
'order' => 50,
),
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce-admin' ),
'parent' => 'woocommerce',
'path' => '/customers',
),
array(
'id' => 'woocommerce-analytics-settings',

View File

@ -69,12 +69,11 @@ class Homescreen {
public function register_page() {
wc_admin_register_page(
array(
'id' => 'woocommerce-home',
'title' => __( 'Home', 'woocommerce-admin' ),
'parent' => 'woocommerce',
'path' => self::MENU_SLUG,
'is_top_level' => true,
'order' => 0,
'id' => 'woocommerce-home',
'title' => __( 'Home', 'woocommerce-admin' ),
'parent' => 'woocommerce',
'path' => self::MENU_SLUG,
'order' => 0,
)
);
}

View File

@ -75,49 +75,43 @@ class CoreMenu {
public static function get_categories() {
return array(
array(
'title' => __( 'Orders', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-orders',
'order' => 10,
'is_top_level' => true,
'title' => __( 'Orders', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-orders',
'order' => 10,
),
array(
'title' => __( 'Products', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-products',
'order' => 20,
'is_top_level' => true,
'title' => __( 'Products', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-products',
'order' => 20,
),
array(
'title' => __( 'Analytics', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-analytics',
'order' => 30,
'is_top_level' => true,
'title' => __( 'Analytics', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-analytics',
'order' => 30,
),
array(
'title' => __( 'Marketing', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-marketing',
'order' => 40,
'is_top_level' => true,
'title' => __( 'Marketing', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-marketing',
'order' => 40,
),
array(
'title' => __( 'Settings', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-settings',
'menuId' => 'secondary',
'order' => 10,
'url' => 'admin.php?page=wc-settings',
'is_top_level' => true,
'title' => __( 'Settings', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-settings',
'menuId' => 'secondary',
'order' => 10,
'url' => 'admin.php?page=wc-settings',
),
array(
'title' => __( 'Tools', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-tools',
'menuId' => 'secondary',
'order' => 30,
'is_top_level' => true,
'title' => __( 'Tools', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-tools',
'menuId' => 'secondary',
'order' => 30,
),
);
}
@ -140,31 +134,49 @@ class CoreMenu {
continue;
}
$wca_items[] = array_merge(
$path = isset( $page['path'] ) ? $page['path'] : null;
$item = array_merge(
array(
'id' => 'wc_admin-' . $page['path'],
'url' => $page['path'],
'id' => 'wc_admin-' . $path,
'url' => $path,
'title' => $page['title'][0],
'capability' => $page['capability'],
'capability' => isset( $page['capability'] ) ? $page['capability'] : 'manage_woocommerce',
),
$page['nav_args']
);
// Don't allow top-level items to be added to the primary menu.
if ( ! isset( $item['parent'] ) || 'woocommerce' === $item['parent'] ) {
$item['menuId'] = 'plugins';
}
$wca_items[] = $item;
}
$home_item = array();
if ( defined( '\Automattic\WooCommerce\Admin\Features\AnalyticsDashboard::MENU_SLUG' ) ) {
$home_item = array(
'id' => 'wc_admin-wc-admin&path=/',
'title' => __( 'Home', 'woocommerce-admin' ),
'url' => \Automattic\WooCommerce\Admin\Features\AnalyticsDashboard::MENU_SLUG,
'is_top_level' => true,
'order' => 0,
'id' => 'wc_admin-wc-admin&path=/',
'title' => __( 'Home', 'woocommerce-admin' ),
'url' => \Automattic\WooCommerce\Admin\Features\AnalyticsDashboard::MENU_SLUG,
'order' => 0,
);
}
$customers_item = array();
if ( class_exists( '\Automattic\WooCommerce\Admin\Features\Analytics' ) ) {
$customers_item = array(
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce-admin' ),
'url' => wc_admin_url( '/customers' ),
'order' => 50,
);
}
return array_merge(
array(
$home_item,
$customers_item,
$order_items['all'],
$order_items['new'],
$product_items['all'],
@ -172,13 +184,12 @@ class CoreMenu {
$coupon_items['default'],
// Marketplace category.
array(
'title' => __( 'Marketplace', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-marketplace',
'url' => 'wc-addons',
'menuId' => 'secondary',
'order' => 20,
'is_top_level' => true,
'title' => __( 'Marketplace', 'woocommerce-admin' ),
'capability' => 'manage_woocommerce',
'id' => 'woocommerce-marketplace',
'url' => 'wc-addons',
'menuId' => 'secondary',
'order' => 20,
),
// Tools category.
array(

View File

@ -49,13 +49,6 @@ class Menu {
*/
const CSS_CLASSES = 4;
/**
* Default parent menu
*
* @var string
*/
const DEFAULT_PARENT = 'woocommerce-settings';
/**
* Store menu items.
*
@ -159,28 +152,24 @@ class Menu {
}
$defaults = array(
'id' => '',
'title' => '',
'capability' => 'manage_woocommerce',
'order' => 100,
'migrate' => true,
'menuId' => 'primary',
'isCategory' => true,
'parent' => self::DEFAULT_PARENT,
'is_top_level' => false,
'id' => '',
'title' => '',
'capability' => 'manage_woocommerce',
'order' => 100,
'migrate' => true,
'menuId' => 'primary',
'isCategory' => true,
);
$menu_item = wp_parse_args( $args, $defaults );
$menu_item['title'] = wp_strip_all_tags( wp_specialchars_decode( $menu_item['title'] ) );
unset( $menu_item['url'] );
if ( true === $menu_item['is_top_level'] ) {
if ( ! isset( $menu_item['parent'] ) ) {
$menu_item['parent'] = 'woocommerce';
$menu_item['backButtonLabel'] = __(
'WooCommerce Home',
'woocommerce-admin'
);
} else {
$menu_item['parent'] = 'woocommerce' === $menu_item['parent'] ? self::DEFAULT_PARENT : $menu_item['parent'];
}
self::$menu_items[ $menu_item['id'] ] = $menu_item;
@ -222,24 +211,20 @@ class Menu {
}
$defaults = array(
'id' => '',
'title' => '',
'parent' => self::DEFAULT_PARENT,
'capability' => 'manage_woocommerce',
'url' => '',
'order' => 100,
'migrate' => true,
'menuId' => 'primary',
'is_top_level' => false,
'id' => '',
'title' => '',
'capability' => 'manage_woocommerce',
'url' => '',
'order' => 100,
'migrate' => true,
'menuId' => 'primary',
);
$menu_item = wp_parse_args( $args, $defaults );
$menu_item['title'] = wp_strip_all_tags( wp_specialchars_decode( $menu_item['title'] ) );
$menu_item['url'] = self::get_callback_url( $menu_item['url'] );
if ( true === $menu_item['is_top_level'] ) {
if ( ! isset( $menu_item['parent'] ) ) {
$menu_item['parent'] = 'woocommerce';
} else {
$menu_item['parent'] = 'woocommerce' === $menu_item['parent'] ? self::DEFAULT_PARENT : $menu_item['parent'];
}
self::$menu_items[ $menu_item['id'] ] = $menu_item;
@ -260,7 +245,6 @@ class Menu {
* 'capability' => (string) Capability to view this menu item.
* 'url' => (string) URL or callback to be used. Required.
* 'migrate' => (bool) Whether or not to hide the item in the wp admin menu.
* 'menuId' => (string) The ID of the menu to add the item to.
* 'order' => (int) Menu item order.
* ).
*/
@ -272,8 +256,7 @@ class Menu {
$item_args = array_merge(
$args,
array(
'menuId' => 'plugins',
'is_top_level' => ! isset( $args['parent'] ),
'menuId' => 'plugins',
)
);
self::add_item( $item_args );
@ -289,7 +272,6 @@ class Menu {
* 'capability' => (string) Capability to view this menu item.
* 'url' => (string) URL or callback to be used. Required.
* 'migrate' => (bool) Whether or not to hide the item in the wp admin menu.
* 'menuId' => (string) The ID of the menu to add the category to.
* 'order' => (int) Menu item order.
* ).
*/
@ -301,57 +283,12 @@ class Menu {
$category_args = array_merge(
$args,
array(
'menuId' => 'plugins',
'is_top_level' => ! isset( $args['parent'] ),
'menuId' => 'plugins',
)
);
self::add_category( $category_args );
}
/**
* Adds a post type as a menu category.
*
* @param string $post_type Post type.
* @param array $args Array of menu item args.
*/
public static function add_post_type_category( $post_type, $args = array() ) {
$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object ) {
return;
}
self::add_category(
array_merge(
array(
'title' => esc_attr( $post_type_object->labels->menu_name ),
'capability' => $post_type_object->cap->edit_posts,
'id' => $post_type,
'is_top_level' => true,
),
$args
)
);
self::add_item(
array(
'parent' => $post_type,
'title' => esc_attr( $post_type_object->labels->all_items ),
'capability' => $post_type_object->cap->edit_posts,
'id' => "{$post_type}-all-items",
'url' => "edit.php?post_type={$post_type}",
)
);
self::add_item(
array(
'parent' => $post_type,
'title' => esc_attr( $post_type_object->labels->add_new ),
'capability' => $post_type_object->cap->create_posts,
'id' => "{$post_type}-add-new",
'url' => "post-new.php?post_type={$post_type}",
)
);
}
/**
* Get menu item templates for a given post type.
*
@ -412,7 +349,11 @@ class Menu {
$items = CoreMenu::get_items();
foreach ( $items as $item ) {
self::add_item( $item );
if ( isset( $item['is_category'] ) && $item['is_category'] ) {
self::add_category( $item );
} else {
self::add_item( $item );
}
}
}