* Add URLs to categories to assist migrating items

* Migrate items when URL matches callback

* Check if nav is enabled before adding extra marketing menu

* Unset URLs on categories after migration
This commit is contained in:
Joshua T Flowers 2020-10-15 16:28:03 -04:00 committed by GitHub
parent 016f4cc2d9
commit 59e7681207
6 changed files with 77 additions and 62 deletions

View File

@ -113,15 +113,14 @@ class Analytics {
*/
public function register_pages() {
$overview_page = array(
'id' => 'woocommerce-analytics',
'title' => __( 'Analytics', 'woocommerce-admin' ),
'path' => '/analytics/overview',
'icon' => 'dashicons-chart-bar',
'position' => 56, // After WooCommerce & Product menu items.
'id' => 'woocommerce-analytics',
'title' => __( 'Analytics', 'woocommerce-admin' ),
'path' => '/analytics/overview',
'icon' => 'dashicons-chart-bar',
'position' => 56, // After WooCommerce & Product menu items.
'is_category' => true,
'is_top_level' => true,
);
if ( Loader::is_feature_enabled( 'navigation' ) ) {
unset( $overview_page['path'] );
}
$report_pages = array(
$overview_page,
@ -186,10 +185,11 @@ class Analytics {
'path' => '/analytics/stock',
) : null,
array(
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce-admin' ),
'parent' => 'woocommerce',
'path' => '/customers',
'id' => 'woocommerce-analytics-customers',
'title' => __( 'Customers', 'woocommerce-admin' ),
'parent' => 'woocommerce',
'path' => '/customers',
'is_top_level' => true,
),
array(
'id' => 'woocommerce-analytics-settings',

View File

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

View File

@ -69,15 +69,17 @@ class Marketing {
* 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
);
if ( ! Loader::is_feature_enabled( 'navigation' ) ) {
add_menu_page(
__( 'Marketing', 'woocommerce-admin' ),
__( 'Marketing', 'woocommerce-admin' ),
'manage_woocommerce',
'woocommerce-marketing',
null,
'dashicons-megaphone',
58
);
}
PageController::get_instance()->connect_page(
[

View File

@ -129,6 +129,7 @@ class CoreMenu {
'id' => 'settings',
'menuId' => 'secondary',
'order' => 10,
'url' => 'admin.php?page=wc-settings',
'is_top_level' => true,
)
);

View File

@ -167,9 +167,7 @@ class Menu {
);
$menu_item = wp_parse_args( $args, $defaults );
$menu_item['title'] = wp_strip_all_tags( wp_specialchars_decode( $menu_item['title'] ) );
if ( isset( $menu_item['url'] ) ) {
$menu_item['url'] = self::get_callback_url( $menu_item['url'] );
}
unset( $menu_item['url'] );
if ( true === $menu_item['is_top_level'] ) {
$menu_item['parent'] = 'woocommerce';
@ -297,8 +295,14 @@ class Menu {
foreach ( $submenu as $parent_key => $parent ) {
foreach ( $parent as $key => $menu_item ) {
if (
isset( self::$callbacks[ $menu_item[ self::CALLBACK ] ] ) &&
self::$callbacks[ $menu_item[ self::CALLBACK ] ]
(
isset( self::$callbacks[ $menu_item[ self::CALLBACK ] ] ) &&
self::$callbacks[ $menu_item[ self::CALLBACK ] ]
) ||
(
isset( self::$callbacks[ self::get_callback_url( $menu_item[ self::CALLBACK ] ) ] ) &&
self::$callbacks[ self::get_callback_url( $menu_item[ self::CALLBACK ] ) ]
)
) {
// Disable phpcs since we need to override submenu classes.
// Note that `phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited` does not work to disable this check.

View File

@ -429,8 +429,6 @@ class PageController {
$options['path'] = self::PAGE_ROOT . '&path=' . $options['path'];
}
$navigation_enabled = Loader::is_feature_enabled( 'navigation' );
if ( is_null( $options['parent'] ) ) {
add_menu_page(
$options['title'],
@ -441,22 +439,6 @@ class PageController {
$options['icon'],
$options['position']
);
if ( $navigation_enabled ) {
$category_options = array(
'id' => $options['id'],
'title' => $options['title'],
'capability' => $options['capability'],
'url' => $options['path'],
'is_top_level' => true,
);
// If there is no path option, remove url because its a parent category item.
if ( 'wc-admin&path=' === $options['path'] ) {
unset( $category_options['url'] );
}
\Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_category( $category_options );
}
} else {
$parent_path = $this->get_path_from_id( $options['parent'] );
// @todo check for null path.
@ -468,25 +450,50 @@ class PageController {
$options['path'],
array( __CLASS__, 'page_wrapper' )
);
if ( $navigation_enabled ) {
$top_level_ids = array( 'woocommerce-home', 'woocommerce-analytics-customers' );
\Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_item(
array(
'id' => $options['id'],
'parent' => $options['parent'],
'title' => $options['title'],
'capability' => $options['capability'],
'url' => $options['path'],
'is_top_level' => in_array( $options['id'], $top_level_ids, true ),
)
);
}
}
self::add_nav_item( $options );
$this->connect_page( $options );
}
/**
* Add the item to the WooCommerce Navigation menu.
*
* @param array $options {
* Array describing the page.
*
* @type string id Id to reference the page.
* @type string title Page title. Used in menus and breadcrumbs.
* @type string|null parent Parent ID. Null for new top level page.
* @type string path Path for this page, full path in app context; ex /analytics/report
* @type string capability Capability needed to access the page.
* @type string icon Icon. Dashicons helper class, base64-encoded SVG, or 'none'.
* @type int position Menu item position.
* }
*/
public static function add_nav_item( $options ) {
$navigation_enabled = Loader::is_feature_enabled( 'navigation' );
if ( ! $navigation_enabled ) {
return;
}
$item_options = array(
'id' => $options['id'],
'parent' => $options['parent'],
'title' => $options['title'],
'capability' => $options['capability'],
'url' => $options['path'],
'is_top_level' => isset( $options['is_top_level'] ) && $options['is_top_level'],
);
if ( isset( $options['is_category'] ) && $options['is_category'] ) {
\Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_category( $item_options );
} else {
\Automattic\WooCommerce\Admin\Features\Navigation\Menu::add_item( $item_options );
}
}
/**
* Set up a div for the app to render into.
*/